Re-commit t_vertex.[ch] changes to fd.o server.
[mesa.git] / src / mesa / tnl / t_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 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 "t_context.h"
41 #include "t_array_api.h"
42 #include "t_vtx_api.h"
43 #include "t_save_api.h"
44 #include "t_pipeline.h"
45 #include "tnl.h"
46
47
48
49 void
50 _tnl_MakeCurrent( GLcontext *ctx,
51 GLframebuffer *drawBuffer,
52 GLframebuffer *readBuffer )
53 {
54 }
55
56
57 static void
58 install_driver_callbacks( GLcontext *ctx )
59 {
60 ctx->Driver.NewList = _tnl_NewList;
61 ctx->Driver.EndList = _tnl_EndList;
62 ctx->Driver.FlushVertices = _tnl_FlushVertices;
63 ctx->Driver.SaveFlushVertices = _tnl_SaveFlushVertices;
64 ctx->Driver.MakeCurrent = _tnl_MakeCurrent;
65 ctx->Driver.BeginCallList = _tnl_BeginCallList;
66 ctx->Driver.EndCallList = _tnl_EndCallList;
67 }
68
69
70
71 GLboolean
72 _tnl_CreateContext( GLcontext *ctx )
73 {
74 TNLcontext *tnl;
75
76 /* Create the TNLcontext structure
77 */
78 ctx->swtnl_context = tnl = (TNLcontext *) CALLOC( sizeof(TNLcontext) );
79
80 if (!tnl) {
81 return GL_FALSE;
82 }
83
84 /* Initialize the VB.
85 */
86 tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
87
88
89 /* Initialize tnl state and tnl->vtxfmt.
90 */
91 _tnl_save_init( ctx );
92 _tnl_array_init( ctx );
93 _tnl_vtx_init( ctx );
94 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
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
106 /* Hook our functions into exec and compile dispatch tables.
107 */
108 _mesa_install_exec_vtxfmt( ctx, &tnl->exec_vtxfmt );
109
110
111 /* Set a few default values in the driver struct.
112 */
113 install_driver_callbacks(ctx);
114 ctx->Driver.NeedFlush = 0;
115 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
116 ctx->Driver.CurrentSavePrimitive = PRIM_UNKNOWN;
117
118 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
119 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
120 tnl->Driver.NotifyMaterialChange = _mesa_validate_all_lighting_tables;
121
122 return GL_TRUE;
123 }
124
125
126 void
127 _tnl_DestroyContext( GLcontext *ctx )
128 {
129 TNLcontext *tnl = TNL_CONTEXT(ctx);
130
131 _tnl_array_destroy( ctx );
132 _tnl_vtx_destroy( ctx );
133 _tnl_save_destroy( ctx );
134 _tnl_destroy_pipeline( ctx );
135 _ae_destroy_context( ctx );
136
137 FREE(tnl);
138 ctx->swtnl_context = 0;
139 }
140
141
142 void
143 _tnl_InvalidateState( GLcontext *ctx, GLuint new_state )
144 {
145 TNLcontext *tnl = TNL_CONTEXT(ctx);
146
147 if (new_state & _NEW_ARRAY) {
148 tnl->pipeline.run_input_changes |= ctx->Array.NewState; /* overkill */
149 }
150
151 _ae_invalidate_state(ctx, new_state);
152
153 tnl->pipeline.run_state_changes |= new_state;
154 tnl->pipeline.build_state_changes |= (new_state &
155 tnl->pipeline.build_state_trigger);
156
157 tnl->vtx.eval.new_state |= new_state;
158
159 /* Calculate tnl->render_inputs:
160 */
161 if (ctx->Visual.rgbMode) {
162 tnl->render_inputs = (_TNL_BIT_POS|
163 _TNL_BIT_COLOR0|
164 (ctx->Texture._EnabledUnits << _TNL_ATTRIB_TEX0));
165
166 if (NEED_SECONDARY_COLOR(ctx))
167 tnl->render_inputs |= _TNL_BIT_COLOR1;
168 }
169 else {
170 tnl->render_inputs |= (_TNL_BIT_POS|_TNL_BIT_INDEX);
171 }
172
173 if (ctx->Fog.Enabled)
174 tnl->render_inputs |= _TNL_BIT_FOG;
175
176 if (ctx->Polygon.FrontMode != GL_FILL ||
177 ctx->Polygon.BackMode != GL_FILL)
178 tnl->render_inputs |= _TNL_BIT_EDGEFLAG;
179
180 if (ctx->RenderMode == GL_FEEDBACK)
181 tnl->render_inputs |= _TNL_BIT_TEX0;
182 }
183
184
185 void
186 _tnl_wakeup_exec( GLcontext *ctx )
187 {
188 TNLcontext *tnl = TNL_CONTEXT(ctx);
189
190 install_driver_callbacks(ctx);
191 ctx->Driver.NeedFlush |= FLUSH_UPDATE_CURRENT;
192
193 /* Hook our functions into exec and compile dispatch tables.
194 */
195 _mesa_install_exec_vtxfmt( ctx, &tnl->exec_vtxfmt );
196
197 /* Call all appropriate driver callbacks to revive state.
198 */
199 _tnl_MakeCurrent( ctx, ctx->DrawBuffer, ctx->ReadBuffer );
200
201 /* Assume we haven't been getting state updates either:
202 */
203 _tnl_InvalidateState( ctx, ~0 );
204 tnl->pipeline.run_input_changes = ~0;
205
206 if (ctx->Light.ColorMaterialEnabled) {
207 _mesa_update_color_material( ctx,
208 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
209 }
210 }
211
212
213 void
214 _tnl_wakeup_save_exec( GLcontext *ctx )
215 {
216 TNLcontext *tnl = TNL_CONTEXT(ctx);
217
218 _tnl_wakeup_exec( ctx );
219 _mesa_install_save_vtxfmt( ctx, &tnl->save_vtxfmt );
220 }
221
222
223 /**
224 * Drivers call this function to tell the TCL module whether or not
225 * it wants Normalized Device Coords (NDC) computed. I.e. whether
226 * we should "Divide-by-W". Software renders will want that.
227 */
228 void
229 _tnl_need_projected_coords( GLcontext *ctx, GLboolean mode )
230 {
231 TNLcontext *tnl = TNL_CONTEXT(ctx);
232 if (tnl->NeedNdcCoords != mode) {
233 tnl->NeedNdcCoords = mode;
234 _tnl_InvalidateState( ctx, _NEW_PROJECTION );
235 }
236 }
237
238 void
239 _tnl_need_dlist_loopback( GLcontext *ctx, GLboolean mode )
240 {
241 TNLcontext *tnl = TNL_CONTEXT(ctx);
242 tnl->LoopbackDListCassettes = mode;
243 }
244
245 void
246 _tnl_need_dlist_norm_lengths( GLcontext *ctx, GLboolean mode )
247 {
248 TNLcontext *tnl = TNL_CONTEXT(ctx);
249 tnl->CalcDListNormalLengths = mode;
250 }
251
252 void
253 _tnl_isolate_materials( GLcontext *ctx, GLboolean mode )
254 {
255 TNLcontext *tnl = TNL_CONTEXT(ctx);
256 tnl->IsolateMaterials = mode;
257 }