mesa: replace VP/FP/ATIfs _Enabled flags with helper functions
[mesa.git] / src / mesa / main / state.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file state.c
28 * State management.
29 *
30 * This file manages recalculation of derived values in struct gl_context.
31 */
32
33
34 #include "glheader.h"
35 #include "mtypes.h"
36 #include "arrayobj.h"
37 #include "context.h"
38 #include "debug.h"
39 #include "macros.h"
40 #include "ffvertex_prog.h"
41 #include "framebuffer.h"
42 #include "light.h"
43 #include "matrix.h"
44 #include "pixel.h"
45 #include "program/program.h"
46 #include "program/prog_parameter.h"
47 #include "shaderobj.h"
48 #include "state.h"
49 #include "stencil.h"
50 #include "texenvprogram.h"
51 #include "texobj.h"
52 #include "texstate.h"
53 #include "varray.h"
54 #include "vbo/vbo_context.h"
55 #include "viewport.h"
56 #include "blend.h"
57
58
59 /**
60 * Update the ctx->*Program._Current pointers to point to the
61 * current/active programs.
62 *
63 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
64 * programs or programs derived from fixed-function state.
65 *
66 * This function needs to be called after texture state validation in case
67 * we're generating a fragment program from fixed-function texture state.
68 *
69 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
70 * or fragment program is being used.
71 */
72 static GLbitfield
73 update_program(struct gl_context *ctx)
74 {
75 struct gl_program *vsProg =
76 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
77 struct gl_program *tcsProg =
78 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
79 struct gl_program *tesProg =
80 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
81 struct gl_program *gsProg =
82 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
83 struct gl_program *fsProg =
84 ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
85 struct gl_program *csProg =
86 ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
87 const struct gl_program *prevVP = ctx->VertexProgram._Current;
88 const struct gl_program *prevFP = ctx->FragmentProgram._Current;
89 const struct gl_program *prevGP = ctx->GeometryProgram._Current;
90 const struct gl_program *prevTCP = ctx->TessCtrlProgram._Current;
91 const struct gl_program *prevTEP = ctx->TessEvalProgram._Current;
92 const struct gl_program *prevCP = ctx->ComputeProgram._Current;
93 GLbitfield new_state = 0x0;
94
95 /*
96 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
97 * pointers to the programs that should be used for rendering. If either
98 * is NULL, use fixed-function code paths.
99 *
100 * These programs may come from several sources. The priority is as
101 * follows:
102 * 1. OpenGL 2.0/ARB vertex/fragment shaders
103 * 2. ARB/NV vertex/fragment programs
104 * 3. ATI fragment shader
105 * 4. Programs derived from fixed-function state.
106 *
107 * Note: it's possible for a vertex shader to get used with a fragment
108 * program (and vice versa) here, but in practice that shouldn't ever
109 * come up, or matter.
110 */
111
112 if (fsProg) {
113 /* Use GLSL fragment shader */
114 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, fsProg);
115 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
116 NULL);
117 }
118 else if (_mesa_arb_fragment_program_enabled(ctx)) {
119 /* Use user-defined fragment program */
120 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
121 ctx->FragmentProgram.Current);
122 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
123 NULL);
124 }
125 else if (_mesa_ati_fragment_shader_enabled(ctx) &&
126 ctx->ATIFragmentShader.Current->Program) {
127 /* Use the enabled ATI fragment shader's associated program */
128 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
129 ctx->ATIFragmentShader.Current->Program);
130 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
131 NULL);
132 }
133 else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
134 /* Use fragment program generated from fixed-function state */
135 struct gl_shader_program *f = _mesa_get_fixed_func_fragment_program(ctx);
136
137 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
138 f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program);
139 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
140 f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program);
141 }
142 else {
143 /* No fragment program */
144 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, NULL);
145 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
146 NULL);
147 }
148
149 if (gsProg) {
150 /* Use GLSL geometry shader */
151 _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, gsProg);
152 } else {
153 /* No geometry program */
154 _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, NULL);
155 }
156
157 if (tesProg) {
158 /* Use GLSL tessellation evaluation shader */
159 _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, tesProg);
160 }
161 else {
162 /* No tessellation evaluation program */
163 _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, NULL);
164 }
165
166 if (tcsProg) {
167 /* Use GLSL tessellation control shader */
168 _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, tcsProg);
169 }
170 else {
171 /* No tessellation control program */
172 _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, NULL);
173 }
174
175 /* Examine vertex program after fragment program as
176 * _mesa_get_fixed_func_vertex_program() needs to know active
177 * fragprog inputs.
178 */
179 if (vsProg) {
180 /* Use GLSL vertex shader */
181 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, vsProg);
182 }
183 else if (_mesa_arb_vertex_program_enabled(ctx)) {
184 /* Use user-defined vertex program */
185 _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
186 ctx->VertexProgram.Current);
187 }
188 else if (ctx->VertexProgram._MaintainTnlProgram) {
189 /* Use vertex program generated from fixed-function state */
190 _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
191 _mesa_get_fixed_func_vertex_program(ctx));
192 _mesa_reference_program(ctx, &ctx->VertexProgram._TnlProgram,
193 ctx->VertexProgram._Current);
194 }
195 else {
196 /* no vertex program */
197 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, NULL);
198 }
199
200 if (csProg) {
201 /* Use GLSL compute shader */
202 _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, csProg);
203 } else {
204 /* no compute program */
205 _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, NULL);
206 }
207
208 /* Let the driver know what's happening:
209 */
210 if (ctx->FragmentProgram._Current != prevFP ||
211 ctx->VertexProgram._Current != prevVP ||
212 ctx->GeometryProgram._Current != prevGP ||
213 ctx->TessEvalProgram._Current != prevTEP ||
214 ctx->TessCtrlProgram._Current != prevTCP ||
215 ctx->ComputeProgram._Current != prevCP)
216 new_state |= _NEW_PROGRAM;
217
218 return new_state;
219 }
220
221
222 /**
223 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
224 */
225 static GLbitfield
226 update_program_constants(struct gl_context *ctx)
227 {
228 GLbitfield new_state = 0x0;
229
230 if (ctx->FragmentProgram._Current) {
231 const struct gl_program_parameter_list *params =
232 ctx->FragmentProgram._Current->Parameters;
233 if (params && params->StateFlags & ctx->NewState) {
234 new_state |= _NEW_PROGRAM_CONSTANTS;
235 }
236 }
237
238 /* Don't handle tessellation and geometry shaders here. They don't use
239 * any state constants.
240 */
241
242 if (ctx->VertexProgram._Current) {
243 const struct gl_program_parameter_list *params =
244 ctx->VertexProgram._Current->Parameters;
245 if (params && params->StateFlags & ctx->NewState) {
246 new_state |= _NEW_PROGRAM_CONSTANTS;
247 }
248 }
249
250 return new_state;
251 }
252
253
254 /**
255 * Compute derived GL state.
256 * If __struct gl_contextRec::NewState is non-zero then this function \b must
257 * be called before rendering anything.
258 *
259 * Calls dd_function_table::UpdateState to perform any internal state
260 * management necessary.
261 *
262 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
263 * _mesa_update_buffer_bounds(),
264 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
265 */
266 void
267 _mesa_update_state_locked( struct gl_context *ctx )
268 {
269 GLbitfield new_state = ctx->NewState;
270 GLbitfield prog_flags = _NEW_PROGRAM;
271 GLbitfield new_prog_state = 0x0;
272 const GLbitfield computed_states = ~(_NEW_CURRENT_ATTRIB | _NEW_LINE);
273
274 /* we can skip a bunch of state validation checks if the dirty
275 * state matches one or more bits in 'computed_states'.
276 */
277 if ((new_state & computed_states) == 0)
278 goto out;
279
280 if (MESA_VERBOSE & VERBOSE_STATE)
281 _mesa_print_state("_mesa_update_state", new_state);
282
283 /* Determine which state flags effect vertex/fragment program state */
284 if (ctx->FragmentProgram._MaintainTexEnvProgram) {
285 prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE_OBJECT | _NEW_FOG |
286 _NEW_VARYING_VP_INPUTS | _NEW_LIGHT | _NEW_POINT |
287 _NEW_RENDERMODE | _NEW_PROGRAM | _NEW_FRAG_CLAMP |
288 _NEW_COLOR | _NEW_TEXTURE_STATE);
289 }
290 if (ctx->VertexProgram._MaintainTnlProgram) {
291 prog_flags |= (_NEW_VARYING_VP_INPUTS | _NEW_TEXTURE_OBJECT |
292 _NEW_TEXTURE_MATRIX | _NEW_TRANSFORM | _NEW_POINT |
293 _NEW_FOG | _NEW_LIGHT | _NEW_TEXTURE_STATE |
294 _MESA_NEW_NEED_EYE_COORDS);
295 }
296
297 /*
298 * Now update derived state info
299 */
300 if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
301 _mesa_update_modelview_project( ctx, new_state );
302
303 if (new_state & _NEW_TEXTURE_MATRIX)
304 _mesa_update_texture_matrices(ctx);
305
306 if (new_state & (_NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE | _NEW_PROGRAM))
307 _mesa_update_texture_state(ctx);
308
309 if (new_state & _NEW_BUFFERS)
310 _mesa_update_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer);
311
312 if (new_state & _NEW_LIGHT)
313 _mesa_update_lighting( ctx );
314
315 if (new_state & _NEW_PIXEL)
316 _mesa_update_pixel( ctx );
317
318 /* ctx->_NeedEyeCoords is now up to date.
319 *
320 * If the truth value of this variable has changed, update for the
321 * new lighting space and recompute the positions of lights and the
322 * normal transform.
323 *
324 * If the lighting space hasn't changed, may still need to recompute
325 * light positions & normal transforms for other reasons.
326 */
327 if (new_state & _MESA_NEW_NEED_EYE_COORDS)
328 _mesa_update_tnl_spaces( ctx, new_state );
329
330 if (new_state & prog_flags) {
331 /* When we generate programs from fixed-function vertex/fragment state
332 * this call may generate/bind a new program. If so, we need to
333 * propogate the _NEW_PROGRAM flag to the driver.
334 */
335 new_prog_state |= update_program( ctx );
336 }
337
338 if (new_state & _NEW_ARRAY)
339 _mesa_update_vao_client_arrays(ctx, ctx->Array.VAO);
340
341 out:
342 new_prog_state |= update_program_constants(ctx);
343
344 ctx->NewState |= new_prog_state;
345 vbo_exec_invalidate_state(ctx);
346
347 /*
348 * Give the driver a chance to act upon the new_state flags.
349 * The driver might plug in different span functions, for example.
350 * Also, this is where the driver can invalidate the state of any
351 * active modules (such as swrast_setup, swrast, tnl, etc).
352 */
353 ctx->Driver.UpdateState(ctx);
354 ctx->NewState = 0;
355 ctx->Array.VAO->NewArrays = 0x0;
356 }
357
358
359 /* This is the usual entrypoint for state updates:
360 */
361 void
362 _mesa_update_state( struct gl_context *ctx )
363 {
364 _mesa_lock_context_textures(ctx);
365 _mesa_update_state_locked(ctx);
366 _mesa_unlock_context_textures(ctx);
367 }
368
369
370
371
372 /**
373 * Want to figure out which fragment program inputs are actually
374 * constant/current values from ctx->Current. These should be
375 * referenced as a tracked state variable rather than a fragment
376 * program input, to save the overhead of putting a constant value in
377 * every submitted vertex, transferring it to hardware, interpolating
378 * it across the triangle, etc...
379 *
380 * When there is a VP bound, just use vp->outputs. But when we're
381 * generating vp from fixed function state, basically want to
382 * calculate:
383 *
384 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
385 * potential_vp_outputs )
386 *
387 * Where potential_vp_outputs is calculated by looking at enabled
388 * texgen, etc.
389 *
390 * The generated fragment program should then only declare inputs that
391 * may vary or otherwise differ from the ctx->Current values.
392 * Otherwise, the fp should track them as state values instead.
393 */
394 void
395 _mesa_set_varying_vp_inputs( struct gl_context *ctx,
396 GLbitfield64 varying_inputs )
397 {
398 if (ctx->varying_vp_inputs != varying_inputs) {
399 ctx->varying_vp_inputs = varying_inputs;
400
401 /* Only the fixed-func generated programs need to use the flag
402 * and the fixed-func fragment program uses it only if there is also
403 * a fixed-func vertex program, so this only depends on the latter.
404 *
405 * It's okay to check the VP pointer here, because this is called after
406 * _mesa_update_state in the vbo module. */
407 if (ctx->VertexProgram._TnlProgram ||
408 ctx->FragmentProgram._TexEnvProgram) {
409 ctx->NewState |= _NEW_VARYING_VP_INPUTS;
410 }
411 /*printf("%s %x\n", __func__, varying_inputs);*/
412 }
413 }
414
415
416 /**
417 * Used by drivers to tell core Mesa that the driver is going to
418 * install/ use its own vertex program. In particular, this will
419 * prevent generated fragment programs from using state vars instead
420 * of ordinary varyings/inputs.
421 */
422 void
423 _mesa_set_vp_override(struct gl_context *ctx, GLboolean flag)
424 {
425 if (ctx->VertexProgram._Overriden != flag) {
426 ctx->VertexProgram._Overriden = flag;
427
428 /* Set one of the bits which will trigger fragment program
429 * regeneration:
430 */
431 ctx->NewState |= _NEW_PROGRAM;
432 }
433 }