t_vb_program.c revision a328e469d328f8b6fd5afdfc21d576fa1a2c43fc
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.5 4 * 5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 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. 23 */ 24 25 26/** 27 * \file tnl/t_vb_program.c 28 * \brief Pipeline stage for executing NVIDIA vertex programs. 29 * \author Brian Paul, Keith Whitwell 30 */ 31 32 33#include "glheader.h" 34#include "api_noop.h" 35#include "colormac.h" 36#include "context.h" 37#include "dlist.h" 38#include "hash.h" 39#include "light.h" 40#include "macros.h" 41#include "imports.h" 42#include "program.h" 43#include "simple_list.h" 44#include "mtypes.h" 45#include "program_instruction.h" 46#include "nvvertexec.h" 47#include "nvprogram.h" 48 49#include "t_context.h" 50#include "t_pipeline.h" 51 52 53 54/*! 55 * Private storage for the vertex program pipeline stage. 56 */ 57struct vp_stage_data { 58 /** The results of running the vertex program go into these arrays. */ 59 GLvector4f attribs[VERT_RESULT_MAX]; 60 61 GLvector4f ndcCoords; /**< normalized device coords */ 62 GLubyte *clipmask; /**< clip flags */ 63 GLubyte ormask, andmask; /**< for clipping */ 64}; 65 66 67#define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr)) 68 69 70/** 71 * This function executes vertex programs 72 */ 73static GLboolean 74run_vp( GLcontext *ctx, struct tnl_pipeline_stage *stage ) 75{ 76 TNLcontext *tnl = TNL_CONTEXT(ctx); 77 struct vp_stage_data *store = VP_STAGE_DATA(stage); 78 struct vertex_buffer *VB = &tnl->vb; 79 struct gl_vertex_program *program = ctx->VertexProgram._Current; 80 struct vp_machine machine; 81 GLuint i; 82 83 if (!program || !program->IsNVProgram) 84 return GL_TRUE; 85 86 _mesa_load_state_parameters(ctx, program->Base.Parameters); 87 88 /* load program parameter registers (they're read-only) */ 89 _mesa_init_vp_per_primitive_registers(ctx); 90 91 for (i = 0; i < VB->Count; i++) { 92 GLuint attr; 93 94 _mesa_init_vp_per_vertex_registers(ctx, &machine); 95 96#if 0 97 printf("Input %d: %f, %f, %f, %f\n", i, 98 VB->AttribPtr[0]->data[i][0], 99 VB->AttribPtr[0]->data[i][1], 100 VB->AttribPtr[0]->data[i][2], 101 VB->AttribPtr[0]->data[i][3]); 102 printf(" color: %f, %f, %f, %f\n", 103 VB->AttribPtr[3]->data[i][0], 104 VB->AttribPtr[3]->data[i][1], 105 VB->AttribPtr[3]->data[i][2], 106 VB->AttribPtr[3]->data[i][3]); 107 printf(" normal: %f, %f, %f, %f\n", 108 VB->AttribPtr[2]->data[i][0], 109 VB->AttribPtr[2]->data[i][1], 110 VB->AttribPtr[2]->data[i][2], 111 VB->AttribPtr[2]->data[i][3]); 112#endif 113 114 /* the vertex array case */ 115 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) { 116 if (program->Base.InputsRead & (1 << attr)) { 117 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data; 118 const GLuint size = VB->AttribPtr[attr]->size; 119 const GLuint stride = VB->AttribPtr[attr]->stride; 120 const GLfloat *data = (GLfloat *) (ptr + stride * i); 121 COPY_CLEAN_4V(machine.Inputs[attr], size, data); 122 } 123 } 124 125 /* execute the program */ 126 ASSERT(program); 127 _mesa_exec_vertex_program(ctx, &machine, program); 128 129 /* Fixup fog an point size results if needed */ 130 if (ctx->Fog.Enabled && 131 (program->Base.OutputsWritten & (1 << VERT_RESULT_FOGC)) == 0) { 132 machine.Outputs[VERT_RESULT_FOGC][0] = 1.0; 133 } 134 135 if (ctx->VertexProgram.PointSizeEnabled && 136 (program->Base.OutputsWritten & (1 << VERT_RESULT_PSIZ)) == 0) { 137 machine.Outputs[VERT_RESULT_PSIZ][0] = ctx->Point.Size; 138 } 139 140 /* copy the output registers into the VB->attribs arrays */ 141 /* XXX (optimize) could use a conditional and smaller loop limit here */ 142 for (attr = 0; attr < VERT_RESULT_MAX; attr++) { 143 COPY_4V(store->attribs[attr].data[i], machine.Outputs[attr]); 144 } 145#if 0 146 printf("HPOS: %f %f %f %f\n", 147 machine.Outputs[0][0], 148 machine.Outputs[0][1], 149 machine.Outputs[0][2], 150 machine.Outputs[0][3]); 151#endif 152 } 153 154 /* Setup the VB pointers so that the next pipeline stages get 155 * their data from the right place (the program output arrays). 156 */ 157 VB->ClipPtr = &store->attribs[VERT_RESULT_HPOS]; 158 VB->ClipPtr->size = 4; 159 VB->ClipPtr->count = VB->Count; 160 VB->ColorPtr[0] = &store->attribs[VERT_RESULT_COL0]; 161 VB->ColorPtr[1] = &store->attribs[VERT_RESULT_BFC0]; 162 VB->SecondaryColorPtr[0] = &store->attribs[VERT_RESULT_COL1]; 163 VB->SecondaryColorPtr[1] = &store->attribs[VERT_RESULT_BFC1]; 164 VB->FogCoordPtr = &store->attribs[VERT_RESULT_FOGC]; 165 166 VB->AttribPtr[VERT_ATTRIB_COLOR0] = &store->attribs[VERT_RESULT_COL0]; 167 VB->AttribPtr[VERT_ATTRIB_COLOR1] = &store->attribs[VERT_RESULT_COL1]; 168 VB->AttribPtr[VERT_ATTRIB_FOG] = &store->attribs[VERT_RESULT_FOGC]; 169 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->attribs[VERT_RESULT_PSIZ]; 170 171 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) { 172 VB->TexCoordPtr[i] = 173 VB->AttribPtr[_TNL_ATTRIB_TEX0 + i] 174 = &store->attribs[VERT_RESULT_TEX0 + i]; 175 } 176 177 /* Cliptest and perspective divide. Clip functions must clear 178 * the clipmask. 179 */ 180 store->ormask = 0; 181 store->andmask = CLIP_FRUSTUM_BITS; 182 183 if (tnl->NeedNdcCoords) { 184 VB->NdcPtr = 185 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr, 186 &store->ndcCoords, 187 store->clipmask, 188 &store->ormask, 189 &store->andmask ); 190 } 191 else { 192 VB->NdcPtr = NULL; 193 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr, 194 NULL, 195 store->clipmask, 196 &store->ormask, 197 &store->andmask ); 198 } 199 200 if (store->andmask) /* All vertices are outside the frustum */ 201 return GL_FALSE; 202 203 204 /* This is where we'd do clip testing against the user-defined 205 * clipping planes, but they're not supported by vertex programs. 206 */ 207 208 VB->ClipOrMask = store->ormask; 209 VB->ClipMask = store->clipmask; 210 211 return GL_TRUE; 212} 213 214 215 216 217/** 218 * Called the first time stage->run is called. In effect, don't 219 * allocate data until the first time the stage is run. 220 */ 221static GLboolean init_vp( GLcontext *ctx, 222 struct tnl_pipeline_stage *stage ) 223{ 224 TNLcontext *tnl = TNL_CONTEXT(ctx); 225 struct vertex_buffer *VB = &(tnl->vb); 226 struct vp_stage_data *store; 227 const GLuint size = VB->Size; 228 GLuint i; 229 230 stage->privatePtr = MALLOC(sizeof(*store)); 231 store = VP_STAGE_DATA(stage); 232 if (!store) 233 return GL_FALSE; 234 235 /* Allocate arrays of vertex output values */ 236 for (i = 0; i < VERT_RESULT_MAX; i++) { 237 _mesa_vector4f_alloc( &store->attribs[i], 0, size, 32 ); 238 store->attribs[i].size = 4; 239 } 240 241 /* a few other misc allocations */ 242 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 ); 243 store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 ); 244 245 return GL_TRUE; 246} 247 248 249 250 251 252/** 253 * Destructor for this pipeline stage. 254 */ 255static void dtr( struct tnl_pipeline_stage *stage ) 256{ 257 struct vp_stage_data *store = VP_STAGE_DATA(stage); 258 259 if (store) { 260 GLuint i; 261 262 /* free the vertex program result arrays */ 263 for (i = 0; i < VERT_RESULT_MAX; i++) 264 _mesa_vector4f_free( &store->attribs[i] ); 265 266 /* free misc arrays */ 267 _mesa_vector4f_free( &store->ndcCoords ); 268 ALIGN_FREE( store->clipmask ); 269 270 FREE( store ); 271 stage->privatePtr = NULL; 272 } 273} 274 275/** 276 * Public description of this pipeline stage. 277 */ 278const struct tnl_pipeline_stage _tnl_vertex_program_stage = 279{ 280 "vertex-program", 281 NULL, /* private_data */ 282 init_vp, /* create */ 283 dtr, /* destroy */ 284 NULL, /* validate */ 285 run_vp /* run -- initially set to ctr */ 286}; 287