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