mesa: move max texture image unit constants to gl_program_constants
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /**
29 * \file tnl/t_vb_program.c
30 * \brief Pipeline stage for executing vertex programs.
31 * \author Brian Paul, Keith Whitwell
32 */
33
34
35 #include "main/glheader.h"
36 #include "main/colormac.h"
37 #include "main/macros.h"
38 #include "main/imports.h"
39 #include "main/samplerobj.h"
40 #include "math/m_xform.h"
41 #include "program/prog_instruction.h"
42 #include "program/prog_statevars.h"
43 #include "program/prog_execute.h"
44 #include "swrast/s_context.h"
45
46 #include "tnl/tnl.h"
47 #include "tnl/t_context.h"
48 #include "tnl/t_pipeline.h"
49
50
51 #ifdef NAN_CHECK
52 /** Check for NaNs and very large values */
53 static inline void
54 check_float(float x)
55 {
56 assert(!IS_INF_OR_NAN(x));
57 assert(1.0e-15 <= x && x <= 1.0e15);
58 }
59 #endif
60
61
62 /*!
63 * Private storage for the vertex program pipeline stage.
64 */
65 struct vp_stage_data {
66 /** The results of running the vertex program go into these arrays. */
67 GLvector4f results[VARYING_SLOT_MAX];
68
69 GLvector4f ndcCoords; /**< normalized device coords */
70 GLubyte *clipmask; /**< clip flags */
71 GLubyte ormask, andmask; /**< for clipping */
72
73 GLboolean vertex_textures;
74
75 struct gl_program_machine machine;
76 };
77
78
79 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
80
81
82 static void
83 userclip( struct gl_context *ctx,
84 GLvector4f *clip,
85 GLubyte *clipmask,
86 GLubyte *clipormask,
87 GLubyte *clipandmask )
88 {
89 GLuint p;
90
91 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
92 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
93 GLuint nr, i;
94 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
95 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
96 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
97 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
98 GLfloat *coord = (GLfloat *)clip->data;
99 GLuint stride = clip->stride;
100 GLuint count = clip->count;
101
102 for (nr = 0, i = 0 ; i < count ; i++) {
103 GLfloat dp = (coord[0] * a +
104 coord[1] * b +
105 coord[2] * c +
106 coord[3] * d);
107
108 if (dp < 0) {
109 nr++;
110 clipmask[i] |= CLIP_USER_BIT;
111 }
112
113 STRIDE_F(coord, stride);
114 }
115
116 if (nr > 0) {
117 *clipormask |= CLIP_USER_BIT;
118 if (nr == count) {
119 *clipandmask |= CLIP_USER_BIT;
120 return;
121 }
122 }
123 }
124 }
125 }
126
127
128 static GLboolean
129 do_ndc_cliptest(struct gl_context *ctx, struct vp_stage_data *store)
130 {
131 TNLcontext *tnl = TNL_CONTEXT(ctx);
132 struct vertex_buffer *VB = &tnl->vb;
133 /* Cliptest and perspective divide. Clip functions must clear
134 * the clipmask.
135 */
136 store->ormask = 0;
137 store->andmask = CLIP_FRUSTUM_BITS;
138
139 tnl_clip_prepare(ctx);
140
141 if (tnl->NeedNdcCoords) {
142 VB->NdcPtr =
143 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
144 &store->ndcCoords,
145 store->clipmask,
146 &store->ormask,
147 &store->andmask,
148 !ctx->Transform.DepthClamp );
149 }
150 else {
151 VB->NdcPtr = NULL;
152 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
153 NULL,
154 store->clipmask,
155 &store->ormask,
156 &store->andmask,
157 !ctx->Transform.DepthClamp );
158 }
159
160 if (store->andmask) {
161 /* All vertices are outside the frustum */
162 return GL_FALSE;
163 }
164
165 /* Test userclip planes. This contributes to VB->ClipMask.
166 */
167 /** XXX NEW_SLANG _Enabled ??? */
168 if (ctx->Transform.ClipPlanesEnabled && (!ctx->VertexProgram._Enabled ||
169 ctx->VertexProgram.Current->IsPositionInvariant)) {
170 userclip( ctx,
171 VB->ClipPtr,
172 store->clipmask,
173 &store->ormask,
174 &store->andmask );
175
176 if (store->andmask) {
177 return GL_FALSE;
178 }
179 }
180
181 VB->ClipAndMask = store->andmask;
182 VB->ClipOrMask = store->ormask;
183 VB->ClipMask = store->clipmask;
184
185 return GL_TRUE;
186 }
187
188
189 /**
190 * XXX the texture sampling code in this module is a bit of a hack.
191 * The texture sampling code is in swrast, though it doesn't have any
192 * real dependencies on the rest of swrast. It should probably be
193 * moved into main/ someday.
194 */
195 static void
196 vp_fetch_texel(struct gl_context *ctx, const GLfloat texcoord[4], GLfloat lambda,
197 GLuint unit, GLfloat color[4])
198 {
199 SWcontext *swrast = SWRAST_CONTEXT(ctx);
200
201 /* XXX use a float-valued TextureSample routine here!!! */
202 swrast->TextureSample[unit](ctx, _mesa_get_samplerobj(ctx, unit),
203 ctx->Texture.Unit[unit]._Current,
204 1, (const GLfloat (*)[4]) texcoord,
205 &lambda, (GLfloat (*)[4]) color);
206 }
207
208
209 /**
210 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
211 * string has been parsed.
212 */
213 GLboolean
214 _tnl_program_string(struct gl_context *ctx, GLenum target, struct gl_program *program)
215 {
216 /* No-op.
217 * If we had derived anything from the program that was private to this
218 * stage we'd recompute/validate it here.
219 */
220 return GL_TRUE;
221 }
222
223
224 /**
225 * Initialize virtual machine state prior to executing vertex program.
226 */
227 static void
228 init_machine(struct gl_context *ctx, struct gl_program_machine *machine,
229 GLuint instID)
230 {
231 /* Input registers get initialized from the current vertex attribs */
232 memcpy(machine->VertAttribs, ctx->Current.Attrib,
233 MAX_VERTEX_GENERIC_ATTRIBS * 4 * sizeof(GLfloat));
234
235 machine->NumDeriv = 0;
236
237 /* init condition codes */
238 machine->CondCodes[0] = COND_EQ;
239 machine->CondCodes[1] = COND_EQ;
240 machine->CondCodes[2] = COND_EQ;
241 machine->CondCodes[3] = COND_EQ;
242
243 /* init call stack */
244 machine->StackDepth = 0;
245
246 machine->FetchTexelLod = vp_fetch_texel;
247 machine->FetchTexelDeriv = NULL; /* not used by vertex programs */
248
249 machine->Samplers = ctx->VertexProgram._Current->Base.SamplerUnits;
250
251 machine->SystemValues[SYSTEM_VALUE_INSTANCE_ID][0] = (GLfloat) instID;
252 }
253
254
255 /**
256 * Map the texture images which the vertex program will access (if any).
257 */
258 static void
259 map_textures(struct gl_context *ctx, const struct gl_vertex_program *vp)
260 {
261 GLuint u;
262
263 for (u = 0; u < ctx->Const.VertexProgram.MaxTextureImageUnits; u++) {
264 if (vp->Base.TexturesUsed[u]) {
265 /* Note: _Current *should* correspond to the target indicated
266 * in TexturesUsed[u].
267 */
268 _swrast_map_texture(ctx, ctx->Texture.Unit[u]._Current);
269 }
270 }
271 }
272
273
274 /**
275 * Unmap the texture images which were used by the vertex program (if any).
276 */
277 static void
278 unmap_textures(struct gl_context *ctx, const struct gl_vertex_program *vp)
279 {
280 GLuint u;
281
282 for (u = 0; u < ctx->Const.VertexProgram.MaxTextureImageUnits; u++) {
283 if (vp->Base.TexturesUsed[u]) {
284 /* Note: _Current *should* correspond to the target indicated
285 * in TexturesUsed[u].
286 */
287 _swrast_unmap_texture(ctx, ctx->Texture.Unit[u]._Current);
288 }
289 }
290 }
291
292
293 /**
294 * This function executes vertex programs
295 */
296 static GLboolean
297 run_vp( struct gl_context *ctx, struct tnl_pipeline_stage *stage )
298 {
299 TNLcontext *tnl = TNL_CONTEXT(ctx);
300 struct vp_stage_data *store = VP_STAGE_DATA(stage);
301 struct vertex_buffer *VB = &tnl->vb;
302 struct gl_vertex_program *program = ctx->VertexProgram._Current;
303 struct gl_program_machine *machine = &store->machine;
304 GLuint outputs[VARYING_SLOT_MAX], numOutputs;
305 GLuint i, j;
306
307 if (!program)
308 return GL_TRUE;
309
310 /* ARB program or vertex shader */
311 _mesa_load_state_parameters(ctx, program->Base.Parameters);
312
313 /* make list of outputs to save some time below */
314 numOutputs = 0;
315 for (i = 0; i < VARYING_SLOT_MAX; i++) {
316 if (program->Base.OutputsWritten & BITFIELD64_BIT(i)) {
317 outputs[numOutputs++] = i;
318 }
319 }
320
321 /* Allocate result vectors. We delay this until now to avoid allocating
322 * memory that would never be used if we don't run the software tnl pipeline.
323 */
324 if (!store->results[0].storage) {
325 for (i = 0; i < VARYING_SLOT_MAX; i++) {
326 assert(!store->results[i].storage);
327 _mesa_vector4f_alloc( &store->results[i], 0, VB->Size, 32 );
328 store->results[i].size = 4;
329 }
330 }
331
332 map_textures(ctx, program);
333
334 for (i = 0; i < VB->Count; i++) {
335 GLuint attr;
336
337 init_machine(ctx, machine, tnl->CurInstance);
338
339 #if 0
340 printf("Input %d: %f, %f, %f, %f\n", i,
341 VB->AttribPtr[0]->data[i][0],
342 VB->AttribPtr[0]->data[i][1],
343 VB->AttribPtr[0]->data[i][2],
344 VB->AttribPtr[0]->data[i][3]);
345 printf(" color: %f, %f, %f, %f\n",
346 VB->AttribPtr[3]->data[i][0],
347 VB->AttribPtr[3]->data[i][1],
348 VB->AttribPtr[3]->data[i][2],
349 VB->AttribPtr[3]->data[i][3]);
350 printf(" normal: %f, %f, %f, %f\n",
351 VB->AttribPtr[2]->data[i][0],
352 VB->AttribPtr[2]->data[i][1],
353 VB->AttribPtr[2]->data[i][2],
354 VB->AttribPtr[2]->data[i][3]);
355 #endif
356
357 /* the vertex array case */
358 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
359 if (program->Base.InputsRead & BITFIELD64_BIT(attr)) {
360 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data;
361 const GLuint size = VB->AttribPtr[attr]->size;
362 const GLuint stride = VB->AttribPtr[attr]->stride;
363 const GLfloat *data = (GLfloat *) (ptr + stride * i);
364 #ifdef NAN_CHECK
365 check_float(data[0]);
366 check_float(data[1]);
367 check_float(data[2]);
368 check_float(data[3]);
369 #endif
370 COPY_CLEAN_4V(machine->VertAttribs[attr], size, data);
371 }
372 }
373
374 /* execute the program */
375 _mesa_execute_program(ctx, &program->Base, machine);
376
377 /* copy the output registers into the VB->attribs arrays */
378 for (j = 0; j < numOutputs; j++) {
379 const GLuint attr = outputs[j];
380 #ifdef NAN_CHECK
381 check_float(machine->Outputs[attr][0]);
382 check_float(machine->Outputs[attr][1]);
383 check_float(machine->Outputs[attr][2]);
384 check_float(machine->Outputs[attr][3]);
385 #endif
386 COPY_4V(store->results[attr].data[i], machine->Outputs[attr]);
387 }
388
389 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
390 if (program->Base.OutputsWritten & BITFIELD64_BIT(VARYING_SLOT_FOGC)) {
391 store->results[VARYING_SLOT_FOGC].data[i][1] = 0.0;
392 store->results[VARYING_SLOT_FOGC].data[i][2] = 0.0;
393 store->results[VARYING_SLOT_FOGC].data[i][3] = 1.0;
394 }
395 #ifdef NAN_CHECK
396 ASSERT(machine->Outputs[0][3] != 0.0F);
397 #endif
398 #if 0
399 printf("HPOS: %f %f %f %f\n",
400 machine->Outputs[0][0],
401 machine->Outputs[0][1],
402 machine->Outputs[0][2],
403 machine->Outputs[0][3]);
404 #endif
405 }
406
407 unmap_textures(ctx, program);
408
409 if (program->IsPositionInvariant) {
410 /* We need the exact same transform as in the fixed function path here
411 * to guarantee invariance, depending on compiler optimization flags
412 * results could be different otherwise.
413 */
414 VB->ClipPtr = TransformRaw( &store->results[0],
415 &ctx->_ModelProjectMatrix,
416 VB->AttribPtr[0] );
417
418 /* Drivers expect this to be clean to element 4...
419 */
420 switch (VB->ClipPtr->size) {
421 case 1:
422 /* impossible */
423 case 2:
424 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
425 /* fall-through */
426 case 3:
427 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
428 /* fall-through */
429 case 4:
430 break;
431 }
432 }
433 else {
434 /* Setup the VB pointers so that the next pipeline stages get
435 * their data from the right place (the program output arrays).
436 */
437 VB->ClipPtr = &store->results[VARYING_SLOT_POS];
438 VB->ClipPtr->size = 4;
439 VB->ClipPtr->count = VB->Count;
440 }
441
442 VB->AttribPtr[VERT_ATTRIB_COLOR0] = &store->results[VARYING_SLOT_COL0];
443 VB->AttribPtr[VERT_ATTRIB_COLOR1] = &store->results[VARYING_SLOT_COL1];
444 VB->AttribPtr[VERT_ATTRIB_FOG] = &store->results[VARYING_SLOT_FOGC];
445 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->results[VARYING_SLOT_PSIZ];
446 VB->BackfaceColorPtr = &store->results[VARYING_SLOT_BFC0];
447 VB->BackfaceSecondaryColorPtr = &store->results[VARYING_SLOT_BFC1];
448
449 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
450 VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]
451 = &store->results[VARYING_SLOT_TEX0 + i];
452 }
453
454 for (i = 0; i < ctx->Const.MaxVarying; i++) {
455 if (program->Base.OutputsWritten & BITFIELD64_BIT(VARYING_SLOT_VAR0 + i)) {
456 /* Note: varying results get put into the generic attributes */
457 VB->AttribPtr[VERT_ATTRIB_GENERIC0+i]
458 = &store->results[VARYING_SLOT_VAR0 + i];
459 }
460 }
461
462
463 /* Perform NDC and cliptest operations:
464 */
465 return do_ndc_cliptest(ctx, store);
466 }
467
468
469 /**
470 * Called the first time stage->run is called. In effect, don't
471 * allocate data until the first time the stage is run.
472 */
473 static GLboolean
474 init_vp(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
475 {
476 TNLcontext *tnl = TNL_CONTEXT(ctx);
477 struct vertex_buffer *VB = &(tnl->vb);
478 struct vp_stage_data *store;
479 const GLuint size = VB->Size;
480
481 stage->privatePtr = calloc(1, sizeof(*store));
482 store = VP_STAGE_DATA(stage);
483 if (!store)
484 return GL_FALSE;
485
486 /* a few other misc allocations */
487 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 );
488 store->clipmask = _mesa_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 < VARYING_SLOT_MAX; i++)
507 _mesa_vector4f_free( &store->results[i] );
508
509 /* free misc arrays */
510 _mesa_vector4f_free( &store->ndcCoords );
511 _mesa_align_free( store->clipmask );
512
513 free( store );
514 stage->privatePtr = NULL;
515 }
516 }
517
518
519 static void
520 validate_vp_stage(struct gl_context *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 };