Merge branch 'r500-support'
[mesa.git] / src / mesa / tnl / t_vb_program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
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 "main/glheader.h"
34 #include "main/colormac.h"
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/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/tnl.h"
45 #include "tnl/t_context.h"
46 #include "tnl/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 static void
67 userclip( GLcontext *ctx,
68 GLvector4f *clip,
69 GLubyte *clipmask,
70 GLubyte *clipormask,
71 GLubyte *clipandmask )
72 {
73 GLuint p;
74
75 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
76 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
77 GLuint nr, i;
78 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
79 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
80 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
81 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
82 GLfloat *coord = (GLfloat *)clip->data;
83 GLuint stride = clip->stride;
84 GLuint count = clip->count;
85
86 for (nr = 0, i = 0 ; i < count ; i++) {
87 GLfloat dp = (coord[0] * a +
88 coord[1] * b +
89 coord[2] * c +
90 coord[3] * d);
91
92 if (dp < 0) {
93 nr++;
94 clipmask[i] |= CLIP_USER_BIT;
95 }
96
97 STRIDE_F(coord, stride);
98 }
99
100 if (nr > 0) {
101 *clipormask |= CLIP_USER_BIT;
102 if (nr == count) {
103 *clipandmask |= CLIP_USER_BIT;
104 return;
105 }
106 }
107 }
108 }
109 }
110
111
112 static GLboolean
113 do_ndc_cliptest(GLcontext *ctx, struct vp_stage_data *store)
114 {
115 TNLcontext *tnl = TNL_CONTEXT(ctx);
116 struct vertex_buffer *VB = &tnl->vb;
117 /* Cliptest and perspective divide. Clip functions must clear
118 * the clipmask.
119 */
120 store->ormask = 0;
121 store->andmask = CLIP_FRUSTUM_BITS;
122
123 if (tnl->NeedNdcCoords) {
124 VB->NdcPtr =
125 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
126 &store->ndcCoords,
127 store->clipmask,
128 &store->ormask,
129 &store->andmask );
130 }
131 else {
132 VB->NdcPtr = NULL;
133 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
134 NULL,
135 store->clipmask,
136 &store->ormask,
137 &store->andmask );
138 }
139
140 if (store->andmask) {
141 /* All vertices are outside the frustum */
142 return GL_FALSE;
143 }
144
145 /* Test userclip planes. This contributes to VB->ClipMask.
146 */
147 /** XXX NEW_SLANG _Enabled ??? */
148 if (ctx->Transform.ClipPlanesEnabled && (!ctx->VertexProgram._Enabled ||
149 ctx->VertexProgram.Current->IsPositionInvariant)) {
150 userclip( ctx,
151 VB->ClipPtr,
152 store->clipmask,
153 &store->ormask,
154 &store->andmask );
155
156 if (store->andmask) {
157 return GL_FALSE;
158 }
159 }
160
161 VB->ClipAndMask = store->andmask;
162 VB->ClipOrMask = store->ormask;
163 VB->ClipMask = store->clipmask;
164
165 return GL_TRUE;
166 }
167
168
169 /**
170 * XXX the texture sampling code in this module is a bit of a hack.
171 * The texture sampling code is in swrast, though it doesn't have any
172 * real dependencies on the rest of swrast. It should probably be
173 * moved into main/ someday.
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 * Map the texture images which the vertex program will access (if any).
250 */
251 static void
252 map_textures(GLcontext *ctx, const struct gl_vertex_program *vp)
253 {
254 GLuint u;
255
256 if (!ctx->Driver.MapTexture)
257 return;
258
259 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) {
260 if (vp->Base.TexturesUsed[u]) {
261 /* Note: _Current *should* correspond to the target indicated
262 * in TexturesUsed[u].
263 */
264 ctx->Driver.MapTexture(ctx, ctx->Texture.Unit[u]._Current);
265 }
266 }
267 }
268
269
270 /**
271 * Unmap the texture images which were used by the vertex program (if any).
272 */
273 static void
274 unmap_textures(GLcontext *ctx, const struct gl_vertex_program *vp)
275 {
276 GLuint u;
277
278 if (!ctx->Driver.MapTexture)
279 return;
280
281 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) {
282 if (vp->Base.TexturesUsed[u]) {
283 /* Note: _Current *should* correspond to the target indicated
284 * in TexturesUsed[u].
285 */
286 ctx->Driver.UnmapTexture(ctx, ctx->Texture.Unit[u]._Current);
287 }
288 }
289 }
290
291
292 /**
293 * This function executes vertex programs
294 */
295 static GLboolean
296 run_vp( GLcontext *ctx, struct tnl_pipeline_stage *stage )
297 {
298 TNLcontext *tnl = TNL_CONTEXT(ctx);
299 struct vp_stage_data *store = VP_STAGE_DATA(stage);
300 struct vertex_buffer *VB = &tnl->vb;
301 struct gl_vertex_program *program = ctx->VertexProgram._Current;
302 struct gl_program_machine machine;
303 GLuint outputs[VERT_RESULT_MAX], numOutputs;
304 GLuint i, j;
305
306 if (!program)
307 return GL_TRUE;
308
309 if (program->IsNVProgram) {
310 _mesa_load_tracked_matrices(ctx);
311 }
312 else {
313 /* ARB program or vertex shader */
314 _mesa_load_state_parameters(ctx, program->Base.Parameters);
315 }
316
317 /* make list of outputs to save some time below */
318 numOutputs = 0;
319 for (i = 0; i < VERT_RESULT_MAX; i++) {
320 if (program->Base.OutputsWritten & (1 << i)) {
321 outputs[numOutputs++] = i;
322 }
323 }
324
325 map_textures(ctx, program);
326
327 for (i = 0; i < VB->Count; i++) {
328 GLuint attr;
329
330 init_machine(ctx, &machine);
331
332 #if 0
333 printf("Input %d: %f, %f, %f, %f\n", i,
334 VB->AttribPtr[0]->data[i][0],
335 VB->AttribPtr[0]->data[i][1],
336 VB->AttribPtr[0]->data[i][2],
337 VB->AttribPtr[0]->data[i][3]);
338 printf(" color: %f, %f, %f, %f\n",
339 VB->AttribPtr[3]->data[i][0],
340 VB->AttribPtr[3]->data[i][1],
341 VB->AttribPtr[3]->data[i][2],
342 VB->AttribPtr[3]->data[i][3]);
343 printf(" normal: %f, %f, %f, %f\n",
344 VB->AttribPtr[2]->data[i][0],
345 VB->AttribPtr[2]->data[i][1],
346 VB->AttribPtr[2]->data[i][2],
347 VB->AttribPtr[2]->data[i][3]);
348 #endif
349
350 /* the vertex array case */
351 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
352 if (program->Base.InputsRead & (1 << attr)) {
353 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data;
354 const GLuint size = VB->AttribPtr[attr]->size;
355 const GLuint stride = VB->AttribPtr[attr]->stride;
356 const GLfloat *data = (GLfloat *) (ptr + stride * i);
357 COPY_CLEAN_4V(machine.VertAttribs[attr], size, data);
358 }
359 }
360
361 /* execute the program */
362 _mesa_execute_program(ctx, &program->Base, &machine);
363
364 /* copy the output registers into the VB->attribs arrays */
365 for (j = 0; j < numOutputs; j++) {
366 const GLuint attr = outputs[j];
367 COPY_4V(store->results[attr].data[i], machine.Outputs[attr]);
368 }
369 #if 0
370 printf("HPOS: %f %f %f %f\n",
371 machine.Outputs[0][0],
372 machine.Outputs[0][1],
373 machine.Outputs[0][2],
374 machine.Outputs[0][3]);
375 #endif
376 }
377
378 unmap_textures(ctx, program);
379
380 /* Fixup fog and point size results if needed */
381 if (program->IsNVProgram) {
382 if (ctx->Fog.Enabled &&
383 (program->Base.OutputsWritten & (1 << VERT_RESULT_FOGC)) == 0) {
384 for (i = 0; i < VB->Count; i++) {
385 store->results[VERT_RESULT_FOGC].data[i][0] = 1.0;
386 }
387 }
388
389 if (ctx->VertexProgram.PointSizeEnabled &&
390 (program->Base.OutputsWritten & (1 << VERT_RESULT_PSIZ)) == 0) {
391 for (i = 0; i < VB->Count; i++) {
392 store->results[VERT_RESULT_PSIZ].data[i][0] = ctx->Point.Size;
393 }
394 }
395 }
396
397 if (program->IsPositionInvariant) {
398 /* We need the exact same transform as in the fixed function path here
399 * to guarantee invariance, depending on compiler optimization flags
400 * results could be different otherwise.
401 */
402 VB->ClipPtr = TransformRaw( &store->results[0],
403 &ctx->_ModelProjectMatrix,
404 VB->AttribPtr[0] );
405
406 /* Drivers expect this to be clean to element 4...
407 */
408 switch (VB->ClipPtr->size) {
409 case 1:
410 /* impossible */
411 case 2:
412 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
413 /* fall-through */
414 case 3:
415 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
416 /* fall-through */
417 case 4:
418 break;
419 }
420 }
421 else {
422 /* Setup the VB pointers so that the next pipeline stages get
423 * their data from the right place (the program output arrays).
424 */
425 VB->ClipPtr = &store->results[VERT_RESULT_HPOS];
426 VB->ClipPtr->size = 4;
427 VB->ClipPtr->count = VB->Count;
428 }
429
430 VB->ColorPtr[0] = &store->results[VERT_RESULT_COL0];
431 VB->ColorPtr[1] = &store->results[VERT_RESULT_BFC0];
432 VB->SecondaryColorPtr[0] = &store->results[VERT_RESULT_COL1];
433 VB->SecondaryColorPtr[1] = &store->results[VERT_RESULT_BFC1];
434 VB->FogCoordPtr = &store->results[VERT_RESULT_FOGC];
435
436 VB->AttribPtr[VERT_ATTRIB_COLOR0] = &store->results[VERT_RESULT_COL0];
437 VB->AttribPtr[VERT_ATTRIB_COLOR1] = &store->results[VERT_RESULT_COL1];
438 VB->AttribPtr[VERT_ATTRIB_FOG] = &store->results[VERT_RESULT_FOGC];
439 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->results[VERT_RESULT_PSIZ];
440
441 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
442 VB->TexCoordPtr[i] =
443 VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]
444 = &store->results[VERT_RESULT_TEX0 + i];
445 }
446
447 for (i = 0; i < ctx->Const.MaxVarying; i++) {
448 if (program->Base.OutputsWritten & (1 << (VERT_RESULT_VAR0 + i))) {
449 /* Note: varying results get put into the generic attributes */
450 VB->AttribPtr[VERT_ATTRIB_GENERIC0+i]
451 = &store->results[VERT_RESULT_VAR0 + i];
452 }
453 }
454
455
456 /* Perform NDC and cliptest operations:
457 */
458 return do_ndc_cliptest(ctx, store);
459 }
460
461
462 /**
463 * Called the first time stage->run is called. In effect, don't
464 * allocate data until the first time the stage is run.
465 */
466 static GLboolean
467 init_vp(GLcontext *ctx, struct tnl_pipeline_stage *stage)
468 {
469 TNLcontext *tnl = TNL_CONTEXT(ctx);
470 struct vertex_buffer *VB = &(tnl->vb);
471 struct vp_stage_data *store;
472 const GLuint size = VB->Size;
473 GLuint i;
474
475 stage->privatePtr = MALLOC(sizeof(*store));
476 store = VP_STAGE_DATA(stage);
477 if (!store)
478 return GL_FALSE;
479
480 /* Allocate arrays of vertex output values */
481 for (i = 0; i < VERT_RESULT_MAX; i++) {
482 _mesa_vector4f_alloc( &store->results[i], 0, size, 32 );
483 store->results[i].size = 4;
484 }
485
486 /* a few other misc allocations */
487 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 );
488 store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
489
490 return GL_TRUE;
491 }
492
493
494 /**
495 * Destructor for this pipeline stage.
496 */
497 static void
498 dtr(struct tnl_pipeline_stage *stage)
499 {
500 struct vp_stage_data *store = VP_STAGE_DATA(stage);
501
502 if (store) {
503 GLuint i;
504
505 /* free the vertex program result arrays */
506 for (i = 0; i < VERT_RESULT_MAX; i++)
507 _mesa_vector4f_free( &store->results[i] );
508
509 /* free misc arrays */
510 _mesa_vector4f_free( &store->ndcCoords );
511 ALIGN_FREE( store->clipmask );
512
513 FREE( store );
514 stage->privatePtr = NULL;
515 }
516 }
517
518
519 static void
520 validate_vp_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage)
521 {
522 if (ctx->VertexProgram._Current) {
523 _swrast_update_texture_samplers(ctx);
524 }
525 }
526
527
528
529 /**
530 * Public description of this pipeline stage.
531 */
532 const struct tnl_pipeline_stage _tnl_vertex_program_stage =
533 {
534 "vertex-program",
535 NULL, /* private_data */
536 init_vp, /* create */
537 dtr, /* destroy */
538 validate_vp_stage, /* validate */
539 run_vp /* run -- initially set to ctr */
540 };