mesa: add swrast_texture_image::Buffer
[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 GLboolean vertex_textures;
72
73 struct gl_program_machine machine;
74 };
75
76
77 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
78
79
80 static void
81 userclip( struct gl_context *ctx,
82 GLvector4f *clip,
83 GLubyte *clipmask,
84 GLubyte *clipormask,
85 GLubyte *clipandmask )
86 {
87 GLuint p;
88
89 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
90 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
91 GLuint nr, i;
92 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
93 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
94 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
95 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
96 GLfloat *coord = (GLfloat *)clip->data;
97 GLuint stride = clip->stride;
98 GLuint count = clip->count;
99
100 for (nr = 0, i = 0 ; i < count ; i++) {
101 GLfloat dp = (coord[0] * a +
102 coord[1] * b +
103 coord[2] * c +
104 coord[3] * d);
105
106 if (dp < 0) {
107 nr++;
108 clipmask[i] |= CLIP_USER_BIT;
109 }
110
111 STRIDE_F(coord, stride);
112 }
113
114 if (nr > 0) {
115 *clipormask |= CLIP_USER_BIT;
116 if (nr == count) {
117 *clipandmask |= CLIP_USER_BIT;
118 return;
119 }
120 }
121 }
122 }
123 }
124
125
126 static GLboolean
127 do_ndc_cliptest(struct gl_context *ctx, struct vp_stage_data *store)
128 {
129 TNLcontext *tnl = TNL_CONTEXT(ctx);
130 struct vertex_buffer *VB = &tnl->vb;
131 /* Cliptest and perspective divide. Clip functions must clear
132 * the clipmask.
133 */
134 store->ormask = 0;
135 store->andmask = CLIP_FRUSTUM_BITS;
136
137 tnl_clip_prepare(ctx);
138
139 if (tnl->NeedNdcCoords) {
140 VB->NdcPtr =
141 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
142 &store->ndcCoords,
143 store->clipmask,
144 &store->ormask,
145 &store->andmask,
146 !ctx->Transform.DepthClamp );
147 }
148 else {
149 VB->NdcPtr = NULL;
150 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
151 NULL,
152 store->clipmask,
153 &store->ormask,
154 &store->andmask,
155 !ctx->Transform.DepthClamp );
156 }
157
158 if (store->andmask) {
159 /* All vertices are outside the frustum */
160 return GL_FALSE;
161 }
162
163 /* Test userclip planes. This contributes to VB->ClipMask.
164 */
165 /** XXX NEW_SLANG _Enabled ??? */
166 if (ctx->Transform.ClipPlanesEnabled && (!ctx->VertexProgram._Enabled ||
167 ctx->VertexProgram.Current->IsPositionInvariant)) {
168 userclip( ctx,
169 VB->ClipPtr,
170 store->clipmask,
171 &store->ormask,
172 &store->andmask );
173
174 if (store->andmask) {
175 return GL_FALSE;
176 }
177 }
178
179 VB->ClipAndMask = store->andmask;
180 VB->ClipOrMask = store->ormask;
181 VB->ClipMask = store->clipmask;
182
183 return GL_TRUE;
184 }
185
186
187 /**
188 * XXX the texture sampling code in this module is a bit of a hack.
189 * The texture sampling code is in swrast, though it doesn't have any
190 * real dependencies on the rest of swrast. It should probably be
191 * moved into main/ someday.
192 */
193 static void
194 vp_fetch_texel(struct gl_context *ctx, const GLfloat texcoord[4], GLfloat lambda,
195 GLuint unit, GLfloat color[4])
196 {
197 SWcontext *swrast = SWRAST_CONTEXT(ctx);
198
199 /* XXX use a float-valued TextureSample routine here!!! */
200 swrast->TextureSample[unit](ctx, ctx->Texture.Unit[unit]._Current,
201 1, (const GLfloat (*)[4]) texcoord,
202 &lambda, (GLfloat (*)[4]) color);
203 }
204
205
206 /**
207 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
208 * string has been parsed.
209 */
210 GLboolean
211 _tnl_program_string(struct gl_context *ctx, GLenum target, struct gl_program *program)
212 {
213 /* No-op.
214 * If we had derived anything from the program that was private to this
215 * stage we'd recompute/validate it here.
216 */
217 return GL_TRUE;
218 }
219
220
221 /**
222 * Initialize virtual machine state prior to executing vertex program.
223 */
224 static void
225 init_machine(struct gl_context *ctx, struct gl_program_machine *machine,
226 GLuint instID)
227 {
228 /* Input registers get initialized from the current vertex attribs */
229 memcpy(machine->VertAttribs, ctx->Current.Attrib,
230 MAX_VERTEX_GENERIC_ATTRIBS * 4 * sizeof(GLfloat));
231
232 if (ctx->VertexProgram._Current->IsNVProgram) {
233 GLuint i;
234 /* Output/result regs are initialized to [0,0,0,1] */
235 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_OUTPUTS; i++) {
236 ASSIGN_4V(machine->Outputs[i], 0.0F, 0.0F, 0.0F, 1.0F);
237 }
238 /* Temp regs are initialized to [0,0,0,0] */
239 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_TEMPS; i++) {
240 ASSIGN_4V(machine->Temporaries[i], 0.0F, 0.0F, 0.0F, 0.0F);
241 }
242 for (i = 0; i < MAX_VERTEX_PROGRAM_ADDRESS_REGS; i++) {
243 ASSIGN_4V(machine->AddressReg[i], 0, 0, 0, 0);
244 }
245 }
246
247 machine->NumDeriv = 0;
248
249 /* init condition codes */
250 machine->CondCodes[0] = COND_EQ;
251 machine->CondCodes[1] = COND_EQ;
252 machine->CondCodes[2] = COND_EQ;
253 machine->CondCodes[3] = COND_EQ;
254
255 /* init call stack */
256 machine->StackDepth = 0;
257
258 machine->FetchTexelLod = vp_fetch_texel;
259 machine->FetchTexelDeriv = NULL; /* not used by vertex programs */
260
261 machine->Samplers = ctx->VertexProgram._Current->Base.SamplerUnits;
262
263 machine->SystemValues[SYSTEM_VALUE_INSTANCE_ID][0] = (GLfloat) instID;
264 }
265
266
267 /**
268 * Map the texture images which the vertex program will access (if any).
269 */
270 static void
271 map_textures(struct gl_context *ctx, const struct gl_vertex_program *vp)
272 {
273 GLuint u;
274
275 if (!ctx->Driver.MapTexture)
276 return;
277
278 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) {
279 if (vp->Base.TexturesUsed[u]) {
280 /* Note: _Current *should* correspond to the target indicated
281 * in TexturesUsed[u].
282 */
283 ctx->Driver.MapTexture(ctx, ctx->Texture.Unit[u]._Current);
284 }
285 }
286 }
287
288
289 /**
290 * Unmap the texture images which were used by the vertex program (if any).
291 */
292 static void
293 unmap_textures(struct gl_context *ctx, const struct gl_vertex_program *vp)
294 {
295 GLuint u;
296
297 if (!ctx->Driver.MapTexture)
298 return;
299
300 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) {
301 if (vp->Base.TexturesUsed[u]) {
302 /* Note: _Current *should* correspond to the target indicated
303 * in TexturesUsed[u].
304 */
305 ctx->Driver.UnmapTexture(ctx, ctx->Texture.Unit[u]._Current);
306 }
307 }
308 }
309
310
311 /**
312 * This function executes vertex programs
313 */
314 static GLboolean
315 run_vp( struct gl_context *ctx, struct tnl_pipeline_stage *stage )
316 {
317 TNLcontext *tnl = TNL_CONTEXT(ctx);
318 struct vp_stage_data *store = VP_STAGE_DATA(stage);
319 struct vertex_buffer *VB = &tnl->vb;
320 struct gl_vertex_program *program = ctx->VertexProgram._Current;
321 struct gl_program_machine *machine = &store->machine;
322 GLuint outputs[VERT_RESULT_MAX], numOutputs;
323 GLuint i, j;
324
325 if (!program)
326 return GL_TRUE;
327
328 if (program->IsNVProgram) {
329 _mesa_load_tracked_matrices(ctx);
330 }
331 else {
332 /* ARB program or vertex shader */
333 _mesa_load_state_parameters(ctx, program->Base.Parameters);
334 }
335
336 /* make list of outputs to save some time below */
337 numOutputs = 0;
338 for (i = 0; i < VERT_RESULT_MAX; i++) {
339 if (program->Base.OutputsWritten & BITFIELD64_BIT(i)) {
340 outputs[numOutputs++] = i;
341 }
342 }
343
344 /* Allocate result vectors. We delay this until now to avoid allocating
345 * memory that would never be used if we don't run the software tnl pipeline.
346 */
347 if (!store->results[0].storage) {
348 for (i = 0; i < VERT_RESULT_MAX; i++) {
349 assert(!store->results[i].storage);
350 _mesa_vector4f_alloc( &store->results[i], 0, VB->Size, 32 );
351 store->results[i].size = 4;
352 }
353 }
354
355 map_textures(ctx, program);
356
357 for (i = 0; i < VB->Count; i++) {
358 GLuint attr;
359
360 init_machine(ctx, machine, tnl->CurInstance);
361
362 #if 0
363 printf("Input %d: %f, %f, %f, %f\n", i,
364 VB->AttribPtr[0]->data[i][0],
365 VB->AttribPtr[0]->data[i][1],
366 VB->AttribPtr[0]->data[i][2],
367 VB->AttribPtr[0]->data[i][3]);
368 printf(" color: %f, %f, %f, %f\n",
369 VB->AttribPtr[3]->data[i][0],
370 VB->AttribPtr[3]->data[i][1],
371 VB->AttribPtr[3]->data[i][2],
372 VB->AttribPtr[3]->data[i][3]);
373 printf(" normal: %f, %f, %f, %f\n",
374 VB->AttribPtr[2]->data[i][0],
375 VB->AttribPtr[2]->data[i][1],
376 VB->AttribPtr[2]->data[i][2],
377 VB->AttribPtr[2]->data[i][3]);
378 #endif
379
380 /* the vertex array case */
381 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
382 if (program->Base.InputsRead & (1 << attr)) {
383 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data;
384 const GLuint size = VB->AttribPtr[attr]->size;
385 const GLuint stride = VB->AttribPtr[attr]->stride;
386 const GLfloat *data = (GLfloat *) (ptr + stride * i);
387 #ifdef NAN_CHECK
388 check_float(data[0]);
389 check_float(data[1]);
390 check_float(data[2]);
391 check_float(data[3]);
392 #endif
393 COPY_CLEAN_4V(machine->VertAttribs[attr], size, data);
394 }
395 }
396
397 /* execute the program */
398 _mesa_execute_program(ctx, &program->Base, machine);
399
400 /* copy the output registers into the VB->attribs arrays */
401 for (j = 0; j < numOutputs; j++) {
402 const GLuint attr = outputs[j];
403 #ifdef NAN_CHECK
404 check_float(machine->Outputs[attr][0]);
405 check_float(machine->Outputs[attr][1]);
406 check_float(machine->Outputs[attr][2]);
407 check_float(machine->Outputs[attr][3]);
408 #endif
409 COPY_4V(store->results[attr].data[i], machine->Outputs[attr]);
410 }
411
412 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
413 if (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_FOGC)) {
414 store->results[VERT_RESULT_FOGC].data[i][1] = 0.0;
415 store->results[VERT_RESULT_FOGC].data[i][2] = 0.0;
416 store->results[VERT_RESULT_FOGC].data[i][3] = 1.0;
417 }
418 #ifdef NAN_CHECK
419 ASSERT(machine->Outputs[0][3] != 0.0F);
420 #endif
421 #if 0
422 printf("HPOS: %f %f %f %f\n",
423 machine->Outputs[0][0],
424 machine->Outputs[0][1],
425 machine->Outputs[0][2],
426 machine->Outputs[0][3]);
427 #endif
428 }
429
430 unmap_textures(ctx, program);
431
432 /* Fixup fog and point size results if needed */
433 if (program->IsNVProgram) {
434 if (ctx->Fog.Enabled &&
435 (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_FOGC)) == 0) {
436 for (i = 0; i < VB->Count; i++) {
437 store->results[VERT_RESULT_FOGC].data[i][0] = 1.0;
438 }
439 }
440
441 if (ctx->VertexProgram.PointSizeEnabled &&
442 (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_PSIZ)) == 0) {
443 for (i = 0; i < VB->Count; i++) {
444 store->results[VERT_RESULT_PSIZ].data[i][0] = ctx->Point.Size;
445 }
446 }
447 }
448
449 if (program->IsPositionInvariant) {
450 /* We need the exact same transform as in the fixed function path here
451 * to guarantee invariance, depending on compiler optimization flags
452 * results could be different otherwise.
453 */
454 VB->ClipPtr = TransformRaw( &store->results[0],
455 &ctx->_ModelProjectMatrix,
456 VB->AttribPtr[0] );
457
458 /* Drivers expect this to be clean to element 4...
459 */
460 switch (VB->ClipPtr->size) {
461 case 1:
462 /* impossible */
463 case 2:
464 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
465 /* fall-through */
466 case 3:
467 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
468 /* fall-through */
469 case 4:
470 break;
471 }
472 }
473 else {
474 /* Setup the VB pointers so that the next pipeline stages get
475 * their data from the right place (the program output arrays).
476 */
477 VB->ClipPtr = &store->results[VERT_RESULT_HPOS];
478 VB->ClipPtr->size = 4;
479 VB->ClipPtr->count = VB->Count;
480 }
481
482 VB->AttribPtr[VERT_ATTRIB_COLOR0] = &store->results[VERT_RESULT_COL0];
483 VB->AttribPtr[VERT_ATTRIB_COLOR1] = &store->results[VERT_RESULT_COL1];
484 VB->AttribPtr[VERT_ATTRIB_FOG] = &store->results[VERT_RESULT_FOGC];
485 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->results[VERT_RESULT_PSIZ];
486 VB->BackfaceColorPtr = &store->results[VERT_RESULT_BFC0];
487 VB->BackfaceSecondaryColorPtr = &store->results[VERT_RESULT_BFC1];
488
489 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
490 VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]
491 = &store->results[VERT_RESULT_TEX0 + i];
492 }
493
494 for (i = 0; i < ctx->Const.MaxVarying; i++) {
495 if (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_VAR0 + i)) {
496 /* Note: varying results get put into the generic attributes */
497 VB->AttribPtr[VERT_ATTRIB_GENERIC0+i]
498 = &store->results[VERT_RESULT_VAR0 + i];
499 }
500 }
501
502
503 /* Perform NDC and cliptest operations:
504 */
505 return do_ndc_cliptest(ctx, store);
506 }
507
508
509 /**
510 * Called the first time stage->run is called. In effect, don't
511 * allocate data until the first time the stage is run.
512 */
513 static GLboolean
514 init_vp(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
515 {
516 TNLcontext *tnl = TNL_CONTEXT(ctx);
517 struct vertex_buffer *VB = &(tnl->vb);
518 struct vp_stage_data *store;
519 const GLuint size = VB->Size;
520
521 stage->privatePtr = CALLOC(sizeof(*store));
522 store = VP_STAGE_DATA(stage);
523 if (!store)
524 return GL_FALSE;
525
526 /* a few other misc allocations */
527 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 );
528 store->clipmask = (GLubyte *) _mesa_align_malloc(sizeof(GLubyte)*size, 32 );
529
530 return GL_TRUE;
531 }
532
533
534 /**
535 * Destructor for this pipeline stage.
536 */
537 static void
538 dtr(struct tnl_pipeline_stage *stage)
539 {
540 struct vp_stage_data *store = VP_STAGE_DATA(stage);
541
542 if (store) {
543 GLuint i;
544
545 /* free the vertex program result arrays */
546 for (i = 0; i < VERT_RESULT_MAX; i++)
547 _mesa_vector4f_free( &store->results[i] );
548
549 /* free misc arrays */
550 _mesa_vector4f_free( &store->ndcCoords );
551 _mesa_align_free( store->clipmask );
552
553 FREE( store );
554 stage->privatePtr = NULL;
555 }
556 }
557
558
559 static void
560 validate_vp_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
561 {
562 if (ctx->VertexProgram._Current) {
563 _swrast_update_texture_samplers(ctx);
564 }
565 }
566
567
568
569 /**
570 * Public description of this pipeline stage.
571 */
572 const struct tnl_pipeline_stage _tnl_vertex_program_stage =
573 {
574 "vertex-program",
575 NULL, /* private_data */
576 init_vp, /* create */
577 dtr, /* destroy */
578 validate_vp_stage, /* validate */
579 run_vp /* run -- initially set to ctr */
580 };