gallium: Use MALLOC().
[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_depth_stencil_alpha,
49 &st_update_clip,
50
51 &st_update_shader,
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_sampler,
59 &st_update_texture,
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 GLuint i;
69
70 st->atoms = malloc(sizeof(atoms));
71 st->nr_atoms = sizeof(atoms)/sizeof(*atoms);
72 memcpy(st->atoms, atoms, sizeof(atoms));
73
74 /* Patch in a pointer to the dynamic state atom:
75 */
76 for (i = 0; i < st->nr_atoms; i++) {
77 if (st->atoms[i] == &st_update_vs_constants) {
78 st->atoms[i] = &st->constants.tracked_state[PIPE_SHADER_VERTEX];
79 st->atoms[i][0] = st_update_vs_constants;
80 }
81
82 if (st->atoms[i] == &st_update_fs_constants) {
83 st->atoms[i] = &st->constants.tracked_state[PIPE_SHADER_FRAGMENT];
84 st->atoms[i][0] = st_update_fs_constants;
85 }
86 }
87 }
88
89
90 void st_destroy_atoms( struct st_context *st )
91 {
92 if (st->atoms) {
93 free(st->atoms);
94 st->atoms = NULL;
95 }
96 }
97
98
99 /***********************************************************************
100 */
101
102 static GLboolean check_state( const struct st_state_flags *a,
103 const struct st_state_flags *b )
104 {
105 return ((a->mesa & b->mesa) ||
106 (a->st & b->st));
107 }
108
109 static void accumulate_state( struct st_state_flags *a,
110 const struct st_state_flags *b )
111 {
112 a->mesa |= b->mesa;
113 a->st |= b->st;
114 }
115
116
117 static void xor_states( struct st_state_flags *result,
118 const struct st_state_flags *a,
119 const struct st_state_flags *b )
120 {
121 result->mesa = a->mesa ^ b->mesa;
122 result->st = a->st ^ b->st;
123 }
124
125
126 /* Too complex to figure out, just check every time:
127 */
128 static void check_program_state( struct st_context *st )
129 {
130 GLcontext *ctx = st->ctx;
131
132 if (ctx->VertexProgram._Current != &st->vp->Base)
133 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
134
135 if (ctx->FragmentProgram._Current != &st->fp->Base)
136 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
137
138 }
139
140
141 /***********************************************************************
142 * Update all derived state:
143 */
144
145 void st_validate_state( struct st_context *st )
146 {
147 struct st_state_flags *state = &st->dirty;
148 GLuint i;
149
150 check_program_state( st );
151
152 if (state->st == 0)
153 return;
154
155 // _mesa_printf("%s %x/%x\n", __FUNCTION__, state->mesa, state->st);
156
157 if (1) {
158 /* Debug version which enforces various sanity checks on the
159 * state flags which are generated and checked to help ensure
160 * state atoms are ordered correctly in the list.
161 */
162 struct st_state_flags examined, prev;
163 memset(&examined, 0, sizeof(examined));
164 prev = *state;
165
166 for (i = 0; i < st->nr_atoms; i++) {
167 const struct st_tracked_state *atom = st->atoms[i];
168 struct st_state_flags generated;
169
170 // _mesa_printf("atom %s %x/%x\n", atom->name, atom->dirty.mesa, atom->dirty.st);
171
172 if (!(atom->dirty.mesa || atom->dirty.st) ||
173 !atom->update) {
174 _mesa_printf("malformed atom %s\n", atom->name);
175 assert(0);
176 }
177
178 if (check_state(state, &atom->dirty)) {
179 st->atoms[i]->update( st );
180 // _mesa_printf("after: %x\n", atom->dirty.mesa);
181 }
182
183 accumulate_state(&examined, &atom->dirty);
184
185 /* generated = (prev ^ state)
186 * if (examined & generated)
187 * fail;
188 */
189 xor_states(&generated, &prev, state);
190 assert(!check_state(&examined, &generated));
191 prev = *state;
192 }
193 // _mesa_printf("\n");
194
195 }
196 else {
197 const GLuint nr = st->nr_atoms;
198
199 for (i = 0; i < nr; i++) {
200 if (check_state(state, &st->atoms[i]->dirty))
201 st->atoms[i]->update( st );
202 }
203 }
204
205 memset(state, 0, sizeof(*state));
206 }
207
208
209