Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[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 "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
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_shader,
50
51 &st_update_rasterizer,
52 &st_update_polygon_stipple,
53 &st_update_viewport,
54 &st_update_scissor,
55 &st_update_blend,
56 &st_update_sampler,
57 &st_update_texture,
58 &st_update_framebuffer,
59 &st_update_vs_constants,
60 &st_update_fs_constants,
61 &st_update_pixel_transfer
62 };
63
64
65 void st_init_atoms( struct st_context *st )
66 {
67 /* no-op */
68 }
69
70
71 void st_destroy_atoms( struct st_context *st )
72 {
73 /* no-op */
74 }
75
76
77 /***********************************************************************
78 */
79
80 static GLboolean check_state( const struct st_state_flags *a,
81 const struct st_state_flags *b )
82 {
83 return ((a->mesa & b->mesa) ||
84 (a->st & b->st));
85 }
86
87 static void accumulate_state( struct st_state_flags *a,
88 const struct st_state_flags *b )
89 {
90 a->mesa |= b->mesa;
91 a->st |= b->st;
92 }
93
94
95 static void xor_states( struct st_state_flags *result,
96 const struct st_state_flags *a,
97 const struct st_state_flags *b )
98 {
99 result->mesa = a->mesa ^ b->mesa;
100 result->st = a->st ^ b->st;
101 }
102
103
104 /* Too complex to figure out, just check every time:
105 */
106 static void check_program_state( struct st_context *st )
107 {
108 GLcontext *ctx = st->ctx;
109
110 if (ctx->VertexProgram._Current != &st->vp->Base)
111 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
112
113 if (ctx->FragmentProgram._Current != &st->fp->Base)
114 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
115
116 }
117
118
119 /***********************************************************************
120 * Update all derived state:
121 */
122
123 void st_validate_state( struct st_context *st )
124 {
125 struct st_state_flags *state = &st->dirty;
126 GLuint i;
127
128 /* The bitmap cache is immune to pixel unpack changes.
129 * Note that GLUT makes several calls to glPixelStore for each
130 * bitmap char it draws so this is an important check.
131 */
132 if (state->mesa & ~_NEW_PACKUNPACK)
133 st_flush_bitmap_cache(st);
134
135 check_program_state( st );
136
137 if (state->st == 0)
138 return;
139
140 /*_mesa_printf("%s %x/%x\n", __FUNCTION__, state->mesa, state->st);*/
141
142 if (1) {
143 /* Debug version which enforces various sanity checks on the
144 * state flags which are generated and checked to help ensure
145 * state atoms are ordered correctly in the list.
146 */
147 struct st_state_flags examined, prev;
148 memset(&examined, 0, sizeof(examined));
149 prev = *state;
150
151 for (i = 0; i < Elements(atoms); i++) {
152 const struct st_tracked_state *atom = atoms[i];
153 struct st_state_flags generated;
154
155 /*_mesa_printf("atom %s %x/%x\n", atom->name, atom->dirty.mesa, atom->dirty.st);*/
156
157 if (!(atom->dirty.mesa || atom->dirty.st) ||
158 !atom->update) {
159 _mesa_printf("malformed atom %s\n", atom->name);
160 assert(0);
161 }
162
163 if (check_state(state, &atom->dirty)) {
164 atoms[i]->update( st );
165 /*_mesa_printf("after: %x\n", atom->dirty.mesa);*/
166 }
167
168 accumulate_state(&examined, &atom->dirty);
169
170 /* generated = (prev ^ state)
171 * if (examined & generated)
172 * fail;
173 */
174 xor_states(&generated, &prev, state);
175 assert(!check_state(&examined, &generated));
176 prev = *state;
177 }
178 /*_mesa_printf("\n");*/
179
180 }
181 else {
182 for (i = 0; i < Elements(atoms); i++) {
183 if (check_state(state, &atoms[i]->dirty))
184 atoms[i]->update( st );
185 }
186 }
187
188 memset(state, 0, sizeof(*state));
189 }
190
191
192