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 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 static void
insert_mvp_dp4_code(struct gl_context * ctx,struct gl_program * vprog)49 insert_mvp_dp4_code(struct gl_context *ctx, struct gl_program *vprog)
50 {
51 struct prog_instruction *newInst;
52 const GLuint origLen = vprog->arb.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_index16 mvpState[4][STATE_LENGTH] = {
61 { STATE_MVP_MATRIX, 0, 0, 0 }, /* state.matrix.mvp.row[0] */
62 { STATE_MVP_MATRIX, 0, 1, 1 }, /* state.matrix.mvp.row[1] */
63 { STATE_MVP_MATRIX, 0, 2, 2 }, /* state.matrix.mvp.row[2] */
64 { STATE_MVP_MATRIX, 0, 3, 3 }, /* 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->Parameters, mvpState[i]);
70 }
71
72 /* Alloc storage for new instructions */
73 newInst = rzalloc_array(vprog, struct prog_instruction, 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 = 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;
99 }
100
101 /* Append original instructions after new instructions */
102 _mesa_copy_instructions (newInst + 4, vprog->arb.Instructions, origLen);
103
104 /* free old instructions */
105 ralloc_free(vprog->arb.Instructions);
106
107 /* install new instructions */
108 vprog->arb.Instructions = newInst;
109 vprog->arb.NumInstructions = newLen;
110 vprog->info.inputs_read |= VERT_BIT_POS;
111 vprog->info.outputs_written |= BITFIELD64_BIT(VARYING_SLOT_POS);
112 }
113
114
115 static void
insert_mvp_mad_code(struct gl_context * ctx,struct gl_program * vprog)116 insert_mvp_mad_code(struct gl_context *ctx, struct gl_program *vprog)
117 {
118 struct prog_instruction *newInst;
119 const GLuint origLen = vprog->arb.NumInstructions;
120 const GLuint newLen = origLen + 4;
121 GLuint hposTemp;
122 GLuint i;
123
124 /*
125 * Setup state references for the modelview/projection matrix.
126 * XXX we should check if these state vars are already declared.
127 */
128 static const gl_state_index16 mvpState[4][STATE_LENGTH] = {
129 { STATE_MVP_MATRIX_TRANSPOSE, 0, 0, 0 },
130 { STATE_MVP_MATRIX_TRANSPOSE, 0, 1, 1 },
131 { STATE_MVP_MATRIX_TRANSPOSE, 0, 2, 2 },
132 { STATE_MVP_MATRIX_TRANSPOSE, 0, 3, 3 },
133 };
134 GLint mvpRef[4];
135
136 for (i = 0; i < 4; i++) {
137 mvpRef[i] = _mesa_add_state_reference(vprog->Parameters, mvpState[i]);
138 }
139
140 /* Alloc storage for new instructions */
141 newInst = rzalloc_array(vprog, struct prog_instruction, newLen);
142 if (!newInst) {
143 _mesa_error(ctx, GL_OUT_OF_MEMORY,
144 "glProgramString(inserting position_invariant code)");
145 return;
146 }
147
148 /* TEMP hposTemp; */
149 hposTemp = vprog->arb.NumTemporaries++;
150
151 /*
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);
157 */
158 _mesa_init_instructions(newInst, 4);
159
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;
170
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;
185 }
186
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;
200
201
202 /* Append original instructions after new instructions */
203 _mesa_copy_instructions (newInst + 4, vprog->arb.Instructions, origLen);
204
205 /* free old instructions */
206 ralloc_free(vprog->arb.Instructions);
207
208 /* install new instructions */
209 vprog->arb.Instructions = newInst;
210 vprog->arb.NumInstructions = newLen;
211 vprog->info.inputs_read |= VERT_BIT_POS;
212 vprog->info.outputs_written |= BITFIELD64_BIT(VARYING_SLOT_POS);
213 }
214
215
216 void
_mesa_insert_mvp_code(struct gl_context * ctx,struct gl_program * vprog)217 _mesa_insert_mvp_code(struct gl_context *ctx, struct gl_program *vprog)
218 {
219 if (ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS)
220 insert_mvp_dp4_code( ctx, vprog );
221 else
222 insert_mvp_mad_code( ctx, vprog );
223 }
224
225
226
227
228
229
230 /**
231 * Append instructions to implement fog
232 *
233 * The \c fragment.fogcoord input is used to compute the fog blend factor.
234 *
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]
239 *
240 * \note
241 * This function sets \c VARYING_BIT_FOGC in \c fprog->info.inputs_read.
242 *
243 * \todo With a little work, this function could be adapted to add fog code
244 * to vertex programs too.
245 */
246 void
_mesa_append_fog_code(struct gl_context * ctx,struct gl_program * fprog,GLenum fog_mode,GLboolean saturate)247 _mesa_append_fog_code(struct gl_context *ctx, struct gl_program *fprog,
248 GLenum fog_mode, GLboolean saturate)
249 {
250 static const gl_state_index16 fogPStateOpt[STATE_LENGTH]
251 = { STATE_FOG_PARAMS_OPTIMIZED, 0, 0 };
252 static const gl_state_index16 fogColorState[STATE_LENGTH]
253 = { STATE_FOG_COLOR, 0, 0, 0 };
254 struct prog_instruction *newInst, *inst;
255 const GLuint origLen = fprog->arb.NumInstructions;
256 const GLuint newLen = origLen + 5;
257 GLuint i;
258 GLint fogPRefOpt, fogColorRef; /* state references */
259 GLuint colorTemp, fogFactorTemp; /* temporary registerss */
260
261 if (fog_mode == GL_NONE) {
262 _mesa_problem(ctx, "_mesa_append_fog_code() called for fragment program"
263 " with fog_mode == GL_NONE");
264 return;
265 }
266
267 if (!(fprog->info.outputs_written & (1 << FRAG_RESULT_COLOR))) {
268 /* program doesn't output color, so nothing to do */
269 return;
270 }
271
272 /* Alloc storage for new instructions */
273 newInst = rzalloc_array(fprog, struct prog_instruction, newLen);
274 if (!newInst) {
275 _mesa_error(ctx, GL_OUT_OF_MEMORY,
276 "glProgramString(inserting fog_option code)");
277 return;
278 }
279
280 /* Copy orig instructions into new instruction buffer */
281 _mesa_copy_instructions(newInst, fprog->arb.Instructions, origLen);
282
283 /* PARAM fogParamsRefOpt = internal optimized fog params; */
284 fogPRefOpt
285 = _mesa_add_state_reference(fprog->Parameters, fogPStateOpt);
286 /* PARAM fogColorRef = state.fog.color; */
287 fogColorRef
288 = _mesa_add_state_reference(fprog->Parameters, fogColorState);
289
290 /* TEMP colorTemp; */
291 colorTemp = fprog->arb.NumTemporaries++;
292 /* TEMP fogFactorTemp; */
293 fogFactorTemp = fprog->arb.NumTemporaries++;
294
295 /* Scan program to find where result.color is written */
296 inst = newInst;
297 for (i = 0; i < fprog->arb.NumInstructions; i++) {
298 if (inst->Opcode == OPCODE_END)
299 break;
300 if (inst->DstReg.File == PROGRAM_OUTPUT &&
301 inst->DstReg.Index == FRAG_RESULT_COLOR) {
302 /* change the instruction to write to colorTemp w/ clamping */
303 inst->DstReg.File = PROGRAM_TEMPORARY;
304 inst->DstReg.Index = colorTemp;
305 inst->Saturate = saturate;
306 /* don't break (may be several writes to result.color) */
307 }
308 inst++;
309 }
310 assert(inst->Opcode == OPCODE_END); /* we'll overwrite this inst */
311
312 _mesa_init_instructions(inst, 5);
313
314 /* emit instructions to compute fog blending factor */
315 /* this is always clamped to [0, 1] regardless of fragment clamping */
316 if (fog_mode == GL_LINEAR) {
317 /* MAD fogFactorTemp.x, fragment.fogcoord.x, fogPRefOpt.x, fogPRefOpt.y; */
318 inst->Opcode = OPCODE_MAD;
319 inst->DstReg.File = PROGRAM_TEMPORARY;
320 inst->DstReg.Index = fogFactorTemp;
321 inst->DstReg.WriteMask = WRITEMASK_X;
322 inst->SrcReg[0].File = PROGRAM_INPUT;
323 inst->SrcReg[0].Index = VARYING_SLOT_FOGC;
324 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
325 inst->SrcReg[1].File = PROGRAM_STATE_VAR;
326 inst->SrcReg[1].Index = fogPRefOpt;
327 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX;
328 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
329 inst->SrcReg[2].Index = fogPRefOpt;
330 inst->SrcReg[2].Swizzle = SWIZZLE_YYYY;
331 inst->Saturate = GL_TRUE;
332 inst++;
333 }
334 else {
335 assert(fog_mode == GL_EXP || fog_mode == GL_EXP2);
336 /* fogPRefOpt.z = d/ln(2), fogPRefOpt.w = d/sqrt(ln(2) */
337 /* EXP: MUL fogFactorTemp.x, fogPRefOpt.z, fragment.fogcoord.x; */
338 /* EXP2: MUL fogFactorTemp.x, fogPRefOpt.w, fragment.fogcoord.x; */
339 inst->Opcode = OPCODE_MUL;
340 inst->DstReg.File = PROGRAM_TEMPORARY;
341 inst->DstReg.Index = fogFactorTemp;
342 inst->DstReg.WriteMask = WRITEMASK_X;
343 inst->SrcReg[0].File = PROGRAM_STATE_VAR;
344 inst->SrcReg[0].Index = fogPRefOpt;
345 inst->SrcReg[0].Swizzle
346 = (fog_mode == GL_EXP) ? SWIZZLE_ZZZZ : SWIZZLE_WWWW;
347 inst->SrcReg[1].File = PROGRAM_INPUT;
348 inst->SrcReg[1].Index = VARYING_SLOT_FOGC;
349 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX;
350 inst++;
351 if (fog_mode == GL_EXP2) {
352 /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */
353 inst->Opcode = OPCODE_MUL;
354 inst->DstReg.File = PROGRAM_TEMPORARY;
355 inst->DstReg.Index = fogFactorTemp;
356 inst->DstReg.WriteMask = WRITEMASK_X;
357 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
358 inst->SrcReg[0].Index = fogFactorTemp;
359 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
360 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
361 inst->SrcReg[1].Index = fogFactorTemp;
362 inst->SrcReg[1].Swizzle = SWIZZLE_XXXX;
363 inst++;
364 }
365 /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */
366 inst->Opcode = OPCODE_EX2;
367 inst->DstReg.File = PROGRAM_TEMPORARY;
368 inst->DstReg.Index = fogFactorTemp;
369 inst->DstReg.WriteMask = WRITEMASK_X;
370 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
371 inst->SrcReg[0].Index = fogFactorTemp;
372 inst->SrcReg[0].Negate = NEGATE_XYZW;
373 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
374 inst->Saturate = GL_TRUE;
375 inst++;
376 }
377 /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */
378 inst->Opcode = OPCODE_LRP;
379 inst->DstReg.File = PROGRAM_OUTPUT;
380 inst->DstReg.Index = FRAG_RESULT_COLOR;
381 inst->DstReg.WriteMask = WRITEMASK_XYZ;
382 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
383 inst->SrcReg[0].Index = fogFactorTemp;
384 inst->SrcReg[0].Swizzle = SWIZZLE_XXXX;
385 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
386 inst->SrcReg[1].Index = colorTemp;
387 inst->SrcReg[1].Swizzle = SWIZZLE_NOOP;
388 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
389 inst->SrcReg[2].Index = fogColorRef;
390 inst->SrcReg[2].Swizzle = SWIZZLE_NOOP;
391 inst++;
392 /* MOV result.color.w, colorTemp.x; # copy alpha */
393 inst->Opcode = OPCODE_MOV;
394 inst->DstReg.File = PROGRAM_OUTPUT;
395 inst->DstReg.Index = FRAG_RESULT_COLOR;
396 inst->DstReg.WriteMask = WRITEMASK_W;
397 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
398 inst->SrcReg[0].Index = colorTemp;
399 inst->SrcReg[0].Swizzle = SWIZZLE_NOOP;
400 inst++;
401 /* END; */
402 inst->Opcode = OPCODE_END;
403 inst++;
404
405 /* free old instructions */
406 ralloc_free(fprog->arb.Instructions);
407
408 /* install new instructions */
409 fprog->arb.Instructions = newInst;
410 fprog->arb.NumInstructions = inst - newInst;
411 fprog->info.inputs_read |= VARYING_BIT_FOGC;
412 assert(fprog->info.outputs_written & (1 << FRAG_RESULT_COLOR));
413 }
414