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