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 struct gl_context.
38 #include "framebuffer.h"
50 update_separate_specular(struct gl_context
*ctx
)
52 if (_mesa_need_secondary_color(ctx
))
53 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
55 ctx
->_TriangleCaps
&= ~DD_SEPARATE_SPECULAR
;
60 * Helper for update_arrays().
61 * \return min(current min, array->_MaxElement).
64 update_min(GLuint min
, struct gl_client_array
*array
)
66 _mesa_update_array_max_element(array
);
67 return MIN2(min
, array
->_MaxElement
);
72 * Update ctx->Array._MaxElement (the max legal index into all enabled arrays).
73 * Need to do this upon new array state or new buffer object state.
76 update_arrays( struct gl_context
*ctx
)
78 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
81 /* find min of _MaxElement values for all enabled arrays.
82 * Note that the generic arrays always take precedence over
87 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
) {
88 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
]);
92 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
].Enabled
) {
93 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
]);
97 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
].Enabled
) {
98 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
]);
102 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
].Enabled
) {
103 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
]);
107 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
].Enabled
) {
108 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
]);
112 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
].Enabled
) {
113 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
]);
117 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX
].Enabled
) {
118 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX
]);
121 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
].Enabled
) {
122 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
]);
125 /* _MaxElement is one past the last legal array element */
126 arrayObj
->_MaxElement
= min
;
130 update_viewport_matrix(struct gl_context
*ctx
)
132 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
134 ASSERT(depthMax
> 0);
136 /* Compute scale and bias values. This is really driver-specific
137 * and should be maintained elsewhere if at all.
138 * NOTE: RasterPos uses this.
140 _math_matrix_viewport(&ctx
->Viewport
._WindowMap
,
141 ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
142 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
,
143 ctx
->Viewport
.Near
, ctx
->Viewport
.Far
,
149 * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
150 * in ctx->_TriangleCaps if needed.
153 update_polygon(struct gl_context
*ctx
)
155 ctx
->_TriangleCaps
&= ~(DD_TRI_CULL_FRONT_BACK
| DD_TRI_OFFSET
);
157 if (ctx
->Polygon
.CullFlag
&& ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
158 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
160 if ( ctx
->Polygon
.OffsetPoint
161 || ctx
->Polygon
.OffsetLine
162 || ctx
->Polygon
.OffsetFill
)
163 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
168 * Update the ctx->_TriangleCaps bitfield.
169 * XXX that bitfield should really go away someday!
170 * This function must be called after other update_*() functions since
171 * there are dependencies on some other derived values.
175 update_tricaps(struct gl_context
*ctx
, GLbitfield new_state
)
177 ctx
->_TriangleCaps
= 0;
182 if (1/*new_state & _NEW_POINT*/) {
183 if (ctx
->Point
.SmoothFlag
)
184 ctx
->_TriangleCaps
|= DD_POINT_SMOOTH
;
185 if (ctx
->Point
._Attenuated
)
186 ctx
->_TriangleCaps
|= DD_POINT_ATTEN
;
192 if (1/*new_state & _NEW_LINE*/) {
193 if (ctx
->Line
.SmoothFlag
)
194 ctx
->_TriangleCaps
|= DD_LINE_SMOOTH
;
195 if (ctx
->Line
.StippleFlag
)
196 ctx
->_TriangleCaps
|= DD_LINE_STIPPLE
;
202 if (1/*new_state & _NEW_POLYGON*/) {
203 if (ctx
->Polygon
.SmoothFlag
)
204 ctx
->_TriangleCaps
|= DD_TRI_SMOOTH
;
205 if (ctx
->Polygon
.StippleFlag
)
206 ctx
->_TriangleCaps
|= DD_TRI_STIPPLE
;
207 if (ctx
->Polygon
.FrontMode
!= GL_FILL
208 || ctx
->Polygon
.BackMode
!= GL_FILL
)
209 ctx
->_TriangleCaps
|= DD_TRI_UNFILLED
;
210 if (ctx
->Polygon
.CullFlag
211 && ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
212 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
213 if (ctx
->Polygon
.OffsetPoint
||
214 ctx
->Polygon
.OffsetLine
||
215 ctx
->Polygon
.OffsetFill
)
216 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
220 * Lighting and shading
222 if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
)
223 ctx
->_TriangleCaps
|= DD_TRI_LIGHT_TWOSIDE
;
224 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
225 ctx
->_TriangleCaps
|= DD_FLATSHADE
;
226 if (_mesa_need_secondary_color(ctx
))
227 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
233 * Compute derived GL state.
234 * If __struct gl_contextRec::NewState is non-zero then this function \b must
235 * be called before rendering anything.
237 * Calls dd_function_table::UpdateState to perform any internal state
238 * management necessary.
240 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
241 * _mesa_update_buffer_bounds(),
242 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
245 _mesa_update_state_locked( struct gl_context
*ctx
)
247 GLbitfield new_state
= ctx
->NewState
;
249 if (new_state
== _NEW_CURRENT_ATTRIB
)
253 * Now update derived state info
256 if (new_state
& (_NEW_MODELVIEW
|_NEW_PROJECTION
))
257 _mesa_update_modelview_project( ctx
, new_state
);
259 if (new_state
& (_NEW_TEXTURE
|_NEW_TEXTURE_MATRIX
))
260 _mesa_update_texture( ctx
, new_state
);
262 if (new_state
& _NEW_BUFFERS
)
263 _mesa_update_framebuffer(ctx
);
265 if (new_state
& (_NEW_SCISSOR
| _NEW_BUFFERS
| _NEW_VIEWPORT
))
266 _mesa_update_draw_buffer_bounds( ctx
);
268 if (new_state
& _NEW_POLYGON
)
269 update_polygon( ctx
);
271 if (new_state
& _NEW_LIGHT
)
272 _mesa_update_lighting( ctx
);
274 if (new_state
& (_NEW_STENCIL
| _NEW_BUFFERS
))
275 _mesa_update_stencil( ctx
);
277 if (new_state
& _NEW_PIXEL
)
278 _mesa_update_pixel( ctx
, new_state
);
280 if (new_state
& _DD_NEW_SEPARATE_SPECULAR
)
281 update_separate_specular( ctx
);
283 if (new_state
& (_NEW_BUFFERS
| _NEW_VIEWPORT
))
284 update_viewport_matrix(ctx
);
287 if (new_state
& (_NEW_POINT
| _NEW_LINE
| _NEW_POLYGON
| _NEW_LIGHT
288 | _NEW_STENCIL
| _DD_NEW_SEPARATE_SPECULAR
))
289 update_tricaps( ctx
, new_state
);
292 /* ctx->_NeedEyeCoords is now up to date.
294 * If the truth value of this variable has changed, update for the
295 * new lighting space and recompute the positions of lights and the
298 * If the lighting space hasn't changed, may still need to recompute
299 * light positions & normal transforms for other reasons.
301 if (new_state
& _MESA_NEW_NEED_EYE_COORDS
)
302 _mesa_update_tnl_spaces( ctx
, new_state
);
304 if (new_state
& (_NEW_ARRAY
| _NEW_BUFFER_OBJECT
))
305 update_arrays( ctx
);
310 * Give the driver a chance to act upon the new_state flags.
311 * The driver might plug in different span functions, for example.
312 * Also, this is where the driver can invalidate the state of any
313 * active modules (such as swrast_setup, swrast, tnl, etc).
315 * Set ctx->NewState to zero to avoid recursion if
316 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
318 new_state
= ctx
->NewState
;
320 ctx
->Driver
.UpdateState(ctx
, new_state
);
321 ctx
->Array
.NewState
= 0;
322 if (!ctx
->Array
.RebindArrays
)
323 ctx
->Array
.RebindArrays
= (new_state
& _NEW_ARRAY
) != 0;
327 /* This is the usual entrypoint for state updates:
330 _mesa_update_state( struct gl_context
*ctx
)
332 _mesa_lock_context_textures(ctx
);
333 _mesa_update_state_locked(ctx
);
334 _mesa_unlock_context_textures(ctx
);