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 "st_public.h"
36 #include "st_context.h"
37 #include "st_cb_fbo.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_context.h"
42 struct st_framebuffer
*
43 st_create_framebuffer( const __GLcontextModes
*visual
,
44 enum pipe_format colorFormat
,
45 enum pipe_format depthFormat
,
46 enum pipe_format stencilFormat
,
47 uint width
, uint height
,
50 struct st_framebuffer
*stfb
= CALLOC_STRUCT(st_framebuffer
);
52 _mesa_initialize_framebuffer(&stfb
->Base
, visual
);
55 /* fake frontbuffer */
56 /* XXX allocation should only happen in the unusual case
57 it's actually needed */
58 struct gl_renderbuffer
*rb
59 = st_new_renderbuffer_fb(colorFormat
);
60 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
63 if (visual
->doubleBufferMode
) {
64 struct gl_renderbuffer
*rb
65 = st_new_renderbuffer_fb(colorFormat
);
66 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
69 if (visual
->depthBits
== 24 && visual
->stencilBits
== 8) {
70 /* combined depth/stencil buffer */
71 struct gl_renderbuffer
*depthStencilRb
72 = st_new_renderbuffer_fb(depthFormat
);
73 /* note: bind RB to two attachment points */
74 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
75 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
78 /* separate depth and/or stencil */
80 if (visual
->depthBits
== 32) {
81 /* 32-bit depth buffer */
82 struct gl_renderbuffer
*depthRb
83 = st_new_renderbuffer_fb(depthFormat
);
84 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
86 else if (visual
->depthBits
== 24) {
87 /* 24-bit depth buffer, ignore stencil bits */
88 struct gl_renderbuffer
*depthRb
89 = st_new_renderbuffer_fb(depthFormat
);
90 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
92 else if (visual
->depthBits
> 0) {
93 /* 16-bit depth buffer */
94 struct gl_renderbuffer
*depthRb
95 = st_new_renderbuffer_fb(depthFormat
);
96 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
99 if (visual
->stencilBits
> 0) {
101 struct gl_renderbuffer
*stencilRb
102 = st_new_renderbuffer_fb(stencilFormat
);
103 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
107 if (visual
->accumRedBits
> 0) {
108 /* 16-bit/channel accum */
109 struct gl_renderbuffer
*accumRb
110 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM
);
111 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
114 stfb
->Base
.Initialized
= GL_TRUE
;
115 stfb
->InitWidth
= width
;
116 stfb
->InitHeight
= height
;
117 stfb
->Private
= private;
123 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
124 uint width
, uint height
)
126 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
127 GET_CURRENT_CONTEXT(ctx
);
129 if (stfb
->InitWidth
== 0 && stfb
->InitHeight
== 0) {
130 /* didn't have a valid size until now */
131 stfb
->InitWidth
= width
;
132 stfb
->InitHeight
= height
;
133 if (ctx
->Viewport
.Width
<= 1) {
134 /* set context's initial viewport/scissor size */
135 _mesa_set_viewport(ctx
, 0, 0, width
, height
);
136 _mesa_set_scissor(ctx
, 0, 0, width
, height
);
140 _mesa_resize_framebuffer(ctx
, &stfb
->Base
, width
, height
);
142 assert(stfb
->Base
.Width
== width
);
143 assert(stfb
->Base
.Height
== height
);
149 void st_unreference_framebuffer( struct st_framebuffer
**stfb
)
151 _mesa_unreference_framebuffer((struct gl_framebuffer
**) stfb
);
157 * Return the pipe_surface for the given renderbuffer.
159 struct pipe_surface
*
160 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
)
162 struct st_renderbuffer
*strb
;
164 assert(surfIndex
<= ST_SURFACE_DEPTH
);
166 /* sanity checks, ST tokens should match Mesa tokens */
167 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
168 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
170 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
172 return strb
->surface
;
178 * This function is to be called prior to SwapBuffers on the given
179 * framebuffer. It checks if the current context is bound to the framebuffer
180 * and flushes rendering if needed.
183 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
185 GET_CURRENT_CONTEXT(ctx
);
187 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
189 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_SWAPBUFFERS
);
195 * Quick hack - allows the winsys to inform the driver that surface
196 * states are now undefined after a glXSwapBuffers or similar.
199 st_notify_swapbuffers_complete(struct st_framebuffer
*stfb
)
201 GET_CURRENT_CONTEXT(ctx
);
203 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
204 struct st_renderbuffer
*strb
;
207 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
208 if (stfb
->Base
.Attachment
[i
].Renderbuffer
) {
209 strb
= st_renderbuffer(stfb
->Base
.Attachment
[i
].Renderbuffer
);
210 strb
->surface
->status
= PIPE_SURFACE_STATUS_UNDEFINED
;
217 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
219 return stfb
->Private
;