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();
56 if (visual
->sampleBuffers
)
57 samples
= visual
->samples
;
59 _mesa_initialize_framebuffer(&stfb
->Base
, visual
);
61 if (visual
->doubleBufferMode
) {
62 struct gl_renderbuffer
*rb
63 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
64 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
67 /* Only allocate front buffer right now if we're single buffered.
68 * If double-buffered, allocate front buffer on demand later.
69 * See check_create_front_buffers() and st_set_framebuffer_surface().
71 struct gl_renderbuffer
*rb
72 = st_new_renderbuffer_fb(colorFormat
, samples
, FALSE
);
73 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
76 if (depthFormat
== stencilFormat
&& depthFormat
!= PIPE_FORMAT_NONE
) {
77 /* combined depth/stencil buffer */
78 struct gl_renderbuffer
*depthStencilRb
79 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
80 /* note: bind RB to two attachment points */
81 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
82 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
85 /* separate depth and/or stencil */
87 if (visual
->depthBits
== 32) {
88 /* 32-bit depth buffer */
89 struct gl_renderbuffer
*depthRb
90 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
91 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
93 else if (visual
->depthBits
== 24) {
94 /* 24-bit depth buffer, ignore stencil bits */
95 struct gl_renderbuffer
*depthRb
96 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
97 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
99 else if (visual
->depthBits
> 0) {
100 /* 16-bit depth buffer */
101 struct gl_renderbuffer
*depthRb
102 = st_new_renderbuffer_fb(depthFormat
, samples
, FALSE
);
103 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
106 if (visual
->stencilBits
> 0) {
108 struct gl_renderbuffer
*stencilRb
109 = st_new_renderbuffer_fb(stencilFormat
, samples
, FALSE
);
110 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
114 if (visual
->accumRedBits
> 0) {
115 /* 16-bit/channel accum */
116 /* TODO: query the pipe screen for accumulation buffer format support */
117 struct gl_renderbuffer
*accumRb
118 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM
, 0, TRUE
);
119 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
122 stfb
->Base
.Initialized
= GL_TRUE
;
123 stfb
->InitWidth
= width
;
124 stfb
->InitHeight
= height
;
125 stfb
->Private
= private;
131 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
132 uint width
, uint height
)
134 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
135 GET_CURRENT_CONTEXT(ctx
);
137 _mesa_check_init_viewport(ctx
, width
, height
);
139 _mesa_resize_framebuffer(ctx
, &stfb
->Base
, width
, height
);
141 assert(stfb
->Base
.Width
== width
);
142 assert(stfb
->Base
.Height
== height
);
148 void st_unreference_framebuffer( struct st_framebuffer
*stfb
)
150 _mesa_reference_framebuffer((struct gl_framebuffer
**) &stfb
, NULL
);
156 * Set/replace a framebuffer surface.
157 * The user of the state tracker can use this instead of
158 * st_resize_framebuffer() to provide new surfaces when a window is resized.
161 st_set_framebuffer_surface(struct st_framebuffer
*stfb
,
162 uint surfIndex
, struct pipe_surface
*surf
)
164 GET_CURRENT_CONTEXT(ctx
);
165 static const GLuint invalid_size
= 9999999;
166 struct st_renderbuffer
*strb
;
167 GLuint width
, height
, i
;
169 assert(surfIndex
< BUFFER_COUNT
);
171 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
174 if (surfIndex
== ST_SURFACE_FRONT_LEFT
) {
175 /* Delayed creation when the window system supplies a fake front buffer */
176 struct st_renderbuffer
*strb_back
177 = st_renderbuffer(stfb
->Base
.Attachment
[ST_SURFACE_BACK_LEFT
].Renderbuffer
);
178 struct gl_renderbuffer
*rb
179 = st_new_renderbuffer_fb(surf
->format
, strb_back
->Base
.NumSamples
, FALSE
);
180 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
181 strb
= st_renderbuffer(rb
);
188 /* replace the renderbuffer's surface/texture pointers */
189 pipe_surface_reference( &strb
->surface
, surf
);
190 pipe_texture_reference( &strb
->texture
, surf
->texture
);
193 /* If ctx isn't set, we've likely not made current yet.
194 * But when we do, we need to start setting this dirty bit
195 * to ensure the renderbuffer attachements are up-to-date
196 * via update_framebuffer.
198 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
201 /* update renderbuffer's width/height */
202 strb
->Base
.Width
= surf
->width
;
203 strb
->Base
.Height
= surf
->height
;
205 /* Try to update the framebuffer's width/height from the renderbuffer
206 * sizes. Before we start drawing, all the rbs _should_ be the same size.
208 width
= height
= invalid_size
;
209 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
210 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
211 if (width
== invalid_size
) {
212 width
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
;
213 height
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
;
215 else if (width
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
||
216 height
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
) {
217 /* inconsistant renderbuffer sizes, bail out */
223 if (width
!= invalid_size
) {
224 /* OK, the renderbuffers are of a consistant size, so update the
225 * parent framebuffer's size.
227 stfb
->Base
.Width
= width
;
228 stfb
->Base
.Height
= height
;
235 * Return the pipe_surface for the given renderbuffer.
238 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_surface
**surface
)
240 struct st_renderbuffer
*strb
;
242 assert(surfIndex
<= ST_SURFACE_DEPTH
);
244 /* sanity checks, ST tokens should match Mesa tokens */
245 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
246 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
248 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
250 *surface
= strb
->surface
;
259 st_get_framebuffer_texture(struct st_framebuffer
*stfb
, uint surfIndex
, struct pipe_texture
**texture
)
261 struct st_renderbuffer
*strb
;
263 assert(surfIndex
<= ST_SURFACE_DEPTH
);
265 /* sanity checks, ST tokens should match Mesa tokens */
266 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
267 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
269 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
271 *texture
= strb
->texture
;
280 * This function is to be called prior to SwapBuffers on the given
281 * framebuffer. It checks if the current context is bound to the framebuffer
282 * and flushes rendering if needed.
285 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
287 GET_CURRENT_CONTEXT(ctx
);
289 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
291 PIPE_FLUSH_RENDER_CACHE
|
292 PIPE_FLUSH_SWAPBUFFERS
|
295 if (st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
))
296 ctx
->st
->frontbuffer_status
= FRONT_STATUS_COPY_OF_BACK
;
302 * Swap the front/back color buffers. Exchange the front/back pointers
303 * and update some derived state.
304 * No need to call st_notify_swapbuffers() first.
306 * For a single-buffered framebuffer, no swap occurs, but we still return
307 * the pointer(s) to the front color buffer(s).
309 * \param front_left returns pointer to front-left renderbuffer after swap
310 * \param front_right returns pointer to front-right renderbuffer after swap
313 st_swapbuffers(struct st_framebuffer
*stfb
,
314 struct pipe_surface
**front_left
,
315 struct pipe_surface
**front_right
)
317 struct gl_framebuffer
*fb
= &stfb
->Base
;
319 GET_CURRENT_CONTEXT(ctx
);
321 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
323 PIPE_FLUSH_RENDER_CACHE
|
324 PIPE_FLUSH_SWAPBUFFERS
|
329 if (!fb
->Visual
.doubleBufferMode
) {
330 /* single buffer mode - return pointers to front surfaces */
332 struct st_renderbuffer
*strb
=
333 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
334 *front_left
= strb
->surface
;
337 struct st_renderbuffer
*strb
=
338 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
339 *front_right
= strb
? strb
->surface
: NULL
;
344 /* swap left buffers */
345 if (fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
&&
346 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
) {
347 struct gl_renderbuffer
*rbTemp
;
348 rbTemp
= fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
349 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
=
350 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
351 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
= rbTemp
;
353 struct st_renderbuffer
*strb
=
354 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
355 *front_left
= strb
->surface
;
357 /* mark back buffer contents as undefined */
359 struct st_renderbuffer
*back
=
360 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
361 back
->defined
= GL_FALSE
;
365 /* no front buffer, display the back buffer */
367 struct st_renderbuffer
*strb
=
368 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
369 *front_left
= strb
->surface
;
373 /* swap right buffers (for stereo) */
374 if (fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
&&
375 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
) {
376 struct gl_renderbuffer
*rbTemp
;
377 rbTemp
= fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
;
378 fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
=
379 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
;
380 fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
= rbTemp
;
382 struct st_renderbuffer
*strb
=
383 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_RIGHT
].Renderbuffer
);
384 *front_right
= strb
->surface
;
386 /* mark back buffer contents as undefined */
388 struct st_renderbuffer
*back
=
389 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
390 back
->defined
= GL_FALSE
;
394 /* no front right buffer, display back right buffer (if exists) */
396 struct st_renderbuffer
*strb
=
397 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_RIGHT
].Renderbuffer
);
398 *front_right
= strb
? strb
->surface
: NULL
;
402 /* Update the _ColorDrawBuffers[] array and _ColorReadBuffer pointer */
403 _mesa_update_framebuffer(ctx
);
405 /* Make sure we draw into the new back surface */
406 st_invalidate_state(ctx
, _NEW_BUFFERS
);
410 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
412 return stfb
->Private
;
415 void st_get_framebuffer_dimensions( struct st_framebuffer
*stfb
,
419 *width
= stfb
->Base
.Width
;
420 *height
= stfb
->Base
.Height
;