change an assertion
[mesa.git] / src / mesa / tnl / t_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 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 * 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.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28
29 #include "api_arrayelt.h"
30 #include "glheader.h"
31 #include "imports.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "mtypes.h"
35 #include "dlist.h"
36 #include "light.h"
37 #include "vtxfmt.h"
38
39 #include "tnl.h"
40 #include "t_array_api.h"
41 #include "t_context.h"
42 #include "t_pipeline.h"
43 #include "t_save_api.h"
44 #include "t_vp_build.h"
45 #include "t_vtx_api.h"
46
47
48
49 static void
50 install_driver_callbacks( GLcontext *ctx )
51 {
52 ctx->Driver.NewList = _tnl_NewList;
53 ctx->Driver.EndList = _tnl_EndList;
54 ctx->Driver.FlushVertices = _tnl_FlushVertices;
55 ctx->Driver.SaveFlushVertices = _tnl_SaveFlushVertices;
56 ctx->Driver.BeginCallList = _tnl_BeginCallList;
57 ctx->Driver.EndCallList = _tnl_EndCallList;
58 }
59
60
61
62 GLboolean
63 _tnl_CreateContext( GLcontext *ctx )
64 {
65 TNLcontext *tnl;
66
67 /* Create the TNLcontext structure
68 */
69 ctx->swtnl_context = tnl = (TNLcontext *) CALLOC( sizeof(TNLcontext) );
70
71 if (!tnl) {
72 return GL_FALSE;
73 }
74
75 if (_mesa_getenv("MESA_CODEGEN"))
76 tnl->AllowCodegen = GL_TRUE;
77
78 /* Initialize the VB.
79 */
80 tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
81
82
83 /* Initialize tnl state and tnl->vtxfmt.
84 */
85 _tnl_save_init( ctx );
86 _tnl_array_init( ctx );
87 _tnl_vtx_init( ctx );
88
89 if (ctx->_MaintainTnlProgram) {
90 tnl->vp_cache = MALLOC(sizeof(*tnl->vp_cache));
91 tnl->vp_cache->size = 5;
92 tnl->vp_cache->n_items = 0;
93 tnl->vp_cache->items = MALLOC(tnl->vp_cache->size *
94 sizeof(*tnl->vp_cache->items));
95 _mesa_memset(tnl->vp_cache->items, 0, tnl->vp_cache->size *
96 sizeof(*tnl->vp_cache->items));
97
98 _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
99 } else {
100 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
101 }
102
103 /* Initialize the arrayelt helper
104 */
105 if (!_ae_create_context( ctx ))
106 return GL_FALSE;
107
108
109 tnl->NeedNdcCoords = GL_TRUE;
110 tnl->LoopbackDListCassettes = GL_FALSE;
111 tnl->CalcDListNormalLengths = GL_TRUE;
112 tnl->AllowVertexFog = GL_TRUE;
113 tnl->AllowPixelFog = GL_TRUE;
114
115 /* Hook our functions into exec and compile dispatch tables.
116 */
117 _mesa_install_exec_vtxfmt( ctx, &tnl->exec_vtxfmt );
118
119
120 /* Set a few default values in the driver struct.
121 */
122 install_driver_callbacks(ctx);
123 ctx->Driver.NeedFlush = 0;
124 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
125 ctx->Driver.CurrentSavePrimitive = PRIM_UNKNOWN;
126
127 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
128 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
129 tnl->Driver.NotifyMaterialChange = _mesa_validate_all_lighting_tables;
130
131 return GL_TRUE;
132 }
133
134
135 void
136 _tnl_DestroyContext( GLcontext *ctx )
137 {
138 TNLcontext *tnl = TNL_CONTEXT(ctx);
139
140 _tnl_array_destroy( ctx );
141 _tnl_vtx_destroy( ctx );
142 _tnl_save_destroy( ctx );
143 _tnl_destroy_pipeline( ctx );
144 _ae_destroy_context( ctx );
145
146 if (ctx->_MaintainTnlProgram)
147 _tnl_ProgramCacheDestroy( ctx );
148
149 FREE(tnl);
150 ctx->swtnl_context = NULL;
151 }
152
153
154 void
155 _tnl_InvalidateState( GLcontext *ctx, GLuint new_state )
156 {
157 TNLcontext *tnl = TNL_CONTEXT(ctx);
158
159 if (new_state & (_NEW_HINT)) {
160 ASSERT(tnl->AllowVertexFog || tnl->AllowPixelFog);
161 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
162 || !tnl->AllowPixelFog;
163 }
164
165 _ae_invalidate_state(ctx, new_state);
166
167 tnl->pipeline.new_state |= new_state;
168 tnl->vtx.eval.new_state |= new_state;
169
170 /* Calculate tnl->render_inputs:
171 */
172 if (ctx->Visual.rgbMode) {
173 tnl->render_inputs = (_TNL_BIT_POS|
174 _TNL_BIT_COLOR0|
175 (ctx->Texture._EnabledCoordUnits << _TNL_ATTRIB_TEX0));
176
177 if (NEED_SECONDARY_COLOR(ctx))
178 tnl->render_inputs |= _TNL_BIT_COLOR1;
179 }
180 else {
181 tnl->render_inputs |= (_TNL_BIT_POS|_TNL_BIT_INDEX);
182 }
183
184 if (ctx->Fog.Enabled ||
185 (ctx->FragmentProgram._Active &&
186 ctx->FragmentProgram._Current->FogOption != GL_NONE))
187 tnl->render_inputs |= _TNL_BIT_FOG;
188
189 if (ctx->Polygon.FrontMode != GL_FILL ||
190 ctx->Polygon.BackMode != GL_FILL)
191 tnl->render_inputs |= _TNL_BIT_EDGEFLAG;
192
193 if (ctx->RenderMode == GL_FEEDBACK)
194 tnl->render_inputs |= _TNL_BIT_TEX0;
195
196 if (ctx->Point._Attenuated ||
197 (ctx->VertexProgram._Enabled && ctx->VertexProgram.PointSizeEnabled))
198 tnl->render_inputs |= _TNL_BIT_POINTSIZE;
199 }
200
201
202 void
203 _tnl_wakeup_exec( GLcontext *ctx )
204 {
205 TNLcontext *tnl = TNL_CONTEXT(ctx);
206
207 install_driver_callbacks(ctx);
208 ctx->Driver.NeedFlush |= FLUSH_UPDATE_CURRENT;
209
210 /* Hook our functions into exec and compile dispatch tables.
211 */
212 _mesa_install_exec_vtxfmt( ctx, &tnl->exec_vtxfmt );
213
214 /* Assume we haven't been getting state updates either:
215 */
216 _tnl_InvalidateState( ctx, ~0 );
217
218 if (ctx->Light.ColorMaterialEnabled) {
219 _mesa_update_color_material( ctx,
220 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
221 }
222 }
223
224
225 void
226 _tnl_wakeup_save_exec( GLcontext *ctx )
227 {
228 TNLcontext *tnl = TNL_CONTEXT(ctx);
229
230 _tnl_wakeup_exec( ctx );
231 _mesa_install_save_vtxfmt( ctx, &tnl->save_vtxfmt );
232 }
233
234
235 /**
236 * Drivers call this function to tell the TCL module whether or not
237 * it wants Normalized Device Coords (NDC) computed. I.e. whether
238 * we should "Divide-by-W". Software renders will want that.
239 */
240 void
241 _tnl_need_projected_coords( GLcontext *ctx, GLboolean mode )
242 {
243 TNLcontext *tnl = TNL_CONTEXT(ctx);
244 if (tnl->NeedNdcCoords != mode) {
245 tnl->NeedNdcCoords = mode;
246 _tnl_InvalidateState( ctx, _NEW_PROJECTION );
247 }
248 }
249
250 void
251 _tnl_need_dlist_loopback( GLcontext *ctx, GLboolean mode )
252 {
253 TNLcontext *tnl = TNL_CONTEXT(ctx);
254 tnl->LoopbackDListCassettes = mode;
255 }
256
257 void
258 _tnl_need_dlist_norm_lengths( GLcontext *ctx, GLboolean mode )
259 {
260 TNLcontext *tnl = TNL_CONTEXT(ctx);
261 tnl->CalcDListNormalLengths = mode;
262 }
263
264 void
265 _tnl_isolate_materials( GLcontext *ctx, GLboolean mode )
266 {
267 TNLcontext *tnl = TNL_CONTEXT(ctx);
268 tnl->IsolateMaterials = mode;
269 }
270
271 void
272 _tnl_allow_vertex_fog( GLcontext *ctx, GLboolean value )
273 {
274 TNLcontext *tnl = TNL_CONTEXT(ctx);
275 tnl->AllowVertexFog = value;
276 }
277
278 void
279 _tnl_allow_pixel_fog( GLcontext *ctx, GLboolean value )
280 {
281 TNLcontext *tnl = TNL_CONTEXT(ctx);
282 tnl->AllowPixelFog = value;
283 }
284