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