blob: c05ace3ad926672dbda44acddc2c3e8ac03ab3fe [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 Capensc2534f42016-04-04 11:13:24 -0400139 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
140 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
141 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
142 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
143 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(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;
Alexis Hetu0f448072016-03-18 10:56:08 -0400160 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500161 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400162 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500163 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
164 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
165 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
166 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
167 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
168 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
169 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
170 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
171 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
172 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
173 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
174 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
175 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
176 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
177 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
178 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
179 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400180 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
181 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
182 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500183 case Shader::OPCODE_ATT: att(d, s0, s1); break;
184 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
185 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
186 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
187 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
188 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400189 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
190 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400191 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500192 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400193 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500194 case Shader::OPCODE_LIT: lit(d, s0); break;
195 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
196 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
197 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
198 case Shader::OPCODE_LOG: log(d, s0, pp); break;
199 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
200 case Shader::OPCODE_STEP: step(d, s0, s1); break;
201 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400202 case Shader::OPCODE_FLOATBITSTOINT:
203 case Shader::OPCODE_FLOATBITSTOUINT:
204 case Shader::OPCODE_INTBITSTOFLOAT:
205 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400206 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
207 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
208 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
209 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500210 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
211 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
212 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
213 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
214 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
215 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
216 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
217 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400218 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
219 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500220 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400221 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
222 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500223 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400224 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400225 case Shader::OPCODE_NEG: neg(d, s0); break;
226 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500227 case Shader::OPCODE_F2B: f2b(d, s0); break;
228 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400229 case Shader::OPCODE_F2I: f2i(d, s0); break;
230 case Shader::OPCODE_I2F: i2f(d, s0); break;
231 case Shader::OPCODE_F2U: f2u(d, s0); break;
232 case Shader::OPCODE_U2F: u2f(d, s0); break;
233 case Shader::OPCODE_I2B: i2b(d, s0); break;
234 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500235 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400236 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500237 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
238 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
239 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
240 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
241 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
242 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
243 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400244 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
245 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500246 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400247 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
248 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
249 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500250 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
251 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
252 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
253 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
254 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
255 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
256 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
257 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
258 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
259 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
260 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
261 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
262 case Shader::OPCODE_SGE: step(d, s1, s0); break;
263 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400264 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500265 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
266 case Shader::OPCODE_COS: cos(d, s0, pp); break;
267 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
268 case Shader::OPCODE_TAN: tan(d, s0); break;
269 case Shader::OPCODE_ACOS: acos(d, s0); break;
270 case Shader::OPCODE_ASIN: asin(d, s0); break;
271 case Shader::OPCODE_ATAN: atan(d, s0); break;
272 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
273 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
274 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
275 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
276 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
277 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
278 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
279 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
280 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400281 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500282 case Shader::OPCODE_BREAK: BREAK(); break;
283 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
284 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
285 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
286 case Shader::OPCODE_TEST: TEST(); break;
287 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
288 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
289 case Shader::OPCODE_ELSE: ELSE(); break;
290 case Shader::OPCODE_ENDIF: ENDIF(); break;
291 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
292 case Shader::OPCODE_ENDREP: ENDREP(); break;
293 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
294 case Shader::OPCODE_IF: IF(src0); break;
295 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
296 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
297 case Shader::OPCODE_LOOP: LOOP(src1); break;
298 case Shader::OPCODE_REP: REP(src0); break;
299 case Shader::OPCODE_WHILE: WHILE(src0); break;
300 case Shader::OPCODE_RET: RET(); break;
301 case Shader::OPCODE_LEAVE: LEAVE(); break;
302 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
303 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400304 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500305 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
306 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
307 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
308 case Shader::OPCODE_ALL: all(d.x, s0); break;
309 case Shader::OPCODE_ANY: any(d.x, s0); break;
310 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400311 case Shader::OPCODE_OR: or(d, s0, s1); break;
312 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
313 case Shader::OPCODE_AND: and(d, s0, s1); break;
314 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
315 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500316 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
317 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
318 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2, s3); break;
319 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
320 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2); break;
321 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2, s3); break;
322 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
323 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
324 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
325 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400326 default:
327 ASSERT(false);
328 }
329
John Bauman19bac1e2014-05-06 15:23:49 -0400330 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400331 {
John Bauman19bac1e2014-05-06 15:23:49 -0400332 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400333 {
John Bauman19bac1e2014-05-06 15:23:49 -0400334 switch(opcode)
335 {
336 case Shader::OPCODE_DIV:
337 if(dst.x) d.x = Trunc(d.x);
338 if(dst.y) d.y = Trunc(d.y);
339 if(dst.z) d.z = Trunc(d.z);
340 if(dst.w) d.w = Trunc(d.w);
341 break;
342 default:
343 break; // No truncation to integer required when arguments are integer
344 }
John Bauman89401822014-05-06 15:04:28 -0400345 }
346
John Bauman19bac1e2014-05-06 15:23:49 -0400347 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400348 {
John Bauman19bac1e2014-05-06 15:23:49 -0400349 if(dst.x) d.x = Max(d.x, Float4(0.0f));
350 if(dst.y) d.y = Max(d.y, Float4(0.0f));
351 if(dst.z) d.z = Max(d.z, Float4(0.0f));
352 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400353
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.x) d.x = Min(d.x, Float4(1.0f));
355 if(dst.y) d.y = Min(d.y, Float4(1.0f));
356 if(dst.z) d.z = Min(d.z, Float4(1.0f));
357 if(dst.w) d.w = Min(d.w, Float4(1.0f));
358 }
359
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400360 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400361 {
362 Vector4f pDst; // FIXME: Rename
363
364 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400365 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500366 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400367 case Shader::PARAMETER_TEMP:
368 if(dst.rel.type == Shader::PARAMETER_VOID)
369 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500370 if(dst.x) pDst.x = r[dst.index].x;
371 if(dst.y) pDst.y = r[dst.index].y;
372 if(dst.z) pDst.z = r[dst.index].z;
373 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400374 }
375 else
376 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500377 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400378
Nicolas Capens7551ac62016-01-20 17:11:53 -0500379 if(dst.x) pDst.x = r[dst.index + a].x;
380 if(dst.y) pDst.y = r[dst.index + a].y;
381 if(dst.z) pDst.z = r[dst.index + a].z;
382 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400383 }
384 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500385 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400386 case Shader::PARAMETER_RASTOUT:
387 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400388 {
389 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500390 if(dst.x) pDst.x = o[Pos].x;
391 if(dst.y) pDst.y = o[Pos].y;
392 if(dst.z) pDst.z = o[Pos].z;
393 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400394 break;
395 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500396 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400397 break;
398 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500399 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400400 break;
401 default:
402 ASSERT(false);
403 }
404 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400405 case Shader::PARAMETER_ATTROUT:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500406 if(dst.x) pDst.x = o[D0 + dst.index].x;
407 if(dst.y) pDst.y = o[D0 + dst.index].y;
408 if(dst.z) pDst.z = o[D0 + dst.index].z;
409 if(dst.w) pDst.w = o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400410 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400411 case Shader::PARAMETER_TEXCRDOUT:
412 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400413 if(version < 0x0300)
414 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500415 if(dst.x) pDst.x = o[T0 + dst.index].x;
416 if(dst.y) pDst.y = o[T0 + dst.index].y;
417 if(dst.z) pDst.z = o[T0 + dst.index].z;
418 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400419 }
420 else
421 {
John Bauman19bac1e2014-05-06 15:23:49 -0400422 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400423 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500424 if(dst.x) pDst.x = o[dst.index].x;
425 if(dst.y) pDst.y = o[dst.index].y;
426 if(dst.z) pDst.z = o[dst.index].z;
427 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400428 }
John Bauman19bac1e2014-05-06 15:23:49 -0400429 else
430 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500431 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400432
Nicolas Capens7551ac62016-01-20 17:11:53 -0500433 if(dst.x) pDst.x = o[dst.index + a].x;
434 if(dst.y) pDst.y = o[dst.index + a].y;
435 if(dst.z) pDst.z = o[dst.index + a].z;
436 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400437 }
438 }
439 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500440 case Shader::PARAMETER_LABEL: break;
441 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
442 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400443 default:
444 ASSERT(false);
445 }
446
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500447 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400448
449 Int4 xEnable = enable;
450 Int4 yEnable = enable;
451 Int4 zEnable = enable;
452 Int4 wEnable = enable;
453
454 if(predicate)
455 {
John Bauman19bac1e2014-05-06 15:23:49 -0400456 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400457
Nicolas Capens7551ac62016-01-20 17:11:53 -0500458 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
459 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
460 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
461 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400462
John Bauman19bac1e2014-05-06 15:23:49 -0400463 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400464 {
John Bauman19bac1e2014-05-06 15:23:49 -0400465 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
466 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
467 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
468 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400469 }
470 else
471 {
John Bauman19bac1e2014-05-06 15:23:49 -0400472 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
473 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
474 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
475 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400476 }
477 }
478
John Bauman19bac1e2014-05-06 15:23:49 -0400479 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
480 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
481 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
482 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400483
John Bauman19bac1e2014-05-06 15:23:49 -0400484 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
485 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
486 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
487 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400488 }
489
John Bauman19bac1e2014-05-06 15:23:49 -0400490 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400491 {
John Bauman19bac1e2014-05-06 15:23:49 -0400492 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400493 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400494 case Shader::PARAMETER_TEMP:
495 if(dst.rel.type == Shader::PARAMETER_VOID)
496 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500497 if(dst.x) r[dst.index].x = d.x;
498 if(dst.y) r[dst.index].y = d.y;
499 if(dst.z) r[dst.index].z = d.z;
500 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400501 }
502 else
503 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500504 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400505
Nicolas Capens7551ac62016-01-20 17:11:53 -0500506 if(dst.x) r[dst.index + a].x = d.x;
507 if(dst.y) r[dst.index + a].y = d.y;
508 if(dst.z) r[dst.index + a].z = d.z;
509 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400510 }
John Bauman89401822014-05-06 15:04:28 -0400511 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400512 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500513 if(dst.x) a0.x = d.x;
514 if(dst.y) a0.y = d.y;
515 if(dst.z) a0.z = d.z;
516 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400517 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400518 case Shader::PARAMETER_RASTOUT:
519 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400520 {
521 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500522 if(dst.x) o[Pos].x = d.x;
523 if(dst.y) o[Pos].y = d.y;
524 if(dst.z) o[Pos].z = d.z;
525 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400526 break;
527 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500528 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400529 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500530 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500531 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400532 break;
533 default: ASSERT(false);
534 }
535 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500536 case Shader::PARAMETER_ATTROUT:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500537 if(dst.x) o[D0 + dst.index].x = d.x;
538 if(dst.y) o[D0 + dst.index].y = d.y;
539 if(dst.z) o[D0 + dst.index].z = d.z;
540 if(dst.w) o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400541 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400542 case Shader::PARAMETER_TEXCRDOUT:
543 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400544 if(version < 0x0300)
545 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500546 if(dst.x) o[T0 + dst.index].x = d.x;
547 if(dst.y) o[T0 + dst.index].y = d.y;
548 if(dst.z) o[T0 + dst.index].z = d.z;
549 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400550 }
551 else
552 {
John Bauman19bac1e2014-05-06 15:23:49 -0400553 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400554 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500555 if(dst.x) o[dst.index].x = d.x;
556 if(dst.y) o[dst.index].y = d.y;
557 if(dst.z) o[dst.index].z = d.z;
558 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400559 }
John Bauman19bac1e2014-05-06 15:23:49 -0400560 else
561 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500562 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400563
Nicolas Capens7551ac62016-01-20 17:11:53 -0500564 if(dst.x) o[dst.index + a].x = d.x;
565 if(dst.y) o[dst.index + a].y = d.y;
566 if(dst.z) o[dst.index + a].z = d.z;
567 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400568 }
569 }
570 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500571 case Shader::PARAMETER_LABEL: break;
572 case Shader::PARAMETER_PREDICATE: p0 = d; break;
573 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400574 default:
575 ASSERT(false);
576 }
577 }
578 }
579
John Bauman19bac1e2014-05-06 15:23:49 -0400580 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400581 {
582 Nucleus::setInsertBlock(returnBlock);
583 }
584 }
585
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500586 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400587 {
John Bauman19bac1e2014-05-06 15:23:49 -0400588 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400589 {
590 for(int i = 0; i < 12; i++)
591 {
John Bauman19bac1e2014-05-06 15:23:49 -0400592 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400593
594 switch(usage)
595 {
596 case 0xFF:
597 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400598 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500599 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400600 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400601 case Shader::USAGE_TEXCOORD:
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_POSITION:
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_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500614 o[i].x = v[i].x;
615 o[i].y = v[i].y;
616 o[i].z = v[i].z;
617 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400618 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400619 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500620 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400621 break;
622 default:
623 ASSERT(false);
624 }
625 }
626 }
627 else
628 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500629 o[Pos].x = v[PositionT].x;
630 o[Pos].y = v[PositionT].y;
631 o[Pos].z = v[PositionT].z;
632 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400633
634 for(int i = 0; i < 2; i++)
635 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500636 o[D0 + i].x = v[Color0 + i].x;
637 o[D0 + i].y = v[Color0 + i].y;
638 o[D0 + i].z = v[Color0 + i].z;
639 o[D0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400640 }
641
642 for(int i = 0; i < 8; i++)
643 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500644 o[T0 + i].x = v[TexCoord0 + i].x;
645 o[T0 + i].y = v[TexCoord0 + i].y;
646 o[T0 + i].z = v[TexCoord0 + i].z;
647 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400648 }
649
Nicolas Capens7551ac62016-01-20 17:11:53 -0500650 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400651 }
652 }
653
Nicolas Capensc2534f42016-04-04 11:13:24 -0400654 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400655 {
John Bauman19bac1e2014-05-06 15:23:49 -0400656 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500657 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400658
John Bauman89401822014-05-06 15:04:28 -0400659 switch(src.type)
660 {
John Bauman19bac1e2014-05-06 15:23:49 -0400661 case Shader::PARAMETER_TEMP:
662 if(src.rel.type == Shader::PARAMETER_VOID)
663 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500664 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400665 }
666 else
667 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400668 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400669 }
670 break;
671 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500672 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400673 break;
674 case Shader::PARAMETER_INPUT:
675 if(src.rel.type == Shader::PARAMETER_VOID)
676 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500677 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400678 }
679 else
680 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400681 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400682 }
683 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500684 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400685 case Shader::PARAMETER_FLOAT4LITERAL:
686 reg.x = Float4(src.value[0]);
687 reg.y = Float4(src.value[1]);
688 reg.z = Float4(src.value[2]);
689 reg.w = Float4(src.value[3]);
690 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500691 case Shader::PARAMETER_ADDR: reg = a0; break;
692 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
693 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
694 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
695 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400696 case Shader::PARAMETER_SAMPLER:
697 if(src.rel.type == Shader::PARAMETER_VOID)
698 {
699 reg.x = As<Float4>(Int4(i));
700 }
701 else if(src.rel.type == Shader::PARAMETER_TEMP)
702 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500703 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400704 }
705 return reg;
706 case Shader::PARAMETER_OUTPUT:
707 if(src.rel.type == Shader::PARAMETER_VOID)
708 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500709 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400710 }
711 else
712 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400713 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400714 }
715 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400716 case Shader::PARAMETER_MISCTYPE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500717 reg.x = As<Float>(Int(instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400718 return reg;
John Bauman89401822014-05-06 15:04:28 -0400719 default:
720 ASSERT(false);
721 }
722
John Bauman66b8ab22014-05-06 15:57:45 -0400723 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
724 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
725 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
726 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400727
John Bauman66b8ab22014-05-06 15:57:45 -0400728 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400729
730 switch(src.modifier)
731 {
John Bauman19bac1e2014-05-06 15:23:49 -0400732 case Shader::MODIFIER_NONE:
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_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400739 mod.x = -x;
740 mod.y = -y;
741 mod.z = -z;
742 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400743 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400744 case Shader::MODIFIER_ABS:
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_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400751 mod.x = -Abs(x);
752 mod.y = -Abs(y);
753 mod.z = -Abs(z);
754 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400755 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400756 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400757 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
758 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
759 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
760 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400761 break;
762 default:
763 ASSERT(false);
764 }
765
766 return mod;
767 }
768
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400769 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
770 {
771 if(bufferIndex == -1)
772 {
773 return data + OFFSET(DrawData, vs.c[index]);
774 }
775 else
776 {
777 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
778 }
779 }
780
781 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
782 {
783 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
784 }
785
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500786 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400787 {
John Bauman19bac1e2014-05-06 15:23:49 -0400788 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500789 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400790
791 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
792 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400793 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400794
795 c.x = c.x.xxxx;
796 c.y = c.y.yyyy;
797 c.z = c.z.zzzz;
798 c.w = c.w.wwww;
799
Nicolas Capenseafdb222015-05-15 15:24:08 -0400800 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400801 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500802 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400803 {
804 const Shader::Instruction &instruction = *shader->getInstruction(j);
805
806 if(instruction.opcode == Shader::OPCODE_DEF)
807 {
808 if(instruction.dst.index == i)
809 {
810 c.x = Float4(instruction.src[0].value[0]);
811 c.y = Float4(instruction.src[0].value[1]);
812 c.z = Float4(instruction.src[0].value[2]);
813 c.w = Float4(instruction.src[0].value[3]);
814
815 break;
816 }
817 }
818 }
819 }
820 }
821 else if(src.rel.type == Shader::PARAMETER_LOOP)
822 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500823 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400824
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400825 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400826
827 c.x = c.x.xxxx;
828 c.y = c.y.yyyy;
829 c.z = c.z.zzzz;
830 c.w = c.w.wwww;
831 }
832 else
833 {
834 if(src.rel.deterministic)
835 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400836 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400837
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400838 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500839
John Bauman19bac1e2014-05-06 15:23:49 -0400840 c.x = c.x.xxxx;
841 c.y = c.y.yyyy;
842 c.z = c.z.zzzz;
843 c.w = c.w.wwww;
844 }
845 else
846 {
847 int component = src.rel.swizzle & 0x03;
848 Float4 a;
849
850 switch(src.rel.type)
851 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500852 case Shader::PARAMETER_ADDR: a = a0[component]; break;
853 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
854 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
855 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400856 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400857 default: ASSERT(false);
858 }
859
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400860 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400861
Alexis Hetu028f41b2016-01-13 14:40:47 -0500862 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 -0500863
John Bauman19bac1e2014-05-06 15:23:49 -0400864 Int index0 = Extract(index, 0);
865 Int index1 = Extract(index, 1);
866 Int index2 = Extract(index, 2);
867 Int index3 = Extract(index, 3);
868
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400869 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
870 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
871 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
872 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400873
874 transpose4x4(c.x, c.y, c.z, c.w);
875 }
876 }
877
878 return c;
879 }
880
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400881 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400882 {
883 ASSERT(var.rel.deterministic);
884
885 if(var.rel.type == Shader::PARAMETER_TEMP)
886 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500887 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400888 }
889 else if(var.rel.type == Shader::PARAMETER_INPUT)
890 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500891 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400892 }
893 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
894 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500895 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400896 }
897 else if(var.rel.type == Shader::PARAMETER_CONST)
898 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500899 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400900 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500901 else if(var.rel.type == Shader::PARAMETER_LOOP)
902 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500903 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500904 }
John Bauman19bac1e2014-05-06 15:23:49 -0400905 else ASSERT(false);
906
907 return 0;
908 }
909
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500910 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400911 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500912 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500913
John Baumand4ae8632014-05-06 16:18:33 -0400914 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400915 {
John Baumand4ae8632014-05-06 16:18:33 -0400916 if(shader->containsBreakInstruction() && instruction->analysisBreak)
917 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500918 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400919 }
John Bauman19bac1e2014-05-06 15:23:49 -0400920
John Baumand4ae8632014-05-06 16:18:33 -0400921 if(shader->containsContinueInstruction() && instruction->analysisContinue)
922 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500923 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400924 }
John Bauman19bac1e2014-05-06 15:23:49 -0400925
John Baumand4ae8632014-05-06 16:18:33 -0400926 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
927 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500928 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400929 }
John Bauman19bac1e2014-05-06 15:23:49 -0400930 }
931
932 return enable;
933 }
934
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500935 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400936 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400937 Vector4f row0 = fetchRegister(src1, 0);
938 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400939
940 dst.x = dot3(src0, row0);
941 dst.y = dot3(src0, row1);
942 }
943
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500944 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400945 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400946 Vector4f row0 = fetchRegister(src1, 0);
947 Vector4f row1 = fetchRegister(src1, 1);
948 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400949
950 dst.x = dot3(src0, row0);
951 dst.y = dot3(src0, row1);
952 dst.z = dot3(src0, row2);
953 }
954
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500955 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400956 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400957 Vector4f row0 = fetchRegister(src1, 0);
958 Vector4f row1 = fetchRegister(src1, 1);
959 Vector4f row2 = fetchRegister(src1, 2);
960 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400961
962 dst.x = dot3(src0, row0);
963 dst.y = dot3(src0, row1);
964 dst.z = dot3(src0, row2);
965 dst.w = dot3(src0, row3);
966 }
967
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500968 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400969 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400970 Vector4f row0 = fetchRegister(src1, 0);
971 Vector4f row1 = fetchRegister(src1, 1);
972 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400973
974 dst.x = dot4(src0, row0);
975 dst.y = dot4(src0, row1);
976 dst.z = dot4(src0, row2);
977 }
978
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500979 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400980 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400981 Vector4f row0 = fetchRegister(src1, 0);
982 Vector4f row1 = fetchRegister(src1, 1);
983 Vector4f row2 = fetchRegister(src1, 2);
984 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400985
986 dst.x = dot4(src0, row0);
987 dst.y = dot4(src0, row1);
988 dst.z = dot4(src0, row2);
989 dst.w = dot4(src0, row3);
990 }
991
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500992 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -0400993 {
994 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
995 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
996
997 if(breakDepth == 0)
998 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500999 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001000 Nucleus::createBr(endBlock);
1001 }
1002 else
1003 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001004 enableBreak = enableBreak & ~enableStack[enableIndex];
1005 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001006
Nicolas Capens7551ac62016-01-20 17:11:53 -05001007 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001008 branch(allBreak, endBlock, deadBlock);
1009 }
1010
1011 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001012 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001013 }
1014
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001015 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001016 {
1017 Int4 condition;
1018
1019 switch(control)
1020 {
John Bauman19bac1e2014-05-06 15:23:49 -04001021 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1022 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1023 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1024 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1025 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1026 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001027 default:
1028 ASSERT(false);
1029 }
1030
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001031 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001032 }
1033
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001034 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001035 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001036 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001037
John Bauman19bac1e2014-05-06 15:23:49 -04001038 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001039 {
1040 condition = ~condition;
1041 }
1042
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001043 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001044 }
1045
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001046 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001047 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001048 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001049
1050 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1051 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1052
Nicolas Capens7551ac62016-01-20 17:11:53 -05001053 enableBreak = enableBreak & ~condition;
1054 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001055
Nicolas Capens7551ac62016-01-20 17:11:53 -05001056 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001057 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001058
John Bauman89401822014-05-06 15:04:28 -04001059 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001060 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001061 }
1062
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001063 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001064 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001065 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001066 }
1067
1068 void VertexProgram::TEST()
1069 {
1070 whileTest = true;
1071 }
1072
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001073 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001074 {
1075 if(!labelBlock[labelIndex])
1076 {
1077 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1078 }
1079
John Bauman19bac1e2014-05-06 15:23:49 -04001080 if(callRetBlock[labelIndex].size() > 1)
1081 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001082 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001083 }
John Bauman89401822014-05-06 15:04:28 -04001084
Nicolas Capens7551ac62016-01-20 17:11:53 -05001085 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001086
1087 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001088 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1089
Nicolas Capens7551ac62016-01-20 17:11:53 -05001090 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001091 }
1092
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001093 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001094 {
John Bauman19bac1e2014-05-06 15:23:49 -04001095 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001096 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001097 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001098 }
John Bauman19bac1e2014-05-06 15:23:49 -04001099 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001100 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001101 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001102 }
1103 else ASSERT(false);
1104 }
1105
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001106 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001107 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001108 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001109
John Bauman19bac1e2014-05-06 15:23:49 -04001110 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001111 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001112 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001113 }
1114
1115 if(!labelBlock[labelIndex])
1116 {
1117 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1118 }
1119
John Bauman19bac1e2014-05-06 15:23:49 -04001120 if(callRetBlock[labelIndex].size() > 1)
1121 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001122 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001123 }
John Bauman89401822014-05-06 15:04:28 -04001124
Nicolas Capens7551ac62016-01-20 17:11:53 -05001125 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001126
John Bauman19bac1e2014-05-06 15:23:49 -04001127 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1128 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1129
Nicolas Capens7551ac62016-01-20 17:11:53 -05001130 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001131 }
1132
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001133 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001134 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001135 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001136
John Bauman19bac1e2014-05-06 15:23:49 -04001137 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001138 {
1139 condition = ~condition;
1140 }
1141
Nicolas Capens7551ac62016-01-20 17:11:53 -05001142 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001143
1144 if(!labelBlock[labelIndex])
1145 {
1146 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1147 }
1148
John Bauman19bac1e2014-05-06 15:23:49 -04001149 if(callRetBlock[labelIndex].size() > 1)
1150 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001151 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001152 }
John Bauman89401822014-05-06 15:04:28 -04001153
Nicolas Capens7551ac62016-01-20 17:11:53 -05001154 enableIndex++;
1155 enableStack[enableIndex] = condition;
1156 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001157
John Bauman19bac1e2014-05-06 15:23:49 -04001158 Bool notAllFalse = SignMask(condition) != 0;
1159 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1160 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001161
Nicolas Capens7551ac62016-01-20 17:11:53 -05001162 enableIndex--;
1163 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001164 }
1165
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001166 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001167 {
1168 ifDepth--;
1169
1170 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1171 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1172
1173 if(isConditionalIf[ifDepth])
1174 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001175 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001176 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001177
1178 branch(notAllFalse, falseBlock, endBlock);
1179
Nicolas Capens7551ac62016-01-20 17:11:53 -05001180 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001181 }
1182 else
1183 {
1184 Nucleus::createBr(endBlock);
1185 Nucleus::setInsertBlock(falseBlock);
1186 }
1187
1188 ifFalseBlock[ifDepth] = endBlock;
1189
1190 ifDepth++;
1191 }
1192
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001193 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001194 {
1195 ifDepth--;
1196
1197 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1198
1199 Nucleus::createBr(endBlock);
1200 Nucleus::setInsertBlock(endBlock);
1201
1202 if(isConditionalIf[ifDepth])
1203 {
1204 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001205 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001206 }
1207 }
1208
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001209 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001210 {
1211 loopRepDepth--;
1212
Nicolas Capens7551ac62016-01-20 17:11:53 -05001213 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001214
1215 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1216 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1217
1218 Nucleus::createBr(testBlock);
1219 Nucleus::setInsertBlock(endBlock);
1220
Nicolas Capens7551ac62016-01-20 17:11:53 -05001221 loopDepth--;
1222 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001223 }
1224
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001225 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001226 {
1227 loopRepDepth--;
1228
1229 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1230 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1231
1232 Nucleus::createBr(testBlock);
1233 Nucleus::setInsertBlock(endBlock);
1234
Nicolas Capens7551ac62016-01-20 17:11:53 -05001235 loopDepth--;
1236 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001237 }
1238
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001239 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001240 {
1241 loopRepDepth--;
1242
1243 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1244 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1245
1246 Nucleus::createBr(testBlock);
1247 Nucleus::setInsertBlock(endBlock);
1248
Nicolas Capens7551ac62016-01-20 17:11:53 -05001249 enableIndex--;
1250 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001251 whileTest = false;
1252 }
1253
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001254 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001255 {
John Bauman19bac1e2014-05-06 15:23:49 -04001256 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001257 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001258 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001259 }
John Bauman19bac1e2014-05-06 15:23:49 -04001260 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001261 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001262 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001263 }
John Bauman19bac1e2014-05-06 15:23:49 -04001264 else
1265 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001266 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001267 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001268 }
John Bauman89401822014-05-06 15:04:28 -04001269 }
1270
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001271 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001272 {
1273 ASSERT(ifDepth < 24 + 4);
1274
Nicolas Capens7551ac62016-01-20 17:11:53 -05001275 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001276
John Bauman19bac1e2014-05-06 15:23:49 -04001277 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001278 {
John Bauman19bac1e2014-05-06 15:23:49 -04001279 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001280 }
1281
1282 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1283 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1284
1285 branch(condition, trueBlock, falseBlock);
1286
1287 isConditionalIf[ifDepth] = false;
1288 ifFalseBlock[ifDepth] = falseBlock;
1289
1290 ifDepth++;
1291 }
1292
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001293 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001294 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001295 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001296
John Bauman19bac1e2014-05-06 15:23:49 -04001297 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001298 {
1299 condition = ~condition;
1300 }
1301
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001302 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001303 }
1304
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001305 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001306 {
1307 Int4 condition;
1308
1309 switch(control)
1310 {
John Bauman19bac1e2014-05-06 15:23:49 -04001311 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1312 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1313 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1314 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1315 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1316 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001317 default:
1318 ASSERT(false);
1319 }
1320
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001321 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001322 }
1323
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001324 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001325 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001326 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001327
Nicolas Capens7551ac62016-01-20 17:11:53 -05001328 enableIndex++;
1329 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001330
1331 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1332 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1333
John Bauman19bac1e2014-05-06 15:23:49 -04001334 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001335
1336 branch(notAllFalse, trueBlock, falseBlock);
1337
1338 isConditionalIf[ifDepth] = true;
1339 ifFalseBlock[ifDepth] = falseBlock;
1340
1341 ifDepth++;
1342 breakDepth++;
1343 }
1344
1345 void VertexProgram::LABEL(int labelIndex)
1346 {
John Bauman19bac1e2014-05-06 15:23:49 -04001347 if(!labelBlock[labelIndex])
1348 {
1349 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1350 }
1351
John Bauman89401822014-05-06 15:04:28 -04001352 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001353 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001354 }
1355
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001356 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001357 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001358 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001359
Nicolas Capens7551ac62016-01-20 17:11:53 -05001360 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1361 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1362 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001363
1364 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001365 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001366 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001367 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001368 }
1369
1370 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1371 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1372 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1373
1374 loopRepTestBlock[loopRepDepth] = testBlock;
1375 loopRepEndBlock[loopRepDepth] = endBlock;
1376
1377 // FIXME: jump(testBlock)
1378 Nucleus::createBr(testBlock);
1379 Nucleus::setInsertBlock(testBlock);
1380
Nicolas Capens7551ac62016-01-20 17:11:53 -05001381 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001382 Nucleus::setInsertBlock(loopBlock);
1383
Nicolas Capens7551ac62016-01-20 17:11:53 -05001384 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001385
John Bauman89401822014-05-06 15:04:28 -04001386 loopRepDepth++;
1387 breakDepth = 0;
1388 }
1389
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001390 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001391 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001392 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001393
Nicolas Capens7551ac62016-01-20 17:11:53 -05001394 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1395 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001396
1397 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1398 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1399 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1400
1401 loopRepTestBlock[loopRepDepth] = testBlock;
1402 loopRepEndBlock[loopRepDepth] = endBlock;
1403
1404 // FIXME: jump(testBlock)
1405 Nucleus::createBr(testBlock);
1406 Nucleus::setInsertBlock(testBlock);
1407
Nicolas Capens7551ac62016-01-20 17:11:53 -05001408 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001409 Nucleus::setInsertBlock(loopBlock);
1410
Nicolas Capens7551ac62016-01-20 17:11:53 -05001411 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001412
1413 loopRepDepth++;
1414 breakDepth = 0;
1415 }
1416
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001417 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001418 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001419 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001420
1421 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1422 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1423 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001424
John Bauman19bac1e2014-05-06 15:23:49 -04001425 loopRepTestBlock[loopRepDepth] = testBlock;
1426 loopRepEndBlock[loopRepDepth] = endBlock;
1427
Nicolas Capens7551ac62016-01-20 17:11:53 -05001428 Int4 restoreBreak = enableBreak;
1429 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001430
1431 // FIXME: jump(testBlock)
1432 Nucleus::createBr(testBlock);
1433 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001434 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001435
Nicolas Capensc2534f42016-04-04 11:13:24 -04001436 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001437 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001438 condition &= enableStack[enableIndex - 1];
1439 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001440
1441 Bool notAllFalse = SignMask(condition) != 0;
1442 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001443
John Bauman19bac1e2014-05-06 15:23:49 -04001444 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001445 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001446
John Bauman19bac1e2014-05-06 15:23:49 -04001447 Nucleus::setInsertBlock(loopBlock);
1448
1449 loopRepDepth++;
1450 breakDepth = 0;
1451 }
1452
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001453 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001454 {
John Bauman19bac1e2014-05-06 15:23:49 -04001455 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001456 {
1457 returnBlock = Nucleus::createBasicBlock();
1458 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001459 }
1460 else
1461 {
John Bauman89401822014-05-06 15:04:28 -04001462 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001463
John Bauman19bac1e2014-05-06 15:23:49 -04001464 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001465 {
John Bauman19bac1e2014-05-06 15:23:49 -04001466 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001467 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001468
John Bauman66b8ab22014-05-06 15:57:45 -04001469 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001470 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1471
1472 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1473 {
1474 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1475 }
1476 }
1477 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1478 {
1479 Nucleus::createBr(callRetBlock[currentLabel][0]);
1480 }
1481 else // Function isn't called
1482 {
1483 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001484 }
1485
1486 Nucleus::setInsertBlock(unreachableBlock);
1487 Nucleus::createUnreachable();
1488 }
1489 }
1490
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001491 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001492 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001493 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001494
John Bauman19bac1e2014-05-06 15:23:49 -04001495 // FIXME: Return from function if all instances left
1496 // FIXME: Use enableLeave in other control-flow constructs
1497 }
John Bauman89401822014-05-06 15:04:28 -04001498
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001499 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001500 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001501 sampleTexture(dst, src1, src0.x, src0.y, src0.z, src0.w, Lod);
John Bauman89401822014-05-06 15:04:28 -04001502 }
John Bauman19bac1e2014-05-06 15:23:49 -04001503
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001504 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001505 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001506 Float4 lod0 = Float4(0.0f);
1507 sampleTexture(dst, src1, src0.x, src0.y, src0.z, lod0, Lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001508 }
1509
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001510 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001511 {
1512 UNIMPLEMENTED();
1513 }
1514
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001515 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001516 {
1517 UNIMPLEMENTED();
1518 }
1519
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001520 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001521 {
1522 UNIMPLEMENTED();
1523 }
1524
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001525 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001526 {
1527 UNIMPLEMENTED();
1528 }
1529
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001530 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001531 {
1532 UNIMPLEMENTED();
1533 }
1534
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001535 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001536 {
1537 UNIMPLEMENTED();
1538 }
1539
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001540 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001541 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001542 Pointer<Byte> textureMipmap = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001543 for(int i = 0; i < 4; ++i)
1544 {
1545 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1546 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1547 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1548 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1549 }
1550 }
1551
Nicolas Capensc2534f42016-04-04 11:13:24 -04001552 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q, SamplerMethod method)
John Bauman19bac1e2014-05-06 15:23:49 -04001553 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001554 Vector4f tmp;
1555
John Bauman19bac1e2014-05-06 15:23:49 -04001556 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1557 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001558 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[TEXTURE_IMAGE_UNITS]) + s.index * sizeof(Texture);
1559 sampler[s.index]->sampleTexture(texture, tmp, u, v, w, q, a0, a0, method);
John Bauman19bac1e2014-05-06 15:23:49 -04001560 }
1561 else
1562 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001563 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001564
Nicolas Capensc2534f42016-04-04 11:13:24 -04001565 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001566 {
1567 if(shader->usesSampler(i))
1568 {
1569 If(index == i)
1570 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001571 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[TEXTURE_IMAGE_UNITS]) + i * sizeof(Texture);
1572 sampler[i]->sampleTexture(texture, tmp, u, v, w, q, a0, a0, method);
John Bauman19bac1e2014-05-06 15:23:49 -04001573 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1574 }
1575 }
1576 }
1577 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001578
1579 c.x = tmp[(s.swizzle >> 0) & 0x3];
1580 c.y = tmp[(s.swizzle >> 2) & 0x3];
1581 c.z = tmp[(s.swizzle >> 4) & 0x3];
1582 c.w = tmp[(s.swizzle >> 6) & 0x3];
John Bauman19bac1e2014-05-06 15:23:49 -04001583 }
John Bauman89401822014-05-06 15:04:28 -04001584}