tnl: implement instanced drawing
[mesa.git] / src / mesa / tnl / t_vb_program.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.6
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file tnl/t_vb_program.c
29 * \brief Pipeline stage for executing vertex programs.
30 * \author Brian Paul, Keith Whitwell
31 */
32
33
34 #include "main/glheader.h"
35 #include "main/colormac.h"
36 #include "main/macros.h"
37 #include "main/imports.h"
38 #include "math/m_xform.h"
39 #include "program/prog_instruction.h"
40 #include "program/prog_statevars.h"
41 #include "program/prog_execute.h"
42 #include "swrast/s_context.h"
43
44 #include "tnl/tnl.h"
45 #include "tnl/t_context.h"
46 #include "tnl/t_pipeline.h"
47
48
49 #ifdef NAN_CHECK
50 /** Check for NaNs and very large values */
51 static INLINE void
52 check_float(float x)
53 {
54 assert(!IS_INF_OR_NAN(x));
55 assert(1.0e-15 <= x && x <= 1.0e15);
56 }
57 #endif
58
59
60 /*!
61 * Private storage for the vertex program pipeline stage.
62 */
63 struct vp_stage_data {
64 /** The results of running the vertex program go into these arrays. */
65 GLvector4f results[VERT_RESULT_MAX];
66
67 GLvector4f ndcCoords; /**< normalized device coords */
68 GLubyte *clipmask; /**< clip flags */
69 GLubyte ormask, andmask; /**< for clipping */
70 };
71
72
73 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
74
75
76 static void
77 userclip( struct gl_context *ctx,
78 GLvector4f *clip,
79 GLubyte *clipmask,
80 GLubyte *clipormask,
81 GLubyte *clipandmask )
82 {
83 GLuint p;
84
85 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
86 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
87 GLuint nr, i;
88 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
89 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
90 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
91 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
92 GLfloat *coord = (GLfloat *)clip->data;
93 GLuint stride = clip->stride;
94 GLuint count = clip->count;
95
96 for (nr = 0, i = 0 ; i < count ; i++) {
97 GLfloat dp = (coord[0] * a +
98 coord[1] * b +
99 coord[2] * c +
100 coord[3] * d);
101
102 if (dp < 0) {
103 nr++;
104 clipmask[i] |= CLIP_USER_BIT;
105 }
106
107 STRIDE_F(coord, stride);
108 }
109
110 if (nr > 0) {
111 *clipormask |= CLIP_USER_BIT;
112 if (nr == count) {
113 *clipandmask |= CLIP_USER_BIT;
114 return;
115 }
116 }
117 }
118 }
119 }
120
121
122 static GLboolean
123 do_ndc_cliptest(struct gl_context *ctx, struct vp_stage_data *store)
124 {
125 TNLcontext *tnl = TNL_CONTEXT(ctx);
126 struct vertex_buffer *VB = &tnl->vb;
127 /* Cliptest and perspective divide. Clip functions must clear
128 * the clipmask.
129 */
130 store->ormask = 0;
131 store->andmask = CLIP_FRUSTUM_BITS;
132
133 tnl_clip_prepare(ctx);
134
135 if (tnl->NeedNdcCoords) {
136 VB->NdcPtr =
137 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
138 &store->ndcCoords,
139 store->clipmask,
140 &store->ormask,
141 &store->andmask,
142 !ctx->Transform.DepthClamp );
143 }
144 else {
145 VB->NdcPtr = NULL;
146 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
147 NULL,
148 store->clipmask,
149 &store->ormask,
150 &store->andmask,
151 !ctx->Transform.DepthClamp );
152 }
153
154 if (store->andmask) {
155 /* All vertices are outside the frustum */
156 return GL_FALSE;
157 }
158
159 /* Test userclip planes. This contributes to VB->ClipMask.
160 */
161 /** XXX NEW_SLANG _Enabled ??? */
162 if (ctx->Transform.ClipPlanesEnabled && (!ctx->VertexProgram._Enabled ||
163 ctx->VertexProgram.Current->IsPositionInvariant)) {
164 userclip( ctx,
165 VB->ClipPtr,
166 store->clipmask,
167 &store->ormask,
168 &store->andmask );
169
170 if (store->andmask) {
171 return GL_FALSE;
172 }
173 }
174
175 VB->ClipAndMask = store->andmask;
176 VB->ClipOrMask = store->ormask;
177 VB->ClipMask = store->clipmask;
178
179 return GL_TRUE;
180 }
181
182
183 /**
184 * XXX the texture sampling code in this module is a bit of a hack.
185 * The texture sampling code is in swrast, though it doesn't have any
186 * real dependencies on the rest of swrast. It should probably be
187 * moved into main/ someday.
188 */
189 static void
190 vp_fetch_texel(struct gl_context *ctx, const GLfloat texcoord[4], GLfloat lambda,
191 GLuint unit, GLfloat color[4])
192 {
193 SWcontext *swrast = SWRAST_CONTEXT(ctx);
194
195 /* XXX use a float-valued TextureSample routine here!!! */
196 swrast->TextureSample[unit](ctx, ctx->Texture.Unit[unit]._Current,
197 1, (const GLfloat (*)[4]) texcoord,
198 &lambda, (GLfloat (*)[4]) color);
199 }
200
201
202 /**
203 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
204 * string has been parsed.
205 */
206 GLboolean
207 _tnl_program_string(struct gl_context *ctx, GLenum target, struct gl_program *program)
208 {
209 /* No-op.
210 * If we had derived anything from the program that was private to this
211 * stage we'd recompute/validate it here.
212 */
213 return GL_TRUE;
214 }
215
216
217 /**
218 * Initialize virtual machine state prior to executing vertex program.
219 */
220 static void
221 init_machine(struct gl_context *ctx, struct gl_program_machine *machine,
222 GLuint instID)
223 {
224 /* Input registers get initialized from the current vertex attribs */
225 memcpy(machine->VertAttribs, ctx->Current.Attrib,
226 MAX_VERTEX_GENERIC_ATTRIBS * 4 * sizeof(GLfloat));
227
228 if (ctx->VertexProgram._Current->IsNVProgram) {
229 GLuint i;
230 /* Output/result regs are initialized to [0,0,0,1] */
231 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_OUTPUTS; i++) {
232 ASSIGN_4V(machine->Outputs[i], 0.0F, 0.0F, 0.0F, 1.0F);
233 }
234 /* Temp regs are initialized to [0,0,0,0] */
235 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_TEMPS; i++) {
236 ASSIGN_4V(machine->Temporaries[i], 0.0F, 0.0F, 0.0F, 0.0F);
237 }
238 for (i = 0; i < MAX_VERTEX_PROGRAM_ADDRESS_REGS; i++) {
239 ASSIGN_4V(machine->AddressReg[i], 0, 0, 0, 0);
240 }
241 }
242
243 machine->NumDeriv = 0;
244
245 /* init condition codes */
246 machine->CondCodes[0] = COND_EQ;
247 machine->CondCodes[1] = COND_EQ;
248 machine->CondCodes[2] = COND_EQ;
249 machine->CondCodes[3] = COND_EQ;
250
251 /* init call stack */
252 machine->StackDepth = 0;
253
254 machine->FetchTexelLod = vp_fetch_texel;
255 machine->FetchTexelDeriv = NULL; /* not used by vertex programs */
256
257 machine->Samplers = ctx->VertexProgram._Current->Base.SamplerUnits;
258
259 machine->SystemValues[SYSTEM_VALUE_INSTANCE_ID][0] = (GLfloat) instID;
260 }
261
262
263 /**
264 * Map the texture images which the vertex program will access (if any).
265 */
266 static void
267 map_textures(struct gl_context *ctx, const struct gl_vertex_program *vp)
268 {
269 GLuint u;
270
271 if (!ctx->Driver.MapTexture)
272 return;
273
274 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) {
275 if (vp->Base.TexturesUsed[u]) {
276 /* Note: _Current *should* correspond to the target indicated
277 * in TexturesUsed[u].
278 */
279 ctx->Driver.MapTexture(ctx, ctx->Texture.Unit[u]._Current);
280 }
281 }
282 }
283
284
285 /**
286 * Unmap the texture images which were used by the vertex program (if any).
287 */
288 static void
289 unmap_textures(struct gl_context *ctx, const struct gl_vertex_program *vp)
290 {
291 GLuint u;
292
293 if (!ctx->Driver.MapTexture)
294 return;
295
296 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) {
297 if (vp->Base.TexturesUsed[u]) {
298 /* Note: _Current *should* correspond to the target indicated
299 * in TexturesUsed[u].
300 */
301 ctx->Driver.UnmapTexture(ctx, ctx->Texture.Unit[u]._Current);
302 }
303 }
304 }
305
306
307 /**
308 * This function executes vertex programs
309 */
310 static GLboolean
311 run_vp( struct gl_context *ctx, struct tnl_pipeline_stage *stage )
312 {
313 TNLcontext *tnl = TNL_CONTEXT(ctx);
314 struct vp_stage_data *store = VP_STAGE_DATA(stage);
315 struct vertex_buffer *VB = &tnl->vb;
316 struct gl_vertex_program *program = ctx->VertexProgram._Current;
317 struct gl_program_machine machine;
318 GLuint outputs[VERT_RESULT_MAX], numOutputs;
319 GLuint i, j;
320
321 if (!program)
322 return GL_TRUE;
323
324 if (program->IsNVProgram) {
325 _mesa_load_tracked_matrices(ctx);
326 }
327 else {
328 /* ARB program or vertex shader */
329 _mesa_load_state_parameters(ctx, program->Base.Parameters);
330 }
331
332 /* make list of outputs to save some time below */
333 numOutputs = 0;
334 for (i = 0; i < VERT_RESULT_MAX; i++) {
335 if (program->Base.OutputsWritten & BITFIELD64_BIT(i)) {
336 outputs[numOutputs++] = i;
337 }
338 }
339
340 map_textures(ctx, program);
341
342 for (i = 0; i < VB->Count; i++) {
343 GLuint attr;
344
345 init_machine(ctx, &machine, tnl->CurInstance);
346
347 #if 0
348 printf("Input %d: %f, %f, %f, %f\n", i,
349 VB->AttribPtr[0]->data[i][0],
350 VB->AttribPtr[0]->data[i][1],
351 VB->AttribPtr[0]->data[i][2],
352 VB->AttribPtr[0]->data[i][3]);
353 printf(" color: %f, %f, %f, %f\n",
354 VB->AttribPtr[3]->data[i][0],
355 VB->AttribPtr[3]->data[i][1],
356 VB->AttribPtr[3]->data[i][2],
357 VB->AttribPtr[3]->data[i][3]);
358 printf(" normal: %f, %f, %f, %f\n",
359 VB->AttribPtr[2]->data[i][0],
360 VB->AttribPtr[2]->data[i][1],
361 VB->AttribPtr[2]->data[i][2],
362 VB->AttribPtr[2]->data[i][3]);
363 #endif
364
365 /* the vertex array case */
366 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
367 if (program->Base.InputsRead & (1 << attr)) {
368 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data;
369 const GLuint size = VB->AttribPtr[attr]->size;
370 const GLuint stride = VB->AttribPtr[attr]->stride;
371 const GLfloat *data = (GLfloat *) (ptr + stride * i);
372 #ifdef NAN_CHECK
373 check_float(data[0]);
374 check_float(data[1]);
375 check_float(data[2]);
376 check_float(data[3]);
377 #endif
378 COPY_CLEAN_4V(machine.VertAttribs[attr], size, data);
379 }
380 }
381
382 /* execute the program */
383 _mesa_execute_program(ctx, &program->Base, &machine);
384
385 /* copy the output registers into the VB->attribs arrays */
386 for (j = 0; j < numOutputs; j++) {
387 const GLuint attr = outputs[j];
388 #ifdef NAN_CHECK
389 check_float(machine.Outputs[attr][0]);
390 check_float(machine.Outputs[attr][1]);
391 check_float(machine.Outputs[attr][2]);
392 check_float(machine.Outputs[attr][3]);
393 #endif
394 COPY_4V(store->results[attr].data[i], machine.Outputs[attr]);
395 }
396
397 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
398 if (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_FOGC)) {
399 store->results[VERT_RESULT_FOGC].data[i][1] = 0.0;
400 store->results[VERT_RESULT_FOGC].data[i][2] = 0.0;
401 store->results[VERT_RESULT_FOGC].data[i][3] = 1.0;
402 }
403 #ifdef NAN_CHECK
404 ASSERT(machine.Outputs[0][3] != 0.0F);
405 #endif
406 #if 0
407 printf("HPOS: %f %f %f %f\n",
408 machine.Outputs[0][0],
409 machine.Outputs[0][1],
410 machine.Outputs[0][2],
411 machine.Outputs[0][3]);
412 #endif
413 }
414
415 unmap_textures(ctx, program);
416
417 /* Fixup fog and point size results if needed */
418 if (program->IsNVProgram) {
419 if (ctx->Fog.Enabled &&
420 (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_FOGC)) == 0) {
421 for (i = 0; i < VB->Count; i++) {
422 store->results[VERT_RESULT_FOGC].data[i][0] = 1.0;
423 }
424 }
425
426 if (ctx->VertexProgram.PointSizeEnabled &&
427 (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_PSIZ)) == 0) {
428 for (i = 0; i < VB->Count; i++) {
429 store->results[VERT_RESULT_PSIZ].data[i][0] = ctx->Point.Size;
430 }
431 }
432 }
433
434 if (program->IsPositionInvariant) {
435 /* We need the exact same transform as in the fixed function path here
436 * to guarantee invariance, depending on compiler optimization flags
437 * results could be different otherwise.
438 */
439 VB->ClipPtr = TransformRaw( &store->results[0],
440 &ctx->_ModelProjectMatrix,
441 VB->AttribPtr[0] );
442
443 /* Drivers expect this to be clean to element 4...
444 */
445 switch (VB->ClipPtr->size) {
446 case 1:
447 /* impossible */
448 case 2:
449 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
450 /* fall-through */
451 case 3:
452 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
453 /* fall-through */
454 case 4:
455 break;
456 }
457 }
458 else {
459 /* Setup the VB pointers so that the next pipeline stages get
460 * their data from the right place (the program output arrays).
461 */
462 VB->ClipPtr = &store->results[VERT_RESULT_HPOS];
463 VB->ClipPtr->size = 4;
464 VB->ClipPtr->count = VB->Count;
465 }
466
467 VB->AttribPtr[VERT_ATTRIB_COLOR0] = &store->results[VERT_RESULT_COL0];
468 VB->AttribPtr[VERT_ATTRIB_COLOR1] = &store->results[VERT_RESULT_COL1];
469 VB->AttribPtr[VERT_ATTRIB_FOG] = &store->results[VERT_RESULT_FOGC];
470 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->results[VERT_RESULT_PSIZ];
471 VB->BackfaceColorPtr = &store->results[VERT_RESULT_BFC0];
472 VB->BackfaceSecondaryColorPtr = &store->results[VERT_RESULT_BFC1];
473
474 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
475 VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]
476 = &store->results[VERT_RESULT_TEX0 + i];
477 }
478
479 for (i = 0; i < ctx->Const.MaxVarying; i++) {
480 if (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_VAR0 + i)) {
481 /* Note: varying results get put into the generic attributes */
482 VB->AttribPtr[VERT_ATTRIB_GENERIC0+i]
483 = &store->results[VERT_RESULT_VAR0 + i];
484 }
485 }
486
487
488 /* Perform NDC and cliptest operations:
489 */
490 return do_ndc_cliptest(ctx, store);
491 }
492
493
494 /**
495 * Called the first time stage->run is called. In effect, don't
496 * allocate data until the first time the stage is run.
497 */
498 static GLboolean
499 init_vp(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
500 {
501 TNLcontext *tnl = TNL_CONTEXT(ctx);
502 struct vertex_buffer *VB = &(tnl->vb);
503 struct vp_stage_data *store;
504 const GLuint size = VB->Size;
505 GLuint i;
506
507 stage->privatePtr = MALLOC(sizeof(*store));
508 store = VP_STAGE_DATA(stage);
509 if (!store)
510 return GL_FALSE;
511
512 /* Allocate arrays of vertex output values */
513 for (i = 0; i < VERT_RESULT_MAX; i++) {
514 _mesa_vector4f_alloc( &store->results[i], 0, size, 32 );
515 store->results[i].size = 4;
516 }
517
518 /* a few other misc allocations */
519 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 );
520 store->clipmask = (GLubyte *) _mesa_align_malloc(sizeof(GLubyte)*size, 32 );
521
522 return GL_TRUE;
523 }
524
525
526 /**
527 * Destructor for this pipeline stage.
528 */
529 static void
530 dtr(struct tnl_pipeline_stage *stage)
531 {
532 struct vp_stage_data *store = VP_STAGE_DATA(stage);
533
534 if (store) {
535 GLuint i;
536
537 /* free the vertex program result arrays */
538 for (i = 0; i < VERT_RESULT_MAX; i++)
539 _mesa_vector4f_free( &store->results[i] );
540
541 /* free misc arrays */
542 _mesa_vector4f_free( &store->ndcCoords );
543 _mesa_align_free( store->clipmask );
544
545 FREE( store );
546 stage->privatePtr = NULL;
547 }
548 }
549
550
551 static void
552 validate_vp_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
553 {
554 if (ctx->VertexProgram._Current) {
555 _swrast_update_texture_samplers(ctx);
556 }
557 }
558
559
560
561 /**
562 * Public description of this pipeline stage.
563 */
564 const struct tnl_pipeline_stage _tnl_vertex_program_stage =
565 {
566 "vertex-program",
567 NULL, /* private_data */
568 init_vp, /* create */
569 dtr, /* destroy */
570 validate_vp_stage, /* validate */
571 run_vp /* run -- initially set to ctr */
572 };