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