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 "st_public.h"
37 #include "st_context.h"
38 #include "st_cb_fbo.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_inlines.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
= CALLOC_STRUCT(st_framebuffer
);
54 _mesa_initialize_framebuffer(&stfb
->Base
, visual
);
57 /* fake frontbuffer */
58 /* XXX allocation should only happen in the unusual case
59 it's actually needed */
60 struct gl_renderbuffer
*rb
61 = st_new_renderbuffer_fb(colorFormat
);
62 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
65 if (visual
->doubleBufferMode
) {
66 struct gl_renderbuffer
*rb
67 = st_new_renderbuffer_fb(colorFormat
);
68 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
71 if (visual
->depthBits
== 24 && visual
->stencilBits
== 8) {
72 /* combined depth/stencil buffer */
73 struct gl_renderbuffer
*depthStencilRb
74 = st_new_renderbuffer_fb(depthFormat
);
75 /* note: bind RB to two attachment points */
76 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
77 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
80 /* separate depth and/or stencil */
82 if (visual
->depthBits
== 32) {
83 /* 32-bit depth buffer */
84 struct gl_renderbuffer
*depthRb
85 = st_new_renderbuffer_fb(depthFormat
);
86 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
88 else if (visual
->depthBits
== 24) {
89 /* 24-bit depth buffer, ignore stencil bits */
90 struct gl_renderbuffer
*depthRb
91 = st_new_renderbuffer_fb(depthFormat
);
92 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
94 else if (visual
->depthBits
> 0) {
95 /* 16-bit depth buffer */
96 struct gl_renderbuffer
*depthRb
97 = st_new_renderbuffer_fb(depthFormat
);
98 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
101 if (visual
->stencilBits
> 0) {
103 struct gl_renderbuffer
*stencilRb
104 = st_new_renderbuffer_fb(stencilFormat
);
105 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
109 if (visual
->accumRedBits
> 0) {
110 /* 16-bit/channel accum */
111 struct gl_renderbuffer
*accumRb
112 = st_new_renderbuffer_fb(DEFAULT_ACCUM_PIPE_FORMAT
);
113 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
116 stfb
->Base
.Initialized
= GL_TRUE
;
117 stfb
->InitWidth
= width
;
118 stfb
->InitHeight
= height
;
119 stfb
->Private
= private;
125 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
126 uint width
, uint height
)
128 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
129 GET_CURRENT_CONTEXT(ctx
);
131 if (stfb
->InitWidth
== 0 && stfb
->InitHeight
== 0) {
132 /* didn't have a valid size until now */
133 stfb
->InitWidth
= width
;
134 stfb
->InitHeight
= height
;
135 if (ctx
->Viewport
.Width
<= 1) {
136 /* set context's initial viewport/scissor size */
137 _mesa_set_viewport(ctx
, 0, 0, width
, height
);
138 _mesa_set_scissor(ctx
, 0, 0, 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_unreference_framebuffer((struct gl_framebuffer
**) stfb
);
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 static const GLuint invalid_size
= 9999999;
168 struct st_renderbuffer
*strb
;
169 GLuint width
, height
, i
;
171 assert(surfIndex
< BUFFER_COUNT
);
173 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
176 /* replace the renderbuffer's surface/texture pointers */
177 pipe_surface_reference( &strb
->surface
, surf
);
178 pipe_texture_reference( &strb
->texture
, surf
->texture
);
180 /* update renderbuffer's width/height */
181 strb
->Base
.Width
= surf
->width
;
182 strb
->Base
.Height
= surf
->height
;
184 /* Try to update the framebuffer's width/height from the renderbuffer
185 * sizes. Before we start drawing, all the rbs _should_ be the same size.
187 width
= height
= invalid_size
;
188 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
189 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
190 if (width
== invalid_size
) {
191 width
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
;
192 height
= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
;
194 else if (width
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Width
||
195 height
!= stfb
->Base
.Attachment
[i
].Renderbuffer
->Height
) {
196 /* inconsistant renderbuffer sizes, bail out */
202 if (width
!= invalid_size
) {
203 /* OK, the renderbuffers are of a consistant size, so update the
204 * parent framebuffer's size.
206 stfb
->Base
.Width
= width
;
207 stfb
->Base
.Height
= height
;
214 * Return the pipe_surface for the given renderbuffer.
216 struct pipe_surface
*
217 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
)
219 struct st_renderbuffer
*strb
;
221 assert(surfIndex
<= ST_SURFACE_DEPTH
);
223 /* sanity checks, ST tokens should match Mesa tokens */
224 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
225 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
227 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
229 return strb
->surface
;
233 struct pipe_texture
*
234 st_get_framebuffer_texture(struct st_framebuffer
*stfb
, uint surfIndex
)
236 struct st_renderbuffer
*strb
;
238 assert(surfIndex
<= ST_SURFACE_DEPTH
);
240 /* sanity checks, ST tokens should match Mesa tokens */
241 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
242 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
244 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
246 return strb
->texture
;
251 * This function is to be called prior to SwapBuffers on the given
252 * framebuffer. It checks if the current context is bound to the framebuffer
253 * and flushes rendering if needed.
256 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
258 GET_CURRENT_CONTEXT(ctx
);
260 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
262 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_SWAPBUFFERS
,
264 ctx
->st
->frontbuffer_status
= FRONT_STATUS_COPY_OF_BACK
;
270 * Quick hack - allows the winsys to inform the driver that surface
271 * states are now undefined after a glXSwapBuffers or similar.
274 st_notify_swapbuffers_complete(struct st_framebuffer
*stfb
)
276 GET_CURRENT_CONTEXT(ctx
);
278 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
279 struct st_renderbuffer
*strb
;
282 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
283 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
284 strb
= st_renderbuffer(stfb
->Base
.Attachment
[i
].Renderbuffer
);
285 strb
->surface
->status
= PIPE_SURFACE_STATUS_UNDEFINED
;
292 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
294 return stfb
->Private
;