programopt.c revision e5c6a92a12b5cd7db205d72039f58d302b0be9d5
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 programopt.c 27 * Vertex/Fragment program optimizations and transformations for program 28 * options, etc. 29 * 30 * \author Brian Paul 31 */ 32 33 34#include "main/glheader.h" 35#include "main/context.h" 36#include "prog_parameter.h" 37#include "prog_statevars.h" 38#include "program.h" 39#include "programopt.h" 40#include "prog_instruction.h" 41 42 43/** 44 * This function inserts instructions for coordinate modelview * projection 45 * into a vertex program. 46 * May be used to implement the position_invariant option. 47 */ 48static void 49_mesa_insert_mvp_dp4_code(struct gl_context *ctx, struct gl_vertex_program *vprog) 50{ 51 struct prog_instruction *newInst; 52 const GLuint origLen = vprog->Base.NumInstructions; 53 const GLuint newLen = origLen + 4; 54 GLuint i; 55 56 /* 57 * Setup state references for the modelview/projection matrix. 58 * XXX we should check if these state vars are already declared. 59 */ 60 static const gl_state_index mvpState[4][STATE_LENGTH] = { 61 { STATE_MVP_MATRIX, 0, 0, 0, 0 }, /* state.matrix.mvp.row[0] */ 62 { STATE_MVP_MATRIX, 0, 1, 1, 0 }, /* state.matrix.mvp.row[1] */ 63 { STATE_MVP_MATRIX, 0, 2, 2, 0 }, /* state.matrix.mvp.row[2] */ 64 { STATE_MVP_MATRIX, 0, 3, 3, 0 }, /* state.matrix.mvp.row[3] */ 65 }; 66 GLint mvpRef[4]; 67 68 for (i = 0; i < 4; i++) { 69 mvpRef[i] = _mesa_add_state_reference(vprog->Base.Parameters, 70 mvpState[i]); 71 } 72 73 /* Alloc storage for new instructions */ 74 newInst = _mesa_alloc_instructions(newLen); 75 if (!newInst) { 76 _mesa_error(ctx, GL_OUT_OF_MEMORY, 77 "glProgramString(inserting position_invariant code)"); 78 return; 79 } 80 81 /* 82 * Generated instructions: 83 * newInst[0] = DP4 result.position.x, mvp.row[0], vertex.position; 84 * newInst[1] = DP4 result.position.y, mvp.row[1], vertex.position; 85 * newInst[2] = DP4 result.position.z, mvp.row[2], vertex.position; 86 * newInst[3] = DP4 result.position.w, mvp.row[3], vertex.position; 87 */ 88 _mesa_init_instructions(newInst, 4); 89 for (i = 0; i < 4; i++) { 90 newInst[i].Opcode = OPCODE_DP4; 91 newInst[i].DstReg.File = PROGRAM_OUTPUT; 92 newInst[i].DstReg.Index = VERT_RESULT_HPOS; 93 newInst[i].DstReg.WriteMask = (WRITEMASK_X << i); 94 newInst[i].SrcReg[0].File = PROGRAM_STATE_VAR; 95 newInst[i].SrcReg[0].Index = mvpRef[i]; 96 newInst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP; 97 newInst[i].SrcReg[1].File = PROGRAM_INPUT; 98 newInst[i].SrcReg[1].Index = VERT_ATTRIB_POS; 99 newInst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP; 100 } 101 102 /* Append original instructions after new instructions */ 103 _mesa_copy_instructions (newInst + 4, vprog->Base.Instructions, origLen); 104 105 /* free old instructions */ 106 _mesa_free_instructions(vprog->Base.Instructions, origLen); 107 108 /* install new instructions */ 109 vprog->Base.Instructions = newInst; 110 vprog->Base.NumInstructions = newLen; 111 vprog->Base.InputsRead |= VERT_BIT_POS; 112 vprog->Base.OutputsWritten |= BITFIELD64_BIT(VERT_RESULT_HPOS); 113} 114 115 116static void 117_mesa_insert_mvp_mad_code(struct gl_context *ctx, struct gl_vertex_program *vprog) 118{ 119 struct prog_instruction *newInst; 120 const GLuint origLen = vprog->Base.NumInstructions; 121 const GLuint newLen = origLen + 4; 122 GLuint hposTemp; 123 GLuint i; 124 125 /* 126 * Setup state references for the modelview/projection matrix. 127 * XXX we should check if these state vars are already declared. 128 */ 129 static const gl_state_index mvpState[4][STATE_LENGTH] = { 130 { STATE_MVP_MATRIX, 0, 0, 0, STATE_MATRIX_TRANSPOSE }, 131 { STATE_MVP_MATRIX, 0, 1, 1, STATE_MATRIX_TRANSPOSE }, 132 { STATE_MVP_MATRIX, 0, 2, 2, STATE_MATRIX_TRANSPOSE }, 133 { STATE_MVP_MATRIX, 0, 3, 3, STATE_MATRIX_TRANSPOSE }, 134 }; 135 GLint mvpRef[4]; 136 137 for (i = 0; i < 4; i++) { 138 mvpRef[i] = _mesa_add_state_reference(vprog->Base.Parameters, 139 mvpState[i]); 140 } 141 142 /* Alloc storage for new instructions */ 143 newInst = _mesa_alloc_instructions(newLen); 144 if (!newInst) { 145 _mesa_error(ctx, GL_OUT_OF_MEMORY, 146 "glProgramString(inserting position_invariant code)"); 147 return; 148 } 149 150 /* TEMP hposTemp; */ 151 hposTemp = vprog->Base.NumTemporaries++; 152 153 /* 154 * Generated instructions: 155 * emit_op2(p, OPCODE_MUL, tmp, 0, swizzle1(src,X), mat[0]); 156 * emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Y), mat[1], tmp); 157 * emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Z), mat[2], tmp); 158 * emit_op3(p, OPCODE_MAD, dest, 0, swizzle1(src,W), mat[3], tmp); 159 */ 160 _mesa_init_instructions(newInst, 4); 161 162 newInst[0].Opcode = OPCODE_MUL; 163 newInst[0].DstReg.File = PROGRAM_TEMPORARY; 164 newInst[0].DstReg.Index = hposTemp; 165 newInst[0].DstReg.WriteMask = WRITEMASK_XYZW; 166 newInst[0].SrcReg[0].File = PROGRAM_INPUT; 167 newInst[0].SrcReg[0].Index = VERT_ATTRIB_POS; 168 newInst[0].SrcReg[0].Swizzle = SWIZZLE_XXXX; 169 newInst[0].SrcReg[1].File = PROGRAM_STATE_VAR; 170 newInst[0].SrcReg[1].Index = mvpRef[0]; 171 newInst[0].SrcReg[1].Swizzle = SWIZZLE_NOOP; 172 173 for (i = 1; i <= 2; i++) { 174 newInst[i].Opcode = OPCODE_MAD; 175 newInst[i].DstReg.File = PROGRAM_TEMPORARY; 176 newInst[i].DstReg.Index = hposTemp; 177 newInst[i].DstReg.WriteMask = WRITEMASK_XYZW; 178 newInst[i].SrcReg[0].File = PROGRAM_INPUT; 179 newInst[i].SrcReg[0].Index = VERT_ATTRIB_POS; 180 newInst[i].SrcReg[0].Swizzle = MAKE_SWIZZLE4(i,i,i,i); 181 newInst[i].SrcReg[1].File = PROGRAM_STATE_VAR; 182 newInst[i].SrcReg[1].Index = mvpRef[i]; 183 newInst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP; 184 newInst[i].SrcReg[2].File = PROGRAM_TEMPORARY; 185 newInst[i].SrcReg[2].Index = hposTemp; 186 newInst[1].SrcReg[2].Swizzle = SWIZZLE_NOOP; 187 } 188 189 newInst[3].Opcode = OPCODE_MAD; 190 newInst[3].DstReg.File = PROGRAM_OUTPUT; 191 newInst[3].DstReg.Index = VERT_RESULT_HPOS; 192 newInst[3].DstReg.WriteMask = WRITEMASK_XYZW; 193 newInst[3].SrcReg[0].File = PROGRAM_INPUT; 194 newInst[3].SrcReg[0].Index = VERT_ATTRIB_POS; 195 newInst[3].SrcReg[0].Swizzle = SWIZZLE_WWWW; 196 newInst[3].SrcReg[1].File = PROGRAM_STATE_VAR; 197 newInst[3].SrcReg[1].Index = mvpRef[3]; 198 newInst[3].SrcReg[1].Swizzle = SWIZZLE_NOOP; 199 newInst[3].SrcReg[2].File = PROGRAM_TEMPORARY; 200 newInst[3].SrcReg[2].Index = hposTemp; 201 newInst[3].SrcReg[2].Swizzle = SWIZZLE_NOOP; 202 203 204 /* Append original instructions after new instructions */ 205 _mesa_copy_instructions (newInst + 4, vprog->Base.Instructions, origLen); 206 207 /* free old instructions */ 208 _mesa_free_instructions(vprog->Base.Instructions, origLen); 209 210 /* install new instructions */ 211 vprog->Base.Instructions = newInst; 212 vprog->Base.NumInstructions = newLen; 213 vprog->Base.InputsRead |= VERT_BIT_POS; 214 vprog->Base.OutputsWritten |= BITFIELD64_BIT(VERT_RESULT_HPOS); 215} 216 217 218void 219_mesa_insert_mvp_code(struct gl_context *ctx, struct gl_vertex_program *vprog) 220{ 221 if (ctx->mvp_with_dp4) 222 _mesa_insert_mvp_dp4_code( ctx, vprog ); 223 else 224 _mesa_insert_mvp_mad_code( ctx, vprog ); 225} 226 227 228 229 230 231 232/** 233 * Append extra instructions onto the given fragment program to implement 234 * the fog mode specified by fprog->FogOption. 235 * The fragment.fogcoord input is used to compute the fog blend factor. 236 * 237 * XXX with a little work, this function could be adapted to add fog code 238 * to vertex programs too. 239 */ 240void 241_mesa_append_fog_code(struct gl_context *ctx, struct gl_fragment_program *fprog, GLboolean saturate) 242{ 243 static const gl_state_index fogPStateOpt[STATE_LENGTH] 244 = { STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED, 0, 0, 0 }; 245 static const gl_state_index fogColorState[STATE_LENGTH] 246 = { STATE_FOG_COLOR, 0, 0, 0, 0}; 247 struct prog_instruction *newInst, *inst; 248 const GLuint origLen = fprog->Base.NumInstructions; 249 const GLuint newLen = origLen + 5; 250 GLuint i; 251 GLint fogPRefOpt, fogColorRef; /* state references */ 252 GLuint colorTemp, fogFactorTemp; /* temporary registerss */ 253 254 if (fprog->FogOption == GL_NONE) { 255 _mesa_problem(ctx, "_mesa_append_fog_code() called for fragment program" 256 " with FogOption == GL_NONE"); 257 return; 258 } 259 260 /* Alloc storage for new instructions */ 261 newInst = _mesa_alloc_instructions(newLen); 262 if (!newInst) { 263 _mesa_error(ctx, GL_OUT_OF_MEMORY, 264 "glProgramString(inserting fog_option code)"); 265 return; 266 } 267 268 /* Copy orig instructions into new instruction buffer */ 269 _mesa_copy_instructions(newInst, fprog->Base.Instructions, origLen); 270 271 /* PARAM fogParamsRefOpt = internal optimized fog params; */ 272 fogPRefOpt 273 = _mesa_add_state_reference(fprog->Base.Parameters, fogPStateOpt); 274 /* PARAM fogColorRef = state.fog.color; */ 275 fogColorRef 276 = _mesa_add_state_reference(fprog->Base.Parameters, fogColorState); 277 278 /* TEMP colorTemp; */ 279 colorTemp = fprog->Base.NumTemporaries++; 280 /* TEMP fogFactorTemp; */ 281 fogFactorTemp = fprog->Base.NumTemporaries++; 282 283 /* Scan program to find where result.color is written */ 284 inst = newInst; 285 for (i = 0; i < fprog->Base.NumInstructions; i++) { 286 if (inst->Opcode == OPCODE_END) 287 break; 288 if (inst->DstReg.File == PROGRAM_OUTPUT && 289 inst->DstReg.Index == FRAG_RESULT_COLOR) { 290 /* change the instruction to write to colorTemp w/ clamping */ 291 inst->DstReg.File = PROGRAM_TEMPORARY; 292 inst->DstReg.Index = colorTemp; 293 inst->SaturateMode = saturate; 294 /* don't break (may be several writes to result.color) */ 295 } 296 inst++; 297 } 298 assert(inst->Opcode == OPCODE_END); /* we'll overwrite this inst */ 299 300 _mesa_init_instructions(inst, 5); 301 302 /* emit instructions to compute fog blending factor */ 303 /* this is always clamped to [0, 1] regardless of fragment clamping */ 304 if (fprog->FogOption == GL_LINEAR) { 305 /* MAD fogFactorTemp.x, fragment.fogcoord.x, fogPRefOpt.x, fogPRefOpt.y; */ 306 inst->Opcode = OPCODE_MAD; 307 inst->DstReg.File = PROGRAM_TEMPORARY; 308 inst->DstReg.Index = fogFactorTemp; 309 inst->DstReg.WriteMask = WRITEMASK_X; 310 inst->SrcReg[0].File = PROGRAM_INPUT; 311 inst->SrcReg[0].Index = FRAG_ATTRIB_FOGC; 312 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 313 inst->SrcReg[1].File = PROGRAM_STATE_VAR; 314 inst->SrcReg[1].Index = fogPRefOpt; 315 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX; 316 inst->SrcReg[2].File = PROGRAM_STATE_VAR; 317 inst->SrcReg[2].Index = fogPRefOpt; 318 inst->SrcReg[2].Swizzle = SWIZZLE_YYYY; 319 inst->SaturateMode = SATURATE_ZERO_ONE; 320 inst++; 321 } 322 else { 323 ASSERT(fprog->FogOption == GL_EXP || fprog->FogOption == GL_EXP2); 324 /* fogPRefOpt.z = d/ln(2), fogPRefOpt.w = d/sqrt(ln(2) */ 325 /* EXP: MUL fogFactorTemp.x, fogPRefOpt.z, fragment.fogcoord.x; */ 326 /* EXP2: MUL fogFactorTemp.x, fogPRefOpt.w, fragment.fogcoord.x; */ 327 inst->Opcode = OPCODE_MUL; 328 inst->DstReg.File = PROGRAM_TEMPORARY; 329 inst->DstReg.Index = fogFactorTemp; 330 inst->DstReg.WriteMask = WRITEMASK_X; 331 inst->SrcReg[0].File = PROGRAM_STATE_VAR; 332 inst->SrcReg[0].Index = fogPRefOpt; 333 inst->SrcReg[0].Swizzle 334 = (fprog->FogOption == GL_EXP) ? SWIZZLE_ZZZZ : SWIZZLE_WWWW; 335 inst->SrcReg[1].File = PROGRAM_INPUT; 336 inst->SrcReg[1].Index = FRAG_ATTRIB_FOGC; 337 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX; 338 inst++; 339 if (fprog->FogOption == GL_EXP2) { 340 /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */ 341 inst->Opcode = OPCODE_MUL; 342 inst->DstReg.File = PROGRAM_TEMPORARY; 343 inst->DstReg.Index = fogFactorTemp; 344 inst->DstReg.WriteMask = WRITEMASK_X; 345 inst->SrcReg[0].File = PROGRAM_TEMPORARY; 346 inst->SrcReg[0].Index = fogFactorTemp; 347 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 348 inst->SrcReg[1].File = PROGRAM_TEMPORARY; 349 inst->SrcReg[1].Index = fogFactorTemp; 350 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX; 351 inst++; 352 } 353 /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */ 354 inst->Opcode = OPCODE_EX2; 355 inst->DstReg.File = PROGRAM_TEMPORARY; 356 inst->DstReg.Index = fogFactorTemp; 357 inst->DstReg.WriteMask = WRITEMASK_X; 358 inst->SrcReg[0].File = PROGRAM_TEMPORARY; 359 inst->SrcReg[0].Index = fogFactorTemp; 360 inst->SrcReg[0].Negate = NEGATE_XYZW; 361 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 362 inst->SaturateMode = SATURATE_ZERO_ONE; 363 inst++; 364 } 365 /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */ 366 inst->Opcode = OPCODE_LRP; 367 inst->DstReg.File = PROGRAM_OUTPUT; 368 inst->DstReg.Index = FRAG_RESULT_COLOR; 369 inst->DstReg.WriteMask = WRITEMASK_XYZ; 370 inst->SrcReg[0].File = PROGRAM_TEMPORARY; 371 inst->SrcReg[0].Index = fogFactorTemp; 372 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX; 373 inst->SrcReg[1].File = PROGRAM_TEMPORARY; 374 inst->SrcReg[1].Index = colorTemp; 375 inst->SrcReg[1].Swizzle = SWIZZLE_NOOP; 376 inst->SrcReg[2].File = PROGRAM_STATE_VAR; 377 inst->SrcReg[2].Index = fogColorRef; 378 inst->SrcReg[2].Swizzle = SWIZZLE_NOOP; 379 inst++; 380 /* MOV result.color.w, colorTemp.x; # copy alpha */ 381 inst->Opcode = OPCODE_MOV; 382 inst->DstReg.File = PROGRAM_OUTPUT; 383 inst->DstReg.Index = FRAG_RESULT_COLOR; 384 inst->DstReg.WriteMask = WRITEMASK_W; 385 inst->SrcReg[0].File = PROGRAM_TEMPORARY; 386 inst->SrcReg[0].Index = colorTemp; 387 inst->SrcReg[0].Swizzle = SWIZZLE_NOOP; 388 inst++; 389 /* END; */ 390 inst->Opcode = OPCODE_END; 391 inst++; 392 393 /* free old instructions */ 394 _mesa_free_instructions(fprog->Base.Instructions, origLen); 395 396 /* install new instructions */ 397 fprog->Base.Instructions = newInst; 398 fprog->Base.NumInstructions = inst - newInst; 399 fprog->Base.InputsRead |= FRAG_BIT_FOGC; 400 /* XXX do this? fprog->FogOption = GL_NONE; */ 401} 402 403 404 405static GLboolean 406is_texture_instruction(const struct prog_instruction *inst) 407{ 408 switch (inst->Opcode) { 409 case OPCODE_TEX: 410 case OPCODE_TXB: 411 case OPCODE_TXD: 412 case OPCODE_TXL: 413 case OPCODE_TXP: 414 case OPCODE_TXP_NV: 415 return GL_TRUE; 416 default: 417 return GL_FALSE; 418 } 419} 420 421 422/** 423 * Count the number of texure indirections in the given program. 424 * The program's NumTexIndirections field will be updated. 425 * See the GL_ARB_fragment_program spec (issue 24) for details. 426 * XXX we count texture indirections in texenvprogram.c (maybe use this code 427 * instead and elsewhere). 428 */ 429void 430_mesa_count_texture_indirections(struct gl_program *prog) 431{ 432 GLuint indirections = 1; 433 GLbitfield tempsOutput = 0x0; 434 GLbitfield aluTemps = 0x0; 435 GLuint i; 436 437 for (i = 0; i < prog->NumInstructions; i++) { 438 const struct prog_instruction *inst = prog->Instructions + i; 439 440 if (is_texture_instruction(inst)) { 441 if (((inst->SrcReg[0].File == PROGRAM_TEMPORARY) && 442 (tempsOutput & (1 << inst->SrcReg[0].Index))) || 443 ((inst->Opcode != OPCODE_KIL) && 444 (inst->DstReg.File == PROGRAM_TEMPORARY) && 445 (aluTemps & (1 << inst->DstReg.Index)))) 446 { 447 indirections++; 448 tempsOutput = 0x0; 449 aluTemps = 0x0; 450 } 451 } 452 else { 453 GLuint j; 454 for (j = 0; j < 3; j++) { 455 if (inst->SrcReg[j].File == PROGRAM_TEMPORARY) 456 aluTemps |= (1 << inst->SrcReg[j].Index); 457 } 458 if (inst->DstReg.File == PROGRAM_TEMPORARY) 459 aluTemps |= (1 << inst->DstReg.Index); 460 } 461 462 if ((inst->Opcode != OPCODE_KIL) && (inst->DstReg.File == PROGRAM_TEMPORARY)) 463 tempsOutput |= (1 << inst->DstReg.Index); 464 } 465 466 prog->NumTexIndirections = indirections; 467} 468 469 470/** 471 * Count number of texture instructions in given program and update the 472 * program's NumTexInstructions field. 473 */ 474void 475_mesa_count_texture_instructions(struct gl_program *prog) 476{ 477 GLuint i; 478 prog->NumTexInstructions = 0; 479 for (i = 0; i < prog->NumInstructions; i++) { 480 prog->NumTexInstructions += is_texture_instruction(prog->Instructions + i); 481 } 482} 483 484 485/** 486 * Scan/rewrite program to remove reads of custom (output) registers. 487 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING 488 * (for vertex shaders). 489 * In GLSL shaders, varying vars can be read and written. 490 * On some hardware, trying to read an output register causes trouble. 491 * So, rewrite the program to use a temporary register in this case. 492 */ 493void 494_mesa_remove_output_reads(struct gl_program *prog, gl_register_file type) 495{ 496 GLuint i; 497 GLint outputMap[VERT_RESULT_MAX]; 498 GLuint numVaryingReads = 0; 499 GLboolean usedTemps[MAX_PROGRAM_TEMPS]; 500 GLuint firstTemp = 0; 501 502 _mesa_find_used_registers(prog, PROGRAM_TEMPORARY, 503 usedTemps, MAX_PROGRAM_TEMPS); 504 505 assert(type == PROGRAM_VARYING || type == PROGRAM_OUTPUT); 506 assert(prog->Target == GL_VERTEX_PROGRAM_ARB || type != PROGRAM_VARYING); 507 508 for (i = 0; i < VERT_RESULT_MAX; i++) 509 outputMap[i] = -1; 510 511 /* look for instructions which read from varying vars */ 512 for (i = 0; i < prog->NumInstructions; i++) { 513 struct prog_instruction *inst = prog->Instructions + i; 514 const GLuint numSrc = _mesa_num_inst_src_regs(inst->Opcode); 515 GLuint j; 516 for (j = 0; j < numSrc; j++) { 517 if (inst->SrcReg[j].File == type) { 518 /* replace the read with a temp reg */ 519 const GLuint var = inst->SrcReg[j].Index; 520 if (outputMap[var] == -1) { 521 numVaryingReads++; 522 outputMap[var] = _mesa_find_free_register(usedTemps, 523 MAX_PROGRAM_TEMPS, 524 firstTemp); 525 firstTemp = outputMap[var] + 1; 526 } 527 inst->SrcReg[j].File = PROGRAM_TEMPORARY; 528 inst->SrcReg[j].Index = outputMap[var]; 529 } 530 } 531 } 532 533 if (numVaryingReads == 0) 534 return; /* nothing to be done */ 535 536 /* look for instructions which write to the varying vars identified above */ 537 for (i = 0; i < prog->NumInstructions; i++) { 538 struct prog_instruction *inst = prog->Instructions + i; 539 if (inst->DstReg.File == type && 540 outputMap[inst->DstReg.Index] >= 0) { 541 /* change inst to write to the temp reg, instead of the varying */ 542 inst->DstReg.File = PROGRAM_TEMPORARY; 543 inst->DstReg.Index = outputMap[inst->DstReg.Index]; 544 } 545 } 546 547 /* insert new instructions to copy the temp vars to the varying vars */ 548 { 549 struct prog_instruction *inst; 550 GLint endPos, var; 551 552 /* Look for END instruction and insert the new varying writes */ 553 endPos = -1; 554 for (i = 0; i < prog->NumInstructions; i++) { 555 struct prog_instruction *inst = prog->Instructions + i; 556 if (inst->Opcode == OPCODE_END) { 557 endPos = i; 558 _mesa_insert_instructions(prog, i, numVaryingReads); 559 break; 560 } 561 } 562 563 assert(endPos >= 0); 564 565 /* insert new MOV instructions here */ 566 inst = prog->Instructions + endPos; 567 for (var = 0; var < VERT_RESULT_MAX; var++) { 568 if (outputMap[var] >= 0) { 569 /* MOV VAR[var], TEMP[tmp]; */ 570 inst->Opcode = OPCODE_MOV; 571 inst->DstReg.File = type; 572 inst->DstReg.Index = var; 573 inst->SrcReg[0].File = PROGRAM_TEMPORARY; 574 inst->SrcReg[0].Index = outputMap[var]; 575 inst++; 576 } 577 } 578 } 579} 580 581 582/** 583 * Make the given fragment program into a "no-op" shader. 584 * Actually, just copy the incoming fragment color (or texcoord) 585 * to the output color. 586 * This is for debug/test purposes. 587 */ 588void 589_mesa_nop_fragment_program(struct gl_context *ctx, struct gl_fragment_program *prog) 590{ 591 struct prog_instruction *inst; 592 GLuint inputAttr; 593 594 inst = _mesa_alloc_instructions(2); 595 if (!inst) { 596 _mesa_error(ctx, GL_OUT_OF_MEMORY, "_mesa_nop_fragment_program"); 597 return; 598 } 599 600 _mesa_init_instructions(inst, 2); 601 602 inst[0].Opcode = OPCODE_MOV; 603 inst[0].DstReg.File = PROGRAM_OUTPUT; 604 inst[0].DstReg.Index = FRAG_RESULT_COLOR; 605 inst[0].SrcReg[0].File = PROGRAM_INPUT; 606 if (prog->Base.InputsRead & FRAG_BIT_COL0) 607 inputAttr = FRAG_ATTRIB_COL0; 608 else 609 inputAttr = FRAG_ATTRIB_TEX0; 610 inst[0].SrcReg[0].Index = inputAttr; 611 612 inst[1].Opcode = OPCODE_END; 613 614 _mesa_free_instructions(prog->Base.Instructions, 615 prog->Base.NumInstructions); 616 617 prog->Base.Instructions = inst; 618 prog->Base.NumInstructions = 2; 619 prog->Base.InputsRead = 1 << inputAttr; 620 prog->Base.OutputsWritten = BITFIELD64_BIT(FRAG_RESULT_COLOR); 621} 622 623 624/** 625 * \sa _mesa_nop_fragment_program 626 * Replace the given vertex program with a "no-op" program that just 627 * transforms vertex position and emits color. 628 */ 629void 630_mesa_nop_vertex_program(struct gl_context *ctx, struct gl_vertex_program *prog) 631{ 632 struct prog_instruction *inst; 633 GLuint inputAttr; 634 635 /* 636 * Start with a simple vertex program that emits color. 637 */ 638 inst = _mesa_alloc_instructions(2); 639 if (!inst) { 640 _mesa_error(ctx, GL_OUT_OF_MEMORY, "_mesa_nop_vertex_program"); 641 return; 642 } 643 644 _mesa_init_instructions(inst, 2); 645 646 inst[0].Opcode = OPCODE_MOV; 647 inst[0].DstReg.File = PROGRAM_OUTPUT; 648 inst[0].DstReg.Index = VERT_RESULT_COL0; 649 inst[0].SrcReg[0].File = PROGRAM_INPUT; 650 if (prog->Base.InputsRead & VERT_BIT_COLOR0) 651 inputAttr = VERT_ATTRIB_COLOR0; 652 else 653 inputAttr = VERT_ATTRIB_TEX0; 654 inst[0].SrcReg[0].Index = inputAttr; 655 656 inst[1].Opcode = OPCODE_END; 657 658 _mesa_free_instructions(prog->Base.Instructions, 659 prog->Base.NumInstructions); 660 661 prog->Base.Instructions = inst; 662 prog->Base.NumInstructions = 2; 663 prog->Base.InputsRead = 1 << inputAttr; 664 prog->Base.OutputsWritten = BITFIELD64_BIT(VERT_RESULT_COL0); 665 666 /* 667 * Now insert code to do standard modelview/projection transformation. 668 */ 669 _mesa_insert_mvp_code(ctx, prog); 670} 671