Merge branch 'master' of git+ssh://brianp@git.freedesktop.org/git/mesa/mesa into...
[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 "glheader.h"
35 #include "context.h"
36 #include "prog_parameter.h"
37 #include "prog_statevars.h"
38 #include "programopt.h"
39 #include "prog_instruction.h"
40
41
42 /**
43 * This function inserts instructions for coordinate modelview * projection
44 * into a vertex program.
45 * May be used to implement the position_invariant option.
46 */
47 void
48 _mesa_insert_mvp_code(GLcontext *ctx, struct gl_vertex_program *vprog)
49 {
50 struct prog_instruction *newInst;
51 const GLuint origLen = vprog->Base.NumInstructions;
52 const GLuint newLen = origLen + 4;
53 GLuint i;
54
55 /*
56 * Setup state references for the modelview/projection matrix.
57 * XXX we should check if these state vars are already declared.
58 */
59 static const gl_state_index mvpState[4][STATE_LENGTH] = {
60 { STATE_MVP_MATRIX, 0, 0, 0, 0 }, /* state.matrix.mvp.row[0] */
61 { STATE_MVP_MATRIX, 0, 1, 1, 0 }, /* state.matrix.mvp.row[1] */
62 { STATE_MVP_MATRIX, 0, 2, 2, 0 }, /* state.matrix.mvp.row[2] */
63 { STATE_MVP_MATRIX, 0, 3, 3, 0 }, /* state.matrix.mvp.row[3] */
64 };
65 GLint mvpRef[4];
66
67 for (i = 0; i < 4; i++) {
68 mvpRef[i] = _mesa_add_state_reference(vprog->Base.Parameters,
69 mvpState[i]);
70 }
71
72 /* Alloc storage for new instructions */
73 newInst = _mesa_alloc_instructions(newLen);
74 if (!newInst) {
75 _mesa_error(ctx, GL_OUT_OF_MEMORY,
76 "glProgramString(inserting position_invariant code)");
77 return;
78 }
79
80 /*
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;
86 */
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 = VERT_RESULT_HPOS;
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;
99 }
100
101 /* Append original instructions after new instructions */
102 _mesa_memcpy(newInst + 4, vprog->Base.Instructions,
103 origLen * sizeof(struct prog_instruction));
104
105 /* free old instructions */
106 _mesa_free(vprog->Base.Instructions);
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_memcpy(newInst, fprog->Base.Instructions,
155 origLen * sizeof(struct prog_instruction));
156
157 /* PARAM fogParamsRefOpt = internal optimized fog params; */
158 fogPRefOpt
159 = _mesa_add_state_reference(fprog->Base.Parameters, fogPStateOpt);
160 /* PARAM fogColorRef = state.fog.color; */
161 fogColorRef
162 = _mesa_add_state_reference(fprog->Base.Parameters, fogColorState);
163
164 /* TEMP colorTemp; */
165 colorTemp = fprog->Base.NumTemporaries++;
166 /* TEMP fogFactorTemp; */
167 fogFactorTemp = fprog->Base.NumTemporaries++;
168
169 /* Scan program to find where result.color is written */
170 inst = newInst;
171 for (i = 0; i < fprog->Base.NumInstructions; i++) {
172 if (inst->Opcode == OPCODE_END)
173 break;
174 if (inst->DstReg.File == PROGRAM_OUTPUT &&
175 inst->DstReg.Index == FRAG_RESULT_COLR) {
176 /* change the instruction to write to colorTemp w/ clamping */
177 inst->DstReg.File = PROGRAM_TEMPORARY;
178 inst->DstReg.Index = colorTemp;
179 inst->SaturateMode = SATURATE_ZERO_ONE;
180 /* don't break (may be several writes to result.color) */
181 }
182 inst++;
183 }
184 assert(inst->Opcode == OPCODE_END); /* we'll overwrite this inst */
185
186 _mesa_init_instructions(inst, 5);
187
188 /* emit instructions to compute fog blending factor */
189 if (fprog->FogOption == GL_LINEAR) {
190 /* MAD fogFactorTemp.x, fragment.fogcoord.x, fogPRefOpt.x, fogPRefOpt.y; */
191 inst->Opcode = OPCODE_MAD;
192 inst->DstReg.File = PROGRAM_TEMPORARY;
193 inst->DstReg.Index = fogFactorTemp;
194 inst->DstReg.WriteMask = WRITEMASK_X;
195 inst->SrcReg[0].File = PROGRAM_INPUT;
196 inst->SrcReg[0].Index = FRAG_ATTRIB_FOGC;
197 inst->SrcReg[0].Swizzle = SWIZZLE_X;
198 inst->SrcReg[1].File = PROGRAM_STATE_VAR;
199 inst->SrcReg[1].Index = fogPRefOpt;
200 inst->SrcReg[1].Swizzle = SWIZZLE_X;
201 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
202 inst->SrcReg[2].Index = fogPRefOpt;
203 inst->SrcReg[2].Swizzle = SWIZZLE_Y;
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_Z : SWIZZLE_W;
219 inst->SrcReg[1].File = PROGRAM_INPUT;
220 inst->SrcReg[1].Index = FRAG_ATTRIB_FOGC;
221 inst->SrcReg[1].Swizzle = SWIZZLE_X;
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_X;
232 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
233 inst->SrcReg[1].Index = fogFactorTemp;
234 inst->SrcReg[1].Swizzle = SWIZZLE_X;
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 = GL_TRUE;
245 inst->SrcReg[0].Swizzle = SWIZZLE_X;
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
257 = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
258 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
259 inst->SrcReg[1].Index = colorTemp;
260 inst->SrcReg[1].Swizzle = SWIZZLE_NOOP;
261 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
262 inst->SrcReg[2].Index = fogColorRef;
263 inst->SrcReg[2].Swizzle = SWIZZLE_NOOP;
264 inst++;
265 /* MOV result.color.w, colorTemp.x; # copy alpha */
266 inst->Opcode = OPCODE_MOV;
267 inst->DstReg.File = PROGRAM_OUTPUT;
268 inst->DstReg.Index = FRAG_RESULT_COLR;
269 inst->DstReg.WriteMask = WRITEMASK_W;
270 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
271 inst->SrcReg[0].Index = colorTemp;
272 inst->SrcReg[0].Swizzle = SWIZZLE_NOOP;
273 inst++;
274 /* END; */
275 inst->Opcode = OPCODE_END;
276 inst++;
277
278 /* free old instructions */
279 _mesa_free(fprog->Base.Instructions);
280
281 /* install new instructions */
282 fprog->Base.Instructions = newInst;
283 fprog->Base.NumInstructions = inst - newInst;
284 fprog->Base.InputsRead |= FRAG_BIT_FOGC;
285 /* XXX do this? fprog->FogOption = GL_NONE; */
286 }
287
288
289
290 static GLboolean
291 is_texture_instruction(const struct prog_instruction *inst)
292 {
293 switch (inst->Opcode) {
294 case OPCODE_TEX:
295 case OPCODE_TXB:
296 case OPCODE_TXD:
297 case OPCODE_TXL:
298 case OPCODE_TXP:
299 case OPCODE_TXP_NV:
300 return GL_TRUE;
301 default:
302 return GL_FALSE;
303 }
304 }
305
306
307 /**
308 * Count the number of texure indirections in the given program.
309 * The program's NumTexIndirections field will be updated.
310 * See the GL_ARB_fragment_program spec (issue 24) for details.
311 * XXX we count texture indirections in texenvprogram.c (maybe use this code
312 * instead and elsewhere).
313 */
314 void
315 _mesa_count_texture_indirections(struct gl_program *prog)
316 {
317 GLuint indirections = 1;
318 GLbitfield tempsOutput = 0x0;
319 GLbitfield aluTemps = 0x0;
320 GLuint i;
321
322 for (i = 0; i < prog->NumInstructions; i++) {
323 const struct prog_instruction *inst = prog->Instructions + i;
324
325 if (is_texture_instruction(inst)) {
326 if (((inst->SrcReg[0].File == PROGRAM_TEMPORARY) &&
327 (tempsOutput & (1 << inst->SrcReg[0].Index))) ||
328 ((inst->Opcode != OPCODE_KIL) &&
329 (inst->DstReg.File == PROGRAM_TEMPORARY) &&
330 (aluTemps & (1 << inst->DstReg.Index))))
331 {
332 indirections++;
333 tempsOutput = 0x0;
334 aluTemps = 0x0;
335 }
336 }
337 else {
338 GLuint j;
339 for (j = 0; j < 3; j++) {
340 if (inst->SrcReg[j].File == PROGRAM_TEMPORARY)
341 aluTemps |= (1 << inst->SrcReg[j].Index);
342 }
343 if (inst->DstReg.File == PROGRAM_TEMPORARY)
344 aluTemps |= (1 << inst->DstReg.Index);
345 }
346
347 if ((inst->Opcode != OPCODE_KIL) && (inst->DstReg.File == PROGRAM_TEMPORARY))
348 tempsOutput |= (1 << inst->DstReg.Index);
349 }
350
351 prog->NumTexIndirections = indirections;
352 }
353
354
355 /**
356 * Count number of texture instructions in given program and update the
357 * program's NumTexInstructions field.
358 */
359 void
360 _mesa_count_texture_instructions(struct gl_program *prog)
361 {
362 GLuint i;
363 prog->NumTexInstructions = 0;
364 for (i = 0; i < prog->NumInstructions; i++) {
365 prog->NumTexInstructions += is_texture_instruction(prog->Instructions + i);
366 }
367 }
368