st/mesa: set default tessellation levels
[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_cb_bitmap.h"
37 #include "st_program.h"
38 #include "st_manager.h"
39
40
41 /**
42 * This is used to initialize st->atoms[].
43 */
44 static const struct st_tracked_state *atoms[] =
45 {
46 &st_update_depth_stencil_alpha,
47 &st_update_clip,
48
49 &st_finalize_textures,
50 &st_update_fp,
51 &st_update_gp,
52 &st_update_tep,
53 &st_update_tcp,
54 &st_update_vp,
55
56 &st_update_rasterizer,
57 &st_update_polygon_stipple,
58 &st_update_viewport,
59 &st_update_scissor,
60 &st_update_blend,
61 &st_update_vertex_texture,
62 &st_update_fragment_texture,
63 &st_update_geometry_texture,
64 &st_update_tessctrl_texture,
65 &st_update_tesseval_texture,
66 &st_update_sampler, /* depends on update_*_texture for swizzle */
67 &st_update_framebuffer,
68 &st_update_msaa,
69 &st_update_sample_shading,
70 &st_update_vs_constants,
71 &st_update_tcs_constants,
72 &st_update_tes_constants,
73 &st_update_gs_constants,
74 &st_update_fs_constants,
75 &st_bind_vs_ubos,
76 &st_bind_tcs_ubos,
77 &st_bind_tes_ubos,
78 &st_bind_fs_ubos,
79 &st_bind_gs_ubos,
80 &st_update_pixel_transfer,
81 &st_update_tess,
82
83 /* this must be done after the vertex program update */
84 &st_update_array
85 };
86
87
88 void st_init_atoms( struct st_context *st )
89 {
90 /* no-op */
91 }
92
93
94 void st_destroy_atoms( struct st_context *st )
95 {
96 /* no-op */
97 }
98
99
100 /***********************************************************************
101 */
102
103 static GLboolean check_state( const struct st_state_flags *a,
104 const struct st_state_flags *b )
105 {
106 return ((a->mesa & b->mesa) ||
107 (a->st & b->st));
108 }
109
110 static void accumulate_state( struct st_state_flags *a,
111 const struct st_state_flags *b )
112 {
113 a->mesa |= b->mesa;
114 a->st |= b->st;
115 }
116
117
118 static void xor_states( struct st_state_flags *result,
119 const struct st_state_flags *a,
120 const struct st_state_flags *b )
121 {
122 result->mesa = a->mesa ^ b->mesa;
123 result->st = a->st ^ b->st;
124 }
125
126
127 /* Too complex to figure out, just check every time:
128 */
129 static void check_program_state( struct st_context *st )
130 {
131 struct gl_context *ctx = st->ctx;
132
133 if (ctx->VertexProgram._Current != &st->vp->Base)
134 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
135
136 if (ctx->FragmentProgram._Current != &st->fp->Base)
137 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
138
139 if (ctx->GeometryProgram._Current != &st->gp->Base)
140 st->dirty.st |= ST_NEW_GEOMETRY_PROGRAM;
141 }
142
143 static void check_attrib_edgeflag(struct st_context *st)
144 {
145 const struct gl_client_array **arrays = st->ctx->Array._DrawArrays;
146 GLboolean vertdata_edgeflags, edgeflag_culls_prims, edgeflags_enabled;
147
148 if (!arrays)
149 return;
150
151 edgeflags_enabled = st->ctx->Polygon.FrontMode != GL_FILL ||
152 st->ctx->Polygon.BackMode != GL_FILL;
153
154 vertdata_edgeflags = edgeflags_enabled &&
155 arrays[VERT_ATTRIB_EDGEFLAG]->StrideB != 0;
156 if (vertdata_edgeflags != st->vertdata_edgeflags) {
157 st->vertdata_edgeflags = vertdata_edgeflags;
158 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
159 }
160
161 edgeflag_culls_prims = edgeflags_enabled && !vertdata_edgeflags &&
162 !st->ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG][0];
163 if (edgeflag_culls_prims != st->edgeflag_culls_prims) {
164 st->edgeflag_culls_prims = edgeflag_culls_prims;
165 st->dirty.st |= ST_NEW_RASTERIZER;
166 }
167 }
168
169
170 /***********************************************************************
171 * Update all derived state:
172 */
173
174 void st_validate_state( struct st_context *st )
175 {
176 struct st_state_flags *state = &st->dirty;
177 GLuint i;
178
179 /* Get Mesa driver state. */
180 st->dirty.st |= st->ctx->NewDriverState;
181 st->ctx->NewDriverState = 0;
182
183 check_attrib_edgeflag(st);
184
185 if (state->mesa)
186 st_flush_bitmap_cache(st);
187
188 check_program_state( st );
189
190 st_manager_validate_framebuffers(st);
191
192 if (state->st == 0)
193 return;
194
195 /*printf("%s %x/%x\n", __func__, state->mesa, state->st);*/
196
197 #ifdef DEBUG
198 if (1) {
199 #else
200 if (0) {
201 #endif
202 /* Debug version which enforces various sanity checks on the
203 * state flags which are generated and checked to help ensure
204 * state atoms are ordered correctly in the list.
205 */
206 struct st_state_flags examined, prev;
207 memset(&examined, 0, sizeof(examined));
208 prev = *state;
209
210 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
211 const struct st_tracked_state *atom = atoms[i];
212 struct st_state_flags generated;
213
214 /*printf("atom %s %x/%x\n", atom->name, atom->dirty.mesa, atom->dirty.st);*/
215
216 if (!(atom->dirty.mesa || atom->dirty.st) ||
217 !atom->update) {
218 printf("malformed atom %s\n", atom->name);
219 assert(0);
220 }
221
222 if (check_state(state, &atom->dirty)) {
223 atoms[i]->update( st );
224 /*printf("after: %x\n", atom->dirty.mesa);*/
225 }
226
227 accumulate_state(&examined, &atom->dirty);
228
229 /* generated = (prev ^ state)
230 * if (examined & generated)
231 * fail;
232 */
233 xor_states(&generated, &prev, state);
234 assert(!check_state(&examined, &generated));
235 prev = *state;
236 }
237 /*printf("\n");*/
238
239 }
240 else {
241 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
242 if (check_state(state, &atoms[i]->dirty))
243 atoms[i]->update( st );
244 }
245 }
246
247 memset(state, 0, sizeof(*state));
248 }
249
250
251