blob: efae39cca607dfe21ac8741cfe649f73c76b1137 [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
John Bauman19bac1e2014-05-06 15:23:49 -0400834 c.x = c.x.xxxx;
835 c.y = c.y.yyyy;
836 c.z = c.z.zzzz;
837 c.w = c.w.wwww;
838 }
839 else
840 {
841 int component = src.rel.swizzle & 0x03;
842 Float4 a;
843
844 switch(src.rel.type)
845 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500846 case Shader::PARAMETER_ADDR: a = a0[component]; break;
847 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
848 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
849 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400850 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400851 default: ASSERT(false);
852 }
853
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400854 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400855
Alexis Hetu028f41b2016-01-13 14:40:47 -0500856 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 -0500857
John Bauman19bac1e2014-05-06 15:23:49 -0400858 Int index0 = Extract(index, 0);
859 Int index1 = Extract(index, 1);
860 Int index2 = Extract(index, 2);
861 Int index3 = Extract(index, 3);
862
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400863 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
864 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
865 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
866 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400867
868 transpose4x4(c.x, c.y, c.z, c.w);
869 }
870 }
871
872 return c;
873 }
874
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400875 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400876 {
877 ASSERT(var.rel.deterministic);
878
879 if(var.rel.type == Shader::PARAMETER_TEMP)
880 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500881 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400882 }
883 else if(var.rel.type == Shader::PARAMETER_INPUT)
884 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500885 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400886 }
887 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
888 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500889 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400890 }
891 else if(var.rel.type == Shader::PARAMETER_CONST)
892 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500893 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400894 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500895 else if(var.rel.type == Shader::PARAMETER_LOOP)
896 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500897 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500898 }
John Bauman19bac1e2014-05-06 15:23:49 -0400899 else ASSERT(false);
900
901 return 0;
902 }
903
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500904 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400905 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500906 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500907
John Baumand4ae8632014-05-06 16:18:33 -0400908 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400909 {
John Baumand4ae8632014-05-06 16:18:33 -0400910 if(shader->containsBreakInstruction() && instruction->analysisBreak)
911 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500912 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400913 }
John Bauman19bac1e2014-05-06 15:23:49 -0400914
John Baumand4ae8632014-05-06 16:18:33 -0400915 if(shader->containsContinueInstruction() && instruction->analysisContinue)
916 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500917 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400918 }
John Bauman19bac1e2014-05-06 15:23:49 -0400919
John Baumand4ae8632014-05-06 16:18:33 -0400920 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
921 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500922 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400923 }
John Bauman19bac1e2014-05-06 15:23:49 -0400924 }
925
926 return enable;
927 }
928
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500929 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400930 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500931 Vector4f row0 = fetchRegisterF(src1, 0);
932 Vector4f row1 = fetchRegisterF(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400933
934 dst.x = dot3(src0, row0);
935 dst.y = dot3(src0, row1);
936 }
937
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500938 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400939 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500940 Vector4f row0 = fetchRegisterF(src1, 0);
941 Vector4f row1 = fetchRegisterF(src1, 1);
942 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400943
944 dst.x = dot3(src0, row0);
945 dst.y = dot3(src0, row1);
946 dst.z = dot3(src0, row2);
947 }
948
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500949 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400950 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500951 Vector4f row0 = fetchRegisterF(src1, 0);
952 Vector4f row1 = fetchRegisterF(src1, 1);
953 Vector4f row2 = fetchRegisterF(src1, 2);
954 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400955
956 dst.x = dot3(src0, row0);
957 dst.y = dot3(src0, row1);
958 dst.z = dot3(src0, row2);
959 dst.w = dot3(src0, row3);
960 }
961
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500962 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400963 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500964 Vector4f row0 = fetchRegisterF(src1, 0);
965 Vector4f row1 = fetchRegisterF(src1, 1);
966 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400967
968 dst.x = dot4(src0, row0);
969 dst.y = dot4(src0, row1);
970 dst.z = dot4(src0, row2);
971 }
972
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500973 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400974 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500975 Vector4f row0 = fetchRegisterF(src1, 0);
976 Vector4f row1 = fetchRegisterF(src1, 1);
977 Vector4f row2 = fetchRegisterF(src1, 2);
978 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400979
980 dst.x = dot4(src0, row0);
981 dst.y = dot4(src0, row1);
982 dst.z = dot4(src0, row2);
983 dst.w = dot4(src0, row3);
984 }
985
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500986 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -0400987 {
988 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
989 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
990
991 if(breakDepth == 0)
992 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500993 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400994 Nucleus::createBr(endBlock);
995 }
996 else
997 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500998 enableBreak = enableBreak & ~enableStack[enableIndex];
999 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001000
Nicolas Capens7551ac62016-01-20 17:11:53 -05001001 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001002 branch(allBreak, endBlock, deadBlock);
1003 }
1004
1005 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001006 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001007 }
1008
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001009 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001010 {
1011 Int4 condition;
1012
1013 switch(control)
1014 {
John Bauman19bac1e2014-05-06 15:23:49 -04001015 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1016 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1017 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1018 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1019 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1020 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001021 default:
1022 ASSERT(false);
1023 }
1024
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001025 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001026 }
1027
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001028 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001029 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001030 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001031
John Bauman19bac1e2014-05-06 15:23:49 -04001032 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001033 {
1034 condition = ~condition;
1035 }
1036
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001037 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001038 }
1039
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001040 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001041 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001042 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001043
1044 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1045 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1046
Nicolas Capens7551ac62016-01-20 17:11:53 -05001047 enableBreak = enableBreak & ~condition;
1048 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001049
Nicolas Capens7551ac62016-01-20 17:11:53 -05001050 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001051 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001052
John Bauman89401822014-05-06 15:04:28 -04001053 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001054 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001055 }
1056
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001057 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001058 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001059 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001060 }
1061
1062 void VertexProgram::TEST()
1063 {
1064 whileTest = true;
1065 }
1066
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001067 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001068 {
1069 if(!labelBlock[labelIndex])
1070 {
1071 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1072 }
1073
John Bauman19bac1e2014-05-06 15:23:49 -04001074 if(callRetBlock[labelIndex].size() > 1)
1075 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001076 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001077 }
John Bauman89401822014-05-06 15:04:28 -04001078
Nicolas Capens7551ac62016-01-20 17:11:53 -05001079 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001080
1081 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001082 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1083
Nicolas Capens7551ac62016-01-20 17:11:53 -05001084 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001085 }
1086
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001087 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001088 {
John Bauman19bac1e2014-05-06 15:23:49 -04001089 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001090 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001091 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001092 }
John Bauman19bac1e2014-05-06 15:23:49 -04001093 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001094 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001095 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001096 }
1097 else ASSERT(false);
1098 }
1099
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001100 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001101 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001102 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001103
John Bauman19bac1e2014-05-06 15:23:49 -04001104 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001105 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001106 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001107 }
1108
1109 if(!labelBlock[labelIndex])
1110 {
1111 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1112 }
1113
John Bauman19bac1e2014-05-06 15:23:49 -04001114 if(callRetBlock[labelIndex].size() > 1)
1115 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001116 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001117 }
John Bauman89401822014-05-06 15:04:28 -04001118
Nicolas Capens7551ac62016-01-20 17:11:53 -05001119 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001120
John Bauman19bac1e2014-05-06 15:23:49 -04001121 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1122 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1123
Nicolas Capens7551ac62016-01-20 17:11:53 -05001124 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001125 }
1126
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001127 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001128 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001129 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001130
John Bauman19bac1e2014-05-06 15:23:49 -04001131 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001132 {
1133 condition = ~condition;
1134 }
1135
Nicolas Capens7551ac62016-01-20 17:11:53 -05001136 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001137
1138 if(!labelBlock[labelIndex])
1139 {
1140 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1141 }
1142
John Bauman19bac1e2014-05-06 15:23:49 -04001143 if(callRetBlock[labelIndex].size() > 1)
1144 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001145 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001146 }
John Bauman89401822014-05-06 15:04:28 -04001147
Nicolas Capens7551ac62016-01-20 17:11:53 -05001148 enableIndex++;
1149 enableStack[enableIndex] = condition;
1150 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001151
John Bauman19bac1e2014-05-06 15:23:49 -04001152 Bool notAllFalse = SignMask(condition) != 0;
1153 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1154 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001155
Nicolas Capens7551ac62016-01-20 17:11:53 -05001156 enableIndex--;
1157 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001158 }
1159
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001160 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001161 {
1162 ifDepth--;
1163
1164 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1165 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1166
1167 if(isConditionalIf[ifDepth])
1168 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001169 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001170 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001171
1172 branch(notAllFalse, falseBlock, endBlock);
1173
Nicolas Capens7551ac62016-01-20 17:11:53 -05001174 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001175 }
1176 else
1177 {
1178 Nucleus::createBr(endBlock);
1179 Nucleus::setInsertBlock(falseBlock);
1180 }
1181
1182 ifFalseBlock[ifDepth] = endBlock;
1183
1184 ifDepth++;
1185 }
1186
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001187 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001188 {
1189 ifDepth--;
1190
1191 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1192
1193 Nucleus::createBr(endBlock);
1194 Nucleus::setInsertBlock(endBlock);
1195
1196 if(isConditionalIf[ifDepth])
1197 {
1198 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001199 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001200 }
1201 }
1202
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001203 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001204 {
1205 loopRepDepth--;
1206
Nicolas Capens7551ac62016-01-20 17:11:53 -05001207 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001208
1209 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1210 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1211
1212 Nucleus::createBr(testBlock);
1213 Nucleus::setInsertBlock(endBlock);
1214
Nicolas Capens7551ac62016-01-20 17:11:53 -05001215 loopDepth--;
1216 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001217 }
1218
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001219 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001220 {
1221 loopRepDepth--;
1222
1223 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1224 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1225
1226 Nucleus::createBr(testBlock);
1227 Nucleus::setInsertBlock(endBlock);
1228
Nicolas Capens7551ac62016-01-20 17:11:53 -05001229 loopDepth--;
1230 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001231 }
1232
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001233 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001234 {
1235 loopRepDepth--;
1236
1237 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1238 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1239
1240 Nucleus::createBr(testBlock);
1241 Nucleus::setInsertBlock(endBlock);
1242
Nicolas Capens7551ac62016-01-20 17:11:53 -05001243 enableIndex--;
1244 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001245 whileTest = false;
1246 }
1247
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001248 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001249 {
John Bauman19bac1e2014-05-06 15:23:49 -04001250 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001251 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001252 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001253 }
John Bauman19bac1e2014-05-06 15:23:49 -04001254 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001255 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001256 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001257 }
John Bauman19bac1e2014-05-06 15:23:49 -04001258 else
1259 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001260 Int4 condition = As<Int4>(fetchRegisterF(src).x);
1261 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001262 }
John Bauman89401822014-05-06 15:04:28 -04001263 }
1264
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001265 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001266 {
1267 ASSERT(ifDepth < 24 + 4);
1268
Nicolas Capens7551ac62016-01-20 17:11:53 -05001269 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001270
John Bauman19bac1e2014-05-06 15:23:49 -04001271 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001272 {
John Bauman19bac1e2014-05-06 15:23:49 -04001273 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001274 }
1275
1276 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1277 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1278
1279 branch(condition, trueBlock, falseBlock);
1280
1281 isConditionalIf[ifDepth] = false;
1282 ifFalseBlock[ifDepth] = falseBlock;
1283
1284 ifDepth++;
1285 }
1286
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001287 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001288 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001289 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001290
John Bauman19bac1e2014-05-06 15:23:49 -04001291 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001292 {
1293 condition = ~condition;
1294 }
1295
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001296 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001297 }
1298
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001299 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001300 {
1301 Int4 condition;
1302
1303 switch(control)
1304 {
John Bauman19bac1e2014-05-06 15:23:49 -04001305 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1306 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1307 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1308 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1309 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1310 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001311 default:
1312 ASSERT(false);
1313 }
1314
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001315 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001316 }
1317
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001318 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001319 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001320 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001321
Nicolas Capens7551ac62016-01-20 17:11:53 -05001322 enableIndex++;
1323 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001324
1325 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1326 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1327
John Bauman19bac1e2014-05-06 15:23:49 -04001328 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001329
1330 branch(notAllFalse, trueBlock, falseBlock);
1331
1332 isConditionalIf[ifDepth] = true;
1333 ifFalseBlock[ifDepth] = falseBlock;
1334
1335 ifDepth++;
1336 breakDepth++;
1337 }
1338
1339 void VertexProgram::LABEL(int labelIndex)
1340 {
John Bauman19bac1e2014-05-06 15:23:49 -04001341 if(!labelBlock[labelIndex])
1342 {
1343 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1344 }
1345
John Bauman89401822014-05-06 15:04:28 -04001346 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001347 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001348 }
1349
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001350 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001351 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001352 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001353
Nicolas Capens7551ac62016-01-20 17:11:53 -05001354 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1355 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1356 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001357
1358 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001359 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001360 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001361 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001362 }
1363
1364 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1365 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1366 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1367
1368 loopRepTestBlock[loopRepDepth] = testBlock;
1369 loopRepEndBlock[loopRepDepth] = endBlock;
1370
1371 // FIXME: jump(testBlock)
1372 Nucleus::createBr(testBlock);
1373 Nucleus::setInsertBlock(testBlock);
1374
Nicolas Capens7551ac62016-01-20 17:11:53 -05001375 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001376 Nucleus::setInsertBlock(loopBlock);
1377
Nicolas Capens7551ac62016-01-20 17:11:53 -05001378 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001379
John Bauman89401822014-05-06 15:04:28 -04001380 loopRepDepth++;
1381 breakDepth = 0;
1382 }
1383
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001384 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001385 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001386 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001387
Nicolas Capens7551ac62016-01-20 17:11:53 -05001388 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1389 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001390
1391 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1392 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1393 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1394
1395 loopRepTestBlock[loopRepDepth] = testBlock;
1396 loopRepEndBlock[loopRepDepth] = endBlock;
1397
1398 // FIXME: jump(testBlock)
1399 Nucleus::createBr(testBlock);
1400 Nucleus::setInsertBlock(testBlock);
1401
Nicolas Capens7551ac62016-01-20 17:11:53 -05001402 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001403 Nucleus::setInsertBlock(loopBlock);
1404
Nicolas Capens7551ac62016-01-20 17:11:53 -05001405 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001406
1407 loopRepDepth++;
1408 breakDepth = 0;
1409 }
1410
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001411 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001412 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001413 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001414
1415 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1416 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1417 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001418
John Bauman19bac1e2014-05-06 15:23:49 -04001419 loopRepTestBlock[loopRepDepth] = testBlock;
1420 loopRepEndBlock[loopRepDepth] = endBlock;
1421
Nicolas Capens7551ac62016-01-20 17:11:53 -05001422 Int4 restoreBreak = enableBreak;
1423 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001424
1425 // FIXME: jump(testBlock)
1426 Nucleus::createBr(testBlock);
1427 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001428 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001429
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001430 const Vector4f &src = fetchRegisterF(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001431 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001432 condition &= enableStack[enableIndex - 1];
1433 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001434
1435 Bool notAllFalse = SignMask(condition) != 0;
1436 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001437
John Bauman19bac1e2014-05-06 15:23:49 -04001438 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001439 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001440
John Bauman19bac1e2014-05-06 15:23:49 -04001441 Nucleus::setInsertBlock(loopBlock);
1442
1443 loopRepDepth++;
1444 breakDepth = 0;
1445 }
1446
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001447 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001448 {
John Bauman19bac1e2014-05-06 15:23:49 -04001449 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001450 {
1451 returnBlock = Nucleus::createBasicBlock();
1452 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001453 }
1454 else
1455 {
John Bauman89401822014-05-06 15:04:28 -04001456 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001457
John Bauman19bac1e2014-05-06 15:23:49 -04001458 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001459 {
John Bauman19bac1e2014-05-06 15:23:49 -04001460 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001461 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001462
John Bauman66b8ab22014-05-06 15:57:45 -04001463 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001464 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1465
1466 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1467 {
1468 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1469 }
1470 }
1471 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1472 {
1473 Nucleus::createBr(callRetBlock[currentLabel][0]);
1474 }
1475 else // Function isn't called
1476 {
1477 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001478 }
1479
1480 Nucleus::setInsertBlock(unreachableBlock);
1481 Nucleus::createUnreachable();
1482 }
1483 }
1484
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001485 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001486 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001487 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001488
John Bauman19bac1e2014-05-06 15:23:49 -04001489 // FIXME: Return from function if all instances left
1490 // FIXME: Use enableLeave in other control-flow constructs
1491 }
John Bauman89401822014-05-06 15:04:28 -04001492
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001493 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001494 {
1495 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001496 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001497
1498 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1499 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1500 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1501 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1502 }
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 {
1506 Float4 lod = Float4(0.0f);
1507 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001508 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001509
1510 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1511 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1512 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1513 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1514 }
1515
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001516 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001517 {
1518 UNIMPLEMENTED();
1519 }
1520
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001521 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001522 {
1523 UNIMPLEMENTED();
1524 }
1525
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001526 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001527 {
1528 UNIMPLEMENTED();
1529 }
1530
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001531 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001532 {
1533 UNIMPLEMENTED();
1534 }
1535
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001536 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001537 {
1538 UNIMPLEMENTED();
1539 }
1540
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001541 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001542 {
1543 UNIMPLEMENTED();
1544 }
1545
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001546 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001547 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001548 Pointer<Byte> textureMipmap = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001549 for(int i = 0; i < 4; ++i)
1550 {
1551 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1552 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1553 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1554 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1555 }
1556 }
1557
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001558 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
John Bauman19bac1e2014-05-06 15:23:49 -04001559 {
1560 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1561 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001562 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1563 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, a0, a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001564 }
1565 else
1566 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001567 Int index = As<Int>(Float(fetchRegisterF(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001568
1569 for(int i = 0; i < 16; i++)
1570 {
1571 if(shader->usesSampler(i))
1572 {
1573 If(index == i)
1574 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001575 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1576 sampler[i]->sampleTexture(texture, c, u, v, w, q, a0, a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001577 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1578 }
1579 }
1580 }
1581 }
1582 }
John Bauman89401822014-05-06 15:04:28 -04001583}