1 /**************************************************************************
3 * Copyright 2003 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 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/buffers.h"
31 #include "main/context.h"
32 #include "main/framebuffer.h"
33 #include "main/renderbuffer.h"
34 #include "st_context.h"
35 #include "st_cb_fbo.h"
36 #include "st_public.h"
37 #include "pipe/p_defines.h"
40 struct st_framebuffer
*
41 st_create_framebuffer( const __GLcontextModes
*visual
,
42 enum pipe_format colorFormat
,
43 enum pipe_format depthFormat
,
44 enum pipe_format stencilFormat
,
45 uint width
, uint height
,
48 struct st_framebuffer
*stfb
= ST_CALLOC_STRUCT(st_framebuffer
);
50 int samples
= st_get_msaa();
53 if (visual
->sampleBuffers
)
54 samples
= visual
->samples
;
56 _mesa_initialize_framebuffer(&stfb
->Base
, visual
);
58 if (visual
->doubleBufferMode
) {
59 struct gl_renderbuffer
*rb
60 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
61 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
64 /* Only allocate front buffer right now if we're single buffered.
65 * If double-buffered, allocate front buffer on demand later.
66 * See check_create_front_buffers() and st_set_framebuffer_surface().
68 struct gl_renderbuffer
*rb
69 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
70 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
73 if (depthFormat
== stencilFormat
&& depthFormat
!= PIPE_FORMAT_NONE
) {
74 /* combined depth/stencil buffer */
75 struct gl_renderbuffer
*depthStencilRb
76 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
77 /* note: bind RB to two attachment points */
78 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
79 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
82 /* separate depth and/or stencil */
84 if (visual
->depthBits
== 32) {
85 /* 32-bit depth buffer */
86 struct gl_renderbuffer
*depthRb
87 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
88 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
90 else if (visual
->depthBits
== 24) {
91 /* 24-bit depth buffer, ignore stencil bits */
92 struct gl_renderbuffer
*depthRb
93 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
94 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
96 else if (visual
->depthBits
> 0) {
97 /* 16-bit depth buffer */
98 struct gl_renderbuffer
*depthRb
99 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
100 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
103 if (visual
->stencilBits
> 0) {
105 struct gl_renderbuffer
*stencilRb
106 = st_new_renderbuffer_fb(stencilFormat
, samples
, FALSE
);
107 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
111 if (visual
->accumRedBits
> 0) {
112 /* 16-bit/channel accum */
113 /* TODO: query the pipe screen for accumulation buffer format support */
114 struct gl_renderbuffer
*accumRb
115 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM
, 0, TRUE
);
116 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
119 for (i
= 0; i
< visual
->numAuxBuffers
; i
++) {
120 struct gl_renderbuffer
*aux
121 = st_new_renderbuffer_fb(colorFormat
, 0, FALSE
);
122 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_AUX0
+ i
, aux
);
125 stfb
->Base
.Initialized
= GL_TRUE
;
126 stfb
->InitWidth
= width
;
127 stfb
->InitHeight
= height
;
128 stfb
->Private
= private;
134 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
135 uint width
, uint height
)
137 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
138 GET_CURRENT_CONTEXT(ctx
);
140 _mesa_check_init_viewport(ctx
, width
, height
);
142 _mesa_resize_framebuffer(ctx
, &stfb
->Base
, width
, height
);
144 assert(stfb
->Base
.Width
== width
);
145 assert(stfb
->Base
.Height
== height
);
151 void st_unreference_framebuffer( struct st_framebuffer
*stfb
)
153 _mesa_reference_framebuffer((struct gl_framebuffer
**) &stfb
, NULL
);
159 * Set/replace a framebuffer surface.
160 * The user of the state tracker can use this instead of
161 * st_resize_framebuffer() to provide new surfaces when a window is resized.
164 st_set_framebuffer_surface(struct st_framebuffer
*stfb
,
165 uint surfIndex
, struct pipe_surface
*surf
)
167 GET_CURRENT_CONTEXT(ctx
);
168 static const GLuint invalid_size
= 9999999;
169 struct st_renderbuffer
*strb
;
170 GLuint width
, height
, i
;
172 assert(surfIndex
< BUFFER_COUNT
);
174 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
177 if (surfIndex
== ST_SURFACE_FRONT_LEFT
) {
178 /* Delayed creation when the window system supplies a fake front buffer */
179 struct st_renderbuffer
*strb_back
180 = st_renderbuffer(stfb
->Base
.Attachment
[ST_SURFACE_BACK_LEFT
].Renderbuffer
);
181 struct gl_renderbuffer
*rb
182 = st_new_renderbuffer_fb(surf
->format
, strb_back
->Base
.NumSamples
, FALSE
);
183 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
184 strb
= st_renderbuffer(rb
);
191 /* replace the renderbuffer's surface/texture pointers */
192 pipe_surface_reference( &strb
->surface
, surf
);
193 pipe_texture_reference( &strb
->texture
, surf
->texture
);
196 /* If ctx isn't set, we've likely not made current yet.
197 * But when we do, we need to start setting this dirty bit
198 * to ensure the renderbuffer attachements are up-to-date
199 * via update_framebuffer.
201 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
204 /* update renderbuffer's width/height */
205 strb
->Base
.Width
= surf
->width
;
206 strb
->Base
.Height
= surf
->height
;
208 /* Try to update the framebuffer's width/height from the renderbuffer
209 * sizes. Before we start drawing, all the rbs _should_ be the same size.
211 width
= height
= invalid_size
;
212 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
213 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
214 if (width
== invalid_size
) {
215 width
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
;
216 height
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
;
218 else if (width
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
||
219 height
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
) {
220 /* inconsistant renderbuffer sizes, bail out */
226 if (width
!= invalid_size
) {
227 /* OK, the renderbuffers are of a consistant size, so update the
228 * parent framebuffer's size.
230 stfb
->Base
.Width
= width
;
231 stfb
->Base
.Height
= height
;
238 * Return the pipe_surface for the given renderbuffer.
241 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_surface
**surface
)
243 struct st_renderbuffer
*strb
;
245 assert(surfIndex
<= ST_SURFACE_DEPTH
);
247 /* sanity checks, ST tokens should match Mesa tokens */
248 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
249 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
251 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
253 *surface
= strb
->surface
;
262 st_get_framebuffer_texture(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_texture
**texture
)
264 struct st_renderbuffer
*strb
;
266 assert(surfIndex
<= ST_SURFACE_DEPTH
);
268 /* sanity checks, ST tokens should match Mesa tokens */
269 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
270 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
272 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
274 *texture
= strb
->texture
;
283 * This function is to be called prior to SwapBuffers on the given
284 * framebuffer. It checks if the current context is bound to the framebuffer
285 * and flushes rendering if needed.
288 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
290 GET_CURRENT_CONTEXT(ctx
);
292 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
294 PIPE_FLUSH_RENDER_CACHE
|
295 PIPE_FLUSH_SWAPBUFFERS
|
298 if (st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
))
299 ctx
->st
->frontbuffer_status
= FRONT_STATUS_COPY_OF_BACK
;
305 * Swap the front/back color buffers. Exchange the front/back pointers
306 * and update some derived state.
307 * No need to call st_notify_swapbuffers() first.
309 * For a single-buffered framebuffer, no swap occurs, but we still return
310 * the pointer(s) to the front color buffer(s).
312 * \param front_left returns pointer to front-left renderbuffer after swap
313 * \param front_right returns pointer to front-right renderbuffer after swap
316 st_swapbuffers(struct st_framebuffer
*stfb
,
317 struct pipe_surface
**front_left
,
318 struct pipe_surface
**front_right
)
320 struct gl_framebuffer
*fb
= &stfb
->Base
;
322 GET_CURRENT_CONTEXT(ctx
);
324 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
326 PIPE_FLUSH_RENDER_CACHE
|
327 PIPE_FLUSH_SWAPBUFFERS
|
332 if (!fb
->Visual
.doubleBufferMode
) {
333 /* single buffer mode - return pointers to front surfaces */
335 struct st_renderbuffer
*strb
=
336 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
337 *front_left
= strb
->surface
;
340 struct st_renderbuffer
*strb
=
341 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
342 *front_right
= strb
? strb
->surface
: NULL
;
347 /* swap left buffers */
348 if (fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
&&
349 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
) {
350 struct gl_renderbuffer
*rbTemp
;
351 rbTemp
= fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
352 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
=
353 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
354 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
= rbTemp
;
356 struct st_renderbuffer
*strb
=
357 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
358 *front_left
= strb
->surface
;
360 /* mark back buffer contents as undefined */
362 struct st_renderbuffer
*back
=
363 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
364 back
->defined
= GL_FALSE
;
368 /* no front buffer, display the back buffer */
370 struct st_renderbuffer
*strb
=
371 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
372 *front_left
= strb
->surface
;
376 /* swap right buffers (for stereo) */
377 if (fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
&&
378 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
) {
379 struct gl_renderbuffer
*rbTemp
;
380 rbTemp
= fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
;
381 fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
=
382 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
;
383 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
= rbTemp
;
385 struct st_renderbuffer
*strb
=
386 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
387 *front_right
= strb
->surface
;
389 /* mark back buffer contents as undefined */
391 struct st_renderbuffer
*back
=
392 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
393 back
->defined
= GL_FALSE
;
397 /* no front right buffer, display back right buffer (if exists) */
399 struct st_renderbuffer
*strb
=
400 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
401 *front_right
= strb
? strb
->surface
: NULL
;
405 /* Update the _ColorDrawBuffers[] array and _ColorReadBuffer pointer */
406 _mesa_update_framebuffer(ctx
);
408 /* Make sure we draw into the new back surface */
409 st_invalidate_state(ctx
, _NEW_BUFFERS
);
413 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
415 return stfb
->Private
;
418 void st_get_framebuffer_dimensions( struct st_framebuffer
*stfb
,
422 *width
= stfb
->Base
.Width
;
423 *height
= stfb
->Base
.Height
;