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