fixup vertex building code ..
[mesa.git] / src / mesa / tnl / t_vb_program.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
25
26 /**
27 * \file tnl/t_vb_program.c
28 * \brief Pipeline stage for executing NVIDIA vertex programs.
29 * \author Brian Paul, Keith Whitwell
30 */
31
32
33 #include "glheader.h"
34 #include "api_noop.h"
35 #include "colormac.h"
36 #include "context.h"
37 #include "dlist.h"
38 #include "hash.h"
39 #include "light.h"
40 #include "macros.h"
41 #include "imports.h"
42 #include "simple_list.h"
43 #include "mtypes.h"
44 #include "nvvertprog.h"
45 #include "nvvertexec.h"
46 #include "nvprogram.h"
47
48 #include "math/m_translate.h"
49
50 #include "t_context.h"
51 #include "t_pipeline.h"
52
53
54 /**
55 * \warning These values _MUST_ match the values in the OutputRegisters[]
56 * array in vpparse.c!!!
57 */
58 #define VERT_RESULT_HPOS 0
59 #define VERT_RESULT_COL0 1
60 #define VERT_RESULT_COL1 2
61 #define VERT_RESULT_BFC0 3
62 #define VERT_RESULT_BFC1 4
63 #define VERT_RESULT_FOGC 5
64 #define VERT_RESULT_PSIZ 6
65 #define VERT_RESULT_TEX0 7
66 #define VERT_RESULT_TEX1 8
67 #define VERT_RESULT_TEX2 9
68 #define VERT_RESULT_TEX3 10
69 #define VERT_RESULT_TEX4 11
70 #define VERT_RESULT_TEX5 12
71 #define VERT_RESULT_TEX6 13
72 #define VERT_RESULT_TEX7 14
73
74
75 /*!
76 * Private storage for the vertex program pipeline stage.
77 */
78 struct vp_stage_data {
79 /** The results of running the vertex program go into these arrays. */
80 GLvector4f attribs[15];
81
82 GLvector4f ndcCoords; /**< normalized device coords */
83 GLubyte *clipmask; /**< clip flags */
84 GLubyte ormask, andmask; /**< for clipping */
85 };
86
87
88 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
89
90
91 /**
92 * This function executes vertex programs
93 */
94 static GLboolean run_vp( GLcontext *ctx, struct tnl_pipeline_stage *stage )
95 {
96 TNLcontext *tnl = TNL_CONTEXT(ctx);
97 struct vp_stage_data *store = VP_STAGE_DATA(stage);
98 struct vertex_buffer *VB = &tnl->vb;
99 struct vertex_program *program = ctx->VertexProgram.Current;
100 GLuint i;
101
102 _mesa_init_tracked_matrices(ctx); /* load registers with matrices */
103 _mesa_init_vp_registers(ctx); /* init temp and result regs */
104
105 for (i = 0; i < VB->Count; i++) {
106 GLuint attr;
107
108 #if 0
109 printf("Input %d: %f, %f, %f, %f\n", i,
110 VB->AttribPtr[0]->data[i][0],
111 VB->AttribPtr[0]->data[i][1],
112 VB->AttribPtr[0]->data[i][2],
113 VB->AttribPtr[0]->data[i][3]);
114 printf(" color: %f, %f, %f, %f\n",
115 VB->AttribPtr[3]->data[i][0],
116 VB->AttribPtr[3]->data[i][1],
117 VB->AttribPtr[3]->data[i][2],
118 VB->AttribPtr[3]->data[i][3]);
119 printf(" normal: %f, %f, %f, %f\n",
120 VB->AttribPtr[2]->data[i][0],
121 VB->AttribPtr[2]->data[i][1],
122 VB->AttribPtr[2]->data[i][2],
123 VB->AttribPtr[2]->data[i][3]);
124 #endif
125
126 /* the vertex array case */
127 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
128 if (program->InputsRead & (1 << attr)) {
129 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data;
130 const GLuint size = VB->AttribPtr[attr]->size;
131 const GLuint stride = VB->AttribPtr[attr]->stride;
132 const GLfloat *data = (GLfloat *) (ptr + stride * i);
133 ASSIGN_4V(ctx->VertexProgram.Inputs[attr], 0, 0, 0, 1);
134 COPY_SZ_4V(ctx->VertexProgram.Inputs[attr], size, data);
135 }
136 }
137
138 /* execute the program */
139 ASSERT(program);
140 _mesa_exec_vertex_program(ctx, program);
141
142 /* Fixup fog an point size results if needed */
143 if (ctx->Fog.Enabled &&
144 (program->OutputsWritten & (1 << VERT_RESULT_FOGC)) == 0) {
145 ctx->VertexProgram.Outputs[VERT_RESULT_FOGC][0] = 1.0;
146 }
147
148 if (ctx->VertexProgram.PointSizeEnabled &&
149 (program->OutputsWritten & (1 << VERT_RESULT_PSIZ)) == 0) {
150 ctx->VertexProgram.Outputs[VERT_RESULT_PSIZ][0] = ctx->Point.Size;
151 }
152
153 /* copy the output registers into the VB->attribs arrays */
154 /* XXX (optimize) could use a conditional and smaller loop limit here */
155 for (attr = 0; attr < 15; attr++) {
156 COPY_4V(store->attribs[attr].data[i],
157 ctx->VertexProgram.Outputs[attr]);
158 }
159 }
160
161 /* Setup the VB pointers so that the next pipeline stages get
162 * their data from the right place (the program output arrays).
163 */
164 VB->ClipPtr = &store->attribs[VERT_RESULT_HPOS];
165 VB->ClipPtr->size = 4;
166 VB->ClipPtr->count = VB->Count;
167 VB->ColorPtr[0] = &store->attribs[VERT_RESULT_COL0];
168 VB->ColorPtr[1] = &store->attribs[VERT_RESULT_BFC0];
169 VB->SecondaryColorPtr[0] = &store->attribs[VERT_RESULT_COL1];
170 VB->SecondaryColorPtr[1] = &store->attribs[VERT_RESULT_BFC1];
171 VB->FogCoordPtr = &store->attribs[VERT_RESULT_FOGC];
172 VB->PointSizePtr = &store->attribs[VERT_RESULT_PSIZ];
173
174 VB->AttribPtr[VERT_ATTRIB_COLOR0] = VB->ColorPtr[0];
175 VB->AttribPtr[VERT_ATTRIB_COLOR1] = VB->SecondaryColorPtr[0];
176 VB->AttribPtr[VERT_ATTRIB_FOG] = VB->FogCoordPtr;
177 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->attribs[VERT_RESULT_PSIZ];
178
179 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
180 VB->AttribPtr[VERT_ATTRIB_TEX0+i] = VB->TexCoordPtr[i] =
181 &store->attribs[VERT_RESULT_TEX0 + i];
182 }
183
184
185
186 /* Cliptest and perspective divide. Clip functions must clear
187 * the clipmask.
188 */
189 store->ormask = 0;
190 store->andmask = CLIP_ALL_BITS;
191
192 if (tnl->NeedNdcCoords) {
193 VB->NdcPtr =
194 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
195 &store->ndcCoords,
196 store->clipmask,
197 &store->ormask,
198 &store->andmask );
199 }
200 else {
201 VB->NdcPtr = 0;
202 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
203 0,
204 store->clipmask,
205 &store->ormask,
206 &store->andmask );
207 }
208
209 if (store->andmask) /* All vertices are outside the frustum */
210 return GL_FALSE;
211
212
213 /* This is where we'd do clip testing against the user-defined
214 * clipping planes, but they're not supported by vertex programs.
215 */
216
217 VB->ClipOrMask = store->ormask;
218 VB->ClipMask = store->clipmask;
219
220 return GL_TRUE;
221 }
222
223
224 /**
225 * This function validates stuff.
226 */
227 static GLboolean run_validate_program( GLcontext *ctx,
228 struct tnl_pipeline_stage *stage )
229 {
230 #if 000
231 /* XXX do we need any validation for vertex programs? */
232 GLuint ind = 0;
233 light_func *tab;
234
235 if (ctx->Visual.rgbMode) {
236 if (ctx->Light._NeedVertices) {
237 if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
238 tab = _tnl_light_spec_tab;
239 else
240 tab = _tnl_light_tab;
241 }
242 else {
243 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
244 tab = _tnl_light_fast_single_tab;
245 else
246 tab = _tnl_light_fast_tab;
247 }
248 }
249 else
250 tab = _tnl_light_ci_tab;
251
252 if (ctx->Light.ColorMaterialEnabled)
253 ind |= LIGHT_COLORMATERIAL;
254
255 if (ctx->Light.Model.TwoSide)
256 ind |= LIGHT_TWOSIDE;
257
258 VP_STAGE_DATA(stage)->light_func_tab = &tab[ind];
259
260 /* This and the above should only be done on _NEW_LIGHT:
261 */
262 _mesa_validate_all_lighting_tables( ctx );
263 #endif
264
265 /* Now run the stage...
266 */
267 stage->run = run_vp;
268 return stage->run( ctx, stage );
269 }
270
271
272
273 /**
274 * Called the first time stage->run is called. In effect, don't
275 * allocate data until the first time the stage is run.
276 */
277 static GLboolean run_init_vp( GLcontext *ctx,
278 struct tnl_pipeline_stage *stage )
279 {
280 TNLcontext *tnl = TNL_CONTEXT(ctx);
281 struct vertex_buffer *VB = &(tnl->vb);
282 struct vp_stage_data *store;
283 const GLuint size = VB->Size;
284 GLuint i;
285
286 stage->privatePtr = MALLOC(sizeof(*store));
287 store = VP_STAGE_DATA(stage);
288 if (!store)
289 return GL_FALSE;
290
291 /* Allocate arrays of vertex output values */
292 for (i = 0; i < 15; i++) {
293 _mesa_vector4f_alloc( &store->attribs[i], 0, size, 32 );
294 store->attribs[i].size = 4;
295 }
296
297 /* a few other misc allocations */
298 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 );
299 store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
300
301 /* Now validate the stage derived data...
302 */
303 stage->run = run_validate_program;
304 return stage->run( ctx, stage );
305 }
306
307
308
309 /**
310 * Check if vertex program mode is enabled.
311 * If so, configure the pipeline stage's type, inputs, and outputs.
312 */
313 static void check_vp( GLcontext *ctx, struct tnl_pipeline_stage *stage )
314 {
315 stage->active = ctx->VertexProgram.Enabled;
316
317 if (stage->active) {
318 /* Set stage->inputs equal to the bitmask of vertex attributes
319 * which the program needs for inputs.
320 */
321 stage->inputs = ctx->VertexProgram.Current->InputsRead;
322 }
323 }
324
325
326 /**
327 * Destructor for this pipeline stage.
328 */
329 static void dtr( struct tnl_pipeline_stage *stage )
330 {
331 struct vp_stage_data *store = VP_STAGE_DATA(stage);
332
333 if (store) {
334 GLuint i;
335
336 /* free the vertex program result arrays */
337 for (i = 0; i < 15; i++)
338 _mesa_vector4f_free( &store->attribs[i] );
339
340 /* free misc arrays */
341 _mesa_vector4f_free( &store->ndcCoords );
342 ALIGN_FREE( store->clipmask );
343
344 FREE( store );
345 stage->privatePtr = 0;
346 }
347 }
348
349 /**
350 * Public description of this pipeline stage.
351 */
352 const struct tnl_pipeline_stage _tnl_vertex_program_stage =
353 {
354 "vertex-program",
355 _NEW_ALL, /*XXX FIX */ /* recheck */
356 _NEW_ALL, /*XXX FIX */ /* recalc */
357 GL_FALSE, /* active */
358 0, /* inputs - calculated on the fly */
359 _TNL_BITS_PROG_ANY, /* outputs -- could calculate */
360 0, /* changed_inputs */
361 NULL, /* private_data */
362 dtr, /* destroy */
363 check_vp, /* check */
364 run_init_vp /* run -- initially set to ctr */
365 };