1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "st_context.h"
36 #include "st_cb_fbo.h"
37 #include "st_texture.h"
38 #include "pipe/p_context.h"
39 #include "cso_cache/cso_context.h"
40 #include "util/u_math.h"
41 #include "util/u_inlines.h"
42 #include "util/u_format.h"
46 * When doing GL render to texture, we have to be sure that finalize_texture()
47 * didn't yank out the pipe_resource that we earlier created a surface for.
48 * Check for that here and create a new surface if needed.
51 update_renderbuffer_surface(struct st_context
*st
,
52 struct st_renderbuffer
*strb
)
54 struct pipe_context
*pipe
= st
->pipe
;
55 struct pipe_resource
*resource
= strb
->rtt
->pt
;
56 int rtt_width
= strb
->Base
.Width
;
57 int rtt_height
= strb
->Base
.Height
;
58 enum pipe_format format
= st
->ctx
->Color
.sRGBEnabled
? resource
->format
: util_format_linear(resource
->format
);
61 strb
->surface
->format
!= format
||
62 strb
->surface
->texture
!= resource
||
63 strb
->surface
->width
!= rtt_width
||
64 strb
->surface
->height
!= rtt_height
) {
66 /* find matching mipmap level size */
67 for (level
= 0; level
<= resource
->last_level
; level
++) {
68 if (u_minify(resource
->width0
, level
) == rtt_width
&&
69 u_minify(resource
->height0
, level
) == rtt_height
) {
70 struct pipe_surface surf_tmpl
;
71 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
72 surf_tmpl
.format
= format
;
73 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
74 surf_tmpl
.u
.tex
.level
= level
;
75 surf_tmpl
.u
.tex
.first_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
76 surf_tmpl
.u
.tex
.last_layer
= strb
->rtt_face
+ strb
->rtt_slice
;
78 pipe_surface_reference(&strb
->surface
, NULL
);
80 strb
->surface
= pipe
->create_surface(pipe
,
84 printf("-- alloc new surface %d x %d into tex %p\n",
85 strb
->surface
->width
, strb
->surface
->height
,
96 * Update framebuffer state (color, depth, stencil, etc. buffers)
99 update_framebuffer_state( struct st_context
*st
)
101 struct pipe_framebuffer_state
*framebuffer
= &st
->state
.framebuffer
;
102 struct gl_framebuffer
*fb
= st
->ctx
->DrawBuffer
;
103 struct st_renderbuffer
*strb
;
106 framebuffer
->width
= fb
->Width
;
107 framebuffer
->height
= fb
->Height
;
109 /*printf("------ fb size %d x %d\n", fb->Width, fb->Height);*/
111 /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
112 * to determine which surfaces to draw to
114 framebuffer
->nr_cbufs
= 0;
115 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
116 strb
= st_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
119 /*printf("--------- framebuffer surface rtt %p\n", strb->rtt);*/
121 /* rendering to a GL texture, may have to update surface */
122 update_renderbuffer_surface(st
, strb
);
126 pipe_surface_reference(&framebuffer
->cbufs
[framebuffer
->nr_cbufs
],
128 framebuffer
->nr_cbufs
++;
130 strb
->defined
= GL_TRUE
; /* we'll be drawing something */
133 for (i
= framebuffer
->nr_cbufs
; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
134 pipe_surface_reference(&framebuffer
->cbufs
[i
], NULL
);
138 * Depth/Stencil renderbuffer/surface.
140 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
142 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
144 /* rendering to a GL texture, may have to update surface */
145 update_renderbuffer_surface(st
, strb
);
147 pipe_surface_reference(&framebuffer
->zsbuf
, strb
->surface
);
150 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
152 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
153 assert(strb
->surface
);
154 pipe_surface_reference(&framebuffer
->zsbuf
, strb
->surface
);
157 pipe_surface_reference(&framebuffer
->zsbuf
, NULL
);
161 /* Make sure the resource binding flags were set properly */
162 for (i
= 0; i
< framebuffer
->nr_cbufs
; i
++) {
163 assert(framebuffer
->cbufs
[i
]->texture
->bind
& PIPE_BIND_RENDER_TARGET
);
165 if (framebuffer
->zsbuf
) {
166 assert(framebuffer
->zsbuf
->texture
->bind
& PIPE_BIND_DEPTH_STENCIL
);
170 cso_set_framebuffer(st
->cso_context
, framebuffer
);
174 const struct st_tracked_state st_update_framebuffer
= {
175 "st_update_framebuffer", /* name */
177 _NEW_BUFFERS
, /* mesa */
178 ST_NEW_FRAMEBUFFER
, /* st */
180 update_framebuffer_state
/* update */