Use PIPE_FORMAT in state tracker.
[mesa.git] / src / mesa / state_tracker / st_atom.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 "glheader.h"
30 #include "context.h"
31
32 #include "pipe/p_defines.h"
33 #include "st_context.h"
34 #include "st_atom.h"
35 #include "st_program.h"
36
37
38
39 /* This is used to initialize st->atoms[]. We could use this list
40 * directly except for a single atom, st_update_constants, which has a
41 * .dirty value which changes according to the parameters of the
42 * current fragment and vertex programs, and so cannot be a static
43 * value.
44 */
45 static const struct st_tracked_state *atoms[] =
46 {
47 &st_update_framebuffer,
48 &st_update_clear_color,
49 &st_update_depth_stencil,
50 &st_update_clip,
51
52 &st_update_tnl,
53 &st_update_shader,
54
55 &st_update_rasterizer,
56 &st_update_polygon_stipple,
57 &st_update_viewport,
58 &st_update_scissor,
59 &st_update_blend,
60 &st_update_sampler,
61 &st_update_texture,
62 &st_update_vs_constants,
63 &st_update_fs_constants,
64 &st_update_alpha_test
65 };
66
67
68 void st_init_atoms( struct st_context *st )
69 {
70 GLuint i;
71
72 st->atoms = malloc(sizeof(atoms));
73 st->nr_atoms = sizeof(atoms)/sizeof(*atoms);
74 memcpy(st->atoms, atoms, sizeof(atoms));
75
76 /* Patch in a pointer to the dynamic state atom:
77 */
78 for (i = 0; i < st->nr_atoms; i++) {
79 if (st->atoms[i] == &st_update_vs_constants) {
80 st->atoms[i] = &st->constants.tracked_state[PIPE_SHADER_VERTEX];
81 st->atoms[i][0] = st_update_vs_constants;
82 }
83
84 if (st->atoms[i] == &st_update_fs_constants) {
85 st->atoms[i] = &st->constants.tracked_state[PIPE_SHADER_FRAGMENT];
86 st->atoms[i][0] = st_update_fs_constants;
87 }
88 }
89 }
90
91
92 void st_destroy_atoms( struct st_context *st )
93 {
94 if (st->atoms) {
95 free(st->atoms);
96 st->atoms = NULL;
97 }
98 }
99
100
101 /***********************************************************************
102 */
103
104 static GLboolean check_state( const struct st_state_flags *a,
105 const struct st_state_flags *b )
106 {
107 return ((a->mesa & b->mesa) ||
108 (a->st & b->st));
109 }
110
111 static void accumulate_state( struct st_state_flags *a,
112 const struct st_state_flags *b )
113 {
114 a->mesa |= b->mesa;
115 a->st |= b->st;
116 }
117
118
119 static void xor_states( struct st_state_flags *result,
120 const struct st_state_flags *a,
121 const struct st_state_flags *b )
122 {
123 result->mesa = a->mesa ^ b->mesa;
124 result->st = a->st ^ b->st;
125 }
126
127
128 /* Too complex to figure out, just check every time:
129 */
130 static void check_program_state( struct st_context *st )
131 {
132 GLcontext *ctx = st->ctx;
133
134 if (ctx->VertexProgram._Current != &st->vp->Base)
135 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
136
137 if (ctx->FragmentProgram._Current != &st->fp->Base)
138 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
139
140 }
141
142
143 /***********************************************************************
144 * Update all derived state:
145 */
146
147 void st_validate_state( struct st_context *st )
148 {
149 struct st_state_flags *state = &st->dirty;
150 GLuint i;
151
152 check_program_state( st );
153
154 if (state->st == 0)
155 return;
156
157 // _mesa_printf("%s %x/%x\n", __FUNCTION__, state->mesa, state->st);
158
159 if (1) {
160 /* Debug version which enforces various sanity checks on the
161 * state flags which are generated and checked to help ensure
162 * state atoms are ordered correctly in the list.
163 */
164 struct st_state_flags examined, prev;
165 memset(&examined, 0, sizeof(examined));
166 prev = *state;
167
168 for (i = 0; i < st->nr_atoms; i++) {
169 const struct st_tracked_state *atom = st->atoms[i];
170 struct st_state_flags generated;
171
172 // _mesa_printf("atom %s %x/%x\n", atom->name, atom->dirty.mesa, atom->dirty.st);
173
174 if (!(atom->dirty.mesa || atom->dirty.st) ||
175 !atom->update) {
176 _mesa_printf("malformed atom %s\n", atom->name);
177 assert(0);
178 }
179
180 if (check_state(state, &atom->dirty)) {
181 st->atoms[i]->update( st );
182 // _mesa_printf("after: %x\n", atom->dirty.mesa);
183 }
184
185 accumulate_state(&examined, &atom->dirty);
186
187 /* generated = (prev ^ state)
188 * if (examined & generated)
189 * fail;
190 */
191 xor_states(&generated, &prev, state);
192 assert(!check_state(&examined, &generated));
193 prev = *state;
194 }
195 // _mesa_printf("\n");
196
197 }
198 else {
199 const GLuint nr = st->nr_atoms;
200
201 for (i = 0; i < nr; i++) {
202 if (check_state(state, &st->atoms[i]->dirty))
203 st->atoms[i]->update( st );
204 }
205 }
206
207 memset(state, 0, sizeof(*state));
208 }
209
210
211