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