2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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/macros.h"
36 #include "main/imports.h"
37 #include "main/samplerobj.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 #include "util/bitscan.h"
46 #include "tnl/t_context.h"
47 #include "tnl/t_pipeline.h"
51 /** Check for NaNs and very large values */
55 assert(!IS_INF_OR_NAN(x
));
56 assert(1.0e-15 <= x
&& x
<= 1.0e15
);
62 * Private storage for the vertex program pipeline stage.
64 struct vp_stage_data
{
65 /** The results of running the vertex program go into these arrays. */
66 GLvector4f results
[VARYING_SLOT_MAX
];
68 GLvector4f ndcCoords
; /**< normalized device coords */
69 GLubyte
*clipmask
; /**< clip flags */
70 GLubyte ormask
, andmask
; /**< for clipping */
72 GLboolean vertex_textures
;
74 struct gl_program_machine machine
;
78 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
82 userclip( struct gl_context
*ctx
,
86 GLubyte
*clipandmask
)
88 GLbitfield mask
= ctx
->Transform
.ClipPlanesEnabled
;
90 const int p
= u_bit_scan(&mask
);
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
;
126 do_ndc_cliptest(struct gl_context
*ctx
, struct vp_stage_data
*store
)
128 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
129 struct vertex_buffer
*VB
= &tnl
->vb
;
130 /* Cliptest and perspective divide. Clip functions must clear
134 store
->andmask
= CLIP_FRUSTUM_BITS
;
136 tnl_clip_prepare(ctx
);
138 if (tnl
->NeedNdcCoords
) {
140 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
145 !ctx
->Transform
.DepthClamp
);
149 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
154 !ctx
->Transform
.DepthClamp
);
157 if (store
->andmask
) {
158 /* All vertices are outside the frustum */
162 /* Test userclip planes. This contributes to VB->ClipMask.
164 /** XXX NEW_SLANG _Enabled ??? */
165 if (ctx
->Transform
.ClipPlanesEnabled
&& (!ctx
->VertexProgram
._Enabled
||
166 ctx
->VertexProgram
.Current
->IsPositionInvariant
)) {
173 if (store
->andmask
) {
178 VB
->ClipAndMask
= store
->andmask
;
179 VB
->ClipOrMask
= store
->ormask
;
180 VB
->ClipMask
= store
->clipmask
;
187 * XXX the texture sampling code in this module is a bit of a hack.
188 * The texture sampling code is in swrast, though it doesn't have any
189 * real dependencies on the rest of swrast. It should probably be
190 * moved into main/ someday.
193 vp_fetch_texel(struct gl_context
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
194 GLuint unit
, GLfloat color
[4])
196 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
198 /* XXX use a float-valued TextureSample routine here!!! */
199 swrast
->TextureSample
[unit
](ctx
, _mesa_get_samplerobj(ctx
, unit
),
200 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 machine
->NumDeriv
= 0;
234 /* init call stack */
235 machine
->StackDepth
= 0;
237 machine
->FetchTexelLod
= vp_fetch_texel
;
238 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
240 machine
->Samplers
= ctx
->VertexProgram
._Current
->SamplerUnits
;
242 machine
->SystemValues
[SYSTEM_VALUE_INSTANCE_ID
][0] = (GLfloat
) instID
;
247 * Map the texture images which the vertex program will access (if any).
250 map_textures(struct gl_context
*ctx
, const struct gl_program
*vp
)
254 for (u
= 0; u
< ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
; u
++) {
255 if (vp
->TexturesUsed
[u
]) {
256 /* Note: _Current *should* correspond to the target indicated
257 * in TexturesUsed[u].
259 _swrast_map_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
266 * Unmap the texture images which were used by the vertex program (if any).
269 unmap_textures(struct gl_context
*ctx
, const struct gl_program
*vp
)
273 for (u
= 0; u
< ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
; u
++) {
274 if (vp
->TexturesUsed
[u
]) {
275 /* Note: _Current *should* correspond to the target indicated
276 * in TexturesUsed[u].
278 _swrast_unmap_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
285 * This function executes vertex programs
288 run_vp( struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
290 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
291 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
292 struct vertex_buffer
*VB
= &tnl
->vb
;
293 struct gl_program
*program
= ctx
->VertexProgram
._Current
;
294 struct gl_program_machine
*machine
= &store
->machine
;
295 GLuint outputs
[VARYING_SLOT_MAX
], numOutputs
;
301 /* ARB program or vertex shader */
302 _mesa_load_state_parameters(ctx
, program
->Parameters
);
304 /* make list of outputs to save some time below */
306 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
307 if (program
->OutputsWritten
& BITFIELD64_BIT(i
)) {
308 outputs
[numOutputs
++] = i
;
312 /* Allocate result vectors. We delay this until now to avoid allocating
313 * memory that would never be used if we don't run the software tnl pipeline.
315 if (!store
->results
[0].storage
) {
316 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
317 assert(!store
->results
[i
].storage
);
318 _mesa_vector4f_alloc( &store
->results
[i
], 0, VB
->Size
, 32 );
319 store
->results
[i
].size
= 4;
323 map_textures(ctx
, program
);
325 for (i
= 0; i
< VB
->Count
; i
++) {
328 init_machine(ctx
, machine
, tnl
->CurInstance
);
331 printf("Input %d: %f, %f, %f, %f\n", i
,
332 VB
->AttribPtr
[0]->data
[i
][0],
333 VB
->AttribPtr
[0]->data
[i
][1],
334 VB
->AttribPtr
[0]->data
[i
][2],
335 VB
->AttribPtr
[0]->data
[i
][3]);
336 printf(" color: %f, %f, %f, %f\n",
337 VB
->AttribPtr
[3]->data
[i
][0],
338 VB
->AttribPtr
[3]->data
[i
][1],
339 VB
->AttribPtr
[3]->data
[i
][2],
340 VB
->AttribPtr
[3]->data
[i
][3]);
341 printf(" normal: %f, %f, %f, %f\n",
342 VB
->AttribPtr
[2]->data
[i
][0],
343 VB
->AttribPtr
[2]->data
[i
][1],
344 VB
->AttribPtr
[2]->data
[i
][2],
345 VB
->AttribPtr
[2]->data
[i
][3]);
348 /* the vertex array case */
349 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
350 if (program
->InputsRead
& BITFIELD64_BIT(attr
)) {
351 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
352 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
353 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
354 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
356 check_float(data
[0]);
357 check_float(data
[1]);
358 check_float(data
[2]);
359 check_float(data
[3]);
361 COPY_CLEAN_4V(machine
->VertAttribs
[attr
], size
, data
);
365 /* execute the program */
366 _mesa_execute_program(ctx
, program
, machine
);
368 /* copy the output registers into the VB->attribs arrays */
369 for (j
= 0; j
< numOutputs
; j
++) {
370 const GLuint attr
= outputs
[j
];
372 check_float(machine
->Outputs
[attr
][0]);
373 check_float(machine
->Outputs
[attr
][1]);
374 check_float(machine
->Outputs
[attr
][2]);
375 check_float(machine
->Outputs
[attr
][3]);
377 COPY_4V(store
->results
[attr
].data
[i
], machine
->Outputs
[attr
]);
380 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
381 if (program
->OutputsWritten
& BITFIELD64_BIT(VARYING_SLOT_FOGC
)) {
382 store
->results
[VARYING_SLOT_FOGC
].data
[i
][1] = 0.0;
383 store
->results
[VARYING_SLOT_FOGC
].data
[i
][2] = 0.0;
384 store
->results
[VARYING_SLOT_FOGC
].data
[i
][3] = 1.0;
387 assert(machine
->Outputs
[0][3] != 0.0F
);
390 printf("HPOS: %f %f %f %f\n",
391 machine
->Outputs
[0][0],
392 machine
->Outputs
[0][1],
393 machine
->Outputs
[0][2],
394 machine
->Outputs
[0][3]);
398 unmap_textures(ctx
, program
);
400 if (program
->IsPositionInvariant
) {
401 /* We need the exact same transform as in the fixed function path here
402 * to guarantee invariance, depending on compiler optimization flags
403 * results could be different otherwise.
405 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
406 &ctx
->_ModelProjectMatrix
,
409 /* Drivers expect this to be clean to element 4...
411 switch (VB
->ClipPtr
->size
) {
415 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
418 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
425 /* Setup the VB pointers so that the next pipeline stages get
426 * their data from the right place (the program output arrays).
428 VB
->ClipPtr
= &store
->results
[VARYING_SLOT_POS
];
429 VB
->ClipPtr
->size
= 4;
430 VB
->ClipPtr
->count
= VB
->Count
;
433 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = &store
->results
[VARYING_SLOT_COL0
];
434 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = &store
->results
[VARYING_SLOT_COL1
];
435 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = &store
->results
[VARYING_SLOT_FOGC
];
436 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->results
[VARYING_SLOT_PSIZ
];
437 VB
->BackfaceColorPtr
= &store
->results
[VARYING_SLOT_BFC0
];
438 VB
->BackfaceSecondaryColorPtr
= &store
->results
[VARYING_SLOT_BFC1
];
440 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
441 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
442 = &store
->results
[VARYING_SLOT_TEX0
+ i
];
445 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
446 if (program
->OutputsWritten
& BITFIELD64_BIT(VARYING_SLOT_VAR0
+ i
)) {
447 /* Note: varying results get put into the generic attributes */
448 VB
->AttribPtr
[VERT_ATTRIB_GENERIC0
+i
]
449 = &store
->results
[VARYING_SLOT_VAR0
+ i
];
454 /* Perform NDC and cliptest operations:
456 return do_ndc_cliptest(ctx
, store
);
461 * Called the first time stage->run is called. In effect, don't
462 * allocate data until the first time the stage is run.
465 init_vp(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
467 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
468 struct vertex_buffer
*VB
= &(tnl
->vb
);
469 struct vp_stage_data
*store
;
470 const GLuint size
= VB
->Size
;
472 stage
->privatePtr
= calloc(1, sizeof(*store
));
473 store
= VP_STAGE_DATA(stage
);
477 /* a few other misc allocations */
478 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
479 store
->clipmask
= _mesa_align_malloc(sizeof(GLubyte
)*size
, 32 );
486 * Destructor for this pipeline stage.
489 dtr(struct tnl_pipeline_stage
*stage
)
491 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
496 /* free the vertex program result arrays */
497 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++)
498 _mesa_vector4f_free( &store
->results
[i
] );
500 /* free misc arrays */
501 _mesa_vector4f_free( &store
->ndcCoords
);
502 _mesa_align_free( store
->clipmask
);
505 stage
->privatePtr
= NULL
;
511 validate_vp_stage(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
513 if (ctx
->VertexProgram
._Current
) {
514 _swrast_update_texture_samplers(ctx
);
521 * Public description of this pipeline stage.
523 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
526 NULL
, /* private_data */
527 init_vp
, /* create */
529 validate_vp_stage
, /* validate */
530 run_vp
/* run -- initially set to ctr */