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 "main/state.h"
39 #include "math/m_xform.h"
40 #include "program/prog_instruction.h"
41 #include "program/prog_statevars.h"
42 #include "program/prog_execute.h"
43 #include "swrast/s_context.h"
44 #include "util/bitscan.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
)
89 GLbitfield mask
= ctx
->Transform
.ClipPlanesEnabled
;
91 const int p
= u_bit_scan(&mask
);
93 const GLfloat a
= ctx
->Transform
._ClipUserPlane
[p
][0];
94 const GLfloat b
= ctx
->Transform
._ClipUserPlane
[p
][1];
95 const GLfloat c
= ctx
->Transform
._ClipUserPlane
[p
][2];
96 const GLfloat d
= ctx
->Transform
._ClipUserPlane
[p
][3];
97 GLfloat
*coord
= (GLfloat
*)clip
->data
;
98 GLuint stride
= clip
->stride
;
99 GLuint count
= clip
->count
;
101 for (nr
= 0, i
= 0 ; i
< count
; i
++) {
102 GLfloat dp
= (coord
[0] * a
+
109 clipmask
[i
] |= CLIP_USER_BIT
;
112 STRIDE_F(coord
, stride
);
116 *clipormask
|= CLIP_USER_BIT
;
118 *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
&&
167 (!_mesa_arb_vertex_program_enabled(ctx
) ||
168 ctx
->VertexProgram
.Current
->arb
.IsPositionInvariant
)) {
175 if (store
->andmask
) {
180 VB
->ClipAndMask
= store
->andmask
;
181 VB
->ClipOrMask
= store
->ormask
;
182 VB
->ClipMask
= store
->clipmask
;
189 * XXX the texture sampling code in this module is a bit of a hack.
190 * The texture sampling code is in swrast, though it doesn't have any
191 * real dependencies on the rest of swrast. It should probably be
192 * moved into main/ someday.
195 vp_fetch_texel(struct gl_context
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
196 GLuint unit
, GLfloat color
[4])
198 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
200 /* XXX use a float-valued TextureSample routine here!!! */
201 swrast
->TextureSample
[unit
](ctx
, _mesa_get_samplerobj(ctx
, unit
),
202 ctx
->Texture
.Unit
[unit
]._Current
,
203 1, (const GLfloat (*)[4]) texcoord
,
204 &lambda
, (GLfloat (*)[4]) color
);
209 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
210 * string has been parsed.
213 _tnl_program_string(struct gl_context
*ctx
, GLenum target
, struct gl_program
*program
)
216 * If we had derived anything from the program that was private to this
217 * stage we'd recompute/validate it here.
224 * Initialize virtual machine state prior to executing vertex program.
227 init_machine(struct gl_context
*ctx
, struct gl_program_machine
*machine
,
230 /* Input registers get initialized from the current vertex attribs */
231 memcpy(machine
->VertAttribs
, ctx
->Current
.Attrib
,
232 MAX_VERTEX_GENERIC_ATTRIBS
* 4 * sizeof(GLfloat
));
234 machine
->NumDeriv
= 0;
236 /* init call stack */
237 machine
->StackDepth
= 0;
239 machine
->FetchTexelLod
= vp_fetch_texel
;
240 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
242 machine
->Samplers
= ctx
->VertexProgram
._Current
->SamplerUnits
;
244 machine
->SystemValues
[SYSTEM_VALUE_INSTANCE_ID
][0] = (GLfloat
) instID
;
249 * Map the texture images which the vertex program will access (if any).
252 map_textures(struct gl_context
*ctx
, const struct gl_program
*vp
)
256 for (u
= 0; u
< ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
; u
++) {
257 if (vp
->TexturesUsed
[u
]) {
258 /* Note: _Current *should* correspond to the target indicated
259 * in TexturesUsed[u].
261 _swrast_map_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
268 * Unmap the texture images which were used by the vertex program (if any).
271 unmap_textures(struct gl_context
*ctx
, const struct gl_program
*vp
)
275 for (u
= 0; u
< ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
; u
++) {
276 if (vp
->TexturesUsed
[u
]) {
277 /* Note: _Current *should* correspond to the target indicated
278 * in TexturesUsed[u].
280 _swrast_unmap_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
287 * This function executes vertex programs
290 run_vp( struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
292 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
293 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
294 struct vertex_buffer
*VB
= &tnl
->vb
;
295 struct gl_program
*program
= ctx
->VertexProgram
._Current
;
296 struct gl_program_machine
*machine
= &store
->machine
;
297 GLuint outputs
[VARYING_SLOT_MAX
], numOutputs
;
303 /* ARB program or vertex shader */
304 _mesa_load_state_parameters(ctx
, program
->Parameters
);
306 /* make list of outputs to save some time below */
308 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
309 if (program
->info
.outputs_written
& BITFIELD64_BIT(i
)) {
310 outputs
[numOutputs
++] = i
;
314 /* Allocate result vectors. We delay this until now to avoid allocating
315 * memory that would never be used if we don't run the software tnl pipeline.
317 if (!store
->results
[0].storage
) {
318 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
319 assert(!store
->results
[i
].storage
);
320 _mesa_vector4f_alloc( &store
->results
[i
], 0, VB
->Size
, 32 );
321 store
->results
[i
].size
= 4;
325 map_textures(ctx
, program
);
327 for (i
= 0; i
< VB
->Count
; i
++) {
330 init_machine(ctx
, machine
, tnl
->CurInstance
);
333 printf("Input %d: %f, %f, %f, %f\n", i
,
334 VB
->AttribPtr
[0]->data
[i
][0],
335 VB
->AttribPtr
[0]->data
[i
][1],
336 VB
->AttribPtr
[0]->data
[i
][2],
337 VB
->AttribPtr
[0]->data
[i
][3]);
338 printf(" color: %f, %f, %f, %f\n",
339 VB
->AttribPtr
[3]->data
[i
][0],
340 VB
->AttribPtr
[3]->data
[i
][1],
341 VB
->AttribPtr
[3]->data
[i
][2],
342 VB
->AttribPtr
[3]->data
[i
][3]);
343 printf(" normal: %f, %f, %f, %f\n",
344 VB
->AttribPtr
[2]->data
[i
][0],
345 VB
->AttribPtr
[2]->data
[i
][1],
346 VB
->AttribPtr
[2]->data
[i
][2],
347 VB
->AttribPtr
[2]->data
[i
][3]);
350 /* the vertex array case */
351 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
352 if (program
->info
.inputs_read
& BITFIELD64_BIT(attr
)) {
353 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
354 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
355 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
356 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
358 check_float(data
[0]);
359 check_float(data
[1]);
360 check_float(data
[2]);
361 check_float(data
[3]);
363 COPY_CLEAN_4V(machine
->VertAttribs
[attr
], size
, data
);
367 /* execute the program */
368 _mesa_execute_program(ctx
, program
, machine
);
370 /* copy the output registers into the VB->attribs arrays */
371 for (j
= 0; j
< numOutputs
; j
++) {
372 const GLuint attr
= outputs
[j
];
374 check_float(machine
->Outputs
[attr
][0]);
375 check_float(machine
->Outputs
[attr
][1]);
376 check_float(machine
->Outputs
[attr
][2]);
377 check_float(machine
->Outputs
[attr
][3]);
379 COPY_4V(store
->results
[attr
].data
[i
], machine
->Outputs
[attr
]);
382 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
383 if (program
->info
.outputs_written
& BITFIELD64_BIT(VARYING_SLOT_FOGC
)) {
384 store
->results
[VARYING_SLOT_FOGC
].data
[i
][1] = 0.0;
385 store
->results
[VARYING_SLOT_FOGC
].data
[i
][2] = 0.0;
386 store
->results
[VARYING_SLOT_FOGC
].data
[i
][3] = 1.0;
389 assert(machine
->Outputs
[0][3] != 0.0F
);
392 printf("HPOS: %f %f %f %f\n",
393 machine
->Outputs
[0][0],
394 machine
->Outputs
[0][1],
395 machine
->Outputs
[0][2],
396 machine
->Outputs
[0][3]);
400 unmap_textures(ctx
, program
);
402 if (program
->arb
.IsPositionInvariant
) {
403 /* We need the exact same transform as in the fixed function path here
404 * to guarantee invariance, depending on compiler optimization flags
405 * results could be different otherwise.
407 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
408 &ctx
->_ModelProjectMatrix
,
411 /* Drivers expect this to be clean to element 4...
413 switch (VB
->ClipPtr
->size
) {
417 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
420 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
427 /* Setup the VB pointers so that the next pipeline stages get
428 * their data from the right place (the program output arrays).
430 VB
->ClipPtr
= &store
->results
[VARYING_SLOT_POS
];
431 VB
->ClipPtr
->size
= 4;
432 VB
->ClipPtr
->count
= VB
->Count
;
435 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = &store
->results
[VARYING_SLOT_COL0
];
436 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = &store
->results
[VARYING_SLOT_COL1
];
437 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = &store
->results
[VARYING_SLOT_FOGC
];
438 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->results
[VARYING_SLOT_PSIZ
];
439 VB
->BackfaceColorPtr
= &store
->results
[VARYING_SLOT_BFC0
];
440 VB
->BackfaceSecondaryColorPtr
= &store
->results
[VARYING_SLOT_BFC1
];
442 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
443 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
444 = &store
->results
[VARYING_SLOT_TEX0
+ i
];
447 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
448 if (program
->info
.outputs_written
&
449 BITFIELD64_BIT(VARYING_SLOT_VAR0
+ i
)) {
450 /* Note: varying results get put into the generic attributes */
451 VB
->AttribPtr
[VERT_ATTRIB_GENERIC0
+i
]
452 = &store
->results
[VARYING_SLOT_VAR0
+ i
];
457 /* Perform NDC and cliptest operations:
459 return do_ndc_cliptest(ctx
, store
);
464 * Called the first time stage->run is called. In effect, don't
465 * allocate data until the first time the stage is run.
468 init_vp(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
470 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
471 struct vertex_buffer
*VB
= &(tnl
->vb
);
472 struct vp_stage_data
*store
;
473 const GLuint size
= VB
->Size
;
475 stage
->privatePtr
= calloc(1, sizeof(*store
));
476 store
= VP_STAGE_DATA(stage
);
480 /* a few other misc allocations */
481 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
482 store
->clipmask
= _mesa_align_malloc(sizeof(GLubyte
)*size
, 32 );
489 * Destructor for this pipeline stage.
492 dtr(struct tnl_pipeline_stage
*stage
)
494 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
499 /* free the vertex program result arrays */
500 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++)
501 _mesa_vector4f_free( &store
->results
[i
] );
503 /* free misc arrays */
504 _mesa_vector4f_free( &store
->ndcCoords
);
505 _mesa_align_free( store
->clipmask
);
508 stage
->privatePtr
= NULL
;
514 validate_vp_stage(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
516 if (ctx
->VertexProgram
._Current
) {
517 _swrast_update_texture_samplers(ctx
);
524 * Public description of this pipeline stage.
526 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
529 NULL
, /* private_data */
530 init_vp
, /* create */
532 validate_vp_stage
, /* validate */
533 run_vp
/* run -- initially set to ctr */