mesa: remove unused gl_array_object::NewArray
[mesa.git] / src / mesa / main / state.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file state.c
29 * State management.
30 *
31 * This file manages recalculation of derived values in struct gl_context.
32 */
33
34
35 #include "glheader.h"
36 #include "mtypes.h"
37 #include "arrayobj.h"
38 #include "context.h"
39 #include "debug.h"
40 #include "macros.h"
41 #include "ffvertex_prog.h"
42 #include "framebuffer.h"
43 #include "light.h"
44 #include "matrix.h"
45 #include "pixel.h"
46 #include "program/program.h"
47 #include "program/prog_parameter.h"
48 #include "shaderobj.h"
49 #include "state.h"
50 #include "stencil.h"
51 #include "texenvprogram.h"
52 #include "texobj.h"
53 #include "texstate.h"
54 #include "varray.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->Vertex/Geometry/FragmentProgram._Current pointers to point
83 * to the 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 = ctx->Shader.CurrentVertexProgram;
99 const struct gl_shader_program *gsProg = ctx->Shader.CurrentGeometryProgram;
100 struct gl_shader_program *fsProg = ctx->Shader.CurrentFragmentProgram;
101 const struct gl_vertex_program *prevVP = ctx->VertexProgram._Current;
102 const struct gl_fragment_program *prevFP = ctx->FragmentProgram._Current;
103 const struct gl_geometry_program *prevGP = ctx->GeometryProgram._Current;
104 GLbitfield new_state = 0x0;
105
106 /*
107 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
108 * pointers to the programs that should be used for rendering. If either
109 * is NULL, use fixed-function code paths.
110 *
111 * These programs may come from several sources. The priority is as
112 * follows:
113 * 1. OpenGL 2.0/ARB vertex/fragment shaders
114 * 2. ARB/NV vertex/fragment programs
115 * 3. Programs derived from fixed-function state.
116 *
117 * Note: it's possible for a vertex shader to get used with a fragment
118 * program (and vice versa) here, but in practice that shouldn't ever
119 * come up, or matter.
120 */
121
122 if (fsProg && fsProg->LinkStatus
123 && fsProg->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
124 /* Use GLSL fragment shader */
125 _mesa_reference_shader_program(ctx,
126 &ctx->Shader._CurrentFragmentProgram,
127 fsProg);
128 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
129 gl_fragment_program(fsProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program));
130 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
131 NULL);
132 }
133 else if (ctx->FragmentProgram._Enabled) {
134 /* Use user-defined fragment program */
135 _mesa_reference_shader_program(ctx,
136 &ctx->Shader._CurrentFragmentProgram,
137 NULL);
138 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
139 ctx->FragmentProgram.Current);
140 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
141 NULL);
142 }
143 else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
144 /* Use fragment program generated from fixed-function state */
145 struct gl_shader_program *f = _mesa_get_fixed_func_fragment_program(ctx);
146
147 _mesa_reference_shader_program(ctx,
148 &ctx->Shader._CurrentFragmentProgram,
149 f);
150 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
151 gl_fragment_program(f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program));
152 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
153 gl_fragment_program(f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program));
154 }
155 else {
156 /* No fragment program */
157 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
158 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
159 NULL);
160 }
161
162 if (gsProg && gsProg->LinkStatus
163 && gsProg->_LinkedShaders[MESA_SHADER_GEOMETRY]) {
164 /* Use GLSL geometry shader */
165 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current,
166 gl_geometry_program(gsProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program));
167 } else {
168 /* No geometry program */
169 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current, NULL);
170 }
171
172 /* Examine vertex program after fragment program as
173 * _mesa_get_fixed_func_vertex_program() needs to know active
174 * fragprog inputs.
175 */
176 if (vsProg && vsProg->LinkStatus
177 && vsProg->_LinkedShaders[MESA_SHADER_VERTEX]) {
178 /* Use GLSL vertex shader */
179 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
180 gl_vertex_program(vsProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program));
181 }
182 else if (ctx->VertexProgram._Enabled) {
183 /* Use user-defined vertex program */
184 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
185 ctx->VertexProgram.Current);
186 }
187 else if (ctx->VertexProgram._MaintainTnlProgram) {
188 /* Use vertex program generated from fixed-function state */
189 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
190 _mesa_get_fixed_func_vertex_program(ctx));
191 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram,
192 ctx->VertexProgram._Current);
193 }
194 else {
195 /* no vertex program */
196 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
197 }
198
199 /* Let the driver know what's happening:
200 */
201 if (ctx->FragmentProgram._Current != prevFP) {
202 new_state |= _NEW_PROGRAM;
203 if (ctx->Driver.BindProgram) {
204 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
205 (struct gl_program *) ctx->FragmentProgram._Current);
206 }
207 }
208
209 if (ctx->GeometryProgram._Current != prevGP) {
210 new_state |= _NEW_PROGRAM;
211 if (ctx->Driver.BindProgram) {
212 ctx->Driver.BindProgram(ctx, MESA_GEOMETRY_PROGRAM,
213 (struct gl_program *) ctx->GeometryProgram._Current);
214 }
215 }
216
217 if (ctx->VertexProgram._Current != prevVP) {
218 new_state |= _NEW_PROGRAM;
219 if (ctx->Driver.BindProgram) {
220 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
221 (struct gl_program *) ctx->VertexProgram._Current);
222 }
223 }
224
225 return new_state;
226 }
227
228
229 /**
230 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
231 */
232 static GLbitfield
233 update_program_constants(struct gl_context *ctx)
234 {
235 GLbitfield new_state = 0x0;
236
237 if (ctx->FragmentProgram._Current) {
238 const struct gl_program_parameter_list *params =
239 ctx->FragmentProgram._Current->Base.Parameters;
240 if (params && params->StateFlags & ctx->NewState) {
241 new_state |= _NEW_PROGRAM_CONSTANTS;
242 }
243 }
244
245 if (ctx->GeometryProgram._Current) {
246 const struct gl_program_parameter_list *params =
247 ctx->GeometryProgram._Current->Base.Parameters;
248 /*FIXME: StateFlags is always 0 because we have unnamed constant
249 * not state changes */
250 if (params /*&& params->StateFlags & ctx->NewState*/) {
251 new_state |= _NEW_PROGRAM_CONSTANTS;
252 }
253 }
254
255 if (ctx->VertexProgram._Current) {
256 const struct gl_program_parameter_list *params =
257 ctx->VertexProgram._Current->Base.Parameters;
258 if (params && params->StateFlags & ctx->NewState) {
259 new_state |= _NEW_PROGRAM_CONSTANTS;
260 }
261 }
262
263 return new_state;
264 }
265
266
267
268
269 static void
270 update_viewport_matrix(struct gl_context *ctx)
271 {
272 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
273
274 ASSERT(depthMax > 0);
275
276 /* Compute scale and bias values. This is really driver-specific
277 * and should be maintained elsewhere if at all.
278 * NOTE: RasterPos uses this.
279 */
280 _math_matrix_viewport(&ctx->Viewport._WindowMap,
281 ctx->Viewport.X, ctx->Viewport.Y,
282 ctx->Viewport.Width, ctx->Viewport.Height,
283 ctx->Viewport.Near, ctx->Viewport.Far,
284 depthMax);
285 }
286
287
288 /**
289 * Update derived multisample state.
290 */
291 static void
292 update_multisample(struct gl_context *ctx)
293 {
294 ctx->Multisample._Enabled = GL_FALSE;
295 if (ctx->Multisample.Enabled &&
296 ctx->DrawBuffer &&
297 ctx->DrawBuffer->Visual.sampleBuffers)
298 ctx->Multisample._Enabled = GL_TRUE;
299 }
300
301
302 /**
303 * Update the ctx->VertexProgram._TwoSideEnabled flag.
304 */
305 static void
306 update_twoside(struct gl_context *ctx)
307 {
308 if (ctx->Shader.CurrentVertexProgram ||
309 ctx->VertexProgram._Enabled) {
310 ctx->VertexProgram._TwoSideEnabled = ctx->VertexProgram.TwoSideEnabled;
311 } else {
312 ctx->VertexProgram._TwoSideEnabled = (ctx->Light.Enabled &&
313 ctx->Light.Model.TwoSide);
314 }
315 }
316
317
318 /**
319 * Compute derived GL state.
320 * If __struct gl_contextRec::NewState is non-zero then this function \b must
321 * be called before rendering anything.
322 *
323 * Calls dd_function_table::UpdateState to perform any internal state
324 * management necessary.
325 *
326 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
327 * _mesa_update_buffer_bounds(),
328 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
329 */
330 void
331 _mesa_update_state_locked( struct gl_context *ctx )
332 {
333 GLbitfield new_state = ctx->NewState;
334 GLbitfield prog_flags = _NEW_PROGRAM;
335 GLbitfield new_prog_state = 0x0;
336
337 if (new_state == _NEW_CURRENT_ATTRIB)
338 goto out;
339
340 if (MESA_VERBOSE & VERBOSE_STATE)
341 _mesa_print_state("_mesa_update_state", new_state);
342
343 /* Determine which state flags effect vertex/fragment program state */
344 if (ctx->FragmentProgram._MaintainTexEnvProgram) {
345 prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE | _NEW_FOG |
346 _NEW_VARYING_VP_INPUTS | _NEW_LIGHT | _NEW_POINT |
347 _NEW_RENDERMODE | _NEW_PROGRAM | _NEW_FRAG_CLAMP |
348 _NEW_COLOR);
349 }
350 if (ctx->VertexProgram._MaintainTnlProgram) {
351 prog_flags |= (_NEW_VARYING_VP_INPUTS | _NEW_TEXTURE |
352 _NEW_TEXTURE_MATRIX | _NEW_TRANSFORM | _NEW_POINT |
353 _NEW_FOG | _NEW_LIGHT |
354 _MESA_NEW_NEED_EYE_COORDS);
355 }
356
357 /*
358 * Now update derived state info
359 */
360
361 if (new_state & prog_flags)
362 update_program_enables( ctx );
363
364 if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
365 _mesa_update_modelview_project( ctx, new_state );
366
367 if (new_state & (_NEW_PROGRAM|_NEW_TEXTURE|_NEW_TEXTURE_MATRIX))
368 _mesa_update_texture( ctx, new_state );
369
370 if (new_state & _NEW_BUFFERS)
371 _mesa_update_framebuffer(ctx);
372
373 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
374 _mesa_update_draw_buffer_bounds( ctx );
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, new_state );
387
388 if (new_state & (_NEW_BUFFERS | _NEW_VIEWPORT))
389 update_viewport_matrix(ctx);
390
391 if (new_state & (_NEW_MULTISAMPLE | _NEW_BUFFERS))
392 update_multisample( ctx );
393
394 /* ctx->_NeedEyeCoords is now up to date.
395 *
396 * If the truth value of this variable has changed, update for the
397 * new lighting space and recompute the positions of lights and the
398 * normal transform.
399 *
400 * If the lighting space hasn't changed, may still need to recompute
401 * light positions & normal transforms for other reasons.
402 */
403 if (new_state & _MESA_NEW_NEED_EYE_COORDS)
404 _mesa_update_tnl_spaces( ctx, new_state );
405
406 if (new_state & prog_flags) {
407 /* When we generate programs from fixed-function vertex/fragment state
408 * this call may generate/bind a new program. If so, we need to
409 * propogate the _NEW_PROGRAM flag to the driver.
410 */
411 new_prog_state |= update_program( ctx );
412 }
413
414 if (new_state & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT))
415 _mesa_update_array_object_max_element(ctx, ctx->Array.ArrayObj);
416
417 out:
418 new_prog_state |= update_program_constants(ctx);
419
420 /*
421 * Give the driver a chance to act upon the new_state flags.
422 * The driver might plug in different span functions, for example.
423 * Also, this is where the driver can invalidate the state of any
424 * active modules (such as swrast_setup, swrast, tnl, etc).
425 *
426 * Set ctx->NewState to zero to avoid recursion if
427 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
428 */
429 new_state = ctx->NewState | new_prog_state;
430 ctx->NewState = 0;
431 ctx->Driver.UpdateState(ctx, new_state);
432 }
433
434
435 /* This is the usual entrypoint for state updates:
436 */
437 void
438 _mesa_update_state( struct gl_context *ctx )
439 {
440 _mesa_lock_context_textures(ctx);
441 _mesa_update_state_locked(ctx);
442 _mesa_unlock_context_textures(ctx);
443 }
444
445
446
447
448 /**
449 * Want to figure out which fragment program inputs are actually
450 * constant/current values from ctx->Current. These should be
451 * referenced as a tracked state variable rather than a fragment
452 * program input, to save the overhead of putting a constant value in
453 * every submitted vertex, transferring it to hardware, interpolating
454 * it across the triangle, etc...
455 *
456 * When there is a VP bound, just use vp->outputs. But when we're
457 * generating vp from fixed function state, basically want to
458 * calculate:
459 *
460 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
461 * potential_vp_outputs )
462 *
463 * Where potential_vp_outputs is calculated by looking at enabled
464 * texgen, etc.
465 *
466 * The generated fragment program should then only declare inputs that
467 * may vary or otherwise differ from the ctx->Current values.
468 * Otherwise, the fp should track them as state values instead.
469 */
470 void
471 _mesa_set_varying_vp_inputs( struct gl_context *ctx,
472 GLbitfield64 varying_inputs )
473 {
474 if (ctx->varying_vp_inputs != varying_inputs) {
475 ctx->varying_vp_inputs = varying_inputs;
476
477 /* Only the fixed-func generated programs need to use the flag
478 * and the fixed-func fragment program uses it only if there is also
479 * a fixed-func vertex program, so this only depends on the latter.
480 *
481 * It's okay to check the VP pointer here, because this is called after
482 * _mesa_update_state in the vbo module. */
483 if (ctx->VertexProgram._TnlProgram ||
484 ctx->FragmentProgram._TexEnvProgram) {
485 ctx->NewState |= _NEW_VARYING_VP_INPUTS;
486 }
487 /*printf("%s %x\n", __FUNCTION__, varying_inputs);*/
488 }
489 }
490
491
492 /**
493 * Used by drivers to tell core Mesa that the driver is going to
494 * install/ use its own vertex program. In particular, this will
495 * prevent generated fragment programs from using state vars instead
496 * of ordinary varyings/inputs.
497 */
498 void
499 _mesa_set_vp_override(struct gl_context *ctx, GLboolean flag)
500 {
501 if (ctx->VertexProgram._Overriden != flag) {
502 ctx->VertexProgram._Overriden = flag;
503
504 /* Set one of the bits which will trigger fragment program
505 * regeneration:
506 */
507 ctx->NewState |= _NEW_PROGRAM;
508 }
509 }