2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 * This file manages recalculation of derived values in GLcontext.
39 #include "ffvertex_prog.h"
40 #include "framebuffer.h"
43 #if FEATURE_pixel_transfer
46 #include "shader/program.h"
47 #include "shader/prog_parameter.h"
50 #include "texenvprogram.h"
57 update_separate_specular(GLcontext *ctx)
59 if (NEED_SECONDARY_COLOR(ctx))
60 ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
62 ctx->_TriangleCaps &= ~DD_SEPARATE_SPECULAR;
67 * Compute the index of the last array element that can be safely accessed
68 * in a vertex array. We can really only do this when the array lives in
70 * The array->_MaxElement field will be updated.
71 * Later in glDrawArrays/Elements/etc we can do some bounds checking.
74 compute_max_element(struct gl_client_array *array)
76 assert(array->Enabled);
77 if (array->BufferObj->Name) {
78 GLsizeiptrARB offset = (GLsizeiptrARB) array->Ptr;
79 GLsizeiptrARB obj_size = (GLsizeiptrARB) array->BufferObj->Size;
81 if (offset < obj_size) {
82 array->_MaxElement = (obj_size - offset +
84 array->_ElementSize) / array->StrideB;
86 array->_MaxElement = 0;
88 /* Compute the max element we can access in the VBO without going
91 array->_MaxElement = ((GLsizeiptrARB) array->BufferObj->Size
92 - (GLsizeiptrARB) array->Ptr + array->StrideB
93 - array->_ElementSize) / array->StrideB;
96 /* user-space array, no idea how big it is */
97 array->_MaxElement = 2 * 1000 * 1000 * 1000; /* just a big number */
103 * Helper for update_arrays().
104 * \return min(current min, array->_MaxElement).
107 update_min(GLuint min, struct gl_client_array *array)
109 compute_max_element(array);
110 return MIN2(min, array->_MaxElement);
115 * Update ctx->Array._MaxElement (the max legal index into all enabled arrays).
116 * Need to do this upon new array state or new buffer object state.
119 update_arrays( GLcontext *ctx )
121 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
124 /* find min of _MaxElement values for all enabled arrays */
127 if (ctx->VertexProgram._Current
128 && arrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled) {
129 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_POS]);
131 else if (arrayObj->Vertex.Enabled) {
132 min = update_min(min, &arrayObj->Vertex);
136 if (ctx->VertexProgram._Enabled
137 && arrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT].Enabled) {
138 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT]);
140 /* no conventional vertex weight array */
143 if (ctx->VertexProgram._Enabled
144 && arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL].Enabled) {
145 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL]);
147 else if (arrayObj->Normal.Enabled) {
148 min = update_min(min, &arrayObj->Normal);
152 if (ctx->VertexProgram._Enabled
153 && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0].Enabled) {
154 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0]);
156 else if (arrayObj->Color.Enabled) {
157 min = update_min(min, &arrayObj->Color);
161 if (ctx->VertexProgram._Enabled
162 && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR1].Enabled) {
163 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR1]);
165 else if (arrayObj->SecondaryColor.Enabled) {
166 min = update_min(min, &arrayObj->SecondaryColor);
170 if (ctx->VertexProgram._Enabled
171 && arrayObj->VertexAttrib[VERT_ATTRIB_FOG].Enabled) {
172 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_FOG]);
174 else if (arrayObj->FogCoord.Enabled) {
175 min = update_min(min, &arrayObj->FogCoord);
179 if (ctx->VertexProgram._Enabled
180 && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX].Enabled) {
181 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX]);
183 else if (arrayObj->Index.Enabled) {
184 min = update_min(min, &arrayObj->Index);
188 if (ctx->VertexProgram._Enabled
189 && arrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG].Enabled) {
190 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG]);
194 for (i = VERT_ATTRIB_TEX0; i <= VERT_ATTRIB_TEX7; i++) {
195 if (ctx->VertexProgram._Enabled
196 && arrayObj->VertexAttrib[i].Enabled) {
197 min = update_min(min, &arrayObj->VertexAttrib[i]);
199 else if (i - VERT_ATTRIB_TEX0 < ctx->Const.MaxTextureCoordUnits
200 && arrayObj->TexCoord[i - VERT_ATTRIB_TEX0].Enabled) {
201 min = update_min(min, &arrayObj->TexCoord[i - VERT_ATTRIB_TEX0]);
206 if (ctx->VertexProgram._Current) {
207 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) {
208 if (arrayObj->VertexAttrib[i].Enabled) {
209 min = update_min(min, &arrayObj->VertexAttrib[i]);
214 if (arrayObj->EdgeFlag.Enabled) {
215 min = update_min(min, &arrayObj->EdgeFlag);
218 /* _MaxElement is one past the last legal array element */
219 arrayObj->_MaxElement = min;
224 * Update the following fields:
225 * ctx->VertexProgram._Enabled
226 * ctx->FragmentProgram._Enabled
227 * ctx->ATIFragmentShader._Enabled
228 * This needs to be done before texture state validation.
231 update_program_enables(GLcontext *ctx)
233 /* These _Enabled flags indicate if the program is enabled AND valid. */
234 ctx->VertexProgram._Enabled = ctx->VertexProgram.Enabled
235 && ctx->VertexProgram.Current->Base.Instructions;
236 ctx->FragmentProgram._Enabled = ctx->FragmentProgram.Enabled
237 && ctx->FragmentProgram.Current->Base.Instructions;
238 ctx->ATIFragmentShader._Enabled = ctx->ATIFragmentShader.Enabled
239 && ctx->ATIFragmentShader.Current->Instructions[0];
244 * Update vertex/fragment program state. In particular, update these fields:
245 * ctx->VertexProgram._Current
246 * ctx->VertexProgram._TnlProgram,
247 * These point to the highest priority enabled vertex/fragment program or are
248 * NULL if fixed-function processing is to be done.
250 * This function needs to be called after texture state validation in case
251 * we're generating a fragment program from fixed-function texture state.
253 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
254 * or fragment program is being used.
257 update_program(GLcontext *ctx)
259 const struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
260 const struct gl_vertex_program *prevVP = ctx->VertexProgram._Current;
261 const struct gl_fragment_program *prevFP = ctx->FragmentProgram._Current;
262 GLbitfield new_state = 0x0;
265 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
266 * pointers to the programs that should be used for rendering. If either
267 * is NULL, use fixed-function code paths.
269 * These programs may come from several sources. The priority is as
271 * 1. OpenGL 2.0/ARB vertex/fragment shaders
272 * 2. ARB/NV vertex/fragment programs
273 * 3. Programs derived from fixed-function state.
275 * Note: it's possible for a vertex shader to get used with a fragment
276 * program (and vice versa) here, but in practice that shouldn't ever
277 * come up, or matter.
280 if (shProg && shProg->LinkStatus && shProg->FragmentProgram) {
281 /* Use shader programs */
282 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
283 shProg->FragmentProgram);
285 else if (ctx->FragmentProgram._Enabled) {
286 /* use user-defined vertex program */
287 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
288 ctx->FragmentProgram.Current);
290 else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
291 /* Use fragment program generated from fixed-function state.
293 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
294 _mesa_get_fixed_func_fragment_program(ctx));
295 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
296 ctx->FragmentProgram._Current);
299 /* no fragment program */
300 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
303 /* Examine vertex program after fragment program as
304 * _mesa_get_fixed_func_vertex_program() needs to know active
307 if (shProg && shProg->LinkStatus && shProg->VertexProgram) {
308 /* Use shader programs */
309 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
310 shProg->VertexProgram);
312 else if (ctx->VertexProgram._Enabled) {
313 /* use user-defined vertex program */
314 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
315 ctx->VertexProgram.Current);
317 else if (ctx->VertexProgram._MaintainTnlProgram) {
318 /* Use vertex program generated from fixed-function state.
320 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
321 _mesa_get_fixed_func_vertex_program(ctx));
322 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram,
323 ctx->VertexProgram._Current);
326 /* no vertex program */
327 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
330 /* Let the driver know what's happening:
332 if (ctx->FragmentProgram._Current != prevFP) {
333 new_state |= _NEW_PROGRAM;
334 if (ctx->Driver.BindProgram) {
335 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
336 (struct gl_program *) ctx->FragmentProgram._Current);
340 if (ctx->VertexProgram._Current != prevVP) {
341 new_state |= _NEW_PROGRAM;
342 if (ctx->Driver.BindProgram) {
343 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
344 (struct gl_program *) ctx->VertexProgram._Current);
353 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
356 update_program_constants(GLcontext *ctx)
358 GLbitfield new_state = 0x0;
360 if (ctx->FragmentProgram._Current) {
361 const struct gl_program_parameter_list *params =
362 ctx->FragmentProgram._Current->Base.Parameters;
363 if (params && params->StateFlags & ctx->NewState) {
364 new_state |= _NEW_PROGRAM_CONSTANTS;
368 if (ctx->VertexProgram._Current) {
369 const struct gl_program_parameter_list *params =
370 ctx->VertexProgram._Current->Base.Parameters;
371 if (params && params->StateFlags & ctx->NewState) {
372 new_state |= _NEW_PROGRAM_CONSTANTS;
383 update_viewport_matrix(GLcontext *ctx)
385 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
387 ASSERT(depthMax > 0);
389 /* Compute scale and bias values. This is really driver-specific
390 * and should be maintained elsewhere if at all.
391 * NOTE: RasterPos uses this.
393 _math_matrix_viewport(&ctx->Viewport._WindowMap,
394 ctx->Viewport.X, ctx->Viewport.Y,
395 ctx->Viewport.Width, ctx->Viewport.Height,
396 ctx->Viewport.Near, ctx->Viewport.Far,
402 * Update derived multisample state.
405 update_multisample(GLcontext *ctx)
407 ctx->Multisample._Enabled = GL_FALSE;
408 if (ctx->Multisample.Enabled &&
410 ctx->DrawBuffer->Visual.sampleBuffers)
411 ctx->Multisample._Enabled = GL_TRUE;
416 * Update derived color/blend/logicop state.
419 update_color(GLcontext *ctx)
421 /* This is needed to support 1.1's RGB logic ops AND
422 * 1.0's blending logicops.
424 ctx->Color._LogicOpEnabled = RGBA_LOGICOP_ENABLED(ctx);
429 * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
430 * in ctx->_TriangleCaps if needed.
433 update_polygon(GLcontext *ctx)
435 ctx->_TriangleCaps &= ~(DD_TRI_CULL_FRONT_BACK | DD_TRI_OFFSET);
437 if (ctx->Polygon.CullFlag && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
438 ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
440 if ( ctx->Polygon.OffsetPoint
441 || ctx->Polygon.OffsetLine
442 || ctx->Polygon.OffsetFill)
443 ctx->_TriangleCaps |= DD_TRI_OFFSET;
448 * Update the ctx->_TriangleCaps bitfield.
449 * XXX that bitfield should really go away someday!
450 * This function must be called after other update_*() functions since
451 * there are dependencies on some other derived values.
455 update_tricaps(GLcontext *ctx, GLbitfield new_state)
457 ctx->_TriangleCaps = 0;
462 if (1/*new_state & _NEW_POINT*/) {
463 if (ctx->Point.SmoothFlag)
464 ctx->_TriangleCaps |= DD_POINT_SMOOTH;
465 if (ctx->Point.Size != 1.0F)
466 ctx->_TriangleCaps |= DD_POINT_SIZE;
467 if (ctx->Point._Attenuated)
468 ctx->_TriangleCaps |= DD_POINT_ATTEN;
474 if (1/*new_state & _NEW_LINE*/) {
475 if (ctx->Line.SmoothFlag)
476 ctx->_TriangleCaps |= DD_LINE_SMOOTH;
477 if (ctx->Line.StippleFlag)
478 ctx->_TriangleCaps |= DD_LINE_STIPPLE;
479 if (ctx->Line.Width != 1.0)
480 ctx->_TriangleCaps |= DD_LINE_WIDTH;
486 if (1/*new_state & _NEW_POLYGON*/) {
487 if (ctx->Polygon.SmoothFlag)
488 ctx->_TriangleCaps |= DD_TRI_SMOOTH;
489 if (ctx->Polygon.StippleFlag)
490 ctx->_TriangleCaps |= DD_TRI_STIPPLE;
491 if (ctx->Polygon.FrontMode != GL_FILL
492 || ctx->Polygon.BackMode != GL_FILL)
493 ctx->_TriangleCaps |= DD_TRI_UNFILLED;
494 if (ctx->Polygon.CullFlag
495 && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
496 ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
497 if (ctx->Polygon.OffsetPoint ||
498 ctx->Polygon.OffsetLine ||
499 ctx->Polygon.OffsetFill)
500 ctx->_TriangleCaps |= DD_TRI_OFFSET;
504 * Lighting and shading
506 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
507 ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
508 if (ctx->Light.ShadeModel == GL_FLAT)
509 ctx->_TriangleCaps |= DD_FLATSHADE;
510 if (NEED_SECONDARY_COLOR(ctx))
511 ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
516 if (ctx->Stencil._TestTwoSide)
517 ctx->_TriangleCaps |= DD_TRI_TWOSTENCIL;
523 * Compute derived GL state.
524 * If __GLcontextRec::NewState is non-zero then this function \b must
525 * be called before rendering anything.
527 * Calls dd_function_table::UpdateState to perform any internal state
528 * management necessary.
530 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
531 * _mesa_update_buffer_bounds(),
532 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
535 _mesa_update_state_locked( GLcontext *ctx )
537 GLbitfield new_state = ctx->NewState;
538 GLbitfield prog_flags = _NEW_PROGRAM;
539 GLbitfield new_prog_state = 0x0;
541 if (new_state == _NEW_CURRENT_ATTRIB)
544 if (MESA_VERBOSE & VERBOSE_STATE)
545 _mesa_print_state("_mesa_update_state", new_state);
547 /* Determine which state flags effect vertex/fragment program state */
548 if (ctx->FragmentProgram._MaintainTexEnvProgram) {
549 prog_flags |= (_NEW_TEXTURE | _NEW_FOG |
550 _NEW_ARRAY | _NEW_LIGHT | _NEW_POINT | _NEW_RENDERMODE);
552 if (ctx->VertexProgram._MaintainTnlProgram) {
553 prog_flags |= (_NEW_ARRAY | _NEW_TEXTURE | _NEW_TEXTURE_MATRIX |
554 _NEW_TRANSFORM | _NEW_POINT |
555 _NEW_FOG | _NEW_LIGHT |
556 _MESA_NEW_NEED_EYE_COORDS);
560 * Now update derived state info
563 if (new_state & prog_flags)
564 update_program_enables( ctx );
566 if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
567 _mesa_update_modelview_project( ctx, new_state );
569 if (new_state & (_NEW_PROGRAM|_NEW_TEXTURE|_NEW_TEXTURE_MATRIX))
570 _mesa_update_texture( ctx, new_state );
572 if (new_state & _NEW_BUFFERS)
573 _mesa_update_framebuffer(ctx);
575 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
576 _mesa_update_draw_buffer_bounds( ctx );
578 if (new_state & _NEW_POLYGON)
579 update_polygon( ctx );
581 if (new_state & _NEW_LIGHT)
582 _mesa_update_lighting( ctx );
584 if (new_state & (_NEW_STENCIL | _NEW_BUFFERS))
585 _mesa_update_stencil( ctx );
587 #if FEATURE_pixel_transfer
588 if (new_state & _MESA_NEW_TRANSFER_STATE)
589 _mesa_update_pixel( ctx, new_state );
592 if (new_state & _DD_NEW_SEPARATE_SPECULAR)
593 update_separate_specular( ctx );
595 if (new_state & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT))
596 update_arrays( ctx );
598 if (new_state & (_NEW_BUFFERS | _NEW_VIEWPORT))
599 update_viewport_matrix(ctx);
601 if (new_state & _NEW_MULTISAMPLE)
602 update_multisample( ctx );
604 if (new_state & _NEW_COLOR)
608 if (new_state & (_NEW_POINT | _NEW_LINE | _NEW_POLYGON | _NEW_LIGHT
609 | _NEW_STENCIL | _DD_NEW_SEPARATE_SPECULAR))
610 update_tricaps( ctx, new_state );
613 /* ctx->_NeedEyeCoords is now up to date.
615 * If the truth value of this variable has changed, update for the
616 * new lighting space and recompute the positions of lights and the
619 * If the lighting space hasn't changed, may still need to recompute
620 * light positions & normal transforms for other reasons.
622 if (new_state & _MESA_NEW_NEED_EYE_COORDS)
623 _mesa_update_tnl_spaces( ctx, new_state );
625 if (new_state & prog_flags) {
626 /* When we generate programs from fixed-function vertex/fragment state
627 * this call may generate/bind a new program. If so, we need to
628 * propogate the _NEW_PROGRAM flag to the driver.
630 new_prog_state |= update_program( ctx );
635 new_prog_state |= update_program_constants(ctx);
638 * Give the driver a chance to act upon the new_state flags.
639 * The driver might plug in different span functions, for example.
640 * Also, this is where the driver can invalidate the state of any
641 * active modules (such as swrast_setup, swrast, tnl, etc).
643 * Set ctx->NewState to zero to avoid recursion if
644 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
646 new_state = ctx->NewState | new_prog_state;
648 ctx->Driver.UpdateState(ctx, new_state);
649 ctx->Array.NewState = 0;
653 /* This is the usual entrypoint for state updates:
656 _mesa_update_state( GLcontext *ctx )
658 _mesa_lock_context_textures(ctx);
659 _mesa_update_state_locked(ctx);
660 _mesa_unlock_context_textures(ctx);
667 * Want to figure out which fragment program inputs are actually
668 * constant/current values from ctx->Current. These should be
669 * referenced as a tracked state variable rather than a fragment
670 * program input, to save the overhead of putting a constant value in
671 * every submitted vertex, transferring it to hardware, interpolating
672 * it across the triangle, etc...
674 * When there is a VP bound, just use vp->outputs. But when we're
675 * generating vp from fixed function state, basically want to
678 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
679 * potential_vp_outputs )
681 * Where potential_vp_outputs is calculated by looking at enabled
684 * The generated fragment program should then only declare inputs that
685 * may vary or otherwise differ from the ctx->Current values.
686 * Otherwise, the fp should track them as state values instead.
689 _mesa_set_varying_vp_inputs( GLcontext *ctx,
690 GLbitfield varying_inputs )
692 if (ctx->varying_vp_inputs != varying_inputs) {
693 ctx->varying_vp_inputs = varying_inputs;
694 ctx->NewState |= _NEW_ARRAY;
695 /*_mesa_printf("%s %x\n", __FUNCTION__, varying_inputs);*/
701 * Used by drivers to tell core Mesa that the driver is going to
702 * install/ use its own vertex program. In particular, this will
703 * prevent generated fragment programs from using state vars instead
704 * of ordinary varyings/inputs.
707 _mesa_set_vp_override(GLcontext *ctx, GLboolean flag)
709 if (ctx->VertexProgram._Overriden != flag) {
710 ctx->VertexProgram._Overriden = flag;
712 /* Set one of the bits which will trigger fragment program
715 ctx->NewState |= _NEW_ARRAY;