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"
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
[VARYING_SLOT_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
, _mesa_get_samplerobj(ctx
, unit
),
201 ctx
->Texture
.Unit
[unit
]._Current
,
202 1, (const GLfloat (*)[4]) texcoord
,
203 &lambda
, (GLfloat (*)[4]) color
);
208 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
209 * string has been parsed.
212 _tnl_program_string(struct gl_context
*ctx
, GLenum target
, struct gl_program
*program
)
215 * If we had derived anything from the program that was private to this
216 * stage we'd recompute/validate it here.
223 * Initialize virtual machine state prior to executing vertex program.
226 init_machine(struct gl_context
*ctx
, struct gl_program_machine
*machine
,
229 /* Input registers get initialized from the current vertex attribs */
230 memcpy(machine
->VertAttribs
, ctx
->Current
.Attrib
,
231 MAX_VERTEX_GENERIC_ATTRIBS
* 4 * sizeof(GLfloat
));
233 machine
->NumDeriv
= 0;
235 /* init call stack */
236 machine
->StackDepth
= 0;
238 machine
->FetchTexelLod
= vp_fetch_texel
;
239 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
241 machine
->Samplers
= ctx
->VertexProgram
._Current
->Base
.SamplerUnits
;
243 machine
->SystemValues
[SYSTEM_VALUE_INSTANCE_ID
][0] = (GLfloat
) instID
;
248 * Map the texture images which the vertex program will access (if any).
251 map_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
255 for (u
= 0; u
< ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
; u
++) {
256 if (vp
->Base
.TexturesUsed
[u
]) {
257 /* Note: _Current *should* correspond to the target indicated
258 * in TexturesUsed[u].
260 _swrast_map_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
267 * Unmap the texture images which were used by the vertex program (if any).
270 unmap_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
274 for (u
= 0; u
< ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
; u
++) {
275 if (vp
->Base
.TexturesUsed
[u
]) {
276 /* Note: _Current *should* correspond to the target indicated
277 * in TexturesUsed[u].
279 _swrast_unmap_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
286 * This function executes vertex programs
289 run_vp( struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
291 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
292 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
293 struct vertex_buffer
*VB
= &tnl
->vb
;
294 struct gl_vertex_program
*program
= ctx
->VertexProgram
._Current
;
295 struct gl_program_machine
*machine
= &store
->machine
;
296 GLuint outputs
[VARYING_SLOT_MAX
], numOutputs
;
302 /* ARB program or vertex shader */
303 _mesa_load_state_parameters(ctx
, program
->Base
.Parameters
);
305 /* make list of outputs to save some time below */
307 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
308 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(i
)) {
309 outputs
[numOutputs
++] = i
;
313 /* Allocate result vectors. We delay this until now to avoid allocating
314 * memory that would never be used if we don't run the software tnl pipeline.
316 if (!store
->results
[0].storage
) {
317 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
318 assert(!store
->results
[i
].storage
);
319 _mesa_vector4f_alloc( &store
->results
[i
], 0, VB
->Size
, 32 );
320 store
->results
[i
].size
= 4;
324 map_textures(ctx
, program
);
326 for (i
= 0; i
< VB
->Count
; i
++) {
329 init_machine(ctx
, machine
, tnl
->CurInstance
);
332 printf("Input %d: %f, %f, %f, %f\n", i
,
333 VB
->AttribPtr
[0]->data
[i
][0],
334 VB
->AttribPtr
[0]->data
[i
][1],
335 VB
->AttribPtr
[0]->data
[i
][2],
336 VB
->AttribPtr
[0]->data
[i
][3]);
337 printf(" color: %f, %f, %f, %f\n",
338 VB
->AttribPtr
[3]->data
[i
][0],
339 VB
->AttribPtr
[3]->data
[i
][1],
340 VB
->AttribPtr
[3]->data
[i
][2],
341 VB
->AttribPtr
[3]->data
[i
][3]);
342 printf(" normal: %f, %f, %f, %f\n",
343 VB
->AttribPtr
[2]->data
[i
][0],
344 VB
->AttribPtr
[2]->data
[i
][1],
345 VB
->AttribPtr
[2]->data
[i
][2],
346 VB
->AttribPtr
[2]->data
[i
][3]);
349 /* the vertex array case */
350 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
351 if (program
->Base
.InputsRead
& BITFIELD64_BIT(attr
)) {
352 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
353 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
354 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
355 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
357 check_float(data
[0]);
358 check_float(data
[1]);
359 check_float(data
[2]);
360 check_float(data
[3]);
362 COPY_CLEAN_4V(machine
->VertAttribs
[attr
], size
, data
);
366 /* execute the program */
367 _mesa_execute_program(ctx
, &program
->Base
, machine
);
369 /* copy the output registers into the VB->attribs arrays */
370 for (j
= 0; j
< numOutputs
; j
++) {
371 const GLuint attr
= outputs
[j
];
373 check_float(machine
->Outputs
[attr
][0]);
374 check_float(machine
->Outputs
[attr
][1]);
375 check_float(machine
->Outputs
[attr
][2]);
376 check_float(machine
->Outputs
[attr
][3]);
378 COPY_4V(store
->results
[attr
].data
[i
], machine
->Outputs
[attr
]);
381 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
382 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VARYING_SLOT_FOGC
)) {
383 store
->results
[VARYING_SLOT_FOGC
].data
[i
][1] = 0.0;
384 store
->results
[VARYING_SLOT_FOGC
].data
[i
][2] = 0.0;
385 store
->results
[VARYING_SLOT_FOGC
].data
[i
][3] = 1.0;
388 assert(machine
->Outputs
[0][3] != 0.0F
);
391 printf("HPOS: %f %f %f %f\n",
392 machine
->Outputs
[0][0],
393 machine
->Outputs
[0][1],
394 machine
->Outputs
[0][2],
395 machine
->Outputs
[0][3]);
399 unmap_textures(ctx
, program
);
401 if (program
->IsPositionInvariant
) {
402 /* We need the exact same transform as in the fixed function path here
403 * to guarantee invariance, depending on compiler optimization flags
404 * results could be different otherwise.
406 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
407 &ctx
->_ModelProjectMatrix
,
410 /* Drivers expect this to be clean to element 4...
412 switch (VB
->ClipPtr
->size
) {
416 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
419 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
426 /* Setup the VB pointers so that the next pipeline stages get
427 * their data from the right place (the program output arrays).
429 VB
->ClipPtr
= &store
->results
[VARYING_SLOT_POS
];
430 VB
->ClipPtr
->size
= 4;
431 VB
->ClipPtr
->count
= VB
->Count
;
434 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = &store
->results
[VARYING_SLOT_COL0
];
435 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = &store
->results
[VARYING_SLOT_COL1
];
436 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = &store
->results
[VARYING_SLOT_FOGC
];
437 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->results
[VARYING_SLOT_PSIZ
];
438 VB
->BackfaceColorPtr
= &store
->results
[VARYING_SLOT_BFC0
];
439 VB
->BackfaceSecondaryColorPtr
= &store
->results
[VARYING_SLOT_BFC1
];
441 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
442 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
443 = &store
->results
[VARYING_SLOT_TEX0
+ i
];
446 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
447 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VARYING_SLOT_VAR0
+ i
)) {
448 /* Note: varying results get put into the generic attributes */
449 VB
->AttribPtr
[VERT_ATTRIB_GENERIC0
+i
]
450 = &store
->results
[VARYING_SLOT_VAR0
+ i
];
455 /* Perform NDC and cliptest operations:
457 return do_ndc_cliptest(ctx
, store
);
462 * Called the first time stage->run is called. In effect, don't
463 * allocate data until the first time the stage is run.
466 init_vp(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
468 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
469 struct vertex_buffer
*VB
= &(tnl
->vb
);
470 struct vp_stage_data
*store
;
471 const GLuint size
= VB
->Size
;
473 stage
->privatePtr
= calloc(1, sizeof(*store
));
474 store
= VP_STAGE_DATA(stage
);
478 /* a few other misc allocations */
479 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
480 store
->clipmask
= _mesa_align_malloc(sizeof(GLubyte
)*size
, 32 );
487 * Destructor for this pipeline stage.
490 dtr(struct tnl_pipeline_stage
*stage
)
492 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
497 /* free the vertex program result arrays */
498 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++)
499 _mesa_vector4f_free( &store
->results
[i
] );
501 /* free misc arrays */
502 _mesa_vector4f_free( &store
->ndcCoords
);
503 _mesa_align_free( store
->clipmask
);
506 stage
->privatePtr
= NULL
;
512 validate_vp_stage(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
514 if (ctx
->VertexProgram
._Current
) {
515 _swrast_update_texture_samplers(ctx
);
522 * Public description of this pipeline stage.
524 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
527 NULL
, /* private_data */
528 init_vp
, /* create */
530 validate_vp_stage
, /* validate */
531 run_vp
/* run -- initially set to ctr */