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