2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
28 * \file tnl/t_vb_program.c
29 * \brief Pipeline stage for executing vertex programs.
30 * \author Brian Paul, Keith Whitwell
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"
45 #include "tnl/t_context.h"
46 #include "tnl/t_pipeline.h"
50 /** Check for NaNs and very large values */
54 assert(!IS_INF_OR_NAN(x
));
55 assert(1.0e-15 <= x
&& x
<= 1.0e15
);
61 * Private storage for the vertex program pipeline stage.
63 struct vp_stage_data
{
64 /** The results of running the vertex program go into these arrays. */
65 GLvector4f results
[VERT_RESULT_MAX
];
67 GLvector4f ndcCoords
; /**< normalized device coords */
68 GLubyte
*clipmask
; /**< clip flags */
69 GLubyte ormask
, andmask
; /**< for clipping */
71 GLboolean vertex_textures
;
73 struct gl_program_machine machine
;
77 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
81 userclip( struct gl_context
*ctx
,
85 GLubyte
*clipandmask
)
89 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
90 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
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
;
100 for (nr
= 0, i
= 0 ; i
< count
; i
++) {
101 GLfloat dp
= (coord
[0] * a
+
108 clipmask
[i
] |= CLIP_USER_BIT
;
111 STRIDE_F(coord
, stride
);
115 *clipormask
|= CLIP_USER_BIT
;
117 *clipandmask
|= CLIP_USER_BIT
;
127 do_ndc_cliptest(struct gl_context
*ctx
, struct vp_stage_data
*store
)
129 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
130 struct vertex_buffer
*VB
= &tnl
->vb
;
131 /* Cliptest and perspective divide. Clip functions must clear
135 store
->andmask
= CLIP_FRUSTUM_BITS
;
137 tnl_clip_prepare(ctx
);
139 if (tnl
->NeedNdcCoords
) {
141 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
146 !ctx
->Transform
.DepthClamp
);
150 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
155 !ctx
->Transform
.DepthClamp
);
158 if (store
->andmask
) {
159 /* All vertices are outside the frustum */
163 /* Test userclip planes. This contributes to VB->ClipMask.
165 /** XXX NEW_SLANG _Enabled ??? */
166 if (ctx
->Transform
.ClipPlanesEnabled
&& (!ctx
->VertexProgram
._Enabled
||
167 ctx
->VertexProgram
.Current
->IsPositionInvariant
)) {
174 if (store
->andmask
) {
179 VB
->ClipAndMask
= store
->andmask
;
180 VB
->ClipOrMask
= store
->ormask
;
181 VB
->ClipMask
= store
->clipmask
;
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.
194 vp_fetch_texel(struct gl_context
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
195 GLuint unit
, GLfloat color
[4])
197 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
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
);
207 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
208 * string has been parsed.
211 _tnl_program_string(struct gl_context
*ctx
, GLenum target
, struct gl_program
*program
)
214 * If we had derived anything from the program that was private to this
215 * stage we'd recompute/validate it here.
222 * Initialize virtual machine state prior to executing vertex program.
225 init_machine(struct gl_context
*ctx
, struct gl_program_machine
*machine
,
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
));
232 if (ctx
->VertexProgram
._Current
->IsNVProgram
) {
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
);
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
);
242 for (i
= 0; i
< MAX_VERTEX_PROGRAM_ADDRESS_REGS
; i
++) {
243 ASSIGN_4V(machine
->AddressReg
[i
], 0, 0, 0, 0);
247 machine
->NumDeriv
= 0;
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
;
255 /* init call stack */
256 machine
->StackDepth
= 0;
258 machine
->FetchTexelLod
= vp_fetch_texel
;
259 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
261 machine
->Samplers
= ctx
->VertexProgram
._Current
->Base
.SamplerUnits
;
263 machine
->SystemValues
[SYSTEM_VALUE_INSTANCE_ID
][0] = (GLfloat
) instID
;
268 * Map the texture images which the vertex program will access (if any).
271 map_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
275 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
276 if (vp
->Base
.TexturesUsed
[u
]) {
277 /* Note: _Current *should* correspond to the target indicated
278 * in TexturesUsed[u].
280 _swrast_map_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
287 * Unmap the texture images which were used by the vertex program (if any).
290 unmap_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
294 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
295 if (vp
->Base
.TexturesUsed
[u
]) {
296 /* Note: _Current *should* correspond to the target indicated
297 * in TexturesUsed[u].
299 _swrast_unmap_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
306 * This function executes vertex programs
309 run_vp( struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
311 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
312 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
313 struct vertex_buffer
*VB
= &tnl
->vb
;
314 struct gl_vertex_program
*program
= ctx
->VertexProgram
._Current
;
315 struct gl_program_machine
*machine
= &store
->machine
;
316 GLuint outputs
[VERT_RESULT_MAX
], numOutputs
;
322 if (program
->IsNVProgram
) {
323 _mesa_load_tracked_matrices(ctx
);
326 /* ARB program or vertex shader */
327 _mesa_load_state_parameters(ctx
, program
->Base
.Parameters
);
330 /* make list of outputs to save some time below */
332 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
333 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(i
)) {
334 outputs
[numOutputs
++] = i
;
338 /* Allocate result vectors. We delay this until now to avoid allocating
339 * memory that would never be used if we don't run the software tnl pipeline.
341 if (!store
->results
[0].storage
) {
342 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
343 assert(!store
->results
[i
].storage
);
344 _mesa_vector4f_alloc( &store
->results
[i
], 0, VB
->Size
, 32 );
345 store
->results
[i
].size
= 4;
349 map_textures(ctx
, program
);
351 for (i
= 0; i
< VB
->Count
; i
++) {
354 init_machine(ctx
, machine
, tnl
->CurInstance
);
357 printf("Input %d: %f, %f, %f, %f\n", i
,
358 VB
->AttribPtr
[0]->data
[i
][0],
359 VB
->AttribPtr
[0]->data
[i
][1],
360 VB
->AttribPtr
[0]->data
[i
][2],
361 VB
->AttribPtr
[0]->data
[i
][3]);
362 printf(" color: %f, %f, %f, %f\n",
363 VB
->AttribPtr
[3]->data
[i
][0],
364 VB
->AttribPtr
[3]->data
[i
][1],
365 VB
->AttribPtr
[3]->data
[i
][2],
366 VB
->AttribPtr
[3]->data
[i
][3]);
367 printf(" normal: %f, %f, %f, %f\n",
368 VB
->AttribPtr
[2]->data
[i
][0],
369 VB
->AttribPtr
[2]->data
[i
][1],
370 VB
->AttribPtr
[2]->data
[i
][2],
371 VB
->AttribPtr
[2]->data
[i
][3]);
374 /* the vertex array case */
375 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
376 if (program
->Base
.InputsRead
& BITFIELD64_BIT(attr
)) {
377 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
378 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
379 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
380 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
382 check_float(data
[0]);
383 check_float(data
[1]);
384 check_float(data
[2]);
385 check_float(data
[3]);
387 COPY_CLEAN_4V(machine
->VertAttribs
[attr
], size
, data
);
391 /* execute the program */
392 _mesa_execute_program(ctx
, &program
->Base
, machine
);
394 /* copy the output registers into the VB->attribs arrays */
395 for (j
= 0; j
< numOutputs
; j
++) {
396 const GLuint attr
= outputs
[j
];
398 check_float(machine
->Outputs
[attr
][0]);
399 check_float(machine
->Outputs
[attr
][1]);
400 check_float(machine
->Outputs
[attr
][2]);
401 check_float(machine
->Outputs
[attr
][3]);
403 COPY_4V(store
->results
[attr
].data
[i
], machine
->Outputs
[attr
]);
406 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
407 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_FOGC
)) {
408 store
->results
[VERT_RESULT_FOGC
].data
[i
][1] = 0.0;
409 store
->results
[VERT_RESULT_FOGC
].data
[i
][2] = 0.0;
410 store
->results
[VERT_RESULT_FOGC
].data
[i
][3] = 1.0;
413 ASSERT(machine
->Outputs
[0][3] != 0.0F
);
416 printf("HPOS: %f %f %f %f\n",
417 machine
->Outputs
[0][0],
418 machine
->Outputs
[0][1],
419 machine
->Outputs
[0][2],
420 machine
->Outputs
[0][3]);
424 unmap_textures(ctx
, program
);
426 /* Fixup fog and point size results if needed */
427 if (program
->IsNVProgram
) {
428 if (ctx
->Fog
.Enabled
&&
429 (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_FOGC
)) == 0) {
430 for (i
= 0; i
< VB
->Count
; i
++) {
431 store
->results
[VERT_RESULT_FOGC
].data
[i
][0] = 1.0;
435 if (ctx
->VertexProgram
.PointSizeEnabled
&&
436 (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) == 0) {
437 for (i
= 0; i
< VB
->Count
; i
++) {
438 store
->results
[VERT_RESULT_PSIZ
].data
[i
][0] = ctx
->Point
.Size
;
443 if (program
->IsPositionInvariant
) {
444 /* We need the exact same transform as in the fixed function path here
445 * to guarantee invariance, depending on compiler optimization flags
446 * results could be different otherwise.
448 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
449 &ctx
->_ModelProjectMatrix
,
452 /* Drivers expect this to be clean to element 4...
454 switch (VB
->ClipPtr
->size
) {
458 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
461 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
468 /* Setup the VB pointers so that the next pipeline stages get
469 * their data from the right place (the program output arrays).
471 VB
->ClipPtr
= &store
->results
[VERT_RESULT_HPOS
];
472 VB
->ClipPtr
->size
= 4;
473 VB
->ClipPtr
->count
= VB
->Count
;
476 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = &store
->results
[VERT_RESULT_COL0
];
477 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = &store
->results
[VERT_RESULT_COL1
];
478 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = &store
->results
[VERT_RESULT_FOGC
];
479 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->results
[VERT_RESULT_PSIZ
];
480 VB
->BackfaceColorPtr
= &store
->results
[VERT_RESULT_BFC0
];
481 VB
->BackfaceSecondaryColorPtr
= &store
->results
[VERT_RESULT_BFC1
];
483 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
484 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
485 = &store
->results
[VERT_RESULT_TEX0
+ i
];
488 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
489 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_VAR0
+ i
)) {
490 /* Note: varying results get put into the generic attributes */
491 VB
->AttribPtr
[VERT_ATTRIB_GENERIC0
+i
]
492 = &store
->results
[VERT_RESULT_VAR0
+ i
];
497 /* Perform NDC and cliptest operations:
499 return do_ndc_cliptest(ctx
, store
);
504 * Called the first time stage->run is called. In effect, don't
505 * allocate data until the first time the stage is run.
508 init_vp(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
510 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
511 struct vertex_buffer
*VB
= &(tnl
->vb
);
512 struct vp_stage_data
*store
;
513 const GLuint size
= VB
->Size
;
515 stage
->privatePtr
= CALLOC(sizeof(*store
));
516 store
= VP_STAGE_DATA(stage
);
520 /* a few other misc allocations */
521 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
522 store
->clipmask
= (GLubyte
*) _mesa_align_malloc(sizeof(GLubyte
)*size
, 32 );
529 * Destructor for this pipeline stage.
532 dtr(struct tnl_pipeline_stage
*stage
)
534 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
539 /* free the vertex program result arrays */
540 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
541 _mesa_vector4f_free( &store
->results
[i
] );
543 /* free misc arrays */
544 _mesa_vector4f_free( &store
->ndcCoords
);
545 _mesa_align_free( store
->clipmask
);
548 stage
->privatePtr
= NULL
;
554 validate_vp_stage(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
556 if (ctx
->VertexProgram
._Current
) {
557 _swrast_update_texture_samplers(ctx
);
564 * Public description of this pipeline stage.
566 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
569 NULL
, /* private_data */
570 init_vp
, /* create */
572 validate_vp_stage
, /* validate */
573 run_vp
/* run -- initially set to ctr */