blob: c190071e9ce05055379e448828c6ebf3507ff86d [file] [log] [blame]
Brian Paulc6511ab2006-08-24 22:11:40 +00001/*
2 * Mesa 3-D graphics library
Brian Paulf5eea0c2006-10-28 17:14:47 +00003 * Version: 6.5.2
Brian Paulc6511ab2006-08-24 22:11:40 +00004 *
5 * Copyright (C) 1999-2006 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 "imports.h"
37#include "mtypes.h"
38#include "program.h"
Brian57d95312006-12-14 15:03:04 -070039#include "prog_parameter.h"
40#include "prog_statevars.h"
Brian Paulc6511ab2006-08-24 22:11:40 +000041#include "programopt.h"
Brian57d95312006-12-14 15:03:04 -070042#include "prog_instruction.h"
Brian Paulc6511ab2006-08-24 22:11:40 +000043
44
45/**
Brian Paul3c54e832006-08-25 15:15:24 +000046 * This function inserts instructions for coordinate modelview * projection
47 * into a vertex program.
48 * May be used to implement the position_invariant option.
Brian Paulc6511ab2006-08-24 22:11:40 +000049 */
50void
Brian Paul3c54e832006-08-25 15:15:24 +000051_mesa_insert_mvp_code(GLcontext *ctx, struct gl_vertex_program *vprog)
Brian Paulc6511ab2006-08-24 22:11:40 +000052{
Brian Paul3c54e832006-08-25 15:15:24 +000053 struct prog_instruction *newInst;
Brian Paulc6511ab2006-08-24 22:11:40 +000054 const GLuint origLen = vprog->Base.NumInstructions;
Brian Paul3c54e832006-08-25 15:15:24 +000055 const GLuint newLen = origLen + 4;
Brian Paulc6511ab2006-08-24 22:11:40 +000056 GLuint i;
57
58 /*
59 * Setup state references for the modelview/projection matrix.
60 * XXX we should check if these state vars are already declared.
61 */
62 static const GLint mvpState[4][5] = {
63 { STATE_MATRIX, STATE_MVP, 0, 0, 0 }, /* state.matrix.mvp.row[0] */
64 { STATE_MATRIX, STATE_MVP, 0, 1, 1 }, /* state.matrix.mvp.row[1] */
65 { STATE_MATRIX, STATE_MVP, 0, 2, 2 }, /* state.matrix.mvp.row[2] */
66 { STATE_MATRIX, STATE_MVP, 0, 3, 3 }, /* state.matrix.mvp.row[3] */
67 };
68 GLint mvpRef[4];
69
70 for (i = 0; i < 4; i++) {
71 mvpRef[i] = _mesa_add_state_reference(vprog->Base.Parameters,
72 mvpState[i]);
73 }
74
Brian Paul3c54e832006-08-25 15:15:24 +000075 /* Alloc storage for new instructions */
76 newInst = _mesa_alloc_instructions(newLen);
77 if (!newInst) {
78 _mesa_error(ctx, GL_OUT_OF_MEMORY,
79 "glProgramString(inserting position_invariant code)");
80 return;
81 }
82
Brian Paulc6511ab2006-08-24 22:11:40 +000083 /*
84 * Generated instructions:
85 * newInst[0] = DP4 result.position.x, mvp.row[0], vertex.position;
86 * newInst[1] = DP4 result.position.y, mvp.row[1], vertex.position;
87 * newInst[2] = DP4 result.position.z, mvp.row[2], vertex.position;
88 * newInst[3] = DP4 result.position.w, mvp.row[3], vertex.position;
Brian Paulc6511ab2006-08-24 22:11:40 +000089 */
Brian Pauld6272e02006-10-29 18:03:16 +000090 _mesa_init_instructions(newInst, 4);
Brian Paulc6511ab2006-08-24 22:11:40 +000091 for (i = 0; i < 4; i++) {
Brian Paulc6511ab2006-08-24 22:11:40 +000092 newInst[i].Opcode = OPCODE_DP4;
93 newInst[i].DstReg.File = PROGRAM_OUTPUT;
94 newInst[i].DstReg.Index = VERT_RESULT_HPOS;
95 newInst[i].DstReg.WriteMask = (WRITEMASK_X << i);
96 newInst[i].SrcReg[0].File = PROGRAM_STATE_VAR;
97 newInst[i].SrcReg[0].Index = mvpRef[i];
98 newInst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP;
99 newInst[i].SrcReg[1].File = PROGRAM_INPUT;
100 newInst[i].SrcReg[1].Index = VERT_ATTRIB_POS;
101 newInst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP;
102 }
Brian Paulc6511ab2006-08-24 22:11:40 +0000103
Brian Paul3c54e832006-08-25 15:15:24 +0000104 /* Append original instructions after new instructions */
105 _mesa_memcpy(newInst + 4, vprog->Base.Instructions,
106 origLen * sizeof(struct prog_instruction));
Brian Paulc6511ab2006-08-24 22:11:40 +0000107
Brian Paul3c54e832006-08-25 15:15:24 +0000108 /* free old instructions */
109 _mesa_free(vprog->Base.Instructions);
110
111 /* install new instructions */
112 vprog->Base.Instructions = newInst;
113 vprog->Base.NumInstructions = newLen;
Brian Paulc6511ab2006-08-24 22:11:40 +0000114 vprog->Base.InputsRead |= VERT_BIT_POS;
115 vprog->Base.OutputsWritten |= (1 << VERT_RESULT_HPOS);
116}
117
118
119
120/**
121 * Append extra instructions onto the given fragment program to implement
Brian Paulf5eea0c2006-10-28 17:14:47 +0000122 * the fog mode specified by fprog->FogOption.
123 * The fragment.fogcoord input is used to compute the fog blend factor.
124 *
125 * XXX with a little work, this function could be adapted to add fog code
126 * to vertex programs too.
Brian Paulc6511ab2006-08-24 22:11:40 +0000127 */
128void
129_mesa_append_fog_code(GLcontext *ctx, struct gl_fragment_program *fprog)
130{
Brian Paulf5eea0c2006-10-28 17:14:47 +0000131 static const GLint fogParamsState[] = { STATE_FOG_PARAMS, 0, 0, 0, 0 };
132 static const GLint fogColorState[] = { STATE_FOG_COLOR, 0, 0, 0, 0 };
133 struct prog_instruction *newInst, *inst;
134 const GLuint origLen = fprog->Base.NumInstructions;
135 const GLuint newLen = origLen + 6;
136 GLuint i;
137 GLint fogParamsRef, fogColorRef; /* state references */
138 GLuint colorTemp, fogFactorTemp; /* temporary registerss */
139 GLfloat fogVals[4];
140 GLuint fogConsts; /* constant values for EXP, EXP2 mode */
Brianfe1d01c2006-12-13 14:54:47 -0700141 GLuint swizzle;
Brian Paulc6511ab2006-08-24 22:11:40 +0000142
Brian Paulabb14302006-10-29 18:14:00 +0000143 if (fprog->FogOption == GL_NONE) {
Brian Paulf5eea0c2006-10-28 17:14:47 +0000144 _mesa_problem(ctx, "_mesa_append_fog_code() called for fragment program"
145 " with FogOption == GL_NONE");
Brian Paulc6511ab2006-08-24 22:11:40 +0000146 return;
147 }
148
Brian Paulf5eea0c2006-10-28 17:14:47 +0000149 /* Alloc storage for new instructions */
150 newInst = _mesa_alloc_instructions(newLen);
151 if (!newInst) {
152 _mesa_error(ctx, GL_OUT_OF_MEMORY,
153 "glProgramString(inserting fog_option code)");
154 return;
155 }
Brian Paulc6511ab2006-08-24 22:11:40 +0000156
Brian Paulf5eea0c2006-10-28 17:14:47 +0000157 /* Copy orig instructions into new instruction buffer */
158 _mesa_memcpy(newInst, fprog->Base.Instructions,
159 origLen * sizeof(struct prog_instruction));
Brian Paulc6511ab2006-08-24 22:11:40 +0000160
Brian Paulf5eea0c2006-10-28 17:14:47 +0000161 /* PARAM fogParamsRef = state.fog.params; */
162 fogParamsRef
163 = _mesa_add_state_reference(fprog->Base.Parameters, fogParamsState);
164 /* PARAM fogColorRef = state.fog.color; */
165 fogColorRef
166 = _mesa_add_state_reference(fprog->Base.Parameters, fogColorState);
167
168 /* TEMP colorTemp; */
169 colorTemp = fprog->Base.NumTemporaries++;
170 /* TEMP fogFactorTemp; */
171 fogFactorTemp = fprog->Base.NumTemporaries++;
172
173 /* PARAM fogVals = { 1/ln(2), 1/sqrt(ln(2), 0, 0 }; */
174 fogVals[0] = 1.0 / log(2.0);
175 fogVals[1] = 1.0 / SQRTF(log(2.0));
176 fogVals[2] = 0.0;
177 fogVals[3] = 0.0;
Brianfe1d01c2006-12-13 14:54:47 -0700178 fogConsts = _mesa_add_unnamed_constant(fprog->Base.Parameters,
179 fogVals, 4, &swizzle);
180 ASSERT(swizzle == SWIZZLE_NOOP);
Brian Paulf5eea0c2006-10-28 17:14:47 +0000181
182 /* Scan program to find where result.color is written */
183 inst = newInst;
184 for (i = 0; i < fprog->Base.NumInstructions; i++) {
185 if (inst->Opcode == OPCODE_END)
186 break;
187 if (inst->DstReg.File == PROGRAM_OUTPUT &&
188 inst->DstReg.Index == FRAG_RESULT_COLR) {
189 /* change the instruction to write to colorTemp w/ clamping */
190 inst->DstReg.File = PROGRAM_TEMPORARY;
191 inst->DstReg.Index = colorTemp;
192 inst->SaturateMode = SATURATE_ZERO_ONE;
193 /* don't break (may be several writes to result.color) */
194 }
195 inst++;
196 }
197 assert(inst->Opcode == OPCODE_END); /* we'll overwrite this inst */
198
Brian Pauld6272e02006-10-29 18:03:16 +0000199 _mesa_init_instructions(inst, 6);
Brian Paulf5eea0c2006-10-28 17:14:47 +0000200
201 /* emit instructions to compute fog blending factor */
202 if (fprog->FogOption == GL_LINEAR) {
203 /* SUB fogFactorTemp.x, fogParamsRef.z, fragment.fogcoord.x; */
204 inst->Opcode = OPCODE_SUB;
205 inst->DstReg.File = PROGRAM_TEMPORARY;
206 inst->DstReg.Index = fogFactorTemp;
207 inst->DstReg.WriteMask = WRITEMASK_X;
208 inst->SrcReg[0].File = PROGRAM_STATE_VAR;
209 inst->SrcReg[0].Index = fogParamsRef;
210 inst->SrcReg[0].Swizzle = SWIZZLE_Z;
211 inst->SrcReg[1].File = PROGRAM_INPUT;
212 inst->SrcReg[1].Index = FRAG_ATTRIB_FOGC;
213 inst++;
214 /* MUL fogFactorTemp.x, fogFactorTemp, fogParamsRef.w; */
215 inst->Opcode = OPCODE_MUL;
216 inst->DstReg.File = PROGRAM_TEMPORARY;
217 inst->DstReg.Index = fogFactorTemp;
218 inst->DstReg.WriteMask = WRITEMASK_X;
219 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
220 inst->SrcReg[0].Index = fogFactorTemp;
221 inst->SrcReg[1].File = PROGRAM_STATE_VAR;
222 inst->SrcReg[1].Index = fogParamsRef;
223 inst->SrcReg[1].Swizzle = SWIZZLE_W;
224 inst++;
225 }
226 else {
227 ASSERT(fprog->FogOption == GL_EXP || fprog->FogOption == GL_EXP2);
228 /* MUL fogFactorTemp.x, fogParamsRef.x, fragment.fogcoord; */
229 inst->Opcode = OPCODE_MUL;
230 inst->DstReg.File = PROGRAM_TEMPORARY;
231 inst->DstReg.Index = fogFactorTemp;
232 inst->DstReg.WriteMask = WRITEMASK_X;
233 inst->SrcReg[0].File = PROGRAM_STATE_VAR;
234 inst->SrcReg[0].Index = fogParamsRef;
235 inst->SrcReg[0].Swizzle = SWIZZLE_X; /* X=density */
236 inst->SrcReg[1].File = PROGRAM_INPUT;
237 inst->SrcReg[1].Index = FRAG_ATTRIB_FOGC;
238 inst->SrcReg[1].Swizzle = SWIZZLE_X;
239 inst++;
240 if (fprog->FogOption == GL_EXP2) {
241 /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */
242 inst->Opcode = OPCODE_MUL;
243 inst->DstReg.File = PROGRAM_TEMPORARY;
244 inst->DstReg.Index = fogFactorTemp;
245 inst->DstReg.WriteMask = WRITEMASK_X;
246 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
247 inst->SrcReg[0].Index = fogFactorTemp;
248 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
249 inst->SrcReg[1].Index = fogFactorTemp;
250 inst++;
251 }
252 /* EXP: MUL fogFactorTemp.x, fogFactorTemp.x, {1/ln(2)}; */
253 /* EXP2: MUL fogFactorTemp.x, fogFactorTemp.x, {1/sqrt(ln(2))}; */
254 inst->Opcode = OPCODE_MUL;
255 inst->DstReg.File = PROGRAM_TEMPORARY;
256 inst->DstReg.Index = fogFactorTemp;
257 inst->DstReg.WriteMask = WRITEMASK_X;
258 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
259 inst->SrcReg[0].Index = fogFactorTemp;
260 inst->SrcReg[1].File = PROGRAM_CONSTANT;
261 inst->SrcReg[1].Index = fogConsts;
262 inst->SrcReg[1].Swizzle
263 = (fprog->FogOption == GL_EXP) ? SWIZZLE_X : SWIZZLE_Y;
264 inst++;
265 /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */
266 inst->Opcode = OPCODE_EX2;
267 inst->DstReg.File = PROGRAM_TEMPORARY;
268 inst->DstReg.Index = fogFactorTemp;
269 inst->DstReg.WriteMask = WRITEMASK_X;
270 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
271 inst->SrcReg[0].Index = fogFactorTemp;
272 inst->SrcReg[0].NegateBase = GL_TRUE;
273 inst->SaturateMode = SATURATE_ZERO_ONE;
274 inst++;
275 }
276 /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */
277 inst->Opcode = OPCODE_LRP;
278 inst->DstReg.File = PROGRAM_OUTPUT;
279 inst->DstReg.Index = FRAG_RESULT_COLR;
280 inst->DstReg.WriteMask = WRITEMASK_XYZ;
281 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
282 inst->SrcReg[0].Index = fogFactorTemp;
283 inst->SrcReg[0].Swizzle
284 = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
285 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
286 inst->SrcReg[1].Index = colorTemp;
287 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
288 inst->SrcReg[2].Index = fogColorRef;
289 inst++;
290 /* MOV result.color.w, colorTemp.x; # copy alpha */
291 inst->Opcode = OPCODE_MOV;
292 inst->DstReg.File = PROGRAM_OUTPUT;
293 inst->DstReg.Index = FRAG_RESULT_COLR;
294 inst->DstReg.WriteMask = WRITEMASK_W;
295 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
296 inst->SrcReg[0].Index = colorTemp;
297 inst++;
298 /* END; */
299 inst->Opcode = OPCODE_END;
300 inst++;
301
302 /* free old instructions */
303 _mesa_free(fprog->Base.Instructions);
304
305 /* install new instructions */
306 fprog->Base.Instructions = newInst;
307 fprog->Base.NumInstructions = inst - newInst;
308 fprog->Base.InputsRead |= FRAG_BIT_FOGC;
309 /* XXX do this? fprog->FogOption = GL_NONE; */
Brian Paulc6511ab2006-08-24 22:11:40 +0000310}