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"
38 #include "util/u_inlines.h"
41 struct st_framebuffer
*
42 st_create_framebuffer( const __GLcontextModes
*visual
,
43 enum pipe_format colorFormat
,
44 enum pipe_format depthFormat
,
45 enum pipe_format stencilFormat
,
46 uint width
, uint height
,
49 struct st_framebuffer
*stfb
= ST_CALLOC_STRUCT(st_framebuffer
);
51 int samples
= st_get_msaa();
54 if (visual
->sampleBuffers
)
55 samples
= visual
->samples
;
57 _mesa_initialize_window_framebuffer(&stfb
->Base
, visual
);
59 if (visual
->doubleBufferMode
) {
60 struct gl_renderbuffer
*rb
61 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
62 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
65 /* Only allocate front buffer right now if we're single buffered.
66 * If double-buffered, allocate front buffer on demand later.
67 * See check_create_front_buffers() and st_set_framebuffer_surface().
69 struct gl_renderbuffer
*rb
70 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
71 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
74 if (depthFormat
== stencilFormat
&& depthFormat
!= PIPE_FORMAT_NONE
) {
75 /* combined depth/stencil buffer */
76 struct gl_renderbuffer
*depthStencilRb
77 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
78 /* note: bind RB to two attachment points */
79 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
80 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
83 /* separate depth and/or stencil */
85 if (visual
->depthBits
== 32) {
86 /* 32-bit depth buffer */
87 struct gl_renderbuffer
*depthRb
88 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
89 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
91 else if (visual
->depthBits
== 24) {
92 /* 24-bit depth buffer, ignore stencil bits */
93 struct gl_renderbuffer
*depthRb
94 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
95 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
97 else if (visual
->depthBits
> 0) {
98 /* 16-bit depth buffer */
99 struct gl_renderbuffer
*depthRb
100 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
101 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
104 if (visual
->stencilBits
> 0) {
106 struct gl_renderbuffer
*stencilRb
107 = st_new_renderbuffer_fb(stencilFormat
, samples
, FALSE
);
108 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
112 if (visual
->accumRedBits
> 0) {
113 /* 16-bit/channel accum */
114 /* TODO: query the pipe screen for accumulation buffer format support */
115 struct gl_renderbuffer
*accumRb
116 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM
, 0, TRUE
);
117 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
120 for (i
= 0; i
< visual
->numAuxBuffers
; i
++) {
121 struct gl_renderbuffer
*aux
122 = st_new_renderbuffer_fb(colorFormat
, 0, FALSE
);
123 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_AUX0
+ i
, aux
);
126 stfb
->Base
.Initialized
= GL_TRUE
;
127 stfb
->InitWidth
= width
;
128 stfb
->InitHeight
= height
;
129 stfb
->Private
= private;
135 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
136 uint width
, uint height
)
138 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
139 GET_CURRENT_CONTEXT(ctx
);
141 _mesa_check_init_viewport(ctx
, width
, height
);
143 _mesa_resize_framebuffer(ctx
, &stfb
->Base
, width
, height
);
145 assert(stfb
->Base
.Width
== width
);
146 assert(stfb
->Base
.Height
== height
);
152 void st_unreference_framebuffer( struct st_framebuffer
*stfb
)
154 _mesa_reference_framebuffer((struct gl_framebuffer
**) &stfb
, NULL
);
160 * Set/replace a framebuffer surface.
161 * The user of the state tracker can use this instead of
162 * st_resize_framebuffer() to provide new surfaces when a window is resized.
165 st_set_framebuffer_surface(struct st_framebuffer
*stfb
,
166 uint surfIndex
, struct pipe_surface
*surf
)
168 GET_CURRENT_CONTEXT(ctx
);
169 static const GLuint invalid_size
= 9999999;
170 struct st_renderbuffer
*strb
;
171 GLuint width
, height
, i
;
173 assert(surfIndex
< BUFFER_COUNT
);
175 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
178 if (surfIndex
== ST_SURFACE_FRONT_LEFT
) {
179 /* Delayed creation when the window system supplies a fake front buffer */
180 struct st_renderbuffer
*strb_back
181 = st_renderbuffer(stfb
->Base
.Attachment
[ST_SURFACE_BACK_LEFT
].Renderbuffer
);
182 struct gl_renderbuffer
*rb
183 = st_new_renderbuffer_fb(surf
->format
, strb_back
->Base
.NumSamples
, FALSE
);
184 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
185 strb
= st_renderbuffer(rb
);
192 /* replace the renderbuffer's surface/texture pointers */
193 pipe_surface_reference( &strb
->surface
, surf
);
194 pipe_texture_reference( &strb
->texture
, surf
->texture
);
197 /* If ctx isn't set, we've likely not made current yet.
198 * But when we do, we need to start setting this dirty bit
199 * to ensure the renderbuffer attachements are up-to-date
200 * via update_framebuffer.
202 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
205 /* update renderbuffer's width/height */
206 strb
->Base
.Width
= surf
->width
;
207 strb
->Base
.Height
= surf
->height
;
209 /* Try to update the framebuffer's width/height from the renderbuffer
210 * sizes. Before we start drawing, all the rbs _should_ be the same size.
212 width
= height
= invalid_size
;
213 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
214 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
215 if (width
== invalid_size
) {
216 width
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
;
217 height
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
;
219 else if (width
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
||
220 height
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
) {
221 /* inconsistant renderbuffer sizes, bail out */
227 if (width
!= invalid_size
) {
228 /* OK, the renderbuffers are of a consistant size, so update the
229 * parent framebuffer's size.
231 stfb
->Base
.Width
= width
;
232 stfb
->Base
.Height
= height
;
239 * Return the pipe_surface for the given renderbuffer.
242 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_surface
**surface
)
244 struct st_renderbuffer
*strb
;
246 assert(surfIndex
<= ST_SURFACE_DEPTH
);
248 /* sanity checks, ST tokens should match Mesa tokens */
249 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
250 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
252 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
254 *surface
= strb
->surface
;
263 st_get_framebuffer_texture(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_texture
**texture
)
265 struct st_renderbuffer
*strb
;
267 assert(surfIndex
<= ST_SURFACE_DEPTH
);
269 /* sanity checks, ST tokens should match Mesa tokens */
270 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
271 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
273 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
275 *texture
= strb
->texture
;
284 * This function is to be called prior to SwapBuffers on the given
285 * framebuffer. It checks if the current context is bound to the framebuffer
286 * and flushes rendering if needed.
289 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
291 GET_CURRENT_CONTEXT(ctx
);
293 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
295 PIPE_FLUSH_RENDER_CACHE
|
296 PIPE_FLUSH_SWAPBUFFERS
|
299 if (st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
))
300 ctx
->st
->frontbuffer_status
= FRONT_STATUS_COPY_OF_BACK
;
306 * Swap the front/back color buffers. Exchange the front/back pointers
307 * and update some derived state.
308 * No need to call st_notify_swapbuffers() first.
310 * For a single-buffered framebuffer, no swap occurs, but we still return
311 * the pointer(s) to the front color buffer(s).
313 * \param front_left returns pointer to front-left renderbuffer after swap
314 * \param front_right returns pointer to front-right renderbuffer after swap
317 st_swapbuffers(struct st_framebuffer
*stfb
,
318 struct pipe_surface
**front_left
,
319 struct pipe_surface
**front_right
)
321 struct gl_framebuffer
*fb
= &stfb
->Base
;
323 GET_CURRENT_CONTEXT(ctx
);
325 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
327 PIPE_FLUSH_RENDER_CACHE
|
328 PIPE_FLUSH_SWAPBUFFERS
|
333 if (!fb
->Visual
.doubleBufferMode
) {
334 /* single buffer mode - return pointers to front surfaces */
336 struct st_renderbuffer
*strb
=
337 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
338 *front_left
= strb
->surface
;
341 struct st_renderbuffer
*strb
=
342 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
343 *front_right
= strb
? strb
->surface
: NULL
;
348 /* swap left buffers */
349 if (fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
&&
350 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
) {
351 struct gl_renderbuffer
*rbTemp
;
352 rbTemp
= fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
353 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
=
354 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
355 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
= rbTemp
;
357 struct st_renderbuffer
*strb
=
358 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
359 *front_left
= strb
->surface
;
361 /* mark back buffer contents as undefined */
363 struct st_renderbuffer
*back
=
364 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
365 back
->defined
= GL_FALSE
;
369 /* no front buffer, display the back buffer */
371 struct st_renderbuffer
*strb
=
372 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
373 *front_left
= strb
->surface
;
377 /* swap right buffers (for stereo) */
378 if (fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
&&
379 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
) {
380 struct gl_renderbuffer
*rbTemp
;
381 rbTemp
= fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
;
382 fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
=
383 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
;
384 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
= rbTemp
;
386 struct st_renderbuffer
*strb
=
387 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
388 *front_right
= strb
->surface
;
390 /* mark back buffer contents as undefined */
392 struct st_renderbuffer
*back
=
393 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
394 back
->defined
= GL_FALSE
;
398 /* no front right buffer, display back right buffer (if exists) */
400 struct st_renderbuffer
*strb
=
401 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
402 *front_right
= strb
? strb
->surface
: NULL
;
406 /* Update the _ColorDrawBuffers[] array and _ColorReadBuffer pointer */
407 _mesa_update_framebuffer(ctx
);
409 /* Make sure we draw into the new back surface */
410 st_invalidate_state(ctx
, _NEW_BUFFERS
);
414 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
416 return stfb
->Private
;
419 void st_get_framebuffer_dimensions( struct st_framebuffer
*stfb
,
423 *width
= stfb
->Base
.Width
;
424 *height
= stfb
->Base
.Height
;