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