blob: 5a516c42da35b990fb966af3e2b3115fff6d437f [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Baumand4ae8632014-05-06 16:18:33 -04003// Copyright(c) 2005-2013 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
John Bauman89401822014-05-06 15:04:28 -040021namespace sw
22{
John Bauman19bac1e2014-05-06 15:23:49 -040023 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader) : VertexRoutine(state, shader)
John Bauman89401822014-05-06 15:04:28 -040024 {
John Bauman89401822014-05-06 15:04:28 -040025 ifDepth = 0;
26 loopRepDepth = 0;
27 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040028 currentLabel = -1;
29 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040030
31 for(int i = 0; i < 2048; i++)
32 {
33 labelBlock[i] = 0;
34 }
35 }
36
37 VertexProgram::~VertexProgram()
38 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040039 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040040 {
41 delete sampler[i];
42 }
43 }
44
45 void VertexProgram::pipeline(Registers &r)
46 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040047 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040048 {
49 sampler[i] = new SamplerCore(r.constants, state.samplerState[i]);
50 }
51
52 if(!state.preTransformed)
53 {
John Bauman19bac1e2014-05-06 15:23:49 -040054 program(r);
John Bauman89401822014-05-06 15:04:28 -040055 }
56 else
57 {
58 passThrough(r);
59 }
60 }
61
John Bauman19bac1e2014-05-06 15:23:49 -040062 void VertexProgram::program(Registers &r)
John Bauman89401822014-05-06 15:04:28 -040063 {
John Bauman19bac1e2014-05-06 15:23:49 -040064 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040065
John Bauman19bac1e2014-05-06 15:23:49 -040066 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040067
68 r.enableIndex = 0;
69 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040070
Nicolas Capens4677a5f2014-05-06 16:42:26 -040071 if(shader->containsLeaveInstruction())
72 {
73 r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
74 }
75
John Bauman19bac1e2014-05-06 15:23:49 -040076 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050077 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -040078 {
John Bauman19bac1e2014-05-06 15:23:49 -040079 const Shader::Instruction *instruction = shader->getInstruction(i);
80 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -040081
John Bauman19bac1e2014-05-06 15:23:49 -040082 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
83 {
84 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -040085
John Bauman19bac1e2014-05-06 15:23:49 -040086 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
87 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
88 }
89 }
90
Alexis Hetu903e0252014-11-25 14:25:32 -050091 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -040092 {
93 const Shader::Instruction *instruction = shader->getInstruction(i);
94 Shader::Opcode opcode = instruction->opcode;
95
96 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -040097 {
98 continue;
99 }
100
John Bauman19bac1e2014-05-06 15:23:49 -0400101 Dst dst = instruction->dst;
102 Src src0 = instruction->src[0];
103 Src src1 = instruction->src[1];
104 Src src2 = instruction->src[2];
John Bauman89401822014-05-06 15:04:28 -0400105
John Bauman19bac1e2014-05-06 15:23:49 -0400106 bool predicate = instruction->predicate;
107 int size = shader->size(opcode);
108 Usage usage = instruction->usage;
109 unsigned char usageIndex = instruction->usageIndex;
110 Control control = instruction->control;
111 bool integer = dst.type == Shader::PARAMETER_ADDR;
112 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400113
John Bauman19bac1e2014-05-06 15:23:49 -0400114 Vector4f d;
115 Vector4f s0;
116 Vector4f s1;
117 Vector4f s2;
John Bauman89401822014-05-06 15:04:28 -0400118
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400119 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(r, src0);
120 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(r, src1);
121 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(r, src2);
John Bauman89401822014-05-06 15:04:28 -0400122
123 switch(opcode)
124 {
John Bauman19bac1e2014-05-06 15:23:49 -0400125 case Shader::OPCODE_VS_1_0: break;
126 case Shader::OPCODE_VS_1_1: break;
127 case Shader::OPCODE_VS_2_0: break;
128 case Shader::OPCODE_VS_2_x: break;
129 case Shader::OPCODE_VS_2_sw: break;
130 case Shader::OPCODE_VS_3_0: break;
131 case Shader::OPCODE_VS_3_sw: break;
132 case Shader::OPCODE_DCL: break;
133 case Shader::OPCODE_DEF: break;
134 case Shader::OPCODE_DEFI: break;
135 case Shader::OPCODE_DEFB: break;
136 case Shader::OPCODE_NOP: break;
137 case Shader::OPCODE_ABS: abs(d, s0); break;
138 case Shader::OPCODE_ADD: add(d, s0, s1); break;
139 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
140 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
141 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
142 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
143 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
144 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
145 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
146 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
147 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
148 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
149 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
150 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
151 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
152 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
153 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
154 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
155 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
156 case Shader::OPCODE_ATT: att(d, s0, s1); break;
157 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
158 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
159 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
160 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
161 case Shader::OPCODE_FRC: frc(d, s0); break;
162 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
163 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400164 case Shader::OPCODE_ROUND: round(d, s0); break;
Alexis Hetu8e851c12015-06-04 11:30:54 -0400165 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400166 case Shader::OPCODE_CEIL: ceil(d, s0); break;
167 case Shader::OPCODE_LIT: lit(d, s0); break;
168 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
169 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
170 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
171 case Shader::OPCODE_LOG: log(d, s0, pp); break;
172 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
173 case Shader::OPCODE_STEP: step(d, s0, s1); break;
174 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
175 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
176 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
177 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
178 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
179 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
180 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
181 case Shader::OPCODE_MAX: max(d, s0, s1); break;
182 case Shader::OPCODE_MIN: min(d, s0, s1); break;
183 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
184 case Shader::OPCODE_MOVA: mov(d, s0); break;
185 case Shader::OPCODE_F2B: f2b(d, s0); break;
186 case Shader::OPCODE_B2F: b2f(d, s0); break;
187 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
188 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
189 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
190 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
191 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
192 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
193 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
194 case Shader::OPCODE_DIV: div(d, s0, s1); break;
195 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
196 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
197 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
198 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
199 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
200 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
201 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
202 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
203 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
204 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
205 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
206 case Shader::OPCODE_SGE: step(d, s1, s0); break;
207 case Shader::OPCODE_SGN: sgn(d, s0); break;
208 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
209 case Shader::OPCODE_COS: cos(d, s0, pp); break;
210 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
211 case Shader::OPCODE_TAN: tan(d, s0); break;
212 case Shader::OPCODE_ACOS: acos(d, s0); break;
213 case Shader::OPCODE_ASIN: asin(d, s0); break;
214 case Shader::OPCODE_ATAN: atan(d, s0); break;
215 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400216 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
217 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
218 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
219 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
220 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
221 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400222 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
223 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
224 case Shader::OPCODE_BREAK: BREAK(r); break;
225 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
226 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
227 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
228 case Shader::OPCODE_TEST: TEST(); break;
229 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
230 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
231 case Shader::OPCODE_ELSE: ELSE(r); break;
232 case Shader::OPCODE_ENDIF: ENDIF(r); break;
233 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
234 case Shader::OPCODE_ENDREP: ENDREP(r); break;
235 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
236 case Shader::OPCODE_IF: IF(r, src0); break;
237 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
238 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
239 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
240 case Shader::OPCODE_REP: REP(r, src0); break;
241 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
242 case Shader::OPCODE_RET: RET(r); break;
243 case Shader::OPCODE_LEAVE: LEAVE(r); break;
244 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
245 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
246 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
247 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
248 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
249 case Shader::OPCODE_ALL: all(d.x, s0); break;
250 case Shader::OPCODE_ANY: any(d.x, s0); break;
251 case Shader::OPCODE_NOT: not(d, s0); break;
252 case Shader::OPCODE_OR: or(d.x, s0.x, s1.x); break;
253 case Shader::OPCODE_XOR: xor(d.x, s0.x, s1.x); break;
254 case Shader::OPCODE_AND: and(d.x, s0.x, s1.x); break;
255 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1); break;
256 case Shader::OPCODE_TEX: TEX(r, d, s0, src1); break;
257 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400258 default:
259 ASSERT(false);
260 }
261
John Bauman19bac1e2014-05-06 15:23:49 -0400262 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400263 {
John Bauman19bac1e2014-05-06 15:23:49 -0400264 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400265 {
John Bauman19bac1e2014-05-06 15:23:49 -0400266 switch(opcode)
267 {
268 case Shader::OPCODE_DIV:
269 if(dst.x) d.x = Trunc(d.x);
270 if(dst.y) d.y = Trunc(d.y);
271 if(dst.z) d.z = Trunc(d.z);
272 if(dst.w) d.w = Trunc(d.w);
273 break;
274 default:
275 break; // No truncation to integer required when arguments are integer
276 }
John Bauman89401822014-05-06 15:04:28 -0400277 }
278
John Bauman19bac1e2014-05-06 15:23:49 -0400279 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400280 {
John Bauman19bac1e2014-05-06 15:23:49 -0400281 if(dst.x) d.x = Max(d.x, Float4(0.0f));
282 if(dst.y) d.y = Max(d.y, Float4(0.0f));
283 if(dst.z) d.z = Max(d.z, Float4(0.0f));
284 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400285
John Bauman19bac1e2014-05-06 15:23:49 -0400286 if(dst.x) d.x = Min(d.x, Float4(1.0f));
287 if(dst.y) d.y = Min(d.y, Float4(1.0f));
288 if(dst.z) d.z = Min(d.z, Float4(1.0f));
289 if(dst.w) d.w = Min(d.w, Float4(1.0f));
290 }
291
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400292 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400293 {
294 Vector4f pDst; // FIXME: Rename
295
296 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400297 {
John Bauman19bac1e2014-05-06 15:23:49 -0400298 case Shader::PARAMETER_VOID: break;
299 case Shader::PARAMETER_TEMP:
300 if(dst.rel.type == Shader::PARAMETER_VOID)
301 {
302 if(dst.x) pDst.x = r.r[dst.index].x;
303 if(dst.y) pDst.y = r.r[dst.index].y;
304 if(dst.z) pDst.z = r.r[dst.index].z;
305 if(dst.w) pDst.w = r.r[dst.index].w;
306 }
307 else
308 {
309 Int a = relativeAddress(r, dst);
310
311 if(dst.x) pDst.x = r.r[dst.index + a].x;
312 if(dst.y) pDst.y = r.r[dst.index + a].y;
313 if(dst.z) pDst.z = r.r[dst.index + a].z;
314 if(dst.w) pDst.w = r.r[dst.index + a].w;
315 }
316 break;
317 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
318 case Shader::PARAMETER_RASTOUT:
319 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400320 {
321 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400322 if(dst.x) pDst.x = r.o[Pos].x;
323 if(dst.y) pDst.y = r.o[Pos].y;
324 if(dst.z) pDst.z = r.o[Pos].z;
325 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400326 break;
327 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400328 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400329 break;
330 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400331 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400332 break;
333 default:
334 ASSERT(false);
335 }
336 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400337 case Shader::PARAMETER_ATTROUT:
338 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
339 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
340 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
341 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400342 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400343 case Shader::PARAMETER_TEXCRDOUT:
344 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400345 if(version < 0x0300)
346 {
John Bauman19bac1e2014-05-06 15:23:49 -0400347 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
348 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
349 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
350 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400351 }
352 else
353 {
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400355 {
John Bauman19bac1e2014-05-06 15:23:49 -0400356 if(dst.x) pDst.x = r.o[dst.index].x;
357 if(dst.y) pDst.y = r.o[dst.index].y;
358 if(dst.z) pDst.z = r.o[dst.index].z;
359 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400360 }
John Bauman19bac1e2014-05-06 15:23:49 -0400361 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400362 {
363 Int aL = r.aL[r.loopDepth];
364
John Bauman19bac1e2014-05-06 15:23:49 -0400365 if(dst.x) pDst.x = r.o[dst.index + aL].x;
366 if(dst.y) pDst.y = r.o[dst.index + aL].y;
367 if(dst.z) pDst.z = r.o[dst.index + aL].z;
368 if(dst.w) pDst.w = r.o[dst.index + aL].w;
369 }
370 else
371 {
372 Int a = relativeAddress(r, dst);
373
374 if(dst.x) pDst.x = r.o[dst.index + a].x;
375 if(dst.y) pDst.y = r.o[dst.index + a].y;
376 if(dst.z) pDst.z = r.o[dst.index + a].z;
377 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400378 }
379 }
380 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400381 case Shader::PARAMETER_LABEL: break;
382 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
383 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400384 default:
385 ASSERT(false);
386 }
387
John Bauman19bac1e2014-05-06 15:23:49 -0400388 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -0400389
390 Int4 xEnable = enable;
391 Int4 yEnable = enable;
392 Int4 zEnable = enable;
393 Int4 wEnable = enable;
394
395 if(predicate)
396 {
John Bauman19bac1e2014-05-06 15:23:49 -0400397 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400398
399 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
400 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
401 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
402 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
403
John Bauman19bac1e2014-05-06 15:23:49 -0400404 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400405 {
John Bauman19bac1e2014-05-06 15:23:49 -0400406 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
407 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
408 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
409 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400410 }
411 else
412 {
John Bauman19bac1e2014-05-06 15:23:49 -0400413 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
414 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
415 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
416 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400417 }
418 }
419
John Bauman19bac1e2014-05-06 15:23:49 -0400420 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
421 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
422 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
423 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400424
John Bauman19bac1e2014-05-06 15:23:49 -0400425 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
426 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
427 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
428 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400429 }
430
John Bauman19bac1e2014-05-06 15:23:49 -0400431 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400432 {
John Bauman19bac1e2014-05-06 15:23:49 -0400433 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400434 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400435 case Shader::PARAMETER_TEMP:
436 if(dst.rel.type == Shader::PARAMETER_VOID)
437 {
438 if(dst.x) r.r[dst.index].x = d.x;
439 if(dst.y) r.r[dst.index].y = d.y;
440 if(dst.z) r.r[dst.index].z = d.z;
441 if(dst.w) r.r[dst.index].w = d.w;
442 }
443 else
444 {
445 Int a = relativeAddress(r, dst);
446
447 if(dst.x) r.r[dst.index + a].x = d.x;
448 if(dst.y) r.r[dst.index + a].y = d.y;
449 if(dst.z) r.r[dst.index + a].z = d.z;
450 if(dst.w) r.r[dst.index + a].w = d.w;
451 }
John Bauman89401822014-05-06 15:04:28 -0400452 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400453 case Shader::PARAMETER_ADDR:
454 if(dst.x) r.a0.x = d.x;
455 if(dst.y) r.a0.y = d.y;
456 if(dst.z) r.a0.z = d.z;
457 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400458 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400459 case Shader::PARAMETER_RASTOUT:
460 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400461 {
462 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400463 if(dst.x) r.o[Pos].x = d.x;
464 if(dst.y) r.o[Pos].y = d.y;
465 if(dst.z) r.o[Pos].z = d.z;
466 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400467 break;
468 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400469 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400470 break;
471 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400472 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400473 break;
474 default: ASSERT(false);
475 }
476 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400477 case Shader::PARAMETER_ATTROUT:
478 if(dst.x) r.o[D0 + dst.index].x = d.x;
479 if(dst.y) r.o[D0 + dst.index].y = d.y;
480 if(dst.z) r.o[D0 + dst.index].z = d.z;
481 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400482 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400483 case Shader::PARAMETER_TEXCRDOUT:
484 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400485 if(version < 0x0300)
486 {
John Bauman19bac1e2014-05-06 15:23:49 -0400487 if(dst.x) r.o[T0 + dst.index].x = d.x;
488 if(dst.y) r.o[T0 + dst.index].y = d.y;
489 if(dst.z) r.o[T0 + dst.index].z = d.z;
490 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400491 }
492 else
493 {
John Bauman19bac1e2014-05-06 15:23:49 -0400494 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400495 {
John Bauman19bac1e2014-05-06 15:23:49 -0400496 if(dst.x) r.o[dst.index].x = d.x;
497 if(dst.y) r.o[dst.index].y = d.y;
498 if(dst.z) r.o[dst.index].z = d.z;
499 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400500 }
John Bauman19bac1e2014-05-06 15:23:49 -0400501 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400502 {
503 Int aL = r.aL[r.loopDepth];
504
John Bauman19bac1e2014-05-06 15:23:49 -0400505 if(dst.x) r.o[dst.index + aL].x = d.x;
506 if(dst.y) r.o[dst.index + aL].y = d.y;
507 if(dst.z) r.o[dst.index + aL].z = d.z;
508 if(dst.w) r.o[dst.index + aL].w = d.w;
509 }
510 else
511 {
512 Int a = relativeAddress(r, dst);
513
514 if(dst.x) r.o[dst.index + a].x = d.x;
515 if(dst.y) r.o[dst.index + a].y = d.y;
516 if(dst.z) r.o[dst.index + a].z = d.z;
517 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400518 }
519 }
520 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400521 case Shader::PARAMETER_LABEL: break;
522 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
523 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400524 default:
525 ASSERT(false);
526 }
527 }
528 }
529
John Bauman19bac1e2014-05-06 15:23:49 -0400530 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400531 {
532 Nucleus::setInsertBlock(returnBlock);
533 }
534 }
535
536 void VertexProgram::passThrough(Registers &r)
537 {
John Bauman19bac1e2014-05-06 15:23:49 -0400538 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400539 {
540 for(int i = 0; i < 12; i++)
541 {
John Bauman19bac1e2014-05-06 15:23:49 -0400542 unsigned char usage = shader->output[i][0].usage;
543 unsigned char index = shader->output[i][0].index;
John Bauman89401822014-05-06 15:04:28 -0400544
545 switch(usage)
546 {
547 case 0xFF:
548 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400549 case Shader::USAGE_PSIZE:
550 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400551 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400552 case Shader::USAGE_TEXCOORD:
553 r.o[i].x = r.v[i].x;
554 r.o[i].y = r.v[i].y;
555 r.o[i].z = r.v[i].z;
556 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400557 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400558 case Shader::USAGE_POSITION:
559 r.o[i].x = r.v[i].x;
560 r.o[i].y = r.v[i].y;
561 r.o[i].z = r.v[i].z;
562 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400563 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400564 case Shader::USAGE_COLOR:
565 r.o[i].x = r.v[i].x;
566 r.o[i].y = r.v[i].y;
567 r.o[i].z = r.v[i].z;
568 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400569 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400570 case Shader::USAGE_FOG:
571 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400572 break;
573 default:
574 ASSERT(false);
575 }
576 }
577 }
578 else
579 {
John Bauman19bac1e2014-05-06 15:23:49 -0400580 r.o[Pos].x = r.v[PositionT].x;
581 r.o[Pos].y = r.v[PositionT].y;
582 r.o[Pos].z = r.v[PositionT].z;
583 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400584
585 for(int i = 0; i < 2; i++)
586 {
John Bauman19bac1e2014-05-06 15:23:49 -0400587 r.o[D0 + i].x = r.v[Color0 + i].x;
588 r.o[D0 + i].y = r.v[Color0 + i].y;
589 r.o[D0 + i].z = r.v[Color0 + i].z;
590 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400591 }
592
593 for(int i = 0; i < 8; i++)
594 {
John Bauman19bac1e2014-05-06 15:23:49 -0400595 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
596 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
597 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
598 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400599 }
600
John Bauman66b8ab22014-05-06 15:57:45 -0400601 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400602 }
603 }
604
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400605 Vector4f VertexProgram::fetchRegisterF(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400606 {
607 int i = src.index + offset;
608
John Bauman19bac1e2014-05-06 15:23:49 -0400609 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -0400610
John Bauman89401822014-05-06 15:04:28 -0400611 switch(src.type)
612 {
John Bauman19bac1e2014-05-06 15:23:49 -0400613 case Shader::PARAMETER_TEMP:
614 if(src.rel.type == Shader::PARAMETER_VOID)
615 {
616 reg = r.r[i];
617 }
618 else
619 {
620 reg = r.r[i + relativeAddress(r, src)];
621 }
622 break;
623 case Shader::PARAMETER_CONST:
624 reg = readConstant(r, src, offset);
625 break;
626 case Shader::PARAMETER_INPUT:
627 if(src.rel.type == Shader::PARAMETER_VOID)
628 {
629 reg = r.v[i];
630 }
631 else
632 {
633 reg = r.v[i + relativeAddress(r, src)];
634 }
635 break;
636 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
637 case Shader::PARAMETER_FLOAT4LITERAL:
638 reg.x = Float4(src.value[0]);
639 reg.y = Float4(src.value[1]);
640 reg.z = Float4(src.value[2]);
641 reg.w = Float4(src.value[3]);
642 break;
643 case Shader::PARAMETER_ADDR: reg = r.a0; break;
644 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
645 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
646 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
647 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
648 case Shader::PARAMETER_SAMPLER:
649 if(src.rel.type == Shader::PARAMETER_VOID)
650 {
651 reg.x = As<Float4>(Int4(i));
652 }
653 else if(src.rel.type == Shader::PARAMETER_TEMP)
654 {
655 reg.x = As<Float4>(Int4(i) + RoundInt(r.r[src.rel.index].x));
656 }
657 return reg;
658 case Shader::PARAMETER_OUTPUT:
659 if(src.rel.type == Shader::PARAMETER_VOID)
660 {
661 reg = r.o[i];
662 }
663 else
664 {
665 reg = r.o[i + relativeAddress(r, src)];
666 }
667 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400668 case Shader::PARAMETER_MISCTYPE:
669 reg.x = Float(r.instanceID);
670 return reg;
John Bauman89401822014-05-06 15:04:28 -0400671 default:
672 ASSERT(false);
673 }
674
John Bauman66b8ab22014-05-06 15:57:45 -0400675 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
676 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
677 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
678 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400679
John Bauman66b8ab22014-05-06 15:57:45 -0400680 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400681
682 switch(src.modifier)
683 {
John Bauman19bac1e2014-05-06 15:23:49 -0400684 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400685 mod.x = x;
686 mod.y = y;
687 mod.z = z;
688 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400689 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400690 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400691 mod.x = -x;
692 mod.y = -y;
693 mod.z = -z;
694 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400695 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400696 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400697 mod.x = Abs(x);
698 mod.y = Abs(y);
699 mod.z = Abs(z);
700 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400701 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400702 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400703 mod.x = -Abs(x);
704 mod.y = -Abs(y);
705 mod.z = -Abs(z);
706 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400707 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400708 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400709 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
710 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
711 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
712 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400713 break;
714 default:
715 ASSERT(false);
716 }
717
718 return mod;
719 }
720
John Bauman19bac1e2014-05-06 15:23:49 -0400721 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400722 {
John Bauman19bac1e2014-05-06 15:23:49 -0400723 Vector4f c;
724
725 int i = src.index + offset;
726
727 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
728 {
729 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
730
731 c.x = c.x.xxxx;
732 c.y = c.y.yyyy;
733 c.z = c.z.zzzz;
734 c.w = c.w.wwww;
735
Nicolas Capenseafdb222015-05-15 15:24:08 -0400736 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400737 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500738 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400739 {
740 const Shader::Instruction &instruction = *shader->getInstruction(j);
741
742 if(instruction.opcode == Shader::OPCODE_DEF)
743 {
744 if(instruction.dst.index == i)
745 {
746 c.x = Float4(instruction.src[0].value[0]);
747 c.y = Float4(instruction.src[0].value[1]);
748 c.z = Float4(instruction.src[0].value[2]);
749 c.w = Float4(instruction.src[0].value[3]);
750
751 break;
752 }
753 }
754 }
755 }
756 }
757 else if(src.rel.type == Shader::PARAMETER_LOOP)
758 {
759 Int loopCounter = r.aL[r.loopDepth];
760
761 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
762
763 c.x = c.x.xxxx;
764 c.y = c.y.yyyy;
765 c.z = c.z.zzzz;
766 c.w = c.w.wwww;
767 }
768 else
769 {
770 if(src.rel.deterministic)
771 {
772 Int a = relativeAddress(r, src);
773
774 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
775
776 c.x = c.x.xxxx;
777 c.y = c.y.yyyy;
778 c.z = c.z.zzzz;
779 c.w = c.w.wwww;
780 }
781 else
782 {
783 int component = src.rel.swizzle & 0x03;
784 Float4 a;
785
786 switch(src.rel.type)
787 {
788 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
789 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
790 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
791 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400792 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 -0400793 default: ASSERT(false);
794 }
795
796 Int4 index = Int4(i) + RoundInt(a) * Int4(src.rel.scale);
797
798 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
799
800 Int index0 = Extract(index, 0);
801 Int index1 = Extract(index, 1);
802 Int index2 = Extract(index, 2);
803 Int index3 = Extract(index, 3);
804
805 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
806 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
807 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
808 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
809
810 transpose4x4(c.x, c.y, c.z, c.w);
811 }
812 }
813
814 return c;
815 }
816
817 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
818 {
819 ASSERT(var.rel.deterministic);
820
821 if(var.rel.type == Shader::PARAMETER_TEMP)
822 {
823 return RoundInt(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
824 }
825 else if(var.rel.type == Shader::PARAMETER_INPUT)
826 {
827 return RoundInt(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
828 }
829 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
830 {
831 return RoundInt(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
832 }
833 else if(var.rel.type == Shader::PARAMETER_CONST)
834 {
835 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[var.rel.index]));
836
837 return RoundInt(Extract(c, 0)) * var.rel.scale;
838 }
839 else ASSERT(false);
840
841 return 0;
842 }
843
844 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
845 {
846 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -0400847
848 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400849 {
John Baumand4ae8632014-05-06 16:18:33 -0400850 if(shader->containsBreakInstruction() && instruction->analysisBreak)
851 {
852 enable &= r.enableBreak;
853 }
John Bauman19bac1e2014-05-06 15:23:49 -0400854
John Baumand4ae8632014-05-06 16:18:33 -0400855 if(shader->containsContinueInstruction() && instruction->analysisContinue)
856 {
857 enable &= r.enableContinue;
858 }
John Bauman19bac1e2014-05-06 15:23:49 -0400859
John Baumand4ae8632014-05-06 16:18:33 -0400860 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
861 {
862 enable &= r.enableLeave;
863 }
John Bauman19bac1e2014-05-06 15:23:49 -0400864 }
865
866 return enable;
867 }
868
869 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
870 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400871 Vector4f row0 = fetchRegisterF(r, src1, 0);
872 Vector4f row1 = fetchRegisterF(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400873
874 dst.x = dot3(src0, row0);
875 dst.y = dot3(src0, row1);
876 }
877
John Bauman19bac1e2014-05-06 15:23:49 -0400878 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400879 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400880 Vector4f row0 = fetchRegisterF(r, src1, 0);
881 Vector4f row1 = fetchRegisterF(r, src1, 1);
882 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400883
884 dst.x = dot3(src0, row0);
885 dst.y = dot3(src0, row1);
886 dst.z = dot3(src0, row2);
887 }
888
John Bauman19bac1e2014-05-06 15:23:49 -0400889 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400890 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400891 Vector4f row0 = fetchRegisterF(r, src1, 0);
892 Vector4f row1 = fetchRegisterF(r, src1, 1);
893 Vector4f row2 = fetchRegisterF(r, src1, 2);
894 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400895
896 dst.x = dot3(src0, row0);
897 dst.y = dot3(src0, row1);
898 dst.z = dot3(src0, row2);
899 dst.w = dot3(src0, row3);
900 }
901
John Bauman19bac1e2014-05-06 15:23:49 -0400902 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400903 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400904 Vector4f row0 = fetchRegisterF(r, src1, 0);
905 Vector4f row1 = fetchRegisterF(r, src1, 1);
906 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400907
908 dst.x = dot4(src0, row0);
909 dst.y = dot4(src0, row1);
910 dst.z = dot4(src0, row2);
911 }
912
John Bauman19bac1e2014-05-06 15:23:49 -0400913 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400914 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400915 Vector4f row0 = fetchRegisterF(r, src1, 0);
916 Vector4f row1 = fetchRegisterF(r, src1, 1);
917 Vector4f row2 = fetchRegisterF(r, src1, 2);
918 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400919
920 dst.x = dot4(src0, row0);
921 dst.y = dot4(src0, row1);
922 dst.z = dot4(src0, row2);
923 dst.w = dot4(src0, row3);
924 }
925
926 void VertexProgram::BREAK(Registers &r)
927 {
928 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
929 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
930
931 if(breakDepth == 0)
932 {
John Bauman19bac1e2014-05-06 15:23:49 -0400933 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400934 Nucleus::createBr(endBlock);
935 }
936 else
937 {
938 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
939 Bool allBreak = SignMask(r.enableBreak) == 0x0;
940
John Bauman19bac1e2014-05-06 15:23:49 -0400941 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400942 branch(allBreak, endBlock, deadBlock);
943 }
944
945 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400946 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400947 }
948
John Bauman19bac1e2014-05-06 15:23:49 -0400949 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400950 {
951 Int4 condition;
952
953 switch(control)
954 {
John Bauman19bac1e2014-05-06 15:23:49 -0400955 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
956 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
957 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
958 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
959 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
960 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400961 default:
962 ASSERT(false);
963 }
964
John Bauman19bac1e2014-05-06 15:23:49 -0400965 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -0400966 }
967
968 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
969 {
970 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
971
John Bauman19bac1e2014-05-06 15:23:49 -0400972 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -0400973 {
974 condition = ~condition;
975 }
976
John Bauman19bac1e2014-05-06 15:23:49 -0400977 BREAK(r, condition);
978 }
979
980 void VertexProgram::BREAK(Registers &r, Int4 &condition)
981 {
John Bauman89401822014-05-06 15:04:28 -0400982 condition &= r.enableStack[r.enableIndex];
983
984 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
985 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
986
987 r.enableBreak = r.enableBreak & ~condition;
988 Bool allBreak = SignMask(r.enableBreak) == 0x0;
989
John Bauman19bac1e2014-05-06 15:23:49 -0400990 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400991 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400992
John Bauman89401822014-05-06 15:04:28 -0400993 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400994 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400995 }
996
John Bauman19bac1e2014-05-06 15:23:49 -0400997 void VertexProgram::CONTINUE(Registers &r)
998 {
999 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1000 }
1001
1002 void VertexProgram::TEST()
1003 {
1004 whileTest = true;
1005 }
1006
1007 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001008 {
1009 if(!labelBlock[labelIndex])
1010 {
1011 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1012 }
1013
John Bauman19bac1e2014-05-06 15:23:49 -04001014 if(callRetBlock[labelIndex].size() > 1)
1015 {
1016 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1017 }
John Bauman89401822014-05-06 15:04:28 -04001018
John Bauman19bac1e2014-05-06 15:23:49 -04001019 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001020
1021 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001022 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1023
1024 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001025 }
1026
John Bauman19bac1e2014-05-06 15:23:49 -04001027 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001028 {
John Bauman19bac1e2014-05-06 15:23:49 -04001029 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001030 {
John Bauman19bac1e2014-05-06 15:23:49 -04001031 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001032 }
John Bauman19bac1e2014-05-06 15:23:49 -04001033 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001034 {
John Bauman19bac1e2014-05-06 15:23:49 -04001035 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001036 }
1037 else ASSERT(false);
1038 }
1039
John Bauman19bac1e2014-05-06 15:23:49 -04001040 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001041 {
1042 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1043
John Bauman19bac1e2014-05-06 15:23:49 -04001044 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001045 {
1046 condition = !condition;
1047 }
1048
1049 if(!labelBlock[labelIndex])
1050 {
1051 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1052 }
1053
John Bauman19bac1e2014-05-06 15:23:49 -04001054 if(callRetBlock[labelIndex].size() > 1)
1055 {
1056 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1057 }
John Bauman89401822014-05-06 15:04:28 -04001058
John Bauman19bac1e2014-05-06 15:23:49 -04001059 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001060
John Bauman19bac1e2014-05-06 15:23:49 -04001061 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1062 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1063
1064 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001065 }
1066
John Bauman19bac1e2014-05-06 15:23:49 -04001067 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001068 {
1069 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1070
John Bauman19bac1e2014-05-06 15:23:49 -04001071 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001072 {
1073 condition = ~condition;
1074 }
1075
1076 condition &= r.enableStack[r.enableIndex];
1077
1078 if(!labelBlock[labelIndex])
1079 {
1080 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1081 }
1082
John Bauman19bac1e2014-05-06 15:23:49 -04001083 if(callRetBlock[labelIndex].size() > 1)
1084 {
1085 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1086 }
John Bauman89401822014-05-06 15:04:28 -04001087
1088 r.enableIndex++;
1089 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001090 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001091
John Bauman19bac1e2014-05-06 15:23:49 -04001092 Bool notAllFalse = SignMask(condition) != 0;
1093 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1094 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001095
1096 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001097 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001098 }
1099
1100 void VertexProgram::ELSE(Registers &r)
1101 {
1102 ifDepth--;
1103
1104 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1105 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1106
1107 if(isConditionalIf[ifDepth])
1108 {
1109 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001110 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001111
1112 branch(notAllFalse, falseBlock, endBlock);
1113
1114 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1115 }
1116 else
1117 {
1118 Nucleus::createBr(endBlock);
1119 Nucleus::setInsertBlock(falseBlock);
1120 }
1121
1122 ifFalseBlock[ifDepth] = endBlock;
1123
1124 ifDepth++;
1125 }
1126
1127 void VertexProgram::ENDIF(Registers &r)
1128 {
1129 ifDepth--;
1130
1131 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1132
1133 Nucleus::createBr(endBlock);
1134 Nucleus::setInsertBlock(endBlock);
1135
1136 if(isConditionalIf[ifDepth])
1137 {
1138 breakDepth--;
1139 r.enableIndex--;
1140 }
1141 }
1142
John Bauman89401822014-05-06 15:04:28 -04001143 void VertexProgram::ENDLOOP(Registers &r)
1144 {
1145 loopRepDepth--;
1146
1147 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1148
1149 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1150 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1151
1152 Nucleus::createBr(testBlock);
1153 Nucleus::setInsertBlock(endBlock);
1154
1155 r.loopDepth--;
1156 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1157 }
1158
John Bauman19bac1e2014-05-06 15:23:49 -04001159 void VertexProgram::ENDREP(Registers &r)
1160 {
1161 loopRepDepth--;
1162
1163 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1164 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1165
1166 Nucleus::createBr(testBlock);
1167 Nucleus::setInsertBlock(endBlock);
1168
1169 r.loopDepth--;
1170 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1171 }
1172
1173 void VertexProgram::ENDWHILE(Registers &r)
1174 {
1175 loopRepDepth--;
1176
1177 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1178 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1179
1180 Nucleus::createBr(testBlock);
1181 Nucleus::setInsertBlock(endBlock);
1182
1183 r.enableIndex--;
1184 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1185 whileTest = false;
1186 }
1187
John Bauman89401822014-05-06 15:04:28 -04001188 void VertexProgram::IF(Registers &r, const Src &src)
1189 {
John Bauman19bac1e2014-05-06 15:23:49 -04001190 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001191 {
1192 IFb(r, src);
1193 }
John Bauman19bac1e2014-05-06 15:23:49 -04001194 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001195 {
1196 IFp(r, src);
1197 }
John Bauman19bac1e2014-05-06 15:23:49 -04001198 else
1199 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001200 Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
John Bauman19bac1e2014-05-06 15:23:49 -04001201 IF(r, condition);
1202 }
John Bauman89401822014-05-06 15:04:28 -04001203 }
1204
1205 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1206 {
1207 ASSERT(ifDepth < 24 + 4);
1208
1209 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1210
John Bauman19bac1e2014-05-06 15:23:49 -04001211 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001212 {
John Bauman19bac1e2014-05-06 15:23:49 -04001213 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001214 }
1215
1216 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1217 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1218
1219 branch(condition, trueBlock, falseBlock);
1220
1221 isConditionalIf[ifDepth] = false;
1222 ifFalseBlock[ifDepth] = falseBlock;
1223
1224 ifDepth++;
1225 }
1226
John Bauman19bac1e2014-05-06 15:23:49 -04001227 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001228 {
1229 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1230
John Bauman19bac1e2014-05-06 15:23:49 -04001231 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001232 {
1233 condition = ~condition;
1234 }
1235
John Bauman19bac1e2014-05-06 15:23:49 -04001236 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001237 }
1238
John Bauman19bac1e2014-05-06 15:23:49 -04001239 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001240 {
1241 Int4 condition;
1242
1243 switch(control)
1244 {
John Bauman19bac1e2014-05-06 15:23:49 -04001245 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1246 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1247 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1248 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1249 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1250 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001251 default:
1252 ASSERT(false);
1253 }
1254
John Bauman19bac1e2014-05-06 15:23:49 -04001255 IF(r, condition);
1256 }
1257
1258 void VertexProgram::IF(Registers &r, Int4 &condition)
1259 {
John Bauman89401822014-05-06 15:04:28 -04001260 condition &= r.enableStack[r.enableIndex];
1261
1262 r.enableIndex++;
1263 r.enableStack[r.enableIndex] = condition;
1264
1265 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1266 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1267
John Bauman19bac1e2014-05-06 15:23:49 -04001268 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001269
1270 branch(notAllFalse, trueBlock, falseBlock);
1271
1272 isConditionalIf[ifDepth] = true;
1273 ifFalseBlock[ifDepth] = falseBlock;
1274
1275 ifDepth++;
1276 breakDepth++;
1277 }
1278
1279 void VertexProgram::LABEL(int labelIndex)
1280 {
John Bauman19bac1e2014-05-06 15:23:49 -04001281 if(!labelBlock[labelIndex])
1282 {
1283 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1284 }
1285
John Bauman89401822014-05-06 15:04:28 -04001286 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001287 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001288 }
1289
1290 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1291 {
1292 r.loopDepth++;
1293
1294 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1295 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1296 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1297
1298 // FIXME: Compiles to two instructions?
1299 If(r.increment[r.loopDepth] == 0)
1300 {
1301 r.increment[r.loopDepth] = 1;
1302 }
1303
1304 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1305 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1306 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1307
1308 loopRepTestBlock[loopRepDepth] = testBlock;
1309 loopRepEndBlock[loopRepDepth] = endBlock;
1310
1311 // FIXME: jump(testBlock)
1312 Nucleus::createBr(testBlock);
1313 Nucleus::setInsertBlock(testBlock);
1314
1315 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1316 Nucleus::setInsertBlock(loopBlock);
1317
1318 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1319
1320 loopRepDepth++;
1321 breakDepth = 0;
1322 }
1323
1324 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1325 {
1326 r.loopDepth++;
1327
1328 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1329 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1330
1331 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1332 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1333 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1334
1335 loopRepTestBlock[loopRepDepth] = testBlock;
1336 loopRepEndBlock[loopRepDepth] = endBlock;
1337
1338 // FIXME: jump(testBlock)
1339 Nucleus::createBr(testBlock);
1340 Nucleus::setInsertBlock(testBlock);
1341
1342 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1343 Nucleus::setInsertBlock(loopBlock);
1344
1345 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1346
1347 loopRepDepth++;
1348 breakDepth = 0;
1349 }
1350
John Bauman19bac1e2014-05-06 15:23:49 -04001351 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1352 {
1353 r.enableIndex++;
1354
1355 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1356 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1357 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1358
1359 loopRepTestBlock[loopRepDepth] = testBlock;
1360 loopRepEndBlock[loopRepDepth] = endBlock;
1361
1362 Int4 restoreBreak = r.enableBreak;
1363 Int4 restoreContinue = r.enableContinue;
1364
1365 // FIXME: jump(testBlock)
1366 Nucleus::createBr(testBlock);
1367 Nucleus::setInsertBlock(testBlock);
1368 r.enableContinue = restoreContinue;
1369
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001370 const Vector4f &src = fetchRegisterF(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001371 Int4 condition = As<Int4>(src.x);
1372 condition &= r.enableStack[r.enableIndex - 1];
1373 r.enableStack[r.enableIndex] = condition;
1374
1375 Bool notAllFalse = SignMask(condition) != 0;
1376 branch(notAllFalse, loopBlock, endBlock);
1377
1378 Nucleus::setInsertBlock(endBlock);
1379 r.enableBreak = restoreBreak;
1380
1381 Nucleus::setInsertBlock(loopBlock);
1382
1383 loopRepDepth++;
1384 breakDepth = 0;
1385 }
1386
John Bauman89401822014-05-06 15:04:28 -04001387 void VertexProgram::RET(Registers &r)
1388 {
John Bauman19bac1e2014-05-06 15:23:49 -04001389 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001390 {
1391 returnBlock = Nucleus::createBasicBlock();
1392 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001393 }
1394 else
1395 {
John Bauman89401822014-05-06 15:04:28 -04001396 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001397
John Bauman19bac1e2014-05-06 15:23:49 -04001398 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001399 {
John Bauman19bac1e2014-05-06 15:23:49 -04001400 // FIXME: Encapsulate
1401 UInt index = r.callStack[--r.stackIndex];
1402
John Bauman66b8ab22014-05-06 15:57:45 -04001403 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001404 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1405
1406 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1407 {
1408 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1409 }
1410 }
1411 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1412 {
1413 Nucleus::createBr(callRetBlock[currentLabel][0]);
1414 }
1415 else // Function isn't called
1416 {
1417 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001418 }
1419
1420 Nucleus::setInsertBlock(unreachableBlock);
1421 Nucleus::createUnreachable();
1422 }
1423 }
1424
John Bauman19bac1e2014-05-06 15:23:49 -04001425 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001426 {
John Bauman19bac1e2014-05-06 15:23:49 -04001427 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001428
John Bauman19bac1e2014-05-06 15:23:49 -04001429 // FIXME: Return from function if all instances left
1430 // FIXME: Use enableLeave in other control-flow constructs
1431 }
John Bauman89401822014-05-06 15:04:28 -04001432
John Bauman19bac1e2014-05-06 15:23:49 -04001433 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1434 {
1435 Vector4f tmp;
1436 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001437
1438 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1439 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1440 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1441 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1442 }
John Bauman19bac1e2014-05-06 15:23:49 -04001443
1444 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1445 {
1446 Float4 lod = Float4(0.0f);
1447 Vector4f tmp;
1448 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1449
1450 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1451 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1452 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1453 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1454 }
1455
1456 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1457 {
1458 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1459 {
1460 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1461 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1462 }
1463 else
1464 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001465 Int index = As<Int>(Float(fetchRegisterF(r, s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001466
1467 for(int i = 0; i < 16; i++)
1468 {
1469 if(shader->usesSampler(i))
1470 {
1471 If(index == i)
1472 {
1473 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1474 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1475 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1476 }
1477 }
1478 }
1479 }
1480 }
John Bauman89401822014-05-06 15:04:28 -04001481}