2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
27 * Vertex/Fragment program optimizations and transformations for program
34 #include "main/glheader.h"
35 #include "main/context.h"
36 #include "prog_parameter.h"
37 #include "prog_statevars.h"
39 #include "programopt.h"
40 #include "prog_instruction.h"
44 * This function inserts instructions for coordinate modelview * projection
45 * into a vertex program.
46 * May be used to implement the position_invariant option.
49 _mesa_insert_mvp_dp4_code(struct gl_context
*ctx
, struct gl_program
*vprog
)
51 struct prog_instruction
*newInst
;
52 const GLuint origLen
= vprog
->NumInstructions
;
53 const GLuint newLen
= origLen
+ 4;
57 * Setup state references for the modelview/projection matrix.
58 * XXX we should check if these state vars are already declared.
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] */
68 for (i
= 0; i
< 4; i
++) {
69 mvpRef
[i
] = _mesa_add_state_reference(vprog
->Parameters
, mvpState
[i
]);
72 /* Alloc storage for new instructions */
73 newInst
= _mesa_alloc_instructions(newLen
);
75 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
76 "glProgramString(inserting position_invariant code)");
81 * Generated instructions:
82 * newInst[0] = DP4 result.position.x, mvp.row[0], vertex.position;
83 * newInst[1] = DP4 result.position.y, mvp.row[1], vertex.position;
84 * newInst[2] = DP4 result.position.z, mvp.row[2], vertex.position;
85 * newInst[3] = DP4 result.position.w, mvp.row[3], vertex.position;
87 _mesa_init_instructions(newInst
, 4);
88 for (i
= 0; i
< 4; i
++) {
89 newInst
[i
].Opcode
= OPCODE_DP4
;
90 newInst
[i
].DstReg
.File
= PROGRAM_OUTPUT
;
91 newInst
[i
].DstReg
.Index
= VARYING_SLOT_POS
;
92 newInst
[i
].DstReg
.WriteMask
= (WRITEMASK_X
<< i
);
93 newInst
[i
].SrcReg
[0].File
= PROGRAM_STATE_VAR
;
94 newInst
[i
].SrcReg
[0].Index
= mvpRef
[i
];
95 newInst
[i
].SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
96 newInst
[i
].SrcReg
[1].File
= PROGRAM_INPUT
;
97 newInst
[i
].SrcReg
[1].Index
= VERT_ATTRIB_POS
;
98 newInst
[i
].SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
101 /* Append original instructions after new instructions */
102 _mesa_copy_instructions (newInst
+ 4, vprog
->Instructions
, origLen
);
104 /* free old instructions */
105 _mesa_free_instructions(vprog
->Instructions
, origLen
);
107 /* install new instructions */
108 vprog
->Instructions
= newInst
;
109 vprog
->NumInstructions
= newLen
;
110 vprog
->InputsRead
|= VERT_BIT_POS
;
111 vprog
->OutputsWritten
|= BITFIELD64_BIT(VARYING_SLOT_POS
);
116 _mesa_insert_mvp_mad_code(struct gl_context
*ctx
, struct gl_program
*vprog
)
118 struct prog_instruction
*newInst
;
119 const GLuint origLen
= vprog
->NumInstructions
;
120 const GLuint newLen
= origLen
+ 4;
125 * Setup state references for the modelview/projection matrix.
126 * XXX we should check if these state vars are already declared.
128 static const gl_state_index mvpState
[4][STATE_LENGTH
] = {
129 { STATE_MVP_MATRIX
, 0, 0, 0, STATE_MATRIX_TRANSPOSE
},
130 { STATE_MVP_MATRIX
, 0, 1, 1, STATE_MATRIX_TRANSPOSE
},
131 { STATE_MVP_MATRIX
, 0, 2, 2, STATE_MATRIX_TRANSPOSE
},
132 { STATE_MVP_MATRIX
, 0, 3, 3, STATE_MATRIX_TRANSPOSE
},
136 for (i
= 0; i
< 4; i
++) {
137 mvpRef
[i
] = _mesa_add_state_reference(vprog
->Parameters
, mvpState
[i
]);
140 /* Alloc storage for new instructions */
141 newInst
= _mesa_alloc_instructions(newLen
);
143 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
144 "glProgramString(inserting position_invariant code)");
149 hposTemp
= vprog
->NumTemporaries
++;
152 * Generated instructions:
153 * emit_op2(p, OPCODE_MUL, tmp, 0, swizzle1(src,X), mat[0]);
154 * emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Y), mat[1], tmp);
155 * emit_op3(p, OPCODE_MAD, tmp, 0, swizzle1(src,Z), mat[2], tmp);
156 * emit_op3(p, OPCODE_MAD, dest, 0, swizzle1(src,W), mat[3], tmp);
158 _mesa_init_instructions(newInst
, 4);
160 newInst
[0].Opcode
= OPCODE_MUL
;
161 newInst
[0].DstReg
.File
= PROGRAM_TEMPORARY
;
162 newInst
[0].DstReg
.Index
= hposTemp
;
163 newInst
[0].DstReg
.WriteMask
= WRITEMASK_XYZW
;
164 newInst
[0].SrcReg
[0].File
= PROGRAM_INPUT
;
165 newInst
[0].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
166 newInst
[0].SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
167 newInst
[0].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
168 newInst
[0].SrcReg
[1].Index
= mvpRef
[0];
169 newInst
[0].SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
171 for (i
= 1; i
<= 2; i
++) {
172 newInst
[i
].Opcode
= OPCODE_MAD
;
173 newInst
[i
].DstReg
.File
= PROGRAM_TEMPORARY
;
174 newInst
[i
].DstReg
.Index
= hposTemp
;
175 newInst
[i
].DstReg
.WriteMask
= WRITEMASK_XYZW
;
176 newInst
[i
].SrcReg
[0].File
= PROGRAM_INPUT
;
177 newInst
[i
].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
178 newInst
[i
].SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(i
,i
,i
,i
);
179 newInst
[i
].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
180 newInst
[i
].SrcReg
[1].Index
= mvpRef
[i
];
181 newInst
[i
].SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
182 newInst
[i
].SrcReg
[2].File
= PROGRAM_TEMPORARY
;
183 newInst
[i
].SrcReg
[2].Index
= hposTemp
;
184 newInst
[1].SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
187 newInst
[3].Opcode
= OPCODE_MAD
;
188 newInst
[3].DstReg
.File
= PROGRAM_OUTPUT
;
189 newInst
[3].DstReg
.Index
= VARYING_SLOT_POS
;
190 newInst
[3].DstReg
.WriteMask
= WRITEMASK_XYZW
;
191 newInst
[3].SrcReg
[0].File
= PROGRAM_INPUT
;
192 newInst
[3].SrcReg
[0].Index
= VERT_ATTRIB_POS
;
193 newInst
[3].SrcReg
[0].Swizzle
= SWIZZLE_WWWW
;
194 newInst
[3].SrcReg
[1].File
= PROGRAM_STATE_VAR
;
195 newInst
[3].SrcReg
[1].Index
= mvpRef
[3];
196 newInst
[3].SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
197 newInst
[3].SrcReg
[2].File
= PROGRAM_TEMPORARY
;
198 newInst
[3].SrcReg
[2].Index
= hposTemp
;
199 newInst
[3].SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
202 /* Append original instructions after new instructions */
203 _mesa_copy_instructions (newInst
+ 4, vprog
->Instructions
, origLen
);
205 /* free old instructions */
206 _mesa_free_instructions(vprog
->Instructions
, origLen
);
208 /* install new instructions */
209 vprog
->Instructions
= newInst
;
210 vprog
->NumInstructions
= newLen
;
211 vprog
->InputsRead
|= VERT_BIT_POS
;
212 vprog
->OutputsWritten
|= BITFIELD64_BIT(VARYING_SLOT_POS
);
217 _mesa_insert_mvp_code(struct gl_context
*ctx
, struct gl_program
*vprog
)
219 if (ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].OptimizeForAOS
)
220 _mesa_insert_mvp_dp4_code( ctx
, vprog
);
222 _mesa_insert_mvp_mad_code( ctx
, vprog
);
231 * Append instructions to implement fog
233 * The \c fragment.fogcoord input is used to compute the fog blend factor.
235 * \param ctx The GL context
236 * \param fprog Fragment program that fog instructions will be appended to.
237 * \param fog_mode Fog mode. One of \c GL_EXP, \c GL_EXP2, or \c GL_LINEAR.
238 * \param saturate True if writes to color outputs should be clamped to [0, 1]
241 * This function sets \c VARYING_BIT_FOGC in \c fprog->Base.InputsRead.
243 * \todo With a little work, this function could be adapted to add fog code
244 * to vertex programs too.
247 _mesa_append_fog_code(struct gl_context
*ctx
,
248 struct gl_fragment_program
*fprog
, GLenum fog_mode
,
251 static const gl_state_index fogPStateOpt
[STATE_LENGTH
]
252 = { STATE_INTERNAL
, STATE_FOG_PARAMS_OPTIMIZED
, 0, 0, 0 };
253 static const gl_state_index fogColorState
[STATE_LENGTH
]
254 = { STATE_FOG_COLOR
, 0, 0, 0, 0};
255 struct prog_instruction
*newInst
, *inst
;
256 const GLuint origLen
= fprog
->Base
.NumInstructions
;
257 const GLuint newLen
= origLen
+ 5;
259 GLint fogPRefOpt
, fogColorRef
; /* state references */
260 GLuint colorTemp
, fogFactorTemp
; /* temporary registerss */
262 if (fog_mode
== GL_NONE
) {
263 _mesa_problem(ctx
, "_mesa_append_fog_code() called for fragment program"
264 " with fog_mode == GL_NONE");
268 if (!(fprog
->Base
.OutputsWritten
& (1 << FRAG_RESULT_COLOR
))) {
269 /* program doesn't output color, so nothing to do */
273 /* Alloc storage for new instructions */
274 newInst
= _mesa_alloc_instructions(newLen
);
276 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
277 "glProgramString(inserting fog_option code)");
281 /* Copy orig instructions into new instruction buffer */
282 _mesa_copy_instructions(newInst
, fprog
->Base
.Instructions
, origLen
);
284 /* PARAM fogParamsRefOpt = internal optimized fog params; */
286 = _mesa_add_state_reference(fprog
->Base
.Parameters
, fogPStateOpt
);
287 /* PARAM fogColorRef = state.fog.color; */
289 = _mesa_add_state_reference(fprog
->Base
.Parameters
, fogColorState
);
291 /* TEMP colorTemp; */
292 colorTemp
= fprog
->Base
.NumTemporaries
++;
293 /* TEMP fogFactorTemp; */
294 fogFactorTemp
= fprog
->Base
.NumTemporaries
++;
296 /* Scan program to find where result.color is written */
298 for (i
= 0; i
< fprog
->Base
.NumInstructions
; i
++) {
299 if (inst
->Opcode
== OPCODE_END
)
301 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
&&
302 inst
->DstReg
.Index
== FRAG_RESULT_COLOR
) {
303 /* change the instruction to write to colorTemp w/ clamping */
304 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
305 inst
->DstReg
.Index
= colorTemp
;
306 inst
->Saturate
= saturate
;
307 /* don't break (may be several writes to result.color) */
311 assert(inst
->Opcode
== OPCODE_END
); /* we'll overwrite this inst */
313 _mesa_init_instructions(inst
, 5);
315 /* emit instructions to compute fog blending factor */
316 /* this is always clamped to [0, 1] regardless of fragment clamping */
317 if (fog_mode
== GL_LINEAR
) {
318 /* MAD fogFactorTemp.x, fragment.fogcoord.x, fogPRefOpt.x, fogPRefOpt.y; */
319 inst
->Opcode
= OPCODE_MAD
;
320 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
321 inst
->DstReg
.Index
= fogFactorTemp
;
322 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
323 inst
->SrcReg
[0].File
= PROGRAM_INPUT
;
324 inst
->SrcReg
[0].Index
= VARYING_SLOT_FOGC
;
325 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
326 inst
->SrcReg
[1].File
= PROGRAM_STATE_VAR
;
327 inst
->SrcReg
[1].Index
= fogPRefOpt
;
328 inst
->SrcReg
[1].Swizzle
= SWIZZLE_XXXX
;
329 inst
->SrcReg
[2].File
= PROGRAM_STATE_VAR
;
330 inst
->SrcReg
[2].Index
= fogPRefOpt
;
331 inst
->SrcReg
[2].Swizzle
= SWIZZLE_YYYY
;
332 inst
->Saturate
= GL_TRUE
;
336 assert(fog_mode
== GL_EXP
|| fog_mode
== GL_EXP2
);
337 /* fogPRefOpt.z = d/ln(2), fogPRefOpt.w = d/sqrt(ln(2) */
338 /* EXP: MUL fogFactorTemp.x, fogPRefOpt.z, fragment.fogcoord.x; */
339 /* EXP2: MUL fogFactorTemp.x, fogPRefOpt.w, fragment.fogcoord.x; */
340 inst
->Opcode
= OPCODE_MUL
;
341 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
342 inst
->DstReg
.Index
= fogFactorTemp
;
343 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
344 inst
->SrcReg
[0].File
= PROGRAM_STATE_VAR
;
345 inst
->SrcReg
[0].Index
= fogPRefOpt
;
346 inst
->SrcReg
[0].Swizzle
347 = (fog_mode
== GL_EXP
) ? SWIZZLE_ZZZZ
: SWIZZLE_WWWW
;
348 inst
->SrcReg
[1].File
= PROGRAM_INPUT
;
349 inst
->SrcReg
[1].Index
= VARYING_SLOT_FOGC
;
350 inst
->SrcReg
[1].Swizzle
= SWIZZLE_XXXX
;
352 if (fog_mode
== GL_EXP2
) {
353 /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */
354 inst
->Opcode
= OPCODE_MUL
;
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].Swizzle
= SWIZZLE_XXXX
;
361 inst
->SrcReg
[1].File
= PROGRAM_TEMPORARY
;
362 inst
->SrcReg
[1].Index
= fogFactorTemp
;
363 inst
->SrcReg
[1].Swizzle
= SWIZZLE_XXXX
;
366 /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */
367 inst
->Opcode
= OPCODE_EX2
;
368 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
369 inst
->DstReg
.Index
= fogFactorTemp
;
370 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
371 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
372 inst
->SrcReg
[0].Index
= fogFactorTemp
;
373 inst
->SrcReg
[0].Negate
= NEGATE_XYZW
;
374 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
375 inst
->Saturate
= GL_TRUE
;
378 /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */
379 inst
->Opcode
= OPCODE_LRP
;
380 inst
->DstReg
.File
= PROGRAM_OUTPUT
;
381 inst
->DstReg
.Index
= FRAG_RESULT_COLOR
;
382 inst
->DstReg
.WriteMask
= WRITEMASK_XYZ
;
383 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
384 inst
->SrcReg
[0].Index
= fogFactorTemp
;
385 inst
->SrcReg
[0].Swizzle
= SWIZZLE_XXXX
;
386 inst
->SrcReg
[1].File
= PROGRAM_TEMPORARY
;
387 inst
->SrcReg
[1].Index
= colorTemp
;
388 inst
->SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
389 inst
->SrcReg
[2].File
= PROGRAM_STATE_VAR
;
390 inst
->SrcReg
[2].Index
= fogColorRef
;
391 inst
->SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
393 /* MOV result.color.w, colorTemp.x; # copy alpha */
394 inst
->Opcode
= OPCODE_MOV
;
395 inst
->DstReg
.File
= PROGRAM_OUTPUT
;
396 inst
->DstReg
.Index
= FRAG_RESULT_COLOR
;
397 inst
->DstReg
.WriteMask
= WRITEMASK_W
;
398 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
399 inst
->SrcReg
[0].Index
= colorTemp
;
400 inst
->SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
403 inst
->Opcode
= OPCODE_END
;
406 /* free old instructions */
407 _mesa_free_instructions(fprog
->Base
.Instructions
, origLen
);
409 /* install new instructions */
410 fprog
->Base
.Instructions
= newInst
;
411 fprog
->Base
.NumInstructions
= inst
- newInst
;
412 fprog
->Base
.InputsRead
|= VARYING_BIT_FOGC
;
413 assert(fprog
->Base
.OutputsWritten
& (1 << FRAG_RESULT_COLOR
));
419 is_texture_instruction(const struct prog_instruction
*inst
)
421 switch (inst
->Opcode
) {
435 * Count the number of texure indirections in the given program.
436 * The program's NumTexIndirections field will be updated.
437 * See the GL_ARB_fragment_program spec (issue 24) for details.
438 * XXX we count texture indirections in texenvprogram.c (maybe use this code
439 * instead and elsewhere).
442 _mesa_count_texture_indirections(struct gl_program
*prog
)
444 GLuint indirections
= 1;
445 GLbitfield tempsOutput
= 0x0;
446 GLbitfield aluTemps
= 0x0;
449 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
450 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
452 if (is_texture_instruction(inst
)) {
453 if (((inst
->SrcReg
[0].File
== PROGRAM_TEMPORARY
) &&
454 (tempsOutput
& (1 << inst
->SrcReg
[0].Index
))) ||
455 ((inst
->Opcode
!= OPCODE_KIL
) &&
456 (inst
->DstReg
.File
== PROGRAM_TEMPORARY
) &&
457 (aluTemps
& (1 << inst
->DstReg
.Index
))))
466 for (j
= 0; j
< 3; j
++) {
467 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
)
468 aluTemps
|= (1 << inst
->SrcReg
[j
].Index
);
470 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
471 aluTemps
|= (1 << inst
->DstReg
.Index
);
474 if ((inst
->Opcode
!= OPCODE_KIL
) && (inst
->DstReg
.File
== PROGRAM_TEMPORARY
))
475 tempsOutput
|= (1 << inst
->DstReg
.Index
);
478 prog
->NumTexIndirections
= indirections
;
483 * Count number of texture instructions in given program and update the
484 * program's NumTexInstructions field.
487 _mesa_count_texture_instructions(struct gl_program
*prog
)
490 prog
->NumTexInstructions
= 0;
491 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
492 prog
->NumTexInstructions
+= is_texture_instruction(prog
->Instructions
+ i
);
498 * Scan/rewrite program to remove reads of custom (output) registers.
499 * The passed type has to be PROGRAM_OUTPUT.
500 * On some hardware, trying to read an output register causes trouble.
501 * So, rewrite the program to use a temporary register in this case.
504 _mesa_remove_output_reads(struct gl_program
*prog
, gl_register_file type
)
507 GLint outputMap
[VARYING_SLOT_MAX
];
508 GLuint numVaryingReads
= 0;
509 GLboolean usedTemps
[MAX_PROGRAM_TEMPS
];
510 GLuint firstTemp
= 0;
512 _mesa_find_used_registers(prog
, PROGRAM_TEMPORARY
,
513 usedTemps
, MAX_PROGRAM_TEMPS
);
515 assert(type
== PROGRAM_OUTPUT
);
517 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++)
520 /* look for instructions which read from varying vars */
521 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
522 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
523 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
525 for (j
= 0; j
< numSrc
; j
++) {
526 if (inst
->SrcReg
[j
].File
== type
) {
527 /* replace the read with a temp reg */
528 const GLuint var
= inst
->SrcReg
[j
].Index
;
529 if (outputMap
[var
] == -1) {
531 outputMap
[var
] = _mesa_find_free_register(usedTemps
,
534 firstTemp
= outputMap
[var
] + 1;
536 inst
->SrcReg
[j
].File
= PROGRAM_TEMPORARY
;
537 inst
->SrcReg
[j
].Index
= outputMap
[var
];
542 if (numVaryingReads
== 0)
543 return; /* nothing to be done */
545 /* look for instructions which write to the varying vars identified above */
546 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
547 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
548 if (inst
->DstReg
.File
== type
&&
549 outputMap
[inst
->DstReg
.Index
] >= 0) {
550 /* change inst to write to the temp reg, instead of the varying */
551 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
552 inst
->DstReg
.Index
= outputMap
[inst
->DstReg
.Index
];
556 /* insert new instructions to copy the temp vars to the varying vars */
558 struct prog_instruction
*inst
;
561 /* Look for END instruction and insert the new varying writes */
563 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
564 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
565 if (inst
->Opcode
== OPCODE_END
) {
567 _mesa_insert_instructions(prog
, i
, numVaryingReads
);
574 /* insert new MOV instructions here */
575 inst
= prog
->Instructions
+ endPos
;
576 for (var
= 0; var
< VARYING_SLOT_MAX
; var
++) {
577 if (outputMap
[var
] >= 0) {
578 /* MOV VAR[var], TEMP[tmp]; */
579 inst
->Opcode
= OPCODE_MOV
;
580 inst
->DstReg
.File
= type
;
581 inst
->DstReg
.Index
= var
;
582 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
583 inst
->SrcReg
[0].Index
= outputMap
[var
];
591 _mesa_program_fragment_position_to_sysval(struct gl_program
*prog
)
595 if (prog
->Target
!= GL_FRAGMENT_PROGRAM_ARB
||
596 !(prog
->InputsRead
& BITFIELD64_BIT(VARYING_SLOT_POS
)))
599 prog
->InputsRead
&= ~BITFIELD64_BIT(VARYING_SLOT_POS
);
600 prog
->SystemValuesRead
|= 1 << SYSTEM_VALUE_FRAG_COORD
;
602 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
603 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
604 const GLuint numSrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
607 for (j
= 0; j
< numSrc
; j
++) {
608 if (inst
->SrcReg
[j
].File
== PROGRAM_INPUT
&&
609 inst
->SrcReg
[j
].Index
== VARYING_SLOT_POS
) {
610 inst
->SrcReg
[j
].File
= PROGRAM_SYSTEM_VALUE
;
611 inst
->SrcReg
[j
].Index
= SYSTEM_VALUE_FRAG_COORD
;