gallium: remove some debug assertions in vertex format validation
[mesa.git] / src / mesa / state_tracker / st_framebuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "main/imports.h"
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "st_public.h"
34 #include "st_context.h"
35 #include "st_cb_fbo.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_context.h"
38
39
40 struct st_framebuffer *
41 st_create_framebuffer( const __GLcontextModes *visual,
42 enum pipe_format colorFormat,
43 enum pipe_format depthFormat,
44 enum pipe_format stencilFormat,
45 uint width, uint height,
46 void *private)
47 {
48 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
49 if (stfb) {
50 _mesa_initialize_framebuffer(&stfb->Base, visual);
51
52 {
53 /* fake frontbuffer */
54 /* XXX allocation should only happen in the unusual case
55 it's actually needed */
56 struct gl_renderbuffer *rb
57 = st_new_renderbuffer_fb(colorFormat);
58 _mesa_add_renderbuffer(&stfb->Base, BUFFER_FRONT_LEFT, rb);
59 }
60
61 if (visual->doubleBufferMode) {
62 struct gl_renderbuffer *rb
63 = st_new_renderbuffer_fb(colorFormat);
64 _mesa_add_renderbuffer(&stfb->Base, BUFFER_BACK_LEFT, rb);
65 }
66
67 if (visual->depthBits == 24 && visual->stencilBits == 8) {
68 /* combined depth/stencil buffer */
69 struct gl_renderbuffer *depthStencilRb
70 = st_new_renderbuffer_fb(depthFormat);
71 /* note: bind RB to two attachment points */
72 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthStencilRb);
73 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, depthStencilRb);
74 }
75 else {
76 /* separate depth and/or stencil */
77
78 if (visual->depthBits == 32) {
79 /* 32-bit depth buffer */
80 struct gl_renderbuffer *depthRb
81 = st_new_renderbuffer_fb(depthFormat);
82 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
83 }
84 else if (visual->depthBits == 24) {
85 /* 24-bit depth buffer, ignore stencil bits */
86 struct gl_renderbuffer *depthRb
87 = st_new_renderbuffer_fb(depthFormat);
88 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
89 }
90 else if (visual->depthBits > 0) {
91 /* 16-bit depth buffer */
92 struct gl_renderbuffer *depthRb
93 = st_new_renderbuffer_fb(depthFormat);
94 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
95 }
96
97 if (visual->stencilBits > 0) {
98 /* 8-bit stencil */
99 struct gl_renderbuffer *stencilRb
100 = st_new_renderbuffer_fb(stencilFormat);
101 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, stencilRb);
102 }
103 }
104
105 if (visual->accumRedBits > 0) {
106 /* 16-bit/channel accum */
107 struct gl_renderbuffer *accumRb
108 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM);
109 _mesa_add_renderbuffer(&stfb->Base, BUFFER_ACCUM, accumRb);
110 }
111
112 stfb->Base.Initialized = GL_TRUE;
113 stfb->InitWidth = width;
114 stfb->InitHeight = height;
115 stfb->Private = private;
116 }
117 return stfb;
118 }
119
120
121 void st_resize_framebuffer( struct st_framebuffer *stfb,
122 uint width, uint height )
123 {
124 if (stfb->Base.Width != width || stfb->Base.Height != height) {
125 GET_CURRENT_CONTEXT(ctx);
126 if (ctx) {
127 _mesa_resize_framebuffer(ctx, &stfb->Base, width, height);
128
129 assert(stfb->Base.Width == width);
130 assert(stfb->Base.Height == height);
131 }
132 }
133 }
134
135
136 void st_unreference_framebuffer( struct st_framebuffer **stfb )
137 {
138 _mesa_unreference_framebuffer((struct gl_framebuffer **) stfb);
139 }
140
141
142
143 /**
144 * Return the pipe_surface for the given renderbuffer.
145 */
146 struct pipe_surface *
147 st_get_framebuffer_surface(struct st_framebuffer *stfb, uint surfIndex)
148 {
149 struct st_renderbuffer *strb;
150
151 assert(surfIndex <= ST_SURFACE_DEPTH);
152
153 /* sanity checks, ST tokens should match Mesa tokens */
154 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
155 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
156
157 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
158 if (strb)
159 return strb->surface;
160 return NULL;
161 }
162
163
164 /**
165 * This function is to be called prior to SwapBuffers on the given
166 * framebuffer. It checks if the current context is bound to the framebuffer
167 * and flushes rendering if needed.
168 */
169 void
170 st_notify_swapbuffers(struct st_framebuffer *stfb)
171 {
172 GET_CURRENT_CONTEXT(ctx);
173
174 if (ctx && ctx->DrawBuffer == &stfb->Base) {
175 st_flush( ctx->st,
176 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_SWAPBUFFERS);
177 }
178 }
179
180
181 /**
182 * Quick hack - allows the winsys to inform the driver that surface
183 * states are now undefined after a glXSwapBuffers or similar.
184 */
185 void
186 st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
187 {
188 GET_CURRENT_CONTEXT(ctx);
189
190 if (ctx && ctx->DrawBuffer == &stfb->Base) {
191 struct st_renderbuffer *strb;
192 int i;
193
194 for (i = 0; i < BUFFER_COUNT; i++) {
195 if (stfb->Base.Attachment[i].Renderbuffer) {
196 strb = st_renderbuffer(stfb->Base.Attachment[i].Renderbuffer);
197 strb->surface->status = PIPE_SURFACE_STATUS_UNDEFINED;
198 }
199 }
200 }
201 }
202
203
204 void *st_framebuffer_private( struct st_framebuffer *stfb )
205 {
206 return stfb->Private;
207 }
208