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 struct gl_program_machine machine
;
75 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
79 userclip( struct gl_context
*ctx
,
83 GLubyte
*clipandmask
)
87 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
88 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
90 const GLfloat a
= ctx
->Transform
._ClipUserPlane
[p
][0];
91 const GLfloat b
= ctx
->Transform
._ClipUserPlane
[p
][1];
92 const GLfloat c
= ctx
->Transform
._ClipUserPlane
[p
][2];
93 const GLfloat d
= ctx
->Transform
._ClipUserPlane
[p
][3];
94 GLfloat
*coord
= (GLfloat
*)clip
->data
;
95 GLuint stride
= clip
->stride
;
96 GLuint count
= clip
->count
;
98 for (nr
= 0, i
= 0 ; i
< count
; i
++) {
99 GLfloat dp
= (coord
[0] * a
+
106 clipmask
[i
] |= CLIP_USER_BIT
;
109 STRIDE_F(coord
, stride
);
113 *clipormask
|= CLIP_USER_BIT
;
115 *clipandmask
|= CLIP_USER_BIT
;
125 do_ndc_cliptest(struct gl_context
*ctx
, struct vp_stage_data
*store
)
127 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
128 struct vertex_buffer
*VB
= &tnl
->vb
;
129 /* Cliptest and perspective divide. Clip functions must clear
133 store
->andmask
= CLIP_FRUSTUM_BITS
;
135 tnl_clip_prepare(ctx
);
137 if (tnl
->NeedNdcCoords
) {
139 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
144 !ctx
->Transform
.DepthClamp
);
148 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
153 !ctx
->Transform
.DepthClamp
);
156 if (store
->andmask
) {
157 /* All vertices are outside the frustum */
161 /* Test userclip planes. This contributes to VB->ClipMask.
163 /** XXX NEW_SLANG _Enabled ??? */
164 if (ctx
->Transform
.ClipPlanesEnabled
&& (!ctx
->VertexProgram
._Enabled
||
165 ctx
->VertexProgram
.Current
->IsPositionInvariant
)) {
172 if (store
->andmask
) {
177 VB
->ClipAndMask
= store
->andmask
;
178 VB
->ClipOrMask
= store
->ormask
;
179 VB
->ClipMask
= store
->clipmask
;
186 * XXX the texture sampling code in this module is a bit of a hack.
187 * The texture sampling code is in swrast, though it doesn't have any
188 * real dependencies on the rest of swrast. It should probably be
189 * moved into main/ someday.
192 vp_fetch_texel(struct gl_context
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
193 GLuint unit
, GLfloat color
[4])
195 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
197 /* XXX use a float-valued TextureSample routine here!!! */
198 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
199 1, (const GLfloat (*)[4]) texcoord
,
200 &lambda
, (GLfloat (*)[4]) color
);
205 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
206 * string has been parsed.
209 _tnl_program_string(struct gl_context
*ctx
, GLenum target
, struct gl_program
*program
)
212 * If we had derived anything from the program that was private to this
213 * stage we'd recompute/validate it here.
220 * Initialize virtual machine state prior to executing vertex program.
223 init_machine(struct gl_context
*ctx
, struct gl_program_machine
*machine
)
225 /* Input registers get initialized from the current vertex attribs */
226 memcpy(machine
->VertAttribs
, ctx
->Current
.Attrib
,
227 MAX_VERTEX_GENERIC_ATTRIBS
* 4 * sizeof(GLfloat
));
229 if (ctx
->VertexProgram
._Current
->IsNVProgram
) {
231 /* Output/result regs are initialized to [0,0,0,1] */
232 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_OUTPUTS
; i
++) {
233 ASSIGN_4V(machine
->Outputs
[i
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
235 /* Temp regs are initialized to [0,0,0,0] */
236 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_TEMPS
; i
++) {
237 ASSIGN_4V(machine
->Temporaries
[i
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
239 for (i
= 0; i
< MAX_VERTEX_PROGRAM_ADDRESS_REGS
; i
++) {
240 ASSIGN_4V(machine
->AddressReg
[i
], 0, 0, 0, 0);
244 machine
->NumDeriv
= 0;
246 /* init condition codes */
247 machine
->CondCodes
[0] = COND_EQ
;
248 machine
->CondCodes
[1] = COND_EQ
;
249 machine
->CondCodes
[2] = COND_EQ
;
250 machine
->CondCodes
[3] = COND_EQ
;
252 /* init call stack */
253 machine
->StackDepth
= 0;
255 machine
->FetchTexelLod
= vp_fetch_texel
;
256 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
258 machine
->Samplers
= ctx
->VertexProgram
._Current
->Base
.SamplerUnits
;
263 * Map the texture images which the vertex program will access (if any).
266 map_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
270 if (!ctx
->Driver
.MapTexture
)
273 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
274 if (vp
->Base
.TexturesUsed
[u
]) {
275 /* Note: _Current *should* correspond to the target indicated
276 * in TexturesUsed[u].
278 ctx
->Driver
.MapTexture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
285 * Unmap the texture images which were used by the vertex program (if any).
288 unmap_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
292 if (!ctx
->Driver
.MapTexture
)
295 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
296 if (vp
->Base
.TexturesUsed
[u
]) {
297 /* Note: _Current *should* correspond to the target indicated
298 * in TexturesUsed[u].
300 ctx
->Driver
.UnmapTexture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
307 * This function executes vertex programs
310 run_vp( struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
312 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
313 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
314 struct vertex_buffer
*VB
= &tnl
->vb
;
315 struct gl_vertex_program
*program
= ctx
->VertexProgram
._Current
;
316 struct gl_program_machine
*machine
= &store
->machine
;
317 GLuint outputs
[VERT_RESULT_MAX
], numOutputs
;
323 if (program
->IsNVProgram
) {
324 _mesa_load_tracked_matrices(ctx
);
327 /* ARB program or vertex shader */
328 _mesa_load_state_parameters(ctx
, program
->Base
.Parameters
);
331 /* make list of outputs to save some time below */
333 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
334 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(i
)) {
335 outputs
[numOutputs
++] = i
;
339 map_textures(ctx
, program
);
341 for (i
= 0; i
< VB
->Count
; i
++) {
344 init_machine(ctx
, machine
);
347 printf("Input %d: %f, %f, %f, %f\n", i
,
348 VB
->AttribPtr
[0]->data
[i
][0],
349 VB
->AttribPtr
[0]->data
[i
][1],
350 VB
->AttribPtr
[0]->data
[i
][2],
351 VB
->AttribPtr
[0]->data
[i
][3]);
352 printf(" color: %f, %f, %f, %f\n",
353 VB
->AttribPtr
[3]->data
[i
][0],
354 VB
->AttribPtr
[3]->data
[i
][1],
355 VB
->AttribPtr
[3]->data
[i
][2],
356 VB
->AttribPtr
[3]->data
[i
][3]);
357 printf(" normal: %f, %f, %f, %f\n",
358 VB
->AttribPtr
[2]->data
[i
][0],
359 VB
->AttribPtr
[2]->data
[i
][1],
360 VB
->AttribPtr
[2]->data
[i
][2],
361 VB
->AttribPtr
[2]->data
[i
][3]);
364 /* the vertex array case */
365 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
366 if (program
->Base
.InputsRead
& (1 << attr
)) {
367 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
368 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
369 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
370 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
372 check_float(data
[0]);
373 check_float(data
[1]);
374 check_float(data
[2]);
375 check_float(data
[3]);
377 COPY_CLEAN_4V(machine
->VertAttribs
[attr
], size
, data
);
381 /* execute the program */
382 _mesa_execute_program(ctx
, &program
->Base
, machine
);
384 /* copy the output registers into the VB->attribs arrays */
385 for (j
= 0; j
< numOutputs
; j
++) {
386 const GLuint attr
= outputs
[j
];
388 check_float(machine
->Outputs
[attr
][0]);
389 check_float(machine
->Outputs
[attr
][1]);
390 check_float(machine
->Outputs
[attr
][2]);
391 check_float(machine
->Outputs
[attr
][3]);
393 COPY_4V(store
->results
[attr
].data
[i
], machine
->Outputs
[attr
]);
396 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
397 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_FOGC
)) {
398 store
->results
[VERT_RESULT_FOGC
].data
[i
][1] = 0.0;
399 store
->results
[VERT_RESULT_FOGC
].data
[i
][2] = 0.0;
400 store
->results
[VERT_RESULT_FOGC
].data
[i
][3] = 1.0;
403 ASSERT(machine
->Outputs
[0][3] != 0.0F
);
406 printf("HPOS: %f %f %f %f\n",
407 machine
->Outputs
[0][0],
408 machine
->Outputs
[0][1],
409 machine
->Outputs
[0][2],
410 machine
->Outputs
[0][3]);
414 unmap_textures(ctx
, program
);
416 /* Fixup fog and point size results if needed */
417 if (program
->IsNVProgram
) {
418 if (ctx
->Fog
.Enabled
&&
419 (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_FOGC
)) == 0) {
420 for (i
= 0; i
< VB
->Count
; i
++) {
421 store
->results
[VERT_RESULT_FOGC
].data
[i
][0] = 1.0;
425 if (ctx
->VertexProgram
.PointSizeEnabled
&&
426 (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_PSIZ
)) == 0) {
427 for (i
= 0; i
< VB
->Count
; i
++) {
428 store
->results
[VERT_RESULT_PSIZ
].data
[i
][0] = ctx
->Point
.Size
;
433 if (program
->IsPositionInvariant
) {
434 /* We need the exact same transform as in the fixed function path here
435 * to guarantee invariance, depending on compiler optimization flags
436 * results could be different otherwise.
438 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
439 &ctx
->_ModelProjectMatrix
,
442 /* Drivers expect this to be clean to element 4...
444 switch (VB
->ClipPtr
->size
) {
448 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
451 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
458 /* Setup the VB pointers so that the next pipeline stages get
459 * their data from the right place (the program output arrays).
461 VB
->ClipPtr
= &store
->results
[VERT_RESULT_HPOS
];
462 VB
->ClipPtr
->size
= 4;
463 VB
->ClipPtr
->count
= VB
->Count
;
466 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = &store
->results
[VERT_RESULT_COL0
];
467 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = &store
->results
[VERT_RESULT_COL1
];
468 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = &store
->results
[VERT_RESULT_FOGC
];
469 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->results
[VERT_RESULT_PSIZ
];
470 VB
->BackfaceColorPtr
= &store
->results
[VERT_RESULT_BFC0
];
471 VB
->BackfaceSecondaryColorPtr
= &store
->results
[VERT_RESULT_BFC1
];
473 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
474 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
475 = &store
->results
[VERT_RESULT_TEX0
+ i
];
478 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
479 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_VAR0
+ i
)) {
480 /* Note: varying results get put into the generic attributes */
481 VB
->AttribPtr
[VERT_ATTRIB_GENERIC0
+i
]
482 = &store
->results
[VERT_RESULT_VAR0
+ i
];
487 /* Perform NDC and cliptest operations:
489 return do_ndc_cliptest(ctx
, store
);
494 * Called the first time stage->run is called. In effect, don't
495 * allocate data until the first time the stage is run.
498 init_vp(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
500 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
501 struct vertex_buffer
*VB
= &(tnl
->vb
);
502 struct vp_stage_data
*store
;
503 const GLuint size
= VB
->Size
;
506 stage
->privatePtr
= CALLOC(sizeof(*store
));
507 store
= VP_STAGE_DATA(stage
);
511 /* Allocate arrays of vertex output values */
512 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
513 _mesa_vector4f_alloc( &store
->results
[i
], 0, size
, 32 );
514 store
->results
[i
].size
= 4;
517 /* a few other misc allocations */
518 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
519 store
->clipmask
= (GLubyte
*) _mesa_align_malloc(sizeof(GLubyte
)*size
, 32 );
526 * Destructor for this pipeline stage.
529 dtr(struct tnl_pipeline_stage
*stage
)
531 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
536 /* free the vertex program result arrays */
537 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
538 _mesa_vector4f_free( &store
->results
[i
] );
540 /* free misc arrays */
541 _mesa_vector4f_free( &store
->ndcCoords
);
542 _mesa_align_free( store
->clipmask
);
545 stage
->privatePtr
= NULL
;
551 validate_vp_stage(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
553 if (ctx
->VertexProgram
._Current
) {
554 _swrast_update_texture_samplers(ctx
);
561 * Public description of this pipeline stage.
563 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
566 NULL
, /* private_data */
567 init_vp
, /* create */
569 validate_vp_stage
, /* validate */
570 run_vp
/* run -- initially set to ctr */