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