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