program.c revision 009250a096cdf2babc8d87defa05309e96b8bba2
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.5.3 4 * 5 * Copyright (C) 1999-2007 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 "main/glheader.h" 33#include "main/context.h" 34#include "main/hash.h" 35#include "main/mfeatures.h" 36#include "program.h" 37#include "prog_cache.h" 38#include "prog_parameter.h" 39#include "prog_instruction.h" 40 41 42/** 43 * A pointer to this dummy program is put into the hash table when 44 * glGenPrograms is called. 45 */ 46struct gl_program _mesa_DummyProgram; 47 48 49/** 50 * Init context's vertex/fragment program state 51 */ 52void 53_mesa_init_program(struct gl_context *ctx) 54{ 55 GLuint i; 56 57 /* 58 * If this assertion fails, we need to increase the field 59 * size for register indexes (see INST_INDEX_BITS). 60 */ 61 ASSERT(ctx->Const.VertexProgram.MaxUniformComponents / 4 62 <= (1 << INST_INDEX_BITS)); 63 ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents / 4 64 <= (1 << INST_INDEX_BITS)); 65 66 ASSERT(ctx->Const.VertexProgram.MaxTemps <= (1 << INST_INDEX_BITS)); 67 ASSERT(ctx->Const.VertexProgram.MaxLocalParams <= (1 << INST_INDEX_BITS)); 68 ASSERT(ctx->Const.FragmentProgram.MaxTemps <= (1 << INST_INDEX_BITS)); 69 ASSERT(ctx->Const.FragmentProgram.MaxLocalParams <= (1 << INST_INDEX_BITS)); 70 71 ASSERT(ctx->Const.VertexProgram.MaxUniformComponents <= 4 * MAX_UNIFORMS); 72 ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents <= 4 * MAX_UNIFORMS); 73 74 ASSERT(ctx->Const.VertexProgram.MaxAddressOffset <= (1 << INST_INDEX_BITS)); 75 ASSERT(ctx->Const.FragmentProgram.MaxAddressOffset <= (1 << INST_INDEX_BITS)); 76 77 /* If this fails, increase prog_instruction::TexSrcUnit size */ 78 ASSERT(MAX_TEXTURE_UNITS <= (1 << 5)); 79 80 /* If this fails, increase prog_instruction::TexSrcTarget size */ 81 ASSERT(NUM_TEXTURE_TARGETS <= (1 << 4)); 82 83 ctx->Program.ErrorPos = -1; 84 ctx->Program.ErrorString = _mesa_strdup(""); 85 86#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program 87 ctx->VertexProgram.Enabled = GL_FALSE; 88 ctx->VertexProgram.PointSizeEnabled = 89 (ctx->API == API_OPENGLES2) ? GL_TRUE : GL_FALSE; 90 ctx->VertexProgram.TwoSideEnabled = GL_FALSE; 91 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, 92 ctx->Shared->DefaultVertexProgram); 93 assert(ctx->VertexProgram.Current); 94 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) { 95 ctx->VertexProgram.TrackMatrix[i] = GL_NONE; 96 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV; 97 } 98 ctx->VertexProgram.Cache = _mesa_new_program_cache(); 99#endif 100 101#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program 102 ctx->FragmentProgram.Enabled = GL_FALSE; 103 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, 104 ctx->Shared->DefaultFragmentProgram); 105 assert(ctx->FragmentProgram.Current); 106 ctx->FragmentProgram.Cache = _mesa_new_program_cache(); 107#endif 108 109#if FEATURE_ARB_geometry_shader4 110 ctx->GeometryProgram.Enabled = GL_FALSE; 111 /* right now by default we don't have a geometry program */ 112 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current, 113 NULL); 114 ctx->GeometryProgram.Cache = _mesa_new_program_cache(); 115#endif 116 117 /* XXX probably move this stuff */ 118#if FEATURE_ATI_fragment_shader 119 ctx->ATIFragmentShader.Enabled = GL_FALSE; 120 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader; 121 assert(ctx->ATIFragmentShader.Current); 122 ctx->ATIFragmentShader.Current->RefCount++; 123#endif 124} 125 126 127/** 128 * Free a context's vertex/fragment program state 129 */ 130void 131_mesa_free_program_data(struct gl_context *ctx) 132{ 133#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program 134 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL); 135 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache); 136#endif 137#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program 138 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL); 139 _mesa_delete_shader_cache(ctx, ctx->FragmentProgram.Cache); 140#endif 141#if FEATURE_ARB_geometry_shader4 142 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current, NULL); 143 _mesa_delete_program_cache(ctx, ctx->GeometryProgram.Cache); 144#endif 145 /* XXX probably move this stuff */ 146#if FEATURE_ATI_fragment_shader 147 if (ctx->ATIFragmentShader.Current) { 148 ctx->ATIFragmentShader.Current->RefCount--; 149 if (ctx->ATIFragmentShader.Current->RefCount <= 0) { 150 free(ctx->ATIFragmentShader.Current); 151 } 152 } 153#endif 154 free((void *) ctx->Program.ErrorString); 155} 156 157 158/** 159 * Update the default program objects in the given context to reference those 160 * specified in the shared state and release those referencing the old 161 * shared state. 162 */ 163void 164_mesa_update_default_objects_program(struct gl_context *ctx) 165{ 166#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program 167 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, 168 ctx->Shared->DefaultVertexProgram); 169 assert(ctx->VertexProgram.Current); 170#endif 171 172#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program 173 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, 174 ctx->Shared->DefaultFragmentProgram); 175 assert(ctx->FragmentProgram.Current); 176#endif 177 178#if FEATURE_ARB_geometry_shader4 179 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current, 180 ctx->Shared->DefaultGeometryProgram); 181#endif 182 183 /* XXX probably move this stuff */ 184#if FEATURE_ATI_fragment_shader 185 if (ctx->ATIFragmentShader.Current) { 186 ctx->ATIFragmentShader.Current->RefCount--; 187 if (ctx->ATIFragmentShader.Current->RefCount <= 0) { 188 free(ctx->ATIFragmentShader.Current); 189 } 190 } 191 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader; 192 assert(ctx->ATIFragmentShader.Current); 193 ctx->ATIFragmentShader.Current->RefCount++; 194#endif 195} 196 197 198/** 199 * Set the vertex/fragment program error state (position and error string). 200 * This is generally called from within the parsers. 201 */ 202void 203_mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string) 204{ 205 ctx->Program.ErrorPos = pos; 206 free((void *) ctx->Program.ErrorString); 207 if (!string) 208 string = ""; 209 ctx->Program.ErrorString = _mesa_strdup(string); 210} 211 212 213/** 214 * Find the line number and column for 'pos' within 'string'. 215 * Return a copy of the line which contains 'pos'. Free the line with 216 * free(). 217 * \param string the program string 218 * \param pos the position within the string 219 * \param line returns the line number corresponding to 'pos'. 220 * \param col returns the column number corresponding to 'pos'. 221 * \return copy of the line containing 'pos'. 222 */ 223const GLubyte * 224_mesa_find_line_column(const GLubyte *string, const GLubyte *pos, 225 GLint *line, GLint *col) 226{ 227 const GLubyte *lineStart = string; 228 const GLubyte *p = string; 229 GLubyte *s; 230 int len; 231 232 *line = 1; 233 234 while (p != pos) { 235 if (*p == (GLubyte) '\n') { 236 (*line)++; 237 lineStart = p + 1; 238 } 239 p++; 240 } 241 242 *col = (pos - lineStart) + 1; 243 244 /* return copy of this line */ 245 while (*p != 0 && *p != '\n') 246 p++; 247 len = p - lineStart; 248 s = malloc(len + 1); 249 memcpy(s, lineStart, len); 250 s[len] = 0; 251 252 return s; 253} 254 255 256/** 257 * Initialize a new vertex/fragment program object. 258 */ 259static struct gl_program * 260_mesa_init_program_struct( struct gl_context *ctx, struct gl_program *prog, 261 GLenum target, GLuint id) 262{ 263 (void) ctx; 264 if (prog) { 265 GLuint i; 266 memset(prog, 0, sizeof(*prog)); 267 prog->Id = id; 268 prog->Target = target; 269 prog->Resident = GL_TRUE; 270 prog->RefCount = 1; 271 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB; 272 273 /* default mapping from samplers to texture units */ 274 for (i = 0; i < MAX_SAMPLERS; i++) 275 prog->SamplerUnits[i] = i; 276 } 277 278 return prog; 279} 280 281 282/** 283 * Initialize a new fragment program object. 284 */ 285struct gl_program * 286_mesa_init_fragment_program( struct gl_context *ctx, struct gl_fragment_program *prog, 287 GLenum target, GLuint id) 288{ 289 if (prog) 290 return _mesa_init_program_struct( ctx, &prog->Base, target, id ); 291 else 292 return NULL; 293} 294 295 296/** 297 * Initialize a new vertex program object. 298 */ 299struct gl_program * 300_mesa_init_vertex_program( struct gl_context *ctx, struct gl_vertex_program *prog, 301 GLenum target, GLuint id) 302{ 303 if (prog) 304 return _mesa_init_program_struct( ctx, &prog->Base, target, id ); 305 else 306 return NULL; 307} 308 309 310/** 311 * Initialize a new geometry program object. 312 */ 313struct gl_program * 314_mesa_init_geometry_program( struct gl_context *ctx, struct gl_geometry_program *prog, 315 GLenum target, GLuint id) 316{ 317 if (prog) 318 return _mesa_init_program_struct( ctx, &prog->Base, target, id ); 319 else 320 return NULL; 321} 322 323 324/** 325 * Allocate and initialize a new fragment/vertex program object but 326 * don't put it into the program hash table. Called via 327 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a 328 * device driver function to implement OO deriviation with additional 329 * types not understood by this function. 330 * 331 * \param ctx context 332 * \param id program id/number 333 * \param target program target/type 334 * \return pointer to new program object 335 */ 336struct gl_program * 337_mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id) 338{ 339 struct gl_program *prog; 340 switch (target) { 341 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */ 342 case GL_VERTEX_STATE_PROGRAM_NV: 343 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), 344 target, id ); 345 break; 346 case GL_FRAGMENT_PROGRAM_NV: 347 case GL_FRAGMENT_PROGRAM_ARB: 348 prog =_mesa_init_fragment_program(ctx, 349 CALLOC_STRUCT(gl_fragment_program), 350 target, id ); 351 break; 352 case MESA_GEOMETRY_PROGRAM: 353 prog = _mesa_init_geometry_program(ctx, 354 CALLOC_STRUCT(gl_geometry_program), 355 target, id); 356 break; 357 default: 358 _mesa_problem(ctx, "bad target in _mesa_new_program"); 359 prog = NULL; 360 } 361 return prog; 362} 363 364 365/** 366 * Delete a program and remove it from the hash table, ignoring the 367 * reference count. 368 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation) 369 * by a device driver function. 370 */ 371void 372_mesa_delete_program(struct gl_context *ctx, struct gl_program *prog) 373{ 374 (void) ctx; 375 ASSERT(prog); 376 ASSERT(prog->RefCount==0); 377 378 if (prog == &_mesa_DummyProgram) 379 return; 380 381 free(prog->String); 382 383 if (prog->Instructions) { 384 _mesa_free_instructions(prog->Instructions, prog->NumInstructions); 385 } 386 if (prog->Parameters) { 387 _mesa_free_parameter_list(prog->Parameters); 388 } 389 390 free(prog); 391} 392 393 394/** 395 * Return the gl_program object for a given ID. 396 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of 397 * casts elsewhere. 398 */ 399struct gl_program * 400_mesa_lookup_program(struct gl_context *ctx, GLuint id) 401{ 402 if (id) 403 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id); 404 else 405 return NULL; 406} 407 408 409/** 410 * Reference counting for vertex/fragment programs 411 * This is normally only called from the _mesa_reference_program() macro 412 * when there's a real pointer change. 413 */ 414void 415_mesa_reference_program_(struct gl_context *ctx, 416 struct gl_program **ptr, 417 struct gl_program *prog) 418{ 419#ifndef NDEBUG 420 assert(ptr); 421 if (*ptr && prog) { 422 /* sanity check */ 423 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB) 424 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB); 425 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB) 426 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB || 427 prog->Target == GL_FRAGMENT_PROGRAM_NV); 428 else if ((*ptr)->Target == MESA_GEOMETRY_PROGRAM) 429 ASSERT(prog->Target == MESA_GEOMETRY_PROGRAM); 430 } 431#endif 432 433 if (*ptr) { 434 GLboolean deleteFlag; 435 436 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/ 437#if 0 438 printf("Program %p ID=%u Target=%s Refcount-- to %d\n", 439 *ptr, (*ptr)->Id, 440 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : 441 ((*ptr)->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")), 442 (*ptr)->RefCount - 1); 443#endif 444 ASSERT((*ptr)->RefCount > 0); 445 (*ptr)->RefCount--; 446 447 deleteFlag = ((*ptr)->RefCount == 0); 448 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/ 449 450 if (deleteFlag) { 451 ASSERT(ctx); 452 ctx->Driver.DeleteProgram(ctx, *ptr); 453 } 454 455 *ptr = NULL; 456 } 457 458 assert(!*ptr); 459 if (prog) { 460 /*_glthread_LOCK_MUTEX(prog->Mutex);*/ 461 prog->RefCount++; 462#if 0 463 printf("Program %p ID=%u Target=%s Refcount++ to %d\n", 464 prog, prog->Id, 465 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : 466 (prog->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")), 467 prog->RefCount); 468#endif 469 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/ 470 } 471 472 *ptr = prog; 473} 474 475 476/** 477 * Return a copy of a program. 478 * XXX Problem here if the program object is actually OO-derivation 479 * made by a device driver. 480 */ 481struct gl_program * 482_mesa_clone_program(struct gl_context *ctx, const struct gl_program *prog) 483{ 484 struct gl_program *clone; 485 486 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id); 487 if (!clone) 488 return NULL; 489 490 assert(clone->Target == prog->Target); 491 assert(clone->RefCount == 1); 492 493 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String); 494 clone->Format = prog->Format; 495 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions); 496 if (!clone->Instructions) { 497 _mesa_reference_program(ctx, &clone, NULL); 498 return NULL; 499 } 500 _mesa_copy_instructions(clone->Instructions, prog->Instructions, 501 prog->NumInstructions); 502 clone->InputsRead = prog->InputsRead; 503 clone->OutputsWritten = prog->OutputsWritten; 504 clone->SamplersUsed = prog->SamplersUsed; 505 clone->ShadowSamplers = prog->ShadowSamplers; 506 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed)); 507 508 if (prog->Parameters) 509 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters); 510 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); 511 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams)); 512 clone->IndirectRegisterFiles = prog->IndirectRegisterFiles; 513 clone->NumInstructions = prog->NumInstructions; 514 clone->NumTemporaries = prog->NumTemporaries; 515 clone->NumParameters = prog->NumParameters; 516 clone->NumAttributes = prog->NumAttributes; 517 clone->NumAddressRegs = prog->NumAddressRegs; 518 clone->NumNativeInstructions = prog->NumNativeInstructions; 519 clone->NumNativeTemporaries = prog->NumNativeTemporaries; 520 clone->NumNativeParameters = prog->NumNativeParameters; 521 clone->NumNativeAttributes = prog->NumNativeAttributes; 522 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs; 523 clone->NumAluInstructions = prog->NumAluInstructions; 524 clone->NumTexInstructions = prog->NumTexInstructions; 525 clone->NumTexIndirections = prog->NumTexIndirections; 526 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions; 527 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions; 528 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections; 529 530 switch (prog->Target) { 531 case GL_VERTEX_PROGRAM_ARB: 532 { 533 const struct gl_vertex_program *vp = gl_vertex_program_const(prog); 534 struct gl_vertex_program *vpc = gl_vertex_program(clone); 535 vpc->IsPositionInvariant = vp->IsPositionInvariant; 536 vpc->IsNVProgram = vp->IsNVProgram; 537 } 538 break; 539 case GL_FRAGMENT_PROGRAM_ARB: 540 { 541 const struct gl_fragment_program *fp = gl_fragment_program_const(prog); 542 struct gl_fragment_program *fpc = gl_fragment_program(clone); 543 fpc->UsesKill = fp->UsesKill; 544 fpc->UsesDFdy = fp->UsesDFdy; 545 fpc->OriginUpperLeft = fp->OriginUpperLeft; 546 fpc->PixelCenterInteger = fp->PixelCenterInteger; 547 } 548 break; 549 case MESA_GEOMETRY_PROGRAM: 550 { 551 const struct gl_geometry_program *gp = gl_geometry_program_const(prog); 552 struct gl_geometry_program *gpc = gl_geometry_program(clone); 553 gpc->VerticesOut = gp->VerticesOut; 554 gpc->InputType = gp->InputType; 555 gpc->OutputType = gp->OutputType; 556 } 557 break; 558 default: 559 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program"); 560 } 561 562 return clone; 563} 564 565 566/** 567 * Insert 'count' NOP instructions at 'start' in the given program. 568 * Adjust branch targets accordingly. 569 */ 570GLboolean 571_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count) 572{ 573 const GLuint origLen = prog->NumInstructions; 574 const GLuint newLen = origLen + count; 575 struct prog_instruction *newInst; 576 GLuint i; 577 578 /* adjust branches */ 579 for (i = 0; i < prog->NumInstructions; i++) { 580 struct prog_instruction *inst = prog->Instructions + i; 581 if (inst->BranchTarget > 0) { 582 if ((GLuint)inst->BranchTarget >= start) { 583 inst->BranchTarget += count; 584 } 585 } 586 } 587 588 /* Alloc storage for new instructions */ 589 newInst = _mesa_alloc_instructions(newLen); 590 if (!newInst) { 591 return GL_FALSE; 592 } 593 594 /* Copy 'start' instructions into new instruction buffer */ 595 _mesa_copy_instructions(newInst, prog->Instructions, start); 596 597 /* init the new instructions */ 598 _mesa_init_instructions(newInst + start, count); 599 600 /* Copy the remaining/tail instructions to new inst buffer */ 601 _mesa_copy_instructions(newInst + start + count, 602 prog->Instructions + start, 603 origLen - start); 604 605 /* free old instructions */ 606 _mesa_free_instructions(prog->Instructions, origLen); 607 608 /* install new instructions */ 609 prog->Instructions = newInst; 610 prog->NumInstructions = newLen; 611 612 return GL_TRUE; 613} 614 615/** 616 * Delete 'count' instructions at 'start' in the given program. 617 * Adjust branch targets accordingly. 618 */ 619GLboolean 620_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count) 621{ 622 const GLuint origLen = prog->NumInstructions; 623 const GLuint newLen = origLen - count; 624 struct prog_instruction *newInst; 625 GLuint i; 626 627 /* adjust branches */ 628 for (i = 0; i < prog->NumInstructions; i++) { 629 struct prog_instruction *inst = prog->Instructions + i; 630 if (inst->BranchTarget > 0) { 631 if (inst->BranchTarget > (GLint) start) { 632 inst->BranchTarget -= count; 633 } 634 } 635 } 636 637 /* Alloc storage for new instructions */ 638 newInst = _mesa_alloc_instructions(newLen); 639 if (!newInst) { 640 return GL_FALSE; 641 } 642 643 /* Copy 'start' instructions into new instruction buffer */ 644 _mesa_copy_instructions(newInst, prog->Instructions, start); 645 646 /* Copy the remaining/tail instructions to new inst buffer */ 647 _mesa_copy_instructions(newInst + start, 648 prog->Instructions + start + count, 649 newLen - start); 650 651 /* free old instructions */ 652 _mesa_free_instructions(prog->Instructions, origLen); 653 654 /* install new instructions */ 655 prog->Instructions = newInst; 656 prog->NumInstructions = newLen; 657 658 return GL_TRUE; 659} 660 661 662/** 663 * Search instructions for registers that match (oldFile, oldIndex), 664 * replacing them with (newFile, newIndex). 665 */ 666static void 667replace_registers(struct prog_instruction *inst, GLuint numInst, 668 GLuint oldFile, GLuint oldIndex, 669 GLuint newFile, GLuint newIndex) 670{ 671 GLuint i, j; 672 for (i = 0; i < numInst; i++) { 673 /* src regs */ 674 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) { 675 if (inst[i].SrcReg[j].File == oldFile && 676 inst[i].SrcReg[j].Index == oldIndex) { 677 inst[i].SrcReg[j].File = newFile; 678 inst[i].SrcReg[j].Index = newIndex; 679 } 680 } 681 /* dst reg */ 682 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) { 683 inst[i].DstReg.File = newFile; 684 inst[i].DstReg.Index = newIndex; 685 } 686 } 687} 688 689 690/** 691 * Search instructions for references to program parameters. When found, 692 * increment the parameter index by 'offset'. 693 * Used when combining programs. 694 */ 695static void 696adjust_param_indexes(struct prog_instruction *inst, GLuint numInst, 697 GLuint offset) 698{ 699 GLuint i, j; 700 for (i = 0; i < numInst; i++) { 701 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) { 702 GLuint f = inst[i].SrcReg[j].File; 703 if (f == PROGRAM_CONSTANT || 704 f == PROGRAM_UNIFORM || 705 f == PROGRAM_STATE_VAR) { 706 inst[i].SrcReg[j].Index += offset; 707 } 708 } 709 } 710} 711 712 713/** 714 * Combine two programs into one. Fix instructions so the outputs of 715 * the first program go to the inputs of the second program. 716 */ 717struct gl_program * 718_mesa_combine_programs(struct gl_context *ctx, 719 const struct gl_program *progA, 720 const struct gl_program *progB) 721{ 722 struct prog_instruction *newInst; 723 struct gl_program *newProg; 724 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */ 725 const GLuint lenB = progB->NumInstructions; 726 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters); 727 const GLuint newLength = lenA + lenB; 728 GLboolean usedTemps[MAX_PROGRAM_TEMPS]; 729 GLuint firstTemp = 0; 730 GLbitfield inputsB; 731 GLuint i; 732 733 ASSERT(progA->Target == progB->Target); 734 735 newInst = _mesa_alloc_instructions(newLength); 736 if (!newInst) 737 return GL_FALSE; 738 739 _mesa_copy_instructions(newInst, progA->Instructions, lenA); 740 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB); 741 742 /* adjust branch / instruction addresses for B's instructions */ 743 for (i = 0; i < lenB; i++) { 744 newInst[lenA + i].BranchTarget += lenA; 745 } 746 747 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0); 748 newProg->Instructions = newInst; 749 newProg->NumInstructions = newLength; 750 751 /* find used temp regs (we may need new temps below) */ 752 _mesa_find_used_registers(newProg, PROGRAM_TEMPORARY, 753 usedTemps, MAX_PROGRAM_TEMPS); 754 755 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) { 756 const struct gl_fragment_program *fprogA, *fprogB; 757 struct gl_fragment_program *newFprog; 758 GLbitfield progB_inputsRead = progB->InputsRead; 759 GLint progB_colorFile, progB_colorIndex; 760 761 fprogA = gl_fragment_program_const(progA); 762 fprogB = gl_fragment_program_const(progB); 763 newFprog = gl_fragment_program(newProg); 764 765 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill; 766 newFprog->UsesDFdy = fprogA->UsesDFdy || fprogB->UsesDFdy; 767 768 /* We'll do a search and replace for instances 769 * of progB_colorFile/progB_colorIndex below... 770 */ 771 progB_colorFile = PROGRAM_INPUT; 772 progB_colorIndex = FRAG_ATTRIB_COL0; 773 774 /* 775 * The fragment program may get color from a state var rather than 776 * a fragment input (vertex output) if it's constant. 777 * See the texenvprogram.c code. 778 * So, search the program's parameter list now to see if the program 779 * gets color from a state var instead of a conventional fragment 780 * input register. 781 */ 782 for (i = 0; i < progB->Parameters->NumParameters; i++) { 783 struct gl_program_parameter *p = &progB->Parameters->Parameters[i]; 784 if (p->Type == PROGRAM_STATE_VAR && 785 p->StateIndexes[0] == STATE_INTERNAL && 786 p->StateIndexes[1] == STATE_CURRENT_ATTRIB && 787 (int) p->StateIndexes[2] == (int) VERT_ATTRIB_COLOR0) { 788 progB_inputsRead |= FRAG_BIT_COL0; 789 progB_colorFile = PROGRAM_STATE_VAR; 790 progB_colorIndex = i; 791 break; 792 } 793 } 794 795 /* Connect color outputs of fprogA to color inputs of fprogB, via a 796 * new temporary register. 797 */ 798 if ((progA->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) && 799 (progB_inputsRead & FRAG_BIT_COL0)) { 800 GLint tempReg = _mesa_find_free_register(usedTemps, MAX_PROGRAM_TEMPS, 801 firstTemp); 802 if (tempReg < 0) { 803 _mesa_problem(ctx, "No free temp regs found in " 804 "_mesa_combine_programs(), using 31"); 805 tempReg = 31; 806 } 807 firstTemp = tempReg + 1; 808 809 /* replace writes to result.color[0] with tempReg */ 810 replace_registers(newInst, lenA, 811 PROGRAM_OUTPUT, FRAG_RESULT_COLOR, 812 PROGRAM_TEMPORARY, tempReg); 813 /* replace reads from the input color with tempReg */ 814 replace_registers(newInst + lenA, lenB, 815 progB_colorFile, progB_colorIndex, /* search for */ 816 PROGRAM_TEMPORARY, tempReg /* replace with */ ); 817 } 818 819 /* compute combined program's InputsRead */ 820 inputsB = progB_inputsRead; 821 if (progA->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) { 822 inputsB &= ~(1 << FRAG_ATTRIB_COL0); 823 } 824 newProg->InputsRead = progA->InputsRead | inputsB; 825 newProg->OutputsWritten = progB->OutputsWritten; 826 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed; 827 } 828 else { 829 /* vertex program */ 830 assert(0); /* XXX todo */ 831 } 832 833 /* 834 * Merge parameters (uniforms, constants, etc) 835 */ 836 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters, 837 progB->Parameters); 838 839 adjust_param_indexes(newInst + lenA, lenB, numParamsA); 840 841 842 return newProg; 843} 844 845 846/** 847 * Populate the 'used' array with flags indicating which registers (TEMPs, 848 * INPUTs, OUTPUTs, etc, are used by the given program. 849 * \param file type of register to scan for 850 * \param used returns true/false flags for in use / free 851 * \param usedSize size of the 'used' array 852 */ 853void 854_mesa_find_used_registers(const struct gl_program *prog, 855 gl_register_file file, 856 GLboolean used[], GLuint usedSize) 857{ 858 GLuint i, j; 859 860 memset(used, 0, usedSize); 861 862 for (i = 0; i < prog->NumInstructions; i++) { 863 const struct prog_instruction *inst = prog->Instructions + i; 864 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode); 865 866 if (inst->DstReg.File == file) { 867 ASSERT(inst->DstReg.Index < usedSize); 868 if(inst->DstReg.Index < usedSize) 869 used[inst->DstReg.Index] = GL_TRUE; 870 } 871 872 for (j = 0; j < n; j++) { 873 if (inst->SrcReg[j].File == file) { 874 ASSERT(inst->SrcReg[j].Index < usedSize); 875 if(inst->SrcReg[j].Index < usedSize) 876 used[inst->SrcReg[j].Index] = GL_TRUE; 877 } 878 } 879 } 880} 881 882 883/** 884 * Scan the given 'used' register flag array for the first entry 885 * that's >= firstReg. 886 * \param used vector of flags indicating registers in use (as returned 887 * by _mesa_find_used_registers()) 888 * \param usedSize size of the 'used' array 889 * \param firstReg first register to start searching at 890 * \return index of unused register, or -1 if none. 891 */ 892GLint 893_mesa_find_free_register(const GLboolean used[], 894 GLuint usedSize, GLuint firstReg) 895{ 896 GLuint i; 897 898 assert(firstReg < usedSize); 899 900 for (i = firstReg; i < usedSize; i++) 901 if (!used[i]) 902 return i; 903 904 return -1; 905} 906 907 908 909/** 910 * Check if the given register index is valid (doesn't exceed implementation- 911 * dependent limits). 912 * \return GL_TRUE if OK, GL_FALSE if bad index 913 */ 914GLboolean 915_mesa_valid_register_index(const struct gl_context *ctx, 916 gl_shader_type shaderType, 917 gl_register_file file, GLint index) 918{ 919 const struct gl_program_constants *c; 920 921 switch (shaderType) { 922 case MESA_SHADER_VERTEX: 923 c = &ctx->Const.VertexProgram; 924 break; 925 case MESA_SHADER_FRAGMENT: 926 c = &ctx->Const.FragmentProgram; 927 break; 928 case MESA_SHADER_GEOMETRY: 929 c = &ctx->Const.GeometryProgram; 930 break; 931 default: 932 _mesa_problem(ctx, 933 "unexpected shader type in _mesa_valid_register_index()"); 934 return GL_FALSE; 935 } 936 937 switch (file) { 938 case PROGRAM_UNDEFINED: 939 return GL_TRUE; /* XXX or maybe false? */ 940 941 case PROGRAM_TEMPORARY: 942 return index >= 0 && index < c->MaxTemps; 943 944 case PROGRAM_ENV_PARAM: 945 return index >= 0 && index < c->MaxEnvParams; 946 947 case PROGRAM_LOCAL_PARAM: 948 return index >= 0 && index < c->MaxLocalParams; 949 950 case PROGRAM_NAMED_PARAM: 951 return index >= 0 && index < c->MaxParameters; 952 953 case PROGRAM_UNIFORM: 954 case PROGRAM_STATE_VAR: 955 /* aka constant buffer */ 956 return index >= 0 && index < c->MaxUniformComponents / 4; 957 958 case PROGRAM_CONSTANT: 959 /* constant buffer w/ possible relative negative addressing */ 960 return (index > (int) c->MaxUniformComponents / -4 && 961 index < c->MaxUniformComponents / 4); 962 963 case PROGRAM_INPUT: 964 if (index < 0) 965 return GL_FALSE; 966 967 switch (shaderType) { 968 case MESA_SHADER_VERTEX: 969 return index < VERT_ATTRIB_GENERIC0 + c->MaxAttribs; 970 case MESA_SHADER_FRAGMENT: 971 return index < FRAG_ATTRIB_VAR0 + ctx->Const.MaxVarying; 972 case MESA_SHADER_GEOMETRY: 973 return index < GEOM_ATTRIB_VAR0 + ctx->Const.MaxVarying; 974 default: 975 return GL_FALSE; 976 } 977 978 case PROGRAM_OUTPUT: 979 if (index < 0) 980 return GL_FALSE; 981 982 switch (shaderType) { 983 case MESA_SHADER_VERTEX: 984 return index < VERT_RESULT_VAR0 + ctx->Const.MaxVarying; 985 case MESA_SHADER_FRAGMENT: 986 return index < FRAG_RESULT_DATA0 + ctx->Const.MaxDrawBuffers; 987 case MESA_SHADER_GEOMETRY: 988 return index < GEOM_RESULT_VAR0 + ctx->Const.MaxVarying; 989 default: 990 return GL_FALSE; 991 } 992 993 case PROGRAM_ADDRESS: 994 return index >= 0 && index < c->MaxAddressRegs; 995 996 default: 997 _mesa_problem(ctx, 998 "unexpected register file in _mesa_valid_register_index()"); 999 return GL_FALSE; 1000 } 1001} 1002 1003 1004 1005/** 1006 * "Post-process" a GPU program. This is intended to be used for debugging. 1007 * Example actions include no-op'ing instructions or changing instruction 1008 * behaviour. 1009 */ 1010void 1011_mesa_postprocess_program(struct gl_context *ctx, struct gl_program *prog) 1012{ 1013 static const GLfloat white[4] = { 0.5, 0.5, 0.5, 0.5 }; 1014 GLuint i; 1015 GLuint whiteSwizzle; 1016 GLint whiteIndex = _mesa_add_unnamed_constant(prog->Parameters, 1017 (gl_constant_value *) white, 1018 4, &whiteSwizzle); 1019 1020 (void) whiteIndex; 1021 1022 for (i = 0; i < prog->NumInstructions; i++) { 1023 struct prog_instruction *inst = prog->Instructions + i; 1024 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode); 1025 1026 (void) n; 1027 1028 if (_mesa_is_tex_instruction(inst->Opcode)) { 1029#if 0 1030 /* replace TEX/TXP/TXB with MOV */ 1031 inst->Opcode = OPCODE_MOV; 1032 inst->DstReg.WriteMask = WRITEMASK_XYZW; 1033 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW; 1034 inst->SrcReg[0].Negate = NEGATE_NONE; 1035#endif 1036 1037#if 0 1038 /* disable shadow texture mode */ 1039 inst->TexShadow = 0; 1040#endif 1041 } 1042 1043 if (inst->Opcode == OPCODE_TXP) { 1044#if 0 1045 inst->Opcode = OPCODE_MOV; 1046 inst->DstReg.WriteMask = WRITEMASK_XYZW; 1047 inst->SrcReg[0].File = PROGRAM_CONSTANT; 1048 inst->SrcReg[0].Index = whiteIndex; 1049 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW; 1050 inst->SrcReg[0].Negate = NEGATE_NONE; 1051#endif 1052#if 0 1053 inst->TexShadow = 0; 1054#endif 1055#if 0 1056 inst->Opcode = OPCODE_TEX; 1057 inst->TexShadow = 0; 1058#endif 1059 } 1060 1061 } 1062} 1063