blob: 0d93220a77e60ce6bd82760817aecd02df945fb2 [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Bauman19bac1e2014-05-06 15:23:49 -04003// Copyright(c) 2005-2012 TransGaming Inc.
John Bauman89401822014-05-06 15:04:28 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#include "VertexProgram.hpp"
13
14#include "Renderer.hpp"
15#include "VertexShader.hpp"
16#include "Vertex.hpp"
17#include "Half.hpp"
18#include "SamplerCore.hpp"
19#include "Debug.hpp"
20
21extern bool localShaderConstants;
22
23namespace sw
24{
John Bauman19bac1e2014-05-06 15:23:49 -040025 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader) : VertexRoutine(state, shader)
John Bauman89401822014-05-06 15:04:28 -040026 {
John Bauman89401822014-05-06 15:04:28 -040027 ifDepth = 0;
28 loopRepDepth = 0;
29 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040030 currentLabel = -1;
31 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040032
33 for(int i = 0; i < 2048; i++)
34 {
35 labelBlock[i] = 0;
36 }
37 }
38
39 VertexProgram::~VertexProgram()
40 {
41 for(int i = 0; i < 4; i++)
42 {
43 delete sampler[i];
44 }
45 }
46
47 void VertexProgram::pipeline(Registers &r)
48 {
49 for(int i = 0; i < 4; i++)
50 {
51 sampler[i] = new SamplerCore(r.constants, state.samplerState[i]);
52 }
53
54 if(!state.preTransformed)
55 {
John Bauman19bac1e2014-05-06 15:23:49 -040056 program(r);
John Bauman89401822014-05-06 15:04:28 -040057 }
58 else
59 {
60 passThrough(r);
61 }
62 }
63
John Bauman19bac1e2014-05-06 15:23:49 -040064 void VertexProgram::program(Registers &r)
John Bauman89401822014-05-06 15:04:28 -040065 {
John Bauman19bac1e2014-05-06 15:23:49 -040066 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040067
John Bauman19bac1e2014-05-06 15:23:49 -040068 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040069
70 r.enableIndex = 0;
71 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040072
73 // Create all call site return blocks up front
74 for(int i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -040075 {
John Bauman19bac1e2014-05-06 15:23:49 -040076 const Shader::Instruction *instruction = shader->getInstruction(i);
77 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -040078
John Bauman19bac1e2014-05-06 15:23:49 -040079 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
80 {
81 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -040082
John Bauman19bac1e2014-05-06 15:23:49 -040083 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
84 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
85 }
86 }
87
88 for(int i = 0; i < shader->getLength(); i++)
89 {
90 const Shader::Instruction *instruction = shader->getInstruction(i);
91 Shader::Opcode opcode = instruction->opcode;
92
93 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -040094 {
95 continue;
96 }
97
John Bauman19bac1e2014-05-06 15:23:49 -040098 Dst dst = instruction->dst;
99 Src src0 = instruction->src[0];
100 Src src1 = instruction->src[1];
101 Src src2 = instruction->src[2];
John Bauman89401822014-05-06 15:04:28 -0400102
John Bauman19bac1e2014-05-06 15:23:49 -0400103 bool predicate = instruction->predicate;
104 int size = shader->size(opcode);
105 Usage usage = instruction->usage;
106 unsigned char usageIndex = instruction->usageIndex;
107 Control control = instruction->control;
108 bool integer = dst.type == Shader::PARAMETER_ADDR;
109 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400110
John Bauman19bac1e2014-05-06 15:23:49 -0400111 Vector4f d;
112 Vector4f s0;
113 Vector4f s1;
114 Vector4f s2;
John Bauman89401822014-05-06 15:04:28 -0400115
John Bauman19bac1e2014-05-06 15:23:49 -0400116 if(src0.type != Shader::PARAMETER_VOID) s0 = reg(r, src0);
117 if(src1.type != Shader::PARAMETER_VOID) s1 = reg(r, src1);
118 if(src2.type != Shader::PARAMETER_VOID) s2 = reg(r, src2);
John Bauman89401822014-05-06 15:04:28 -0400119
120 switch(opcode)
121 {
John Bauman19bac1e2014-05-06 15:23:49 -0400122 case Shader::OPCODE_VS_1_0: break;
123 case Shader::OPCODE_VS_1_1: break;
124 case Shader::OPCODE_VS_2_0: break;
125 case Shader::OPCODE_VS_2_x: break;
126 case Shader::OPCODE_VS_2_sw: break;
127 case Shader::OPCODE_VS_3_0: break;
128 case Shader::OPCODE_VS_3_sw: break;
129 case Shader::OPCODE_DCL: break;
130 case Shader::OPCODE_DEF: break;
131 case Shader::OPCODE_DEFI: break;
132 case Shader::OPCODE_DEFB: break;
133 case Shader::OPCODE_NOP: break;
134 case Shader::OPCODE_ABS: abs(d, s0); break;
135 case Shader::OPCODE_ADD: add(d, s0, s1); break;
136 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
137 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
138 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
139 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
140 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
141 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
142 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
143 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
144 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
145 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
146 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
147 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
148 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
149 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
150 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
151 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
152 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
153 case Shader::OPCODE_ATT: att(d, s0, s1); break;
154 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
155 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
156 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
157 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
158 case Shader::OPCODE_FRC: frc(d, s0); break;
159 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
160 case Shader::OPCODE_FLOOR: floor(d, s0); break;
161 case Shader::OPCODE_CEIL: ceil(d, s0); break;
162 case Shader::OPCODE_LIT: lit(d, s0); break;
163 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
164 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
165 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
166 case Shader::OPCODE_LOG: log(d, s0, pp); break;
167 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
168 case Shader::OPCODE_STEP: step(d, s0, s1); break;
169 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
170 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
171 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
172 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
173 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
174 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
175 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
176 case Shader::OPCODE_MAX: max(d, s0, s1); break;
177 case Shader::OPCODE_MIN: min(d, s0, s1); break;
178 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
179 case Shader::OPCODE_MOVA: mov(d, s0); break;
180 case Shader::OPCODE_F2B: f2b(d, s0); break;
181 case Shader::OPCODE_B2F: b2f(d, s0); break;
182 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
183 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
184 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
185 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
186 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
187 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
188 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
189 case Shader::OPCODE_DIV: div(d, s0, s1); break;
190 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
191 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
192 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
193 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
194 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
195 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
196 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
197 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
198 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
199 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
200 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
201 case Shader::OPCODE_SGE: step(d, s1, s0); break;
202 case Shader::OPCODE_SGN: sgn(d, s0); break;
203 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
204 case Shader::OPCODE_COS: cos(d, s0, pp); break;
205 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
206 case Shader::OPCODE_TAN: tan(d, s0); break;
207 case Shader::OPCODE_ACOS: acos(d, s0); break;
208 case Shader::OPCODE_ASIN: asin(d, s0); break;
209 case Shader::OPCODE_ATAN: atan(d, s0); break;
210 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
211 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
212 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
213 case Shader::OPCODE_BREAK: BREAK(r); break;
214 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
215 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
216 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
217 case Shader::OPCODE_TEST: TEST(); break;
218 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
219 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
220 case Shader::OPCODE_ELSE: ELSE(r); break;
221 case Shader::OPCODE_ENDIF: ENDIF(r); break;
222 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
223 case Shader::OPCODE_ENDREP: ENDREP(r); break;
224 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
225 case Shader::OPCODE_IF: IF(r, src0); break;
226 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
227 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
228 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
229 case Shader::OPCODE_REP: REP(r, src0); break;
230 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
231 case Shader::OPCODE_RET: RET(r); break;
232 case Shader::OPCODE_LEAVE: LEAVE(r); break;
233 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
234 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
235 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
236 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
237 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
238 case Shader::OPCODE_ALL: all(d.x, s0); break;
239 case Shader::OPCODE_ANY: any(d.x, s0); break;
240 case Shader::OPCODE_NOT: not(d, s0); break;
241 case Shader::OPCODE_OR: or(d.x, s0.x, s1.x); break;
242 case Shader::OPCODE_XOR: xor(d.x, s0.x, s1.x); break;
243 case Shader::OPCODE_AND: and(d.x, s0.x, s1.x); break;
244 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1); break;
245 case Shader::OPCODE_TEX: TEX(r, d, s0, src1); break;
246 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400247 default:
248 ASSERT(false);
249 }
250
John Bauman19bac1e2014-05-06 15:23:49 -0400251 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400252 {
John Bauman19bac1e2014-05-06 15:23:49 -0400253 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400254 {
John Bauman19bac1e2014-05-06 15:23:49 -0400255 switch(opcode)
256 {
257 case Shader::OPCODE_DIV:
258 if(dst.x) d.x = Trunc(d.x);
259 if(dst.y) d.y = Trunc(d.y);
260 if(dst.z) d.z = Trunc(d.z);
261 if(dst.w) d.w = Trunc(d.w);
262 break;
263 default:
264 break; // No truncation to integer required when arguments are integer
265 }
John Bauman89401822014-05-06 15:04:28 -0400266 }
267
John Bauman19bac1e2014-05-06 15:23:49 -0400268 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400269 {
John Bauman19bac1e2014-05-06 15:23:49 -0400270 if(dst.x) d.x = Max(d.x, Float4(0.0f));
271 if(dst.y) d.y = Max(d.y, Float4(0.0f));
272 if(dst.z) d.z = Max(d.z, Float4(0.0f));
273 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400274
John Bauman19bac1e2014-05-06 15:23:49 -0400275 if(dst.x) d.x = Min(d.x, Float4(1.0f));
276 if(dst.y) d.y = Min(d.y, Float4(1.0f));
277 if(dst.z) d.z = Min(d.z, Float4(1.0f));
278 if(dst.w) d.w = Min(d.w, Float4(1.0f));
279 }
280
281 if(shader->containsDynamicBranching())
282 {
283 Vector4f pDst; // FIXME: Rename
284
285 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400286 {
John Bauman19bac1e2014-05-06 15:23:49 -0400287 case Shader::PARAMETER_VOID: break;
288 case Shader::PARAMETER_TEMP:
289 if(dst.rel.type == Shader::PARAMETER_VOID)
290 {
291 if(dst.x) pDst.x = r.r[dst.index].x;
292 if(dst.y) pDst.y = r.r[dst.index].y;
293 if(dst.z) pDst.z = r.r[dst.index].z;
294 if(dst.w) pDst.w = r.r[dst.index].w;
295 }
296 else
297 {
298 Int a = relativeAddress(r, dst);
299
300 if(dst.x) pDst.x = r.r[dst.index + a].x;
301 if(dst.y) pDst.y = r.r[dst.index + a].y;
302 if(dst.z) pDst.z = r.r[dst.index + a].z;
303 if(dst.w) pDst.w = r.r[dst.index + a].w;
304 }
305 break;
306 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
307 case Shader::PARAMETER_RASTOUT:
308 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400309 {
310 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400311 if(dst.x) pDst.x = r.o[Pos].x;
312 if(dst.y) pDst.y = r.o[Pos].y;
313 if(dst.z) pDst.z = r.o[Pos].z;
314 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400315 break;
316 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400317 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400318 break;
319 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400320 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400321 break;
322 default:
323 ASSERT(false);
324 }
325 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400326 case Shader::PARAMETER_ATTROUT:
327 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
328 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
329 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
330 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400331 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400332 case Shader::PARAMETER_TEXCRDOUT:
333 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400334 if(version < 0x0300)
335 {
John Bauman19bac1e2014-05-06 15:23:49 -0400336 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
337 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
338 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
339 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400340 }
341 else
342 {
John Bauman19bac1e2014-05-06 15:23:49 -0400343 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400344 {
John Bauman19bac1e2014-05-06 15:23:49 -0400345 if(dst.x) pDst.x = r.o[dst.index].x;
346 if(dst.y) pDst.y = r.o[dst.index].y;
347 if(dst.z) pDst.z = r.o[dst.index].z;
348 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400349 }
John Bauman19bac1e2014-05-06 15:23:49 -0400350 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400351 {
352 Int aL = r.aL[r.loopDepth];
353
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.x) pDst.x = r.o[dst.index + aL].x;
355 if(dst.y) pDst.y = r.o[dst.index + aL].y;
356 if(dst.z) pDst.z = r.o[dst.index + aL].z;
357 if(dst.w) pDst.w = r.o[dst.index + aL].w;
358 }
359 else
360 {
361 Int a = relativeAddress(r, dst);
362
363 if(dst.x) pDst.x = r.o[dst.index + a].x;
364 if(dst.y) pDst.y = r.o[dst.index + a].y;
365 if(dst.z) pDst.z = r.o[dst.index + a].z;
366 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400367 }
368 }
369 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400370 case Shader::PARAMETER_LABEL: break;
371 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
372 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400373 default:
374 ASSERT(false);
375 }
376
John Bauman19bac1e2014-05-06 15:23:49 -0400377 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -0400378
379 Int4 xEnable = enable;
380 Int4 yEnable = enable;
381 Int4 zEnable = enable;
382 Int4 wEnable = enable;
383
384 if(predicate)
385 {
John Bauman19bac1e2014-05-06 15:23:49 -0400386 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400387
388 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
389 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
390 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
391 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
392
John Bauman19bac1e2014-05-06 15:23:49 -0400393 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400394 {
John Bauman19bac1e2014-05-06 15:23:49 -0400395 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
396 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
397 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
398 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400399 }
400 else
401 {
John Bauman19bac1e2014-05-06 15:23:49 -0400402 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
403 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
404 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
405 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400406 }
407 }
408
John Bauman19bac1e2014-05-06 15:23:49 -0400409 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
410 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
411 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
412 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400413
John Bauman19bac1e2014-05-06 15:23:49 -0400414 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
415 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
416 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
417 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400418 }
419
John Bauman19bac1e2014-05-06 15:23:49 -0400420 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400421 {
John Bauman19bac1e2014-05-06 15:23:49 -0400422 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400423 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400424 case Shader::PARAMETER_TEMP:
425 if(dst.rel.type == Shader::PARAMETER_VOID)
426 {
427 if(dst.x) r.r[dst.index].x = d.x;
428 if(dst.y) r.r[dst.index].y = d.y;
429 if(dst.z) r.r[dst.index].z = d.z;
430 if(dst.w) r.r[dst.index].w = d.w;
431 }
432 else
433 {
434 Int a = relativeAddress(r, dst);
435
436 if(dst.x) r.r[dst.index + a].x = d.x;
437 if(dst.y) r.r[dst.index + a].y = d.y;
438 if(dst.z) r.r[dst.index + a].z = d.z;
439 if(dst.w) r.r[dst.index + a].w = d.w;
440 }
John Bauman89401822014-05-06 15:04:28 -0400441 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400442 case Shader::PARAMETER_ADDR:
443 if(dst.x) r.a0.x = d.x;
444 if(dst.y) r.a0.y = d.y;
445 if(dst.z) r.a0.z = d.z;
446 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400447 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400448 case Shader::PARAMETER_RASTOUT:
449 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400450 {
451 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400452 if(dst.x) r.o[Pos].x = d.x;
453 if(dst.y) r.o[Pos].y = d.y;
454 if(dst.z) r.o[Pos].z = d.z;
455 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400456 break;
457 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400458 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400459 break;
460 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400461 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400462 break;
463 default: ASSERT(false);
464 }
465 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400466 case Shader::PARAMETER_ATTROUT:
467 if(dst.x) r.o[D0 + dst.index].x = d.x;
468 if(dst.y) r.o[D0 + dst.index].y = d.y;
469 if(dst.z) r.o[D0 + dst.index].z = d.z;
470 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400471 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400472 case Shader::PARAMETER_TEXCRDOUT:
473 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400474 if(version < 0x0300)
475 {
John Bauman19bac1e2014-05-06 15:23:49 -0400476 if(dst.x) r.o[T0 + dst.index].x = d.x;
477 if(dst.y) r.o[T0 + dst.index].y = d.y;
478 if(dst.z) r.o[T0 + dst.index].z = d.z;
479 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400480 }
481 else
482 {
John Bauman19bac1e2014-05-06 15:23:49 -0400483 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400484 {
John Bauman19bac1e2014-05-06 15:23:49 -0400485 if(dst.x) r.o[dst.index].x = d.x;
486 if(dst.y) r.o[dst.index].y = d.y;
487 if(dst.z) r.o[dst.index].z = d.z;
488 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400489 }
John Bauman19bac1e2014-05-06 15:23:49 -0400490 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400491 {
492 Int aL = r.aL[r.loopDepth];
493
John Bauman19bac1e2014-05-06 15:23:49 -0400494 if(dst.x) r.o[dst.index + aL].x = d.x;
495 if(dst.y) r.o[dst.index + aL].y = d.y;
496 if(dst.z) r.o[dst.index + aL].z = d.z;
497 if(dst.w) r.o[dst.index + aL].w = d.w;
498 }
499 else
500 {
501 Int a = relativeAddress(r, dst);
502
503 if(dst.x) r.o[dst.index + a].x = d.x;
504 if(dst.y) r.o[dst.index + a].y = d.y;
505 if(dst.z) r.o[dst.index + a].z = d.z;
506 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400507 }
508 }
509 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400510 case Shader::PARAMETER_LABEL: break;
511 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
512 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400513 default:
514 ASSERT(false);
515 }
516 }
517 }
518
John Bauman19bac1e2014-05-06 15:23:49 -0400519 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400520 {
521 Nucleus::setInsertBlock(returnBlock);
522 }
523 }
524
525 void VertexProgram::passThrough(Registers &r)
526 {
John Bauman19bac1e2014-05-06 15:23:49 -0400527 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400528 {
529 for(int i = 0; i < 12; i++)
530 {
John Bauman19bac1e2014-05-06 15:23:49 -0400531 unsigned char usage = shader->output[i][0].usage;
532 unsigned char index = shader->output[i][0].index;
John Bauman89401822014-05-06 15:04:28 -0400533
534 switch(usage)
535 {
536 case 0xFF:
537 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400538 case Shader::USAGE_PSIZE:
539 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400540 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400541 case Shader::USAGE_TEXCOORD:
542 r.o[i].x = r.v[i].x;
543 r.o[i].y = r.v[i].y;
544 r.o[i].z = r.v[i].z;
545 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400546 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400547 case Shader::USAGE_POSITION:
548 r.o[i].x = r.v[i].x;
549 r.o[i].y = r.v[i].y;
550 r.o[i].z = r.v[i].z;
551 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400552 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400553 case Shader::USAGE_COLOR:
554 r.o[i].x = r.v[i].x;
555 r.o[i].y = r.v[i].y;
556 r.o[i].z = r.v[i].z;
557 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400558 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400559 case Shader::USAGE_FOG:
560 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400561 break;
562 default:
563 ASSERT(false);
564 }
565 }
566 }
567 else
568 {
John Bauman19bac1e2014-05-06 15:23:49 -0400569 r.o[Pos].x = r.v[PositionT].x;
570 r.o[Pos].y = r.v[PositionT].y;
571 r.o[Pos].z = r.v[PositionT].z;
572 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400573
574 for(int i = 0; i < 2; i++)
575 {
John Bauman19bac1e2014-05-06 15:23:49 -0400576 r.o[D0 + i].x = r.v[Color0 + i].x;
577 r.o[D0 + i].y = r.v[Color0 + i].y;
578 r.o[D0 + i].z = r.v[Color0 + i].z;
579 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400580 }
581
582 for(int i = 0; i < 8; i++)
583 {
John Bauman19bac1e2014-05-06 15:23:49 -0400584 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
585 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
586 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
587 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400588 }
589
John Bauman66b8ab22014-05-06 15:57:45 -0400590 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400591 }
592 }
593
John Bauman19bac1e2014-05-06 15:23:49 -0400594 Vector4f VertexProgram::reg(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400595 {
596 int i = src.index + offset;
597
John Bauman19bac1e2014-05-06 15:23:49 -0400598 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -0400599
John Bauman89401822014-05-06 15:04:28 -0400600 switch(src.type)
601 {
John Bauman19bac1e2014-05-06 15:23:49 -0400602 case Shader::PARAMETER_TEMP:
603 if(src.rel.type == Shader::PARAMETER_VOID)
604 {
605 reg = r.r[i];
606 }
607 else
608 {
609 reg = r.r[i + relativeAddress(r, src)];
610 }
611 break;
612 case Shader::PARAMETER_CONST:
613 reg = readConstant(r, src, offset);
614 break;
615 case Shader::PARAMETER_INPUT:
616 if(src.rel.type == Shader::PARAMETER_VOID)
617 {
618 reg = r.v[i];
619 }
620 else
621 {
622 reg = r.v[i + relativeAddress(r, src)];
623 }
624 break;
625 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
626 case Shader::PARAMETER_FLOAT4LITERAL:
627 reg.x = Float4(src.value[0]);
628 reg.y = Float4(src.value[1]);
629 reg.z = Float4(src.value[2]);
630 reg.w = Float4(src.value[3]);
631 break;
632 case Shader::PARAMETER_ADDR: reg = r.a0; break;
633 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
634 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
635 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
636 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
637 case Shader::PARAMETER_SAMPLER:
638 if(src.rel.type == Shader::PARAMETER_VOID)
639 {
640 reg.x = As<Float4>(Int4(i));
641 }
642 else if(src.rel.type == Shader::PARAMETER_TEMP)
643 {
644 reg.x = As<Float4>(Int4(i) + RoundInt(r.r[src.rel.index].x));
645 }
646 return reg;
647 case Shader::PARAMETER_OUTPUT:
648 if(src.rel.type == Shader::PARAMETER_VOID)
649 {
650 reg = r.o[i];
651 }
652 else
653 {
654 reg = r.o[i + relativeAddress(r, src)];
655 }
656 break;
John Bauman89401822014-05-06 15:04:28 -0400657 default:
658 ASSERT(false);
659 }
660
John Bauman66b8ab22014-05-06 15:57:45 -0400661 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
662 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
663 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
664 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400665
John Bauman66b8ab22014-05-06 15:57:45 -0400666 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400667
668 switch(src.modifier)
669 {
John Bauman19bac1e2014-05-06 15:23:49 -0400670 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400671 mod.x = x;
672 mod.y = y;
673 mod.z = z;
674 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400675 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400676 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400677 mod.x = -x;
678 mod.y = -y;
679 mod.z = -z;
680 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400681 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400682 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400683 mod.x = Abs(x);
684 mod.y = Abs(y);
685 mod.z = Abs(z);
686 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400687 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400688 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400689 mod.x = -Abs(x);
690 mod.y = -Abs(y);
691 mod.z = -Abs(z);
692 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400693 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400694 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400695 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
696 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
697 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
698 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400699 break;
700 default:
701 ASSERT(false);
702 }
703
704 return mod;
705 }
706
John Bauman19bac1e2014-05-06 15:23:49 -0400707 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400708 {
John Bauman19bac1e2014-05-06 15:23:49 -0400709 Vector4f c;
710
711 int i = src.index + offset;
712
713 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
714 {
715 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
716
717 c.x = c.x.xxxx;
718 c.y = c.y.yyyy;
719 c.z = c.z.zzzz;
720 c.w = c.w.wwww;
721
722 if(localShaderConstants) // Constant may be known at compile time
723 {
724 for(int j = 0; j < shader->getLength(); j++)
725 {
726 const Shader::Instruction &instruction = *shader->getInstruction(j);
727
728 if(instruction.opcode == Shader::OPCODE_DEF)
729 {
730 if(instruction.dst.index == i)
731 {
732 c.x = Float4(instruction.src[0].value[0]);
733 c.y = Float4(instruction.src[0].value[1]);
734 c.z = Float4(instruction.src[0].value[2]);
735 c.w = Float4(instruction.src[0].value[3]);
736
737 break;
738 }
739 }
740 }
741 }
742 }
743 else if(src.rel.type == Shader::PARAMETER_LOOP)
744 {
745 Int loopCounter = r.aL[r.loopDepth];
746
747 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
748
749 c.x = c.x.xxxx;
750 c.y = c.y.yyyy;
751 c.z = c.z.zzzz;
752 c.w = c.w.wwww;
753 }
754 else
755 {
756 if(src.rel.deterministic)
757 {
758 Int a = relativeAddress(r, src);
759
760 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
761
762 c.x = c.x.xxxx;
763 c.y = c.y.yyyy;
764 c.z = c.z.zzzz;
765 c.w = c.w.wwww;
766 }
767 else
768 {
769 int component = src.rel.swizzle & 0x03;
770 Float4 a;
771
772 switch(src.rel.type)
773 {
774 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
775 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
776 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
777 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400778 case Shader::PARAMETER_CONST: a = *Pointer<Float>(r.data + OFFSET(DrawData,vs.c[src.rel.index][component])); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400779 default: ASSERT(false);
780 }
781
782 Int4 index = Int4(i) + RoundInt(a) * Int4(src.rel.scale);
783
784 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
785
786 Int index0 = Extract(index, 0);
787 Int index1 = Extract(index, 1);
788 Int index2 = Extract(index, 2);
789 Int index3 = Extract(index, 3);
790
791 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
792 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
793 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
794 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
795
796 transpose4x4(c.x, c.y, c.z, c.w);
797 }
798 }
799
800 return c;
801 }
802
803 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
804 {
805 ASSERT(var.rel.deterministic);
806
807 if(var.rel.type == Shader::PARAMETER_TEMP)
808 {
809 return RoundInt(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
810 }
811 else if(var.rel.type == Shader::PARAMETER_INPUT)
812 {
813 return RoundInt(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
814 }
815 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
816 {
817 return RoundInt(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
818 }
819 else if(var.rel.type == Shader::PARAMETER_CONST)
820 {
821 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[var.rel.index]));
822
823 return RoundInt(Extract(c, 0)) * var.rel.scale;
824 }
825 else ASSERT(false);
826
827 return 0;
828 }
829
830 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
831 {
832 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
833
834 if(shader->containsBreakInstruction() && !whileTest && instruction->analysisBreak)
835 {
836 enable &= r.enableBreak;
837 }
838
839 if(shader->containsContinueInstruction() && !whileTest && instruction->analysisContinue)
840 {
841 enable &= r.enableContinue;
842 }
843
844 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
845 {
846 enable &= r.enableLeave;
847 }
848
849 return enable;
850 }
851
852 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
853 {
854 Vector4f row0 = reg(r, src1, 0);
855 Vector4f row1 = reg(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400856
857 dst.x = dot3(src0, row0);
858 dst.y = dot3(src0, row1);
859 }
860
John Bauman19bac1e2014-05-06 15:23:49 -0400861 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400862 {
John Bauman19bac1e2014-05-06 15:23:49 -0400863 Vector4f row0 = reg(r, src1, 0);
864 Vector4f row1 = reg(r, src1, 1);
865 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400866
867 dst.x = dot3(src0, row0);
868 dst.y = dot3(src0, row1);
869 dst.z = dot3(src0, row2);
870 }
871
John Bauman19bac1e2014-05-06 15:23:49 -0400872 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400873 {
John Bauman19bac1e2014-05-06 15:23:49 -0400874 Vector4f row0 = reg(r, src1, 0);
875 Vector4f row1 = reg(r, src1, 1);
876 Vector4f row2 = reg(r, src1, 2);
877 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400878
879 dst.x = dot3(src0, row0);
880 dst.y = dot3(src0, row1);
881 dst.z = dot3(src0, row2);
882 dst.w = dot3(src0, row3);
883 }
884
John Bauman19bac1e2014-05-06 15:23:49 -0400885 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400886 {
John Bauman19bac1e2014-05-06 15:23:49 -0400887 Vector4f row0 = reg(r, src1, 0);
888 Vector4f row1 = reg(r, src1, 1);
889 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400890
891 dst.x = dot4(src0, row0);
892 dst.y = dot4(src0, row1);
893 dst.z = dot4(src0, row2);
894 }
895
John Bauman19bac1e2014-05-06 15:23:49 -0400896 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400897 {
John Bauman19bac1e2014-05-06 15:23:49 -0400898 Vector4f row0 = reg(r, src1, 0);
899 Vector4f row1 = reg(r, src1, 1);
900 Vector4f row2 = reg(r, src1, 2);
901 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400902
903 dst.x = dot4(src0, row0);
904 dst.y = dot4(src0, row1);
905 dst.z = dot4(src0, row2);
906 dst.w = dot4(src0, row3);
907 }
908
909 void VertexProgram::BREAK(Registers &r)
910 {
911 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
912 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
913
914 if(breakDepth == 0)
915 {
John Bauman19bac1e2014-05-06 15:23:49 -0400916 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400917 Nucleus::createBr(endBlock);
918 }
919 else
920 {
921 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
922 Bool allBreak = SignMask(r.enableBreak) == 0x0;
923
John Bauman19bac1e2014-05-06 15:23:49 -0400924 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400925 branch(allBreak, endBlock, deadBlock);
926 }
927
928 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400929 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400930 }
931
John Bauman19bac1e2014-05-06 15:23:49 -0400932 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400933 {
934 Int4 condition;
935
936 switch(control)
937 {
John Bauman19bac1e2014-05-06 15:23:49 -0400938 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
939 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
940 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
941 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
942 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
943 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400944 default:
945 ASSERT(false);
946 }
947
John Bauman19bac1e2014-05-06 15:23:49 -0400948 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -0400949 }
950
951 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
952 {
953 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
954
John Bauman19bac1e2014-05-06 15:23:49 -0400955 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -0400956 {
957 condition = ~condition;
958 }
959
John Bauman19bac1e2014-05-06 15:23:49 -0400960 BREAK(r, condition);
961 }
962
963 void VertexProgram::BREAK(Registers &r, Int4 &condition)
964 {
John Bauman89401822014-05-06 15:04:28 -0400965 condition &= r.enableStack[r.enableIndex];
966
967 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
968 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
969
970 r.enableBreak = r.enableBreak & ~condition;
971 Bool allBreak = SignMask(r.enableBreak) == 0x0;
972
John Bauman19bac1e2014-05-06 15:23:49 -0400973 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400974 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400975
John Bauman89401822014-05-06 15:04:28 -0400976 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400977 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400978 }
979
John Bauman19bac1e2014-05-06 15:23:49 -0400980 void VertexProgram::CONTINUE(Registers &r)
981 {
982 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
983 }
984
985 void VertexProgram::TEST()
986 {
987 whileTest = true;
988 }
989
990 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -0400991 {
992 if(!labelBlock[labelIndex])
993 {
994 labelBlock[labelIndex] = Nucleus::createBasicBlock();
995 }
996
John Bauman19bac1e2014-05-06 15:23:49 -0400997 if(callRetBlock[labelIndex].size() > 1)
998 {
999 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1000 }
John Bauman89401822014-05-06 15:04:28 -04001001
John Bauman19bac1e2014-05-06 15:23:49 -04001002 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001003
1004 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001005 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1006
1007 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001008 }
1009
John Bauman19bac1e2014-05-06 15:23:49 -04001010 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001011 {
John Bauman19bac1e2014-05-06 15:23:49 -04001012 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001013 {
John Bauman19bac1e2014-05-06 15:23:49 -04001014 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001015 }
John Bauman19bac1e2014-05-06 15:23:49 -04001016 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001017 {
John Bauman19bac1e2014-05-06 15:23:49 -04001018 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001019 }
1020 else ASSERT(false);
1021 }
1022
John Bauman19bac1e2014-05-06 15:23:49 -04001023 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001024 {
1025 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1026
John Bauman19bac1e2014-05-06 15:23:49 -04001027 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001028 {
1029 condition = !condition;
1030 }
1031
1032 if(!labelBlock[labelIndex])
1033 {
1034 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1035 }
1036
John Bauman19bac1e2014-05-06 15:23:49 -04001037 if(callRetBlock[labelIndex].size() > 1)
1038 {
1039 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1040 }
John Bauman89401822014-05-06 15:04:28 -04001041
John Bauman19bac1e2014-05-06 15:23:49 -04001042 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001043
John Bauman19bac1e2014-05-06 15:23:49 -04001044 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1045 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1046
1047 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001048 }
1049
John Bauman19bac1e2014-05-06 15:23:49 -04001050 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001051 {
1052 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1053
John Bauman19bac1e2014-05-06 15:23:49 -04001054 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001055 {
1056 condition = ~condition;
1057 }
1058
1059 condition &= r.enableStack[r.enableIndex];
1060
1061 if(!labelBlock[labelIndex])
1062 {
1063 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1064 }
1065
John Bauman19bac1e2014-05-06 15:23:49 -04001066 if(callRetBlock[labelIndex].size() > 1)
1067 {
1068 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1069 }
John Bauman89401822014-05-06 15:04:28 -04001070
1071 r.enableIndex++;
1072 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001073 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001074
John Bauman19bac1e2014-05-06 15:23:49 -04001075 Bool notAllFalse = SignMask(condition) != 0;
1076 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1077 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001078
1079 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001080 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001081 }
1082
1083 void VertexProgram::ELSE(Registers &r)
1084 {
1085 ifDepth--;
1086
1087 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1088 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1089
1090 if(isConditionalIf[ifDepth])
1091 {
1092 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001093 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001094
1095 branch(notAllFalse, falseBlock, endBlock);
1096
1097 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1098 }
1099 else
1100 {
1101 Nucleus::createBr(endBlock);
1102 Nucleus::setInsertBlock(falseBlock);
1103 }
1104
1105 ifFalseBlock[ifDepth] = endBlock;
1106
1107 ifDepth++;
1108 }
1109
1110 void VertexProgram::ENDIF(Registers &r)
1111 {
1112 ifDepth--;
1113
1114 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1115
1116 Nucleus::createBr(endBlock);
1117 Nucleus::setInsertBlock(endBlock);
1118
1119 if(isConditionalIf[ifDepth])
1120 {
1121 breakDepth--;
1122 r.enableIndex--;
1123 }
1124 }
1125
John Bauman89401822014-05-06 15:04:28 -04001126 void VertexProgram::ENDLOOP(Registers &r)
1127 {
1128 loopRepDepth--;
1129
1130 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1131
1132 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1133 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1134
1135 Nucleus::createBr(testBlock);
1136 Nucleus::setInsertBlock(endBlock);
1137
1138 r.loopDepth--;
1139 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1140 }
1141
John Bauman19bac1e2014-05-06 15:23:49 -04001142 void VertexProgram::ENDREP(Registers &r)
1143 {
1144 loopRepDepth--;
1145
1146 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1147 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1148
1149 Nucleus::createBr(testBlock);
1150 Nucleus::setInsertBlock(endBlock);
1151
1152 r.loopDepth--;
1153 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1154 }
1155
1156 void VertexProgram::ENDWHILE(Registers &r)
1157 {
1158 loopRepDepth--;
1159
1160 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1161 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1162
1163 Nucleus::createBr(testBlock);
1164 Nucleus::setInsertBlock(endBlock);
1165
1166 r.enableIndex--;
1167 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1168 whileTest = false;
1169 }
1170
John Bauman89401822014-05-06 15:04:28 -04001171 void VertexProgram::IF(Registers &r, const Src &src)
1172 {
John Bauman19bac1e2014-05-06 15:23:49 -04001173 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001174 {
1175 IFb(r, src);
1176 }
John Bauman19bac1e2014-05-06 15:23:49 -04001177 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001178 {
1179 IFp(r, src);
1180 }
John Bauman19bac1e2014-05-06 15:23:49 -04001181 else
1182 {
1183 Int4 condition = As<Int4>(reg(r, src).x);
1184 IF(r, condition);
1185 }
John Bauman89401822014-05-06 15:04:28 -04001186 }
1187
1188 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1189 {
1190 ASSERT(ifDepth < 24 + 4);
1191
1192 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1193
John Bauman19bac1e2014-05-06 15:23:49 -04001194 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001195 {
John Bauman19bac1e2014-05-06 15:23:49 -04001196 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001197 }
1198
1199 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1200 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1201
1202 branch(condition, trueBlock, falseBlock);
1203
1204 isConditionalIf[ifDepth] = false;
1205 ifFalseBlock[ifDepth] = falseBlock;
1206
1207 ifDepth++;
1208 }
1209
John Bauman19bac1e2014-05-06 15:23:49 -04001210 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001211 {
1212 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1213
John Bauman19bac1e2014-05-06 15:23:49 -04001214 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001215 {
1216 condition = ~condition;
1217 }
1218
John Bauman19bac1e2014-05-06 15:23:49 -04001219 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001220 }
1221
John Bauman19bac1e2014-05-06 15:23:49 -04001222 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001223 {
1224 Int4 condition;
1225
1226 switch(control)
1227 {
John Bauman19bac1e2014-05-06 15:23:49 -04001228 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1229 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1230 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1231 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1232 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1233 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001234 default:
1235 ASSERT(false);
1236 }
1237
John Bauman19bac1e2014-05-06 15:23:49 -04001238 IF(r, condition);
1239 }
1240
1241 void VertexProgram::IF(Registers &r, Int4 &condition)
1242 {
John Bauman89401822014-05-06 15:04:28 -04001243 condition &= r.enableStack[r.enableIndex];
1244
1245 r.enableIndex++;
1246 r.enableStack[r.enableIndex] = condition;
1247
1248 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1249 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1250
John Bauman19bac1e2014-05-06 15:23:49 -04001251 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001252
1253 branch(notAllFalse, trueBlock, falseBlock);
1254
1255 isConditionalIf[ifDepth] = true;
1256 ifFalseBlock[ifDepth] = falseBlock;
1257
1258 ifDepth++;
1259 breakDepth++;
1260 }
1261
1262 void VertexProgram::LABEL(int labelIndex)
1263 {
John Bauman19bac1e2014-05-06 15:23:49 -04001264 if(!labelBlock[labelIndex])
1265 {
1266 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1267 }
1268
John Bauman89401822014-05-06 15:04:28 -04001269 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001270 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001271 }
1272
1273 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1274 {
1275 r.loopDepth++;
1276
1277 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1278 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1279 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1280
1281 // FIXME: Compiles to two instructions?
1282 If(r.increment[r.loopDepth] == 0)
1283 {
1284 r.increment[r.loopDepth] = 1;
1285 }
1286
1287 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1288 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1289 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1290
1291 loopRepTestBlock[loopRepDepth] = testBlock;
1292 loopRepEndBlock[loopRepDepth] = endBlock;
1293
1294 // FIXME: jump(testBlock)
1295 Nucleus::createBr(testBlock);
1296 Nucleus::setInsertBlock(testBlock);
1297
1298 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1299 Nucleus::setInsertBlock(loopBlock);
1300
1301 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1302
1303 loopRepDepth++;
1304 breakDepth = 0;
1305 }
1306
1307 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1308 {
1309 r.loopDepth++;
1310
1311 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1312 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1313
1314 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1315 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1316 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1317
1318 loopRepTestBlock[loopRepDepth] = testBlock;
1319 loopRepEndBlock[loopRepDepth] = endBlock;
1320
1321 // FIXME: jump(testBlock)
1322 Nucleus::createBr(testBlock);
1323 Nucleus::setInsertBlock(testBlock);
1324
1325 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1326 Nucleus::setInsertBlock(loopBlock);
1327
1328 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1329
1330 loopRepDepth++;
1331 breakDepth = 0;
1332 }
1333
John Bauman19bac1e2014-05-06 15:23:49 -04001334 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1335 {
1336 r.enableIndex++;
1337
1338 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1339 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1340 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1341
1342 loopRepTestBlock[loopRepDepth] = testBlock;
1343 loopRepEndBlock[loopRepDepth] = endBlock;
1344
1345 Int4 restoreBreak = r.enableBreak;
1346 Int4 restoreContinue = r.enableContinue;
1347
1348 // FIXME: jump(testBlock)
1349 Nucleus::createBr(testBlock);
1350 Nucleus::setInsertBlock(testBlock);
1351 r.enableContinue = restoreContinue;
1352
John Bauman66b8ab22014-05-06 15:57:45 -04001353 const Vector4f &src = reg(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001354 Int4 condition = As<Int4>(src.x);
1355 condition &= r.enableStack[r.enableIndex - 1];
1356 r.enableStack[r.enableIndex] = condition;
1357
1358 Bool notAllFalse = SignMask(condition) != 0;
1359 branch(notAllFalse, loopBlock, endBlock);
1360
1361 Nucleus::setInsertBlock(endBlock);
1362 r.enableBreak = restoreBreak;
1363
1364 Nucleus::setInsertBlock(loopBlock);
1365
1366 loopRepDepth++;
1367 breakDepth = 0;
1368 }
1369
John Bauman89401822014-05-06 15:04:28 -04001370 void VertexProgram::RET(Registers &r)
1371 {
John Bauman19bac1e2014-05-06 15:23:49 -04001372 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001373 {
1374 returnBlock = Nucleus::createBasicBlock();
1375 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001376 }
1377 else
1378 {
John Bauman89401822014-05-06 15:04:28 -04001379 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001380
John Bauman19bac1e2014-05-06 15:23:49 -04001381 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001382 {
John Bauman19bac1e2014-05-06 15:23:49 -04001383 // FIXME: Encapsulate
1384 UInt index = r.callStack[--r.stackIndex];
1385
John Bauman66b8ab22014-05-06 15:57:45 -04001386 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001387 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1388
1389 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1390 {
1391 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1392 }
1393 }
1394 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1395 {
1396 Nucleus::createBr(callRetBlock[currentLabel][0]);
1397 }
1398 else // Function isn't called
1399 {
1400 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001401 }
1402
1403 Nucleus::setInsertBlock(unreachableBlock);
1404 Nucleus::createUnreachable();
1405 }
1406 }
1407
John Bauman19bac1e2014-05-06 15:23:49 -04001408 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001409 {
John Bauman19bac1e2014-05-06 15:23:49 -04001410 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001411
John Bauman19bac1e2014-05-06 15:23:49 -04001412 // FIXME: Return from function if all instances left
1413 // FIXME: Use enableLeave in other control-flow constructs
1414 }
John Bauman89401822014-05-06 15:04:28 -04001415
John Bauman19bac1e2014-05-06 15:23:49 -04001416 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1417 {
1418 Vector4f tmp;
1419 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001420
1421 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1422 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1423 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1424 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1425 }
John Bauman19bac1e2014-05-06 15:23:49 -04001426
1427 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1428 {
1429 Float4 lod = Float4(0.0f);
1430 Vector4f tmp;
1431 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1432
1433 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1434 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1435 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1436 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1437 }
1438
1439 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1440 {
1441 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1442 {
1443 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1444 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1445 }
1446 else
1447 {
1448 Int index = As<Int>(Float(reg(r, s).x.x));
1449
1450 for(int i = 0; i < 16; i++)
1451 {
1452 if(shader->usesSampler(i))
1453 {
1454 If(index == i)
1455 {
1456 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1457 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1458 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1459 }
1460 }
1461 }
1462 }
1463 }
John Bauman89401822014-05-06 15:04:28 -04001464}