blob: 7cd1daa819ead01e0fb16c4413f95fa9293ab2f8 [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
21extern bool localShaderConstants;
22
23namespace sw
24{
John Bauman19bac1e2014-05-06 15:23:49 -040025 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader) : VertexRoutine(state, shader)
John Bauman89401822014-05-06 15:04:28 -040026 {
John Bauman89401822014-05-06 15:04:28 -040027 ifDepth = 0;
28 loopRepDepth = 0;
29 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040030 currentLabel = -1;
31 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040032
33 for(int i = 0; i < 2048; i++)
34 {
35 labelBlock[i] = 0;
36 }
37 }
38
39 VertexProgram::~VertexProgram()
40 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040041 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040042 {
43 delete sampler[i];
44 }
45 }
46
47 void VertexProgram::pipeline(Registers &r)
48 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040049 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040050 {
51 sampler[i] = new SamplerCore(r.constants, state.samplerState[i]);
52 }
53
54 if(!state.preTransformed)
55 {
John Bauman19bac1e2014-05-06 15:23:49 -040056 program(r);
John Bauman89401822014-05-06 15:04:28 -040057 }
58 else
59 {
60 passThrough(r);
61 }
62 }
63
John Bauman19bac1e2014-05-06 15:23:49 -040064 void VertexProgram::program(Registers &r)
John Bauman89401822014-05-06 15:04:28 -040065 {
John Bauman19bac1e2014-05-06 15:23:49 -040066 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040067
John Bauman19bac1e2014-05-06 15:23:49 -040068 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040069
70 r.enableIndex = 0;
71 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040072
Nicolas Capens4677a5f2014-05-06 16:42:26 -040073 if(shader->containsLeaveInstruction())
74 {
75 r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
76 }
77
John Bauman19bac1e2014-05-06 15:23:49 -040078 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050079 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -040080 {
John Bauman19bac1e2014-05-06 15:23:49 -040081 const Shader::Instruction *instruction = shader->getInstruction(i);
82 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -040083
John Bauman19bac1e2014-05-06 15:23:49 -040084 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
85 {
86 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -040087
John Bauman19bac1e2014-05-06 15:23:49 -040088 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
89 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
90 }
91 }
92
Alexis Hetu903e0252014-11-25 14:25:32 -050093 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -040094 {
95 const Shader::Instruction *instruction = shader->getInstruction(i);
96 Shader::Opcode opcode = instruction->opcode;
97
98 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -040099 {
100 continue;
101 }
102
John Bauman19bac1e2014-05-06 15:23:49 -0400103 Dst dst = instruction->dst;
104 Src src0 = instruction->src[0];
105 Src src1 = instruction->src[1];
106 Src src2 = instruction->src[2];
John Bauman89401822014-05-06 15:04:28 -0400107
John Bauman19bac1e2014-05-06 15:23:49 -0400108 bool predicate = instruction->predicate;
109 int size = shader->size(opcode);
110 Usage usage = instruction->usage;
111 unsigned char usageIndex = instruction->usageIndex;
112 Control control = instruction->control;
113 bool integer = dst.type == Shader::PARAMETER_ADDR;
114 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400115
John Bauman19bac1e2014-05-06 15:23:49 -0400116 Vector4f d;
117 Vector4f s0;
118 Vector4f s1;
119 Vector4f s2;
John Bauman89401822014-05-06 15:04:28 -0400120
John Bauman19bac1e2014-05-06 15:23:49 -0400121 if(src0.type != Shader::PARAMETER_VOID) s0 = reg(r, src0);
122 if(src1.type != Shader::PARAMETER_VOID) s1 = reg(r, src1);
123 if(src2.type != Shader::PARAMETER_VOID) s2 = reg(r, src2);
John Bauman89401822014-05-06 15:04:28 -0400124
125 switch(opcode)
126 {
John Bauman19bac1e2014-05-06 15:23:49 -0400127 case Shader::OPCODE_VS_1_0: break;
128 case Shader::OPCODE_VS_1_1: break;
129 case Shader::OPCODE_VS_2_0: break;
130 case Shader::OPCODE_VS_2_x: break;
131 case Shader::OPCODE_VS_2_sw: break;
132 case Shader::OPCODE_VS_3_0: break;
133 case Shader::OPCODE_VS_3_sw: break;
134 case Shader::OPCODE_DCL: break;
135 case Shader::OPCODE_DEF: break;
136 case Shader::OPCODE_DEFI: break;
137 case Shader::OPCODE_DEFB: break;
138 case Shader::OPCODE_NOP: break;
139 case Shader::OPCODE_ABS: abs(d, s0); break;
140 case Shader::OPCODE_ADD: add(d, s0, s1); break;
141 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
142 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
143 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
144 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
145 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
146 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
147 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
148 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
149 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
150 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
151 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
152 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
153 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
154 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
155 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
156 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
157 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
158 case Shader::OPCODE_ATT: att(d, s0, s1); break;
159 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
160 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
161 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
162 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
163 case Shader::OPCODE_FRC: frc(d, s0); break;
164 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
165 case Shader::OPCODE_FLOOR: floor(d, s0); break;
166 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;
216 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
217 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
218 case Shader::OPCODE_BREAK: BREAK(r); break;
219 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
220 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
221 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
222 case Shader::OPCODE_TEST: TEST(); break;
223 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
224 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
225 case Shader::OPCODE_ELSE: ELSE(r); break;
226 case Shader::OPCODE_ENDIF: ENDIF(r); break;
227 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
228 case Shader::OPCODE_ENDREP: ENDREP(r); break;
229 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
230 case Shader::OPCODE_IF: IF(r, src0); break;
231 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
232 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
233 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
234 case Shader::OPCODE_REP: REP(r, src0); break;
235 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
236 case Shader::OPCODE_RET: RET(r); break;
237 case Shader::OPCODE_LEAVE: LEAVE(r); break;
238 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
239 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
240 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
241 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
242 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
243 case Shader::OPCODE_ALL: all(d.x, s0); break;
244 case Shader::OPCODE_ANY: any(d.x, s0); break;
245 case Shader::OPCODE_NOT: not(d, s0); break;
246 case Shader::OPCODE_OR: or(d.x, s0.x, s1.x); break;
247 case Shader::OPCODE_XOR: xor(d.x, s0.x, s1.x); break;
248 case Shader::OPCODE_AND: and(d.x, s0.x, s1.x); break;
249 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1); break;
250 case Shader::OPCODE_TEX: TEX(r, d, s0, src1); break;
251 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400252 default:
253 ASSERT(false);
254 }
255
John Bauman19bac1e2014-05-06 15:23:49 -0400256 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400257 {
John Bauman19bac1e2014-05-06 15:23:49 -0400258 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400259 {
John Bauman19bac1e2014-05-06 15:23:49 -0400260 switch(opcode)
261 {
262 case Shader::OPCODE_DIV:
263 if(dst.x) d.x = Trunc(d.x);
264 if(dst.y) d.y = Trunc(d.y);
265 if(dst.z) d.z = Trunc(d.z);
266 if(dst.w) d.w = Trunc(d.w);
267 break;
268 default:
269 break; // No truncation to integer required when arguments are integer
270 }
John Bauman89401822014-05-06 15:04:28 -0400271 }
272
John Bauman19bac1e2014-05-06 15:23:49 -0400273 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400274 {
John Bauman19bac1e2014-05-06 15:23:49 -0400275 if(dst.x) d.x = Max(d.x, Float4(0.0f));
276 if(dst.y) d.y = Max(d.y, Float4(0.0f));
277 if(dst.z) d.z = Max(d.z, Float4(0.0f));
278 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400279
John Bauman19bac1e2014-05-06 15:23:49 -0400280 if(dst.x) d.x = Min(d.x, Float4(1.0f));
281 if(dst.y) d.y = Min(d.y, Float4(1.0f));
282 if(dst.z) d.z = Min(d.z, Float4(1.0f));
283 if(dst.w) d.w = Min(d.w, Float4(1.0f));
284 }
285
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400286 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400287 {
288 Vector4f pDst; // FIXME: Rename
289
290 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400291 {
John Bauman19bac1e2014-05-06 15:23:49 -0400292 case Shader::PARAMETER_VOID: break;
293 case Shader::PARAMETER_TEMP:
294 if(dst.rel.type == Shader::PARAMETER_VOID)
295 {
296 if(dst.x) pDst.x = r.r[dst.index].x;
297 if(dst.y) pDst.y = r.r[dst.index].y;
298 if(dst.z) pDst.z = r.r[dst.index].z;
299 if(dst.w) pDst.w = r.r[dst.index].w;
300 }
301 else
302 {
303 Int a = relativeAddress(r, dst);
304
305 if(dst.x) pDst.x = r.r[dst.index + a].x;
306 if(dst.y) pDst.y = r.r[dst.index + a].y;
307 if(dst.z) pDst.z = r.r[dst.index + a].z;
308 if(dst.w) pDst.w = r.r[dst.index + a].w;
309 }
310 break;
311 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
312 case Shader::PARAMETER_RASTOUT:
313 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400314 {
315 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400316 if(dst.x) pDst.x = r.o[Pos].x;
317 if(dst.y) pDst.y = r.o[Pos].y;
318 if(dst.z) pDst.z = r.o[Pos].z;
319 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400320 break;
321 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400322 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400323 break;
324 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400325 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400326 break;
327 default:
328 ASSERT(false);
329 }
330 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400331 case Shader::PARAMETER_ATTROUT:
332 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
333 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
334 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
335 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400336 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400337 case Shader::PARAMETER_TEXCRDOUT:
338 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400339 if(version < 0x0300)
340 {
John Bauman19bac1e2014-05-06 15:23:49 -0400341 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
342 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
343 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
344 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400345 }
346 else
347 {
John Bauman19bac1e2014-05-06 15:23:49 -0400348 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400349 {
John Bauman19bac1e2014-05-06 15:23:49 -0400350 if(dst.x) pDst.x = r.o[dst.index].x;
351 if(dst.y) pDst.y = r.o[dst.index].y;
352 if(dst.z) pDst.z = r.o[dst.index].z;
353 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400354 }
John Bauman19bac1e2014-05-06 15:23:49 -0400355 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400356 {
357 Int aL = r.aL[r.loopDepth];
358
John Bauman19bac1e2014-05-06 15:23:49 -0400359 if(dst.x) pDst.x = r.o[dst.index + aL].x;
360 if(dst.y) pDst.y = r.o[dst.index + aL].y;
361 if(dst.z) pDst.z = r.o[dst.index + aL].z;
362 if(dst.w) pDst.w = r.o[dst.index + aL].w;
363 }
364 else
365 {
366 Int a = relativeAddress(r, dst);
367
368 if(dst.x) pDst.x = r.o[dst.index + a].x;
369 if(dst.y) pDst.y = r.o[dst.index + a].y;
370 if(dst.z) pDst.z = r.o[dst.index + a].z;
371 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400372 }
373 }
374 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400375 case Shader::PARAMETER_LABEL: break;
376 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
377 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400378 default:
379 ASSERT(false);
380 }
381
John Bauman19bac1e2014-05-06 15:23:49 -0400382 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -0400383
384 Int4 xEnable = enable;
385 Int4 yEnable = enable;
386 Int4 zEnable = enable;
387 Int4 wEnable = enable;
388
389 if(predicate)
390 {
John Bauman19bac1e2014-05-06 15:23:49 -0400391 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400392
393 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
394 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
395 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
396 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
397
John Bauman19bac1e2014-05-06 15:23:49 -0400398 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400399 {
John Bauman19bac1e2014-05-06 15:23:49 -0400400 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
401 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
402 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
403 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400404 }
405 else
406 {
John Bauman19bac1e2014-05-06 15:23:49 -0400407 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
408 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
409 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
410 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400411 }
412 }
413
John Bauman19bac1e2014-05-06 15:23:49 -0400414 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
415 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
416 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
417 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400418
John Bauman19bac1e2014-05-06 15:23:49 -0400419 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
420 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
421 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
422 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400423 }
424
John Bauman19bac1e2014-05-06 15:23:49 -0400425 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400426 {
John Bauman19bac1e2014-05-06 15:23:49 -0400427 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400428 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400429 case Shader::PARAMETER_TEMP:
430 if(dst.rel.type == Shader::PARAMETER_VOID)
431 {
432 if(dst.x) r.r[dst.index].x = d.x;
433 if(dst.y) r.r[dst.index].y = d.y;
434 if(dst.z) r.r[dst.index].z = d.z;
435 if(dst.w) r.r[dst.index].w = d.w;
436 }
437 else
438 {
439 Int a = relativeAddress(r, dst);
440
441 if(dst.x) r.r[dst.index + a].x = d.x;
442 if(dst.y) r.r[dst.index + a].y = d.y;
443 if(dst.z) r.r[dst.index + a].z = d.z;
444 if(dst.w) r.r[dst.index + a].w = d.w;
445 }
John Bauman89401822014-05-06 15:04:28 -0400446 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400447 case Shader::PARAMETER_ADDR:
448 if(dst.x) r.a0.x = d.x;
449 if(dst.y) r.a0.y = d.y;
450 if(dst.z) r.a0.z = d.z;
451 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400452 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400453 case Shader::PARAMETER_RASTOUT:
454 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400455 {
456 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400457 if(dst.x) r.o[Pos].x = d.x;
458 if(dst.y) r.o[Pos].y = d.y;
459 if(dst.z) r.o[Pos].z = d.z;
460 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400461 break;
462 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400463 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400464 break;
465 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400466 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400467 break;
468 default: ASSERT(false);
469 }
470 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400471 case Shader::PARAMETER_ATTROUT:
472 if(dst.x) r.o[D0 + dst.index].x = d.x;
473 if(dst.y) r.o[D0 + dst.index].y = d.y;
474 if(dst.z) r.o[D0 + dst.index].z = d.z;
475 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400476 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400477 case Shader::PARAMETER_TEXCRDOUT:
478 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400479 if(version < 0x0300)
480 {
John Bauman19bac1e2014-05-06 15:23:49 -0400481 if(dst.x) r.o[T0 + dst.index].x = d.x;
482 if(dst.y) r.o[T0 + dst.index].y = d.y;
483 if(dst.z) r.o[T0 + dst.index].z = d.z;
484 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400485 }
486 else
487 {
John Bauman19bac1e2014-05-06 15:23:49 -0400488 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400489 {
John Bauman19bac1e2014-05-06 15:23:49 -0400490 if(dst.x) r.o[dst.index].x = d.x;
491 if(dst.y) r.o[dst.index].y = d.y;
492 if(dst.z) r.o[dst.index].z = d.z;
493 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400494 }
John Bauman19bac1e2014-05-06 15:23:49 -0400495 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400496 {
497 Int aL = r.aL[r.loopDepth];
498
John Bauman19bac1e2014-05-06 15:23:49 -0400499 if(dst.x) r.o[dst.index + aL].x = d.x;
500 if(dst.y) r.o[dst.index + aL].y = d.y;
501 if(dst.z) r.o[dst.index + aL].z = d.z;
502 if(dst.w) r.o[dst.index + aL].w = d.w;
503 }
504 else
505 {
506 Int a = relativeAddress(r, dst);
507
508 if(dst.x) r.o[dst.index + a].x = d.x;
509 if(dst.y) r.o[dst.index + a].y = d.y;
510 if(dst.z) r.o[dst.index + a].z = d.z;
511 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400512 }
513 }
514 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400515 case Shader::PARAMETER_LABEL: break;
516 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
517 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400518 default:
519 ASSERT(false);
520 }
521 }
522 }
523
John Bauman19bac1e2014-05-06 15:23:49 -0400524 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400525 {
526 Nucleus::setInsertBlock(returnBlock);
527 }
528 }
529
530 void VertexProgram::passThrough(Registers &r)
531 {
John Bauman19bac1e2014-05-06 15:23:49 -0400532 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400533 {
534 for(int i = 0; i < 12; i++)
535 {
John Bauman19bac1e2014-05-06 15:23:49 -0400536 unsigned char usage = shader->output[i][0].usage;
537 unsigned char index = shader->output[i][0].index;
John Bauman89401822014-05-06 15:04:28 -0400538
539 switch(usage)
540 {
541 case 0xFF:
542 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400543 case Shader::USAGE_PSIZE:
544 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400545 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400546 case Shader::USAGE_TEXCOORD:
547 r.o[i].x = r.v[i].x;
548 r.o[i].y = r.v[i].y;
549 r.o[i].z = r.v[i].z;
550 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400551 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400552 case Shader::USAGE_POSITION:
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_COLOR:
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_FOG:
565 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400566 break;
567 default:
568 ASSERT(false);
569 }
570 }
571 }
572 else
573 {
John Bauman19bac1e2014-05-06 15:23:49 -0400574 r.o[Pos].x = r.v[PositionT].x;
575 r.o[Pos].y = r.v[PositionT].y;
576 r.o[Pos].z = r.v[PositionT].z;
577 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400578
579 for(int i = 0; i < 2; i++)
580 {
John Bauman19bac1e2014-05-06 15:23:49 -0400581 r.o[D0 + i].x = r.v[Color0 + i].x;
582 r.o[D0 + i].y = r.v[Color0 + i].y;
583 r.o[D0 + i].z = r.v[Color0 + i].z;
584 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400585 }
586
587 for(int i = 0; i < 8; i++)
588 {
John Bauman19bac1e2014-05-06 15:23:49 -0400589 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
590 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
591 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
592 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400593 }
594
John Bauman66b8ab22014-05-06 15:57:45 -0400595 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400596 }
597 }
598
John Bauman19bac1e2014-05-06 15:23:49 -0400599 Vector4f VertexProgram::reg(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400600 {
601 int i = src.index + offset;
602
John Bauman19bac1e2014-05-06 15:23:49 -0400603 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -0400604
John Bauman89401822014-05-06 15:04:28 -0400605 switch(src.type)
606 {
John Bauman19bac1e2014-05-06 15:23:49 -0400607 case Shader::PARAMETER_TEMP:
608 if(src.rel.type == Shader::PARAMETER_VOID)
609 {
610 reg = r.r[i];
611 }
612 else
613 {
614 reg = r.r[i + relativeAddress(r, src)];
615 }
616 break;
617 case Shader::PARAMETER_CONST:
618 reg = readConstant(r, src, offset);
619 break;
620 case Shader::PARAMETER_INPUT:
621 if(src.rel.type == Shader::PARAMETER_VOID)
622 {
623 reg = r.v[i];
624 }
625 else
626 {
627 reg = r.v[i + relativeAddress(r, src)];
628 }
629 break;
630 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
631 case Shader::PARAMETER_FLOAT4LITERAL:
632 reg.x = Float4(src.value[0]);
633 reg.y = Float4(src.value[1]);
634 reg.z = Float4(src.value[2]);
635 reg.w = Float4(src.value[3]);
636 break;
637 case Shader::PARAMETER_ADDR: reg = r.a0; break;
638 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
639 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
640 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
641 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
642 case Shader::PARAMETER_SAMPLER:
643 if(src.rel.type == Shader::PARAMETER_VOID)
644 {
645 reg.x = As<Float4>(Int4(i));
646 }
647 else if(src.rel.type == Shader::PARAMETER_TEMP)
648 {
649 reg.x = As<Float4>(Int4(i) + RoundInt(r.r[src.rel.index].x));
650 }
651 return reg;
652 case Shader::PARAMETER_OUTPUT:
653 if(src.rel.type == Shader::PARAMETER_VOID)
654 {
655 reg = r.o[i];
656 }
657 else
658 {
659 reg = r.o[i + relativeAddress(r, src)];
660 }
661 break;
John Bauman89401822014-05-06 15:04:28 -0400662 default:
663 ASSERT(false);
664 }
665
John Bauman66b8ab22014-05-06 15:57:45 -0400666 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
667 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
668 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
669 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400670
John Bauman66b8ab22014-05-06 15:57:45 -0400671 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400672
673 switch(src.modifier)
674 {
John Bauman19bac1e2014-05-06 15:23:49 -0400675 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400676 mod.x = x;
677 mod.y = y;
678 mod.z = z;
679 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400680 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400681 case Shader::MODIFIER_NEGATE:
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_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400688 mod.x = Abs(x);
689 mod.y = Abs(y);
690 mod.z = Abs(z);
691 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400692 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400693 case Shader::MODIFIER_ABS_NEGATE:
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_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400700 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
701 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
702 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
703 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400704 break;
705 default:
706 ASSERT(false);
707 }
708
709 return mod;
710 }
711
John Bauman19bac1e2014-05-06 15:23:49 -0400712 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400713 {
John Bauman19bac1e2014-05-06 15:23:49 -0400714 Vector4f c;
715
716 int i = src.index + offset;
717
718 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
719 {
720 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
721
722 c.x = c.x.xxxx;
723 c.y = c.y.yyyy;
724 c.z = c.z.zzzz;
725 c.w = c.w.wwww;
726
727 if(localShaderConstants) // Constant may be known at compile time
728 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500729 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400730 {
731 const Shader::Instruction &instruction = *shader->getInstruction(j);
732
733 if(instruction.opcode == Shader::OPCODE_DEF)
734 {
735 if(instruction.dst.index == i)
736 {
737 c.x = Float4(instruction.src[0].value[0]);
738 c.y = Float4(instruction.src[0].value[1]);
739 c.z = Float4(instruction.src[0].value[2]);
740 c.w = Float4(instruction.src[0].value[3]);
741
742 break;
743 }
744 }
745 }
746 }
747 }
748 else if(src.rel.type == Shader::PARAMETER_LOOP)
749 {
750 Int loopCounter = r.aL[r.loopDepth];
751
752 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
753
754 c.x = c.x.xxxx;
755 c.y = c.y.yyyy;
756 c.z = c.z.zzzz;
757 c.w = c.w.wwww;
758 }
759 else
760 {
761 if(src.rel.deterministic)
762 {
763 Int a = relativeAddress(r, src);
764
765 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
766
767 c.x = c.x.xxxx;
768 c.y = c.y.yyyy;
769 c.z = c.z.zzzz;
770 c.w = c.w.wwww;
771 }
772 else
773 {
774 int component = src.rel.swizzle & 0x03;
775 Float4 a;
776
777 switch(src.rel.type)
778 {
779 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
780 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
781 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
782 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400783 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 -0400784 default: ASSERT(false);
785 }
786
787 Int4 index = Int4(i) + RoundInt(a) * Int4(src.rel.scale);
788
789 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
790
791 Int index0 = Extract(index, 0);
792 Int index1 = Extract(index, 1);
793 Int index2 = Extract(index, 2);
794 Int index3 = Extract(index, 3);
795
796 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
797 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
798 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
799 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
800
801 transpose4x4(c.x, c.y, c.z, c.w);
802 }
803 }
804
805 return c;
806 }
807
808 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
809 {
810 ASSERT(var.rel.deterministic);
811
812 if(var.rel.type == Shader::PARAMETER_TEMP)
813 {
814 return RoundInt(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
815 }
816 else if(var.rel.type == Shader::PARAMETER_INPUT)
817 {
818 return RoundInt(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
819 }
820 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
821 {
822 return RoundInt(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
823 }
824 else if(var.rel.type == Shader::PARAMETER_CONST)
825 {
826 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[var.rel.index]));
827
828 return RoundInt(Extract(c, 0)) * var.rel.scale;
829 }
830 else ASSERT(false);
831
832 return 0;
833 }
834
835 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
836 {
837 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -0400838
839 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400840 {
John Baumand4ae8632014-05-06 16:18:33 -0400841 if(shader->containsBreakInstruction() && instruction->analysisBreak)
842 {
843 enable &= r.enableBreak;
844 }
John Bauman19bac1e2014-05-06 15:23:49 -0400845
John Baumand4ae8632014-05-06 16:18:33 -0400846 if(shader->containsContinueInstruction() && instruction->analysisContinue)
847 {
848 enable &= r.enableContinue;
849 }
John Bauman19bac1e2014-05-06 15:23:49 -0400850
John Baumand4ae8632014-05-06 16:18:33 -0400851 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
852 {
853 enable &= r.enableLeave;
854 }
John Bauman19bac1e2014-05-06 15:23:49 -0400855 }
856
857 return enable;
858 }
859
860 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
861 {
862 Vector4f row0 = reg(r, src1, 0);
863 Vector4f row1 = reg(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400864
865 dst.x = dot3(src0, row0);
866 dst.y = dot3(src0, row1);
867 }
868
John Bauman19bac1e2014-05-06 15:23:49 -0400869 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400870 {
John Bauman19bac1e2014-05-06 15:23:49 -0400871 Vector4f row0 = reg(r, src1, 0);
872 Vector4f row1 = reg(r, src1, 1);
873 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400874
875 dst.x = dot3(src0, row0);
876 dst.y = dot3(src0, row1);
877 dst.z = dot3(src0, row2);
878 }
879
John Bauman19bac1e2014-05-06 15:23:49 -0400880 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400881 {
John Bauman19bac1e2014-05-06 15:23:49 -0400882 Vector4f row0 = reg(r, src1, 0);
883 Vector4f row1 = reg(r, src1, 1);
884 Vector4f row2 = reg(r, src1, 2);
885 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400886
887 dst.x = dot3(src0, row0);
888 dst.y = dot3(src0, row1);
889 dst.z = dot3(src0, row2);
890 dst.w = dot3(src0, row3);
891 }
892
John Bauman19bac1e2014-05-06 15:23:49 -0400893 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400894 {
John Bauman19bac1e2014-05-06 15:23:49 -0400895 Vector4f row0 = reg(r, src1, 0);
896 Vector4f row1 = reg(r, src1, 1);
897 Vector4f row2 = reg(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400898
899 dst.x = dot4(src0, row0);
900 dst.y = dot4(src0, row1);
901 dst.z = dot4(src0, row2);
902 }
903
John Bauman19bac1e2014-05-06 15:23:49 -0400904 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400905 {
John Bauman19bac1e2014-05-06 15:23:49 -0400906 Vector4f row0 = reg(r, src1, 0);
907 Vector4f row1 = reg(r, src1, 1);
908 Vector4f row2 = reg(r, src1, 2);
909 Vector4f row3 = reg(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400910
911 dst.x = dot4(src0, row0);
912 dst.y = dot4(src0, row1);
913 dst.z = dot4(src0, row2);
914 dst.w = dot4(src0, row3);
915 }
916
917 void VertexProgram::BREAK(Registers &r)
918 {
919 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
920 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
921
922 if(breakDepth == 0)
923 {
John Bauman19bac1e2014-05-06 15:23:49 -0400924 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400925 Nucleus::createBr(endBlock);
926 }
927 else
928 {
929 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
930 Bool allBreak = SignMask(r.enableBreak) == 0x0;
931
John Bauman19bac1e2014-05-06 15:23:49 -0400932 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400933 branch(allBreak, endBlock, deadBlock);
934 }
935
936 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400937 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400938 }
939
John Bauman19bac1e2014-05-06 15:23:49 -0400940 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400941 {
942 Int4 condition;
943
944 switch(control)
945 {
John Bauman19bac1e2014-05-06 15:23:49 -0400946 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
947 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
948 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
949 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
950 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
951 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400952 default:
953 ASSERT(false);
954 }
955
John Bauman19bac1e2014-05-06 15:23:49 -0400956 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -0400957 }
958
959 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
960 {
961 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
962
John Bauman19bac1e2014-05-06 15:23:49 -0400963 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -0400964 {
965 condition = ~condition;
966 }
967
John Bauman19bac1e2014-05-06 15:23:49 -0400968 BREAK(r, condition);
969 }
970
971 void VertexProgram::BREAK(Registers &r, Int4 &condition)
972 {
John Bauman89401822014-05-06 15:04:28 -0400973 condition &= r.enableStack[r.enableIndex];
974
975 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
976 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
977
978 r.enableBreak = r.enableBreak & ~condition;
979 Bool allBreak = SignMask(r.enableBreak) == 0x0;
980
John Bauman19bac1e2014-05-06 15:23:49 -0400981 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400982 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400983
John Bauman89401822014-05-06 15:04:28 -0400984 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400985 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400986 }
987
John Bauman19bac1e2014-05-06 15:23:49 -0400988 void VertexProgram::CONTINUE(Registers &r)
989 {
990 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
991 }
992
993 void VertexProgram::TEST()
994 {
995 whileTest = true;
996 }
997
998 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -0400999 {
1000 if(!labelBlock[labelIndex])
1001 {
1002 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1003 }
1004
John Bauman19bac1e2014-05-06 15:23:49 -04001005 if(callRetBlock[labelIndex].size() > 1)
1006 {
1007 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1008 }
John Bauman89401822014-05-06 15:04:28 -04001009
John Bauman19bac1e2014-05-06 15:23:49 -04001010 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001011
1012 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001013 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1014
1015 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001016 }
1017
John Bauman19bac1e2014-05-06 15:23:49 -04001018 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001019 {
John Bauman19bac1e2014-05-06 15:23:49 -04001020 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001021 {
John Bauman19bac1e2014-05-06 15:23:49 -04001022 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001023 }
John Bauman19bac1e2014-05-06 15:23:49 -04001024 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001025 {
John Bauman19bac1e2014-05-06 15:23:49 -04001026 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001027 }
1028 else ASSERT(false);
1029 }
1030
John Bauman19bac1e2014-05-06 15:23:49 -04001031 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001032 {
1033 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1034
John Bauman19bac1e2014-05-06 15:23:49 -04001035 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001036 {
1037 condition = !condition;
1038 }
1039
1040 if(!labelBlock[labelIndex])
1041 {
1042 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1043 }
1044
John Bauman19bac1e2014-05-06 15:23:49 -04001045 if(callRetBlock[labelIndex].size() > 1)
1046 {
1047 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1048 }
John Bauman89401822014-05-06 15:04:28 -04001049
John Bauman19bac1e2014-05-06 15:23:49 -04001050 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001051
John Bauman19bac1e2014-05-06 15:23:49 -04001052 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1053 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1054
1055 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001056 }
1057
John Bauman19bac1e2014-05-06 15:23:49 -04001058 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001059 {
1060 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1061
John Bauman19bac1e2014-05-06 15:23:49 -04001062 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001063 {
1064 condition = ~condition;
1065 }
1066
1067 condition &= r.enableStack[r.enableIndex];
1068
1069 if(!labelBlock[labelIndex])
1070 {
1071 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1072 }
1073
John Bauman19bac1e2014-05-06 15:23:49 -04001074 if(callRetBlock[labelIndex].size() > 1)
1075 {
1076 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1077 }
John Bauman89401822014-05-06 15:04:28 -04001078
1079 r.enableIndex++;
1080 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001081 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001082
John Bauman19bac1e2014-05-06 15:23:49 -04001083 Bool notAllFalse = SignMask(condition) != 0;
1084 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1085 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001086
1087 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001088 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001089 }
1090
1091 void VertexProgram::ELSE(Registers &r)
1092 {
1093 ifDepth--;
1094
1095 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1096 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1097
1098 if(isConditionalIf[ifDepth])
1099 {
1100 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001101 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001102
1103 branch(notAllFalse, falseBlock, endBlock);
1104
1105 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1106 }
1107 else
1108 {
1109 Nucleus::createBr(endBlock);
1110 Nucleus::setInsertBlock(falseBlock);
1111 }
1112
1113 ifFalseBlock[ifDepth] = endBlock;
1114
1115 ifDepth++;
1116 }
1117
1118 void VertexProgram::ENDIF(Registers &r)
1119 {
1120 ifDepth--;
1121
1122 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1123
1124 Nucleus::createBr(endBlock);
1125 Nucleus::setInsertBlock(endBlock);
1126
1127 if(isConditionalIf[ifDepth])
1128 {
1129 breakDepth--;
1130 r.enableIndex--;
1131 }
1132 }
1133
John Bauman89401822014-05-06 15:04:28 -04001134 void VertexProgram::ENDLOOP(Registers &r)
1135 {
1136 loopRepDepth--;
1137
1138 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1139
1140 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1141 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1142
1143 Nucleus::createBr(testBlock);
1144 Nucleus::setInsertBlock(endBlock);
1145
1146 r.loopDepth--;
1147 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1148 }
1149
John Bauman19bac1e2014-05-06 15:23:49 -04001150 void VertexProgram::ENDREP(Registers &r)
1151 {
1152 loopRepDepth--;
1153
1154 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1155 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1156
1157 Nucleus::createBr(testBlock);
1158 Nucleus::setInsertBlock(endBlock);
1159
1160 r.loopDepth--;
1161 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1162 }
1163
1164 void VertexProgram::ENDWHILE(Registers &r)
1165 {
1166 loopRepDepth--;
1167
1168 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1169 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1170
1171 Nucleus::createBr(testBlock);
1172 Nucleus::setInsertBlock(endBlock);
1173
1174 r.enableIndex--;
1175 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1176 whileTest = false;
1177 }
1178
John Bauman89401822014-05-06 15:04:28 -04001179 void VertexProgram::IF(Registers &r, const Src &src)
1180 {
John Bauman19bac1e2014-05-06 15:23:49 -04001181 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001182 {
1183 IFb(r, src);
1184 }
John Bauman19bac1e2014-05-06 15:23:49 -04001185 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001186 {
1187 IFp(r, src);
1188 }
John Bauman19bac1e2014-05-06 15:23:49 -04001189 else
1190 {
1191 Int4 condition = As<Int4>(reg(r, src).x);
1192 IF(r, condition);
1193 }
John Bauman89401822014-05-06 15:04:28 -04001194 }
1195
1196 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1197 {
1198 ASSERT(ifDepth < 24 + 4);
1199
1200 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1201
John Bauman19bac1e2014-05-06 15:23:49 -04001202 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001203 {
John Bauman19bac1e2014-05-06 15:23:49 -04001204 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001205 }
1206
1207 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1208 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1209
1210 branch(condition, trueBlock, falseBlock);
1211
1212 isConditionalIf[ifDepth] = false;
1213 ifFalseBlock[ifDepth] = falseBlock;
1214
1215 ifDepth++;
1216 }
1217
John Bauman19bac1e2014-05-06 15:23:49 -04001218 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001219 {
1220 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1221
John Bauman19bac1e2014-05-06 15:23:49 -04001222 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001223 {
1224 condition = ~condition;
1225 }
1226
John Bauman19bac1e2014-05-06 15:23:49 -04001227 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001228 }
1229
John Bauman19bac1e2014-05-06 15:23:49 -04001230 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001231 {
1232 Int4 condition;
1233
1234 switch(control)
1235 {
John Bauman19bac1e2014-05-06 15:23:49 -04001236 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1237 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1238 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1239 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1240 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1241 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001242 default:
1243 ASSERT(false);
1244 }
1245
John Bauman19bac1e2014-05-06 15:23:49 -04001246 IF(r, condition);
1247 }
1248
1249 void VertexProgram::IF(Registers &r, Int4 &condition)
1250 {
John Bauman89401822014-05-06 15:04:28 -04001251 condition &= r.enableStack[r.enableIndex];
1252
1253 r.enableIndex++;
1254 r.enableStack[r.enableIndex] = condition;
1255
1256 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1257 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1258
John Bauman19bac1e2014-05-06 15:23:49 -04001259 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001260
1261 branch(notAllFalse, trueBlock, falseBlock);
1262
1263 isConditionalIf[ifDepth] = true;
1264 ifFalseBlock[ifDepth] = falseBlock;
1265
1266 ifDepth++;
1267 breakDepth++;
1268 }
1269
1270 void VertexProgram::LABEL(int labelIndex)
1271 {
John Bauman19bac1e2014-05-06 15:23:49 -04001272 if(!labelBlock[labelIndex])
1273 {
1274 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1275 }
1276
John Bauman89401822014-05-06 15:04:28 -04001277 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001278 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001279 }
1280
1281 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1282 {
1283 r.loopDepth++;
1284
1285 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1286 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1287 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1288
1289 // FIXME: Compiles to two instructions?
1290 If(r.increment[r.loopDepth] == 0)
1291 {
1292 r.increment[r.loopDepth] = 1;
1293 }
1294
1295 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1296 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1297 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1298
1299 loopRepTestBlock[loopRepDepth] = testBlock;
1300 loopRepEndBlock[loopRepDepth] = endBlock;
1301
1302 // FIXME: jump(testBlock)
1303 Nucleus::createBr(testBlock);
1304 Nucleus::setInsertBlock(testBlock);
1305
1306 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1307 Nucleus::setInsertBlock(loopBlock);
1308
1309 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1310
1311 loopRepDepth++;
1312 breakDepth = 0;
1313 }
1314
1315 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1316 {
1317 r.loopDepth++;
1318
1319 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1320 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1321
1322 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1323 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1324 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1325
1326 loopRepTestBlock[loopRepDepth] = testBlock;
1327 loopRepEndBlock[loopRepDepth] = endBlock;
1328
1329 // FIXME: jump(testBlock)
1330 Nucleus::createBr(testBlock);
1331 Nucleus::setInsertBlock(testBlock);
1332
1333 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1334 Nucleus::setInsertBlock(loopBlock);
1335
1336 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1337
1338 loopRepDepth++;
1339 breakDepth = 0;
1340 }
1341
John Bauman19bac1e2014-05-06 15:23:49 -04001342 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1343 {
1344 r.enableIndex++;
1345
1346 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1347 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1348 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1349
1350 loopRepTestBlock[loopRepDepth] = testBlock;
1351 loopRepEndBlock[loopRepDepth] = endBlock;
1352
1353 Int4 restoreBreak = r.enableBreak;
1354 Int4 restoreContinue = r.enableContinue;
1355
1356 // FIXME: jump(testBlock)
1357 Nucleus::createBr(testBlock);
1358 Nucleus::setInsertBlock(testBlock);
1359 r.enableContinue = restoreContinue;
1360
John Bauman66b8ab22014-05-06 15:57:45 -04001361 const Vector4f &src = reg(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001362 Int4 condition = As<Int4>(src.x);
1363 condition &= r.enableStack[r.enableIndex - 1];
1364 r.enableStack[r.enableIndex] = condition;
1365
1366 Bool notAllFalse = SignMask(condition) != 0;
1367 branch(notAllFalse, loopBlock, endBlock);
1368
1369 Nucleus::setInsertBlock(endBlock);
1370 r.enableBreak = restoreBreak;
1371
1372 Nucleus::setInsertBlock(loopBlock);
1373
1374 loopRepDepth++;
1375 breakDepth = 0;
1376 }
1377
John Bauman89401822014-05-06 15:04:28 -04001378 void VertexProgram::RET(Registers &r)
1379 {
John Bauman19bac1e2014-05-06 15:23:49 -04001380 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001381 {
1382 returnBlock = Nucleus::createBasicBlock();
1383 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001384 }
1385 else
1386 {
John Bauman89401822014-05-06 15:04:28 -04001387 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001388
John Bauman19bac1e2014-05-06 15:23:49 -04001389 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001390 {
John Bauman19bac1e2014-05-06 15:23:49 -04001391 // FIXME: Encapsulate
1392 UInt index = r.callStack[--r.stackIndex];
1393
John Bauman66b8ab22014-05-06 15:57:45 -04001394 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001395 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1396
1397 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1398 {
1399 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1400 }
1401 }
1402 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1403 {
1404 Nucleus::createBr(callRetBlock[currentLabel][0]);
1405 }
1406 else // Function isn't called
1407 {
1408 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001409 }
1410
1411 Nucleus::setInsertBlock(unreachableBlock);
1412 Nucleus::createUnreachable();
1413 }
1414 }
1415
John Bauman19bac1e2014-05-06 15:23:49 -04001416 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001417 {
John Bauman19bac1e2014-05-06 15:23:49 -04001418 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001419
John Bauman19bac1e2014-05-06 15:23:49 -04001420 // FIXME: Return from function if all instances left
1421 // FIXME: Use enableLeave in other control-flow constructs
1422 }
John Bauman89401822014-05-06 15:04:28 -04001423
John Bauman19bac1e2014-05-06 15:23:49 -04001424 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1425 {
1426 Vector4f tmp;
1427 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001428
1429 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1430 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1431 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1432 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1433 }
John Bauman19bac1e2014-05-06 15:23:49 -04001434
1435 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1436 {
1437 Float4 lod = Float4(0.0f);
1438 Vector4f tmp;
1439 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1440
1441 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1442 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1443 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1444 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1445 }
1446
1447 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1448 {
1449 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1450 {
1451 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1452 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1453 }
1454 else
1455 {
1456 Int index = As<Int>(Float(reg(r, s).x.x));
1457
1458 for(int i = 0; i < 16; i++)
1459 {
1460 if(shader->usesSampler(i))
1461 {
1462 If(index == i)
1463 {
1464 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1465 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1466 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1467 }
1468 }
1469 }
1470 }
1471 }
John Bauman89401822014-05-06 15:04:28 -04001472}