blob: 51caa02f9fa5fdc94ca2d60fe65de7f6b2c103bb [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"
39#include "programopt.h"
40#include "program_instruction.h"
41
42
43/**
Brian Paul3c54e832006-08-25 15:15:24 +000044 * This function inserts instructions for coordinate modelview * projection
45 * into a vertex program.
46 * May be used to implement the position_invariant option.
Brian Paulc6511ab2006-08-24 22:11:40 +000047 */
48void
Brian Paul3c54e832006-08-25 15:15:24 +000049_mesa_insert_mvp_code(GLcontext *ctx, struct gl_vertex_program *vprog)
Brian Paulc6511ab2006-08-24 22:11:40 +000050{
Brian Paul3c54e832006-08-25 15:15:24 +000051 struct prog_instruction *newInst;
Brian Paulc6511ab2006-08-24 22:11:40 +000052 const GLuint origLen = vprog->Base.NumInstructions;
Brian Paul3c54e832006-08-25 15:15:24 +000053 const GLuint newLen = origLen + 4;
Brian Paulc6511ab2006-08-24 22:11:40 +000054 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 GLint mvpState[4][5] = {
61 { STATE_MATRIX, STATE_MVP, 0, 0, 0 }, /* state.matrix.mvp.row[0] */
62 { STATE_MATRIX, STATE_MVP, 0, 1, 1 }, /* state.matrix.mvp.row[1] */
63 { STATE_MATRIX, STATE_MVP, 0, 2, 2 }, /* state.matrix.mvp.row[2] */
64 { STATE_MATRIX, STATE_MVP, 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->Base.Parameters,
70 mvpState[i]);
71 }
72
Brian Paul3c54e832006-08-25 15:15:24 +000073 /* 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
Brian Paulc6511ab2006-08-24 22:11:40 +000081 /*
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;
Brian Paulc6511ab2006-08-24 22:11:40 +000087 */
Brian Pauld6272e02006-10-29 18:03:16 +000088 _mesa_init_instructions(newInst, 4);
Brian Paulc6511ab2006-08-24 22:11:40 +000089 for (i = 0; i < 4; i++) {
Brian Paulc6511ab2006-08-24 22:11:40 +000090 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 }
Brian Paulc6511ab2006-08-24 22:11:40 +0000101
Brian Paul3c54e832006-08-25 15:15:24 +0000102 /* Append original instructions after new instructions */
103 _mesa_memcpy(newInst + 4, vprog->Base.Instructions,
104 origLen * sizeof(struct prog_instruction));
Brian Paulc6511ab2006-08-24 22:11:40 +0000105
Brian Paul3c54e832006-08-25 15:15:24 +0000106 /* free old instructions */
107 _mesa_free(vprog->Base.Instructions);
108
109 /* install new instructions */
110 vprog->Base.Instructions = newInst;
111 vprog->Base.NumInstructions = newLen;
Brian Paulc6511ab2006-08-24 22:11:40 +0000112 vprog->Base.InputsRead |= VERT_BIT_POS;
113 vprog->Base.OutputsWritten |= (1 << VERT_RESULT_HPOS);
114}
115
116
117
118/**
119 * Append extra instructions onto the given fragment program to implement
Brian Paulf5eea0c2006-10-28 17:14:47 +0000120 * the fog mode specified by fprog->FogOption.
121 * The fragment.fogcoord input is used to compute the fog blend factor.
122 *
123 * XXX with a little work, this function could be adapted to add fog code
124 * to vertex programs too.
Brian Paulc6511ab2006-08-24 22:11:40 +0000125 */
126void
127_mesa_append_fog_code(GLcontext *ctx, struct gl_fragment_program *fprog)
128{
Brian Paulf5eea0c2006-10-28 17:14:47 +0000129 static const GLint fogParamsState[] = { STATE_FOG_PARAMS, 0, 0, 0, 0 };
130 static const GLint fogColorState[] = { STATE_FOG_COLOR, 0, 0, 0, 0 };
131 struct prog_instruction *newInst, *inst;
132 const GLuint origLen = fprog->Base.NumInstructions;
133 const GLuint newLen = origLen + 6;
134 GLuint i;
135 GLint fogParamsRef, fogColorRef; /* state references */
136 GLuint colorTemp, fogFactorTemp; /* temporary registerss */
137 GLfloat fogVals[4];
138 GLuint fogConsts; /* constant values for EXP, EXP2 mode */
Brianfe1d01c2006-12-13 14:54:47 -0700139 GLuint swizzle;
Brian Paulc6511ab2006-08-24 22:11:40 +0000140
Brian Paulabb14302006-10-29 18:14:00 +0000141 if (fprog->FogOption == GL_NONE) {
Brian Paulf5eea0c2006-10-28 17:14:47 +0000142 _mesa_problem(ctx, "_mesa_append_fog_code() called for fragment program"
143 " with FogOption == GL_NONE");
Brian Paulc6511ab2006-08-24 22:11:40 +0000144 return;
145 }
146
Brian Paulf5eea0c2006-10-28 17:14:47 +0000147 /* Alloc storage for new instructions */
148 newInst = _mesa_alloc_instructions(newLen);
149 if (!newInst) {
150 _mesa_error(ctx, GL_OUT_OF_MEMORY,
151 "glProgramString(inserting fog_option code)");
152 return;
153 }
Brian Paulc6511ab2006-08-24 22:11:40 +0000154
Brian Paulf5eea0c2006-10-28 17:14:47 +0000155 /* Copy orig instructions into new instruction buffer */
156 _mesa_memcpy(newInst, fprog->Base.Instructions,
157 origLen * sizeof(struct prog_instruction));
Brian Paulc6511ab2006-08-24 22:11:40 +0000158
Brian Paulf5eea0c2006-10-28 17:14:47 +0000159 /* PARAM fogParamsRef = state.fog.params; */
160 fogParamsRef
161 = _mesa_add_state_reference(fprog->Base.Parameters, fogParamsState);
162 /* PARAM fogColorRef = state.fog.color; */
163 fogColorRef
164 = _mesa_add_state_reference(fprog->Base.Parameters, fogColorState);
165
166 /* TEMP colorTemp; */
167 colorTemp = fprog->Base.NumTemporaries++;
168 /* TEMP fogFactorTemp; */
169 fogFactorTemp = fprog->Base.NumTemporaries++;
170
171 /* PARAM fogVals = { 1/ln(2), 1/sqrt(ln(2), 0, 0 }; */
172 fogVals[0] = 1.0 / log(2.0);
173 fogVals[1] = 1.0 / SQRTF(log(2.0));
174 fogVals[2] = 0.0;
175 fogVals[3] = 0.0;
Brianfe1d01c2006-12-13 14:54:47 -0700176 fogConsts = _mesa_add_unnamed_constant(fprog->Base.Parameters,
177 fogVals, 4, &swizzle);
178 ASSERT(swizzle == SWIZZLE_NOOP);
Brian Paulf5eea0c2006-10-28 17:14:47 +0000179
180 /* Scan program to find where result.color is written */
181 inst = newInst;
182 for (i = 0; i < fprog->Base.NumInstructions; i++) {
183 if (inst->Opcode == OPCODE_END)
184 break;
185 if (inst->DstReg.File == PROGRAM_OUTPUT &&
186 inst->DstReg.Index == FRAG_RESULT_COLR) {
187 /* change the instruction to write to colorTemp w/ clamping */
188 inst->DstReg.File = PROGRAM_TEMPORARY;
189 inst->DstReg.Index = colorTemp;
190 inst->SaturateMode = SATURATE_ZERO_ONE;
191 /* don't break (may be several writes to result.color) */
192 }
193 inst++;
194 }
195 assert(inst->Opcode == OPCODE_END); /* we'll overwrite this inst */
196
Brian Pauld6272e02006-10-29 18:03:16 +0000197 _mesa_init_instructions(inst, 6);
Brian Paulf5eea0c2006-10-28 17:14:47 +0000198
199 /* emit instructions to compute fog blending factor */
200 if (fprog->FogOption == GL_LINEAR) {
201 /* SUB fogFactorTemp.x, fogParamsRef.z, fragment.fogcoord.x; */
202 inst->Opcode = OPCODE_SUB;
203 inst->DstReg.File = PROGRAM_TEMPORARY;
204 inst->DstReg.Index = fogFactorTemp;
205 inst->DstReg.WriteMask = WRITEMASK_X;
206 inst->SrcReg[0].File = PROGRAM_STATE_VAR;
207 inst->SrcReg[0].Index = fogParamsRef;
208 inst->SrcReg[0].Swizzle = SWIZZLE_Z;
209 inst->SrcReg[1].File = PROGRAM_INPUT;
210 inst->SrcReg[1].Index = FRAG_ATTRIB_FOGC;
211 inst++;
212 /* MUL fogFactorTemp.x, fogFactorTemp, fogParamsRef.w; */
213 inst->Opcode = OPCODE_MUL;
214 inst->DstReg.File = PROGRAM_TEMPORARY;
215 inst->DstReg.Index = fogFactorTemp;
216 inst->DstReg.WriteMask = WRITEMASK_X;
217 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
218 inst->SrcReg[0].Index = fogFactorTemp;
219 inst->SrcReg[1].File = PROGRAM_STATE_VAR;
220 inst->SrcReg[1].Index = fogParamsRef;
221 inst->SrcReg[1].Swizzle = SWIZZLE_W;
222 inst++;
223 }
224 else {
225 ASSERT(fprog->FogOption == GL_EXP || fprog->FogOption == GL_EXP2);
226 /* MUL fogFactorTemp.x, fogParamsRef.x, fragment.fogcoord; */
227 inst->Opcode = OPCODE_MUL;
228 inst->DstReg.File = PROGRAM_TEMPORARY;
229 inst->DstReg.Index = fogFactorTemp;
230 inst->DstReg.WriteMask = WRITEMASK_X;
231 inst->SrcReg[0].File = PROGRAM_STATE_VAR;
232 inst->SrcReg[0].Index = fogParamsRef;
233 inst->SrcReg[0].Swizzle = SWIZZLE_X; /* X=density */
234 inst->SrcReg[1].File = PROGRAM_INPUT;
235 inst->SrcReg[1].Index = FRAG_ATTRIB_FOGC;
236 inst->SrcReg[1].Swizzle = SWIZZLE_X;
237 inst++;
238 if (fprog->FogOption == GL_EXP2) {
239 /* MUL fogFactorTemp.x, fogFactorTemp.x, fogFactorTemp.x; */
240 inst->Opcode = OPCODE_MUL;
241 inst->DstReg.File = PROGRAM_TEMPORARY;
242 inst->DstReg.Index = fogFactorTemp;
243 inst->DstReg.WriteMask = WRITEMASK_X;
244 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
245 inst->SrcReg[0].Index = fogFactorTemp;
246 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
247 inst->SrcReg[1].Index = fogFactorTemp;
248 inst++;
249 }
250 /* EXP: MUL fogFactorTemp.x, fogFactorTemp.x, {1/ln(2)}; */
251 /* EXP2: MUL fogFactorTemp.x, fogFactorTemp.x, {1/sqrt(ln(2))}; */
252 inst->Opcode = OPCODE_MUL;
253 inst->DstReg.File = PROGRAM_TEMPORARY;
254 inst->DstReg.Index = fogFactorTemp;
255 inst->DstReg.WriteMask = WRITEMASK_X;
256 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
257 inst->SrcReg[0].Index = fogFactorTemp;
258 inst->SrcReg[1].File = PROGRAM_CONSTANT;
259 inst->SrcReg[1].Index = fogConsts;
260 inst->SrcReg[1].Swizzle
261 = (fprog->FogOption == GL_EXP) ? SWIZZLE_X : SWIZZLE_Y;
262 inst++;
263 /* EX2_SAT fogFactorTemp.x, -fogFactorTemp.x; */
264 inst->Opcode = OPCODE_EX2;
265 inst->DstReg.File = PROGRAM_TEMPORARY;
266 inst->DstReg.Index = fogFactorTemp;
267 inst->DstReg.WriteMask = WRITEMASK_X;
268 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
269 inst->SrcReg[0].Index = fogFactorTemp;
270 inst->SrcReg[0].NegateBase = GL_TRUE;
271 inst->SaturateMode = SATURATE_ZERO_ONE;
272 inst++;
273 }
274 /* LRP result.color.xyz, fogFactorTemp.xxxx, colorTemp, fogColorRef; */
275 inst->Opcode = OPCODE_LRP;
276 inst->DstReg.File = PROGRAM_OUTPUT;
277 inst->DstReg.Index = FRAG_RESULT_COLR;
278 inst->DstReg.WriteMask = WRITEMASK_XYZ;
279 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
280 inst->SrcReg[0].Index = fogFactorTemp;
281 inst->SrcReg[0].Swizzle
282 = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
283 inst->SrcReg[1].File = PROGRAM_TEMPORARY;
284 inst->SrcReg[1].Index = colorTemp;
285 inst->SrcReg[2].File = PROGRAM_STATE_VAR;
286 inst->SrcReg[2].Index = fogColorRef;
287 inst++;
288 /* MOV result.color.w, colorTemp.x; # copy alpha */
289 inst->Opcode = OPCODE_MOV;
290 inst->DstReg.File = PROGRAM_OUTPUT;
291 inst->DstReg.Index = FRAG_RESULT_COLR;
292 inst->DstReg.WriteMask = WRITEMASK_W;
293 inst->SrcReg[0].File = PROGRAM_TEMPORARY;
294 inst->SrcReg[0].Index = colorTemp;
295 inst++;
296 /* END; */
297 inst->Opcode = OPCODE_END;
298 inst++;
299
300 /* free old instructions */
301 _mesa_free(fprog->Base.Instructions);
302
303 /* install new instructions */
304 fprog->Base.Instructions = newInst;
305 fprog->Base.NumInstructions = inst - newInst;
306 fprog->Base.InputsRead |= FRAG_BIT_FOGC;
307 /* XXX do this? fprog->FogOption = GL_NONE; */
Brian Paulc6511ab2006-08-24 22:11:40 +0000308}