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