2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * \file tnl/t_vb_program.c
28 * \brief Pipeline stage for executing NVIDIA vertex programs.
29 * \author Brian Paul, Keith Whitwell
42 #include "simple_list.h"
44 #include "nvvertprog.h"
45 #include "nvvertexec.h"
46 #include "nvprogram.h"
48 #include "math/m_translate.h"
50 #include "t_context.h"
51 #include "t_pipeline.h"
55 * \warning These values _MUST_ match the values in the OutputRegisters[]
56 * array in vpparse.c!!!
58 #define VERT_RESULT_HPOS 0
59 #define VERT_RESULT_COL0 1
60 #define VERT_RESULT_COL1 2
61 #define VERT_RESULT_BFC0 3
62 #define VERT_RESULT_BFC1 4
63 #define VERT_RESULT_FOGC 5
64 #define VERT_RESULT_PSIZ 6
65 #define VERT_RESULT_TEX0 7
66 #define VERT_RESULT_TEX1 8
67 #define VERT_RESULT_TEX2 9
68 #define VERT_RESULT_TEX3 10
69 #define VERT_RESULT_TEX4 11
70 #define VERT_RESULT_TEX5 12
71 #define VERT_RESULT_TEX6 13
72 #define VERT_RESULT_TEX7 14
76 * Private storage for the vertex program pipeline stage.
78 struct vp_stage_data
{
79 /** The results of running the vertex program go into these arrays. */
80 GLvector4f attribs
[15];
82 GLvector4f ndcCoords
; /**< normalized device coords */
83 GLubyte
*clipmask
; /**< clip flags */
84 GLubyte ormask
, andmask
; /**< for clipping */
88 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
92 * This function executes vertex programs
95 run_vp( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
97 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
98 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
99 struct vertex_buffer
*VB
= &tnl
->vb
;
100 struct vertex_program
*program
= ctx
->VertexProgram
.Current
;
103 /* load program parameter registers (they're read-only) */
104 _mesa_init_vp_per_primitive_registers(ctx
);
106 for (i
= 0; i
< VB
->Count
; i
++) {
109 _mesa_init_vp_per_vertex_registers(ctx
);
112 printf("Input %d: %f, %f, %f, %f\n", i
,
113 VB
->AttribPtr
[0]->data
[i
][0],
114 VB
->AttribPtr
[0]->data
[i
][1],
115 VB
->AttribPtr
[0]->data
[i
][2],
116 VB
->AttribPtr
[0]->data
[i
][3]);
117 printf(" color: %f, %f, %f, %f\n",
118 VB
->AttribPtr
[3]->data
[i
][0],
119 VB
->AttribPtr
[3]->data
[i
][1],
120 VB
->AttribPtr
[3]->data
[i
][2],
121 VB
->AttribPtr
[3]->data
[i
][3]);
122 printf(" normal: %f, %f, %f, %f\n",
123 VB
->AttribPtr
[2]->data
[i
][0],
124 VB
->AttribPtr
[2]->data
[i
][1],
125 VB
->AttribPtr
[2]->data
[i
][2],
126 VB
->AttribPtr
[2]->data
[i
][3]);
129 /* the vertex array case */
130 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
131 if (program
->InputsRead
& (1 << attr
)) {
132 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
133 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
134 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
135 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
136 ASSIGN_4V(ctx
->VertexProgram
.Inputs
[attr
], 0, 0, 0, 1);
137 COPY_SZ_4V(ctx
->VertexProgram
.Inputs
[attr
], size
, data
);
141 /* execute the program */
143 _mesa_exec_vertex_program(ctx
, program
);
145 /* Fixup fog an point size results if needed */
146 if (ctx
->Fog
.Enabled
&&
147 (program
->OutputsWritten
& (1 << VERT_RESULT_FOGC
)) == 0) {
148 ctx
->VertexProgram
.Outputs
[VERT_RESULT_FOGC
][0] = 1.0;
151 if (ctx
->VertexProgram
.PointSizeEnabled
&&
152 (program
->OutputsWritten
& (1 << VERT_RESULT_PSIZ
)) == 0) {
153 ctx
->VertexProgram
.Outputs
[VERT_RESULT_PSIZ
][0] = ctx
->Point
.Size
;
156 /* copy the output registers into the VB->attribs arrays */
157 /* XXX (optimize) could use a conditional and smaller loop limit here */
158 for (attr
= 0; attr
< 15; attr
++) {
159 COPY_4V(store
->attribs
[attr
].data
[i
],
160 ctx
->VertexProgram
.Outputs
[attr
]);
164 /* Setup the VB pointers so that the next pipeline stages get
165 * their data from the right place (the program output arrays).
167 VB
->ClipPtr
= &store
->attribs
[VERT_RESULT_HPOS
];
168 VB
->ClipPtr
->size
= 4;
169 VB
->ClipPtr
->count
= VB
->Count
;
170 VB
->ColorPtr
[0] = &store
->attribs
[VERT_RESULT_COL0
];
171 VB
->ColorPtr
[1] = &store
->attribs
[VERT_RESULT_BFC0
];
172 VB
->SecondaryColorPtr
[0] = &store
->attribs
[VERT_RESULT_COL1
];
173 VB
->SecondaryColorPtr
[1] = &store
->attribs
[VERT_RESULT_BFC1
];
174 VB
->FogCoordPtr
= &store
->attribs
[VERT_RESULT_FOGC
];
175 VB
->PointSizePtr
= &store
->attribs
[VERT_RESULT_PSIZ
];
177 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = VB
->ColorPtr
[0];
178 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = VB
->SecondaryColorPtr
[0];
179 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = VB
->FogCoordPtr
;
180 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->attribs
[VERT_RESULT_PSIZ
];
182 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
183 VB
->AttribPtr
[VERT_ATTRIB_TEX0
+i
] = VB
->TexCoordPtr
[i
] =
184 &store
->attribs
[VERT_RESULT_TEX0
+ i
];
189 /* Cliptest and perspective divide. Clip functions must clear
193 store
->andmask
= CLIP_ALL_BITS
;
195 if (tnl
->NeedNdcCoords
) {
197 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
205 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
212 if (store
->andmask
) /* All vertices are outside the frustum */
216 /* This is where we'd do clip testing against the user-defined
217 * clipping planes, but they're not supported by vertex programs.
220 VB
->ClipOrMask
= store
->ormask
;
221 VB
->ClipMask
= store
->clipmask
;
228 * This function validates stuff.
230 static GLboolean
run_validate_program( GLcontext
*ctx
,
231 struct tnl_pipeline_stage
*stage
)
234 /* XXX do we need any validation for vertex programs? */
238 if (ctx
->Visual
.rgbMode
) {
239 if (ctx
->Light
._NeedVertices
) {
240 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
241 tab
= _tnl_light_spec_tab
;
243 tab
= _tnl_light_tab
;
246 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
247 tab
= _tnl_light_fast_single_tab
;
249 tab
= _tnl_light_fast_tab
;
253 tab
= _tnl_light_ci_tab
;
255 if (ctx
->Light
.ColorMaterialEnabled
)
256 ind
|= LIGHT_COLORMATERIAL
;
258 if (ctx
->Light
.Model
.TwoSide
)
259 ind
|= LIGHT_TWOSIDE
;
261 VP_STAGE_DATA(stage
)->light_func_tab
= &tab
[ind
];
263 /* This and the above should only be done on _NEW_LIGHT:
265 _mesa_validate_all_lighting_tables( ctx
);
268 /* Now run the stage...
271 return stage
->run( ctx
, stage
);
277 * Called the first time stage->run is called. In effect, don't
278 * allocate data until the first time the stage is run.
280 static GLboolean
run_init_vp( GLcontext
*ctx
,
281 struct tnl_pipeline_stage
*stage
)
283 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
284 struct vertex_buffer
*VB
= &(tnl
->vb
);
285 struct vp_stage_data
*store
;
286 const GLuint size
= VB
->Size
;
289 stage
->privatePtr
= MALLOC(sizeof(*store
));
290 store
= VP_STAGE_DATA(stage
);
294 /* Allocate arrays of vertex output values */
295 for (i
= 0; i
< 15; i
++) {
296 _mesa_vector4f_alloc( &store
->attribs
[i
], 0, size
, 32 );
297 store
->attribs
[i
].size
= 4;
300 /* a few other misc allocations */
301 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
302 store
->clipmask
= (GLubyte
*) ALIGN_MALLOC(sizeof(GLubyte
)*size
, 32 );
304 /* Now validate the stage derived data...
306 stage
->run
= run_validate_program
;
307 return stage
->run( ctx
, stage
);
313 * Check if vertex program mode is enabled.
314 * If so, configure the pipeline stage's type, inputs, and outputs.
316 static void check_vp( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
318 stage
->active
= ctx
->VertexProgram
._Enabled
;
321 /* Set stage->inputs equal to the bitmask of vertex attributes
322 * which the program needs for inputs.
324 stage
->inputs
= ctx
->VertexProgram
.Current
->InputsRead
;
330 * Destructor for this pipeline stage.
332 static void dtr( struct tnl_pipeline_stage
*stage
)
334 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
339 /* free the vertex program result arrays */
340 for (i
= 0; i
< 15; i
++)
341 _mesa_vector4f_free( &store
->attribs
[i
] );
343 /* free misc arrays */
344 _mesa_vector4f_free( &store
->ndcCoords
);
345 ALIGN_FREE( store
->clipmask
);
348 stage
->privatePtr
= 0;
353 * Public description of this pipeline stage.
355 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
358 _NEW_ALL
, /*XXX FIX */ /* recheck */
359 _NEW_ALL
, /*XXX FIX */ /* recalc */
360 GL_FALSE
, /* active */
361 0, /* inputs - calculated on the fly */
362 _TNL_BITS_PROG_ANY
, /* outputs -- could calculate */
363 0, /* changed_inputs */
364 NULL
, /* private_data */
366 check_vp
, /* check */
367 run_init_vp
/* run -- initially set to ctr */