st/mesa: fix fallout from xfb changes.
[mesa.git] / src / mesa / state_tracker / st_atom.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 <stdio.h>
30 #include "main/glheader.h"
31 #include "main/context.h"
32
33 #include "pipe/p_defines.h"
34 #include "st_context.h"
35 #include "st_atom.h"
36 #include "st_program.h"
37 #include "st_manager.h"
38
39
40 /**
41 * This is used to initialize st->render_atoms[].
42 */
43 static const struct st_tracked_state *render_atoms[] =
44 {
45 &st_update_depth_stencil_alpha,
46 &st_update_clip,
47
48 &st_update_fp,
49 &st_update_gp,
50 &st_update_tep,
51 &st_update_tcp,
52 &st_update_vp,
53
54 &st_update_rasterizer,
55 &st_update_polygon_stipple,
56 &st_update_viewport,
57 &st_update_scissor,
58 &st_update_blend,
59 &st_update_vertex_texture,
60 &st_update_fragment_texture,
61 &st_update_geometry_texture,
62 &st_update_tessctrl_texture,
63 &st_update_tesseval_texture,
64 &st_update_sampler, /* depends on update_*_texture for swizzle */
65 &st_bind_vs_images,
66 &st_bind_tcs_images,
67 &st_bind_tes_images,
68 &st_bind_gs_images,
69 &st_bind_fs_images,
70 &st_update_framebuffer, /* depends on update_*_texture and bind_*_images */
71 &st_update_msaa,
72 &st_update_sample_shading,
73 &st_update_vs_constants,
74 &st_update_tcs_constants,
75 &st_update_tes_constants,
76 &st_update_gs_constants,
77 &st_update_fs_constants,
78 &st_bind_vs_ubos,
79 &st_bind_tcs_ubos,
80 &st_bind_tes_ubos,
81 &st_bind_fs_ubos,
82 &st_bind_gs_ubos,
83 &st_bind_vs_atomics,
84 &st_bind_tcs_atomics,
85 &st_bind_tes_atomics,
86 &st_bind_fs_atomics,
87 &st_bind_gs_atomics,
88 &st_bind_vs_ssbos,
89 &st_bind_tcs_ssbos,
90 &st_bind_tes_ssbos,
91 &st_bind_fs_ssbos,
92 &st_bind_gs_ssbos,
93 &st_update_pixel_transfer,
94 &st_update_tess,
95
96 /* this must be done after the vertex program update */
97 &st_update_array
98 };
99
100
101 /**
102 * This is used to initialize st->compute_atoms[].
103 */
104 static const struct st_tracked_state *compute_atoms[] =
105 {
106 &st_update_cp,
107 &st_update_compute_texture,
108 &st_update_sampler, /* depends on update_compute_texture for swizzle */
109 &st_update_cs_constants,
110 &st_bind_cs_ubos,
111 &st_bind_cs_atomics,
112 &st_bind_cs_ssbos,
113 &st_bind_cs_images,
114 };
115
116
117 void st_init_atoms( struct st_context *st )
118 {
119 /* no-op */
120 }
121
122
123 void st_destroy_atoms( struct st_context *st )
124 {
125 /* no-op */
126 }
127
128
129
130 static bool
131 check_state(const struct st_state_flags *a, const struct st_state_flags *b)
132 {
133 return (a->mesa & b->mesa) || (a->st & b->st);
134 }
135
136
137 static void
138 accumulate_state(struct st_state_flags *a, const struct st_state_flags *b)
139 {
140 a->mesa |= b->mesa;
141 a->st |= b->st;
142 }
143
144
145 static void
146 xor_states(struct st_state_flags *result,
147 const struct st_state_flags *a,
148 const struct st_state_flags *b)
149 {
150 result->mesa = a->mesa ^ b->mesa;
151 result->st = a->st ^ b->st;
152 }
153
154
155 /* Too complex to figure out, just check every time:
156 */
157 static void check_program_state( struct st_context *st )
158 {
159 struct gl_context *ctx = st->ctx;
160
161 if (ctx->VertexProgram._Current != &st->vp->Base)
162 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
163
164 if (ctx->FragmentProgram._Current != &st->fp->Base)
165 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
166
167 if (ctx->GeometryProgram._Current != &st->gp->Base)
168 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
169 }
170
171 static void check_attrib_edgeflag(struct st_context *st)
172 {
173 const struct gl_client_array **arrays = st->ctx->Array._DrawArrays;
174 GLboolean vertdata_edgeflags, edgeflag_culls_prims, edgeflags_enabled;
175
176 if (!arrays)
177 return;
178
179 edgeflags_enabled = st->ctx->Polygon.FrontMode != GL_FILL ||
180 st->ctx->Polygon.BackMode != GL_FILL;
181
182 vertdata_edgeflags = edgeflags_enabled &&
183 arrays[VERT_ATTRIB_EDGEFLAG]->StrideB != 0;
184 if (vertdata_edgeflags != st->vertdata_edgeflags) {
185 st->vertdata_edgeflags = vertdata_edgeflags;
186 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
187 }
188
189 edgeflag_culls_prims = edgeflags_enabled && !vertdata_edgeflags &&
190 !st->ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG][0];
191 if (edgeflag_culls_prims != st->edgeflag_culls_prims) {
192 st->edgeflag_culls_prims = edgeflag_culls_prims;
193 st->dirty.st |= ST_NEW_RASTERIZER;
194 }
195 }
196
197
198 /***********************************************************************
199 * Update all derived state:
200 */
201
202 void st_validate_state( struct st_context *st, enum st_pipeline pipeline )
203 {
204 const struct st_tracked_state **atoms;
205 struct st_state_flags *state;
206 GLuint num_atoms;
207 GLuint i;
208
209 /* Get pipeline state. */
210 switch (pipeline) {
211 case ST_PIPELINE_RENDER:
212 atoms = render_atoms;
213 num_atoms = ARRAY_SIZE(render_atoms);
214 state = &st->dirty;
215 break;
216 case ST_PIPELINE_COMPUTE:
217 atoms = compute_atoms;
218 num_atoms = ARRAY_SIZE(compute_atoms);
219 state = &st->dirty_cp;
220 break;
221 default:
222 unreachable("Invalid pipeline specified");
223 }
224
225 /* Get Mesa driver state. */
226 st->dirty.st |= st->ctx->NewDriverState;
227 st->dirty_cp.st |= st->ctx->NewDriverState;
228 st->ctx->NewDriverState = 0;
229
230 if (pipeline == ST_PIPELINE_RENDER) {
231 check_attrib_edgeflag(st);
232
233 check_program_state(st);
234
235 st_manager_validate_framebuffers(st);
236 }
237
238 if (state->st == 0 && state->mesa == 0)
239 return;
240
241 /*printf("%s %x/%x\n", __func__, state->mesa, state->st);*/
242
243 #ifdef DEBUG
244 if (1) {
245 #else
246 if (0) {
247 #endif
248 /* Debug version which enforces various sanity checks on the
249 * state flags which are generated and checked to help ensure
250 * state atoms are ordered correctly in the list.
251 */
252 struct st_state_flags examined, prev;
253 memset(&examined, 0, sizeof(examined));
254 prev = *state;
255
256 for (i = 0; i < num_atoms; i++) {
257 const struct st_tracked_state *atom = atoms[i];
258 struct st_state_flags generated;
259
260 /*printf("atom %s %x/%x\n", atom->name, atom->dirty.mesa, atom->dirty.st);*/
261
262 if (!(atom->dirty.mesa || atom->dirty.st) ||
263 !atom->update) {
264 printf("malformed atom %s\n", atom->name);
265 assert(0);
266 }
267
268 if (check_state(state, &atom->dirty)) {
269 atoms[i]->update( st );
270 /*printf("after: %x\n", atom->dirty.mesa);*/
271 }
272
273 accumulate_state(&examined, &atom->dirty);
274
275 /* generated = (prev ^ state)
276 * if (examined & generated)
277 * fail;
278 */
279 xor_states(&generated, &prev, state);
280 assert(!check_state(&examined, &generated));
281 prev = *state;
282 }
283 /*printf("\n");*/
284
285 }
286 else {
287 for (i = 0; i < num_atoms; i++) {
288 if (check_state(state, &atoms[i]->dirty))
289 atoms[i]->update( st );
290 }
291 }
292
293 memset(state, 0, sizeof(*state));
294 }