Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 3 | * Version: 6.5 |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 4 | * |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 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 | * \file program.c |
| 27 | * Vertex and fragment program support functions. |
| 28 | * \author Brian Paul |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "glheader.h" |
| 33 | #include "context.h" |
| 34 | #include "hash.h" |
| 35 | #include "imports.h" |
| 36 | #include "macros.h" |
| 37 | #include "mtypes.h" |
| 38 | #include "program.h" |
| 39 | #include "nvfragparse.h" |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 40 | #include "program_instruction.h" |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 41 | #include "nvvertparse.h" |
Roland Scheidegger | f519a77 | 2005-09-02 01:11:53 +0000 | [diff] [blame] | 42 | #include "atifragshader.h" |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | /**********************************************************************/ |
| 46 | /* Utility functions */ |
| 47 | /**********************************************************************/ |
| 48 | |
| 49 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 50 | /* A pointer to this dummy program is put into the hash table when |
| 51 | * glGenPrograms is called. |
| 52 | */ |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 53 | struct program _mesa_DummyProgram; |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 54 | |
| 55 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 56 | /** |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 57 | * Init context's vertex/fragment program state |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 58 | */ |
| 59 | void |
| 60 | _mesa_init_program(GLcontext *ctx) |
| 61 | { |
| 62 | GLuint i; |
| 63 | |
| 64 | ctx->Program.ErrorPos = -1; |
| 65 | ctx->Program.ErrorString = _mesa_strdup(""); |
| 66 | |
| 67 | #if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program |
| 68 | ctx->VertexProgram.Enabled = GL_FALSE; |
| 69 | ctx->VertexProgram.PointSizeEnabled = GL_FALSE; |
| 70 | ctx->VertexProgram.TwoSideEnabled = GL_FALSE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 71 | ctx->VertexProgram.Current = (struct vertex_program *) ctx->Shared->DefaultVertexProgram; |
| 72 | assert(ctx->VertexProgram.Current); |
| 73 | ctx->VertexProgram.Current->Base.RefCount++; |
| 74 | for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) { |
| 75 | ctx->VertexProgram.TrackMatrix[i] = GL_NONE; |
| 76 | ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV; |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | #if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program |
| 81 | ctx->FragmentProgram.Enabled = GL_FALSE; |
| 82 | ctx->FragmentProgram.Current = (struct fragment_program *) ctx->Shared->DefaultFragmentProgram; |
| 83 | assert(ctx->FragmentProgram.Current); |
| 84 | ctx->FragmentProgram.Current->Base.RefCount++; |
| 85 | #endif |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 86 | |
| 87 | #if FEATURE_ATI_fragment_shader |
| 88 | ctx->ATIFragmentShader.Enabled = GL_FALSE; |
| 89 | ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader; |
| 90 | assert(ctx->ATIFragmentShader.Current); |
| 91 | ctx->ATIFragmentShader.Current->Base.RefCount++; |
| 92 | #endif |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 97 | * Free a context's vertex/fragment program state |
| 98 | */ |
| 99 | void |
| 100 | _mesa_free_program_data(GLcontext *ctx) |
| 101 | { |
| 102 | #if FEATURE_NV_vertex_program |
| 103 | if (ctx->VertexProgram.Current) { |
| 104 | ctx->VertexProgram.Current->Base.RefCount--; |
| 105 | if (ctx->VertexProgram.Current->Base.RefCount <= 0) |
| 106 | ctx->Driver.DeleteProgram(ctx, &(ctx->VertexProgram.Current->Base)); |
| 107 | } |
| 108 | #endif |
| 109 | #if FEATURE_NV_fragment_program |
| 110 | if (ctx->FragmentProgram.Current) { |
| 111 | ctx->FragmentProgram.Current->Base.RefCount--; |
| 112 | if (ctx->FragmentProgram.Current->Base.RefCount <= 0) |
| 113 | ctx->Driver.DeleteProgram(ctx, &(ctx->FragmentProgram.Current->Base)); |
| 114 | } |
| 115 | #endif |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 116 | #if FEATURE_ATI_fragment_shader |
| 117 | if (ctx->ATIFragmentShader.Current) { |
| 118 | ctx->ATIFragmentShader.Current->Base.RefCount--; |
| 119 | if (ctx->ATIFragmentShader.Current->Base.RefCount <= 0) |
| 120 | ctx->Driver.DeleteProgram(ctx, &(ctx->ATIFragmentShader.Current->Base)); |
| 121 | } |
| 122 | #endif |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 123 | _mesa_free((void *) ctx->Program.ErrorString); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | /** |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 130 | * Set the vertex/fragment program error state (position and error string). |
| 131 | * This is generally called from within the parsers. |
| 132 | */ |
| 133 | void |
| 134 | _mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string) |
| 135 | { |
| 136 | ctx->Program.ErrorPos = pos; |
| 137 | _mesa_free((void *) ctx->Program.ErrorString); |
| 138 | if (!string) |
| 139 | string = ""; |
| 140 | ctx->Program.ErrorString = _mesa_strdup(string); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Find the line number and column for 'pos' within 'string'. |
| 146 | * Return a copy of the line which contains 'pos'. Free the line with |
| 147 | * _mesa_free(). |
| 148 | * \param string the program string |
| 149 | * \param pos the position within the string |
| 150 | * \param line returns the line number corresponding to 'pos'. |
| 151 | * \param col returns the column number corresponding to 'pos'. |
| 152 | * \return copy of the line containing 'pos'. |
| 153 | */ |
| 154 | const GLubyte * |
| 155 | _mesa_find_line_column(const GLubyte *string, const GLubyte *pos, |
| 156 | GLint *line, GLint *col) |
| 157 | { |
| 158 | const GLubyte *lineStart = string; |
| 159 | const GLubyte *p = string; |
| 160 | GLubyte *s; |
| 161 | int len; |
| 162 | |
| 163 | *line = 1; |
| 164 | |
| 165 | while (p != pos) { |
| 166 | if (*p == (GLubyte) '\n') { |
| 167 | (*line)++; |
| 168 | lineStart = p + 1; |
| 169 | } |
| 170 | p++; |
| 171 | } |
| 172 | |
| 173 | *col = (pos - lineStart) + 1; |
| 174 | |
| 175 | /* return copy of this line */ |
| 176 | while (*p != 0 && *p != '\n') |
| 177 | p++; |
| 178 | len = p - lineStart; |
| 179 | s = (GLubyte *) _mesa_malloc(len + 1); |
| 180 | _mesa_memcpy(s, lineStart, len); |
| 181 | s[len] = 0; |
| 182 | |
| 183 | return s; |
| 184 | } |
| 185 | |
| 186 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 187 | /** |
| 188 | * Initialize a new vertex/fragment program object. |
| 189 | */ |
| 190 | static struct program * |
| 191 | _mesa_init_program_struct( GLcontext *ctx, struct program *prog, |
| 192 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 193 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 194 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 195 | if (prog) { |
| 196 | prog->Id = id; |
| 197 | prog->Target = target; |
| 198 | prog->Resident = GL_TRUE; |
| 199 | prog->RefCount = 1; |
| 200 | } |
| 201 | |
| 202 | return prog; |
| 203 | } |
| 204 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Initialize a new fragment program object. |
| 208 | */ |
| 209 | struct program * |
| 210 | _mesa_init_fragment_program( GLcontext *ctx, struct fragment_program *prog, |
| 211 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 212 | { |
| 213 | if (prog) |
| 214 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 215 | else |
| 216 | return NULL; |
| 217 | } |
| 218 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * Initialize a new vertex program object. |
| 222 | */ |
| 223 | struct program * |
| 224 | _mesa_init_vertex_program( GLcontext *ctx, struct vertex_program *prog, |
| 225 | GLenum target, GLuint id) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 226 | { |
| 227 | if (prog) |
| 228 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 229 | else |
| 230 | return NULL; |
| 231 | } |
| 232 | |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 233 | /** |
| 234 | * Initialize a new ATI fragment shader object. |
| 235 | */ |
| 236 | struct program * |
Brian Paul | cdb6541 | 2005-01-11 15:56:47 +0000 | [diff] [blame] | 237 | _mesa_init_ati_fragment_shader( GLcontext *ctx, |
| 238 | struct ati_fragment_shader *prog, |
| 239 | GLenum target, GLuint id ) |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 240 | { |
| 241 | if (prog) |
| 242 | return _mesa_init_program_struct( ctx, &prog->Base, target, id ); |
| 243 | else |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Allocate and initialize a new fragment/vertex program object but |
| 251 | * don't put it into the program hash table. Called via |
| 252 | * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a |
| 253 | * device driver function to implement OO deriviation with additional |
| 254 | * types not understood by this function. |
| 255 | * |
| 256 | * \param ctx context |
| 257 | * \param id program id/number |
| 258 | * \param target program target/type |
| 259 | * \return pointer to new program object |
| 260 | */ |
| 261 | struct program * |
| 262 | _mesa_new_program(GLcontext *ctx, GLenum target, GLuint id) |
| 263 | { |
| 264 | switch (target) { |
| 265 | case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ |
| 266 | return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program), |
| 267 | target, id ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 268 | case GL_FRAGMENT_PROGRAM_NV: |
| 269 | case GL_FRAGMENT_PROGRAM_ARB: |
| 270 | return _mesa_init_fragment_program( ctx, CALLOC_STRUCT(fragment_program), |
| 271 | target, id ); |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 272 | case GL_FRAGMENT_SHADER_ATI: |
| 273 | return _mesa_init_ati_fragment_shader( ctx, CALLOC_STRUCT(ati_fragment_shader), |
| 274 | target, id ); |
| 275 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 276 | default: |
| 277 | _mesa_problem(ctx, "bad target in _mesa_new_program"); |
| 278 | return NULL; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /** |
| 284 | * Delete a program and remove it from the hash table, ignoring the |
| 285 | * reference count. |
| 286 | * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation) |
| 287 | * by a device driver function. |
| 288 | */ |
| 289 | void |
| 290 | _mesa_delete_program(GLcontext *ctx, struct program *prog) |
| 291 | { |
Brian Paul | a6c423d | 2004-08-25 15:59:48 +0000 | [diff] [blame] | 292 | (void) ctx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 293 | ASSERT(prog); |
| 294 | |
| 295 | if (prog->String) |
| 296 | _mesa_free(prog->String); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 297 | |
| 298 | if (prog->Instructions) { |
| 299 | GLuint i; |
| 300 | for (i = 0; i < prog->NumInstructions; i++) { |
| 301 | if (prog->Instructions[i].Data) |
| 302 | _mesa_free(prog->Instructions[i].Data); |
Brian Paul | 575700f | 2004-12-16 03:07:18 +0000 | [diff] [blame] | 303 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 304 | _mesa_free(prog->Instructions); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 305 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 306 | |
| 307 | if (prog->Parameters) |
| 308 | _mesa_free_parameter_list(prog->Parameters); |
| 309 | |
| 310 | if (prog->Target == GL_FRAGMENT_SHADER_ATI) { |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 311 | struct ati_fragment_shader *atifs = (struct ati_fragment_shader *)prog; |
Roland Scheidegger | f519a77 | 2005-09-02 01:11:53 +0000 | [diff] [blame] | 312 | GLuint i; |
| 313 | for (i = 0; i < MAX_NUM_PASSES_ATI; i++) { |
| 314 | if (atifs->Instructions[i]) |
| 315 | _mesa_free(atifs->Instructions[i]); |
| 316 | if (atifs->SetupInst[i]) |
| 317 | _mesa_free(atifs->SetupInst[i]); |
| 318 | } |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 321 | _mesa_free(prog); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | |
| 326 | /**********************************************************************/ |
| 327 | /* Program parameter functions */ |
| 328 | /**********************************************************************/ |
| 329 | |
| 330 | struct program_parameter_list * |
| 331 | _mesa_new_parameter_list(void) |
| 332 | { |
| 333 | return (struct program_parameter_list *) |
| 334 | _mesa_calloc(sizeof(struct program_parameter_list)); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /** |
| 339 | * Free a parameter list and all its parameters |
| 340 | */ |
| 341 | void |
| 342 | _mesa_free_parameter_list(struct program_parameter_list *paramList) |
| 343 | { |
| 344 | _mesa_free_parameters(paramList); |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 345 | _mesa_free(paramList->Parameters); |
Keith Whitwell | 9899f58 | 2005-06-08 21:57:45 +0000 | [diff] [blame] | 346 | if (paramList->ParameterValues) |
| 347 | ALIGN_FREE(paramList->ParameterValues); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 348 | _mesa_free(paramList); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /** |
| 353 | * Free all the parameters in the given list, but don't free the |
| 354 | * paramList structure itself. |
| 355 | */ |
| 356 | void |
| 357 | _mesa_free_parameters(struct program_parameter_list *paramList) |
| 358 | { |
| 359 | GLuint i; |
| 360 | for (i = 0; i < paramList->NumParameters; i++) { |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 361 | if (paramList->Parameters[i].Name) |
| 362 | _mesa_free((void *) paramList->Parameters[i].Name); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 363 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 364 | paramList->NumParameters = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | |
| 368 | /** |
| 369 | * Helper function used by the functions below. |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 370 | * \return index of new parameter in the list, or -1 if error (out of mem) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 371 | */ |
| 372 | static GLint |
| 373 | add_parameter(struct program_parameter_list *paramList, |
| 374 | const char *name, const GLfloat values[4], |
Brian Paul | 613e1ad | 2005-11-05 02:15:21 +0000 | [diff] [blame] | 375 | enum register_file type) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 376 | { |
| 377 | const GLuint n = paramList->NumParameters; |
| 378 | |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 379 | if (n == paramList->Size) { |
Keith Whitwell | 9899f58 | 2005-06-08 21:57:45 +0000 | [diff] [blame] | 380 | GLfloat (*tmp)[4]; |
| 381 | |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 382 | paramList->Size *= 2; |
| 383 | if (!paramList->Size) |
Keith Whitwell | 9899f58 | 2005-06-08 21:57:45 +0000 | [diff] [blame] | 384 | paramList->Size = 8; |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 385 | |
| 386 | paramList->Parameters = (struct program_parameter *) |
| 387 | _mesa_realloc(paramList->Parameters, |
| 388 | n * sizeof(struct program_parameter), |
| 389 | paramList->Size * sizeof(struct program_parameter)); |
Keith Whitwell | 9899f58 | 2005-06-08 21:57:45 +0000 | [diff] [blame] | 390 | |
| 391 | tmp = paramList->ParameterValues; |
| 392 | paramList->ParameterValues = ALIGN_MALLOC(paramList->Size * 4 * sizeof(GLfloat), 16); |
| 393 | if (tmp) { |
| 394 | _mesa_memcpy(paramList->ParameterValues, tmp, |
| 395 | n * 4 * sizeof(GLfloat)); |
| 396 | ALIGN_FREE(tmp); |
| 397 | } |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 398 | } |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 399 | |
| 400 | if (!paramList->Parameters || |
| 401 | !paramList->ParameterValues) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 402 | /* out of memory */ |
| 403 | paramList->NumParameters = 0; |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 404 | paramList->Size = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 405 | return -1; |
| 406 | } |
| 407 | else { |
| 408 | paramList->NumParameters = n + 1; |
Keith Whitwell | a42fe19 | 2005-05-10 18:22:19 +0000 | [diff] [blame] | 409 | |
| 410 | _mesa_memset(¶mList->Parameters[n], 0, |
| 411 | sizeof(struct program_parameter)); |
| 412 | |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 413 | paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 414 | paramList->Parameters[n].Type = type; |
| 415 | if (values) |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 416 | COPY_4V(paramList->ParameterValues[n], values); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 417 | return (GLint) n; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /** |
| 423 | * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement) |
| 424 | * \return index of the new entry in the parameter list |
| 425 | */ |
| 426 | GLint |
| 427 | _mesa_add_named_parameter(struct program_parameter_list *paramList, |
| 428 | const char *name, const GLfloat values[4]) |
| 429 | { |
Brian Paul | 613e1ad | 2005-11-05 02:15:21 +0000 | [diff] [blame] | 430 | return add_parameter(paramList, name, values, PROGRAM_NAMED_PARAM); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
| 434 | /** |
| 435 | * Add a new unnamed constant to the parameter list. |
| 436 | * \param paramList - the parameter list |
| 437 | * \param values - four float values |
| 438 | * \return index of the new parameter. |
| 439 | */ |
| 440 | GLint |
| 441 | _mesa_add_named_constant(struct program_parameter_list *paramList, |
| 442 | const char *name, const GLfloat values[4]) |
| 443 | { |
Brian Paul | 613e1ad | 2005-11-05 02:15:21 +0000 | [diff] [blame] | 444 | return add_parameter(paramList, name, values, PROGRAM_CONSTANT); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
| 448 | /** |
| 449 | * Add a new unnamed constant to the parameter list. |
| 450 | * \param paramList - the parameter list |
| 451 | * \param values - four float values |
| 452 | * \return index of the new parameter. |
| 453 | */ |
| 454 | GLint |
| 455 | _mesa_add_unnamed_constant(struct program_parameter_list *paramList, |
| 456 | const GLfloat values[4]) |
| 457 | { |
Brian Paul | 613e1ad | 2005-11-05 02:15:21 +0000 | [diff] [blame] | 458 | return add_parameter(paramList, NULL, values, PROGRAM_CONSTANT); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
| 462 | /** |
| 463 | * Add a new state reference to the parameter list. |
| 464 | * \param paramList - the parameter list |
| 465 | * \param state - an array of 6 state tokens |
| 466 | * |
| 467 | * \return index of the new parameter. |
| 468 | */ |
| 469 | GLint |
| 470 | _mesa_add_state_reference(struct program_parameter_list *paramList, |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 471 | const GLint *stateTokens) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 472 | { |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 473 | /* XXX we should probably search the current parameter list to see if |
| 474 | * the new state reference is already present. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 475 | */ |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 476 | GLint index; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 477 | |
Brian Paul | 613e1ad | 2005-11-05 02:15:21 +0000 | [diff] [blame] | 478 | index = add_parameter(paramList, NULL, NULL, PROGRAM_STATE_VAR); |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 479 | if (index >= 0) { |
| 480 | GLuint i; |
| 481 | for (i = 0; i < 6; i++) |
| 482 | paramList->Parameters[index].StateIndexes[i] |
| 483 | = (enum state_index) stateTokens[i]; |
| 484 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 485 | |
Brian Paul | 1624162 | 2005-11-03 02:26:47 +0000 | [diff] [blame] | 486 | return index; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | |
| 490 | /** |
| 491 | * Lookup a parameter value by name in the given parameter list. |
| 492 | * \return pointer to the float[4] values. |
| 493 | */ |
| 494 | GLfloat * |
| 495 | _mesa_lookup_parameter_value(struct program_parameter_list *paramList, |
| 496 | GLsizei nameLen, const char *name) |
| 497 | { |
| 498 | GLuint i; |
| 499 | |
| 500 | if (!paramList) |
| 501 | return NULL; |
| 502 | |
| 503 | if (nameLen == -1) { |
| 504 | /* name is null-terminated */ |
| 505 | for (i = 0; i < paramList->NumParameters; i++) { |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 506 | if (paramList->Parameters[i].Name && |
| 507 | _mesa_strcmp(paramList->Parameters[i].Name, name) == 0) |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 508 | return paramList->ParameterValues[i]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | else { |
| 512 | /* name is not null-terminated, use nameLen */ |
| 513 | for (i = 0; i < paramList->NumParameters; i++) { |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 514 | if (paramList->Parameters[i].Name && |
| 515 | _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0 |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 516 | && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen) |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 517 | return paramList->ParameterValues[i]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /** |
| 525 | * Lookup a parameter index by name in the given parameter list. |
| 526 | * \return index of parameter in the list. |
| 527 | */ |
| 528 | GLint |
| 529 | _mesa_lookup_parameter_index(struct program_parameter_list *paramList, |
| 530 | GLsizei nameLen, const char *name) |
| 531 | { |
| 532 | GLint i; |
| 533 | |
| 534 | if (!paramList) |
| 535 | return -1; |
| 536 | |
| 537 | if (nameLen == -1) { |
| 538 | /* name is null-terminated */ |
| 539 | for (i = 0; i < (GLint) paramList->NumParameters; i++) { |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 540 | if (paramList->Parameters[i].Name && |
| 541 | _mesa_strcmp(paramList->Parameters[i].Name, name) == 0) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 542 | return i; |
| 543 | } |
| 544 | } |
| 545 | else { |
| 546 | /* name is not null-terminated, use nameLen */ |
| 547 | for (i = 0; i < (GLint) paramList->NumParameters; i++) { |
Keith Whitwell | f29f2fc | 2005-05-10 13:56:23 +0000 | [diff] [blame] | 548 | if (paramList->Parameters[i].Name && |
| 549 | _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0 |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 550 | && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen) |
| 551 | return i; |
| 552 | } |
| 553 | } |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | /** |
| 559 | * Use the list of tokens in the state[] array to find global GL state |
| 560 | * and return it in <value>. Usually, four values are returned in <value> |
| 561 | * but matrix queries may return as many as 16 values. |
| 562 | * This function is used for ARB vertex/fragment programs. |
| 563 | * The program parser will produce the state[] values. |
| 564 | */ |
| 565 | static void |
| 566 | _mesa_fetch_state(GLcontext *ctx, const enum state_index state[], |
| 567 | GLfloat *value) |
| 568 | { |
| 569 | switch (state[0]) { |
| 570 | case STATE_MATERIAL: |
| 571 | { |
| 572 | /* state[1] is either 0=front or 1=back side */ |
| 573 | const GLuint face = (GLuint) state[1]; |
| 574 | /* state[2] is the material attribute */ |
| 575 | switch (state[2]) { |
| 576 | case STATE_AMBIENT: |
| 577 | if (face == 0) |
| 578 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]); |
| 579 | else |
| 580 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]); |
| 581 | return; |
| 582 | case STATE_DIFFUSE: |
| 583 | if (face == 0) |
| 584 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]); |
| 585 | else |
| 586 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]); |
| 587 | return; |
| 588 | case STATE_SPECULAR: |
| 589 | if (face == 0) |
| 590 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]); |
| 591 | else |
| 592 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]); |
| 593 | return; |
| 594 | case STATE_EMISSION: |
| 595 | if (face == 0) |
| 596 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]); |
| 597 | else |
| 598 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]); |
| 599 | return; |
| 600 | case STATE_SHININESS: |
| 601 | if (face == 0) |
| 602 | value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0]; |
| 603 | else |
| 604 | value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0]; |
| 605 | value[1] = 0.0F; |
| 606 | value[2] = 0.0F; |
| 607 | value[3] = 1.0F; |
| 608 | return; |
| 609 | default: |
| 610 | _mesa_problem(ctx, "Invalid material state in fetch_state"); |
| 611 | return; |
| 612 | } |
Alan Hourihane | 22ae633 | 2004-12-02 13:29:40 +0000 | [diff] [blame] | 613 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 614 | case STATE_LIGHT: |
| 615 | { |
| 616 | /* state[1] is the light number */ |
| 617 | const GLuint ln = (GLuint) state[1]; |
| 618 | /* state[2] is the light attribute */ |
| 619 | switch (state[2]) { |
| 620 | case STATE_AMBIENT: |
| 621 | COPY_4V(value, ctx->Light.Light[ln].Ambient); |
| 622 | return; |
| 623 | case STATE_DIFFUSE: |
| 624 | COPY_4V(value, ctx->Light.Light[ln].Diffuse); |
| 625 | return; |
| 626 | case STATE_SPECULAR: |
| 627 | COPY_4V(value, ctx->Light.Light[ln].Specular); |
| 628 | return; |
| 629 | case STATE_POSITION: |
| 630 | COPY_4V(value, ctx->Light.Light[ln].EyePosition); |
| 631 | return; |
| 632 | case STATE_ATTENUATION: |
| 633 | value[0] = ctx->Light.Light[ln].ConstantAttenuation; |
| 634 | value[1] = ctx->Light.Light[ln].LinearAttenuation; |
| 635 | value[2] = ctx->Light.Light[ln].QuadraticAttenuation; |
| 636 | value[3] = ctx->Light.Light[ln].SpotExponent; |
| 637 | return; |
| 638 | case STATE_SPOT_DIRECTION: |
Brian Paul | 52bf005 | 2005-04-20 23:47:03 +0000 | [diff] [blame] | 639 | COPY_3V(value, ctx->Light.Light[ln].EyeDirection); |
| 640 | value[3] = ctx->Light.Light[ln]._CosCutoff; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 641 | return; |
| 642 | case STATE_HALF: |
| 643 | { |
| 644 | GLfloat eye_z[] = {0, 0, 1}; |
| 645 | |
| 646 | /* Compute infinite half angle vector: |
| 647 | * half-vector = light_position + (0, 0, 1) |
| 648 | * and then normalize. w = 0 |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 649 | * |
| 650 | * light.EyePosition.w should be 0 for infinite lights. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 651 | */ |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 652 | ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition); |
| 653 | NORMALIZE_3FV(value); |
| 654 | value[3] = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 655 | } |
| 656 | return; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 657 | case STATE_POSITION_NORMALIZED: |
| 658 | COPY_4V(value, ctx->Light.Light[ln].EyePosition); |
| 659 | NORMALIZE_3FV( value ); |
| 660 | return; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 661 | default: |
| 662 | _mesa_problem(ctx, "Invalid light state in fetch_state"); |
| 663 | return; |
| 664 | } |
| 665 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 666 | case STATE_LIGHTMODEL_AMBIENT: |
| 667 | COPY_4V(value, ctx->Light.Model.Ambient); |
| 668 | return; |
| 669 | case STATE_LIGHTMODEL_SCENECOLOR: |
| 670 | if (state[1] == 0) { |
| 671 | /* front */ |
| 672 | GLint i; |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 673 | for (i = 0; i < 3; i++) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 674 | value[i] = ctx->Light.Model.Ambient[i] |
| 675 | * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i] |
| 676 | + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i]; |
| 677 | } |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 678 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 679 | } |
| 680 | else { |
| 681 | /* back */ |
| 682 | GLint i; |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 683 | for (i = 0; i < 3; i++) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 684 | value[i] = ctx->Light.Model.Ambient[i] |
| 685 | * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i] |
| 686 | + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i]; |
| 687 | } |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 688 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 689 | } |
| 690 | return; |
| 691 | case STATE_LIGHTPROD: |
| 692 | { |
| 693 | const GLuint ln = (GLuint) state[1]; |
| 694 | const GLuint face = (GLuint) state[2]; |
| 695 | GLint i; |
| 696 | ASSERT(face == 0 || face == 1); |
| 697 | switch (state[3]) { |
| 698 | case STATE_AMBIENT: |
| 699 | for (i = 0; i < 3; i++) { |
| 700 | value[i] = ctx->Light.Light[ln].Ambient[i] * |
| 701 | ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i]; |
| 702 | } |
| 703 | /* [3] = material alpha */ |
| 704 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3]; |
| 705 | return; |
| 706 | case STATE_DIFFUSE: |
| 707 | for (i = 0; i < 3; i++) { |
| 708 | value[i] = ctx->Light.Light[ln].Diffuse[i] * |
| 709 | ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i]; |
| 710 | } |
| 711 | /* [3] = material alpha */ |
| 712 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3]; |
| 713 | return; |
| 714 | case STATE_SPECULAR: |
| 715 | for (i = 0; i < 3; i++) { |
| 716 | value[i] = ctx->Light.Light[ln].Specular[i] * |
| 717 | ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i]; |
| 718 | } |
| 719 | /* [3] = material alpha */ |
| 720 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3]; |
| 721 | return; |
| 722 | default: |
| 723 | _mesa_problem(ctx, "Invalid lightprod state in fetch_state"); |
| 724 | return; |
| 725 | } |
| 726 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 727 | case STATE_TEXGEN: |
| 728 | { |
| 729 | /* state[1] is the texture unit */ |
| 730 | const GLuint unit = (GLuint) state[1]; |
| 731 | /* state[2] is the texgen attribute */ |
| 732 | switch (state[2]) { |
| 733 | case STATE_TEXGEN_EYE_S: |
| 734 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS); |
| 735 | return; |
| 736 | case STATE_TEXGEN_EYE_T: |
| 737 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT); |
| 738 | return; |
| 739 | case STATE_TEXGEN_EYE_R: |
| 740 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR); |
| 741 | return; |
| 742 | case STATE_TEXGEN_EYE_Q: |
| 743 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ); |
| 744 | return; |
| 745 | case STATE_TEXGEN_OBJECT_S: |
| 746 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS); |
| 747 | return; |
| 748 | case STATE_TEXGEN_OBJECT_T: |
| 749 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT); |
| 750 | return; |
| 751 | case STATE_TEXGEN_OBJECT_R: |
| 752 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR); |
| 753 | return; |
| 754 | case STATE_TEXGEN_OBJECT_Q: |
| 755 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ); |
| 756 | return; |
| 757 | default: |
| 758 | _mesa_problem(ctx, "Invalid texgen state in fetch_state"); |
| 759 | return; |
| 760 | } |
| 761 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 762 | case STATE_TEXENV_COLOR: |
| 763 | { |
| 764 | /* state[1] is the texture unit */ |
| 765 | const GLuint unit = (GLuint) state[1]; |
| 766 | COPY_4V(value, ctx->Texture.Unit[unit].EnvColor); |
| 767 | } |
| 768 | return; |
| 769 | case STATE_FOG_COLOR: |
| 770 | COPY_4V(value, ctx->Fog.Color); |
| 771 | return; |
| 772 | case STATE_FOG_PARAMS: |
| 773 | value[0] = ctx->Fog.Density; |
| 774 | value[1] = ctx->Fog.Start; |
| 775 | value[2] = ctx->Fog.End; |
| 776 | value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start); |
| 777 | return; |
| 778 | case STATE_CLIPPLANE: |
| 779 | { |
| 780 | const GLuint plane = (GLuint) state[1]; |
| 781 | COPY_4V(value, ctx->Transform.EyeUserPlane[plane]); |
| 782 | } |
| 783 | return; |
| 784 | case STATE_POINT_SIZE: |
| 785 | value[0] = ctx->Point.Size; |
| 786 | value[1] = ctx->Point.MinSize; |
| 787 | value[2] = ctx->Point.MaxSize; |
| 788 | value[3] = ctx->Point.Threshold; |
| 789 | return; |
| 790 | case STATE_POINT_ATTENUATION: |
| 791 | value[0] = ctx->Point.Params[0]; |
| 792 | value[1] = ctx->Point.Params[1]; |
| 793 | value[2] = ctx->Point.Params[2]; |
| 794 | value[3] = 1.0F; |
| 795 | return; |
| 796 | case STATE_MATRIX: |
| 797 | { |
| 798 | /* state[1] = modelview, projection, texture, etc. */ |
| 799 | /* state[2] = which texture matrix or program matrix */ |
| 800 | /* state[3] = first column to fetch */ |
| 801 | /* state[4] = last column to fetch */ |
| 802 | /* state[5] = transpose, inverse or invtrans */ |
| 803 | |
| 804 | const GLmatrix *matrix; |
| 805 | const enum state_index mat = state[1]; |
| 806 | const GLuint index = (GLuint) state[2]; |
| 807 | const GLuint first = (GLuint) state[3]; |
| 808 | const GLuint last = (GLuint) state[4]; |
| 809 | const enum state_index modifier = state[5]; |
| 810 | const GLfloat *m; |
| 811 | GLuint row, i; |
| 812 | if (mat == STATE_MODELVIEW) { |
| 813 | matrix = ctx->ModelviewMatrixStack.Top; |
| 814 | } |
| 815 | else if (mat == STATE_PROJECTION) { |
| 816 | matrix = ctx->ProjectionMatrixStack.Top; |
| 817 | } |
| 818 | else if (mat == STATE_MVP) { |
| 819 | matrix = &ctx->_ModelProjectMatrix; |
| 820 | } |
| 821 | else if (mat == STATE_TEXTURE) { |
| 822 | matrix = ctx->TextureMatrixStack[index].Top; |
| 823 | } |
| 824 | else if (mat == STATE_PROGRAM) { |
| 825 | matrix = ctx->ProgramMatrixStack[index].Top; |
| 826 | } |
| 827 | else { |
| 828 | _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()"); |
| 829 | return; |
| 830 | } |
| 831 | if (modifier == STATE_MATRIX_INVERSE || |
| 832 | modifier == STATE_MATRIX_INVTRANS) { |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 833 | /* Be sure inverse is up to date: |
| 834 | */ |
Jouk Jansen | 49b1d95 | 2005-04-18 13:05:24 +0000 | [diff] [blame] | 835 | _math_matrix_analyse( (GLmatrix*) matrix ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 836 | m = matrix->inv; |
| 837 | } |
| 838 | else { |
| 839 | m = matrix->m; |
| 840 | } |
| 841 | if (modifier == STATE_MATRIX_TRANSPOSE || |
| 842 | modifier == STATE_MATRIX_INVTRANS) { |
| 843 | for (i = 0, row = first; row <= last; row++) { |
| 844 | value[i++] = m[row * 4 + 0]; |
| 845 | value[i++] = m[row * 4 + 1]; |
| 846 | value[i++] = m[row * 4 + 2]; |
| 847 | value[i++] = m[row * 4 + 3]; |
| 848 | } |
| 849 | } |
| 850 | else { |
| 851 | for (i = 0, row = first; row <= last; row++) { |
| 852 | value[i++] = m[row + 0]; |
| 853 | value[i++] = m[row + 4]; |
| 854 | value[i++] = m[row + 8]; |
| 855 | value[i++] = m[row + 12]; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | return; |
| 860 | case STATE_DEPTH_RANGE: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 861 | value[0] = ctx->Viewport.Near; /* near */ |
| 862 | value[1] = ctx->Viewport.Far; /* far */ |
| 863 | value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */ |
| 864 | value[3] = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 865 | return; |
| 866 | case STATE_FRAGMENT_PROGRAM: |
| 867 | { |
| 868 | /* state[1] = {STATE_ENV, STATE_LOCAL} */ |
| 869 | /* state[2] = parameter index */ |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 870 | const int idx = (int) state[2]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 871 | switch (state[1]) { |
| 872 | case STATE_ENV: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 873 | COPY_4V(value, ctx->FragmentProgram.Parameters[idx]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 874 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 875 | case STATE_LOCAL: |
| 876 | COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]); |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 877 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 878 | default: |
| 879 | _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()"); |
| 880 | return; |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 883 | return; |
| 884 | |
| 885 | case STATE_VERTEX_PROGRAM: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 886 | { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 887 | /* state[1] = {STATE_ENV, STATE_LOCAL} */ |
| 888 | /* state[2] = parameter index */ |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 889 | const int idx = (int) state[2]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 890 | switch (state[1]) { |
| 891 | case STATE_ENV: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 892 | COPY_4V(value, ctx->VertexProgram.Parameters[idx]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 893 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 894 | case STATE_LOCAL: |
| 895 | COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]); |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 896 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 897 | default: |
| 898 | _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()"); |
| 899 | return; |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 902 | return; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 903 | |
| 904 | case STATE_INTERNAL: |
| 905 | { |
| 906 | switch (state[1]) { |
| 907 | case STATE_NORMAL_SCALE: |
| 908 | ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1); |
| 909 | break; |
| 910 | default: |
| 911 | _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()"); |
| 912 | return; |
| 913 | } |
| 914 | } |
| 915 | return; |
| 916 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 917 | default: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 918 | _mesa_problem(ctx, "Invalid state in _mesa_fetch_state"); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 919 | return; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | |
| 924 | /** |
| 925 | * Loop over all the parameters in a parameter list. If the parameter |
| 926 | * is a GL state reference, look up the current value of that state |
| 927 | * variable and put it into the parameter's Value[4] array. |
| 928 | * This would be called at glBegin time when using a fragment program. |
| 929 | */ |
| 930 | void |
| 931 | _mesa_load_state_parameters(GLcontext *ctx, |
| 932 | struct program_parameter_list *paramList) |
| 933 | { |
| 934 | GLuint i; |
| 935 | |
| 936 | if (!paramList) |
| 937 | return; |
| 938 | |
| 939 | for (i = 0; i < paramList->NumParameters; i++) { |
Brian Paul | 613e1ad | 2005-11-05 02:15:21 +0000 | [diff] [blame] | 940 | if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 941 | _mesa_fetch_state(ctx, |
| 942 | paramList->Parameters[i].StateIndexes, |
| 943 | paramList->ParameterValues[i]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 949 | /** |
| 950 | * Basic info about each instruction |
| 951 | */ |
| 952 | struct instruction_info |
| 953 | { |
| 954 | enum prog_opcode Opcode; |
| 955 | const char *Name; |
| 956 | GLuint NumSrcRegs; |
| 957 | }; |
| 958 | |
| 959 | /** |
| 960 | * Instruction info |
| 961 | * \note Opcode should equal array index! |
| 962 | */ |
| 963 | static const struct instruction_info InstInfo[MAX_OPCODE] = { |
| 964 | { OPCODE_ABS, "ABS", 1 }, |
| 965 | { OPCODE_ADD, "ADD", 2 }, |
Ian Romanick | 4884db6 | 2005-11-08 22:40:26 +0000 | [diff] [blame] | 966 | { OPCODE_ARA, "ARA", 1 }, |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 967 | { OPCODE_ARL, "ARL", 1 }, |
Ian Romanick | 4884db6 | 2005-11-08 22:40:26 +0000 | [diff] [blame] | 968 | { OPCODE_ARL_NV, "ARL", 1 }, |
| 969 | { OPCODE_ARR, "ARL", 1 }, |
| 970 | { OPCODE_BRA, "BRA", 1 }, |
| 971 | { OPCODE_CAL, "CAL", 1 }, |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 972 | { OPCODE_CMP, "CMP", 3 }, |
| 973 | { OPCODE_COS, "COS", 1 }, |
| 974 | { OPCODE_DDX, "DDX", 1 }, |
| 975 | { OPCODE_DDY, "DDY", 1 }, |
| 976 | { OPCODE_DP3, "DP3", 2 }, |
| 977 | { OPCODE_DP4, "DP4", 2 }, |
| 978 | { OPCODE_DPH, "DPH", 2 }, |
| 979 | { OPCODE_DST, "DST", 2 }, |
| 980 | { OPCODE_END, "END", 0 }, |
| 981 | { OPCODE_EX2, "EX2", 1 }, |
| 982 | { OPCODE_EXP, "EXP", 1 }, |
| 983 | { OPCODE_FLR, "FLR", 1 }, |
| 984 | { OPCODE_FRC, "FRC", 1 }, |
| 985 | { OPCODE_KIL, "KIL", 1 }, |
| 986 | { OPCODE_KIL_NV, "KIL", 0 }, |
| 987 | { OPCODE_LG2, "LG2", 1 }, |
| 988 | { OPCODE_LIT, "LIT", 1 }, |
| 989 | { OPCODE_LOG, "LOG", 1 }, |
| 990 | { OPCODE_LRP, "LRP", 3 }, |
| 991 | { OPCODE_MAD, "MAD", 3 }, |
| 992 | { OPCODE_MAX, "MAX", 2 }, |
| 993 | { OPCODE_MIN, "MIN", 2 }, |
| 994 | { OPCODE_MOV, "MOV", 1 }, |
| 995 | { OPCODE_MUL, "MUL", 2 }, |
| 996 | { OPCODE_PK2H, "PK2H", 1 }, |
| 997 | { OPCODE_PK2US, "PK2US", 1 }, |
| 998 | { OPCODE_PK4B, "PK4B", 1 }, |
| 999 | { OPCODE_PK4UB, "PK4UB", 1 }, |
| 1000 | { OPCODE_POW, "POW", 2 }, |
Ian Romanick | 4884db6 | 2005-11-08 22:40:26 +0000 | [diff] [blame] | 1001 | { OPCODE_POPA, "POPA", 0 }, |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 1002 | { OPCODE_PRINT, "PRINT", 1 }, |
Ian Romanick | 4884db6 | 2005-11-08 22:40:26 +0000 | [diff] [blame] | 1003 | { OPCODE_PUSHA, "PUSHA", 0 }, |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 1004 | { OPCODE_RCC, "RCC", 1 }, |
| 1005 | { OPCODE_RCP, "RCP", 1 }, |
Ian Romanick | 4884db6 | 2005-11-08 22:40:26 +0000 | [diff] [blame] | 1006 | { OPCODE_RET, "RET", 1 }, |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 1007 | { OPCODE_RFL, "RFL", 1 }, |
| 1008 | { OPCODE_RSQ, "RSQ", 1 }, |
| 1009 | { OPCODE_SCS, "SCS", 1 }, |
| 1010 | { OPCODE_SEQ, "SEQ", 2 }, |
| 1011 | { OPCODE_SFL, "SFL", 0 }, |
| 1012 | { OPCODE_SGE, "SGE", 2 }, |
| 1013 | { OPCODE_SGT, "SGT", 2 }, |
| 1014 | { OPCODE_SIN, "SIN", 1 }, |
| 1015 | { OPCODE_SLE, "SLE", 2 }, |
| 1016 | { OPCODE_SLT, "SLT", 2 }, |
| 1017 | { OPCODE_SNE, "SNE", 2 }, |
Ian Romanick | 4884db6 | 2005-11-08 22:40:26 +0000 | [diff] [blame] | 1018 | { OPCODE_SSG, "SSG", 1 }, |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 1019 | { OPCODE_STR, "STR", 0 }, |
| 1020 | { OPCODE_SUB, "SUB", 2 }, |
| 1021 | { OPCODE_SWZ, "SWZ", 1 }, |
| 1022 | { OPCODE_TEX, "TEX", 1 }, |
| 1023 | { OPCODE_TXB, "TXB", 1 }, |
| 1024 | { OPCODE_TXD, "TXD", 3 }, |
Ian Romanick | 4884db6 | 2005-11-08 22:40:26 +0000 | [diff] [blame] | 1025 | { OPCODE_TXL, "TXL", 1 }, |
Brian Paul | 1fcdaf1 | 2005-11-05 19:12:36 +0000 | [diff] [blame] | 1026 | { OPCODE_TXP, "TXP", 1 }, |
| 1027 | { OPCODE_TXP_NV, "TXP", 1 }, |
| 1028 | { OPCODE_UP2H, "UP2H", 1 }, |
| 1029 | { OPCODE_UP2US, "UP2US", 1 }, |
| 1030 | { OPCODE_UP4B, "UP4B", 1 }, |
| 1031 | { OPCODE_UP4UB, "UP4UB", 1 }, |
| 1032 | { OPCODE_X2D, "X2D", 3 }, |
| 1033 | { OPCODE_XPD, "XPD", 2 } |
| 1034 | }; |
| 1035 | |
| 1036 | |
| 1037 | /** |
| 1038 | * Return the number of src registers for the given instruction/opcode. |
| 1039 | */ |
| 1040 | GLuint |
| 1041 | _mesa_num_inst_src_regs(enum prog_opcode opcode) |
| 1042 | { |
| 1043 | GLuint i; |
| 1044 | #ifdef DEBUG |
| 1045 | for (i = 0; i < MAX_OPCODE; i++) { |
| 1046 | ASSERT(i == InstInfo[i].Opcode); |
| 1047 | } |
| 1048 | #endif |
| 1049 | for (i = 0; i < MAX_OPCODE; i++) { |
| 1050 | if (InstInfo[i].Opcode == opcode) { |
| 1051 | return InstInfo[i].NumSrcRegs; |
| 1052 | } |
| 1053 | } |
| 1054 | _mesa_problem(NULL, "invalid opcode in _mesa_num_inst_src_regs"); |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | /** |
| 1060 | * Return string name for given program opcode. |
| 1061 | */ |
| 1062 | const char * |
| 1063 | _mesa_opcode_string(enum prog_opcode opcode) |
| 1064 | { |
| 1065 | ASSERT(opcode < MAX_OPCODE); |
| 1066 | return InstInfo[opcode].Name; |
| 1067 | } |
| 1068 | |
Brian Paul | bf41bc0 | 2005-11-05 19:32:36 +0000 | [diff] [blame] | 1069 | /** |
| 1070 | * Return string name for given program/register file. |
| 1071 | */ |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1072 | static const char * |
| 1073 | program_file_string(enum register_file f) |
Brian Paul | bf41bc0 | 2005-11-05 19:32:36 +0000 | [diff] [blame] | 1074 | { |
| 1075 | switch (f) { |
| 1076 | case PROGRAM_TEMPORARY: |
| 1077 | return "TEMP"; |
| 1078 | case PROGRAM_LOCAL_PARAM: |
| 1079 | return "LOCAL"; |
| 1080 | case PROGRAM_ENV_PARAM: |
| 1081 | return "ENV"; |
| 1082 | case PROGRAM_STATE_VAR: |
| 1083 | return "STATE"; |
| 1084 | case PROGRAM_INPUT: |
| 1085 | return "INPUT"; |
| 1086 | case PROGRAM_OUTPUT: |
| 1087 | return "OUTPUT"; |
| 1088 | case PROGRAM_NAMED_PARAM: |
| 1089 | return "NAMED"; |
| 1090 | case PROGRAM_CONSTANT: |
| 1091 | return "CONST"; |
| 1092 | case PROGRAM_WRITE_ONLY: |
| 1093 | return "WRITE_ONLY"; |
| 1094 | case PROGRAM_ADDRESS: |
| 1095 | return "ADDR"; |
| 1096 | default: |
| 1097 | return "!unkown!"; |
| 1098 | } |
| 1099 | } |
| 1100 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1101 | |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1102 | /** |
| 1103 | * Return a string representation of the given swizzle word. |
| 1104 | */ |
| 1105 | static const char * |
| 1106 | swizzle_string(GLuint swizzle, GLuint negateBase) |
| 1107 | { |
| 1108 | static const char swz[] = "xyzw01"; |
| 1109 | static char s[20]; |
| 1110 | GLuint i = 0; |
| 1111 | |
| 1112 | if (swizzle == SWIZZLE_NOOP && negateBase == 0) |
| 1113 | return ""; /* no swizzle/negation */ |
| 1114 | |
| 1115 | s[i++] = '.'; |
| 1116 | |
| 1117 | if (negateBase & 0x1) |
| 1118 | s[i++] = '-'; |
| 1119 | s[i++] = swz[GET_SWZ(swizzle, 0)]; |
| 1120 | |
| 1121 | if (negateBase & 0x2) |
| 1122 | s[i++] = '-'; |
| 1123 | s[i++] = swz[GET_SWZ(swizzle, 1)]; |
| 1124 | |
| 1125 | if (negateBase & 0x4) |
| 1126 | s[i++] = '-'; |
| 1127 | s[i++] = swz[GET_SWZ(swizzle, 2)]; |
| 1128 | |
| 1129 | if (negateBase & 0x8) |
| 1130 | s[i++] = '-'; |
| 1131 | s[i++] = swz[GET_SWZ(swizzle, 3)]; |
| 1132 | |
| 1133 | s[i] = 0; |
| 1134 | return s; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | static const char * |
| 1139 | writemask_string(GLuint writeMask) |
| 1140 | { |
| 1141 | static char s[10]; |
| 1142 | GLuint i = 0; |
| 1143 | |
| 1144 | if (writeMask == WRITEMASK_XYZW) |
| 1145 | return ""; |
| 1146 | |
| 1147 | s[i++] = '.'; |
| 1148 | if (writeMask & WRITEMASK_X) |
| 1149 | s[i++] = 'x'; |
| 1150 | if (writeMask & WRITEMASK_Y) |
| 1151 | s[i++] = 'y'; |
| 1152 | if (writeMask & WRITEMASK_Z) |
| 1153 | s[i++] = 'z'; |
| 1154 | if (writeMask & WRITEMASK_W) |
| 1155 | s[i++] = 'w'; |
| 1156 | |
| 1157 | s[i] = 0; |
| 1158 | return s; |
| 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | /** |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1163 | * Print a single vertex/fragment program instruction. |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1164 | */ |
| 1165 | void |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1166 | _mesa_print_instruction(const struct prog_instruction *inst) |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1167 | { |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1168 | switch (inst->Opcode) { |
| 1169 | case OPCODE_PRINT: |
| 1170 | _mesa_printf("PRINT '%s'", inst->Data); |
| 1171 | if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) { |
| 1172 | _mesa_printf(", "); |
| 1173 | _mesa_printf("%s[%d]%s", |
| 1174 | program_file_string(inst->SrcReg[0].File), |
| 1175 | inst->SrcReg[0].Index, |
| 1176 | swizzle_string(inst->SrcReg[0].Swizzle, |
| 1177 | inst->SrcReg[0].NegateBase)); |
| 1178 | } |
| 1179 | _mesa_printf(";\n"); |
| 1180 | break; |
| 1181 | /* XXX check for a bunch of other special-case instructions */ |
| 1182 | default: |
| 1183 | /* typical alu instruction */ |
| 1184 | { |
| 1185 | const GLuint numRegs = _mesa_num_inst_src_regs(inst->Opcode); |
| 1186 | GLuint j; |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1187 | |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1188 | _mesa_printf("%s", _mesa_opcode_string(inst->Opcode)); |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1189 | |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1190 | /* frag prog only */ |
| 1191 | if (inst->Saturate) |
| 1192 | _mesa_printf("_SAT"); |
| 1193 | |
| 1194 | if (inst->DstReg.File != PROGRAM_UNDEFINED) { |
| 1195 | _mesa_printf(" %s[%d]%s", |
| 1196 | program_file_string(inst->DstReg.File), |
| 1197 | inst->DstReg.Index, |
| 1198 | writemask_string(inst->DstReg.WriteMask)); |
| 1199 | } |
| 1200 | |
| 1201 | if (numRegs > 0) |
Brian Paul | 02df9e1 | 2005-11-08 14:42:52 +0000 | [diff] [blame] | 1202 | _mesa_printf(", "); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1203 | |
| 1204 | for (j = 0; j < numRegs; j++) { |
Brian Paul | 02df9e1 | 2005-11-08 14:42:52 +0000 | [diff] [blame] | 1205 | _mesa_printf("%s[%d]%s", |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1206 | program_file_string(inst->SrcReg[j].File), |
| 1207 | inst->SrcReg[j].Index, |
| 1208 | swizzle_string(inst->SrcReg[j].Swizzle, |
| 1209 | inst->SrcReg[j].NegateBase)); |
| 1210 | if (j + 1 < numRegs) |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1211 | _mesa_printf(", "); |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1212 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1213 | |
| 1214 | _mesa_printf(";\n"); |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1220 | /** |
| 1221 | * Print a vertx/fragment program to stdout. |
| 1222 | * XXX this function could be greatly improved. |
| 1223 | */ |
| 1224 | void |
| 1225 | _mesa_print_program(const struct program *prog) |
| 1226 | { |
| 1227 | GLuint i; |
| 1228 | for (i = 0; i < prog->NumInstructions; i++) { |
| 1229 | _mesa_printf("%3d: ", i); |
| 1230 | _mesa_print_instruction(prog->Instructions + i); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | /** |
| 1236 | * Print all of a program's parameters. |
| 1237 | */ |
| 1238 | void |
| 1239 | _mesa_print_program_parameters(GLcontext *ctx, const struct program *prog) |
| 1240 | { |
| 1241 | GLint i; |
| 1242 | |
| 1243 | _mesa_printf("NumInstructions=%d\n", prog->NumInstructions); |
| 1244 | _mesa_printf("NumTemporaries=%d\n", prog->NumTemporaries); |
| 1245 | _mesa_printf("NumParameters=%d\n", prog->NumParameters); |
| 1246 | _mesa_printf("NumAttributes=%d\n", prog->NumAttributes); |
| 1247 | _mesa_printf("NumAddressRegs=%d\n", prog->NumAddressRegs); |
| 1248 | |
| 1249 | _mesa_load_state_parameters(ctx, prog->Parameters); |
| 1250 | |
| 1251 | #if 0 |
| 1252 | _mesa_printf("Local Params:\n"); |
| 1253 | for (i = 0; i < MAX_PROGRAM_LOCAL_PARAMS; i++){ |
| 1254 | const GLfloat *p = prog->LocalParams[i]; |
| 1255 | _mesa_printf("%2d: %f, %f, %f, %f\n", i, p[0], p[1], p[2], p[3]); |
| 1256 | } |
| 1257 | #endif |
| 1258 | |
| 1259 | for (i = 0; i < prog->Parameters->NumParameters; i++){ |
| 1260 | const GLfloat *p = prog->Parameters->ParameterValues[i]; |
| 1261 | _mesa_printf("param %02d:", i); |
| 1262 | |
| 1263 | switch (prog->Parameters->Parameters[i].Type) { |
| 1264 | case PROGRAM_NAMED_PARAM: |
| 1265 | _mesa_printf("%s", prog->Parameters->Parameters[i].Name); |
| 1266 | _mesa_printf("(NAMED_PARAMETER)"); |
| 1267 | break; |
| 1268 | case PROGRAM_CONSTANT: |
| 1269 | _mesa_printf("(CONSTANT)"); |
| 1270 | break; |
| 1271 | case PROGRAM_STATE_VAR: |
| 1272 | _mesa_printf("(STATE)\n"); |
| 1273 | break; |
| 1274 | default: |
| 1275 | _mesa_printf("(UNK)\n"); |
| 1276 | break; |
| 1277 | } |
| 1278 | |
| 1279 | _mesa_printf("{ %f, %f, %f, %f }\n", p[0], p[1], p[2], p[3]); |
| 1280 | } |
| 1281 | } |
| 1282 | |
Brian Paul | 30d6a4b | 2005-11-05 20:18:18 +0000 | [diff] [blame] | 1283 | |
| 1284 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1285 | /**********************************************************************/ |
| 1286 | /* API functions */ |
| 1287 | /**********************************************************************/ |
| 1288 | |
| 1289 | |
| 1290 | /** |
| 1291 | * Bind a program (make it current) |
| 1292 | * \note Called from the GL API dispatcher by both glBindProgramNV |
| 1293 | * and glBindProgramARB. |
| 1294 | */ |
| 1295 | void GLAPIENTRY |
| 1296 | _mesa_BindProgram(GLenum target, GLuint id) |
| 1297 | { |
| 1298 | struct program *prog; |
| 1299 | GET_CURRENT_CONTEXT(ctx); |
| 1300 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 1301 | |
| 1302 | FLUSH_VERTICES(ctx, _NEW_PROGRAM); |
| 1303 | |
| 1304 | if ((target == GL_VERTEX_PROGRAM_NV |
| 1305 | && ctx->Extensions.NV_vertex_program) || |
| 1306 | (target == GL_VERTEX_PROGRAM_ARB |
| 1307 | && ctx->Extensions.ARB_vertex_program)) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1308 | /*** Vertex program binding ***/ |
| 1309 | struct vertex_program *curProg = ctx->VertexProgram.Current; |
| 1310 | if (curProg->Base.Id == id) { |
| 1311 | /* binding same program - no change */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1312 | return; |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1313 | } |
| 1314 | if (curProg->Base.Id != 0) { |
| 1315 | /* decrement refcount on previously bound vertex program */ |
| 1316 | curProg->Base.RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1317 | /* and delete if refcount goes below one */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1318 | if (curProg->Base.RefCount <= 0) { |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 1319 | /* the program ID was already removed from the hash table */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1320 | ctx->Driver.DeleteProgram(ctx, &(curProg->Base)); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | else if ((target == GL_FRAGMENT_PROGRAM_NV |
| 1325 | && ctx->Extensions.NV_fragment_program) || |
| 1326 | (target == GL_FRAGMENT_PROGRAM_ARB |
| 1327 | && ctx->Extensions.ARB_fragment_program)) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1328 | /*** Fragment program binding ***/ |
| 1329 | struct fragment_program *curProg = ctx->FragmentProgram.Current; |
| 1330 | if (curProg->Base.Id == id) { |
| 1331 | /* binding same program - no change */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1332 | return; |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1333 | } |
| 1334 | if (curProg->Base.Id != 0) { |
| 1335 | /* decrement refcount on previously bound fragment program */ |
| 1336 | curProg->Base.RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1337 | /* and delete if refcount goes below one */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1338 | if (curProg->Base.RefCount <= 0) { |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 1339 | /* the program ID was already removed from the hash table */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1340 | ctx->Driver.DeleteProgram(ctx, &(curProg->Base)); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | else { |
| 1345 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)"); |
| 1346 | return; |
| 1347 | } |
| 1348 | |
| 1349 | /* NOTE: binding to a non-existant program is not an error. |
| 1350 | * That's supposed to be caught in glBegin. |
| 1351 | */ |
| 1352 | if (id == 0) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1353 | /* Bind default program */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1354 | prog = NULL; |
| 1355 | if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) |
| 1356 | prog = ctx->Shared->DefaultVertexProgram; |
| 1357 | else |
| 1358 | prog = ctx->Shared->DefaultFragmentProgram; |
| 1359 | } |
| 1360 | else { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1361 | /* Bind user program */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1362 | prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id); |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 1363 | if (!prog || prog == &_mesa_DummyProgram) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1364 | /* allocate a new program now */ |
| 1365 | prog = ctx->Driver.NewProgram(ctx, target, id); |
| 1366 | if (!prog) { |
| 1367 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB"); |
| 1368 | return; |
| 1369 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1370 | _mesa_HashInsert(ctx->Shared->Programs, id, prog); |
| 1371 | } |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1372 | else if (prog->Target != target) { |
| 1373 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1374 | "glBindProgramNV/ARB(target mismatch)"); |
| 1375 | return; |
| 1376 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /* bind now */ |
| 1380 | if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) { |
| 1381 | ctx->VertexProgram.Current = (struct vertex_program *) prog; |
| 1382 | } |
| 1383 | else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) { |
| 1384 | ctx->FragmentProgram.Current = (struct fragment_program *) prog; |
| 1385 | } |
| 1386 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1387 | /* Never null pointers */ |
| 1388 | ASSERT(ctx->VertexProgram.Current); |
| 1389 | ASSERT(ctx->FragmentProgram.Current); |
| 1390 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1391 | if (prog) |
| 1392 | prog->RefCount++; |
| 1393 | |
| 1394 | if (ctx->Driver.BindProgram) |
| 1395 | ctx->Driver.BindProgram(ctx, target, prog); |
| 1396 | } |
| 1397 | |
| 1398 | |
| 1399 | /** |
| 1400 | * Delete a list of programs. |
| 1401 | * \note Not compiled into display lists. |
| 1402 | * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB. |
| 1403 | */ |
| 1404 | void GLAPIENTRY |
| 1405 | _mesa_DeletePrograms(GLsizei n, const GLuint *ids) |
| 1406 | { |
| 1407 | GLint i; |
| 1408 | GET_CURRENT_CONTEXT(ctx); |
| 1409 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); |
| 1410 | |
| 1411 | if (n < 0) { |
| 1412 | _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" ); |
| 1413 | return; |
| 1414 | } |
| 1415 | |
| 1416 | for (i = 0; i < n; i++) { |
| 1417 | if (ids[i] != 0) { |
| 1418 | struct program *prog = (struct program *) |
| 1419 | _mesa_HashLookup(ctx->Shared->Programs, ids[i]); |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 1420 | if (prog == &_mesa_DummyProgram) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1421 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 1422 | } |
| 1423 | else if (prog) { |
| 1424 | /* Unbind program if necessary */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1425 | if (prog->Target == GL_VERTEX_PROGRAM_NV || |
| 1426 | prog->Target == GL_VERTEX_STATE_PROGRAM_NV) { |
| 1427 | if (ctx->VertexProgram.Current && |
| 1428 | ctx->VertexProgram.Current->Base.Id == ids[i]) { |
| 1429 | /* unbind this currently bound program */ |
| 1430 | _mesa_BindProgram(prog->Target, 0); |
| 1431 | } |
| 1432 | } |
| 1433 | else if (prog->Target == GL_FRAGMENT_PROGRAM_NV || |
| 1434 | prog->Target == GL_FRAGMENT_PROGRAM_ARB) { |
| 1435 | if (ctx->FragmentProgram.Current && |
| 1436 | ctx->FragmentProgram.Current->Base.Id == ids[i]) { |
| 1437 | /* unbind this currently bound program */ |
| 1438 | _mesa_BindProgram(prog->Target, 0); |
| 1439 | } |
| 1440 | } |
| 1441 | else { |
| 1442 | _mesa_problem(ctx, "bad target in glDeleteProgramsNV"); |
| 1443 | return; |
| 1444 | } |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 1445 | /* The ID is immediately available for re-use now */ |
| 1446 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 1447 | prog->RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1448 | if (prog->RefCount <= 0) { |
| 1449 | ctx->Driver.DeleteProgram(ctx, prog); |
| 1450 | } |
| 1451 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | /** |
| 1458 | * Generate a list of new program identifiers. |
| 1459 | * \note Not compiled into display lists. |
| 1460 | * \note Called by both glGenProgramsNV and glGenProgramsARB. |
| 1461 | */ |
| 1462 | void GLAPIENTRY |
| 1463 | _mesa_GenPrograms(GLsizei n, GLuint *ids) |
| 1464 | { |
| 1465 | GLuint first; |
| 1466 | GLuint i; |
| 1467 | GET_CURRENT_CONTEXT(ctx); |
| 1468 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 1469 | |
| 1470 | if (n < 0) { |
| 1471 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms"); |
| 1472 | return; |
| 1473 | } |
| 1474 | |
| 1475 | if (!ids) |
| 1476 | return; |
| 1477 | |
| 1478 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n); |
| 1479 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1480 | /* Insert pointer to dummy program as placeholder */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1481 | for (i = 0; i < (GLuint) n; i++) { |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 1482 | _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /* Return the program names */ |
| 1486 | for (i = 0; i < (GLuint) n; i++) { |
| 1487 | ids[i] = first + i; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | /** |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1493 | * Determine if id names a vertex or fragment program. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1494 | * \note Not compiled into display lists. |
| 1495 | * \note Called from both glIsProgramNV and glIsProgramARB. |
| 1496 | * \param id is the program identifier |
| 1497 | * \return GL_TRUE if id is a program, else GL_FALSE. |
| 1498 | */ |
| 1499 | GLboolean GLAPIENTRY |
| 1500 | _mesa_IsProgram(GLuint id) |
| 1501 | { |
| 1502 | GET_CURRENT_CONTEXT(ctx); |
| 1503 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 1504 | |
| 1505 | if (id == 0) |
| 1506 | return GL_FALSE; |
| 1507 | |
| 1508 | if (_mesa_HashLookup(ctx->Shared->Programs, id)) |
| 1509 | return GL_TRUE; |
| 1510 | else |
| 1511 | return GL_FALSE; |
| 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | /**********************************************************************/ |
| 1517 | /* GL_MESA_program_debug extension */ |
| 1518 | /**********************************************************************/ |
| 1519 | |
| 1520 | |
| 1521 | /* XXX temporary */ |
Daniel Borca | 0a13ceb | 2005-02-14 08:01:59 +0000 | [diff] [blame] | 1522 | GLAPI void GLAPIENTRY |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1523 | glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback, |
| 1524 | GLvoid *data) |
| 1525 | { |
| 1526 | _mesa_ProgramCallbackMESA(target, callback, data); |
| 1527 | } |
| 1528 | |
| 1529 | |
| 1530 | void |
| 1531 | _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback, |
| 1532 | GLvoid *data) |
| 1533 | { |
| 1534 | GET_CURRENT_CONTEXT(ctx); |
| 1535 | |
| 1536 | switch (target) { |
| 1537 | case GL_FRAGMENT_PROGRAM_ARB: |
| 1538 | if (!ctx->Extensions.ARB_fragment_program) { |
| 1539 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1540 | return; |
| 1541 | } |
| 1542 | ctx->FragmentProgram.Callback = callback; |
| 1543 | ctx->FragmentProgram.CallbackData = data; |
| 1544 | break; |
| 1545 | case GL_FRAGMENT_PROGRAM_NV: |
| 1546 | if (!ctx->Extensions.NV_fragment_program) { |
| 1547 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1548 | return; |
| 1549 | } |
| 1550 | ctx->FragmentProgram.Callback = callback; |
| 1551 | ctx->FragmentProgram.CallbackData = data; |
| 1552 | break; |
| 1553 | case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ |
| 1554 | if (!ctx->Extensions.ARB_vertex_program && |
| 1555 | !ctx->Extensions.NV_vertex_program) { |
| 1556 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1557 | return; |
| 1558 | } |
| 1559 | ctx->VertexProgram.Callback = callback; |
| 1560 | ctx->VertexProgram.CallbackData = data; |
| 1561 | break; |
| 1562 | default: |
| 1563 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1564 | return; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | |
| 1569 | /* XXX temporary */ |
Daniel Borca | 0a13ceb | 2005-02-14 08:01:59 +0000 | [diff] [blame] | 1570 | GLAPI void GLAPIENTRY |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1571 | glGetProgramRegisterfvMESA(GLenum target, |
| 1572 | GLsizei len, const GLubyte *registerName, |
| 1573 | GLfloat *v) |
| 1574 | { |
| 1575 | _mesa_GetProgramRegisterfvMESA(target, len, registerName, v); |
| 1576 | } |
| 1577 | |
| 1578 | |
| 1579 | void |
| 1580 | _mesa_GetProgramRegisterfvMESA(GLenum target, |
| 1581 | GLsizei len, const GLubyte *registerName, |
| 1582 | GLfloat *v) |
| 1583 | { |
| 1584 | char reg[1000]; |
| 1585 | GET_CURRENT_CONTEXT(ctx); |
| 1586 | |
| 1587 | /* We _should_ be inside glBegin/glEnd */ |
| 1588 | #if 0 |
| 1589 | if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) { |
| 1590 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA"); |
| 1591 | return; |
| 1592 | } |
| 1593 | #endif |
| 1594 | |
| 1595 | /* make null-terminated copy of registerName */ |
| 1596 | len = MIN2((unsigned int) len, sizeof(reg) - 1); |
| 1597 | _mesa_memcpy(reg, registerName, len); |
| 1598 | reg[len] = 0; |
| 1599 | |
| 1600 | switch (target) { |
| 1601 | case GL_VERTEX_PROGRAM_NV: |
| 1602 | if (!ctx->Extensions.ARB_vertex_program && |
| 1603 | !ctx->Extensions.NV_vertex_program) { |
| 1604 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1605 | "glGetProgramRegisterfvMESA(target)"); |
| 1606 | return; |
| 1607 | } |
Brian Paul | 6d460af | 2004-04-23 14:16:46 +0000 | [diff] [blame] | 1608 | if (!ctx->VertexProgram._Enabled) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1609 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1610 | "glGetProgramRegisterfvMESA"); |
| 1611 | return; |
| 1612 | } |
| 1613 | /* GL_NV_vertex_program */ |
| 1614 | if (reg[0] == 'R') { |
| 1615 | /* Temp register */ |
| 1616 | GLint i = _mesa_atoi(reg + 1); |
Brian Paul | 0505103 | 2005-11-01 04:36:33 +0000 | [diff] [blame] | 1617 | if (i >= (GLint)ctx->Const.VertexProgram.MaxTemps) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1618 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1619 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1620 | return; |
| 1621 | } |
| 1622 | COPY_4V(v, ctx->VertexProgram.Temporaries[i]); |
| 1623 | } |
| 1624 | else if (reg[0] == 'v' && reg[1] == '[') { |
| 1625 | /* Vertex Input attribute */ |
| 1626 | GLuint i; |
Brian Paul | 0505103 | 2005-11-01 04:36:33 +0000 | [diff] [blame] | 1627 | for (i = 0; i < ctx->Const.VertexProgram.MaxAttribs; i++) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1628 | const char *name = _mesa_nv_vertex_input_register_name(i); |
| 1629 | char number[10]; |
Brian Paul | aa20695 | 2005-09-16 18:14:24 +0000 | [diff] [blame] | 1630 | _mesa_sprintf(number, "%d", i); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1631 | if (_mesa_strncmp(reg + 2, name, 4) == 0 || |
| 1632 | _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) { |
| 1633 | COPY_4V(v, ctx->VertexProgram.Inputs[i]); |
| 1634 | return; |
| 1635 | } |
| 1636 | } |
| 1637 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1638 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1639 | return; |
| 1640 | } |
| 1641 | else if (reg[0] == 'o' && reg[1] == '[') { |
| 1642 | /* Vertex output attribute */ |
| 1643 | } |
| 1644 | /* GL_ARB_vertex_program */ |
| 1645 | else if (_mesa_strncmp(reg, "vertex.", 7) == 0) { |
| 1646 | |
| 1647 | } |
| 1648 | else { |
| 1649 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1650 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1651 | return; |
| 1652 | } |
| 1653 | break; |
| 1654 | case GL_FRAGMENT_PROGRAM_ARB: |
| 1655 | if (!ctx->Extensions.ARB_fragment_program) { |
| 1656 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1657 | "glGetProgramRegisterfvMESA(target)"); |
| 1658 | return; |
| 1659 | } |
Brian Paul | 6d460af | 2004-04-23 14:16:46 +0000 | [diff] [blame] | 1660 | if (!ctx->FragmentProgram._Enabled) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1661 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1662 | "glGetProgramRegisterfvMESA"); |
| 1663 | return; |
| 1664 | } |
| 1665 | /* XXX to do */ |
| 1666 | break; |
| 1667 | case GL_FRAGMENT_PROGRAM_NV: |
| 1668 | if (!ctx->Extensions.NV_fragment_program) { |
| 1669 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1670 | "glGetProgramRegisterfvMESA(target)"); |
| 1671 | return; |
| 1672 | } |
Brian Paul | 6d460af | 2004-04-23 14:16:46 +0000 | [diff] [blame] | 1673 | if (!ctx->FragmentProgram._Enabled) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1674 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1675 | "glGetProgramRegisterfvMESA"); |
| 1676 | return; |
| 1677 | } |
| 1678 | if (reg[0] == 'R') { |
| 1679 | /* Temp register */ |
| 1680 | GLint i = _mesa_atoi(reg + 1); |
Brian Paul | 0505103 | 2005-11-01 04:36:33 +0000 | [diff] [blame] | 1681 | if (i >= (GLint)ctx->Const.FragmentProgram.MaxTemps) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1682 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1683 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1684 | return; |
| 1685 | } |
| 1686 | COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]); |
| 1687 | } |
| 1688 | else if (reg[0] == 'f' && reg[1] == '[') { |
| 1689 | /* Fragment input attribute */ |
| 1690 | GLuint i; |
Brian Paul | 0505103 | 2005-11-01 04:36:33 +0000 | [diff] [blame] | 1691 | for (i = 0; i < ctx->Const.FragmentProgram.MaxAttribs; i++) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1692 | const char *name = _mesa_nv_fragment_input_register_name(i); |
| 1693 | if (_mesa_strncmp(reg + 2, name, 4) == 0) { |
| 1694 | COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]); |
| 1695 | return; |
| 1696 | } |
| 1697 | } |
| 1698 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1699 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1700 | return; |
| 1701 | } |
| 1702 | else if (_mesa_strcmp(reg, "o[COLR]") == 0) { |
| 1703 | /* Fragment output color */ |
Brian Paul | 90ebb58 | 2005-11-02 18:06:12 +0000 | [diff] [blame] | 1704 | COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLR]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1705 | } |
| 1706 | else if (_mesa_strcmp(reg, "o[COLH]") == 0) { |
| 1707 | /* Fragment output color */ |
Brian Paul | 90ebb58 | 2005-11-02 18:06:12 +0000 | [diff] [blame] | 1708 | COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_COLH]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1709 | } |
| 1710 | else if (_mesa_strcmp(reg, "o[DEPR]") == 0) { |
| 1711 | /* Fragment output depth */ |
Brian Paul | 90ebb58 | 2005-11-02 18:06:12 +0000 | [diff] [blame] | 1712 | COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_RESULT_DEPR]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1713 | } |
| 1714 | else { |
| 1715 | /* try user-defined identifiers */ |
| 1716 | const GLfloat *value = _mesa_lookup_parameter_value( |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame^] | 1717 | ctx->FragmentProgram.Current->Base.Parameters, -1, reg); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1718 | if (value) { |
| 1719 | COPY_4V(v, value); |
| 1720 | } |
| 1721 | else { |
| 1722 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1723 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1724 | return; |
| 1725 | } |
| 1726 | } |
| 1727 | break; |
| 1728 | default: |
| 1729 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1730 | "glGetProgramRegisterfvMESA(target)"); |
| 1731 | return; |
| 1732 | } |
| 1733 | |
| 1734 | } |