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