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