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