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