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 "main/samplerobj.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"
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
[VERT_RESULT_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
)
90 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
91 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
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
;
128 do_ndc_cliptest(struct gl_context
*ctx
, struct vp_stage_data
*store
)
130 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
131 struct vertex_buffer
*VB
= &tnl
->vb
;
132 /* Cliptest and perspective divide. Clip functions must clear
136 store
->andmask
= CLIP_FRUSTUM_BITS
;
138 tnl_clip_prepare(ctx
);
140 if (tnl
->NeedNdcCoords
) {
142 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
147 !ctx
->Transform
.DepthClamp
);
151 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
156 !ctx
->Transform
.DepthClamp
);
159 if (store
->andmask
) {
160 /* All vertices are outside the frustum */
164 /* Test userclip planes. This contributes to VB->ClipMask.
166 /** XXX NEW_SLANG _Enabled ??? */
167 if (ctx
->Transform
.ClipPlanesEnabled
&& (!ctx
->VertexProgram
._Enabled
||
168 ctx
->VertexProgram
.Current
->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 condition codes */
237 machine
->CondCodes
[0] = COND_EQ
;
238 machine
->CondCodes
[1] = COND_EQ
;
239 machine
->CondCodes
[2] = COND_EQ
;
240 machine
->CondCodes
[3] = COND_EQ
;
242 /* init call stack */
243 machine
->StackDepth
= 0;
245 machine
->FetchTexelLod
= vp_fetch_texel
;
246 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
248 machine
->Samplers
= ctx
->VertexProgram
._Current
->Base
.SamplerUnits
;
250 machine
->SystemValues
[SYSTEM_VALUE_INSTANCE_ID
][0] = (GLfloat
) instID
;
255 * Map the texture images which the vertex program will access (if any).
258 map_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
262 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
263 if (vp
->Base
.TexturesUsed
[u
]) {
264 /* Note: _Current *should* correspond to the target indicated
265 * in TexturesUsed[u].
267 _swrast_map_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
274 * Unmap the texture images which were used by the vertex program (if any).
277 unmap_textures(struct gl_context
*ctx
, const struct gl_vertex_program
*vp
)
281 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
282 if (vp
->Base
.TexturesUsed
[u
]) {
283 /* Note: _Current *should* correspond to the target indicated
284 * in TexturesUsed[u].
286 _swrast_unmap_texture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
293 * This function executes vertex programs
296 run_vp( struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
298 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
299 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
300 struct vertex_buffer
*VB
= &tnl
->vb
;
301 struct gl_vertex_program
*program
= ctx
->VertexProgram
._Current
;
302 struct gl_program_machine
*machine
= &store
->machine
;
303 GLuint outputs
[VERT_RESULT_MAX
], numOutputs
;
309 /* ARB program or vertex shader */
310 _mesa_load_state_parameters(ctx
, program
->Base
.Parameters
);
312 /* make list of outputs to save some time below */
314 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
315 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(i
)) {
316 outputs
[numOutputs
++] = i
;
320 /* Allocate result vectors. We delay this until now to avoid allocating
321 * memory that would never be used if we don't run the software tnl pipeline.
323 if (!store
->results
[0].storage
) {
324 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
325 assert(!store
->results
[i
].storage
);
326 _mesa_vector4f_alloc( &store
->results
[i
], 0, VB
->Size
, 32 );
327 store
->results
[i
].size
= 4;
331 map_textures(ctx
, program
);
333 for (i
= 0; i
< VB
->Count
; i
++) {
336 init_machine(ctx
, machine
, tnl
->CurInstance
);
339 printf("Input %d: %f, %f, %f, %f\n", i
,
340 VB
->AttribPtr
[0]->data
[i
][0],
341 VB
->AttribPtr
[0]->data
[i
][1],
342 VB
->AttribPtr
[0]->data
[i
][2],
343 VB
->AttribPtr
[0]->data
[i
][3]);
344 printf(" color: %f, %f, %f, %f\n",
345 VB
->AttribPtr
[3]->data
[i
][0],
346 VB
->AttribPtr
[3]->data
[i
][1],
347 VB
->AttribPtr
[3]->data
[i
][2],
348 VB
->AttribPtr
[3]->data
[i
][3]);
349 printf(" normal: %f, %f, %f, %f\n",
350 VB
->AttribPtr
[2]->data
[i
][0],
351 VB
->AttribPtr
[2]->data
[i
][1],
352 VB
->AttribPtr
[2]->data
[i
][2],
353 VB
->AttribPtr
[2]->data
[i
][3]);
356 /* the vertex array case */
357 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
358 if (program
->Base
.InputsRead
& BITFIELD64_BIT(attr
)) {
359 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
360 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
361 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
362 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
364 check_float(data
[0]);
365 check_float(data
[1]);
366 check_float(data
[2]);
367 check_float(data
[3]);
369 COPY_CLEAN_4V(machine
->VertAttribs
[attr
], size
, data
);
373 /* execute the program */
374 _mesa_execute_program(ctx
, &program
->Base
, machine
);
376 /* copy the output registers into the VB->attribs arrays */
377 for (j
= 0; j
< numOutputs
; j
++) {
378 const GLuint attr
= outputs
[j
];
380 check_float(machine
->Outputs
[attr
][0]);
381 check_float(machine
->Outputs
[attr
][1]);
382 check_float(machine
->Outputs
[attr
][2]);
383 check_float(machine
->Outputs
[attr
][3]);
385 COPY_4V(store
->results
[attr
].data
[i
], machine
->Outputs
[attr
]);
388 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */
389 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_FOGC
)) {
390 store
->results
[VERT_RESULT_FOGC
].data
[i
][1] = 0.0;
391 store
->results
[VERT_RESULT_FOGC
].data
[i
][2] = 0.0;
392 store
->results
[VERT_RESULT_FOGC
].data
[i
][3] = 1.0;
395 ASSERT(machine
->Outputs
[0][3] != 0.0F
);
398 printf("HPOS: %f %f %f %f\n",
399 machine
->Outputs
[0][0],
400 machine
->Outputs
[0][1],
401 machine
->Outputs
[0][2],
402 machine
->Outputs
[0][3]);
406 unmap_textures(ctx
, program
);
408 if (program
->IsPositionInvariant
) {
409 /* We need the exact same transform as in the fixed function path here
410 * to guarantee invariance, depending on compiler optimization flags
411 * results could be different otherwise.
413 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
414 &ctx
->_ModelProjectMatrix
,
417 /* Drivers expect this to be clean to element 4...
419 switch (VB
->ClipPtr
->size
) {
423 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
426 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
433 /* Setup the VB pointers so that the next pipeline stages get
434 * their data from the right place (the program output arrays).
436 VB
->ClipPtr
= &store
->results
[VERT_RESULT_HPOS
];
437 VB
->ClipPtr
->size
= 4;
438 VB
->ClipPtr
->count
= VB
->Count
;
441 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = &store
->results
[VERT_RESULT_COL0
];
442 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = &store
->results
[VERT_RESULT_COL1
];
443 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = &store
->results
[VERT_RESULT_FOGC
];
444 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->results
[VERT_RESULT_PSIZ
];
445 VB
->BackfaceColorPtr
= &store
->results
[VERT_RESULT_BFC0
];
446 VB
->BackfaceSecondaryColorPtr
= &store
->results
[VERT_RESULT_BFC1
];
448 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
449 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
450 = &store
->results
[VERT_RESULT_TEX0
+ i
];
453 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
454 if (program
->Base
.OutputsWritten
& BITFIELD64_BIT(VERT_RESULT_VAR0
+ i
)) {
455 /* Note: varying results get put into the generic attributes */
456 VB
->AttribPtr
[VERT_ATTRIB_GENERIC0
+i
]
457 = &store
->results
[VERT_RESULT_VAR0
+ i
];
462 /* Perform NDC and cliptest operations:
464 return do_ndc_cliptest(ctx
, store
);
469 * Called the first time stage->run is called. In effect, don't
470 * allocate data until the first time the stage is run.
473 init_vp(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
475 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
476 struct vertex_buffer
*VB
= &(tnl
->vb
);
477 struct vp_stage_data
*store
;
478 const GLuint size
= VB
->Size
;
480 stage
->privatePtr
= calloc(1, sizeof(*store
));
481 store
= VP_STAGE_DATA(stage
);
485 /* a few other misc allocations */
486 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
487 store
->clipmask
= _mesa_align_malloc(sizeof(GLubyte
)*size
, 32 );
494 * Destructor for this pipeline stage.
497 dtr(struct tnl_pipeline_stage
*stage
)
499 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
504 /* free the vertex program result arrays */
505 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
506 _mesa_vector4f_free( &store
->results
[i
] );
508 /* free misc arrays */
509 _mesa_vector4f_free( &store
->ndcCoords
);
510 _mesa_align_free( store
->clipmask
);
513 stage
->privatePtr
= NULL
;
519 validate_vp_stage(struct gl_context
*ctx
, struct tnl_pipeline_stage
*stage
)
521 if (ctx
->VertexProgram
._Current
) {
522 _swrast_update_texture_samplers(ctx
);
529 * Public description of this pipeline stage.
531 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
534 NULL
, /* private_data */
535 init_vp
, /* create */
537 validate_vp_stage
, /* validate */
538 run_vp
/* run -- initially set to ctr */