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/matrix.h"
34 #include "main/renderbuffer.h"
35 #include "main/scissor.h"
36 #include "main/viewport.h"
37 #include "st_context.h"
38 #include "st_cb_fbo.h"
39 #include "st_public.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_context.h"
44 struct st_framebuffer
*
45 st_create_framebuffer( const __GLcontextModes
*visual
,
46 enum pipe_format colorFormat
,
47 enum pipe_format depthFormat
,
48 enum pipe_format stencilFormat
,
49 uint width
, uint height
,
52 struct st_framebuffer
*stfb
= ST_CALLOC_STRUCT(st_framebuffer
);
54 int samples
= st_get_msaa();
57 if (visual
->sampleBuffers
)
58 samples
= visual
->samples
;
60 _mesa_initialize_framebuffer(&stfb
->Base
, visual
);
62 if (visual
->doubleBufferMode
) {
63 struct gl_renderbuffer
*rb
64 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
65 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
68 /* Only allocate front buffer right now if we're single buffered.
69 * If double-buffered, allocate front buffer on demand later.
70 * See check_create_front_buffers() and st_set_framebuffer_surface().
72 struct gl_renderbuffer
*rb
73 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
74 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
77 if (depthFormat
== stencilFormat
&& depthFormat
!= PIPE_FORMAT_NONE
) {
78 /* combined depth/stencil buffer */
79 struct gl_renderbuffer
*depthStencilRb
80 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
81 /* note: bind RB to two attachment points */
82 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
83 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
86 /* separate depth and/or stencil */
88 if (visual
->depthBits
== 32) {
89 /* 32-bit depth buffer */
90 struct gl_renderbuffer
*depthRb
91 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
92 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
94 else if (visual
->depthBits
== 24) {
95 /* 24-bit depth buffer, ignore stencil bits */
96 struct gl_renderbuffer
*depthRb
97 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
98 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
100 else if (visual
->depthBits
> 0) {
101 /* 16-bit depth buffer */
102 struct gl_renderbuffer
*depthRb
103 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
104 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
107 if (visual
->stencilBits
> 0) {
109 struct gl_renderbuffer
*stencilRb
110 = st_new_renderbuffer_fb(stencilFormat
, samples
, FALSE
);
111 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
115 if (visual
->accumRedBits
> 0) {
116 /* 16-bit/channel accum */
117 /* TODO: query the pipe screen for accumulation buffer format support */
118 struct gl_renderbuffer
*accumRb
119 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM
, 0, TRUE
);
120 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
123 for (i
= 0; i
< visual
->numAuxBuffers
; i
++) {
124 struct gl_renderbuffer
*aux
125 = st_new_renderbuffer_fb(colorFormat
, 0, FALSE
);
126 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_AUX0
+ i
, aux
);
129 stfb
->Base
.Initialized
= GL_TRUE
;
130 stfb
->InitWidth
= width
;
131 stfb
->InitHeight
= height
;
132 stfb
->Private
= private;
138 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
139 uint width
, uint height
)
141 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
142 GET_CURRENT_CONTEXT(ctx
);
144 _mesa_check_init_viewport(ctx
, width
, height
);
146 _mesa_resize_framebuffer(ctx
, &stfb
->Base
, width
, height
);
148 assert(stfb
->Base
.Width
== width
);
149 assert(stfb
->Base
.Height
== height
);
155 void st_unreference_framebuffer( struct st_framebuffer
*stfb
)
157 _mesa_reference_framebuffer((struct gl_framebuffer
**) &stfb
, NULL
);
163 * Set/replace a framebuffer surface.
164 * The user of the state tracker can use this instead of
165 * st_resize_framebuffer() to provide new surfaces when a window is resized.
168 st_set_framebuffer_surface(struct st_framebuffer
*stfb
,
169 uint surfIndex
, struct pipe_surface
*surf
)
171 GET_CURRENT_CONTEXT(ctx
);
172 static const GLuint invalid_size
= 9999999;
173 struct st_renderbuffer
*strb
;
174 GLuint width
, height
, i
;
176 assert(surfIndex
< BUFFER_COUNT
);
178 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
181 if (surfIndex
== ST_SURFACE_FRONT_LEFT
) {
182 /* Delayed creation when the window system supplies a fake front buffer */
183 struct st_renderbuffer
*strb_back
184 = st_renderbuffer(stfb
->Base
.Attachment
[ST_SURFACE_BACK_LEFT
].Renderbuffer
);
185 struct gl_renderbuffer
*rb
186 = st_new_renderbuffer_fb(surf
->format
, strb_back
->Base
.NumSamples
, FALSE
);
187 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
188 strb
= st_renderbuffer(rb
);
195 /* replace the renderbuffer's surface/texture pointers */
196 pipe_surface_reference( &strb
->surface
, surf
);
197 pipe_texture_reference( &strb
->texture
, surf
->texture
);
200 /* If ctx isn't set, we've likely not made current yet.
201 * But when we do, we need to start setting this dirty bit
202 * to ensure the renderbuffer attachements are up-to-date
203 * via update_framebuffer.
205 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
208 /* update renderbuffer's width/height */
209 strb
->Base
.Width
= surf
->width
;
210 strb
->Base
.Height
= surf
->height
;
212 /* Try to update the framebuffer's width/height from the renderbuffer
213 * sizes. Before we start drawing, all the rbs _should_ be the same size.
215 width
= height
= invalid_size
;
216 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
217 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
218 if (width
== invalid_size
) {
219 width
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
;
220 height
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
;
222 else if (width
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
||
223 height
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
) {
224 /* inconsistant renderbuffer sizes, bail out */
230 if (width
!= invalid_size
) {
231 /* OK, the renderbuffers are of a consistant size, so update the
232 * parent framebuffer's size.
234 stfb
->Base
.Width
= width
;
235 stfb
->Base
.Height
= height
;
242 * Return the pipe_surface for the given renderbuffer.
245 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_surface
**surface
)
247 struct st_renderbuffer
*strb
;
249 assert(surfIndex
<= ST_SURFACE_DEPTH
);
251 /* sanity checks, ST tokens should match Mesa tokens */
252 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
253 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
255 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
257 *surface
= strb
->surface
;
266 st_get_framebuffer_texture(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_texture
**texture
)
268 struct st_renderbuffer
*strb
;
270 assert(surfIndex
<= ST_SURFACE_DEPTH
);
272 /* sanity checks, ST tokens should match Mesa tokens */
273 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
274 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
276 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
278 *texture
= strb
->texture
;
287 * This function is to be called prior to SwapBuffers on the given
288 * framebuffer. It checks if the current context is bound to the framebuffer
289 * and flushes rendering if needed.
292 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
294 GET_CURRENT_CONTEXT(ctx
);
296 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
298 PIPE_FLUSH_RENDER_CACHE
|
299 PIPE_FLUSH_SWAPBUFFERS
|
302 if (st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
))
303 ctx
->st
->frontbuffer_status
= FRONT_STATUS_COPY_OF_BACK
;
309 * Swap the front/back color buffers. Exchange the front/back pointers
310 * and update some derived state.
311 * No need to call st_notify_swapbuffers() first.
313 * For a single-buffered framebuffer, no swap occurs, but we still return
314 * the pointer(s) to the front color buffer(s).
316 * \param front_left returns pointer to front-left renderbuffer after swap
317 * \param front_right returns pointer to front-right renderbuffer after swap
320 st_swapbuffers(struct st_framebuffer
*stfb
,
321 struct pipe_surface
**front_left
,
322 struct pipe_surface
**front_right
)
324 struct gl_framebuffer
*fb
= &stfb
->Base
;
326 GET_CURRENT_CONTEXT(ctx
);
328 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
330 PIPE_FLUSH_RENDER_CACHE
|
331 PIPE_FLUSH_SWAPBUFFERS
|
336 if (!fb
->Visual
.doubleBufferMode
) {
337 /* single buffer mode - return pointers to front surfaces */
339 struct st_renderbuffer
*strb
=
340 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
341 *front_left
= strb
->surface
;
344 struct st_renderbuffer
*strb
=
345 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
346 *front_right
= strb
? strb
->surface
: NULL
;
351 /* swap left buffers */
352 if (fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
&&
353 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
) {
354 struct gl_renderbuffer
*rbTemp
;
355 rbTemp
= fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
356 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
=
357 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
358 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
= rbTemp
;
360 struct st_renderbuffer
*strb
=
361 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
362 *front_left
= strb
->surface
;
364 /* mark back buffer contents as undefined */
366 struct st_renderbuffer
*back
=
367 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
368 back
->defined
= GL_FALSE
;
372 /* no front buffer, display the back buffer */
374 struct st_renderbuffer
*strb
=
375 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
376 *front_left
= strb
->surface
;
380 /* swap right buffers (for stereo) */
381 if (fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
&&
382 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
) {
383 struct gl_renderbuffer
*rbTemp
;
384 rbTemp
= fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
;
385 fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
=
386 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
;
387 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
= rbTemp
;
389 struct st_renderbuffer
*strb
=
390 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
391 *front_right
= strb
->surface
;
393 /* mark back buffer contents as undefined */
395 struct st_renderbuffer
*back
=
396 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
397 back
->defined
= GL_FALSE
;
401 /* no front right buffer, display back right buffer (if exists) */
403 struct st_renderbuffer
*strb
=
404 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
405 *front_right
= strb
? strb
->surface
: NULL
;
409 /* Update the _ColorDrawBuffers[] array and _ColorReadBuffer pointer */
410 _mesa_update_framebuffer(ctx
);
412 /* Make sure we draw into the new back surface */
413 st_invalidate_state(ctx
, _NEW_BUFFERS
);
417 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
419 return stfb
->Private
;
422 void st_get_framebuffer_dimensions( struct st_framebuffer
*stfb
,
426 *width
= stfb
->Base
.Width
;
427 *height
= stfb
->Base
.Height
;