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