state.c revision 01e04c3f
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.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 */ 72static GLbitfield 73update_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 94 /* 95 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current 96 * pointers to the programs that should be used for rendering. If either 97 * is NULL, use fixed-function code paths. 98 * 99 * These programs may come from several sources. The priority is as 100 * follows: 101 * 1. OpenGL 2.0/ARB vertex/fragment shaders 102 * 2. ARB/NV vertex/fragment programs 103 * 3. ATI fragment shader 104 * 4. Programs derived from fixed-function state. 105 * 106 * Note: it's possible for a vertex shader to get used with a fragment 107 * program (and vice versa) here, but in practice that shouldn't ever 108 * come up, or matter. 109 */ 110 111 if (fsProg) { 112 /* Use GLSL fragment shader */ 113 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, fsProg); 114 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, 115 NULL); 116 } 117 else if (_mesa_arb_fragment_program_enabled(ctx)) { 118 /* Use user-defined fragment program */ 119 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, 120 ctx->FragmentProgram.Current); 121 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, 122 NULL); 123 } 124 else if (_mesa_ati_fragment_shader_enabled(ctx) && 125 ctx->ATIFragmentShader.Current->Program) { 126 /* Use the enabled ATI fragment shader's associated program */ 127 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, 128 ctx->ATIFragmentShader.Current->Program); 129 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, 130 NULL); 131 } 132 else if (ctx->FragmentProgram._MaintainTexEnvProgram) { 133 /* Use fragment program generated from fixed-function state */ 134 struct gl_shader_program *f = _mesa_get_fixed_func_fragment_program(ctx); 135 136 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, 137 f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program); 138 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, 139 f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program); 140 } 141 else { 142 /* No fragment program */ 143 _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, NULL); 144 _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, 145 NULL); 146 } 147 148 if (gsProg) { 149 /* Use GLSL geometry shader */ 150 _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, gsProg); 151 } else { 152 /* No geometry program */ 153 _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, NULL); 154 } 155 156 if (tesProg) { 157 /* Use GLSL tessellation evaluation shader */ 158 _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, tesProg); 159 } 160 else { 161 /* No tessellation evaluation program */ 162 _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, NULL); 163 } 164 165 if (tcsProg) { 166 /* Use GLSL tessellation control shader */ 167 _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, tcsProg); 168 } 169 else { 170 /* No tessellation control program */ 171 _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, NULL); 172 } 173 174 /* Examine vertex program after fragment program as 175 * _mesa_get_fixed_func_vertex_program() needs to know active 176 * fragprog inputs. 177 */ 178 if (vsProg) { 179 /* Use GLSL vertex shader */ 180 assert(VP_MODE_SHADER == ctx->VertexProgram._VPMode); 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 assert(VP_MODE_SHADER == ctx->VertexProgram._VPMode); 186 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, 187 ctx->VertexProgram.Current); 188 } 189 else if (ctx->VertexProgram._MaintainTnlProgram) { 190 /* Use vertex program generated from fixed-function state */ 191 assert(VP_MODE_FF == ctx->VertexProgram._VPMode); 192 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, 193 _mesa_get_fixed_func_vertex_program(ctx)); 194 _mesa_reference_program(ctx, &ctx->VertexProgram._TnlProgram, 195 ctx->VertexProgram._Current); 196 } 197 else { 198 /* no vertex program */ 199 assert(VP_MODE_FF == ctx->VertexProgram._VPMode); 200 _mesa_reference_program(ctx, &ctx->VertexProgram._Current, NULL); 201 } 202 203 if (csProg) { 204 /* Use GLSL compute shader */ 205 _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, csProg); 206 } else { 207 /* no compute program */ 208 _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, NULL); 209 } 210 211 /* Let the driver know what's happening: 212 */ 213 if (ctx->FragmentProgram._Current != prevFP || 214 ctx->VertexProgram._Current != prevVP || 215 ctx->GeometryProgram._Current != prevGP || 216 ctx->TessEvalProgram._Current != prevTEP || 217 ctx->TessCtrlProgram._Current != prevTCP || 218 ctx->ComputeProgram._Current != prevCP) 219 return _NEW_PROGRAM; 220 221 return 0; 222} 223 224 225static GLbitfield 226update_single_program_constants(struct gl_context *ctx, 227 struct gl_program *prog, 228 gl_shader_stage stage) 229{ 230 if (prog) { 231 const struct gl_program_parameter_list *params = prog->Parameters; 232 if (params && params->StateFlags & ctx->NewState) { 233 if (ctx->DriverFlags.NewShaderConstants[stage]) 234 ctx->NewDriverState |= ctx->DriverFlags.NewShaderConstants[stage]; 235 else 236 return _NEW_PROGRAM_CONSTANTS; 237 } 238 } 239 return 0; 240} 241 242 243/** 244 * This updates fixed-func state constants such as gl_ModelViewMatrix. 245 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0. 246 */ 247static GLbitfield 248update_program_constants(struct gl_context *ctx) 249{ 250 GLbitfield new_state = 251 update_single_program_constants(ctx, ctx->VertexProgram._Current, 252 MESA_SHADER_VERTEX) | 253 update_single_program_constants(ctx, ctx->FragmentProgram._Current, 254 MESA_SHADER_FRAGMENT); 255 256 if (ctx->API == API_OPENGL_COMPAT && 257 ctx->Const.GLSLVersionCompat >= 150) { 258 new_state |= 259 update_single_program_constants(ctx, ctx->GeometryProgram._Current, 260 MESA_SHADER_GEOMETRY); 261 262 if (_mesa_has_ARB_tessellation_shader(ctx)) { 263 new_state |= 264 update_single_program_constants(ctx, ctx->TessCtrlProgram._Current, 265 MESA_SHADER_TESS_CTRL) | 266 update_single_program_constants(ctx, ctx->TessEvalProgram._Current, 267 MESA_SHADER_TESS_EVAL); 268 } 269 } 270 271 return new_state; 272} 273 274 275/** 276 * Compute derived GL state. 277 * If __struct gl_contextRec::NewState is non-zero then this function \b must 278 * be called before rendering anything. 279 * 280 * Calls dd_function_table::UpdateState to perform any internal state 281 * management necessary. 282 * 283 * \sa _mesa_update_modelview_project(), _mesa_update_texture(), 284 * _mesa_update_buffer_bounds(), 285 * _mesa_update_lighting() and _mesa_update_tnl_spaces(). 286 */ 287void 288_mesa_update_state_locked( struct gl_context *ctx ) 289{ 290 GLbitfield new_state = ctx->NewState; 291 GLbitfield new_prog_state = 0x0; 292 const GLbitfield computed_states = ~(_NEW_CURRENT_ATTRIB | _NEW_LINE); 293 294 /* we can skip a bunch of state validation checks if the dirty 295 * state matches one or more bits in 'computed_states'. 296 */ 297 if ((new_state & computed_states) == 0) 298 goto out; 299 300 if (MESA_VERBOSE & VERBOSE_STATE) 301 _mesa_print_state("_mesa_update_state", new_state); 302 303 if (new_state & _NEW_BUFFERS) 304 _mesa_update_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer); 305 306 /* Handle Core and Compatibility contexts separately. */ 307 if (ctx->API == API_OPENGL_COMPAT || 308 ctx->API == API_OPENGLES) { 309 GLbitfield prog_flags = _NEW_PROGRAM; 310 311 /* Determine which state flags effect vertex/fragment program state */ 312 if (ctx->FragmentProgram._MaintainTexEnvProgram) { 313 prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE_OBJECT | _NEW_FOG | 314 _NEW_VARYING_VP_INPUTS | _NEW_LIGHT | _NEW_POINT | 315 _NEW_RENDERMODE | _NEW_PROGRAM | _NEW_FRAG_CLAMP | 316 _NEW_COLOR | _NEW_TEXTURE_STATE); 317 } 318 if (ctx->VertexProgram._MaintainTnlProgram) { 319 prog_flags |= (_NEW_VARYING_VP_INPUTS | _NEW_TEXTURE_OBJECT | 320 _NEW_TEXTURE_MATRIX | _NEW_TRANSFORM | _NEW_POINT | 321 _NEW_FOG | _NEW_LIGHT | _NEW_TEXTURE_STATE | 322 _MESA_NEW_NEED_EYE_COORDS); 323 } 324 325 /* 326 * Now update derived state info 327 */ 328 if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION)) 329 _mesa_update_modelview_project( ctx, new_state ); 330 331 if (new_state & _NEW_TEXTURE_MATRIX) 332 _mesa_update_texture_matrices(ctx); 333 334 if (new_state & (_NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE | _NEW_PROGRAM)) 335 _mesa_update_texture_state(ctx); 336 337 if (new_state & _NEW_LIGHT) 338 _mesa_update_lighting(ctx); 339 340 if (new_state & _NEW_PIXEL) 341 _mesa_update_pixel( ctx ); 342 343 /* ctx->_NeedEyeCoords is now up to date. 344 * 345 * If the truth value of this variable has changed, update for the 346 * new lighting space and recompute the positions of lights and the 347 * normal transform. 348 * 349 * If the lighting space hasn't changed, may still need to recompute 350 * light positions & normal transforms for other reasons. 351 */ 352 if (new_state & _MESA_NEW_NEED_EYE_COORDS) 353 _mesa_update_tnl_spaces( ctx, new_state ); 354 355 if (new_state & prog_flags) { 356 /* When we generate programs from fixed-function vertex/fragment state 357 * this call may generate/bind a new program. If so, we need to 358 * propogate the _NEW_PROGRAM flag to the driver. 359 */ 360 new_prog_state |= update_program(ctx); 361 } 362 } else { 363 /* GL Core and GLES 2/3 contexts */ 364 if (new_state & (_NEW_TEXTURE_OBJECT | _NEW_PROGRAM)) 365 _mesa_update_texture_state(ctx); 366 367 if (new_state & _NEW_PROGRAM) 368 update_program(ctx); 369 } 370 371 out: 372 new_prog_state |= update_program_constants(ctx); 373 374 ctx->NewState |= new_prog_state; 375 vbo_exec_invalidate_state(ctx); 376 377 /* 378 * Give the driver a chance to act upon the new_state flags. 379 * The driver might plug in different span functions, for example. 380 * Also, this is where the driver can invalidate the state of any 381 * active modules (such as swrast_setup, swrast, tnl, etc). 382 */ 383 ctx->Driver.UpdateState(ctx); 384 ctx->NewState = 0; 385} 386 387 388/* This is the usual entrypoint for state updates: 389 */ 390void 391_mesa_update_state( struct gl_context *ctx ) 392{ 393 _mesa_lock_context_textures(ctx); 394 _mesa_update_state_locked(ctx); 395 _mesa_unlock_context_textures(ctx); 396} 397 398 399 400 401/** 402 * Want to figure out which fragment program inputs are actually 403 * constant/current values from ctx->Current. These should be 404 * referenced as a tracked state variable rather than a fragment 405 * program input, to save the overhead of putting a constant value in 406 * every submitted vertex, transferring it to hardware, interpolating 407 * it across the triangle, etc... 408 * 409 * When there is a VP bound, just use vp->outputs. But when we're 410 * generating vp from fixed function state, basically want to 411 * calculate: 412 * 413 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) | 414 * potential_vp_outputs ) 415 * 416 * Where potential_vp_outputs is calculated by looking at enabled 417 * texgen, etc. 418 * 419 * The generated fragment program should then only declare inputs that 420 * may vary or otherwise differ from the ctx->Current values. 421 * Otherwise, the fp should track them as state values instead. 422 */ 423void 424_mesa_set_varying_vp_inputs( struct gl_context *ctx, 425 GLbitfield varying_inputs ) 426{ 427 if (ctx->API != API_OPENGL_COMPAT && 428 ctx->API != API_OPENGLES) 429 return; 430 431 if (ctx->varying_vp_inputs != varying_inputs) { 432 ctx->varying_vp_inputs = varying_inputs; 433 434 /* Only the fixed-func generated programs need to use the flag 435 * and the fixed-func fragment program uses it only if there is also 436 * a fixed-func vertex program, so this only depends on the latter. 437 * 438 * It's okay to check the VP pointer here, because this is called after 439 * _mesa_update_state in the vbo module. */ 440 if (ctx->VertexProgram._TnlProgram || 441 ctx->FragmentProgram._TexEnvProgram) { 442 ctx->NewState |= _NEW_VARYING_VP_INPUTS; 443 } 444 /*printf("%s %x\n", __func__, varying_inputs);*/ 445 } 446} 447 448 449/** 450 * Used by drivers to tell core Mesa that the driver is going to 451 * install/ use its own vertex program. In particular, this will 452 * prevent generated fragment programs from using state vars instead 453 * of ordinary varyings/inputs. 454 */ 455void 456_mesa_set_vp_override(struct gl_context *ctx, GLboolean flag) 457{ 458 if (ctx->VertexProgram._Overriden != flag) { 459 ctx->VertexProgram._Overriden = flag; 460 461 /* Set one of the bits which will trigger fragment program 462 * regeneration: 463 */ 464 ctx->NewState |= _NEW_PROGRAM; 465 } 466} 467 468 469static void 470set_vertex_processing_mode(struct gl_context *ctx, gl_vertex_processing_mode m) 471{ 472 if (ctx->VertexProgram._VPMode == m) 473 return; 474 475 /* On change we may get new maps into the current values */ 476 ctx->NewDriverState |= ctx->DriverFlags.NewArray; 477 478 /* Finally memorize the value */ 479 ctx->VertexProgram._VPMode = m; 480} 481 482 483/** 484 * Update ctx->VertexProgram._VPMode. 485 * This is to distinguish whether we're running 486 * a vertex program/shader, 487 * a fixed-function TNL program or 488 * a fixed function vertex transformation without any program. 489 */ 490void 491_mesa_update_vertex_processing_mode(struct gl_context *ctx) 492{ 493 if (ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]) 494 set_vertex_processing_mode(ctx, VP_MODE_SHADER); 495 else if (_mesa_arb_vertex_program_enabled(ctx)) 496 set_vertex_processing_mode(ctx, VP_MODE_SHADER); 497 else 498 set_vertex_processing_mode(ctx, VP_MODE_FF); 499} 500 501 502/** 503 * Set the _DrawVAO and the net enabled arrays. 504 * The vao->_Enabled bitmask is transformed due to position/generic0 505 * as stored in vao->_AttributeMapMode. Then the filter bitmask is applied 506 * to filter out arrays unwanted for the currently executed draw operation. 507 * For example, the generic attributes are masked out form the _DrawVAO's 508 * enabled arrays when a fixed function array draw is executed. 509 */ 510void 511_mesa_set_draw_vao(struct gl_context *ctx, struct gl_vertex_array_object *vao, 512 GLbitfield filter) 513{ 514 struct gl_vertex_array_object **ptr = &ctx->Array._DrawVAO; 515 bool new_array = false; 516 if (*ptr != vao) { 517 _mesa_reference_vao_(ctx, ptr, vao); 518 519 new_array = true; 520 } 521 522 if (vao->NewArrays) { 523 _mesa_update_vao_derived_arrays(ctx, vao); 524 vao->NewArrays = 0; 525 526 new_array = true; 527 } 528 529 /* May shuffle the position and generic0 bits around, filter out unwanted */ 530 const GLbitfield enabled = filter & _mesa_get_vao_vp_inputs(vao); 531 if (ctx->Array._DrawVAOEnabledAttribs != enabled) 532 new_array = true; 533 534 if (new_array) 535 ctx->NewDriverState |= ctx->DriverFlags.NewArray; 536 537 ctx->Array._DrawVAOEnabledAttribs = enabled; 538 _mesa_set_varying_vp_inputs(ctx, enabled); 539} 540