reindent, doxygen-style comments
[mesa.git] / src / mesa / tnl / t_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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_ProgramCacheInit( ctx );
91 _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
92 } else {
93 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
94 }
95
96 /* Initialize the arrayelt helper
97 */
98 if (!_ae_create_context( ctx ))
99 return GL_FALSE;
100
101
102 tnl->NeedNdcCoords = GL_TRUE;
103 tnl->LoopbackDListCassettes = GL_FALSE;
104 tnl->CalcDListNormalLengths = GL_TRUE;
105 tnl->AllowVertexFog = GL_TRUE;
106 tnl->AllowPixelFog = GL_TRUE;
107
108 /* Hook our functions into exec and compile dispatch tables.
109 */
110 _mesa_install_exec_vtxfmt( ctx, &tnl->exec_vtxfmt );
111
112
113 /* Set a few default values in the driver struct.
114 */
115 install_driver_callbacks(ctx);
116 ctx->Driver.NeedFlush = 0;
117 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
118 ctx->Driver.CurrentSavePrimitive = PRIM_UNKNOWN;
119
120 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
121 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
122 tnl->Driver.NotifyMaterialChange = _mesa_validate_all_lighting_tables;
123
124 return GL_TRUE;
125 }
126
127
128 void
129 _tnl_DestroyContext( GLcontext *ctx )
130 {
131 TNLcontext *tnl = TNL_CONTEXT(ctx);
132
133 _tnl_array_destroy( ctx );
134 _tnl_vtx_destroy( ctx );
135 _tnl_save_destroy( ctx );
136 _tnl_destroy_pipeline( ctx );
137 _ae_destroy_context( ctx );
138
139 if (ctx->_MaintainTnlProgram)
140 _tnl_ProgramCacheDestroy( ctx );
141
142 FREE(tnl);
143 ctx->swtnl_context = NULL;
144 }
145
146
147 void
148 _tnl_InvalidateState( GLcontext *ctx, GLuint new_state )
149 {
150 TNLcontext *tnl = TNL_CONTEXT(ctx);
151
152 if (new_state & (_NEW_HINT)) {
153 ASSERT(tnl->AllowVertexFog || tnl->AllowPixelFog);
154 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
155 || !tnl->AllowPixelFog;
156 }
157
158 _ae_invalidate_state(ctx, new_state);
159
160 tnl->pipeline.new_state |= new_state;
161 tnl->vtx.eval.new_state |= new_state;
162
163 /* Calculate tnl->render_inputs:
164 */
165 if (ctx->Visual.rgbMode) {
166 GLuint i;
167
168 RENDERINPUTS_ZERO( tnl->render_inputs_bitset );
169 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
170 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR0 );
171 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
172 if (ctx->Texture._EnabledCoordUnits & (1 << i)) {
173 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX(i) );
174 }
175 }
176
177 if (NEED_SECONDARY_COLOR(ctx))
178 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR1 );
179 }
180 else {
181 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
182 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR_INDEX );
183 }
184
185 if (ctx->Fog.Enabled ||
186 (ctx->FragmentProgram._Active &&
187 (ctx->FragmentProgram._Current->FogOption != GL_NONE ||
188 ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_FOGC)))
189 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_FOG );
190
191 if (ctx->Polygon.FrontMode != GL_FILL ||
192 ctx->Polygon.BackMode != GL_FILL)
193 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_EDGEFLAG );
194
195 if (ctx->RenderMode == GL_FEEDBACK)
196 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX0 );
197
198 if (ctx->Point._Attenuated ||
199 (ctx->VertexProgram._Enabled && ctx->VertexProgram.PointSizeEnabled))
200 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POINTSIZE );
201
202 if (ctx->ShaderObjects._VertexShaderPresent || ctx->ShaderObjects._FragmentShaderPresent)
203 RENDERINPUTS_SET_RANGE( tnl->render_inputs_bitset, _TNL_FIRST_GENERIC, _TNL_LAST_GENERIC );
204 }
205
206
207 void
208 _tnl_wakeup_exec( GLcontext *ctx )
209 {
210 TNLcontext *tnl = TNL_CONTEXT(ctx);
211
212 install_driver_callbacks(ctx);
213 ctx->Driver.NeedFlush |= FLUSH_UPDATE_CURRENT;
214
215 /* Hook our functions into exec and compile dispatch tables.
216 */
217 _mesa_install_exec_vtxfmt( ctx, &tnl->exec_vtxfmt );
218
219 /* Assume we haven't been getting state updates either:
220 */
221 _tnl_InvalidateState( ctx, ~0 );
222
223 if (ctx->Light.ColorMaterialEnabled) {
224 _mesa_update_color_material( ctx,
225 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
226 }
227 }
228
229
230 void
231 _tnl_wakeup_save_exec( GLcontext *ctx )
232 {
233 TNLcontext *tnl = TNL_CONTEXT(ctx);
234
235 _tnl_wakeup_exec( ctx );
236 _mesa_install_save_vtxfmt( ctx, &tnl->save_vtxfmt );
237 }
238
239
240 /**
241 * Drivers call this function to tell the TCL module whether or not
242 * it wants Normalized Device Coords (NDC) computed. I.e. whether
243 * we should "Divide-by-W". Software renders will want that.
244 */
245 void
246 _tnl_need_projected_coords( GLcontext *ctx, GLboolean mode )
247 {
248 TNLcontext *tnl = TNL_CONTEXT(ctx);
249 if (tnl->NeedNdcCoords != mode) {
250 tnl->NeedNdcCoords = mode;
251 _tnl_InvalidateState( ctx, _NEW_PROJECTION );
252 }
253 }
254
255 void
256 _tnl_need_dlist_loopback( GLcontext *ctx, GLboolean mode )
257 {
258 TNLcontext *tnl = TNL_CONTEXT(ctx);
259 tnl->LoopbackDListCassettes = mode;
260 }
261
262 void
263 _tnl_need_dlist_norm_lengths( GLcontext *ctx, GLboolean mode )
264 {
265 TNLcontext *tnl = TNL_CONTEXT(ctx);
266 tnl->CalcDListNormalLengths = mode;
267 }
268
269 void
270 _tnl_isolate_materials( GLcontext *ctx, GLboolean mode )
271 {
272 TNLcontext *tnl = TNL_CONTEXT(ctx);
273 tnl->IsolateMaterials = mode;
274 }
275
276 void
277 _tnl_allow_vertex_fog( GLcontext *ctx, GLboolean value )
278 {
279 TNLcontext *tnl = TNL_CONTEXT(ctx);
280 tnl->AllowVertexFog = value;
281 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
282 || !tnl->AllowPixelFog;
283
284 }
285
286 void
287 _tnl_allow_pixel_fog( GLcontext *ctx, GLboolean value )
288 {
289 TNLcontext *tnl = TNL_CONTEXT(ctx);
290 tnl->AllowPixelFog = value;
291 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
292 || !tnl->AllowPixelFog;
293 }
294