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