glsl_to_tgsi: don't rely on glsl types when visiting tex instructions
[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 "viewport.h"
55 #include "blend.h"
56
57
58 /**
59 * Update the following fields:
60 * ctx->VertexProgram._Enabled
61 * ctx->FragmentProgram._Enabled
62 * ctx->ATIFragmentShader._Enabled
63 * This needs to be done before texture state validation.
64 */
65 static void
66 update_program_enables(struct gl_context *ctx)
67 {
68 /* These _Enabled flags indicate if the user-defined ARB/NV vertex/fragment
69 * program is enabled AND valid. Similarly for ATI fragment shaders.
70 * GLSL shaders not relevant here.
71 */
72 ctx->VertexProgram._Enabled = ctx->VertexProgram.Enabled
73 && ctx->VertexProgram.Current->arb.Instructions;
74 ctx->FragmentProgram._Enabled = ctx->FragmentProgram.Enabled
75 && ctx->FragmentProgram.Current->arb.Instructions;
76 ctx->ATIFragmentShader._Enabled = ctx->ATIFragmentShader.Enabled
77 && ctx->ATIFragmentShader.Current->Instructions[0];
78 }
79
80
81 /**
82 * Update the ctx->*Program._Current pointers to point to the
83 * current/active programs. Then call ctx->Driver.BindProgram() to
84 * tell the driver which programs to use.
85 *
86 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
87 * programs or programs derived from fixed-function state.
88 *
89 * This function needs to be called after texture state validation in case
90 * we're generating a fragment program from fixed-function texture state.
91 *
92 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
93 * or fragment program is being used.
94 */
95 static GLbitfield
96 update_program(struct gl_context *ctx)
97 {
98 struct gl_program *vsProg =
99 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
100 struct gl_program *tcsProg =
101 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
102 struct gl_program *tesProg =
103 ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
104 struct gl_program *gsProg =
105 ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
106 struct gl_program *fsProg =
107 ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
108 struct gl_program *csProg =
109 ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
110 const struct gl_program *prevVP = ctx->VertexProgram._Current;
111 const struct gl_program *prevFP = ctx->FragmentProgram._Current;
112 const struct gl_program *prevGP = ctx->GeometryProgram._Current;
113 const struct gl_program *prevTCP = ctx->TessCtrlProgram._Current;
114 const struct gl_program *prevTEP = ctx->TessEvalProgram._Current;
115 const struct gl_program *prevCP = ctx->ComputeProgram._Current;
116 GLbitfield new_state = 0x0;
117
118 /*
119 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
120 * pointers to the programs that should be used for rendering. If either
121 * is NULL, use fixed-function code paths.
122 *
123 * These programs may come from several sources. The priority is as
124 * follows:
125 * 1. OpenGL 2.0/ARB vertex/fragment shaders
126 * 2. ARB/NV vertex/fragment programs
127 * 3. ATI fragment shader
128 * 4. Programs derived from fixed-function state.
129 *
130 * Note: it's possible for a vertex shader to get used with a fragment
131 * program (and vice versa) here, but in practice that shouldn't ever
132 * come up, or matter.
133 */
134
135 if (fsProg) {
136 /* Use GLSL fragment shader */
137 _mesa_reference_program(ctx, &ctx->_Shader->_CurrentFragmentProgram,
138 fsProg);
139 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, fsProg);
140 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
141 NULL);
142 }
143 else if (ctx->FragmentProgram._Enabled) {
144 /* Use user-defined fragment program */
145 _mesa_reference_program(ctx, &ctx->_Shader->_CurrentFragmentProgram,
146 NULL);
147 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
148 ctx->FragmentProgram.Current);
149 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
150 NULL);
151 }
152 else if (ctx->ATIFragmentShader._Enabled &&
153 ctx->ATIFragmentShader.Current->Program) {
154 /* Use the enabled ATI fragment shader's associated program */
155 _mesa_reference_program(ctx, &ctx->_Shader->_CurrentFragmentProgram,
156 NULL);
157 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
158 ctx->ATIFragmentShader.Current->Program);
159 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
160 NULL);
161 }
162 else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
163 /* Use fragment program generated from fixed-function state */
164 struct gl_shader_program *f = _mesa_get_fixed_func_fragment_program(ctx);
165
166 _mesa_reference_program(ctx, &ctx->_Shader->_CurrentFragmentProgram,
167 f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program);
168 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
169 f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program);
170 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
171 f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program);
172 }
173 else {
174 /* No fragment program */
175 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, NULL);
176 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
177 NULL);
178 }
179
180 if (gsProg) {
181 /* Use GLSL geometry shader */
182 _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, gsProg);
183 } else {
184 /* No geometry program */
185 _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, NULL);
186 }
187
188 if (tesProg) {
189 /* Use GLSL tessellation evaluation shader */
190 _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, tesProg);
191 }
192 else {
193 /* No tessellation evaluation program */
194 _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, NULL);
195 }
196
197 if (tcsProg) {
198 /* Use GLSL tessellation control shader */
199 _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, tcsProg);
200 }
201 else {
202 /* No tessellation control program */
203 _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, NULL);
204 }
205
206 /* Examine vertex program after fragment program as
207 * _mesa_get_fixed_func_vertex_program() needs to know active
208 * fragprog inputs.
209 */
210 if (vsProg) {
211 /* Use GLSL vertex shader */
212 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, vsProg);
213 }
214 else if (ctx->VertexProgram._Enabled) {
215 /* Use user-defined vertex program */
216 _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
217 ctx->VertexProgram.Current);
218 }
219 else if (ctx->VertexProgram._MaintainTnlProgram) {
220 /* Use vertex program generated from fixed-function state */
221 _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
222 _mesa_get_fixed_func_vertex_program(ctx));
223 _mesa_reference_program(ctx, &ctx->VertexProgram._TnlProgram,
224 ctx->VertexProgram._Current);
225 }
226 else {
227 /* no vertex program */
228 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, NULL);
229 }
230
231 if (csProg) {
232 /* Use GLSL compute shader */
233 _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, csProg);
234 } else {
235 /* no compute program */
236 _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, NULL);
237 }
238
239 /* Let the driver know what's happening:
240 */
241 if (ctx->FragmentProgram._Current != prevFP) {
242 new_state |= _NEW_PROGRAM;
243 if (ctx->Driver.BindProgram) {
244 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
245 ctx->FragmentProgram._Current);
246 }
247 }
248
249 if (ctx->GeometryProgram._Current != prevGP) {
250 new_state |= _NEW_PROGRAM;
251 if (ctx->Driver.BindProgram) {
252 ctx->Driver.BindProgram(ctx, GL_GEOMETRY_PROGRAM_NV,
253 ctx->GeometryProgram._Current);
254 }
255 }
256
257 if (ctx->TessEvalProgram._Current != prevTEP) {
258 new_state |= _NEW_PROGRAM;
259 if (ctx->Driver.BindProgram) {
260 ctx->Driver.BindProgram(ctx, GL_TESS_EVALUATION_PROGRAM_NV,
261 ctx->TessEvalProgram._Current);
262 }
263 }
264
265 if (ctx->TessCtrlProgram._Current != prevTCP) {
266 new_state |= _NEW_PROGRAM;
267 if (ctx->Driver.BindProgram) {
268 ctx->Driver.BindProgram(ctx, GL_TESS_CONTROL_PROGRAM_NV,
269 ctx->TessCtrlProgram._Current);
270 }
271 }
272
273 if (ctx->VertexProgram._Current != prevVP) {
274 new_state |= _NEW_PROGRAM;
275 if (ctx->Driver.BindProgram) {
276 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
277 ctx->VertexProgram._Current);
278 }
279 }
280
281 if (ctx->ComputeProgram._Current != prevCP) {
282 new_state |= _NEW_PROGRAM;
283 if (ctx->Driver.BindProgram) {
284 ctx->Driver.BindProgram(ctx, GL_COMPUTE_PROGRAM_NV,
285 ctx->ComputeProgram._Current);
286 }
287 }
288
289 return new_state;
290 }
291
292
293 /**
294 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
295 */
296 static GLbitfield
297 update_program_constants(struct gl_context *ctx)
298 {
299 GLbitfield new_state = 0x0;
300
301 if (ctx->FragmentProgram._Current) {
302 const struct gl_program_parameter_list *params =
303 ctx->FragmentProgram._Current->Parameters;
304 if (params && params->StateFlags & ctx->NewState) {
305 new_state |= _NEW_PROGRAM_CONSTANTS;
306 }
307 }
308
309 /* Don't handle tessellation and geometry shaders here. They don't use
310 * any state constants.
311 */
312
313 if (ctx->VertexProgram._Current) {
314 const struct gl_program_parameter_list *params =
315 ctx->VertexProgram._Current->Parameters;
316 if (params && params->StateFlags & ctx->NewState) {
317 new_state |= _NEW_PROGRAM_CONSTANTS;
318 }
319 }
320
321 return new_state;
322 }
323
324
325
326
327 /**
328 * Update the ctx->Polygon._FrontBit flag.
329 */
330 static void
331 update_frontbit(struct gl_context *ctx)
332 {
333 if (ctx->Transform.ClipOrigin == GL_LOWER_LEFT)
334 ctx->Polygon._FrontBit = (ctx->Polygon.FrontFace == GL_CW);
335 else
336 ctx->Polygon._FrontBit = (ctx->Polygon.FrontFace == GL_CCW);
337 }
338
339
340 /**
341 * Update the ctx->VertexProgram._TwoSideEnabled flag.
342 */
343 static void
344 update_twoside(struct gl_context *ctx)
345 {
346 if (ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] ||
347 ctx->VertexProgram._Enabled) {
348 ctx->VertexProgram._TwoSideEnabled = ctx->VertexProgram.TwoSideEnabled;
349 } else {
350 ctx->VertexProgram._TwoSideEnabled = (ctx->Light.Enabled &&
351 ctx->Light.Model.TwoSide);
352 }
353 }
354
355
356 /**
357 * Compute derived GL state.
358 * If __struct gl_contextRec::NewState is non-zero then this function \b must
359 * be called before rendering anything.
360 *
361 * Calls dd_function_table::UpdateState to perform any internal state
362 * management necessary.
363 *
364 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
365 * _mesa_update_buffer_bounds(),
366 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
367 */
368 void
369 _mesa_update_state_locked( struct gl_context *ctx )
370 {
371 GLbitfield new_state = ctx->NewState;
372 GLbitfield prog_flags = _NEW_PROGRAM;
373 GLbitfield new_prog_state = 0x0;
374 const GLbitfield computed_states = ~(_NEW_CURRENT_ATTRIB | _NEW_LINE);
375
376 /* we can skip a bunch of state validation checks if the dirty
377 * state matches one or more bits in 'computed_states'.
378 */
379 if ((new_state & computed_states) == 0)
380 goto out;
381
382 if (MESA_VERBOSE & VERBOSE_STATE)
383 _mesa_print_state("_mesa_update_state", new_state);
384
385 /* Determine which state flags effect vertex/fragment program state */
386 if (ctx->FragmentProgram._MaintainTexEnvProgram) {
387 prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE | _NEW_FOG |
388 _NEW_VARYING_VP_INPUTS | _NEW_LIGHT | _NEW_POINT |
389 _NEW_RENDERMODE | _NEW_PROGRAM | _NEW_FRAG_CLAMP |
390 _NEW_COLOR);
391 }
392 if (ctx->VertexProgram._MaintainTnlProgram) {
393 prog_flags |= (_NEW_VARYING_VP_INPUTS | _NEW_TEXTURE |
394 _NEW_TEXTURE_MATRIX | _NEW_TRANSFORM | _NEW_POINT |
395 _NEW_FOG | _NEW_LIGHT |
396 _MESA_NEW_NEED_EYE_COORDS);
397 }
398
399 /*
400 * Now update derived state info
401 */
402
403 if (new_state & prog_flags)
404 update_program_enables( ctx );
405
406 if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
407 _mesa_update_modelview_project( ctx, new_state );
408
409 if (new_state & (_NEW_PROGRAM|_NEW_TEXTURE|_NEW_TEXTURE_MATRIX))
410 _mesa_update_texture( ctx, new_state );
411
412 if (new_state & _NEW_POLYGON)
413 update_frontbit( ctx );
414
415 if (new_state & _NEW_BUFFERS)
416 _mesa_update_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer);
417
418 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
419 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
420
421 if (new_state & _NEW_LIGHT)
422 _mesa_update_lighting( ctx );
423
424 if (new_state & (_NEW_LIGHT | _NEW_PROGRAM))
425 update_twoside( ctx );
426
427 if (new_state & (_NEW_STENCIL | _NEW_BUFFERS))
428 _mesa_update_stencil( ctx );
429
430 if (new_state & _NEW_PIXEL)
431 _mesa_update_pixel( ctx, new_state );
432
433 /* ctx->_NeedEyeCoords is now up to date.
434 *
435 * If the truth value of this variable has changed, update for the
436 * new lighting space and recompute the positions of lights and the
437 * normal transform.
438 *
439 * If the lighting space hasn't changed, may still need to recompute
440 * light positions & normal transforms for other reasons.
441 */
442 if (new_state & _MESA_NEW_NEED_EYE_COORDS)
443 _mesa_update_tnl_spaces( ctx, new_state );
444
445 if (new_state & prog_flags) {
446 /* When we generate programs from fixed-function vertex/fragment state
447 * this call may generate/bind a new program. If so, we need to
448 * propogate the _NEW_PROGRAM flag to the driver.
449 */
450 new_prog_state |= update_program( ctx );
451 }
452
453 if (new_state & _NEW_ARRAY)
454 _mesa_update_vao_client_arrays(ctx, ctx->Array.VAO);
455
456 out:
457 new_prog_state |= update_program_constants(ctx);
458
459 /*
460 * Give the driver a chance to act upon the new_state flags.
461 * The driver might plug in different span functions, for example.
462 * Also, this is where the driver can invalidate the state of any
463 * active modules (such as swrast_setup, swrast, tnl, etc).
464 *
465 * Set ctx->NewState to zero to avoid recursion if
466 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
467 */
468 new_state = ctx->NewState | new_prog_state;
469 ctx->NewState = 0;
470 ctx->Driver.UpdateState(ctx, new_state);
471 ctx->Array.VAO->NewArrays = 0x0;
472 }
473
474
475 /* This is the usual entrypoint for state updates:
476 */
477 void
478 _mesa_update_state( struct gl_context *ctx )
479 {
480 _mesa_lock_context_textures(ctx);
481 _mesa_update_state_locked(ctx);
482 _mesa_unlock_context_textures(ctx);
483 }
484
485
486
487
488 /**
489 * Want to figure out which fragment program inputs are actually
490 * constant/current values from ctx->Current. These should be
491 * referenced as a tracked state variable rather than a fragment
492 * program input, to save the overhead of putting a constant value in
493 * every submitted vertex, transferring it to hardware, interpolating
494 * it across the triangle, etc...
495 *
496 * When there is a VP bound, just use vp->outputs. But when we're
497 * generating vp from fixed function state, basically want to
498 * calculate:
499 *
500 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
501 * potential_vp_outputs )
502 *
503 * Where potential_vp_outputs is calculated by looking at enabled
504 * texgen, etc.
505 *
506 * The generated fragment program should then only declare inputs that
507 * may vary or otherwise differ from the ctx->Current values.
508 * Otherwise, the fp should track them as state values instead.
509 */
510 void
511 _mesa_set_varying_vp_inputs( struct gl_context *ctx,
512 GLbitfield64 varying_inputs )
513 {
514 if (ctx->varying_vp_inputs != varying_inputs) {
515 ctx->varying_vp_inputs = varying_inputs;
516
517 /* Only the fixed-func generated programs need to use the flag
518 * and the fixed-func fragment program uses it only if there is also
519 * a fixed-func vertex program, so this only depends on the latter.
520 *
521 * It's okay to check the VP pointer here, because this is called after
522 * _mesa_update_state in the vbo module. */
523 if (ctx->VertexProgram._TnlProgram ||
524 ctx->FragmentProgram._TexEnvProgram) {
525 ctx->NewState |= _NEW_VARYING_VP_INPUTS;
526 }
527 /*printf("%s %x\n", __func__, varying_inputs);*/
528 }
529 }
530
531
532 /**
533 * Used by drivers to tell core Mesa that the driver is going to
534 * install/ use its own vertex program. In particular, this will
535 * prevent generated fragment programs from using state vars instead
536 * of ordinary varyings/inputs.
537 */
538 void
539 _mesa_set_vp_override(struct gl_context *ctx, GLboolean flag)
540 {
541 if (ctx->VertexProgram._Overriden != flag) {
542 ctx->VertexProgram._Overriden = flag;
543
544 /* Set one of the bits which will trigger fragment program
545 * regeneration:
546 */
547 ctx->NewState |= _NEW_PROGRAM;
548 }
549 }