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"
43 struct st_framebuffer
*
44 st_create_framebuffer( const __GLcontextModes
*visual
,
45 enum pipe_format colorFormat
,
46 enum pipe_format depthFormat
,
47 enum pipe_format stencilFormat
,
48 uint width
, uint height
,
51 struct st_framebuffer
*stfb
= CALLOC_STRUCT(st_framebuffer
);
53 _mesa_initialize_framebuffer(&stfb
->Base
, visual
);
56 /* fake frontbuffer */
57 /* XXX allocation should only happen in the unusual case
58 it's actually needed */
59 struct gl_renderbuffer
*rb
60 = st_new_renderbuffer_fb(colorFormat
);
61 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
64 if (visual
->doubleBufferMode
) {
65 struct gl_renderbuffer
*rb
66 = st_new_renderbuffer_fb(colorFormat
);
67 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
70 if (visual
->depthBits
== 24 && visual
->stencilBits
== 8) {
71 /* combined depth/stencil buffer */
72 struct gl_renderbuffer
*depthStencilRb
73 = st_new_renderbuffer_fb(depthFormat
);
74 /* note: bind RB to two attachment points */
75 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
76 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
79 /* separate depth and/or stencil */
81 if (visual
->depthBits
== 32) {
82 /* 32-bit depth buffer */
83 struct gl_renderbuffer
*depthRb
84 = st_new_renderbuffer_fb(depthFormat
);
85 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
87 else if (visual
->depthBits
== 24) {
88 /* 24-bit depth buffer, ignore stencil bits */
89 struct gl_renderbuffer
*depthRb
90 = st_new_renderbuffer_fb(depthFormat
);
91 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
93 else if (visual
->depthBits
> 0) {
94 /* 16-bit depth buffer */
95 struct gl_renderbuffer
*depthRb
96 = st_new_renderbuffer_fb(depthFormat
);
97 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
100 if (visual
->stencilBits
> 0) {
102 struct gl_renderbuffer
*stencilRb
103 = st_new_renderbuffer_fb(stencilFormat
);
104 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
108 if (visual
->accumRedBits
> 0) {
109 /* 16-bit/channel accum */
110 struct gl_renderbuffer
*accumRb
111 = st_new_renderbuffer_fb(DEFAULT_ACCUM_PIPE_FORMAT
);
112 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
115 stfb
->Base
.Initialized
= GL_TRUE
;
116 stfb
->InitWidth
= width
;
117 stfb
->InitHeight
= height
;
118 stfb
->Private
= private;
124 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
125 uint width
, uint height
)
127 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
128 GET_CURRENT_CONTEXT(ctx
);
130 if (stfb
->InitWidth
== 0 && stfb
->InitHeight
== 0) {
131 /* didn't have a valid size until now */
132 stfb
->InitWidth
= width
;
133 stfb
->InitHeight
= height
;
134 if (ctx
->Viewport
.Width
<= 1) {
135 /* set context's initial viewport/scissor size */
136 _mesa_set_viewport(ctx
, 0, 0, width
, height
);
137 _mesa_set_scissor(ctx
, 0, 0, width
, height
);
141 _mesa_resize_framebuffer(ctx
, &stfb
->Base
, width
, height
);
143 assert(stfb
->Base
.Width
== width
);
144 assert(stfb
->Base
.Height
== height
);
150 void st_unreference_framebuffer( struct st_framebuffer
**stfb
)
152 _mesa_unreference_framebuffer((struct gl_framebuffer
**) stfb
);
158 * Return the pipe_surface for the given renderbuffer.
160 struct pipe_surface
*
161 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
)
163 struct st_renderbuffer
*strb
;
165 assert(surfIndex
<= ST_SURFACE_DEPTH
);
167 /* sanity checks, ST tokens should match Mesa tokens */
168 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
169 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
171 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
173 return strb
->surface
;
179 * This function is to be called prior to SwapBuffers on the given
180 * framebuffer. It checks if the current context is bound to the framebuffer
181 * and flushes rendering if needed.
184 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
186 GET_CURRENT_CONTEXT(ctx
);
188 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
190 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_SWAPBUFFERS
,
192 ctx
->st
->frontbuffer_status
= FRONT_STATUS_COPY_OF_BACK
;
198 * Quick hack - allows the winsys to inform the driver that surface
199 * states are now undefined after a glXSwapBuffers or similar.
202 st_notify_swapbuffers_complete(struct st_framebuffer
*stfb
)
204 GET_CURRENT_CONTEXT(ctx
);
206 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
207 struct st_renderbuffer
*strb
;
210 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
211 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
212 strb
= st_renderbuffer(stfb
->Base
.Attachment
[i
].Renderbuffer
);
213 strb
->surface
->status
= PIPE_SURFACE_STATUS_UNDEFINED
;
220 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
222 return stfb
->Private
;