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/context.h"
37 #include "main/macros.h"
38 #include "main/imports.h"
39 #include "shader/prog_instruction.h"
40 #include "shader/prog_statevars.h"
41 #include "shader/prog_execute.h"
42 #include "swrast/s_context.h"
43 #include "swrast/s_texfilter.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 */
74 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
78 userclip( GLcontext
*ctx
,
82 GLubyte
*clipandmask
)
86 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
87 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
89 const GLfloat a
= ctx
->Transform
._ClipUserPlane
[p
][0];
90 const GLfloat b
= ctx
->Transform
._ClipUserPlane
[p
][1];
91 const GLfloat c
= ctx
->Transform
._ClipUserPlane
[p
][2];
92 const GLfloat d
= ctx
->Transform
._ClipUserPlane
[p
][3];
93 GLfloat
*coord
= (GLfloat
*)clip
->data
;
94 GLuint stride
= clip
->stride
;
95 GLuint count
= clip
->count
;
97 for (nr
= 0, i
= 0 ; i
< count
; i
++) {
98 GLfloat dp
= (coord
[0] * a
+
105 clipmask
[i
] |= CLIP_USER_BIT
;
108 STRIDE_F(coord
, stride
);
112 *clipormask
|= CLIP_USER_BIT
;
114 *clipandmask
|= CLIP_USER_BIT
;
124 do_ndc_cliptest(GLcontext
*ctx
, struct vp_stage_data
*store
)
126 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
127 struct vertex_buffer
*VB
= &tnl
->vb
;
128 /* Cliptest and perspective divide. Clip functions must clear
132 store
->andmask
= CLIP_FRUSTUM_BITS
;
134 tnl_clip_prepare(ctx
);
136 if (tnl
->NeedNdcCoords
) {
138 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
143 !ctx
->Transform
.DepthClamp
);
147 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
152 !ctx
->Transform
.DepthClamp
);
155 if (store
->andmask
) {
156 /* All vertices are outside the frustum */
160 /* Test userclip planes. This contributes to VB->ClipMask.
162 /** XXX NEW_SLANG _Enabled ??? */
163 if (ctx
->Transform
.ClipPlanesEnabled
&& (!ctx
->VertexProgram
._Enabled
||
164 ctx
->VertexProgram
.Current
->IsPositionInvariant
)) {
171 if (store
->andmask
) {
176 VB
->ClipAndMask
= store
->andmask
;
177 VB
->ClipOrMask
= store
->ormask
;
178 VB
->ClipMask
= store
->clipmask
;
185 * XXX the texture sampling code in this module is a bit of a hack.
186 * The texture sampling code is in swrast, though it doesn't have any
187 * real dependencies on the rest of swrast. It should probably be
188 * moved into main/ someday.
191 vp_fetch_texel(GLcontext
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
192 GLuint unit
, GLfloat color
[4])
194 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
196 /* XXX use a float-valued TextureSample routine here!!! */
197 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
198 1, (const GLfloat (*)[4]) texcoord
,
199 &lambda
, (GLfloat (*)[4]) color
);
204 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
205 * string has been parsed.
208 _tnl_program_string(GLcontext
*ctx
, GLenum target
, struct gl_program
*program
)
211 * If we had derived anything from the program that was private to this
212 * stage we'd recompute/validate it here.
218 * Initialize virtual machine state prior to executing vertex program.
221 init_machine(GLcontext
*ctx
, struct gl_program_machine
*machine
)
223 /* Input registers get initialized from the current vertex attribs */
224 MEMCPY(machine
->VertAttribs
, ctx
->Current
.Attrib
,
225 MAX_VERTEX_GENERIC_ATTRIBS
* 4 * sizeof(GLfloat
));
227 if (ctx
->VertexProgram
._Current
->IsNVProgram
) {
229 /* Output/result regs are initialized to [0,0,0,1] */
230 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_OUTPUTS
; i
++) {
231 ASSIGN_4V(machine
->Outputs
[i
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
233 /* Temp regs are initialized to [0,0,0,0] */
234 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_TEMPS
; i
++) {
235 ASSIGN_4V(machine
->Temporaries
[i
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
237 for (i
= 0; i
< MAX_VERTEX_PROGRAM_ADDRESS_REGS
; i
++) {
238 ASSIGN_4V(machine
->AddressReg
[i
], 0, 0, 0, 0);
242 machine
->NumDeriv
= 0;
244 /* init condition codes */
245 machine
->CondCodes
[0] = COND_EQ
;
246 machine
->CondCodes
[1] = COND_EQ
;
247 machine
->CondCodes
[2] = COND_EQ
;
248 machine
->CondCodes
[3] = COND_EQ
;
250 /* init call stack */
251 machine
->StackDepth
= 0;
253 machine
->FetchTexelLod
= vp_fetch_texel
;
254 machine
->FetchTexelDeriv
= NULL
; /* not used by vertex programs */
256 machine
->Samplers
= ctx
->VertexProgram
._Current
->Base
.SamplerUnits
;
261 * Map the texture images which the vertex program will access (if any).
264 map_textures(GLcontext
*ctx
, const struct gl_vertex_program
*vp
)
268 if (!ctx
->Driver
.MapTexture
)
271 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
272 if (vp
->Base
.TexturesUsed
[u
]) {
273 /* Note: _Current *should* correspond to the target indicated
274 * in TexturesUsed[u].
276 ctx
->Driver
.MapTexture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
283 * Unmap the texture images which were used by the vertex program (if any).
286 unmap_textures(GLcontext
*ctx
, const struct gl_vertex_program
*vp
)
290 if (!ctx
->Driver
.MapTexture
)
293 for (u
= 0; u
< ctx
->Const
.MaxVertexTextureImageUnits
; u
++) {
294 if (vp
->Base
.TexturesUsed
[u
]) {
295 /* Note: _Current *should* correspond to the target indicated
296 * in TexturesUsed[u].
298 ctx
->Driver
.UnmapTexture(ctx
, ctx
->Texture
.Unit
[u
]._Current
);
305 * This function executes vertex programs
308 run_vp( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
310 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
311 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
312 struct vertex_buffer
*VB
= &tnl
->vb
;
313 struct gl_vertex_program
*program
= ctx
->VertexProgram
._Current
;
314 struct gl_program_machine machine
;
315 GLuint outputs
[VERT_RESULT_MAX
], numOutputs
;
321 if (program
->IsNVProgram
) {
322 _mesa_load_tracked_matrices(ctx
);
325 /* ARB program or vertex shader */
326 _mesa_load_state_parameters(ctx
, program
->Base
.Parameters
);
329 /* make list of outputs to save some time below */
331 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
332 if (program
->Base
.OutputsWritten
& (1 << i
)) {
333 outputs
[numOutputs
++] = i
;
337 map_textures(ctx
, program
);
339 for (i
= 0; i
< VB
->Count
; i
++) {
342 init_machine(ctx
, &machine
);
345 printf("Input %d: %f, %f, %f, %f\n", i
,
346 VB
->AttribPtr
[0]->data
[i
][0],
347 VB
->AttribPtr
[0]->data
[i
][1],
348 VB
->AttribPtr
[0]->data
[i
][2],
349 VB
->AttribPtr
[0]->data
[i
][3]);
350 printf(" color: %f, %f, %f, %f\n",
351 VB
->AttribPtr
[3]->data
[i
][0],
352 VB
->AttribPtr
[3]->data
[i
][1],
353 VB
->AttribPtr
[3]->data
[i
][2],
354 VB
->AttribPtr
[3]->data
[i
][3]);
355 printf(" normal: %f, %f, %f, %f\n",
356 VB
->AttribPtr
[2]->data
[i
][0],
357 VB
->AttribPtr
[2]->data
[i
][1],
358 VB
->AttribPtr
[2]->data
[i
][2],
359 VB
->AttribPtr
[2]->data
[i
][3]);
362 /* the vertex array case */
363 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
364 if (program
->Base
.InputsRead
& (1 << attr
)) {
365 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
366 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
367 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
368 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
370 check_float(data
[0]);
371 check_float(data
[1]);
372 check_float(data
[2]);
373 check_float(data
[3]);
375 COPY_CLEAN_4V(machine
.VertAttribs
[attr
], size
, data
);
379 /* execute the program */
380 _mesa_execute_program(ctx
, &program
->Base
, &machine
);
382 /* copy the output registers into the VB->attribs arrays */
383 for (j
= 0; j
< numOutputs
; j
++) {
384 const GLuint attr
= outputs
[j
];
386 check_float(machine
.Outputs
[attr
][0]);
387 check_float(machine
.Outputs
[attr
][1]);
388 check_float(machine
.Outputs
[attr
][2]);
389 check_float(machine
.Outputs
[attr
][3]);
391 COPY_4V(store
->results
[attr
].data
[i
], machine
.Outputs
[attr
]);
394 ASSERT(machine
.Outputs
[0][3] != 0.0F
);
397 printf("HPOS: %f %f %f %f\n",
398 machine
.Outputs
[0][0],
399 machine
.Outputs
[0][1],
400 machine
.Outputs
[0][2],
401 machine
.Outputs
[0][3]);
405 unmap_textures(ctx
, program
);
407 /* Fixup fog and point size results if needed */
408 if (program
->IsNVProgram
) {
409 if (ctx
->Fog
.Enabled
&&
410 (program
->Base
.OutputsWritten
& (1 << VERT_RESULT_FOGC
)) == 0) {
411 for (i
= 0; i
< VB
->Count
; i
++) {
412 store
->results
[VERT_RESULT_FOGC
].data
[i
][0] = 1.0;
416 if (ctx
->VertexProgram
.PointSizeEnabled
&&
417 (program
->Base
.OutputsWritten
& (1 << VERT_RESULT_PSIZ
)) == 0) {
418 for (i
= 0; i
< VB
->Count
; i
++) {
419 store
->results
[VERT_RESULT_PSIZ
].data
[i
][0] = ctx
->Point
.Size
;
424 if (program
->IsPositionInvariant
) {
425 /* We need the exact same transform as in the fixed function path here
426 * to guarantee invariance, depending on compiler optimization flags
427 * results could be different otherwise.
429 VB
->ClipPtr
= TransformRaw( &store
->results
[0],
430 &ctx
->_ModelProjectMatrix
,
433 /* Drivers expect this to be clean to element 4...
435 switch (VB
->ClipPtr
->size
) {
439 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
442 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
449 /* Setup the VB pointers so that the next pipeline stages get
450 * their data from the right place (the program output arrays).
452 VB
->ClipPtr
= &store
->results
[VERT_RESULT_HPOS
];
453 VB
->ClipPtr
->size
= 4;
454 VB
->ClipPtr
->count
= VB
->Count
;
457 VB
->ColorPtr
[0] = &store
->results
[VERT_RESULT_COL0
];
458 VB
->ColorPtr
[1] = &store
->results
[VERT_RESULT_BFC0
];
459 VB
->SecondaryColorPtr
[0] = &store
->results
[VERT_RESULT_COL1
];
460 VB
->SecondaryColorPtr
[1] = &store
->results
[VERT_RESULT_BFC1
];
461 VB
->FogCoordPtr
= &store
->results
[VERT_RESULT_FOGC
];
463 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = &store
->results
[VERT_RESULT_COL0
];
464 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = &store
->results
[VERT_RESULT_COL1
];
465 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = &store
->results
[VERT_RESULT_FOGC
];
466 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->results
[VERT_RESULT_PSIZ
];
468 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
470 VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
]
471 = &store
->results
[VERT_RESULT_TEX0
+ i
];
474 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
475 if (program
->Base
.OutputsWritten
& (1 << (VERT_RESULT_VAR0
+ i
))) {
476 /* Note: varying results get put into the generic attributes */
477 VB
->AttribPtr
[VERT_ATTRIB_GENERIC0
+i
]
478 = &store
->results
[VERT_RESULT_VAR0
+ i
];
483 /* Perform NDC and cliptest operations:
485 return do_ndc_cliptest(ctx
, store
);
490 * Called the first time stage->run is called. In effect, don't
491 * allocate data until the first time the stage is run.
494 init_vp(GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
496 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
497 struct vertex_buffer
*VB
= &(tnl
->vb
);
498 struct vp_stage_data
*store
;
499 const GLuint size
= VB
->Size
;
502 stage
->privatePtr
= MALLOC(sizeof(*store
));
503 store
= VP_STAGE_DATA(stage
);
507 /* Allocate arrays of vertex output values */
508 for (i
= 0; i
< VERT_RESULT_MAX
; i
++) {
509 _mesa_vector4f_alloc( &store
->results
[i
], 0, size
, 32 );
510 store
->results
[i
].size
= 4;
513 /* a few other misc allocations */
514 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
515 store
->clipmask
= (GLubyte
*) ALIGN_MALLOC(sizeof(GLubyte
)*size
, 32 );
522 * Destructor for this pipeline stage.
525 dtr(struct tnl_pipeline_stage
*stage
)
527 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
532 /* free the vertex program result arrays */
533 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
534 _mesa_vector4f_free( &store
->results
[i
] );
536 /* free misc arrays */
537 _mesa_vector4f_free( &store
->ndcCoords
);
538 ALIGN_FREE( store
->clipmask
);
541 stage
->privatePtr
= NULL
;
547 validate_vp_stage(GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
549 if (ctx
->VertexProgram
._Current
) {
550 _swrast_update_texture_samplers(ctx
);
557 * Public description of this pipeline stage.
559 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
562 NULL
, /* private_data */
563 init_vp
, /* create */
565 validate_vp_stage
, /* validate */
566 run_vp
/* run -- initially set to ctr */