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