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