blob: 14bd5f62093296ba0559b8d583854daabb6538a9 [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;
John Bauman89401822014-05-06 15:04:28 -0400668 default:
669 ASSERT(false);
670 }
671
John Bauman66b8ab22014-05-06 15:57:45 -0400672 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
673 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
674 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
675 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400676
John Bauman66b8ab22014-05-06 15:57:45 -0400677 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400678
679 switch(src.modifier)
680 {
John Bauman19bac1e2014-05-06 15:23:49 -0400681 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400682 mod.x = x;
683 mod.y = y;
684 mod.z = z;
685 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400686 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400687 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400688 mod.x = -x;
689 mod.y = -y;
690 mod.z = -z;
691 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400692 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400693 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400694 mod.x = Abs(x);
695 mod.y = Abs(y);
696 mod.z = Abs(z);
697 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400698 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400699 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400700 mod.x = -Abs(x);
701 mod.y = -Abs(y);
702 mod.z = -Abs(z);
703 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400704 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400705 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400706 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
707 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
708 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
709 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400710 break;
711 default:
712 ASSERT(false);
713 }
714
715 return mod;
716 }
717
John Bauman19bac1e2014-05-06 15:23:49 -0400718 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400719 {
John Bauman19bac1e2014-05-06 15:23:49 -0400720 Vector4f c;
721
722 int i = src.index + offset;
723
724 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
725 {
726 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
727
728 c.x = c.x.xxxx;
729 c.y = c.y.yyyy;
730 c.z = c.z.zzzz;
731 c.w = c.w.wwww;
732
Nicolas Capenseafdb222015-05-15 15:24:08 -0400733 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400734 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500735 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400736 {
737 const Shader::Instruction &instruction = *shader->getInstruction(j);
738
739 if(instruction.opcode == Shader::OPCODE_DEF)
740 {
741 if(instruction.dst.index == i)
742 {
743 c.x = Float4(instruction.src[0].value[0]);
744 c.y = Float4(instruction.src[0].value[1]);
745 c.z = Float4(instruction.src[0].value[2]);
746 c.w = Float4(instruction.src[0].value[3]);
747
748 break;
749 }
750 }
751 }
752 }
753 }
754 else if(src.rel.type == Shader::PARAMETER_LOOP)
755 {
756 Int loopCounter = r.aL[r.loopDepth];
757
758 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
759
760 c.x = c.x.xxxx;
761 c.y = c.y.yyyy;
762 c.z = c.z.zzzz;
763 c.w = c.w.wwww;
764 }
765 else
766 {
767 if(src.rel.deterministic)
768 {
769 Int a = relativeAddress(r, src);
770
771 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
772
773 c.x = c.x.xxxx;
774 c.y = c.y.yyyy;
775 c.z = c.z.zzzz;
776 c.w = c.w.wwww;
777 }
778 else
779 {
780 int component = src.rel.swizzle & 0x03;
781 Float4 a;
782
783 switch(src.rel.type)
784 {
785 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
786 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
787 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
788 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400789 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 -0400790 default: ASSERT(false);
791 }
792
793 Int4 index = Int4(i) + RoundInt(a) * Int4(src.rel.scale);
794
795 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
796
797 Int index0 = Extract(index, 0);
798 Int index1 = Extract(index, 1);
799 Int index2 = Extract(index, 2);
800 Int index3 = Extract(index, 3);
801
802 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
803 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
804 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
805 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
806
807 transpose4x4(c.x, c.y, c.z, c.w);
808 }
809 }
810
811 return c;
812 }
813
814 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
815 {
816 ASSERT(var.rel.deterministic);
817
818 if(var.rel.type == Shader::PARAMETER_TEMP)
819 {
820 return RoundInt(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
821 }
822 else if(var.rel.type == Shader::PARAMETER_INPUT)
823 {
824 return RoundInt(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
825 }
826 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
827 {
828 return RoundInt(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
829 }
830 else if(var.rel.type == Shader::PARAMETER_CONST)
831 {
832 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[var.rel.index]));
833
834 return RoundInt(Extract(c, 0)) * var.rel.scale;
835 }
836 else ASSERT(false);
837
838 return 0;
839 }
840
841 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
842 {
843 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -0400844
845 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400846 {
John Baumand4ae8632014-05-06 16:18:33 -0400847 if(shader->containsBreakInstruction() && instruction->analysisBreak)
848 {
849 enable &= r.enableBreak;
850 }
John Bauman19bac1e2014-05-06 15:23:49 -0400851
John Baumand4ae8632014-05-06 16:18:33 -0400852 if(shader->containsContinueInstruction() && instruction->analysisContinue)
853 {
854 enable &= r.enableContinue;
855 }
John Bauman19bac1e2014-05-06 15:23:49 -0400856
John Baumand4ae8632014-05-06 16:18:33 -0400857 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
858 {
859 enable &= r.enableLeave;
860 }
John Bauman19bac1e2014-05-06 15:23:49 -0400861 }
862
863 return enable;
864 }
865
866 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
867 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400868 Vector4f row0 = fetchRegisterF(r, src1, 0);
869 Vector4f row1 = fetchRegisterF(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400870
871 dst.x = dot3(src0, row0);
872 dst.y = dot3(src0, row1);
873 }
874
John Bauman19bac1e2014-05-06 15:23:49 -0400875 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400876 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400877 Vector4f row0 = fetchRegisterF(r, src1, 0);
878 Vector4f row1 = fetchRegisterF(r, src1, 1);
879 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400880
881 dst.x = dot3(src0, row0);
882 dst.y = dot3(src0, row1);
883 dst.z = dot3(src0, row2);
884 }
885
John Bauman19bac1e2014-05-06 15:23:49 -0400886 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400887 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400888 Vector4f row0 = fetchRegisterF(r, src1, 0);
889 Vector4f row1 = fetchRegisterF(r, src1, 1);
890 Vector4f row2 = fetchRegisterF(r, src1, 2);
891 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400892
893 dst.x = dot3(src0, row0);
894 dst.y = dot3(src0, row1);
895 dst.z = dot3(src0, row2);
896 dst.w = dot3(src0, row3);
897 }
898
John Bauman19bac1e2014-05-06 15:23:49 -0400899 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400900 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400901 Vector4f row0 = fetchRegisterF(r, src1, 0);
902 Vector4f row1 = fetchRegisterF(r, src1, 1);
903 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400904
905 dst.x = dot4(src0, row0);
906 dst.y = dot4(src0, row1);
907 dst.z = dot4(src0, row2);
908 }
909
John Bauman19bac1e2014-05-06 15:23:49 -0400910 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400911 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400912 Vector4f row0 = fetchRegisterF(r, src1, 0);
913 Vector4f row1 = fetchRegisterF(r, src1, 1);
914 Vector4f row2 = fetchRegisterF(r, src1, 2);
915 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400916
917 dst.x = dot4(src0, row0);
918 dst.y = dot4(src0, row1);
919 dst.z = dot4(src0, row2);
920 dst.w = dot4(src0, row3);
921 }
922
923 void VertexProgram::BREAK(Registers &r)
924 {
925 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
926 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
927
928 if(breakDepth == 0)
929 {
John Bauman19bac1e2014-05-06 15:23:49 -0400930 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400931 Nucleus::createBr(endBlock);
932 }
933 else
934 {
935 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
936 Bool allBreak = SignMask(r.enableBreak) == 0x0;
937
John Bauman19bac1e2014-05-06 15:23:49 -0400938 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400939 branch(allBreak, endBlock, deadBlock);
940 }
941
942 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400943 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400944 }
945
John Bauman19bac1e2014-05-06 15:23:49 -0400946 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400947 {
948 Int4 condition;
949
950 switch(control)
951 {
John Bauman19bac1e2014-05-06 15:23:49 -0400952 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
953 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
954 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
955 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
956 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
957 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400958 default:
959 ASSERT(false);
960 }
961
John Bauman19bac1e2014-05-06 15:23:49 -0400962 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -0400963 }
964
965 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
966 {
967 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
968
John Bauman19bac1e2014-05-06 15:23:49 -0400969 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -0400970 {
971 condition = ~condition;
972 }
973
John Bauman19bac1e2014-05-06 15:23:49 -0400974 BREAK(r, condition);
975 }
976
977 void VertexProgram::BREAK(Registers &r, Int4 &condition)
978 {
John Bauman89401822014-05-06 15:04:28 -0400979 condition &= r.enableStack[r.enableIndex];
980
981 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
982 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
983
984 r.enableBreak = r.enableBreak & ~condition;
985 Bool allBreak = SignMask(r.enableBreak) == 0x0;
986
John Bauman19bac1e2014-05-06 15:23:49 -0400987 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400988 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400989
John Bauman89401822014-05-06 15:04:28 -0400990 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400991 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400992 }
993
John Bauman19bac1e2014-05-06 15:23:49 -0400994 void VertexProgram::CONTINUE(Registers &r)
995 {
996 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
997 }
998
999 void VertexProgram::TEST()
1000 {
1001 whileTest = true;
1002 }
1003
1004 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001005 {
1006 if(!labelBlock[labelIndex])
1007 {
1008 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1009 }
1010
John Bauman19bac1e2014-05-06 15:23:49 -04001011 if(callRetBlock[labelIndex].size() > 1)
1012 {
1013 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1014 }
John Bauman89401822014-05-06 15:04:28 -04001015
John Bauman19bac1e2014-05-06 15:23:49 -04001016 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001017
1018 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001019 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1020
1021 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001022 }
1023
John Bauman19bac1e2014-05-06 15:23:49 -04001024 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001025 {
John Bauman19bac1e2014-05-06 15:23:49 -04001026 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001027 {
John Bauman19bac1e2014-05-06 15:23:49 -04001028 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001029 }
John Bauman19bac1e2014-05-06 15:23:49 -04001030 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001031 {
John Bauman19bac1e2014-05-06 15:23:49 -04001032 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001033 }
1034 else ASSERT(false);
1035 }
1036
John Bauman19bac1e2014-05-06 15:23:49 -04001037 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001038 {
1039 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1040
John Bauman19bac1e2014-05-06 15:23:49 -04001041 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001042 {
1043 condition = !condition;
1044 }
1045
1046 if(!labelBlock[labelIndex])
1047 {
1048 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1049 }
1050
John Bauman19bac1e2014-05-06 15:23:49 -04001051 if(callRetBlock[labelIndex].size() > 1)
1052 {
1053 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1054 }
John Bauman89401822014-05-06 15:04:28 -04001055
John Bauman19bac1e2014-05-06 15:23:49 -04001056 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001057
John Bauman19bac1e2014-05-06 15:23:49 -04001058 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1059 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1060
1061 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001062 }
1063
John Bauman19bac1e2014-05-06 15:23:49 -04001064 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001065 {
1066 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1067
John Bauman19bac1e2014-05-06 15:23:49 -04001068 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001069 {
1070 condition = ~condition;
1071 }
1072
1073 condition &= r.enableStack[r.enableIndex];
1074
1075 if(!labelBlock[labelIndex])
1076 {
1077 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1078 }
1079
John Bauman19bac1e2014-05-06 15:23:49 -04001080 if(callRetBlock[labelIndex].size() > 1)
1081 {
1082 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1083 }
John Bauman89401822014-05-06 15:04:28 -04001084
1085 r.enableIndex++;
1086 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001087 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001088
John Bauman19bac1e2014-05-06 15:23:49 -04001089 Bool notAllFalse = SignMask(condition) != 0;
1090 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1091 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001092
1093 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001094 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001095 }
1096
1097 void VertexProgram::ELSE(Registers &r)
1098 {
1099 ifDepth--;
1100
1101 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1102 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1103
1104 if(isConditionalIf[ifDepth])
1105 {
1106 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001107 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001108
1109 branch(notAllFalse, falseBlock, endBlock);
1110
1111 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1112 }
1113 else
1114 {
1115 Nucleus::createBr(endBlock);
1116 Nucleus::setInsertBlock(falseBlock);
1117 }
1118
1119 ifFalseBlock[ifDepth] = endBlock;
1120
1121 ifDepth++;
1122 }
1123
1124 void VertexProgram::ENDIF(Registers &r)
1125 {
1126 ifDepth--;
1127
1128 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1129
1130 Nucleus::createBr(endBlock);
1131 Nucleus::setInsertBlock(endBlock);
1132
1133 if(isConditionalIf[ifDepth])
1134 {
1135 breakDepth--;
1136 r.enableIndex--;
1137 }
1138 }
1139
John Bauman89401822014-05-06 15:04:28 -04001140 void VertexProgram::ENDLOOP(Registers &r)
1141 {
1142 loopRepDepth--;
1143
1144 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1145
1146 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1147 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1148
1149 Nucleus::createBr(testBlock);
1150 Nucleus::setInsertBlock(endBlock);
1151
1152 r.loopDepth--;
1153 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1154 }
1155
John Bauman19bac1e2014-05-06 15:23:49 -04001156 void VertexProgram::ENDREP(Registers &r)
1157 {
1158 loopRepDepth--;
1159
1160 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1161 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1162
1163 Nucleus::createBr(testBlock);
1164 Nucleus::setInsertBlock(endBlock);
1165
1166 r.loopDepth--;
1167 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1168 }
1169
1170 void VertexProgram::ENDWHILE(Registers &r)
1171 {
1172 loopRepDepth--;
1173
1174 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1175 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1176
1177 Nucleus::createBr(testBlock);
1178 Nucleus::setInsertBlock(endBlock);
1179
1180 r.enableIndex--;
1181 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1182 whileTest = false;
1183 }
1184
John Bauman89401822014-05-06 15:04:28 -04001185 void VertexProgram::IF(Registers &r, const Src &src)
1186 {
John Bauman19bac1e2014-05-06 15:23:49 -04001187 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001188 {
1189 IFb(r, src);
1190 }
John Bauman19bac1e2014-05-06 15:23:49 -04001191 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001192 {
1193 IFp(r, src);
1194 }
John Bauman19bac1e2014-05-06 15:23:49 -04001195 else
1196 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001197 Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
John Bauman19bac1e2014-05-06 15:23:49 -04001198 IF(r, condition);
1199 }
John Bauman89401822014-05-06 15:04:28 -04001200 }
1201
1202 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1203 {
1204 ASSERT(ifDepth < 24 + 4);
1205
1206 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1207
John Bauman19bac1e2014-05-06 15:23:49 -04001208 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001209 {
John Bauman19bac1e2014-05-06 15:23:49 -04001210 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001211 }
1212
1213 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1214 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1215
1216 branch(condition, trueBlock, falseBlock);
1217
1218 isConditionalIf[ifDepth] = false;
1219 ifFalseBlock[ifDepth] = falseBlock;
1220
1221 ifDepth++;
1222 }
1223
John Bauman19bac1e2014-05-06 15:23:49 -04001224 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001225 {
1226 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1227
John Bauman19bac1e2014-05-06 15:23:49 -04001228 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001229 {
1230 condition = ~condition;
1231 }
1232
John Bauman19bac1e2014-05-06 15:23:49 -04001233 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001234 }
1235
John Bauman19bac1e2014-05-06 15:23:49 -04001236 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001237 {
1238 Int4 condition;
1239
1240 switch(control)
1241 {
John Bauman19bac1e2014-05-06 15:23:49 -04001242 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1243 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1244 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1245 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1246 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1247 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001248 default:
1249 ASSERT(false);
1250 }
1251
John Bauman19bac1e2014-05-06 15:23:49 -04001252 IF(r, condition);
1253 }
1254
1255 void VertexProgram::IF(Registers &r, Int4 &condition)
1256 {
John Bauman89401822014-05-06 15:04:28 -04001257 condition &= r.enableStack[r.enableIndex];
1258
1259 r.enableIndex++;
1260 r.enableStack[r.enableIndex] = condition;
1261
1262 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1263 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1264
John Bauman19bac1e2014-05-06 15:23:49 -04001265 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001266
1267 branch(notAllFalse, trueBlock, falseBlock);
1268
1269 isConditionalIf[ifDepth] = true;
1270 ifFalseBlock[ifDepth] = falseBlock;
1271
1272 ifDepth++;
1273 breakDepth++;
1274 }
1275
1276 void VertexProgram::LABEL(int labelIndex)
1277 {
John Bauman19bac1e2014-05-06 15:23:49 -04001278 if(!labelBlock[labelIndex])
1279 {
1280 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1281 }
1282
John Bauman89401822014-05-06 15:04:28 -04001283 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001284 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001285 }
1286
1287 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1288 {
1289 r.loopDepth++;
1290
1291 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1292 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1293 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1294
1295 // FIXME: Compiles to two instructions?
1296 If(r.increment[r.loopDepth] == 0)
1297 {
1298 r.increment[r.loopDepth] = 1;
1299 }
1300
1301 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1302 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1303 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1304
1305 loopRepTestBlock[loopRepDepth] = testBlock;
1306 loopRepEndBlock[loopRepDepth] = endBlock;
1307
1308 // FIXME: jump(testBlock)
1309 Nucleus::createBr(testBlock);
1310 Nucleus::setInsertBlock(testBlock);
1311
1312 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1313 Nucleus::setInsertBlock(loopBlock);
1314
1315 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1316
1317 loopRepDepth++;
1318 breakDepth = 0;
1319 }
1320
1321 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1322 {
1323 r.loopDepth++;
1324
1325 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1326 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1327
1328 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1329 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1330 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1331
1332 loopRepTestBlock[loopRepDepth] = testBlock;
1333 loopRepEndBlock[loopRepDepth] = endBlock;
1334
1335 // FIXME: jump(testBlock)
1336 Nucleus::createBr(testBlock);
1337 Nucleus::setInsertBlock(testBlock);
1338
1339 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1340 Nucleus::setInsertBlock(loopBlock);
1341
1342 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1343
1344 loopRepDepth++;
1345 breakDepth = 0;
1346 }
1347
John Bauman19bac1e2014-05-06 15:23:49 -04001348 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1349 {
1350 r.enableIndex++;
1351
1352 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1353 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1354 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1355
1356 loopRepTestBlock[loopRepDepth] = testBlock;
1357 loopRepEndBlock[loopRepDepth] = endBlock;
1358
1359 Int4 restoreBreak = r.enableBreak;
1360 Int4 restoreContinue = r.enableContinue;
1361
1362 // FIXME: jump(testBlock)
1363 Nucleus::createBr(testBlock);
1364 Nucleus::setInsertBlock(testBlock);
1365 r.enableContinue = restoreContinue;
1366
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001367 const Vector4f &src = fetchRegisterF(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001368 Int4 condition = As<Int4>(src.x);
1369 condition &= r.enableStack[r.enableIndex - 1];
1370 r.enableStack[r.enableIndex] = condition;
1371
1372 Bool notAllFalse = SignMask(condition) != 0;
1373 branch(notAllFalse, loopBlock, endBlock);
1374
1375 Nucleus::setInsertBlock(endBlock);
1376 r.enableBreak = restoreBreak;
1377
1378 Nucleus::setInsertBlock(loopBlock);
1379
1380 loopRepDepth++;
1381 breakDepth = 0;
1382 }
1383
John Bauman89401822014-05-06 15:04:28 -04001384 void VertexProgram::RET(Registers &r)
1385 {
John Bauman19bac1e2014-05-06 15:23:49 -04001386 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001387 {
1388 returnBlock = Nucleus::createBasicBlock();
1389 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001390 }
1391 else
1392 {
John Bauman89401822014-05-06 15:04:28 -04001393 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001394
John Bauman19bac1e2014-05-06 15:23:49 -04001395 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001396 {
John Bauman19bac1e2014-05-06 15:23:49 -04001397 // FIXME: Encapsulate
1398 UInt index = r.callStack[--r.stackIndex];
1399
John Bauman66b8ab22014-05-06 15:57:45 -04001400 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001401 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1402
1403 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1404 {
1405 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1406 }
1407 }
1408 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1409 {
1410 Nucleus::createBr(callRetBlock[currentLabel][0]);
1411 }
1412 else // Function isn't called
1413 {
1414 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001415 }
1416
1417 Nucleus::setInsertBlock(unreachableBlock);
1418 Nucleus::createUnreachable();
1419 }
1420 }
1421
John Bauman19bac1e2014-05-06 15:23:49 -04001422 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001423 {
John Bauman19bac1e2014-05-06 15:23:49 -04001424 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001425
John Bauman19bac1e2014-05-06 15:23:49 -04001426 // FIXME: Return from function if all instances left
1427 // FIXME: Use enableLeave in other control-flow constructs
1428 }
John Bauman89401822014-05-06 15:04:28 -04001429
John Bauman19bac1e2014-05-06 15:23:49 -04001430 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1431 {
1432 Vector4f tmp;
1433 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001434
1435 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1436 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1437 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1438 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1439 }
John Bauman19bac1e2014-05-06 15:23:49 -04001440
1441 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1442 {
1443 Float4 lod = Float4(0.0f);
1444 Vector4f tmp;
1445 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1446
1447 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1448 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1449 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1450 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1451 }
1452
1453 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1454 {
1455 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1456 {
1457 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1458 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1459 }
1460 else
1461 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001462 Int index = As<Int>(Float(fetchRegisterF(r, s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001463
1464 for(int i = 0; i < 16; i++)
1465 {
1466 if(shader->usesSampler(i))
1467 {
1468 If(index == i)
1469 {
1470 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1471 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1472 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1473 }
1474 }
1475 }
1476 }
1477 }
John Bauman89401822014-05-06 15:04:28 -04001478}