Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 3 | * Version: 6.2 |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 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 | * \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" |
| 40 | #include "nvfragprog.h" |
| 41 | #include "nvvertparse.h" |
Brian Paul | 575700f | 2004-12-16 03:07:18 +0000 | [diff] [blame] | 42 | #include "nvvertprog.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); |
| 297 | if (prog->Target == GL_VERTEX_PROGRAM_NV || |
| 298 | prog->Target == GL_VERTEX_STATE_PROGRAM_NV) { |
| 299 | struct vertex_program *vprog = (struct vertex_program *) prog; |
Brian Paul | 575700f | 2004-12-16 03:07:18 +0000 | [diff] [blame] | 300 | if (vprog->Instructions) { |
| 301 | GLuint i; |
| 302 | for (i = 0; i < vprog->Base.NumInstructions; i++) { |
| 303 | if (vprog->Instructions[i].Data) |
| 304 | _mesa_free(vprog->Instructions[i].Data); |
| 305 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 306 | _mesa_free(vprog->Instructions); |
Brian Paul | 575700f | 2004-12-16 03:07:18 +0000 | [diff] [blame] | 307 | } |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 308 | if (vprog->Parameters) |
| 309 | _mesa_free_parameter_list(vprog->Parameters); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 310 | } |
| 311 | else if (prog->Target == GL_FRAGMENT_PROGRAM_NV || |
| 312 | prog->Target == GL_FRAGMENT_PROGRAM_ARB) { |
| 313 | struct fragment_program *fprog = (struct fragment_program *) prog; |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 314 | if (fprog->Instructions) { |
| 315 | GLuint i; |
| 316 | for (i = 0; i < fprog->Base.NumInstructions; i++) { |
| 317 | if (fprog->Instructions[i].Data) |
| 318 | _mesa_free(fprog->Instructions[i].Data); |
| 319 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 320 | _mesa_free(fprog->Instructions); |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 321 | } |
Brian Paul | 21841f0 | 2004-08-14 14:28:11 +0000 | [diff] [blame] | 322 | if (fprog->Parameters) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 323 | _mesa_free_parameter_list(fprog->Parameters); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 324 | } |
Dave Airlie | 7f752fe | 2004-12-19 03:06:59 +0000 | [diff] [blame] | 325 | else if (prog->Target == GL_FRAGMENT_SHADER_ATI) { |
| 326 | struct ati_fragment_shader *atifs = (struct ati_fragment_shader *)prog; |
| 327 | if (atifs->Instructions) |
| 328 | _mesa_free(atifs->Instructions); |
| 329 | } |
| 330 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 331 | _mesa_free(prog); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | |
| 336 | /**********************************************************************/ |
| 337 | /* Program parameter functions */ |
| 338 | /**********************************************************************/ |
| 339 | |
| 340 | struct program_parameter_list * |
| 341 | _mesa_new_parameter_list(void) |
| 342 | { |
| 343 | return (struct program_parameter_list *) |
| 344 | _mesa_calloc(sizeof(struct program_parameter_list)); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * Free a parameter list and all its parameters |
| 350 | */ |
| 351 | void |
| 352 | _mesa_free_parameter_list(struct program_parameter_list *paramList) |
| 353 | { |
| 354 | _mesa_free_parameters(paramList); |
| 355 | _mesa_free(paramList); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | /** |
| 360 | * Free all the parameters in the given list, but don't free the |
| 361 | * paramList structure itself. |
| 362 | */ |
| 363 | void |
| 364 | _mesa_free_parameters(struct program_parameter_list *paramList) |
| 365 | { |
| 366 | GLuint i; |
| 367 | for (i = 0; i < paramList->NumParameters; i++) { |
| 368 | _mesa_free((void *) paramList->Parameters[i].Name); |
| 369 | } |
| 370 | _mesa_free(paramList->Parameters); |
| 371 | paramList->NumParameters = 0; |
| 372 | paramList->Parameters = NULL; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /** |
| 377 | * Helper function used by the functions below. |
| 378 | */ |
| 379 | static GLint |
| 380 | add_parameter(struct program_parameter_list *paramList, |
| 381 | const char *name, const GLfloat values[4], |
| 382 | enum parameter_type type) |
| 383 | { |
| 384 | const GLuint n = paramList->NumParameters; |
| 385 | |
| 386 | paramList->Parameters = (struct program_parameter *) |
| 387 | _mesa_realloc(paramList->Parameters, |
| 388 | n * sizeof(struct program_parameter), |
| 389 | (n + 1) * sizeof(struct program_parameter)); |
| 390 | if (!paramList->Parameters) { |
| 391 | /* out of memory */ |
| 392 | paramList->NumParameters = 0; |
| 393 | return -1; |
| 394 | } |
| 395 | else { |
| 396 | paramList->NumParameters = n + 1; |
| 397 | paramList->Parameters[n].Name = _mesa_strdup(name); |
| 398 | paramList->Parameters[n].Type = type; |
| 399 | if (values) |
| 400 | COPY_4V(paramList->Parameters[n].Values, values); |
| 401 | return (GLint) n; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /** |
| 407 | * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement) |
| 408 | * \return index of the new entry in the parameter list |
| 409 | */ |
| 410 | GLint |
| 411 | _mesa_add_named_parameter(struct program_parameter_list *paramList, |
| 412 | const char *name, const GLfloat values[4]) |
| 413 | { |
| 414 | return add_parameter(paramList, name, values, NAMED_PARAMETER); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | /** |
| 419 | * Add a new unnamed constant to the parameter list. |
| 420 | * \param paramList - the parameter list |
| 421 | * \param values - four float values |
| 422 | * \return index of the new parameter. |
| 423 | */ |
| 424 | GLint |
| 425 | _mesa_add_named_constant(struct program_parameter_list *paramList, |
| 426 | const char *name, const GLfloat values[4]) |
| 427 | { |
| 428 | return add_parameter(paramList, name, values, CONSTANT); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /** |
| 433 | * Add a new unnamed constant to the parameter list. |
| 434 | * \param paramList - the parameter list |
| 435 | * \param values - four float values |
| 436 | * \return index of the new parameter. |
| 437 | */ |
| 438 | GLint |
| 439 | _mesa_add_unnamed_constant(struct program_parameter_list *paramList, |
| 440 | const GLfloat values[4]) |
| 441 | { |
| 442 | /* generate a new dummy name */ |
| 443 | static GLuint n = 0; |
| 444 | char name[20]; |
| 445 | _mesa_sprintf(name, "constant%d", n); |
| 446 | n++; |
| 447 | /* store it */ |
| 448 | return add_parameter(paramList, name, values, CONSTANT); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | /** |
| 453 | * Add a new state reference to the parameter list. |
| 454 | * \param paramList - the parameter list |
| 455 | * \param state - an array of 6 state tokens |
| 456 | * |
| 457 | * \return index of the new parameter. |
| 458 | */ |
| 459 | GLint |
| 460 | _mesa_add_state_reference(struct program_parameter_list *paramList, |
| 461 | GLint *stateTokens) |
| 462 | { |
| 463 | /* XXX Should we parse <stateString> here and produce the parameter's |
| 464 | * list of STATE_* tokens here, or in the parser? |
| 465 | */ |
| 466 | GLint a, idx; |
| 467 | |
| 468 | idx = add_parameter(paramList, "Some State", NULL, STATE); |
| 469 | |
| 470 | for (a=0; a<6; a++) |
| 471 | paramList->Parameters[idx].StateIndexes[a] = (enum state_index) stateTokens[a]; |
| 472 | |
| 473 | return idx; |
| 474 | } |
| 475 | |
| 476 | |
| 477 | /** |
| 478 | * Lookup a parameter value by name in the given parameter list. |
| 479 | * \return pointer to the float[4] values. |
| 480 | */ |
| 481 | GLfloat * |
| 482 | _mesa_lookup_parameter_value(struct program_parameter_list *paramList, |
| 483 | GLsizei nameLen, const char *name) |
| 484 | { |
| 485 | GLuint i; |
| 486 | |
| 487 | if (!paramList) |
| 488 | return NULL; |
| 489 | |
| 490 | if (nameLen == -1) { |
| 491 | /* name is null-terminated */ |
| 492 | for (i = 0; i < paramList->NumParameters; i++) { |
| 493 | if (_mesa_strcmp(paramList->Parameters[i].Name, name) == 0) |
| 494 | return paramList->Parameters[i].Values; |
| 495 | } |
| 496 | } |
| 497 | else { |
| 498 | /* name is not null-terminated, use nameLen */ |
| 499 | for (i = 0; i < paramList->NumParameters; i++) { |
| 500 | if (_mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0 |
| 501 | && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen) |
| 502 | return paramList->Parameters[i].Values; |
| 503 | } |
| 504 | } |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | /** |
| 510 | * Lookup a parameter index by name in the given parameter list. |
| 511 | * \return index of parameter in the list. |
| 512 | */ |
| 513 | GLint |
| 514 | _mesa_lookup_parameter_index(struct program_parameter_list *paramList, |
| 515 | GLsizei nameLen, const char *name) |
| 516 | { |
| 517 | GLint i; |
| 518 | |
| 519 | if (!paramList) |
| 520 | return -1; |
| 521 | |
| 522 | if (nameLen == -1) { |
| 523 | /* name is null-terminated */ |
| 524 | for (i = 0; i < (GLint) paramList->NumParameters; i++) { |
| 525 | if (_mesa_strcmp(paramList->Parameters[i].Name, name) == 0) |
| 526 | return i; |
| 527 | } |
| 528 | } |
| 529 | else { |
| 530 | /* name is not null-terminated, use nameLen */ |
| 531 | for (i = 0; i < (GLint) paramList->NumParameters; i++) { |
| 532 | if (_mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0 |
| 533 | && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen) |
| 534 | return i; |
| 535 | } |
| 536 | } |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | /** |
| 542 | * Use the list of tokens in the state[] array to find global GL state |
| 543 | * and return it in <value>. Usually, four values are returned in <value> |
| 544 | * but matrix queries may return as many as 16 values. |
| 545 | * This function is used for ARB vertex/fragment programs. |
| 546 | * The program parser will produce the state[] values. |
| 547 | */ |
| 548 | static void |
| 549 | _mesa_fetch_state(GLcontext *ctx, const enum state_index state[], |
| 550 | GLfloat *value) |
| 551 | { |
| 552 | switch (state[0]) { |
| 553 | case STATE_MATERIAL: |
| 554 | { |
| 555 | /* state[1] is either 0=front or 1=back side */ |
| 556 | const GLuint face = (GLuint) state[1]; |
| 557 | /* state[2] is the material attribute */ |
| 558 | switch (state[2]) { |
| 559 | case STATE_AMBIENT: |
| 560 | if (face == 0) |
| 561 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT]); |
| 562 | else |
| 563 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT]); |
| 564 | return; |
| 565 | case STATE_DIFFUSE: |
| 566 | if (face == 0) |
| 567 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE]); |
| 568 | else |
| 569 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE]); |
| 570 | return; |
| 571 | case STATE_SPECULAR: |
| 572 | if (face == 0) |
| 573 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR]); |
| 574 | else |
| 575 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SPECULAR]); |
| 576 | return; |
| 577 | case STATE_EMISSION: |
| 578 | if (face == 0) |
| 579 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION]); |
| 580 | else |
| 581 | COPY_4V(value, ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION]); |
| 582 | return; |
| 583 | case STATE_SHININESS: |
| 584 | if (face == 0) |
| 585 | value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0]; |
| 586 | else |
| 587 | value[0] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0]; |
| 588 | value[1] = 0.0F; |
| 589 | value[2] = 0.0F; |
| 590 | value[3] = 1.0F; |
| 591 | return; |
| 592 | default: |
| 593 | _mesa_problem(ctx, "Invalid material state in fetch_state"); |
| 594 | return; |
| 595 | } |
Alan Hourihane | 22ae633 | 2004-12-02 13:29:40 +0000 | [diff] [blame] | 596 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 597 | case STATE_LIGHT: |
| 598 | { |
| 599 | /* state[1] is the light number */ |
| 600 | const GLuint ln = (GLuint) state[1]; |
| 601 | /* state[2] is the light attribute */ |
| 602 | switch (state[2]) { |
| 603 | case STATE_AMBIENT: |
| 604 | COPY_4V(value, ctx->Light.Light[ln].Ambient); |
| 605 | return; |
| 606 | case STATE_DIFFUSE: |
| 607 | COPY_4V(value, ctx->Light.Light[ln].Diffuse); |
| 608 | return; |
| 609 | case STATE_SPECULAR: |
| 610 | COPY_4V(value, ctx->Light.Light[ln].Specular); |
| 611 | return; |
| 612 | case STATE_POSITION: |
| 613 | COPY_4V(value, ctx->Light.Light[ln].EyePosition); |
| 614 | return; |
| 615 | case STATE_ATTENUATION: |
| 616 | value[0] = ctx->Light.Light[ln].ConstantAttenuation; |
| 617 | value[1] = ctx->Light.Light[ln].LinearAttenuation; |
| 618 | value[2] = ctx->Light.Light[ln].QuadraticAttenuation; |
| 619 | value[3] = ctx->Light.Light[ln].SpotExponent; |
| 620 | return; |
| 621 | case STATE_SPOT_DIRECTION: |
| 622 | COPY_4V(value, ctx->Light.Light[ln].EyeDirection); |
| 623 | return; |
| 624 | case STATE_HALF: |
| 625 | { |
| 626 | GLfloat eye_z[] = {0, 0, 1}; |
| 627 | |
| 628 | /* Compute infinite half angle vector: |
| 629 | * half-vector = light_position + (0, 0, 1) |
| 630 | * and then normalize. w = 0 |
| 631 | * |
| 632 | * light.EyePosition.w should be 0 for infinite lights. |
| 633 | */ |
| 634 | ADD_3V(value, eye_z, ctx->Light.Light[ln].EyePosition); |
| 635 | NORMALIZE_3FV(value); |
| 636 | value[3] = 0; |
| 637 | } |
| 638 | return; |
| 639 | default: |
| 640 | _mesa_problem(ctx, "Invalid light state in fetch_state"); |
| 641 | return; |
| 642 | } |
| 643 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 644 | case STATE_LIGHTMODEL_AMBIENT: |
| 645 | COPY_4V(value, ctx->Light.Model.Ambient); |
| 646 | return; |
| 647 | case STATE_LIGHTMODEL_SCENECOLOR: |
| 648 | if (state[1] == 0) { |
| 649 | /* front */ |
| 650 | GLint i; |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 651 | for (i = 0; i < 3; i++) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 652 | value[i] = ctx->Light.Model.Ambient[i] |
| 653 | * ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT][i] |
| 654 | + ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_EMISSION][i]; |
| 655 | } |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 656 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE][3]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 657 | } |
| 658 | else { |
| 659 | /* back */ |
| 660 | GLint i; |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 661 | for (i = 0; i < 3; i++) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 662 | value[i] = ctx->Light.Model.Ambient[i] |
| 663 | * ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_AMBIENT][i] |
| 664 | + ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_EMISSION][i]; |
| 665 | } |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 666 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_DIFFUSE][3]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 667 | } |
| 668 | return; |
| 669 | case STATE_LIGHTPROD: |
| 670 | { |
| 671 | const GLuint ln = (GLuint) state[1]; |
| 672 | const GLuint face = (GLuint) state[2]; |
| 673 | GLint i; |
| 674 | ASSERT(face == 0 || face == 1); |
| 675 | switch (state[3]) { |
| 676 | case STATE_AMBIENT: |
| 677 | for (i = 0; i < 3; i++) { |
| 678 | value[i] = ctx->Light.Light[ln].Ambient[i] * |
| 679 | ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_AMBIENT+face][i]; |
| 680 | } |
| 681 | /* [3] = material alpha */ |
| 682 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3]; |
| 683 | return; |
| 684 | case STATE_DIFFUSE: |
| 685 | for (i = 0; i < 3; i++) { |
| 686 | value[i] = ctx->Light.Light[ln].Diffuse[i] * |
| 687 | ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][i]; |
| 688 | } |
| 689 | /* [3] = material alpha */ |
| 690 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3]; |
| 691 | return; |
| 692 | case STATE_SPECULAR: |
| 693 | for (i = 0; i < 3; i++) { |
| 694 | value[i] = ctx->Light.Light[ln].Specular[i] * |
| 695 | ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SPECULAR+face][i]; |
| 696 | } |
| 697 | /* [3] = material alpha */ |
| 698 | value[3] = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_DIFFUSE+face][3]; |
| 699 | return; |
| 700 | default: |
| 701 | _mesa_problem(ctx, "Invalid lightprod state in fetch_state"); |
| 702 | return; |
| 703 | } |
| 704 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 705 | case STATE_TEXGEN: |
| 706 | { |
| 707 | /* state[1] is the texture unit */ |
| 708 | const GLuint unit = (GLuint) state[1]; |
| 709 | /* state[2] is the texgen attribute */ |
| 710 | switch (state[2]) { |
| 711 | case STATE_TEXGEN_EYE_S: |
| 712 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneS); |
| 713 | return; |
| 714 | case STATE_TEXGEN_EYE_T: |
| 715 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneT); |
| 716 | return; |
| 717 | case STATE_TEXGEN_EYE_R: |
| 718 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneR); |
| 719 | return; |
| 720 | case STATE_TEXGEN_EYE_Q: |
| 721 | COPY_4V(value, ctx->Texture.Unit[unit].EyePlaneQ); |
| 722 | return; |
| 723 | case STATE_TEXGEN_OBJECT_S: |
| 724 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneS); |
| 725 | return; |
| 726 | case STATE_TEXGEN_OBJECT_T: |
| 727 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneT); |
| 728 | return; |
| 729 | case STATE_TEXGEN_OBJECT_R: |
| 730 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneR); |
| 731 | return; |
| 732 | case STATE_TEXGEN_OBJECT_Q: |
| 733 | COPY_4V(value, ctx->Texture.Unit[unit].ObjectPlaneQ); |
| 734 | return; |
| 735 | default: |
| 736 | _mesa_problem(ctx, "Invalid texgen state in fetch_state"); |
| 737 | return; |
| 738 | } |
| 739 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 740 | case STATE_TEXENV_COLOR: |
| 741 | { |
| 742 | /* state[1] is the texture unit */ |
| 743 | const GLuint unit = (GLuint) state[1]; |
| 744 | COPY_4V(value, ctx->Texture.Unit[unit].EnvColor); |
| 745 | } |
| 746 | return; |
| 747 | case STATE_FOG_COLOR: |
| 748 | COPY_4V(value, ctx->Fog.Color); |
| 749 | return; |
| 750 | case STATE_FOG_PARAMS: |
| 751 | value[0] = ctx->Fog.Density; |
| 752 | value[1] = ctx->Fog.Start; |
| 753 | value[2] = ctx->Fog.End; |
| 754 | value[3] = 1.0F / (ctx->Fog.End - ctx->Fog.Start); |
| 755 | return; |
| 756 | case STATE_CLIPPLANE: |
| 757 | { |
| 758 | const GLuint plane = (GLuint) state[1]; |
| 759 | COPY_4V(value, ctx->Transform.EyeUserPlane[plane]); |
| 760 | } |
| 761 | return; |
| 762 | case STATE_POINT_SIZE: |
| 763 | value[0] = ctx->Point.Size; |
| 764 | value[1] = ctx->Point.MinSize; |
| 765 | value[2] = ctx->Point.MaxSize; |
| 766 | value[3] = ctx->Point.Threshold; |
| 767 | return; |
| 768 | case STATE_POINT_ATTENUATION: |
| 769 | value[0] = ctx->Point.Params[0]; |
| 770 | value[1] = ctx->Point.Params[1]; |
| 771 | value[2] = ctx->Point.Params[2]; |
| 772 | value[3] = 1.0F; |
| 773 | return; |
| 774 | case STATE_MATRIX: |
| 775 | { |
| 776 | /* state[1] = modelview, projection, texture, etc. */ |
| 777 | /* state[2] = which texture matrix or program matrix */ |
| 778 | /* state[3] = first column to fetch */ |
| 779 | /* state[4] = last column to fetch */ |
| 780 | /* state[5] = transpose, inverse or invtrans */ |
| 781 | |
| 782 | const GLmatrix *matrix; |
| 783 | const enum state_index mat = state[1]; |
| 784 | const GLuint index = (GLuint) state[2]; |
| 785 | const GLuint first = (GLuint) state[3]; |
| 786 | const GLuint last = (GLuint) state[4]; |
| 787 | const enum state_index modifier = state[5]; |
| 788 | const GLfloat *m; |
| 789 | GLuint row, i; |
| 790 | if (mat == STATE_MODELVIEW) { |
| 791 | matrix = ctx->ModelviewMatrixStack.Top; |
| 792 | } |
| 793 | else if (mat == STATE_PROJECTION) { |
| 794 | matrix = ctx->ProjectionMatrixStack.Top; |
| 795 | } |
| 796 | else if (mat == STATE_MVP) { |
| 797 | matrix = &ctx->_ModelProjectMatrix; |
| 798 | } |
| 799 | else if (mat == STATE_TEXTURE) { |
| 800 | matrix = ctx->TextureMatrixStack[index].Top; |
| 801 | } |
| 802 | else if (mat == STATE_PROGRAM) { |
| 803 | matrix = ctx->ProgramMatrixStack[index].Top; |
| 804 | } |
| 805 | else { |
| 806 | _mesa_problem(ctx, "Bad matrix name in _mesa_fetch_state()"); |
| 807 | return; |
| 808 | } |
| 809 | if (modifier == STATE_MATRIX_INVERSE || |
| 810 | modifier == STATE_MATRIX_INVTRANS) { |
Keith Whitwell | 78803b2 | 2005-04-15 12:57:23 +0000 | [diff] [blame] | 811 | /* Be sure inverse is up to date: |
| 812 | */ |
Jouk Jansen | 49b1d95 | 2005-04-18 13:05:24 +0000 | [diff] [blame^] | 813 | _math_matrix_analyse( (GLmatrix*) matrix ); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 814 | m = matrix->inv; |
| 815 | } |
| 816 | else { |
| 817 | m = matrix->m; |
| 818 | } |
| 819 | if (modifier == STATE_MATRIX_TRANSPOSE || |
| 820 | modifier == STATE_MATRIX_INVTRANS) { |
| 821 | for (i = 0, row = first; row <= last; row++) { |
| 822 | value[i++] = m[row * 4 + 0]; |
| 823 | value[i++] = m[row * 4 + 1]; |
| 824 | value[i++] = m[row * 4 + 2]; |
| 825 | value[i++] = m[row * 4 + 3]; |
| 826 | } |
| 827 | } |
| 828 | else { |
| 829 | for (i = 0, row = first; row <= last; row++) { |
| 830 | value[i++] = m[row + 0]; |
| 831 | value[i++] = m[row + 4]; |
| 832 | value[i++] = m[row + 8]; |
| 833 | value[i++] = m[row + 12]; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | return; |
| 838 | case STATE_DEPTH_RANGE: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 839 | value[0] = ctx->Viewport.Near; /* near */ |
| 840 | value[1] = ctx->Viewport.Far; /* far */ |
| 841 | value[2] = ctx->Viewport.Far - ctx->Viewport.Near; /* far - near */ |
| 842 | value[3] = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 843 | return; |
| 844 | case STATE_FRAGMENT_PROGRAM: |
| 845 | { |
| 846 | /* state[1] = {STATE_ENV, STATE_LOCAL} */ |
| 847 | /* state[2] = parameter index */ |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 848 | const int idx = (int) state[2]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 849 | switch (state[1]) { |
| 850 | case STATE_ENV: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 851 | COPY_4V(value, ctx->FragmentProgram.Parameters[idx]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 852 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 853 | case STATE_LOCAL: |
| 854 | COPY_4V(value, ctx->FragmentProgram.Current->Base.LocalParams[idx]); |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 855 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 856 | default: |
| 857 | _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()"); |
| 858 | return; |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 861 | return; |
| 862 | |
| 863 | case STATE_VERTEX_PROGRAM: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 864 | { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 865 | /* state[1] = {STATE_ENV, STATE_LOCAL} */ |
| 866 | /* state[2] = parameter index */ |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 867 | const int idx = (int) state[2]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 868 | switch (state[1]) { |
| 869 | case STATE_ENV: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 870 | COPY_4V(value, ctx->VertexProgram.Parameters[idx]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 871 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 872 | case STATE_LOCAL: |
| 873 | COPY_4V(value, ctx->VertexProgram.Current->Base.LocalParams[idx]); |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 874 | break; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 875 | default: |
| 876 | _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()"); |
| 877 | return; |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 880 | return; |
| 881 | default: |
Brian Paul | 824fdf0 | 2004-06-29 00:00:06 +0000 | [diff] [blame] | 882 | _mesa_problem(ctx, "Invalid state in _mesa_fetch_state"); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 883 | return; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
| 888 | /** |
| 889 | * Loop over all the parameters in a parameter list. If the parameter |
| 890 | * is a GL state reference, look up the current value of that state |
| 891 | * variable and put it into the parameter's Value[4] array. |
| 892 | * This would be called at glBegin time when using a fragment program. |
| 893 | */ |
| 894 | void |
| 895 | _mesa_load_state_parameters(GLcontext *ctx, |
| 896 | struct program_parameter_list *paramList) |
| 897 | { |
| 898 | GLuint i; |
| 899 | |
| 900 | if (!paramList) |
| 901 | return; |
| 902 | |
| 903 | for (i = 0; i < paramList->NumParameters; i++) { |
| 904 | if (paramList->Parameters[i].Type == STATE) { |
| 905 | _mesa_fetch_state(ctx, paramList->Parameters[i].StateIndexes, |
| 906 | paramList->Parameters[i].Values); |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | |
| 912 | |
| 913 | /**********************************************************************/ |
| 914 | /* API functions */ |
| 915 | /**********************************************************************/ |
| 916 | |
| 917 | |
| 918 | /** |
| 919 | * Bind a program (make it current) |
| 920 | * \note Called from the GL API dispatcher by both glBindProgramNV |
| 921 | * and glBindProgramARB. |
| 922 | */ |
| 923 | void GLAPIENTRY |
| 924 | _mesa_BindProgram(GLenum target, GLuint id) |
| 925 | { |
| 926 | struct program *prog; |
| 927 | GET_CURRENT_CONTEXT(ctx); |
| 928 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 929 | |
| 930 | FLUSH_VERTICES(ctx, _NEW_PROGRAM); |
| 931 | |
| 932 | if ((target == GL_VERTEX_PROGRAM_NV |
| 933 | && ctx->Extensions.NV_vertex_program) || |
| 934 | (target == GL_VERTEX_PROGRAM_ARB |
| 935 | && ctx->Extensions.ARB_vertex_program)) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 936 | /*** Vertex program binding ***/ |
| 937 | struct vertex_program *curProg = ctx->VertexProgram.Current; |
| 938 | if (curProg->Base.Id == id) { |
| 939 | /* binding same program - no change */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 940 | return; |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 941 | } |
| 942 | if (curProg->Base.Id != 0) { |
| 943 | /* decrement refcount on previously bound vertex program */ |
| 944 | curProg->Base.RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 945 | /* and delete if refcount goes below one */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 946 | if (curProg->Base.RefCount <= 0) { |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 947 | /* the program ID was already removed from the hash table */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 948 | ctx->Driver.DeleteProgram(ctx, &(curProg->Base)); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
| 952 | else if ((target == GL_FRAGMENT_PROGRAM_NV |
| 953 | && ctx->Extensions.NV_fragment_program) || |
| 954 | (target == GL_FRAGMENT_PROGRAM_ARB |
| 955 | && ctx->Extensions.ARB_fragment_program)) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 956 | /*** Fragment program binding ***/ |
| 957 | struct fragment_program *curProg = ctx->FragmentProgram.Current; |
| 958 | if (curProg->Base.Id == id) { |
| 959 | /* binding same program - no change */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 960 | return; |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 961 | } |
| 962 | if (curProg->Base.Id != 0) { |
| 963 | /* decrement refcount on previously bound fragment program */ |
| 964 | curProg->Base.RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 965 | /* and delete if refcount goes below one */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 966 | if (curProg->Base.RefCount <= 0) { |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 967 | /* the program ID was already removed from the hash table */ |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 968 | ctx->Driver.DeleteProgram(ctx, &(curProg->Base)); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | } |
| 972 | else { |
| 973 | _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)"); |
| 974 | return; |
| 975 | } |
| 976 | |
| 977 | /* NOTE: binding to a non-existant program is not an error. |
| 978 | * That's supposed to be caught in glBegin. |
| 979 | */ |
| 980 | if (id == 0) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 981 | /* Bind default program */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 982 | prog = NULL; |
| 983 | if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) |
| 984 | prog = ctx->Shared->DefaultVertexProgram; |
| 985 | else |
| 986 | prog = ctx->Shared->DefaultFragmentProgram; |
| 987 | } |
| 988 | else { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 989 | /* Bind user program */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 990 | prog = (struct program *) _mesa_HashLookup(ctx->Shared->Programs, id); |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 991 | if (!prog || prog == &_mesa_DummyProgram) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 992 | /* allocate a new program now */ |
| 993 | prog = ctx->Driver.NewProgram(ctx, target, id); |
| 994 | if (!prog) { |
| 995 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB"); |
| 996 | return; |
| 997 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 998 | _mesa_HashInsert(ctx->Shared->Programs, id, prog); |
| 999 | } |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1000 | else if (prog->Target != target) { |
| 1001 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1002 | "glBindProgramNV/ARB(target mismatch)"); |
| 1003 | return; |
| 1004 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /* bind now */ |
| 1008 | if (target == GL_VERTEX_PROGRAM_NV || target == GL_VERTEX_PROGRAM_ARB) { |
| 1009 | ctx->VertexProgram.Current = (struct vertex_program *) prog; |
| 1010 | } |
| 1011 | else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) { |
| 1012 | ctx->FragmentProgram.Current = (struct fragment_program *) prog; |
| 1013 | } |
| 1014 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1015 | /* Never null pointers */ |
| 1016 | ASSERT(ctx->VertexProgram.Current); |
| 1017 | ASSERT(ctx->FragmentProgram.Current); |
| 1018 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1019 | if (prog) |
| 1020 | prog->RefCount++; |
| 1021 | |
| 1022 | if (ctx->Driver.BindProgram) |
| 1023 | ctx->Driver.BindProgram(ctx, target, prog); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | /** |
| 1028 | * Delete a list of programs. |
| 1029 | * \note Not compiled into display lists. |
| 1030 | * \note Called by both glDeleteProgramsNV and glDeleteProgramsARB. |
| 1031 | */ |
| 1032 | void GLAPIENTRY |
| 1033 | _mesa_DeletePrograms(GLsizei n, const GLuint *ids) |
| 1034 | { |
| 1035 | GLint i; |
| 1036 | GET_CURRENT_CONTEXT(ctx); |
| 1037 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); |
| 1038 | |
| 1039 | if (n < 0) { |
| 1040 | _mesa_error( ctx, GL_INVALID_VALUE, "glDeleteProgramsNV" ); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | for (i = 0; i < n; i++) { |
| 1045 | if (ids[i] != 0) { |
| 1046 | struct program *prog = (struct program *) |
| 1047 | _mesa_HashLookup(ctx->Shared->Programs, ids[i]); |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 1048 | if (prog == &_mesa_DummyProgram) { |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1049 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 1050 | } |
| 1051 | else if (prog) { |
| 1052 | /* Unbind program if necessary */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1053 | if (prog->Target == GL_VERTEX_PROGRAM_NV || |
| 1054 | prog->Target == GL_VERTEX_STATE_PROGRAM_NV) { |
| 1055 | if (ctx->VertexProgram.Current && |
| 1056 | ctx->VertexProgram.Current->Base.Id == ids[i]) { |
| 1057 | /* unbind this currently bound program */ |
| 1058 | _mesa_BindProgram(prog->Target, 0); |
| 1059 | } |
| 1060 | } |
| 1061 | else if (prog->Target == GL_FRAGMENT_PROGRAM_NV || |
| 1062 | prog->Target == GL_FRAGMENT_PROGRAM_ARB) { |
| 1063 | if (ctx->FragmentProgram.Current && |
| 1064 | ctx->FragmentProgram.Current->Base.Id == ids[i]) { |
| 1065 | /* unbind this currently bound program */ |
| 1066 | _mesa_BindProgram(prog->Target, 0); |
| 1067 | } |
| 1068 | } |
| 1069 | else { |
| 1070 | _mesa_problem(ctx, "bad target in glDeleteProgramsNV"); |
| 1071 | return; |
| 1072 | } |
Brian Paul | ea2943e | 2005-01-20 04:02:02 +0000 | [diff] [blame] | 1073 | /* The ID is immediately available for re-use now */ |
| 1074 | _mesa_HashRemove(ctx->Shared->Programs, ids[i]); |
| 1075 | prog->RefCount--; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1076 | if (prog->RefCount <= 0) { |
| 1077 | ctx->Driver.DeleteProgram(ctx, prog); |
| 1078 | } |
| 1079 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | /** |
| 1086 | * Generate a list of new program identifiers. |
| 1087 | * \note Not compiled into display lists. |
| 1088 | * \note Called by both glGenProgramsNV and glGenProgramsARB. |
| 1089 | */ |
| 1090 | void GLAPIENTRY |
| 1091 | _mesa_GenPrograms(GLsizei n, GLuint *ids) |
| 1092 | { |
| 1093 | GLuint first; |
| 1094 | GLuint i; |
| 1095 | GET_CURRENT_CONTEXT(ctx); |
| 1096 | ASSERT_OUTSIDE_BEGIN_END(ctx); |
| 1097 | |
| 1098 | if (n < 0) { |
| 1099 | _mesa_error(ctx, GL_INVALID_VALUE, "glGenPrograms"); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | if (!ids) |
| 1104 | return; |
| 1105 | |
| 1106 | first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n); |
| 1107 | |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1108 | /* Insert pointer to dummy program as placeholder */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1109 | for (i = 0; i < (GLuint) n; i++) { |
Brian Paul | 9ca8392 | 2004-10-02 15:16:59 +0000 | [diff] [blame] | 1110 | _mesa_HashInsert(ctx->Shared->Programs, first + i, &_mesa_DummyProgram); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* Return the program names */ |
| 1114 | for (i = 0; i < (GLuint) n; i++) { |
| 1115 | ids[i] = first + i; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | /** |
Brian Paul | 765f1a1 | 2004-09-14 22:28:27 +0000 | [diff] [blame] | 1121 | * Determine if id names a vertex or fragment program. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1122 | * \note Not compiled into display lists. |
| 1123 | * \note Called from both glIsProgramNV and glIsProgramARB. |
| 1124 | * \param id is the program identifier |
| 1125 | * \return GL_TRUE if id is a program, else GL_FALSE. |
| 1126 | */ |
| 1127 | GLboolean GLAPIENTRY |
| 1128 | _mesa_IsProgram(GLuint id) |
| 1129 | { |
| 1130 | GET_CURRENT_CONTEXT(ctx); |
| 1131 | ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE); |
| 1132 | |
| 1133 | if (id == 0) |
| 1134 | return GL_FALSE; |
| 1135 | |
| 1136 | if (_mesa_HashLookup(ctx->Shared->Programs, id)) |
| 1137 | return GL_TRUE; |
| 1138 | else |
| 1139 | return GL_FALSE; |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | /**********************************************************************/ |
| 1145 | /* GL_MESA_program_debug extension */ |
| 1146 | /**********************************************************************/ |
| 1147 | |
| 1148 | |
| 1149 | /* XXX temporary */ |
Daniel Borca | 0a13ceb | 2005-02-14 08:01:59 +0000 | [diff] [blame] | 1150 | GLAPI void GLAPIENTRY |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1151 | glProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback, |
| 1152 | GLvoid *data) |
| 1153 | { |
| 1154 | _mesa_ProgramCallbackMESA(target, callback, data); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | void |
| 1159 | _mesa_ProgramCallbackMESA(GLenum target, GLprogramcallbackMESA callback, |
| 1160 | GLvoid *data) |
| 1161 | { |
| 1162 | GET_CURRENT_CONTEXT(ctx); |
| 1163 | |
| 1164 | switch (target) { |
| 1165 | case GL_FRAGMENT_PROGRAM_ARB: |
| 1166 | if (!ctx->Extensions.ARB_fragment_program) { |
| 1167 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1168 | return; |
| 1169 | } |
| 1170 | ctx->FragmentProgram.Callback = callback; |
| 1171 | ctx->FragmentProgram.CallbackData = data; |
| 1172 | break; |
| 1173 | case GL_FRAGMENT_PROGRAM_NV: |
| 1174 | if (!ctx->Extensions.NV_fragment_program) { |
| 1175 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1176 | return; |
| 1177 | } |
| 1178 | ctx->FragmentProgram.Callback = callback; |
| 1179 | ctx->FragmentProgram.CallbackData = data; |
| 1180 | break; |
| 1181 | case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ |
| 1182 | if (!ctx->Extensions.ARB_vertex_program && |
| 1183 | !ctx->Extensions.NV_vertex_program) { |
| 1184 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1185 | return; |
| 1186 | } |
| 1187 | ctx->VertexProgram.Callback = callback; |
| 1188 | ctx->VertexProgram.CallbackData = data; |
| 1189 | break; |
| 1190 | default: |
| 1191 | _mesa_error(ctx, GL_INVALID_ENUM, "glProgramCallbackMESA(target)"); |
| 1192 | return; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | /* XXX temporary */ |
Daniel Borca | 0a13ceb | 2005-02-14 08:01:59 +0000 | [diff] [blame] | 1198 | GLAPI void GLAPIENTRY |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1199 | glGetProgramRegisterfvMESA(GLenum target, |
| 1200 | GLsizei len, const GLubyte *registerName, |
| 1201 | GLfloat *v) |
| 1202 | { |
| 1203 | _mesa_GetProgramRegisterfvMESA(target, len, registerName, v); |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | void |
| 1208 | _mesa_GetProgramRegisterfvMESA(GLenum target, |
| 1209 | GLsizei len, const GLubyte *registerName, |
| 1210 | GLfloat *v) |
| 1211 | { |
| 1212 | char reg[1000]; |
| 1213 | GET_CURRENT_CONTEXT(ctx); |
| 1214 | |
| 1215 | /* We _should_ be inside glBegin/glEnd */ |
| 1216 | #if 0 |
| 1217 | if (ctx->Driver.CurrentExecPrimitive == PRIM_OUTSIDE_BEGIN_END) { |
| 1218 | _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramRegisterfvMESA"); |
| 1219 | return; |
| 1220 | } |
| 1221 | #endif |
| 1222 | |
| 1223 | /* make null-terminated copy of registerName */ |
| 1224 | len = MIN2((unsigned int) len, sizeof(reg) - 1); |
| 1225 | _mesa_memcpy(reg, registerName, len); |
| 1226 | reg[len] = 0; |
| 1227 | |
| 1228 | switch (target) { |
| 1229 | case GL_VERTEX_PROGRAM_NV: |
| 1230 | if (!ctx->Extensions.ARB_vertex_program && |
| 1231 | !ctx->Extensions.NV_vertex_program) { |
| 1232 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1233 | "glGetProgramRegisterfvMESA(target)"); |
| 1234 | return; |
| 1235 | } |
Brian Paul | 6d460af | 2004-04-23 14:16:46 +0000 | [diff] [blame] | 1236 | if (!ctx->VertexProgram._Enabled) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1237 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1238 | "glGetProgramRegisterfvMESA"); |
| 1239 | return; |
| 1240 | } |
| 1241 | /* GL_NV_vertex_program */ |
| 1242 | if (reg[0] == 'R') { |
| 1243 | /* Temp register */ |
| 1244 | GLint i = _mesa_atoi(reg + 1); |
| 1245 | if (i >= (GLint)ctx->Const.MaxVertexProgramTemps) { |
| 1246 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1247 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1248 | return; |
| 1249 | } |
| 1250 | COPY_4V(v, ctx->VertexProgram.Temporaries[i]); |
| 1251 | } |
| 1252 | else if (reg[0] == 'v' && reg[1] == '[') { |
| 1253 | /* Vertex Input attribute */ |
| 1254 | GLuint i; |
| 1255 | for (i = 0; i < ctx->Const.MaxVertexProgramAttribs; i++) { |
| 1256 | const char *name = _mesa_nv_vertex_input_register_name(i); |
| 1257 | char number[10]; |
| 1258 | sprintf(number, "%d", i); |
| 1259 | if (_mesa_strncmp(reg + 2, name, 4) == 0 || |
| 1260 | _mesa_strncmp(reg + 2, number, _mesa_strlen(number)) == 0) { |
| 1261 | COPY_4V(v, ctx->VertexProgram.Inputs[i]); |
| 1262 | return; |
| 1263 | } |
| 1264 | } |
| 1265 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1266 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1267 | return; |
| 1268 | } |
| 1269 | else if (reg[0] == 'o' && reg[1] == '[') { |
| 1270 | /* Vertex output attribute */ |
| 1271 | } |
| 1272 | /* GL_ARB_vertex_program */ |
| 1273 | else if (_mesa_strncmp(reg, "vertex.", 7) == 0) { |
| 1274 | |
| 1275 | } |
| 1276 | else { |
| 1277 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1278 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1279 | return; |
| 1280 | } |
| 1281 | break; |
| 1282 | case GL_FRAGMENT_PROGRAM_ARB: |
| 1283 | if (!ctx->Extensions.ARB_fragment_program) { |
| 1284 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1285 | "glGetProgramRegisterfvMESA(target)"); |
| 1286 | return; |
| 1287 | } |
Brian Paul | 6d460af | 2004-04-23 14:16:46 +0000 | [diff] [blame] | 1288 | if (!ctx->FragmentProgram._Enabled) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1289 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1290 | "glGetProgramRegisterfvMESA"); |
| 1291 | return; |
| 1292 | } |
| 1293 | /* XXX to do */ |
| 1294 | break; |
| 1295 | case GL_FRAGMENT_PROGRAM_NV: |
| 1296 | if (!ctx->Extensions.NV_fragment_program) { |
| 1297 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1298 | "glGetProgramRegisterfvMESA(target)"); |
| 1299 | return; |
| 1300 | } |
Brian Paul | 6d460af | 2004-04-23 14:16:46 +0000 | [diff] [blame] | 1301 | if (!ctx->FragmentProgram._Enabled) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1302 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1303 | "glGetProgramRegisterfvMESA"); |
| 1304 | return; |
| 1305 | } |
| 1306 | if (reg[0] == 'R') { |
| 1307 | /* Temp register */ |
| 1308 | GLint i = _mesa_atoi(reg + 1); |
| 1309 | if (i >= (GLint)ctx->Const.MaxFragmentProgramTemps) { |
| 1310 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1311 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1312 | return; |
| 1313 | } |
| 1314 | COPY_4V(v, ctx->FragmentProgram.Machine.Temporaries[i]); |
| 1315 | } |
| 1316 | else if (reg[0] == 'f' && reg[1] == '[') { |
| 1317 | /* Fragment input attribute */ |
| 1318 | GLuint i; |
| 1319 | for (i = 0; i < ctx->Const.MaxFragmentProgramAttribs; i++) { |
| 1320 | const char *name = _mesa_nv_fragment_input_register_name(i); |
| 1321 | if (_mesa_strncmp(reg + 2, name, 4) == 0) { |
| 1322 | COPY_4V(v, ctx->FragmentProgram.Machine.Inputs[i]); |
| 1323 | return; |
| 1324 | } |
| 1325 | } |
| 1326 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1327 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1328 | return; |
| 1329 | } |
| 1330 | else if (_mesa_strcmp(reg, "o[COLR]") == 0) { |
| 1331 | /* Fragment output color */ |
| 1332 | COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR]); |
| 1333 | } |
| 1334 | else if (_mesa_strcmp(reg, "o[COLH]") == 0) { |
| 1335 | /* Fragment output color */ |
| 1336 | COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLH]); |
| 1337 | } |
| 1338 | else if (_mesa_strcmp(reg, "o[DEPR]") == 0) { |
| 1339 | /* Fragment output depth */ |
| 1340 | COPY_4V(v, ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR]); |
| 1341 | } |
| 1342 | else { |
| 1343 | /* try user-defined identifiers */ |
| 1344 | const GLfloat *value = _mesa_lookup_parameter_value( |
| 1345 | ctx->FragmentProgram.Current->Parameters, -1, reg); |
| 1346 | if (value) { |
| 1347 | COPY_4V(v, value); |
| 1348 | } |
| 1349 | else { |
| 1350 | _mesa_error(ctx, GL_INVALID_VALUE, |
| 1351 | "glGetProgramRegisterfvMESA(registerName)"); |
| 1352 | return; |
| 1353 | } |
| 1354 | } |
| 1355 | break; |
| 1356 | default: |
| 1357 | _mesa_error(ctx, GL_INVALID_ENUM, |
| 1358 | "glGetProgramRegisterfvMESA(target)"); |
| 1359 | return; |
| 1360 | } |
| 1361 | |
| 1362 | } |