blob: 97e335fe63f8f60b1e5b9b0fcbf2cabbf3ddc7bd [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{
Nicolas Capens7551ac62016-01-20 17:11:53 -050023 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader)
24 : VertexRoutine(state, shader), shader(shader), r(shader->dynamicallyIndexedTemporaries)
John Bauman89401822014-05-06 15:04:28 -040025 {
John Bauman89401822014-05-06 15:04:28 -040026 ifDepth = 0;
27 loopRepDepth = 0;
28 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040029 currentLabel = -1;
30 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040031
32 for(int i = 0; i < 2048; i++)
33 {
34 labelBlock[i] = 0;
35 }
Nicolas Capens7551ac62016-01-20 17:11:53 -050036
37 loopDepth = -1;
38 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
39
40 if(shader && shader->containsBreakInstruction())
41 {
42 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
43 }
44
45 if(shader && shader->containsContinueInstruction())
46 {
47 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
48 }
49
50 if(shader->instanceIdDeclared)
51 {
52 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
53 }
John Bauman89401822014-05-06 15:04:28 -040054 }
55
56 VertexProgram::~VertexProgram()
57 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040058 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040059 {
60 delete sampler[i];
61 }
62 }
63
Nicolas Capensb4fb3672016-01-15 17:02:41 -050064 void VertexProgram::pipeline()
John Bauman89401822014-05-06 15:04:28 -040065 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040066 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040067 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050068 sampler[i] = new SamplerCore(constants, state.samplerState[i]);
John Bauman89401822014-05-06 15:04:28 -040069 }
70
71 if(!state.preTransformed)
72 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050073 program();
John Bauman89401822014-05-06 15:04:28 -040074 }
75 else
76 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050077 passThrough();
John Bauman89401822014-05-06 15:04:28 -040078 }
79 }
80
Nicolas Capensb4fb3672016-01-15 17:02:41 -050081 void VertexProgram::program()
John Bauman89401822014-05-06 15:04:28 -040082 {
John Bauman19bac1e2014-05-06 15:23:49 -040083 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040084
John Bauman19bac1e2014-05-06 15:23:49 -040085 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040086
Nicolas Capens7551ac62016-01-20 17:11:53 -050087 enableIndex = 0;
88 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040089
Nicolas Capens4677a5f2014-05-06 16:42:26 -040090 if(shader->containsLeaveInstruction())
91 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050092 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040093 }
94
John Bauman19bac1e2014-05-06 15:23:49 -040095 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050096 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -040097 {
John Bauman19bac1e2014-05-06 15:23:49 -040098 const Shader::Instruction *instruction = shader->getInstruction(i);
99 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400100
John Bauman19bac1e2014-05-06 15:23:49 -0400101 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
102 {
103 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400104
John Bauman19bac1e2014-05-06 15:23:49 -0400105 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
106 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
107 }
108 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500109
Alexis Hetu903e0252014-11-25 14:25:32 -0500110 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400111 {
112 const Shader::Instruction *instruction = shader->getInstruction(i);
113 Shader::Opcode opcode = instruction->opcode;
114
115 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400116 {
117 continue;
118 }
119
John Bauman19bac1e2014-05-06 15:23:49 -0400120 Dst dst = instruction->dst;
121 Src src0 = instruction->src[0];
122 Src src1 = instruction->src[1];
123 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400124 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400125 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400126
John Bauman19bac1e2014-05-06 15:23:49 -0400127 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400128 Control control = instruction->control;
129 bool integer = dst.type == Shader::PARAMETER_ADDR;
130 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400131
John Bauman19bac1e2014-05-06 15:23:49 -0400132 Vector4f d;
133 Vector4f s0;
134 Vector4f s1;
135 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400136 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400137 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400138
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500139 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(src0);
140 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(src1);
141 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(src2);
142 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegisterF(src3);
143 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegisterF(src4);
John Bauman89401822014-05-06 15:04:28 -0400144
145 switch(opcode)
146 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500147 case Shader::OPCODE_VS_1_0: break;
148 case Shader::OPCODE_VS_1_1: break;
149 case Shader::OPCODE_VS_2_0: break;
150 case Shader::OPCODE_VS_2_x: break;
151 case Shader::OPCODE_VS_2_sw: break;
152 case Shader::OPCODE_VS_3_0: break;
153 case Shader::OPCODE_VS_3_sw: break;
154 case Shader::OPCODE_DCL: break;
155 case Shader::OPCODE_DEF: break;
156 case Shader::OPCODE_DEFI: break;
157 case Shader::OPCODE_DEFB: break;
158 case Shader::OPCODE_NOP: break;
159 case Shader::OPCODE_ABS: abs(d, s0); break;
160 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400161 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500162 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
163 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
164 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
165 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
166 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
167 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
168 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
169 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
170 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
171 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
172 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
173 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
174 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
175 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
176 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
177 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
178 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400179 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
180 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
181 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500182 case Shader::OPCODE_ATT: att(d, s0, s1); break;
183 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
184 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
185 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
186 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
187 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400188 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
189 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400190 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500191 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400192 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500193 case Shader::OPCODE_LIT: lit(d, s0); break;
194 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
195 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
196 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
197 case Shader::OPCODE_LOG: log(d, s0, pp); break;
198 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
199 case Shader::OPCODE_STEP: step(d, s0, s1); break;
200 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400201 case Shader::OPCODE_FLOATBITSTOINT:
202 case Shader::OPCODE_FLOATBITSTOUINT:
203 case Shader::OPCODE_INTBITSTOFLOAT:
204 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500205 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
206 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
207 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
208 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
209 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
210 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
211 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
212 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400213 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
214 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500215 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400216 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
217 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500218 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400219 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400220 case Shader::OPCODE_NEG: neg(d, s0); break;
221 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500222 case Shader::OPCODE_F2B: f2b(d, s0); break;
223 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400224 case Shader::OPCODE_F2I: f2i(d, s0); break;
225 case Shader::OPCODE_I2F: i2f(d, s0); break;
226 case Shader::OPCODE_F2U: f2u(d, s0); break;
227 case Shader::OPCODE_U2F: u2f(d, s0); break;
228 case Shader::OPCODE_I2B: i2b(d, s0); break;
229 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500230 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400231 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500232 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
233 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
234 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
235 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
236 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
237 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
238 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400239 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
240 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500241 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400242 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
243 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
244 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500245 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
246 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
247 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
248 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
249 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
250 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
251 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
252 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
253 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
254 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
255 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
256 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
257 case Shader::OPCODE_SGE: step(d, s1, s0); break;
258 case Shader::OPCODE_SGN: sgn(d, s0); break;
259 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
260 case Shader::OPCODE_COS: cos(d, s0, pp); break;
261 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
262 case Shader::OPCODE_TAN: tan(d, s0); break;
263 case Shader::OPCODE_ACOS: acos(d, s0); break;
264 case Shader::OPCODE_ASIN: asin(d, s0); break;
265 case Shader::OPCODE_ATAN: atan(d, s0); break;
266 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
267 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
268 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
269 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
270 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
271 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
272 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
273 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
274 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400275 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500276 case Shader::OPCODE_BREAK: BREAK(); break;
277 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
278 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
279 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
280 case Shader::OPCODE_TEST: TEST(); break;
281 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
282 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
283 case Shader::OPCODE_ELSE: ELSE(); break;
284 case Shader::OPCODE_ENDIF: ENDIF(); break;
285 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
286 case Shader::OPCODE_ENDREP: ENDREP(); break;
287 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
288 case Shader::OPCODE_IF: IF(src0); break;
289 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
290 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
291 case Shader::OPCODE_LOOP: LOOP(src1); break;
292 case Shader::OPCODE_REP: REP(src0); break;
293 case Shader::OPCODE_WHILE: WHILE(src0); break;
294 case Shader::OPCODE_RET: RET(); break;
295 case Shader::OPCODE_LEAVE: LEAVE(); break;
296 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
297 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400298 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500299 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
300 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
301 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
302 case Shader::OPCODE_ALL: all(d.x, s0); break;
303 case Shader::OPCODE_ANY: any(d.x, s0); break;
304 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400305 case Shader::OPCODE_OR: or(d, s0, s1); break;
306 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
307 case Shader::OPCODE_AND: and(d, s0, s1); break;
308 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
309 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500310 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
311 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
312 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2, s3); break;
313 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
314 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2); break;
315 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2, s3); break;
316 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
317 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
318 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
319 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400320 default:
321 ASSERT(false);
322 }
323
John Bauman19bac1e2014-05-06 15:23:49 -0400324 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400325 {
John Bauman19bac1e2014-05-06 15:23:49 -0400326 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400327 {
John Bauman19bac1e2014-05-06 15:23:49 -0400328 switch(opcode)
329 {
330 case Shader::OPCODE_DIV:
331 if(dst.x) d.x = Trunc(d.x);
332 if(dst.y) d.y = Trunc(d.y);
333 if(dst.z) d.z = Trunc(d.z);
334 if(dst.w) d.w = Trunc(d.w);
335 break;
336 default:
337 break; // No truncation to integer required when arguments are integer
338 }
John Bauman89401822014-05-06 15:04:28 -0400339 }
340
John Bauman19bac1e2014-05-06 15:23:49 -0400341 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400342 {
John Bauman19bac1e2014-05-06 15:23:49 -0400343 if(dst.x) d.x = Max(d.x, Float4(0.0f));
344 if(dst.y) d.y = Max(d.y, Float4(0.0f));
345 if(dst.z) d.z = Max(d.z, Float4(0.0f));
346 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400347
John Bauman19bac1e2014-05-06 15:23:49 -0400348 if(dst.x) d.x = Min(d.x, Float4(1.0f));
349 if(dst.y) d.y = Min(d.y, Float4(1.0f));
350 if(dst.z) d.z = Min(d.z, Float4(1.0f));
351 if(dst.w) d.w = Min(d.w, Float4(1.0f));
352 }
353
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400354 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400355 {
356 Vector4f pDst; // FIXME: Rename
357
358 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400359 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500360 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400361 case Shader::PARAMETER_TEMP:
362 if(dst.rel.type == Shader::PARAMETER_VOID)
363 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500364 if(dst.x) pDst.x = r[dst.index].x;
365 if(dst.y) pDst.y = r[dst.index].y;
366 if(dst.z) pDst.z = r[dst.index].z;
367 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400368 }
369 else
370 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500371 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400372
Nicolas Capens7551ac62016-01-20 17:11:53 -0500373 if(dst.x) pDst.x = r[dst.index + a].x;
374 if(dst.y) pDst.y = r[dst.index + a].y;
375 if(dst.z) pDst.z = r[dst.index + a].z;
376 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400377 }
378 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500379 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400380 case Shader::PARAMETER_RASTOUT:
381 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400382 {
383 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500384 if(dst.x) pDst.x = o[Pos].x;
385 if(dst.y) pDst.y = o[Pos].y;
386 if(dst.z) pDst.z = o[Pos].z;
387 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400388 break;
389 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500390 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400391 break;
392 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500393 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400394 break;
395 default:
396 ASSERT(false);
397 }
398 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400399 case Shader::PARAMETER_ATTROUT:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500400 if(dst.x) pDst.x = o[D0 + dst.index].x;
401 if(dst.y) pDst.y = o[D0 + dst.index].y;
402 if(dst.z) pDst.z = o[D0 + dst.index].z;
403 if(dst.w) pDst.w = o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400404 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400405 case Shader::PARAMETER_TEXCRDOUT:
406 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400407 if(version < 0x0300)
408 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500409 if(dst.x) pDst.x = o[T0 + dst.index].x;
410 if(dst.y) pDst.y = o[T0 + dst.index].y;
411 if(dst.z) pDst.z = o[T0 + dst.index].z;
412 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400413 }
414 else
415 {
John Bauman19bac1e2014-05-06 15:23:49 -0400416 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400417 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500418 if(dst.x) pDst.x = o[dst.index].x;
419 if(dst.y) pDst.y = o[dst.index].y;
420 if(dst.z) pDst.z = o[dst.index].z;
421 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400422 }
John Bauman19bac1e2014-05-06 15:23:49 -0400423 else
424 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500425 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400426
Nicolas Capens7551ac62016-01-20 17:11:53 -0500427 if(dst.x) pDst.x = o[dst.index + a].x;
428 if(dst.y) pDst.y = o[dst.index + a].y;
429 if(dst.z) pDst.z = o[dst.index + a].z;
430 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400431 }
432 }
433 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500434 case Shader::PARAMETER_LABEL: break;
435 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
436 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400437 default:
438 ASSERT(false);
439 }
440
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500441 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400442
443 Int4 xEnable = enable;
444 Int4 yEnable = enable;
445 Int4 zEnable = enable;
446 Int4 wEnable = enable;
447
448 if(predicate)
449 {
John Bauman19bac1e2014-05-06 15:23:49 -0400450 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400451
Nicolas Capens7551ac62016-01-20 17:11:53 -0500452 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
453 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
454 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
455 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400456
John Bauman19bac1e2014-05-06 15:23:49 -0400457 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400458 {
John Bauman19bac1e2014-05-06 15:23:49 -0400459 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
460 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
461 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
462 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400463 }
464 else
465 {
John Bauman19bac1e2014-05-06 15:23:49 -0400466 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
467 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
468 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
469 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400470 }
471 }
472
John Bauman19bac1e2014-05-06 15:23:49 -0400473 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
474 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
475 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
476 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400477
John Bauman19bac1e2014-05-06 15:23:49 -0400478 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
479 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
480 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
481 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400482 }
483
John Bauman19bac1e2014-05-06 15:23:49 -0400484 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400485 {
John Bauman19bac1e2014-05-06 15:23:49 -0400486 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400487 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400488 case Shader::PARAMETER_TEMP:
489 if(dst.rel.type == Shader::PARAMETER_VOID)
490 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500491 if(dst.x) r[dst.index].x = d.x;
492 if(dst.y) r[dst.index].y = d.y;
493 if(dst.z) r[dst.index].z = d.z;
494 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400495 }
496 else
497 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500498 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400499
Nicolas Capens7551ac62016-01-20 17:11:53 -0500500 if(dst.x) r[dst.index + a].x = d.x;
501 if(dst.y) r[dst.index + a].y = d.y;
502 if(dst.z) r[dst.index + a].z = d.z;
503 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400504 }
John Bauman89401822014-05-06 15:04:28 -0400505 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400506 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500507 if(dst.x) a0.x = d.x;
508 if(dst.y) a0.y = d.y;
509 if(dst.z) a0.z = d.z;
510 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400511 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400512 case Shader::PARAMETER_RASTOUT:
513 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400514 {
515 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500516 if(dst.x) o[Pos].x = d.x;
517 if(dst.y) o[Pos].y = d.y;
518 if(dst.z) o[Pos].z = d.z;
519 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400520 break;
521 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500522 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400523 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500524 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500525 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400526 break;
527 default: ASSERT(false);
528 }
529 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500530 case Shader::PARAMETER_ATTROUT:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500531 if(dst.x) o[D0 + dst.index].x = d.x;
532 if(dst.y) o[D0 + dst.index].y = d.y;
533 if(dst.z) o[D0 + dst.index].z = d.z;
534 if(dst.w) o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400535 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400536 case Shader::PARAMETER_TEXCRDOUT:
537 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400538 if(version < 0x0300)
539 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500540 if(dst.x) o[T0 + dst.index].x = d.x;
541 if(dst.y) o[T0 + dst.index].y = d.y;
542 if(dst.z) o[T0 + dst.index].z = d.z;
543 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400544 }
545 else
546 {
John Bauman19bac1e2014-05-06 15:23:49 -0400547 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400548 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500549 if(dst.x) o[dst.index].x = d.x;
550 if(dst.y) o[dst.index].y = d.y;
551 if(dst.z) o[dst.index].z = d.z;
552 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400553 }
John Bauman19bac1e2014-05-06 15:23:49 -0400554 else
555 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500556 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400557
Nicolas Capens7551ac62016-01-20 17:11:53 -0500558 if(dst.x) o[dst.index + a].x = d.x;
559 if(dst.y) o[dst.index + a].y = d.y;
560 if(dst.z) o[dst.index + a].z = d.z;
561 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400562 }
563 }
564 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500565 case Shader::PARAMETER_LABEL: break;
566 case Shader::PARAMETER_PREDICATE: p0 = d; break;
567 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400568 default:
569 ASSERT(false);
570 }
571 }
572 }
573
John Bauman19bac1e2014-05-06 15:23:49 -0400574 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400575 {
576 Nucleus::setInsertBlock(returnBlock);
577 }
578 }
579
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500580 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400581 {
John Bauman19bac1e2014-05-06 15:23:49 -0400582 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400583 {
584 for(int i = 0; i < 12; i++)
585 {
John Bauman19bac1e2014-05-06 15:23:49 -0400586 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400587
588 switch(usage)
589 {
590 case 0xFF:
591 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400592 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500593 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400594 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400595 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500596 o[i].x = v[i].x;
597 o[i].y = v[i].y;
598 o[i].z = v[i].z;
599 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400600 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400601 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500602 o[i].x = v[i].x;
603 o[i].y = v[i].y;
604 o[i].z = v[i].z;
605 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400606 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400607 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500608 o[i].x = v[i].x;
609 o[i].y = v[i].y;
610 o[i].z = v[i].z;
611 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400612 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400613 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500614 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400615 break;
616 default:
617 ASSERT(false);
618 }
619 }
620 }
621 else
622 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500623 o[Pos].x = v[PositionT].x;
624 o[Pos].y = v[PositionT].y;
625 o[Pos].z = v[PositionT].z;
626 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400627
628 for(int i = 0; i < 2; i++)
629 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500630 o[D0 + i].x = v[Color0 + i].x;
631 o[D0 + i].y = v[Color0 + i].y;
632 o[D0 + i].z = v[Color0 + i].z;
633 o[D0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400634 }
635
636 for(int i = 0; i < 8; i++)
637 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500638 o[T0 + i].x = v[TexCoord0 + i].x;
639 o[T0 + i].y = v[TexCoord0 + i].y;
640 o[T0 + i].z = v[TexCoord0 + i].z;
641 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400642 }
643
Nicolas Capens7551ac62016-01-20 17:11:53 -0500644 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400645 }
646 }
647
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500648 Vector4f VertexProgram::fetchRegisterF(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400649 {
John Bauman19bac1e2014-05-06 15:23:49 -0400650 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500651 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400652
John Bauman89401822014-05-06 15:04:28 -0400653 switch(src.type)
654 {
John Bauman19bac1e2014-05-06 15:23:49 -0400655 case Shader::PARAMETER_TEMP:
656 if(src.rel.type == Shader::PARAMETER_VOID)
657 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500658 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400659 }
660 else
661 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400662 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400663 }
664 break;
665 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500666 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400667 break;
668 case Shader::PARAMETER_INPUT:
669 if(src.rel.type == Shader::PARAMETER_VOID)
670 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500671 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400672 }
673 else
674 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400675 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400676 }
677 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500678 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400679 case Shader::PARAMETER_FLOAT4LITERAL:
680 reg.x = Float4(src.value[0]);
681 reg.y = Float4(src.value[1]);
682 reg.z = Float4(src.value[2]);
683 reg.w = Float4(src.value[3]);
684 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500685 case Shader::PARAMETER_ADDR: reg = a0; break;
686 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
687 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
688 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
689 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400690 case Shader::PARAMETER_SAMPLER:
691 if(src.rel.type == Shader::PARAMETER_VOID)
692 {
693 reg.x = As<Float4>(Int4(i));
694 }
695 else if(src.rel.type == Shader::PARAMETER_TEMP)
696 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500697 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400698 }
699 return reg;
700 case Shader::PARAMETER_OUTPUT:
701 if(src.rel.type == Shader::PARAMETER_VOID)
702 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500703 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400704 }
705 else
706 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400707 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400708 }
709 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400710 case Shader::PARAMETER_MISCTYPE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500711 reg.x = As<Float>(Int(instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400712 return reg;
John Bauman89401822014-05-06 15:04:28 -0400713 default:
714 ASSERT(false);
715 }
716
John Bauman66b8ab22014-05-06 15:57:45 -0400717 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
718 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
719 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
720 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400721
John Bauman66b8ab22014-05-06 15:57:45 -0400722 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400723
724 switch(src.modifier)
725 {
John Bauman19bac1e2014-05-06 15:23:49 -0400726 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400727 mod.x = x;
728 mod.y = y;
729 mod.z = z;
730 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400731 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400732 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400733 mod.x = -x;
734 mod.y = -y;
735 mod.z = -z;
736 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400737 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400738 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400739 mod.x = Abs(x);
740 mod.y = Abs(y);
741 mod.z = Abs(z);
742 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400743 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400744 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400745 mod.x = -Abs(x);
746 mod.y = -Abs(y);
747 mod.z = -Abs(z);
748 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400749 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400750 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400751 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
752 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
753 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
754 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400755 break;
756 default:
757 ASSERT(false);
758 }
759
760 return mod;
761 }
762
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400763 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
764 {
765 if(bufferIndex == -1)
766 {
767 return data + OFFSET(DrawData, vs.c[index]);
768 }
769 else
770 {
771 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
772 }
773 }
774
775 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
776 {
777 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
778 }
779
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500780 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400781 {
John Bauman19bac1e2014-05-06 15:23:49 -0400782 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500783 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400784
785 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
786 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400787 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400788
789 c.x = c.x.xxxx;
790 c.y = c.y.yyyy;
791 c.z = c.z.zzzz;
792 c.w = c.w.wwww;
793
Nicolas Capenseafdb222015-05-15 15:24:08 -0400794 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400795 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500796 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400797 {
798 const Shader::Instruction &instruction = *shader->getInstruction(j);
799
800 if(instruction.opcode == Shader::OPCODE_DEF)
801 {
802 if(instruction.dst.index == i)
803 {
804 c.x = Float4(instruction.src[0].value[0]);
805 c.y = Float4(instruction.src[0].value[1]);
806 c.z = Float4(instruction.src[0].value[2]);
807 c.w = Float4(instruction.src[0].value[3]);
808
809 break;
810 }
811 }
812 }
813 }
814 }
815 else if(src.rel.type == Shader::PARAMETER_LOOP)
816 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500817 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400818
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400819 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400820
821 c.x = c.x.xxxx;
822 c.y = c.y.yyyy;
823 c.z = c.z.zzzz;
824 c.w = c.w.wwww;
825 }
826 else
827 {
828 if(src.rel.deterministic)
829 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400830 Int a = relativeAddress(src, src.bufferIndex);
831
832 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500833
Nicolas Capens7551ac62016-01-20 17:11:53 -0500834 c.x = c.y = c.z = c.w = *Pointer<Float4>(data + OFFSET(DrawData,vs.c[i]) + a * 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400835
836 c.x = c.x.xxxx;
837 c.y = c.y.yyyy;
838 c.z = c.z.zzzz;
839 c.w = c.w.wwww;
840 }
841 else
842 {
843 int component = src.rel.swizzle & 0x03;
844 Float4 a;
845
846 switch(src.rel.type)
847 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500848 case Shader::PARAMETER_ADDR: a = a0[component]; break;
849 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
850 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
851 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400852 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400853 default: ASSERT(false);
854 }
855
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400856 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400857
Alexis Hetu028f41b2016-01-13 14:40:47 -0500858 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500859
John Bauman19bac1e2014-05-06 15:23:49 -0400860 Int index0 = Extract(index, 0);
861 Int index1 = Extract(index, 1);
862 Int index2 = Extract(index, 2);
863 Int index3 = Extract(index, 3);
864
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400865 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
866 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
867 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
868 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400869
870 transpose4x4(c.x, c.y, c.z, c.w);
871 }
872 }
873
874 return c;
875 }
876
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400877 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400878 {
879 ASSERT(var.rel.deterministic);
880
881 if(var.rel.type == Shader::PARAMETER_TEMP)
882 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500883 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400884 }
885 else if(var.rel.type == Shader::PARAMETER_INPUT)
886 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500887 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400888 }
889 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
890 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500891 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400892 }
893 else if(var.rel.type == Shader::PARAMETER_CONST)
894 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400895 RValue<Int4> c = *Pointer<Int4>(uniformAddress(bufferIndex, var.rel.index));
John Bauman19bac1e2014-05-06 15:23:49 -0400896
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400897 return Extract(c, 0) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400898 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500899 else if(var.rel.type == Shader::PARAMETER_LOOP)
900 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500901 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500902 }
John Bauman19bac1e2014-05-06 15:23:49 -0400903 else ASSERT(false);
904
905 return 0;
906 }
907
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500908 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400909 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500910 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500911
John Baumand4ae8632014-05-06 16:18:33 -0400912 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400913 {
John Baumand4ae8632014-05-06 16:18:33 -0400914 if(shader->containsBreakInstruction() && instruction->analysisBreak)
915 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500916 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400917 }
John Bauman19bac1e2014-05-06 15:23:49 -0400918
John Baumand4ae8632014-05-06 16:18:33 -0400919 if(shader->containsContinueInstruction() && instruction->analysisContinue)
920 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500921 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400922 }
John Bauman19bac1e2014-05-06 15:23:49 -0400923
John Baumand4ae8632014-05-06 16:18:33 -0400924 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
925 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500926 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400927 }
John Bauman19bac1e2014-05-06 15:23:49 -0400928 }
929
930 return enable;
931 }
932
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500933 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400934 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500935 Vector4f row0 = fetchRegisterF(src1, 0);
936 Vector4f row1 = fetchRegisterF(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400937
938 dst.x = dot3(src0, row0);
939 dst.y = dot3(src0, row1);
940 }
941
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500942 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400943 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500944 Vector4f row0 = fetchRegisterF(src1, 0);
945 Vector4f row1 = fetchRegisterF(src1, 1);
946 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400947
948 dst.x = dot3(src0, row0);
949 dst.y = dot3(src0, row1);
950 dst.z = dot3(src0, row2);
951 }
952
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500953 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400954 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500955 Vector4f row0 = fetchRegisterF(src1, 0);
956 Vector4f row1 = fetchRegisterF(src1, 1);
957 Vector4f row2 = fetchRegisterF(src1, 2);
958 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400959
960 dst.x = dot3(src0, row0);
961 dst.y = dot3(src0, row1);
962 dst.z = dot3(src0, row2);
963 dst.w = dot3(src0, row3);
964 }
965
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500966 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400967 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500968 Vector4f row0 = fetchRegisterF(src1, 0);
969 Vector4f row1 = fetchRegisterF(src1, 1);
970 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400971
972 dst.x = dot4(src0, row0);
973 dst.y = dot4(src0, row1);
974 dst.z = dot4(src0, row2);
975 }
976
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500977 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400978 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500979 Vector4f row0 = fetchRegisterF(src1, 0);
980 Vector4f row1 = fetchRegisterF(src1, 1);
981 Vector4f row2 = fetchRegisterF(src1, 2);
982 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400983
984 dst.x = dot4(src0, row0);
985 dst.y = dot4(src0, row1);
986 dst.z = dot4(src0, row2);
987 dst.w = dot4(src0, row3);
988 }
989
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500990 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -0400991 {
992 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
993 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
994
995 if(breakDepth == 0)
996 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500997 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400998 Nucleus::createBr(endBlock);
999 }
1000 else
1001 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001002 enableBreak = enableBreak & ~enableStack[enableIndex];
1003 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001004
Nicolas Capens7551ac62016-01-20 17:11:53 -05001005 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001006 branch(allBreak, endBlock, deadBlock);
1007 }
1008
1009 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001010 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001011 }
1012
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001013 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001014 {
1015 Int4 condition;
1016
1017 switch(control)
1018 {
John Bauman19bac1e2014-05-06 15:23:49 -04001019 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1020 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1021 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1022 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1023 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1024 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001025 default:
1026 ASSERT(false);
1027 }
1028
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001029 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001030 }
1031
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001032 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001033 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001034 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001035
John Bauman19bac1e2014-05-06 15:23:49 -04001036 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001037 {
1038 condition = ~condition;
1039 }
1040
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001041 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001042 }
1043
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001044 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001045 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001046 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001047
1048 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1049 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1050
Nicolas Capens7551ac62016-01-20 17:11:53 -05001051 enableBreak = enableBreak & ~condition;
1052 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001053
Nicolas Capens7551ac62016-01-20 17:11:53 -05001054 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001055 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001056
John Bauman89401822014-05-06 15:04:28 -04001057 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001058 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001059 }
1060
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001061 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001062 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001063 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001064 }
1065
1066 void VertexProgram::TEST()
1067 {
1068 whileTest = true;
1069 }
1070
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001071 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001072 {
1073 if(!labelBlock[labelIndex])
1074 {
1075 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1076 }
1077
John Bauman19bac1e2014-05-06 15:23:49 -04001078 if(callRetBlock[labelIndex].size() > 1)
1079 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001080 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001081 }
John Bauman89401822014-05-06 15:04:28 -04001082
Nicolas Capens7551ac62016-01-20 17:11:53 -05001083 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001084
1085 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001086 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1087
Nicolas Capens7551ac62016-01-20 17:11:53 -05001088 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001089 }
1090
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001091 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001092 {
John Bauman19bac1e2014-05-06 15:23:49 -04001093 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001094 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001095 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001096 }
John Bauman19bac1e2014-05-06 15:23:49 -04001097 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001098 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001099 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001100 }
1101 else ASSERT(false);
1102 }
1103
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001104 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001105 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001106 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001107
John Bauman19bac1e2014-05-06 15:23:49 -04001108 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001109 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001110 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001111 }
1112
1113 if(!labelBlock[labelIndex])
1114 {
1115 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1116 }
1117
John Bauman19bac1e2014-05-06 15:23:49 -04001118 if(callRetBlock[labelIndex].size() > 1)
1119 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001120 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001121 }
John Bauman89401822014-05-06 15:04:28 -04001122
Nicolas Capens7551ac62016-01-20 17:11:53 -05001123 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001124
John Bauman19bac1e2014-05-06 15:23:49 -04001125 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1126 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1127
Nicolas Capens7551ac62016-01-20 17:11:53 -05001128 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001129 }
1130
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001131 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001132 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001133 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001134
John Bauman19bac1e2014-05-06 15:23:49 -04001135 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001136 {
1137 condition = ~condition;
1138 }
1139
Nicolas Capens7551ac62016-01-20 17:11:53 -05001140 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001141
1142 if(!labelBlock[labelIndex])
1143 {
1144 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1145 }
1146
John Bauman19bac1e2014-05-06 15:23:49 -04001147 if(callRetBlock[labelIndex].size() > 1)
1148 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001149 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001150 }
John Bauman89401822014-05-06 15:04:28 -04001151
Nicolas Capens7551ac62016-01-20 17:11:53 -05001152 enableIndex++;
1153 enableStack[enableIndex] = condition;
1154 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001155
John Bauman19bac1e2014-05-06 15:23:49 -04001156 Bool notAllFalse = SignMask(condition) != 0;
1157 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1158 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001159
Nicolas Capens7551ac62016-01-20 17:11:53 -05001160 enableIndex--;
1161 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001162 }
1163
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001164 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001165 {
1166 ifDepth--;
1167
1168 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1169 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1170
1171 if(isConditionalIf[ifDepth])
1172 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001173 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001174 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001175
1176 branch(notAllFalse, falseBlock, endBlock);
1177
Nicolas Capens7551ac62016-01-20 17:11:53 -05001178 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001179 }
1180 else
1181 {
1182 Nucleus::createBr(endBlock);
1183 Nucleus::setInsertBlock(falseBlock);
1184 }
1185
1186 ifFalseBlock[ifDepth] = endBlock;
1187
1188 ifDepth++;
1189 }
1190
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001191 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001192 {
1193 ifDepth--;
1194
1195 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1196
1197 Nucleus::createBr(endBlock);
1198 Nucleus::setInsertBlock(endBlock);
1199
1200 if(isConditionalIf[ifDepth])
1201 {
1202 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001203 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001204 }
1205 }
1206
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001207 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001208 {
1209 loopRepDepth--;
1210
Nicolas Capens7551ac62016-01-20 17:11:53 -05001211 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001212
1213 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1214 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1215
1216 Nucleus::createBr(testBlock);
1217 Nucleus::setInsertBlock(endBlock);
1218
Nicolas Capens7551ac62016-01-20 17:11:53 -05001219 loopDepth--;
1220 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001221 }
1222
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001223 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001224 {
1225 loopRepDepth--;
1226
1227 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1228 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1229
1230 Nucleus::createBr(testBlock);
1231 Nucleus::setInsertBlock(endBlock);
1232
Nicolas Capens7551ac62016-01-20 17:11:53 -05001233 loopDepth--;
1234 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001235 }
1236
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001237 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001238 {
1239 loopRepDepth--;
1240
1241 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1242 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1243
1244 Nucleus::createBr(testBlock);
1245 Nucleus::setInsertBlock(endBlock);
1246
Nicolas Capens7551ac62016-01-20 17:11:53 -05001247 enableIndex--;
1248 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001249 whileTest = false;
1250 }
1251
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001252 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001253 {
John Bauman19bac1e2014-05-06 15:23:49 -04001254 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001255 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001256 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001257 }
John Bauman19bac1e2014-05-06 15:23:49 -04001258 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001259 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001260 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001261 }
John Bauman19bac1e2014-05-06 15:23:49 -04001262 else
1263 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001264 Int4 condition = As<Int4>(fetchRegisterF(src).x);
1265 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001266 }
John Bauman89401822014-05-06 15:04:28 -04001267 }
1268
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001269 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001270 {
1271 ASSERT(ifDepth < 24 + 4);
1272
Nicolas Capens7551ac62016-01-20 17:11:53 -05001273 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001274
John Bauman19bac1e2014-05-06 15:23:49 -04001275 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001276 {
John Bauman19bac1e2014-05-06 15:23:49 -04001277 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001278 }
1279
1280 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1281 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1282
1283 branch(condition, trueBlock, falseBlock);
1284
1285 isConditionalIf[ifDepth] = false;
1286 ifFalseBlock[ifDepth] = falseBlock;
1287
1288 ifDepth++;
1289 }
1290
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001291 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001292 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001293 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001294
John Bauman19bac1e2014-05-06 15:23:49 -04001295 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001296 {
1297 condition = ~condition;
1298 }
1299
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001300 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001301 }
1302
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001303 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001304 {
1305 Int4 condition;
1306
1307 switch(control)
1308 {
John Bauman19bac1e2014-05-06 15:23:49 -04001309 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1310 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1311 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1312 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1313 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1314 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001315 default:
1316 ASSERT(false);
1317 }
1318
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001319 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001320 }
1321
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001322 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001323 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001324 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001325
Nicolas Capens7551ac62016-01-20 17:11:53 -05001326 enableIndex++;
1327 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001328
1329 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1330 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1331
John Bauman19bac1e2014-05-06 15:23:49 -04001332 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001333
1334 branch(notAllFalse, trueBlock, falseBlock);
1335
1336 isConditionalIf[ifDepth] = true;
1337 ifFalseBlock[ifDepth] = falseBlock;
1338
1339 ifDepth++;
1340 breakDepth++;
1341 }
1342
1343 void VertexProgram::LABEL(int labelIndex)
1344 {
John Bauman19bac1e2014-05-06 15:23:49 -04001345 if(!labelBlock[labelIndex])
1346 {
1347 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1348 }
1349
John Bauman89401822014-05-06 15:04:28 -04001350 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001351 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001352 }
1353
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001354 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001355 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001356 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001357
Nicolas Capens7551ac62016-01-20 17:11:53 -05001358 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1359 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1360 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001361
1362 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001363 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001364 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001365 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001366 }
1367
1368 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1369 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1370 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1371
1372 loopRepTestBlock[loopRepDepth] = testBlock;
1373 loopRepEndBlock[loopRepDepth] = endBlock;
1374
1375 // FIXME: jump(testBlock)
1376 Nucleus::createBr(testBlock);
1377 Nucleus::setInsertBlock(testBlock);
1378
Nicolas Capens7551ac62016-01-20 17:11:53 -05001379 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001380 Nucleus::setInsertBlock(loopBlock);
1381
Nicolas Capens7551ac62016-01-20 17:11:53 -05001382 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001383
John Bauman89401822014-05-06 15:04:28 -04001384 loopRepDepth++;
1385 breakDepth = 0;
1386 }
1387
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001388 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001389 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001390 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001391
Nicolas Capens7551ac62016-01-20 17:11:53 -05001392 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1393 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001394
1395 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1396 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1397 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1398
1399 loopRepTestBlock[loopRepDepth] = testBlock;
1400 loopRepEndBlock[loopRepDepth] = endBlock;
1401
1402 // FIXME: jump(testBlock)
1403 Nucleus::createBr(testBlock);
1404 Nucleus::setInsertBlock(testBlock);
1405
Nicolas Capens7551ac62016-01-20 17:11:53 -05001406 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001407 Nucleus::setInsertBlock(loopBlock);
1408
Nicolas Capens7551ac62016-01-20 17:11:53 -05001409 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001410
1411 loopRepDepth++;
1412 breakDepth = 0;
1413 }
1414
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001415 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001416 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001417 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001418
1419 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1420 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1421 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001422
John Bauman19bac1e2014-05-06 15:23:49 -04001423 loopRepTestBlock[loopRepDepth] = testBlock;
1424 loopRepEndBlock[loopRepDepth] = endBlock;
1425
Nicolas Capens7551ac62016-01-20 17:11:53 -05001426 Int4 restoreBreak = enableBreak;
1427 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001428
1429 // FIXME: jump(testBlock)
1430 Nucleus::createBr(testBlock);
1431 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001432 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001433
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001434 const Vector4f &src = fetchRegisterF(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001435 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001436 condition &= enableStack[enableIndex - 1];
1437 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001438
1439 Bool notAllFalse = SignMask(condition) != 0;
1440 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001441
John Bauman19bac1e2014-05-06 15:23:49 -04001442 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001443 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001444
John Bauman19bac1e2014-05-06 15:23:49 -04001445 Nucleus::setInsertBlock(loopBlock);
1446
1447 loopRepDepth++;
1448 breakDepth = 0;
1449 }
1450
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001451 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001452 {
John Bauman19bac1e2014-05-06 15:23:49 -04001453 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001454 {
1455 returnBlock = Nucleus::createBasicBlock();
1456 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001457 }
1458 else
1459 {
John Bauman89401822014-05-06 15:04:28 -04001460 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001461
John Bauman19bac1e2014-05-06 15:23:49 -04001462 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001463 {
John Bauman19bac1e2014-05-06 15:23:49 -04001464 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001465 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001466
John Bauman66b8ab22014-05-06 15:57:45 -04001467 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001468 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1469
1470 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1471 {
1472 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1473 }
1474 }
1475 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1476 {
1477 Nucleus::createBr(callRetBlock[currentLabel][0]);
1478 }
1479 else // Function isn't called
1480 {
1481 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001482 }
1483
1484 Nucleus::setInsertBlock(unreachableBlock);
1485 Nucleus::createUnreachable();
1486 }
1487 }
1488
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001489 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001490 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001491 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001492
John Bauman19bac1e2014-05-06 15:23:49 -04001493 // FIXME: Return from function if all instances left
1494 // FIXME: Use enableLeave in other control-flow constructs
1495 }
John Bauman89401822014-05-06 15:04:28 -04001496
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001497 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001498 {
1499 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001500 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001501
1502 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1503 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1504 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1505 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1506 }
John Bauman19bac1e2014-05-06 15:23:49 -04001507
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001508 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001509 {
1510 Float4 lod = Float4(0.0f);
1511 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001512 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001513
1514 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1515 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1516 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1517 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1518 }
1519
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001520 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001521 {
1522 UNIMPLEMENTED();
1523 }
1524
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001525 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001526 {
1527 UNIMPLEMENTED();
1528 }
1529
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001530 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001531 {
1532 UNIMPLEMENTED();
1533 }
1534
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001535 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001536 {
1537 UNIMPLEMENTED();
1538 }
1539
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001540 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001541 {
1542 UNIMPLEMENTED();
1543 }
1544
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001545 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001546 {
1547 UNIMPLEMENTED();
1548 }
1549
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001550 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001551 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001552 Pointer<Byte> textureMipmap = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001553 for(int i = 0; i < 4; ++i)
1554 {
1555 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1556 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1557 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1558 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1559 }
1560 }
1561
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001562 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
John Bauman19bac1e2014-05-06 15:23:49 -04001563 {
1564 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1565 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001566 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1567 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, a0, a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001568 }
1569 else
1570 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001571 Int index = As<Int>(Float(fetchRegisterF(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001572
1573 for(int i = 0; i < 16; i++)
1574 {
1575 if(shader->usesSampler(i))
1576 {
1577 If(index == i)
1578 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001579 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1580 sampler[i]->sampleTexture(texture, c, u, v, w, q, a0, a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001581 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1582 }
1583 }
1584 }
1585 }
1586 }
John Bauman89401822014-05-06 15:04:28 -04001587}