f15f9d861971c3174261681afd55e8ed62dc79df
[mesa.git] / src / mesa / tnl / t_vb_program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 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 vertex programs.
29 * \author Brian Paul, Keith Whitwell
30 */
31
32
33 #include "glheader.h"
34 #include "colormac.h"
35 #include "context.h"
36 #include "macros.h"
37 #include "imports.h"
38 #include "shader/prog_instruction.h"
39 #include "shader/prog_statevars.h"
40 #include "shader/prog_execute.h"
41 #include "swrast/s_context.h"
42 #include "swrast/s_texfilter.h"
43
44 #include "tnl.h"
45 #include "t_context.h"
46 #include "t_pipeline.h"
47
48
49
50 /*!
51 * Private storage for the vertex program pipeline stage.
52 */
53 struct vp_stage_data {
54 /** The results of running the vertex program go into these arrays. */
55 GLvector4f results[VERT_RESULT_MAX];
56
57 GLvector4f ndcCoords; /**< normalized device coords */
58 GLubyte *clipmask; /**< clip flags */
59 GLubyte ormask, andmask; /**< for clipping */
60 };
61
62
63 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
64
65
66 /**
67 * XXX the texture sampling code in this module is a bit of a hack.
68 * The texture sampling code is in swrast, though it doesn't have any
69 * real dependencies on the rest of swrast. It should probably be
70 * moved into main/ someday.
71 */
72
73 static void userclip( GLcontext *ctx,
74 GLvector4f *clip,
75 GLubyte *clipmask,
76 GLubyte *clipormask,
77 GLubyte *clipandmask )
78 {
79 GLuint p;
80
81 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
82 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
83 GLuint nr, i;
84 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
85 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
86 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
87 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
88 GLfloat *coord = (GLfloat *)clip->data;
89 GLuint stride = clip->stride;
90 GLuint count = clip->count;
91
92 for (nr = 0, i = 0 ; i < count ; i++) {
93 GLfloat dp = (coord[0] * a +
94 coord[1] * b +
95 coord[2] * c +
96 coord[3] * d);
97
98 if (dp < 0) {
99 nr++;
100 clipmask[i] |= CLIP_USER_BIT;
101 }
102
103 STRIDE_F(coord, stride);
104 }
105
106 if (nr > 0) {
107 *clipormask |= CLIP_USER_BIT;
108 if (nr == count) {
109 *clipandmask |= CLIP_USER_BIT;
110 return;
111 }
112 }
113 }
114 }
115 }
116
117
118 static GLboolean
119 do_ndc_cliptest(GLcontext *ctx, struct vp_stage_data *store)
120 {
121 TNLcontext *tnl = TNL_CONTEXT(ctx);
122 struct vertex_buffer *VB = &tnl->vb;
123 /* Cliptest and perspective divide. Clip functions must clear
124 * the clipmask.
125 */
126 store->ormask = 0;
127 store->andmask = CLIP_FRUSTUM_BITS;
128
129 if (tnl->NeedNdcCoords) {
130 VB->NdcPtr =
131 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
132 &store->ndcCoords,
133 store->clipmask,
134 &store->ormask,
135 &store->andmask );
136 }
137 else {
138 VB->NdcPtr = NULL;
139 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
140 NULL,
141 store->clipmask,
142 &store->ormask,
143 &store->andmask );
144 }
145
146 if (store->andmask) {
147 /* All vertices are outside the frustum */
148 return GL_FALSE;
149 }
150
151 /* Test userclip planes. This contributes to VB->ClipMask.
152 */
153 /** XXX NEW_SLANG _Enabled ??? */
154 if (ctx->Transform.ClipPlanesEnabled && (!ctx->VertexProgram._Enabled ||
155 ctx->VertexProgram.Current->IsPositionInvariant)) {
156 userclip( ctx,
157 VB->ClipPtr,
158 store->clipmask,
159 &store->ormask,
160 &store->andmask );
161
162 if (store->andmask) {
163 return GL_FALSE;
164 }
165 }
166
167 VB->ClipAndMask = store->andmask;
168 VB->ClipOrMask = store->ormask;
169 VB->ClipMask = store->clipmask;
170
171 return GL_TRUE;
172 }
173
174
175 static void
176 vp_fetch_texel(GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
177 GLuint unit, GLfloat color[4])
178 {
179 GLchan rgba[4];
180 SWcontext *swrast = SWRAST_CONTEXT(ctx);
181
182 /* XXX use a float-valued TextureSample routine here!!! */
183 swrast->TextureSample[unit](ctx, ctx->Texture.Unit[unit]._Current,
184 1, (const GLfloat (*)[4]) texcoord,
185 &lambda, &rgba);
186 color[0] = CHAN_TO_FLOAT(rgba[0]);
187 color[1] = CHAN_TO_FLOAT(rgba[1]);
188 color[2] = CHAN_TO_FLOAT(rgba[2]);
189 color[3] = CHAN_TO_FLOAT(rgba[3]);
190 }
191
192
193 /**
194 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
195 * string has been parsed.
196 */
197 void
198 _tnl_program_string(GLcontext *ctx, GLenum target, struct gl_program *program)
199 {
200 /* No-op.
201 * If we had derived anything from the program that was private to this
202 * stage we'd recompute/validate it here.
203 */
204 }
205
206
207 /**
208 * Initialize virtual machine state prior to executing vertex program.
209 */
210 static void
211 init_machine(GLcontext *ctx, struct gl_program_machine *machine)
212 {
213 /* Input registers get initialized from the current vertex attribs */
214 MEMCPY(machine->VertAttribs, ctx->Current.Attrib,
215 MAX_VERTEX_PROGRAM_ATTRIBS * 4 * sizeof(GLfloat));
216
217 if (ctx->VertexProgram._Current->IsNVProgram) {
218 GLuint i;
219 /* Output/result regs are initialized to [0,0,0,1] */
220 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_OUTPUTS; i++) {
221 ASSIGN_4V(machine->Outputs[i], 0.0F, 0.0F, 0.0F, 1.0F);
222 }
223 /* Temp regs are initialized to [0,0,0,0] */
224 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_TEMPS; i++) {
225 ASSIGN_4V(machine->Temporaries[i], 0.0F, 0.0F, 0.0F, 0.0F);
226 }
227 for (i = 0; i < MAX_VERTEX_PROGRAM_ADDRESS_REGS; i++) {
228 ASSIGN_4V(machine->AddressReg[i], 0, 0, 0, 0);
229 }
230 }
231
232 machine->NumDeriv = 0;
233
234 /* init condition codes */
235 machine->CondCodes[0] = COND_EQ;
236 machine->CondCodes[1] = COND_EQ;
237 machine->CondCodes[2] = COND_EQ;
238 machine->CondCodes[3] = COND_EQ;
239
240 /* init call stack */
241 machine->StackDepth = 0;
242
243 machine->FetchTexelLod = vp_fetch_texel;
244 machine->FetchTexelDeriv = NULL; /* not used by vertex programs */
245 }
246
247
248 /**
249 * Copy the 16 elements of a matrix into four consecutive program
250 * registers starting at 'pos'.
251 */
252 static void
253 load_matrix(GLfloat registers[][4], GLuint pos, const GLfloat mat[16])
254 {
255 GLuint i;
256 for (i = 0; i < 4; i++) {
257 registers[pos + i][0] = mat[0 + i];
258 registers[pos + i][1] = mat[4 + i];
259 registers[pos + i][2] = mat[8 + i];
260 registers[pos + i][3] = mat[12 + i];
261 }
262 }
263
264
265 /**
266 * As above, but transpose the matrix.
267 */
268 static void
269 load_transpose_matrix(GLfloat registers[][4], GLuint pos,
270 const GLfloat mat[16])
271 {
272 MEMCPY(registers[pos], mat, 16 * sizeof(GLfloat));
273 }
274
275
276 /**
277 * Load current vertex program's parameter registers with tracked
278 * matrices (if NV program). This only needs to be done per
279 * glBegin/glEnd, not per-vertex.
280 */
281 void
282 _mesa_load_tracked_matrices(GLcontext *ctx)
283 {
284 GLuint i;
285
286 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
287 /* point 'mat' at source matrix */
288 GLmatrix *mat;
289 if (ctx->VertexProgram.TrackMatrix[i] == GL_MODELVIEW) {
290 mat = ctx->ModelviewMatrixStack.Top;
291 }
292 else if (ctx->VertexProgram.TrackMatrix[i] == GL_PROJECTION) {
293 mat = ctx->ProjectionMatrixStack.Top;
294 }
295 else if (ctx->VertexProgram.TrackMatrix[i] == GL_TEXTURE) {
296 mat = ctx->TextureMatrixStack[ctx->Texture.CurrentUnit].Top;
297 }
298 else if (ctx->VertexProgram.TrackMatrix[i] == GL_COLOR) {
299 mat = ctx->ColorMatrixStack.Top;
300 }
301 else if (ctx->VertexProgram.TrackMatrix[i]==GL_MODELVIEW_PROJECTION_NV) {
302 /* XXX verify the combined matrix is up to date */
303 mat = &ctx->_ModelProjectMatrix;
304 }
305 else if (ctx->VertexProgram.TrackMatrix[i] >= GL_MATRIX0_NV &&
306 ctx->VertexProgram.TrackMatrix[i] <= GL_MATRIX7_NV) {
307 GLuint n = ctx->VertexProgram.TrackMatrix[i] - GL_MATRIX0_NV;
308 ASSERT(n < MAX_PROGRAM_MATRICES);
309 mat = ctx->ProgramMatrixStack[n].Top;
310 }
311 else {
312 /* no matrix is tracked, but we leave the register values as-is */
313 assert(ctx->VertexProgram.TrackMatrix[i] == GL_NONE);
314 continue;
315 }
316
317 /* load the matrix values into sequential registers */
318 if (ctx->VertexProgram.TrackMatrixTransform[i] == GL_IDENTITY_NV) {
319 load_matrix(ctx->VertexProgram.Parameters, i*4, mat->m);
320 }
321 else if (ctx->VertexProgram.TrackMatrixTransform[i] == GL_INVERSE_NV) {
322 _math_matrix_analyse(mat); /* update the inverse */
323 ASSERT(!_math_matrix_is_dirty(mat));
324 load_matrix(ctx->VertexProgram.Parameters, i*4, mat->inv);
325 }
326 else if (ctx->VertexProgram.TrackMatrixTransform[i] == GL_TRANSPOSE_NV) {
327 load_transpose_matrix(ctx->VertexProgram.Parameters, i*4, mat->m);
328 }
329 else {
330 assert(ctx->VertexProgram.TrackMatrixTransform[i]
331 == GL_INVERSE_TRANSPOSE_NV);
332 _math_matrix_analyse(mat); /* update the inverse */
333 ASSERT(!_math_matrix_is_dirty(mat));
334 load_transpose_matrix(ctx->VertexProgram.Parameters, i*4, mat->inv);
335 }
336 }
337 }
338
339
340 /**
341 * This function executes vertex programs
342 */
343 static GLboolean
344 run_vp( GLcontext *ctx, struct tnl_pipeline_stage *stage )
345 {
346 TNLcontext *tnl = TNL_CONTEXT(ctx);
347 struct vp_stage_data *store = VP_STAGE_DATA(stage);
348 struct vertex_buffer *VB = &tnl->vb;
349 struct gl_vertex_program *program = ctx->VertexProgram._Current;
350 struct gl_program_machine machine;
351 GLuint outputs[VERT_RESULT_MAX], numOutputs;
352 GLuint i, j;
353
354 if (!program)
355 return GL_TRUE;
356
357 if (program->IsNVProgram) {
358 _mesa_load_tracked_matrices(ctx);
359 }
360 else {
361 /* ARB program or vertex shader */
362 _mesa_load_state_parameters(ctx, program->Base.Parameters);
363 }
364
365 numOutputs = 0;
366 for (i = 0; i < VERT_RESULT_MAX; i++) {
367 if (program->Base.OutputsWritten & (1 << i)) {
368 outputs[numOutputs++] = i;
369 }
370 }
371
372 for (i = 0; i < VB->Count; i++) {
373 GLuint attr;
374
375 init_machine(ctx, &machine);
376
377 #if 0
378 printf("Input %d: %f, %f, %f, %f\n", i,
379 VB->AttribPtr[0]->data[i][0],
380 VB->AttribPtr[0]->data[i][1],
381 VB->AttribPtr[0]->data[i][2],
382 VB->AttribPtr[0]->data[i][3]);
383 printf(" color: %f, %f, %f, %f\n",
384 VB->AttribPtr[3]->data[i][0],
385 VB->AttribPtr[3]->data[i][1],
386 VB->AttribPtr[3]->data[i][2],
387 VB->AttribPtr[3]->data[i][3]);
388 printf(" normal: %f, %f, %f, %f\n",
389 VB->AttribPtr[2]->data[i][0],
390 VB->AttribPtr[2]->data[i][1],
391 VB->AttribPtr[2]->data[i][2],
392 VB->AttribPtr[2]->data[i][3]);
393 #endif
394
395 /* the vertex array case */
396 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
397 if (program->Base.InputsRead & (1 << attr)) {
398 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data;
399 const GLuint size = VB->AttribPtr[attr]->size;
400 const GLuint stride = VB->AttribPtr[attr]->stride;
401 const GLfloat *data = (GLfloat *) (ptr + stride * i);
402 COPY_CLEAN_4V(machine.VertAttribs[attr], size, data);
403 }
404 }
405
406 /* execute the program */
407 _mesa_execute_program(ctx, &program->Base, &machine);
408
409 /* copy the output registers into the VB->attribs arrays */
410 for (j = 0; j < numOutputs; j++) {
411 const GLuint attr = outputs[j];
412 COPY_4V(store->results[attr].data[i], machine.Outputs[attr]);
413 }
414 #if 0
415 printf("HPOS: %f %f %f %f\n",
416 machine.Outputs[0][0],
417 machine.Outputs[0][1],
418 machine.Outputs[0][2],
419 machine.Outputs[0][3]);
420 #endif
421 }
422
423 /* Fixup fog and point size results if needed */
424 if (program->IsNVProgram) {
425 if (ctx->Fog.Enabled &&
426 (program->Base.OutputsWritten & (1 << VERT_RESULT_FOGC)) == 0) {
427 for (i = 0; i < VB->Count; i++) {
428 store->results[VERT_RESULT_FOGC].data[i][0] = 1.0;
429 }
430 }
431
432 if (ctx->VertexProgram.PointSizeEnabled &&
433 (program->Base.OutputsWritten & (1 << VERT_RESULT_PSIZ)) == 0) {
434 for (i = 0; i < VB->Count; i++) {
435 store->results[VERT_RESULT_PSIZ].data[i][0] = ctx->Point.Size;
436 }
437 }
438 }
439
440 if (program->IsPositionInvariant) {
441 /* We need the exact same transform as in the fixed function path here
442 to guarantee invariance, depending on compiler optimization flags results
443 could be different otherwise */
444 VB->ClipPtr = TransformRaw( &store->results[0],
445 &ctx->_ModelProjectMatrix,
446 VB->AttribPtr[0] );
447
448 /* Drivers expect this to be clean to element 4...
449 */
450 switch (VB->ClipPtr->size) {
451 case 1:
452 /* impossible */
453 case 2:
454 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
455 /* fall-through */
456 case 3:
457 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
458 /* fall-through */
459 case 4:
460 break;
461 }
462 }
463
464
465 /* Setup the VB pointers so that the next pipeline stages get
466 * their data from the right place (the program output arrays).
467 */
468 else {
469 VB->ClipPtr = &store->results[VERT_RESULT_HPOS];
470 VB->ClipPtr->size = 4;
471 VB->ClipPtr->count = VB->Count;
472 }
473 VB->ColorPtr[0] = &store->results[VERT_RESULT_COL0];
474 VB->ColorPtr[1] = &store->results[VERT_RESULT_BFC0];
475 VB->SecondaryColorPtr[0] = &store->results[VERT_RESULT_COL1];
476 VB->SecondaryColorPtr[1] = &store->results[VERT_RESULT_BFC1];
477 VB->FogCoordPtr = &store->results[VERT_RESULT_FOGC];
478
479 VB->AttribPtr[VERT_ATTRIB_COLOR0] = &store->results[VERT_RESULT_COL0];
480 VB->AttribPtr[VERT_ATTRIB_COLOR1] = &store->results[VERT_RESULT_COL1];
481 VB->AttribPtr[VERT_ATTRIB_FOG] = &store->results[VERT_RESULT_FOGC];
482 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->results[VERT_RESULT_PSIZ];
483
484 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
485 VB->TexCoordPtr[i] =
486 VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]
487 = &store->results[VERT_RESULT_TEX0 + i];
488 }
489
490 for (i = 0; i < ctx->Const.MaxVarying; i++) {
491 if (program->Base.OutputsWritten & (1 << (VERT_RESULT_VAR0 + i))) {
492 /* Note: varying results get put into the generic attributes */
493 VB->AttribPtr[VERT_ATTRIB_GENERIC0+i]
494 = &store->results[VERT_RESULT_VAR0 + i];
495 }
496 }
497
498
499 /* Perform NDC and cliptest operations:
500 */
501 return do_ndc_cliptest(ctx, store);
502 }
503
504
505 /**
506 * Called the first time stage->run is called. In effect, don't
507 * allocate data until the first time the stage is run.
508 */
509 static GLboolean
510 init_vp(GLcontext *ctx, struct tnl_pipeline_stage *stage)
511 {
512 TNLcontext *tnl = TNL_CONTEXT(ctx);
513 struct vertex_buffer *VB = &(tnl->vb);
514 struct vp_stage_data *store;
515 const GLuint size = VB->Size;
516 GLuint i;
517
518 stage->privatePtr = MALLOC(sizeof(*store));
519 store = VP_STAGE_DATA(stage);
520 if (!store)
521 return GL_FALSE;
522
523 /* Allocate arrays of vertex output values */
524 for (i = 0; i < VERT_RESULT_MAX; i++) {
525 _mesa_vector4f_alloc( &store->results[i], 0, size, 32 );
526 store->results[i].size = 4;
527 }
528
529 /* a few other misc allocations */
530 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 );
531 store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
532
533 return GL_TRUE;
534 }
535
536
537 /**
538 * Destructor for this pipeline stage.
539 */
540 static void
541 dtr(struct tnl_pipeline_stage *stage)
542 {
543 struct vp_stage_data *store = VP_STAGE_DATA(stage);
544
545 if (store) {
546 GLuint i;
547
548 /* free the vertex program result arrays */
549 for (i = 0; i < VERT_RESULT_MAX; i++)
550 _mesa_vector4f_free( &store->results[i] );
551
552 /* free misc arrays */
553 _mesa_vector4f_free( &store->ndcCoords );
554 ALIGN_FREE( store->clipmask );
555
556 FREE( store );
557 stage->privatePtr = NULL;
558 }
559 }
560
561
562 static void
563 validate_vp_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
564 {
565 if (ctx->VertexProgram._Current) {
566 _swrast_update_texture_samplers(ctx);
567 }
568 }
569
570
571
572 /**
573 * Public description of this pipeline stage.
574 */
575 const struct tnl_pipeline_stage _tnl_vertex_program_stage =
576 {
577 "vertex-program",
578 NULL, /* private_data */
579 init_vp, /* create */
580 dtr, /* destroy */
581 validate_vp_stage, /* validate */
582 run_vp /* run -- initially set to ctr */
583 };