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 * 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.
29 * \file tnl/t_vb_program.c
30 * \brief Pipeline stage for executing vertex programs.
31 * \author Brian Paul, Keith Whitwell
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"
47 #include "tnl/t_context.h"
48 #include "tnl/t_pipeline.h"
52 /** Check for NaNs and very large values */
56 assert(!IS_INF_OR_NAN(x
));
57 assert(1.0e-15 <= x
&& x
<= 1.0e15
);
63 * Private storage for the vertex program pipeline stage.
65 struct vp_stage_data
{
66 /** The results of running the vertex program go into these arrays. */
67 GLvector4f results
[VARYING_SLOT_MAX
];
69 GLvector4f ndcCoords
; /**< normalized device coords */
70 GLubyte
*clipmask
; /**< clip flags */
71 GLubyte ormask
, andmask
; /**< for clipping */
73 GLboolean vertex_textures
;
75 struct gl_program_machine machine
;
79 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
83 userclip( struct gl_context
*ctx
,
87 GLubyte
*clipandmask
)
91 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
92 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
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
;
102 for (nr
= 0, i
= 0 ; i
< count
; i
++) {
103 GLfloat dp
= (coord
[0] * a
+
110 clipmask
[i
] |= CLIP_USER_BIT
;
113 STRIDE_F(coord
, stride
);
117 *clipormask
|= CLIP_USER_BIT
;
119 *clipandmask
|= CLIP_USER_BIT
;
129 do_ndc_cliptest(struct gl_context
*ctx
, struct vp_stage_data
*store
)
131 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
132 struct vertex_buffer
*VB
= &tnl
->vb
;
133 /* Cliptest and perspective divide. Clip functions must clear
137 store
->andmask
= CLIP_FRUSTUM_BITS
;
139 tnl_clip_prepare(ctx
);
141 if (tnl
->NeedNdcCoords
) {
143 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
148 !ctx
->Transform
.DepthClamp
);
152 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
157 !ctx
->Transform
.DepthClamp
);
160 if (store
->andmask
) {
161 /* All vertices are outside the frustum */
165 /* Test userclip planes. This contributes to VB->ClipMask.
167 /** XXX NEW_SLANG _Enabled ??? */
168 if (ctx
->Transform
.ClipPlanesEnabled
&& (!ctx
->VertexProgram
._Enabled
||
169 ctx
->VertexProgram
.Current
->IsPositionInvariant
)) {
176 if (store
->andmask
) {
181 VB
->ClipAndMask
= store
->andmask
;
182 VB
->ClipOrMask
= store
->ormask
;
183 VB
->ClipMask
= store
->clipmask
;
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.
196 vp_fetch_texel(struct gl_context
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
197 GLuint unit
, GLfloat color
[4])
199 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
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
);
210 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
211 * string has been parsed.
214 _tnl_program_string(struct gl_context
*ctx
, GLenum target
, struct gl_program
*program
)
217 * If we had derived anything from the program that was private to this
218 * stage we'd recompute/validate it here.
225 * Initialize virtual machine state prior to executing vertex program.
228 init_machine(struct gl_context
*ctx
, struct gl_program_machine
*machine
,
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
));
235 machine
->NumDeriv
= 0;
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
;
243 /* init call stack */
244 machine
->StackDepth
= 0;
246 machine
->FetchTexelLod
= vp_fetch_texel
;
247 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
249 machine
->Samplers
= ctx
->VertexProgram
._Current
->Base
.SamplerUnits
;
251 machine
->SystemValues
[SYSTEM_VALUE_INSTANCE_ID
][0] = (GLfloat
) instID
;
256 * Map the texture images which the vertex program will access (if any).
259 map_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
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].
268 _swrast_map_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
275 * Unmap the texture images which were used by the vertex program (if any).
278 unmap_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
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].
287 _swrast_unmap_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
294 * This function executes vertex programs
297 run_vp( struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
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
;
310 /* ARB program or vertex shader */
311 _mesa_load_state_parameters(ctx
, program
->Base
.Parameters
);
313 /* make list of outputs to save some time below */
315 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
316 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(i
)) {
317 outputs
[numOutputs
++] = i
;
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.
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;
332 map_textures(ctx
, program
);
334 for (i
= 0; i
< VB
->Count
; i
++) {
337 init_machine(ctx
, machine
, tnl
->CurInstance
);
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]);
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
);
365 check_float(data
[0]);
366 check_float(data
[1]);
367 check_float(data
[2]);
368 check_float(data
[3]);
370 COPY_CLEAN_4V(machine
->VertAttribs
[attr
], size
, data
);
374 /* execute the program */
375 _mesa_execute_program(ctx
, &program
->Base
, machine
);
377 /* copy the output registers into the VB->attribs arrays */
378 for (j
= 0; j
< numOutputs
; j
++) {
379 const GLuint attr
= outputs
[j
];
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]);
386 COPY_4V(store
->results
[attr
].data
[i
], machine
->Outputs
[attr
]);
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;
396 ASSERT(machine
->Outputs
[0][3] != 0.0F
);
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]);
407 unmap_textures(ctx
, program
);
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.
414 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
415 &ctx
->_ModelProjectMatrix
,
418 /* Drivers expect this to be clean to element 4...
420 switch (VB
->ClipPtr
->size
) {
424 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
427 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
434 /* Setup the VB pointers so that the next pipeline stages get
435 * their data from the right place (the program output arrays).
437 VB
->ClipPtr
= &store
->results
[VARYING_SLOT_POS
];
438 VB
->ClipPtr
->size
= 4;
439 VB
->ClipPtr
->count
= VB
->Count
;
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
];
449 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
450 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
451 = &store
->results
[VARYING_SLOT_TEX0
+ i
];
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
];
463 /* Perform NDC and cliptest operations:
465 return do_ndc_cliptest(ctx
, store
);
470 * Called the first time stage->run is called. In effect, don't
471 * allocate data until the first time the stage is run.
474 init_vp(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
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
;
481 stage
->privatePtr
= calloc(1, sizeof(*store
));
482 store
= VP_STAGE_DATA(stage
);
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 );
495 * Destructor for this pipeline stage.
498 dtr(struct tnl_pipeline_stage
*stage
)
500 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
505 /* free the vertex program result arrays */
506 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++)
507 _mesa_vector4f_free( &store
->results
[i
] );
509 /* free misc arrays */
510 _mesa_vector4f_free( &store
->ndcCoords
);
511 _mesa_align_free( store
->clipmask
);
514 stage
->privatePtr
= NULL
;
520 validate_vp_stage(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
522 if (ctx
->VertexProgram
._Current
) {
523 _swrast_update_texture_samplers(ctx
);
530 * Public description of this pipeline stage.
532 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
535 NULL
, /* private_data */
536 init_vp
, /* create */
538 validate_vp_stage
, /* validate */
539 run_vp
/* run -- initially set to ctr */