st/mesa: unify TCS, TES, GS st_*_program structures
[mesa.git] / src / mesa / state_tracker / st_atom.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 <stdio.h>
30 #include "main/glheader.h"
31 #include "main/context.h"
32
33 #include "pipe/p_defines.h"
34 #include "st_context.h"
35 #include "st_atom.h"
36 #include "st_program.h"
37 #include "st_manager.h"
38
39 typedef void (*update_func_t)(struct st_context *st);
40
41 /* The list state update functions. */
42 static const update_func_t update_functions[] =
43 {
44 #define ST_STATE(FLAG, st_update) st_update,
45 #include "st_atom_list.h"
46 #undef ST_STATE
47 };
48
49
50 void st_init_atoms( struct st_context *st )
51 {
52 STATIC_ASSERT(ARRAY_SIZE(update_functions) <= 64);
53 }
54
55
56 void st_destroy_atoms( struct st_context *st )
57 {
58 /* no-op */
59 }
60
61
62 /* Too complex to figure out, just check every time:
63 */
64 static void check_program_state( struct st_context *st )
65 {
66 struct gl_context *ctx = st->ctx;
67 struct st_vertex_program *old_vp = st->vp;
68 struct st_common_program *old_tcp = st->tcp;
69 struct st_common_program *old_tep = st->tep;
70 struct st_common_program *old_gp = st->gp;
71 struct st_fragment_program *old_fp = st->fp;
72
73 struct gl_program *new_vp = ctx->VertexProgram._Current;
74 struct gl_program *new_tcp = ctx->TessCtrlProgram._Current;
75 struct gl_program *new_tep = ctx->TessEvalProgram._Current;
76 struct gl_program *new_gp = ctx->GeometryProgram._Current;
77 struct gl_program *new_fp = ctx->FragmentProgram._Current;
78 uint64_t dirty = 0;
79
80 /* Flag states used by both new and old shaders to unbind shader resources
81 * properly when transitioning to shaders that don't use them.
82 */
83 if (unlikely(new_vp != &old_vp->Base)) {
84 if (old_vp)
85 dirty |= old_vp->affected_states;
86 if (new_vp)
87 dirty |= ST_NEW_VERTEX_PROGRAM(st, st_vertex_program(new_vp));
88 }
89
90 if (unlikely(new_tcp != &old_tcp->Base)) {
91 if (old_tcp)
92 dirty |= old_tcp->affected_states;
93 if (new_tcp)
94 dirty |= st_common_program(new_tcp)->affected_states;
95 }
96
97 if (unlikely(new_tep != &old_tep->Base)) {
98 if (old_tep)
99 dirty |= old_tep->affected_states;
100 if (new_tep)
101 dirty |= st_common_program(new_tep)->affected_states;
102 }
103
104 if (unlikely(new_gp != &old_gp->Base)) {
105 if (old_gp)
106 dirty |= old_gp->affected_states;
107 if (new_gp)
108 dirty |= st_common_program(new_gp)->affected_states;
109 }
110
111 if (unlikely(new_fp != &old_fp->Base)) {
112 if (old_fp)
113 dirty |= old_fp->affected_states;
114 if (new_fp)
115 dirty |= st_fragment_program(new_fp)->affected_states;
116 }
117
118 st->dirty |= dirty;
119 st->gfx_shaders_may_be_dirty = false;
120 }
121
122 static void check_attrib_edgeflag(struct st_context *st)
123 {
124 const struct gl_vertex_array **arrays = st->ctx->Array._DrawArrays;
125 GLboolean vertdata_edgeflags, edgeflag_culls_prims, edgeflags_enabled;
126 struct gl_program *vp = st->ctx->VertexProgram._Current;
127
128 if (!arrays)
129 return;
130
131 edgeflags_enabled = st->ctx->Polygon.FrontMode != GL_FILL ||
132 st->ctx->Polygon.BackMode != GL_FILL;
133
134 vertdata_edgeflags = edgeflags_enabled &&
135 arrays[VERT_ATTRIB_EDGEFLAG]->StrideB != 0;
136 if (vertdata_edgeflags != st->vertdata_edgeflags) {
137 st->vertdata_edgeflags = vertdata_edgeflags;
138 if (vp)
139 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, st_vertex_program(vp));
140 }
141
142 edgeflag_culls_prims = edgeflags_enabled && !vertdata_edgeflags &&
143 !st->ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG][0];
144 if (edgeflag_culls_prims != st->edgeflag_culls_prims) {
145 st->edgeflag_culls_prims = edgeflag_culls_prims;
146 st->dirty |= ST_NEW_RASTERIZER;
147 }
148 }
149
150
151 /***********************************************************************
152 * Update all derived state:
153 */
154
155 void st_validate_state( struct st_context *st, enum st_pipeline pipeline )
156 {
157 struct gl_context *ctx = st->ctx;
158 uint64_t dirty, pipeline_mask;
159 uint32_t dirty_lo, dirty_hi;
160
161 /* Get Mesa driver state.
162 *
163 * Inactive states are shader states not used by shaders at the moment.
164 */
165 st->dirty |= ctx->NewDriverState & st->active_states & ST_ALL_STATES_MASK;
166 ctx->NewDriverState = 0;
167
168 /* Get pipeline state. */
169 switch (pipeline) {
170 case ST_PIPELINE_RENDER:
171 if (st->ctx->API == API_OPENGL_COMPAT)
172 check_attrib_edgeflag(st);
173
174 check_program_state(st);
175 st_manager_validate_framebuffers(st);
176
177 pipeline_mask = ST_PIPELINE_RENDER_STATE_MASK;
178 break;
179
180 case ST_PIPELINE_CLEAR:
181 st_manager_validate_framebuffers(st);
182 pipeline_mask = ST_PIPELINE_CLEAR_STATE_MASK;
183 break;
184
185 case ST_PIPELINE_UPDATE_FRAMEBUFFER:
186 st_manager_validate_framebuffers(st);
187 pipeline_mask = ST_PIPELINE_UPDATE_FB_STATE_MASK;
188 break;
189
190 case ST_PIPELINE_COMPUTE: {
191 struct st_compute_program *old_cp = st->cp;
192 struct gl_program *new_cp = ctx->ComputeProgram._Current;
193
194 if (new_cp != &old_cp->Base) {
195 if (old_cp)
196 st->dirty |= old_cp->affected_states;
197 assert(new_cp);
198 st->dirty |= st_compute_program(new_cp)->affected_states;
199 }
200
201 st->compute_shader_may_be_dirty = false;
202
203 /*
204 * We add the ST_NEW_FB_STATE bit here as well, because glBindFramebuffer
205 * acts as a barrier that breaks feedback loops between the framebuffer
206 * and textures bound to the framebuffer, even when those textures are
207 * accessed by compute shaders; so we must inform the driver of new
208 * framebuffer state.
209 */
210 pipeline_mask = ST_PIPELINE_COMPUTE_STATE_MASK | ST_NEW_FB_STATE;
211 break;
212 }
213
214 default:
215 unreachable("Invalid pipeline specified");
216 }
217
218 dirty = st->dirty & pipeline_mask;
219 if (!dirty)
220 return;
221
222 dirty_lo = dirty;
223 dirty_hi = dirty >> 32;
224
225 /* Update states.
226 *
227 * Don't use u_bit_scan64, it may be slower on 32-bit.
228 */
229 while (dirty_lo)
230 update_functions[u_bit_scan(&dirty_lo)](st);
231 while (dirty_hi)
232 update_functions[32 + u_bit_scan(&dirty_hi)](st);
233
234 /* Clear the render or compute state bits. */
235 st->dirty &= ~pipeline_mask;
236 }