Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / mesa / shader / programopt.c
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 */
48 void
49 _mesa_insert_mvp_code(GLcontext *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 |= (1 << VERT_RESULT_HPOS);
113 }
114
115
116
117 /**
118 * Append extra instructions onto the given fragment program to implement
119 * the fog mode specified by fprog->FogOption.
120 * The fragment.fogcoord input is used to compute the fog blend factor.
121 *
122 * XXX with a little work, this function could be adapted to add fog code
123 * to vertex programs too.
124 */
125 void
126 _mesa_append_fog_code(GLcontext *ctx, struct gl_fragment_program *fprog)
127 {
128 static const gl_state_index fogPStateOpt[STATE_LENGTH]
129 = { STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED, 0, 0, 0 };
130 static const gl_state_index fogColorState[STATE_LENGTH]
131 = { STATE_FOG_COLOR, 0, 0, 0, 0};
132 struct prog_instruction *newInst, *inst;
133 const GLuint origLen = fprog->Base.NumInstructions;
134 const GLuint newLen = origLen + 5;
135 GLuint i;
136 GLint fogPRefOpt, fogColorRef; /* state references */
137 GLuint colorTemp, fogFactorTemp; /* temporary registerss */
138
139 if (fprog->FogOption == GL_NONE) {
140 _mesa_problem(ctx, "_mesa_append_fog_code() called for fragment program"
141 " with FogOption == GL_NONE");
142 return;
143 }
144
145 /* Alloc storage for new instructions */
146 newInst = _mesa_alloc_instructions(newLen);
147 if (!newInst) {
148 _mesa_error(ctx, GL_OUT_OF_MEMORY,
149 "glProgramString(inserting fog_option code)");
150 return;
151 }
152
153 /* Copy orig instructions into new instruction buffer */
154 _mesa_copy_instructions(newInst, fprog->Base.Instructions, origLen);
155
156 /* PARAM fogParamsRefOpt = internal optimized fog params; */
157 fogPRefOpt
158 = _mesa_add_state_reference(fprog->Base.Parameters, fogPStateOpt);
159 /* PARAM fogColorRef = state.fog.color; */
160 fogColorRef
161 = _mesa_add_state_reference(fprog->Base.Parameters, fogColorState);
162
163 /* TEMP colorTemp; */
164 colorTemp = fprog->Base.NumTemporaries++;
165 /* TEMP fogFactorTemp; */
166 fogFactorTemp = fprog->Base.NumTemporaries++;
167
168 /* Scan program to find where result.color is written */
169 inst = newInst;
170 for (i = 0; i < fprog->Base.NumInstructions; i++) {
171 if (inst->Opcode == OPCODE_END)
172 break;
173 if (inst->DstReg.File == PROGRAM_OUTPUT &&
174 inst->DstReg.Index == FRAG_RESULT_COLR) {
175 /* change the instruction to write to colorTemp w/ clamping */
176 inst->DstReg.File = PROGRAM_TEMPORARY;
177 inst->DstReg.Index = colorTemp;
178 inst->SaturateMode = SATURATE_ZERO_ONE;
179 /* don't break (may be several writes to result.color) */
180 }
181 inst++;
182 }
183 assert(inst->Opcode == OPCODE_END); /* we'll overwrite this inst */
184
185 _mesa_init_instructions(inst, 5);
186
187 /* emit instructions to compute fog blending factor */
188 if (fprog->FogOption == GL_LINEAR) {
189 /* MAD fogFactorTemp.x, fragment.fogcoord.x, fogPRefOpt.x, fogPRefOpt.y; */
190 inst->Opcode = OPCODE_MAD;
191 inst->DstReg.File = PROGRAM_TEMPORARY;
192 inst->DstReg.Index = fogFactorTemp;
193 inst->DstReg.WriteMask = WRITEMASK_X;
194 inst->SrcReg[0].File = PROGRAM_INPUT;
195 inst->SrcReg[0].Index = FRAG_ATTRIB_FOGC;
196 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
197 inst->SrcReg[1].File = PROGRAM_STATE_VAR;
198 inst->SrcReg[1].Index = fogPRefOpt;
199 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX;
200 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
201 inst->SrcReg[2].Index = fogPRefOpt;
202 inst->SrcReg[2].Swizzle = SWIZZLE_YYYY;
203 inst->SaturateMode = SATURATE_ZERO_ONE;
204 inst++;
205 }
206 else {
207 ASSERT(fprog->FogOption == GL_EXP || fprog->FogOption == GL_EXP2);
208 /* fogPRefOpt.z = d/ln(2), fogPRefOpt.w = d/sqrt(ln(2) */
209 /* EXP: MUL fogFactorTemp.x, fogPRefOpt.z, fragment.fogcoord.x; */
210 /* EXP2: MUL fogFactorTemp.x, fogPRefOpt.w, fragment.fogcoord.x; */
211 inst->Opcode = OPCODE_MUL;
212 inst->DstReg.File = PROGRAM_TEMPORARY;
213 inst->DstReg.Index = fogFactorTemp;
214 inst->DstReg.WriteMask = WRITEMASK_X;
215 inst->SrcReg[0].File = PROGRAM_STATE_VAR;
216 inst->SrcReg[0].Index = fogPRefOpt;
217 inst->SrcReg[0].Swizzle
218 = (fprog->FogOption == GL_EXP) ? SWIZZLE_ZZZZ : SWIZZLE_WWWW;
219 inst->SrcReg[1].File = PROGRAM_INPUT;
220 inst->SrcReg[1].Index = FRAG_ATTRIB_FOGC;
221 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX;
222 inst++;
223 if (fprog->FogOption == GL_EXP2) {
224 /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */
225 inst->Opcode = OPCODE_MUL;
226 inst->DstReg.File = PROGRAM_TEMPORARY;
227 inst->DstReg.Index = fogFactorTemp;
228 inst->DstReg.WriteMask = WRITEMASK_X;
229 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
230 inst->SrcReg[0].Index = fogFactorTemp;
231 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
232 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
233 inst->SrcReg[1].Index = fogFactorTemp;
234 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX;
235 inst++;
236 }
237 /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */
238 inst->Opcode = OPCODE_EX2;
239 inst->DstReg.File = PROGRAM_TEMPORARY;
240 inst->DstReg.Index = fogFactorTemp;
241 inst->DstReg.WriteMask = WRITEMASK_X;
242 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
243 inst->SrcReg[0].Index = fogFactorTemp;
244 inst->SrcReg[0].NegateBase = NEGATE_XYZW;
245 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
246 inst->SaturateMode = SATURATE_ZERO_ONE;
247 inst++;
248 }
249 /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */
250 inst->Opcode = OPCODE_LRP;
251 inst->DstReg.File = PROGRAM_OUTPUT;
252 inst->DstReg.Index = FRAG_RESULT_COLR;
253 inst->DstReg.WriteMask = WRITEMASK_XYZ;
254 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
255 inst->SrcReg[0].Index = fogFactorTemp;
256 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
257 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
258 inst->SrcReg[1].Index = colorTemp;
259 inst->SrcReg[1].Swizzle = SWIZZLE_NOOP;
260 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
261 inst->SrcReg[2].Index = fogColorRef;
262 inst->SrcReg[2].Swizzle = SWIZZLE_NOOP;
263 inst++;
264 /* MOV result.color.w, colorTemp.x; # copy alpha */
265 inst->Opcode = OPCODE_MOV;
266 inst->DstReg.File = PROGRAM_OUTPUT;
267 inst->DstReg.Index = FRAG_RESULT_COLR;
268 inst->DstReg.WriteMask = WRITEMASK_W;
269 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
270 inst->SrcReg[0].Index = colorTemp;
271 inst->SrcReg[0].Swizzle = SWIZZLE_NOOP;
272 inst++;
273 /* END; */
274 inst->Opcode = OPCODE_END;
275 inst++;
276
277 /* free old instructions */
278 _mesa_free_instructions(fprog->Base.Instructions, origLen);
279
280 /* install new instructions */
281 fprog->Base.Instructions = newInst;
282 fprog->Base.NumInstructions = inst - newInst;
283 fprog->Base.InputsRead |= FRAG_BIT_FOGC;
284 /* XXX do this? fprog->FogOption = GL_NONE; */
285 }
286
287
288
289 static GLboolean
290 is_texture_instruction(const struct prog_instruction *inst)
291 {
292 switch (inst->Opcode) {
293 case OPCODE_TEX:
294 case OPCODE_TXB:
295 case OPCODE_TXD:
296 case OPCODE_TXL:
297 case OPCODE_TXP:
298 case OPCODE_TXP_NV:
299 return GL_TRUE;
300 default:
301 return GL_FALSE;
302 }
303 }
304
305
306 /**
307 * Count the number of texure indirections in the given program.
308 * The program's NumTexIndirections field will be updated.
309 * See the GL_ARB_fragment_program spec (issue 24) for details.
310 * XXX we count texture indirections in texenvprogram.c (maybe use this code
311 * instead and elsewhere).
312 */
313 void
314 _mesa_count_texture_indirections(struct gl_program *prog)
315 {
316 GLuint indirections = 1;
317 GLbitfield tempsOutput = 0x0;
318 GLbitfield aluTemps = 0x0;
319 GLuint i;
320
321 for (i = 0; i < prog->NumInstructions; i++) {
322 const struct prog_instruction *inst = prog->Instructions + i;
323
324 if (is_texture_instruction(inst)) {
325 if (((inst->SrcReg[0].File == PROGRAM_TEMPORARY) &&
326 (tempsOutput & (1 << inst->SrcReg[0].Index))) ||
327 ((inst->Opcode != OPCODE_KIL) &&
328 (inst->DstReg.File == PROGRAM_TEMPORARY) &&
329 (aluTemps & (1 << inst->DstReg.Index))))
330 {
331 indirections++;
332 tempsOutput = 0x0;
333 aluTemps = 0x0;
334 }
335 }
336 else {
337 GLuint j;
338 for (j = 0; j < 3; j++) {
339 if (inst->SrcReg[j].File == PROGRAM_TEMPORARY)
340 aluTemps |= (1 << inst->SrcReg[j].Index);
341 }
342 if (inst->DstReg.File == PROGRAM_TEMPORARY)
343 aluTemps |= (1 << inst->DstReg.Index);
344 }
345
346 if ((inst->Opcode != OPCODE_KIL) && (inst->DstReg.File == PROGRAM_TEMPORARY))
347 tempsOutput |= (1 << inst->DstReg.Index);
348 }
349
350 prog->NumTexIndirections = indirections;
351 }
352
353
354 /**
355 * Count number of texture instructions in given program and update the
356 * program's NumTexInstructions field.
357 */
358 void
359 _mesa_count_texture_instructions(struct gl_program *prog)
360 {
361 GLuint i;
362 prog->NumTexInstructions = 0;
363 for (i = 0; i < prog->NumInstructions; i++) {
364 prog->NumTexInstructions += is_texture_instruction(prog->Instructions + i);
365 }
366 }
367
368
369 /**
370 * Scan/rewrite program to remove reads of custom (output) registers.
371 * The passed type has to be either PROGRAM_OUTPUT or PROGRAM_VARYING
372 * (for vertex shaders).
373 * In GLSL shaders, varying vars can be read and written.
374 * On some hardware, trying to read an output register causes trouble.
375 * So, rewrite the program to use a temporary register in this case.
376 */
377 void
378 _mesa_remove_output_reads(struct gl_program *prog, enum register_file type)
379 {
380 GLuint i;
381 GLint outputMap[VERT_RESULT_MAX];
382 GLuint numVaryingReads = 0;
383
384 assert(type == PROGRAM_VARYING || type == PROGRAM_OUTPUT);
385 assert(prog->Target == GL_VERTEX_PROGRAM_ARB || type != PROGRAM_VARYING);
386
387 for (i = 0; i < VERT_RESULT_MAX; i++)
388 outputMap[i] = -1;
389
390 /* look for instructions which read from varying vars */
391 for (i = 0; i < prog->NumInstructions; i++) {
392 struct prog_instruction *inst = prog->Instructions + i;
393 const GLuint numSrc = _mesa_num_inst_src_regs(inst->Opcode);
394 GLuint j;
395 for (j = 0; j < numSrc; j++) {
396 if (inst->SrcReg[j].File == type) {
397 /* replace the read with a temp reg */
398 const GLuint var = inst->SrcReg[j].Index;
399 if (outputMap[var] == -1) {
400 numVaryingReads++;
401 outputMap[var] = _mesa_find_free_register(prog,
402 PROGRAM_TEMPORARY);
403 }
404 inst->SrcReg[j].File = PROGRAM_TEMPORARY;
405 inst->SrcReg[j].Index = outputMap[var];
406 }
407 }
408 }
409
410 if (numVaryingReads == 0)
411 return; /* nothing to be done */
412
413 /* look for instructions which write to the varying vars identified above */
414 for (i = 0; i < prog->NumInstructions; i++) {
415 struct prog_instruction *inst = prog->Instructions + i;
416 const GLuint numSrc = _mesa_num_inst_src_regs(inst->Opcode);
417 GLuint j;
418 for (j = 0; j < numSrc; j++) {
419 if (inst->DstReg.File == type &&
420 outputMap[inst->DstReg.Index] >= 0) {
421 /* change inst to write to the temp reg, instead of the varying */
422 inst->DstReg.File = PROGRAM_TEMPORARY;
423 inst->DstReg.Index = outputMap[inst->DstReg.Index];
424 }
425 }
426 }
427
428 /* insert new instructions to copy the temp vars to the varying vars */
429 {
430 struct prog_instruction *inst;
431 GLint endPos, var;
432
433 /* Look for END instruction and insert the new varying writes */
434 endPos = -1;
435 for (i = 0; i < prog->NumInstructions; i++) {
436 struct prog_instruction *inst = prog->Instructions + i;
437 if (inst->Opcode == OPCODE_END) {
438 endPos = i;
439 _mesa_insert_instructions(prog, i, numVaryingReads);
440 break;
441 }
442 }
443
444 assert(endPos >= 0);
445
446 /* insert new MOV instructions here */
447 inst = prog->Instructions + endPos;
448 for (var = 0; var < VERT_RESULT_MAX; var++) {
449 if (outputMap[var] >= 0) {
450 /* MOV VAR[var], TEMP[tmp]; */
451 inst->Opcode = OPCODE_MOV;
452 inst->DstReg.File = type;
453 inst->DstReg.Index = var;
454 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
455 inst->SrcReg[0].Index = outputMap[var];
456 inst++;
457 }
458 }
459 }
460 }