blob: a292a8e2a0c7022463841a7a3b699d33420dfc07 [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 Bauman19bac1e2014-05-06 15:23:49 -0400590 r.o[Pts].y = r.v[PSize].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 Bauman19bac1e2014-05-06 15:23:49 -0400661 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400662
663 mod.x = reg[(src.swizzle >> 0) & 0x03];
664 mod.y = reg[(src.swizzle >> 2) & 0x03];
665 mod.z = reg[(src.swizzle >> 4) & 0x03];
666 mod.w = reg[(src.swizzle >> 6) & 0x03];
667
668 switch(src.modifier)
669 {
John Bauman19bac1e2014-05-06 15:23:49 -0400670 case Shader::MODIFIER_NONE:
John Bauman89401822014-05-06 15:04:28 -0400671 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400672 case Shader::MODIFIER_NEGATE:
John Bauman89401822014-05-06 15:04:28 -0400673 mod.x = -mod.x;
674 mod.y = -mod.y;
675 mod.z = -mod.z;
676 mod.w = -mod.w;
677 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400678 case Shader::MODIFIER_BIAS:
John Bauman89401822014-05-06 15:04:28 -0400679 ASSERT(false); // NOTE: Unimplemented
680 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400681 case Shader::MODIFIER_BIAS_NEGATE:
John Bauman89401822014-05-06 15:04:28 -0400682 ASSERT(false); // NOTE: Unimplemented
683 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400684 case Shader::MODIFIER_SIGN:
John Bauman89401822014-05-06 15:04:28 -0400685 ASSERT(false); // NOTE: Unimplemented
686 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400687 case Shader::MODIFIER_SIGN_NEGATE:
John Bauman89401822014-05-06 15:04:28 -0400688 ASSERT(false); // NOTE: Unimplemented
689 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400690 case Shader::MODIFIER_COMPLEMENT:
John Bauman89401822014-05-06 15:04:28 -0400691 ASSERT(false); // NOTE: Unimplemented
692 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400693 case Shader::MODIFIER_X2:
John Bauman89401822014-05-06 15:04:28 -0400694 ASSERT(false); // NOTE: Unimplemented
695 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400696 case Shader::MODIFIER_X2_NEGATE:
John Bauman89401822014-05-06 15:04:28 -0400697 ASSERT(false); // NOTE: Unimplemented
698 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400699 case Shader::MODIFIER_DZ:
John Bauman89401822014-05-06 15:04:28 -0400700 ASSERT(false); // NOTE: Unimplemented
701 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400702 case Shader::MODIFIER_DW:
John Bauman89401822014-05-06 15:04:28 -0400703 ASSERT(false); // NOTE: Unimplemented
704 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400705 case Shader::MODIFIER_ABS:
John Bauman89401822014-05-06 15:04:28 -0400706 mod.x = Abs(mod.x);
707 mod.y = Abs(mod.y);
708 mod.z = Abs(mod.z);
709 mod.w = Abs(mod.w);
710 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400711 case Shader::MODIFIER_ABS_NEGATE:
John Bauman89401822014-05-06 15:04:28 -0400712 mod.x = -Abs(mod.x);
713 mod.y = -Abs(mod.y);
714 mod.z = -Abs(mod.z);
715 mod.w = -Abs(mod.w);
716 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400717 case Shader::MODIFIER_NOT:
John Bauman89401822014-05-06 15:04:28 -0400718 UNIMPLEMENTED();
719 break;
720 default:
721 ASSERT(false);
722 }
723
724 return mod;
725 }
726
John Bauman19bac1e2014-05-06 15:23:49 -0400727 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400728 {
John Bauman19bac1e2014-05-06 15:23:49 -0400729 Vector4f c;
730
731 int i = src.index + offset;
732
733 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
734 {
735 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
736
737 c.x = c.x.xxxx;
738 c.y = c.y.yyyy;
739 c.z = c.z.zzzz;
740 c.w = c.w.wwww;
741
742 if(localShaderConstants) // Constant may be known at compile time
743 {
744 for(int j = 0; j < shader->getLength(); j++)
745 {
746 const Shader::Instruction &instruction = *shader->getInstruction(j);
747
748 if(instruction.opcode == Shader::OPCODE_DEF)
749 {
750 if(instruction.dst.index == i)
751 {
752 c.x = Float4(instruction.src[0].value[0]);
753 c.y = Float4(instruction.src[0].value[1]);
754 c.z = Float4(instruction.src[0].value[2]);
755 c.w = Float4(instruction.src[0].value[3]);
756
757 break;
758 }
759 }
760 }
761 }
762 }
763 else if(src.rel.type == Shader::PARAMETER_LOOP)
764 {
765 Int loopCounter = r.aL[r.loopDepth];
766
767 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
768
769 c.x = c.x.xxxx;
770 c.y = c.y.yyyy;
771 c.z = c.z.zzzz;
772 c.w = c.w.wwww;
773 }
774 else
775 {
776 if(src.rel.deterministic)
777 {
778 Int a = relativeAddress(r, src);
779
780 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
781
782 c.x = c.x.xxxx;
783 c.y = c.y.yyyy;
784 c.z = c.z.zzzz;
785 c.w = c.w.wwww;
786 }
787 else
788 {
789 int component = src.rel.swizzle & 0x03;
790 Float4 a;
791
792 switch(src.rel.type)
793 {
794 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
795 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
796 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
797 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
798 case Shader::PARAMETER_CONST: a = Float4(*Pointer<Float>(r.data + OFFSET(DrawData,vs.c[src.rel.index][component]))); break;
799 default: ASSERT(false);
800 }
801
802 Int4 index = Int4(i) + RoundInt(a) * Int4(src.rel.scale);
803
804 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
805
806 Int index0 = Extract(index, 0);
807 Int index1 = Extract(index, 1);
808 Int index2 = Extract(index, 2);
809 Int index3 = Extract(index, 3);
810
811 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
812 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
813 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
814 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
815
816 transpose4x4(c.x, c.y, c.z, c.w);
817 }
818 }
819
820 return c;
821 }
822
823 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
824 {
825 ASSERT(var.rel.deterministic);
826
827 if(var.rel.type == Shader::PARAMETER_TEMP)
828 {
829 return RoundInt(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
830 }
831 else if(var.rel.type == Shader::PARAMETER_INPUT)
832 {
833 return RoundInt(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
834 }
835 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
836 {
837 return RoundInt(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
838 }
839 else if(var.rel.type == Shader::PARAMETER_CONST)
840 {
841 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[var.rel.index]));
842
843 return RoundInt(Extract(c, 0)) * var.rel.scale;
844 }
845 else ASSERT(false);
846
847 return 0;
848 }
849
850 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
851 {
852 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
853
854 if(shader->containsBreakInstruction() && !whileTest && instruction->analysisBreak)
855 {
856 enable &= r.enableBreak;
857 }
858
859 if(shader->containsContinueInstruction() && !whileTest && instruction->analysisContinue)
860 {
861 enable &= r.enableContinue;
862 }
863
864 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
865 {
866 enable &= r.enableLeave;
867 }
868
869 return enable;
870 }
871
872 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
873 {
874 Vector4f row0 = reg(r, src1, 0);
875 Vector4f row1 = reg(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400876
877 dst.x = dot3(src0, row0);
878 dst.y = dot3(src0, row1);
879 }
880
John Bauman19bac1e2014-05-06 15:23:49 -0400881 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400882 {
John Bauman19bac1e2014-05-06 15:23:49 -0400883 Vector4f row0 = reg(r, src1, 0);
884 Vector4f row1 = reg(r, src1, 1);
885 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400886
887 dst.x = dot3(src0, row0);
888 dst.y = dot3(src0, row1);
889 dst.z = dot3(src0, row2);
890 }
891
John Bauman19bac1e2014-05-06 15:23:49 -0400892 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400893 {
John Bauman19bac1e2014-05-06 15:23:49 -0400894 Vector4f row0 = reg(r, src1, 0);
895 Vector4f row1 = reg(r, src1, 1);
896 Vector4f row2 = reg(r, src1, 2);
897 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400898
899 dst.x = dot3(src0, row0);
900 dst.y = dot3(src0, row1);
901 dst.z = dot3(src0, row2);
902 dst.w = dot3(src0, row3);
903 }
904
John Bauman19bac1e2014-05-06 15:23:49 -0400905 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400906 {
John Bauman19bac1e2014-05-06 15:23:49 -0400907 Vector4f row0 = reg(r, src1, 0);
908 Vector4f row1 = reg(r, src1, 1);
909 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400910
911 dst.x = dot4(src0, row0);
912 dst.y = dot4(src0, row1);
913 dst.z = dot4(src0, row2);
914 }
915
John Bauman19bac1e2014-05-06 15:23:49 -0400916 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400917 {
John Bauman19bac1e2014-05-06 15:23:49 -0400918 Vector4f row0 = reg(r, src1, 0);
919 Vector4f row1 = reg(r, src1, 1);
920 Vector4f row2 = reg(r, src1, 2);
921 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400922
923 dst.x = dot4(src0, row0);
924 dst.y = dot4(src0, row1);
925 dst.z = dot4(src0, row2);
926 dst.w = dot4(src0, row3);
927 }
928
929 void VertexProgram::BREAK(Registers &r)
930 {
931 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
932 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
933
934 if(breakDepth == 0)
935 {
John Bauman19bac1e2014-05-06 15:23:49 -0400936 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400937 Nucleus::createBr(endBlock);
938 }
939 else
940 {
941 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
942 Bool allBreak = SignMask(r.enableBreak) == 0x0;
943
John Bauman19bac1e2014-05-06 15:23:49 -0400944 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400945 branch(allBreak, endBlock, deadBlock);
946 }
947
948 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400949 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400950 }
951
John Bauman19bac1e2014-05-06 15:23:49 -0400952 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400953 {
954 Int4 condition;
955
956 switch(control)
957 {
John Bauman19bac1e2014-05-06 15:23:49 -0400958 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
959 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
960 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
961 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
962 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
963 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400964 default:
965 ASSERT(false);
966 }
967
John Bauman19bac1e2014-05-06 15:23:49 -0400968 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -0400969 }
970
971 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
972 {
973 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
974
John Bauman19bac1e2014-05-06 15:23:49 -0400975 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -0400976 {
977 condition = ~condition;
978 }
979
John Bauman19bac1e2014-05-06 15:23:49 -0400980 BREAK(r, condition);
981 }
982
983 void VertexProgram::BREAK(Registers &r, Int4 &condition)
984 {
John Bauman89401822014-05-06 15:04:28 -0400985 condition &= r.enableStack[r.enableIndex];
986
987 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
988 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
989
990 r.enableBreak = r.enableBreak & ~condition;
991 Bool allBreak = SignMask(r.enableBreak) == 0x0;
992
John Bauman19bac1e2014-05-06 15:23:49 -0400993 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400994 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400995
John Bauman89401822014-05-06 15:04:28 -0400996 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400997 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400998 }
999
John Bauman19bac1e2014-05-06 15:23:49 -04001000 void VertexProgram::CONTINUE(Registers &r)
1001 {
1002 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1003 }
1004
1005 void VertexProgram::TEST()
1006 {
1007 whileTest = true;
1008 }
1009
1010 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001011 {
1012 if(!labelBlock[labelIndex])
1013 {
1014 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1015 }
1016
John Bauman19bac1e2014-05-06 15:23:49 -04001017 if(callRetBlock[labelIndex].size() > 1)
1018 {
1019 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1020 }
John Bauman89401822014-05-06 15:04:28 -04001021
John Bauman19bac1e2014-05-06 15:23:49 -04001022 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001023
1024 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001025 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1026
1027 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001028 }
1029
John Bauman19bac1e2014-05-06 15:23:49 -04001030 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001031 {
John Bauman19bac1e2014-05-06 15:23:49 -04001032 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001033 {
John Bauman19bac1e2014-05-06 15:23:49 -04001034 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001035 }
John Bauman19bac1e2014-05-06 15:23:49 -04001036 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001037 {
John Bauman19bac1e2014-05-06 15:23:49 -04001038 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001039 }
1040 else ASSERT(false);
1041 }
1042
John Bauman19bac1e2014-05-06 15:23:49 -04001043 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001044 {
1045 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1046
John Bauman19bac1e2014-05-06 15:23:49 -04001047 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001048 {
1049 condition = !condition;
1050 }
1051
1052 if(!labelBlock[labelIndex])
1053 {
1054 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1055 }
1056
John Bauman19bac1e2014-05-06 15:23:49 -04001057 if(callRetBlock[labelIndex].size() > 1)
1058 {
1059 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1060 }
John Bauman89401822014-05-06 15:04:28 -04001061
John Bauman19bac1e2014-05-06 15:23:49 -04001062 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001063
John Bauman19bac1e2014-05-06 15:23:49 -04001064 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1065 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1066
1067 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001068 }
1069
John Bauman19bac1e2014-05-06 15:23:49 -04001070 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001071 {
1072 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1073
John Bauman19bac1e2014-05-06 15:23:49 -04001074 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001075 {
1076 condition = ~condition;
1077 }
1078
1079 condition &= r.enableStack[r.enableIndex];
1080
1081 if(!labelBlock[labelIndex])
1082 {
1083 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1084 }
1085
John Bauman19bac1e2014-05-06 15:23:49 -04001086 if(callRetBlock[labelIndex].size() > 1)
1087 {
1088 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1089 }
John Bauman89401822014-05-06 15:04:28 -04001090
1091 r.enableIndex++;
1092 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001093 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001094
John Bauman19bac1e2014-05-06 15:23:49 -04001095 Bool notAllFalse = SignMask(condition) != 0;
1096 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1097 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001098
1099 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001100 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001101 }
1102
1103 void VertexProgram::ELSE(Registers &r)
1104 {
1105 ifDepth--;
1106
1107 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1108 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1109
1110 if(isConditionalIf[ifDepth])
1111 {
1112 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001113 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001114
1115 branch(notAllFalse, falseBlock, endBlock);
1116
1117 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1118 }
1119 else
1120 {
1121 Nucleus::createBr(endBlock);
1122 Nucleus::setInsertBlock(falseBlock);
1123 }
1124
1125 ifFalseBlock[ifDepth] = endBlock;
1126
1127 ifDepth++;
1128 }
1129
1130 void VertexProgram::ENDIF(Registers &r)
1131 {
1132 ifDepth--;
1133
1134 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1135
1136 Nucleus::createBr(endBlock);
1137 Nucleus::setInsertBlock(endBlock);
1138
1139 if(isConditionalIf[ifDepth])
1140 {
1141 breakDepth--;
1142 r.enableIndex--;
1143 }
1144 }
1145
John Bauman89401822014-05-06 15:04:28 -04001146 void VertexProgram::ENDLOOP(Registers &r)
1147 {
1148 loopRepDepth--;
1149
1150 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1151
1152 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1153 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1154
1155 Nucleus::createBr(testBlock);
1156 Nucleus::setInsertBlock(endBlock);
1157
1158 r.loopDepth--;
1159 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1160 }
1161
John Bauman19bac1e2014-05-06 15:23:49 -04001162 void VertexProgram::ENDREP(Registers &r)
1163 {
1164 loopRepDepth--;
1165
1166 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1167 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1168
1169 Nucleus::createBr(testBlock);
1170 Nucleus::setInsertBlock(endBlock);
1171
1172 r.loopDepth--;
1173 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1174 }
1175
1176 void VertexProgram::ENDWHILE(Registers &r)
1177 {
1178 loopRepDepth--;
1179
1180 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1181 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1182
1183 Nucleus::createBr(testBlock);
1184 Nucleus::setInsertBlock(endBlock);
1185
1186 r.enableIndex--;
1187 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1188 whileTest = false;
1189 }
1190
John Bauman89401822014-05-06 15:04:28 -04001191 void VertexProgram::IF(Registers &r, const Src &src)
1192 {
John Bauman19bac1e2014-05-06 15:23:49 -04001193 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001194 {
1195 IFb(r, src);
1196 }
John Bauman19bac1e2014-05-06 15:23:49 -04001197 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001198 {
1199 IFp(r, src);
1200 }
John Bauman19bac1e2014-05-06 15:23:49 -04001201 else
1202 {
1203 Int4 condition = As<Int4>(reg(r, src).x);
1204 IF(r, condition);
1205 }
John Bauman89401822014-05-06 15:04:28 -04001206 }
1207
1208 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1209 {
1210 ASSERT(ifDepth < 24 + 4);
1211
1212 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1213
John Bauman19bac1e2014-05-06 15:23:49 -04001214 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001215 {
John Bauman19bac1e2014-05-06 15:23:49 -04001216 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001217 }
1218
1219 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1220 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1221
1222 branch(condition, trueBlock, falseBlock);
1223
1224 isConditionalIf[ifDepth] = false;
1225 ifFalseBlock[ifDepth] = falseBlock;
1226
1227 ifDepth++;
1228 }
1229
John Bauman19bac1e2014-05-06 15:23:49 -04001230 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001231 {
1232 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1233
John Bauman19bac1e2014-05-06 15:23:49 -04001234 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001235 {
1236 condition = ~condition;
1237 }
1238
John Bauman19bac1e2014-05-06 15:23:49 -04001239 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001240 }
1241
John Bauman19bac1e2014-05-06 15:23:49 -04001242 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001243 {
1244 Int4 condition;
1245
1246 switch(control)
1247 {
John Bauman19bac1e2014-05-06 15:23:49 -04001248 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1249 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1250 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1251 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1252 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1253 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001254 default:
1255 ASSERT(false);
1256 }
1257
John Bauman19bac1e2014-05-06 15:23:49 -04001258 IF(r, condition);
1259 }
1260
1261 void VertexProgram::IF(Registers &r, Int4 &condition)
1262 {
John Bauman89401822014-05-06 15:04:28 -04001263 condition &= r.enableStack[r.enableIndex];
1264
1265 r.enableIndex++;
1266 r.enableStack[r.enableIndex] = condition;
1267
1268 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1269 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1270
John Bauman19bac1e2014-05-06 15:23:49 -04001271 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001272
1273 branch(notAllFalse, trueBlock, falseBlock);
1274
1275 isConditionalIf[ifDepth] = true;
1276 ifFalseBlock[ifDepth] = falseBlock;
1277
1278 ifDepth++;
1279 breakDepth++;
1280 }
1281
1282 void VertexProgram::LABEL(int labelIndex)
1283 {
John Bauman19bac1e2014-05-06 15:23:49 -04001284 if(!labelBlock[labelIndex])
1285 {
1286 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1287 }
1288
John Bauman89401822014-05-06 15:04:28 -04001289 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001290 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001291 }
1292
1293 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1294 {
1295 r.loopDepth++;
1296
1297 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1298 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1299 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1300
1301 // FIXME: Compiles to two instructions?
1302 If(r.increment[r.loopDepth] == 0)
1303 {
1304 r.increment[r.loopDepth] = 1;
1305 }
1306
1307 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1308 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1309 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1310
1311 loopRepTestBlock[loopRepDepth] = testBlock;
1312 loopRepEndBlock[loopRepDepth] = endBlock;
1313
1314 // FIXME: jump(testBlock)
1315 Nucleus::createBr(testBlock);
1316 Nucleus::setInsertBlock(testBlock);
1317
1318 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1319 Nucleus::setInsertBlock(loopBlock);
1320
1321 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1322
1323 loopRepDepth++;
1324 breakDepth = 0;
1325 }
1326
1327 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1328 {
1329 r.loopDepth++;
1330
1331 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1332 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1333
1334 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1335 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1336 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1337
1338 loopRepTestBlock[loopRepDepth] = testBlock;
1339 loopRepEndBlock[loopRepDepth] = endBlock;
1340
1341 // FIXME: jump(testBlock)
1342 Nucleus::createBr(testBlock);
1343 Nucleus::setInsertBlock(testBlock);
1344
1345 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1346 Nucleus::setInsertBlock(loopBlock);
1347
1348 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1349
1350 loopRepDepth++;
1351 breakDepth = 0;
1352 }
1353
John Bauman19bac1e2014-05-06 15:23:49 -04001354 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1355 {
1356 r.enableIndex++;
1357
1358 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1359 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1360 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1361
1362 loopRepTestBlock[loopRepDepth] = testBlock;
1363 loopRepEndBlock[loopRepDepth] = endBlock;
1364
1365 Int4 restoreBreak = r.enableBreak;
1366 Int4 restoreContinue = r.enableContinue;
1367
1368 // FIXME: jump(testBlock)
1369 Nucleus::createBr(testBlock);
1370 Nucleus::setInsertBlock(testBlock);
1371 r.enableContinue = restoreContinue;
1372
1373 Vector4f &src = reg(r, temporaryRegister);
1374 Int4 condition = As<Int4>(src.x);
1375 condition &= r.enableStack[r.enableIndex - 1];
1376 r.enableStack[r.enableIndex] = condition;
1377
1378 Bool notAllFalse = SignMask(condition) != 0;
1379 branch(notAllFalse, loopBlock, endBlock);
1380
1381 Nucleus::setInsertBlock(endBlock);
1382 r.enableBreak = restoreBreak;
1383
1384 Nucleus::setInsertBlock(loopBlock);
1385
1386 loopRepDepth++;
1387 breakDepth = 0;
1388 }
1389
John Bauman89401822014-05-06 15:04:28 -04001390 void VertexProgram::RET(Registers &r)
1391 {
John Bauman19bac1e2014-05-06 15:23:49 -04001392 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001393 {
1394 returnBlock = Nucleus::createBasicBlock();
1395 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001396 }
1397 else
1398 {
John Bauman89401822014-05-06 15:04:28 -04001399 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001400
John Bauman19bac1e2014-05-06 15:23:49 -04001401 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001402 {
John Bauman19bac1e2014-05-06 15:23:49 -04001403 // FIXME: Encapsulate
1404 UInt index = r.callStack[--r.stackIndex];
1405
1406 llvm::Value *value = Nucleus::createLoad(index.address);
1407 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1408
1409 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1410 {
1411 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1412 }
1413 }
1414 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1415 {
1416 Nucleus::createBr(callRetBlock[currentLabel][0]);
1417 }
1418 else // Function isn't called
1419 {
1420 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001421 }
1422
1423 Nucleus::setInsertBlock(unreachableBlock);
1424 Nucleus::createUnreachable();
1425 }
1426 }
1427
John Bauman19bac1e2014-05-06 15:23:49 -04001428 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001429 {
John Bauman19bac1e2014-05-06 15:23:49 -04001430 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001431
John Bauman19bac1e2014-05-06 15:23:49 -04001432 // FIXME: Return from function if all instances left
1433 // FIXME: Use enableLeave in other control-flow constructs
1434 }
John Bauman89401822014-05-06 15:04:28 -04001435
John Bauman19bac1e2014-05-06 15:23:49 -04001436 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1437 {
1438 Vector4f tmp;
1439 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001440
1441 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1442 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1443 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1444 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1445 }
John Bauman19bac1e2014-05-06 15:23:49 -04001446
1447 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1448 {
1449 Float4 lod = Float4(0.0f);
1450 Vector4f tmp;
1451 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1452
1453 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1454 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1455 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1456 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1457 }
1458
1459 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1460 {
1461 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1462 {
1463 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1464 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1465 }
1466 else
1467 {
1468 Int index = As<Int>(Float(reg(r, s).x.x));
1469
1470 for(int i = 0; i < 16; i++)
1471 {
1472 if(shader->usesSampler(i))
1473 {
1474 If(index == i)
1475 {
1476 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1477 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1478 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1479 }
1480 }
1481 }
1482 }
1483 }
John Bauman89401822014-05-06 15:04:28 -04001484}