2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Vertex/Fragment program optimizations and transformations for program
36 #include "prog_parameter.h"
37 #include "prog_statevars.h"
38 #include "programopt.h"
39 #include "prog_instruction.h"
43 * This function inserts instructions for coordinate modelview * projection
44 * into a vertex program.
45 * May be used to implement the position_invariant option.
48 _mesa_insert_mvp_code(GLcontext
*ctx
, struct gl_vertex_program
*vprog
)
50 struct prog_instruction
*newInst
;
51 const GLuint origLen
= vprog
->Base
.NumInstructions
;
52 const GLuint newLen
= origLen
+ 4;
56 * Setup state references for the modelview/projection matrix.
57 * XXX we should check if these state vars are already declared.
59 static const GLint mvpState
[4][5] = {
60 { STATE_MATRIX
, STATE_MVP
, 0, 0, 0 }, /* state.matrix.mvp.row[0] */
61 { STATE_MATRIX
, STATE_MVP
, 0, 1, 1 }, /* state.matrix.mvp.row[1] */
62 { STATE_MATRIX
, STATE_MVP
, 0, 2, 2 }, /* state.matrix.mvp.row[2] */
63 { STATE_MATRIX
, STATE_MVP
, 0, 3, 3 }, /* state.matrix.mvp.row[3] */
67 for (i
= 0; i
< 4; i
++) {
68 mvpRef
[i
] = _mesa_add_state_reference(vprog
->Base
.Parameters
,
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
= 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
;
101 /* Append original instructions after new instructions */
102 _mesa_memcpy(newInst
+ 4, vprog
->Base
.Instructions
,
103 origLen
* sizeof(struct prog_instruction
));
105 /* free old instructions */
106 _mesa_free(vprog
->Base
.Instructions
);
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
);
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.
122 * XXX with a little work, this function could be adapted to add fog code
123 * to vertex programs too.
126 _mesa_append_fog_code(GLcontext
*ctx
, struct gl_fragment_program
*fprog
)
128 static const GLint fogParamsState
[] = { STATE_FOG_PARAMS
, 0, 0, 0, 0 };
129 static const GLint fogColorState
[] = { STATE_FOG_COLOR
, 0, 0, 0, 0 };
130 struct prog_instruction
*newInst
, *inst
;
131 const GLuint origLen
= fprog
->Base
.NumInstructions
;
132 const GLuint newLen
= origLen
+ 6;
134 GLint fogParamsRef
, fogColorRef
; /* state references */
135 GLuint colorTemp
, fogFactorTemp
; /* temporary registerss */
137 GLuint fogConsts
; /* constant values for EXP, EXP2 mode */
140 if (fprog
->FogOption
== GL_NONE
) {
141 _mesa_problem(ctx
, "_mesa_append_fog_code() called for fragment program"
142 " with FogOption == GL_NONE");
146 /* Alloc storage for new instructions */
147 newInst
= _mesa_alloc_instructions(newLen
);
149 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
150 "glProgramString(inserting fog_option code)");
154 /* Copy orig instructions into new instruction buffer */
155 _mesa_memcpy(newInst
, fprog
->Base
.Instructions
,
156 origLen
* sizeof(struct prog_instruction
));
158 /* PARAM fogParamsRef = state.fog.params; */
160 = _mesa_add_state_reference(fprog
->Base
.Parameters
, fogParamsState
);
161 /* PARAM fogColorRef = state.fog.color; */
163 = _mesa_add_state_reference(fprog
->Base
.Parameters
, fogColorState
);
165 /* TEMP colorTemp; */
166 colorTemp
= fprog
->Base
.NumTemporaries
++;
167 /* TEMP fogFactorTemp; */
168 fogFactorTemp
= fprog
->Base
.NumTemporaries
++;
170 /* PARAM fogVals = { 1/ln(2), 1/sqrt(ln(2), 0, 0 }; */
171 fogVals
[0] = 1.0 / log(2.0);
172 fogVals
[1] = 1.0 / SQRTF(log(2.0));
175 fogConsts
= _mesa_add_unnamed_constant(fprog
->Base
.Parameters
,
176 fogVals
, 4, &swizzle
);
177 ASSERT(swizzle
== SWIZZLE_NOOP
);
179 /* Scan program to find where result.color is written */
181 for (i
= 0; i
< fprog
->Base
.NumInstructions
; i
++) {
182 if (inst
->Opcode
== OPCODE_END
)
184 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
&&
185 inst
->DstReg
.Index
== FRAG_RESULT_COLR
) {
186 /* change the instruction to write to colorTemp w/ clamping */
187 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
188 inst
->DstReg
.Index
= colorTemp
;
189 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
190 /* don't break (may be several writes to result.color) */
194 assert(inst
->Opcode
== OPCODE_END
); /* we'll overwrite this inst */
196 _mesa_init_instructions(inst
, 6);
198 /* emit instructions to compute fog blending factor */
199 if (fprog
->FogOption
== GL_LINEAR
) {
200 /* SUB fogFactorTemp.x, fogParamsRef.z, fragment.fogcoord.x; */
201 inst
->Opcode
= OPCODE_SUB
;
202 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
203 inst
->DstReg
.Index
= fogFactorTemp
;
204 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
205 inst
->SrcReg
[0].File
= PROGRAM_STATE_VAR
;
206 inst
->SrcReg
[0].Index
= fogParamsRef
;
207 inst
->SrcReg
[0].Swizzle
= SWIZZLE_Z
;
208 inst
->SrcReg
[1].File
= PROGRAM_INPUT
;
209 inst
->SrcReg
[1].Index
= FRAG_ATTRIB_FOGC
;
211 /* MUL fogFactorTemp.x, fogFactorTemp, fogParamsRef.w; */
212 inst
->Opcode
= OPCODE_MUL
;
213 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
214 inst
->DstReg
.Index
= fogFactorTemp
;
215 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
216 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
217 inst
->SrcReg
[0].Index
= fogFactorTemp
;
218 inst
->SrcReg
[1].File
= PROGRAM_STATE_VAR
;
219 inst
->SrcReg
[1].Index
= fogParamsRef
;
220 inst
->SrcReg
[1].Swizzle
= SWIZZLE_W
;
224 ASSERT(fprog
->FogOption
== GL_EXP
|| fprog
->FogOption
== GL_EXP2
);
225 /* MUL fogFactorTemp.x, fogParamsRef.x, fragment.fogcoord; */
226 inst
->Opcode
= OPCODE_MUL
;
227 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
228 inst
->DstReg
.Index
= fogFactorTemp
;
229 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
230 inst
->SrcReg
[0].File
= PROGRAM_STATE_VAR
;
231 inst
->SrcReg
[0].Index
= fogParamsRef
;
232 inst
->SrcReg
[0].Swizzle
= SWIZZLE_X
; /* X=density */
233 inst
->SrcReg
[1].File
= PROGRAM_INPUT
;
234 inst
->SrcReg
[1].Index
= FRAG_ATTRIB_FOGC
;
235 inst
->SrcReg
[1].Swizzle
= SWIZZLE_X
;
237 if (fprog
->FogOption
== GL_EXP2
) {
238 /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */
239 inst
->Opcode
= OPCODE_MUL
;
240 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
241 inst
->DstReg
.Index
= fogFactorTemp
;
242 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
243 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
244 inst
->SrcReg
[0].Index
= fogFactorTemp
;
245 inst
->SrcReg
[1].File
= PROGRAM_TEMPORARY
;
246 inst
->SrcReg
[1].Index
= fogFactorTemp
;
249 /* EXP: MUL fogFactorTemp.x, fogFactorTemp.x, {1/ln(2)}; */
250 /* EXP2: MUL fogFactorTemp.x, fogFactorTemp.x, {1/sqrt(ln(2))}; */
251 inst
->Opcode
= OPCODE_MUL
;
252 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
253 inst
->DstReg
.Index
= fogFactorTemp
;
254 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
255 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
256 inst
->SrcReg
[0].Index
= fogFactorTemp
;
257 inst
->SrcReg
[1].File
= PROGRAM_CONSTANT
;
258 inst
->SrcReg
[1].Index
= fogConsts
;
259 inst
->SrcReg
[1].Swizzle
260 = (fprog
->FogOption
== GL_EXP
) ? SWIZZLE_X
: SWIZZLE_Y
;
262 /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */
263 inst
->Opcode
= OPCODE_EX2
;
264 inst
->DstReg
.File
= PROGRAM_TEMPORARY
;
265 inst
->DstReg
.Index
= fogFactorTemp
;
266 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
267 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
268 inst
->SrcReg
[0].Index
= fogFactorTemp
;
269 inst
->SrcReg
[0].NegateBase
= GL_TRUE
;
270 inst
->SaturateMode
= SATURATE_ZERO_ONE
;
273 /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */
274 inst
->Opcode
= OPCODE_LRP
;
275 inst
->DstReg
.File
= PROGRAM_OUTPUT
;
276 inst
->DstReg
.Index
= FRAG_RESULT_COLR
;
277 inst
->DstReg
.WriteMask
= WRITEMASK_XYZ
;
278 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
279 inst
->SrcReg
[0].Index
= fogFactorTemp
;
280 inst
->SrcReg
[0].Swizzle
281 = MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
282 inst
->SrcReg
[1].File
= PROGRAM_TEMPORARY
;
283 inst
->SrcReg
[1].Index
= colorTemp
;
284 inst
->SrcReg
[2].File
= PROGRAM_STATE_VAR
;
285 inst
->SrcReg
[2].Index
= fogColorRef
;
287 /* MOV result.color.w, colorTemp.x; # copy alpha */
288 inst
->Opcode
= OPCODE_MOV
;
289 inst
->DstReg
.File
= PROGRAM_OUTPUT
;
290 inst
->DstReg
.Index
= FRAG_RESULT_COLR
;
291 inst
->DstReg
.WriteMask
= WRITEMASK_W
;
292 inst
->SrcReg
[0].File
= PROGRAM_TEMPORARY
;
293 inst
->SrcReg
[0].Index
= colorTemp
;
296 inst
->Opcode
= OPCODE_END
;
299 /* free old instructions */
300 _mesa_free(fprog
->Base
.Instructions
);
302 /* install new instructions */
303 fprog
->Base
.Instructions
= newInst
;
304 fprog
->Base
.NumInstructions
= inst
- newInst
;
305 fprog
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
306 /* XXX do this? fprog->FogOption = GL_NONE; */
312 is_texture_instruction(const struct prog_instruction
*inst
)
314 switch (inst
->Opcode
) {
329 * Count the number of texure indirections in the given program.
330 * The program's NumTexIndirections field will be updated.
331 * See the GL_ARB_fragment_program spec (issue 24) for details.
332 * XXX we count texture indirections in texenvprogram.c (maybe use this code
333 * instead and elsewhere).
336 _mesa_count_texture_indirections(struct gl_program
*prog
)
338 GLuint indirections
= 1;
339 GLbitfield tempsOutput
= 0x0;
340 GLbitfield aluTemps
= 0x0;
343 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
344 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
346 if (is_texture_instruction(inst
)) {
347 if (((inst
->SrcReg
[0].File
== PROGRAM_TEMPORARY
) &&
348 (tempsOutput
& (1 << inst
->SrcReg
[0].Index
))) ||
349 ((inst
->Opcode
!= OPCODE_KIL
) &&
350 (inst
->DstReg
.File
== PROGRAM_TEMPORARY
) &&
351 (aluTemps
& (1 << inst
->DstReg
.Index
))))
360 for (j
= 0; j
< 3; j
++) {
361 if (inst
->SrcReg
[j
].File
== PROGRAM_TEMPORARY
)
362 aluTemps
|= (1 << inst
->SrcReg
[j
].Index
);
364 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
)
365 aluTemps
|= (1 << inst
->DstReg
.Index
);
368 if ((inst
->Opcode
!= OPCODE_KIL
) && (inst
->DstReg
.File
== PROGRAM_TEMPORARY
))
369 tempsOutput
|= (1 << inst
->DstReg
.Index
);
372 prog
->NumTexIndirections
= indirections
;
377 * Count number of texture instructions in given program and update the
378 * program's NumTexInstructions field.
381 _mesa_count_texture_instructions(struct gl_program
*prog
)
384 prog
->NumTexInstructions
= 0;
385 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
386 prog
->NumTexInstructions
+= is_texture_instruction(prog
->Instructions
+ i
);