2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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 "program_instruction.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"
56 * Private storage for the vertex program pipeline stage.
58 struct vp_stage_data
{
59 /** The results of running the vertex program go into these arrays. */
60 GLvector4f attribs
[15];
62 GLvector4f ndcCoords
; /**< normalized device coords */
63 GLubyte
*clipmask
; /**< clip flags */
64 GLubyte ormask
, andmask
; /**< for clipping */
68 #define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr))
72 * This function executes vertex programs
75 run_vp( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
77 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
78 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
79 struct vertex_buffer
*VB
= &tnl
->vb
;
80 struct vertex_program
*program
= ctx
->VertexProgram
.Current
;
83 if (ctx
->ShaderObjects
.CurrentProgram
!= NULL
)
86 if (!ctx
->VertexProgram
._Enabled
||
87 !program
->IsNVProgram
)
90 /* load program parameter registers (they're read-only) */
91 _mesa_init_vp_per_primitive_registers(ctx
);
93 for (i
= 0; i
< VB
->Count
; i
++) {
96 _mesa_init_vp_per_vertex_registers(ctx
);
99 printf("Input %d: %f, %f, %f, %f\n", i
,
100 VB
->AttribPtr
[0]->data
[i
][0],
101 VB
->AttribPtr
[0]->data
[i
][1],
102 VB
->AttribPtr
[0]->data
[i
][2],
103 VB
->AttribPtr
[0]->data
[i
][3]);
104 printf(" color: %f, %f, %f, %f\n",
105 VB
->AttribPtr
[3]->data
[i
][0],
106 VB
->AttribPtr
[3]->data
[i
][1],
107 VB
->AttribPtr
[3]->data
[i
][2],
108 VB
->AttribPtr
[3]->data
[i
][3]);
109 printf(" normal: %f, %f, %f, %f\n",
110 VB
->AttribPtr
[2]->data
[i
][0],
111 VB
->AttribPtr
[2]->data
[i
][1],
112 VB
->AttribPtr
[2]->data
[i
][2],
113 VB
->AttribPtr
[2]->data
[i
][3]);
116 /* the vertex array case */
117 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
118 if (program
->Base
.InputsRead
& (1 << attr
)) {
119 const GLubyte
*ptr
= (const GLubyte
*) VB
->AttribPtr
[attr
]->data
;
120 const GLuint size
= VB
->AttribPtr
[attr
]->size
;
121 const GLuint stride
= VB
->AttribPtr
[attr
]->stride
;
122 const GLfloat
*data
= (GLfloat
*) (ptr
+ stride
* i
);
123 COPY_CLEAN_4V(ctx
->VertexProgram
.Inputs
[attr
], size
, data
);
127 /* execute the program */
129 _mesa_exec_vertex_program(ctx
, program
);
131 /* Fixup fog an point size results if needed */
132 if (ctx
->Fog
.Enabled
&&
133 (program
->Base
.OutputsWritten
& (1 << VERT_RESULT_FOGC
)) == 0) {
134 ctx
->VertexProgram
.Outputs
[VERT_RESULT_FOGC
][0] = 1.0;
137 if (ctx
->VertexProgram
.PointSizeEnabled
&&
138 (program
->Base
.OutputsWritten
& (1 << VERT_RESULT_PSIZ
)) == 0) {
139 ctx
->VertexProgram
.Outputs
[VERT_RESULT_PSIZ
][0] = ctx
->Point
.Size
;
142 /* copy the output registers into the VB->attribs arrays */
143 /* XXX (optimize) could use a conditional and smaller loop limit here */
144 for (attr
= 0; attr
< 15; attr
++) {
145 COPY_4V(store
->attribs
[attr
].data
[i
],
146 ctx
->VertexProgram
.Outputs
[attr
]);
150 /* Setup the VB pointers so that the next pipeline stages get
151 * their data from the right place (the program output arrays).
153 VB
->ClipPtr
= &store
->attribs
[VERT_RESULT_HPOS
];
154 VB
->ClipPtr
->size
= 4;
155 VB
->ClipPtr
->count
= VB
->Count
;
156 VB
->ColorPtr
[0] = &store
->attribs
[VERT_RESULT_COL0
];
157 VB
->ColorPtr
[1] = &store
->attribs
[VERT_RESULT_BFC0
];
158 VB
->SecondaryColorPtr
[0] = &store
->attribs
[VERT_RESULT_COL1
];
159 VB
->SecondaryColorPtr
[1] = &store
->attribs
[VERT_RESULT_BFC1
];
160 VB
->FogCoordPtr
= &store
->attribs
[VERT_RESULT_FOGC
];
161 VB
->PointSizePtr
= &store
->attribs
[VERT_RESULT_PSIZ
];
163 VB
->AttribPtr
[VERT_ATTRIB_COLOR0
] = VB
->ColorPtr
[0];
164 VB
->AttribPtr
[VERT_ATTRIB_COLOR1
] = VB
->SecondaryColorPtr
[0];
165 VB
->AttribPtr
[VERT_ATTRIB_FOG
] = VB
->FogCoordPtr
;
166 VB
->AttribPtr
[_TNL_ATTRIB_POINTSIZE
] = &store
->attribs
[VERT_RESULT_PSIZ
];
168 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
169 VB
->AttribPtr
[VERT_ATTRIB_TEX0
+i
] = VB
->TexCoordPtr
[i
] =
170 &store
->attribs
[VERT_RESULT_TEX0
+ i
];
175 /* Cliptest and perspective divide. Clip functions must clear
179 store
->andmask
= CLIP_ALL_BITS
;
181 if (tnl
->NeedNdcCoords
) {
183 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
191 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
198 if (store
->andmask
) /* All vertices are outside the frustum */
202 /* This is where we'd do clip testing against the user-defined
203 * clipping planes, but they're not supported by vertex programs.
206 VB
->ClipOrMask
= store
->ormask
;
207 VB
->ClipMask
= store
->clipmask
;
216 * Called the first time stage->run is called. In effect, don't
217 * allocate data until the first time the stage is run.
219 static GLboolean
init_vp( GLcontext
*ctx
,
220 struct tnl_pipeline_stage
*stage
)
222 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
223 struct vertex_buffer
*VB
= &(tnl
->vb
);
224 struct vp_stage_data
*store
;
225 const GLuint size
= VB
->Size
;
228 stage
->privatePtr
= MALLOC(sizeof(*store
));
229 store
= VP_STAGE_DATA(stage
);
233 /* Allocate arrays of vertex output values */
234 for (i
= 0; i
< 15; i
++) {
235 _mesa_vector4f_alloc( &store
->attribs
[i
], 0, size
, 32 );
236 store
->attribs
[i
].size
= 4;
239 /* a few other misc allocations */
240 _mesa_vector4f_alloc( &store
->ndcCoords
, 0, size
, 32 );
241 store
->clipmask
= (GLubyte
*) ALIGN_MALLOC(sizeof(GLubyte
)*size
, 32 );
251 * Destructor for this pipeline stage.
253 static void dtr( struct tnl_pipeline_stage
*stage
)
255 struct vp_stage_data
*store
= VP_STAGE_DATA(stage
);
260 /* free the vertex program result arrays */
261 for (i
= 0; i
< VERT_RESULT_MAX
; i
++)
262 _mesa_vector4f_free( &store
->attribs
[i
] );
264 /* free misc arrays */
265 _mesa_vector4f_free( &store
->ndcCoords
);
266 ALIGN_FREE( store
->clipmask
);
269 stage
->privatePtr
= NULL
;
274 * Public description of this pipeline stage.
276 const struct tnl_pipeline_stage _tnl_vertex_program_stage
=
279 NULL
, /* private_data */
280 init_vp
, /* create */
283 run_vp
/* run -- initially set to ctr */