blob: 1999efe0fff50906cf31faab7228c7ef1f0b0bd9 [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 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500662 reg = r[i + relativeAddress(src)];
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 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500675 reg = v[i + relativeAddress(src)];
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 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500707 reg = o[i + relativeAddress(src)];
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
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500763 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400764 {
John Bauman19bac1e2014-05-06 15:23:49 -0400765 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500766 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400767
768 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
769 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500770 c.x = c.y = c.z = c.w = *Pointer<Float4>(data + OFFSET(DrawData,vs.c[i]));
John Bauman19bac1e2014-05-06 15:23:49 -0400771
772 c.x = c.x.xxxx;
773 c.y = c.y.yyyy;
774 c.z = c.z.zzzz;
775 c.w = c.w.wwww;
776
Nicolas Capenseafdb222015-05-15 15:24:08 -0400777 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400778 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500779 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400780 {
781 const Shader::Instruction &instruction = *shader->getInstruction(j);
782
783 if(instruction.opcode == Shader::OPCODE_DEF)
784 {
785 if(instruction.dst.index == i)
786 {
787 c.x = Float4(instruction.src[0].value[0]);
788 c.y = Float4(instruction.src[0].value[1]);
789 c.z = Float4(instruction.src[0].value[2]);
790 c.w = Float4(instruction.src[0].value[3]);
791
792 break;
793 }
794 }
795 }
796 }
797 }
798 else if(src.rel.type == Shader::PARAMETER_LOOP)
799 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500800 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400801
Nicolas Capens7551ac62016-01-20 17:11:53 -0500802 c.x = c.y = c.z = c.w = *Pointer<Float4>(data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400803
804 c.x = c.x.xxxx;
805 c.y = c.y.yyyy;
806 c.z = c.z.zzzz;
807 c.w = c.w.wwww;
808 }
809 else
810 {
811 if(src.rel.deterministic)
812 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500813 Int a = relativeAddress(src);
814
Nicolas Capens7551ac62016-01-20 17:11:53 -0500815 c.x = c.y = c.z = c.w = *Pointer<Float4>(data + OFFSET(DrawData,vs.c[i]) + a * 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400816
817 c.x = c.x.xxxx;
818 c.y = c.y.yyyy;
819 c.z = c.z.zzzz;
820 c.w = c.w.wwww;
821 }
822 else
823 {
824 int component = src.rel.swizzle & 0x03;
825 Float4 a;
826
827 switch(src.rel.type)
828 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500829 case Shader::PARAMETER_ADDR: a = a0[component]; break;
830 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
831 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
832 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
833 case Shader::PARAMETER_CONST: a = *Pointer<Float>(data + OFFSET(DrawData,vs.c[src.rel.index][component])); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400834 default: ASSERT(false);
835 }
836
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400837 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400838
Alexis Hetu028f41b2016-01-13 14:40:47 -0500839 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 -0500840
John Bauman19bac1e2014-05-06 15:23:49 -0400841 Int index0 = Extract(index, 0);
842 Int index1 = Extract(index, 1);
843 Int index2 = Extract(index, 2);
844 Int index3 = Extract(index, 3);
845
Nicolas Capens7551ac62016-01-20 17:11:53 -0500846 c.x = *Pointer<Float4>(data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
847 c.y = *Pointer<Float4>(data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
848 c.z = *Pointer<Float4>(data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
849 c.w = *Pointer<Float4>(data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400850
851 transpose4x4(c.x, c.y, c.z, c.w);
852 }
853 }
854
855 return c;
856 }
857
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500858 Int VertexProgram::relativeAddress(const Shader::Parameter &var)
John Bauman19bac1e2014-05-06 15:23:49 -0400859 {
860 ASSERT(var.rel.deterministic);
861
862 if(var.rel.type == Shader::PARAMETER_TEMP)
863 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500864 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400865 }
866 else if(var.rel.type == Shader::PARAMETER_INPUT)
867 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500868 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400869 }
870 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
871 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500872 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400873 }
874 else if(var.rel.type == Shader::PARAMETER_CONST)
875 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500876 RValue<Int4> c = *Pointer<Int4>(data + OFFSET(DrawData, vs.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -0400877
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400878 return Extract(c, 0) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400879 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500880 else if(var.rel.type == Shader::PARAMETER_LOOP)
881 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500882 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500883 }
John Bauman19bac1e2014-05-06 15:23:49 -0400884 else ASSERT(false);
885
886 return 0;
887 }
888
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500889 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400890 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500891 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500892
John Baumand4ae8632014-05-06 16:18:33 -0400893 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400894 {
John Baumand4ae8632014-05-06 16:18:33 -0400895 if(shader->containsBreakInstruction() && instruction->analysisBreak)
896 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500897 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400898 }
John Bauman19bac1e2014-05-06 15:23:49 -0400899
John Baumand4ae8632014-05-06 16:18:33 -0400900 if(shader->containsContinueInstruction() && instruction->analysisContinue)
901 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500902 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400903 }
John Bauman19bac1e2014-05-06 15:23:49 -0400904
John Baumand4ae8632014-05-06 16:18:33 -0400905 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
906 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500907 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400908 }
John Bauman19bac1e2014-05-06 15:23:49 -0400909 }
910
911 return enable;
912 }
913
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500914 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400915 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500916 Vector4f row0 = fetchRegisterF(src1, 0);
917 Vector4f row1 = fetchRegisterF(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400918
919 dst.x = dot3(src0, row0);
920 dst.y = dot3(src0, row1);
921 }
922
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500923 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400924 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500925 Vector4f row0 = fetchRegisterF(src1, 0);
926 Vector4f row1 = fetchRegisterF(src1, 1);
927 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400928
929 dst.x = dot3(src0, row0);
930 dst.y = dot3(src0, row1);
931 dst.z = dot3(src0, row2);
932 }
933
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500934 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400935 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500936 Vector4f row0 = fetchRegisterF(src1, 0);
937 Vector4f row1 = fetchRegisterF(src1, 1);
938 Vector4f row2 = fetchRegisterF(src1, 2);
939 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400940
941 dst.x = dot3(src0, row0);
942 dst.y = dot3(src0, row1);
943 dst.z = dot3(src0, row2);
944 dst.w = dot3(src0, row3);
945 }
946
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500947 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400948 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500949 Vector4f row0 = fetchRegisterF(src1, 0);
950 Vector4f row1 = fetchRegisterF(src1, 1);
951 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400952
953 dst.x = dot4(src0, row0);
954 dst.y = dot4(src0, row1);
955 dst.z = dot4(src0, row2);
956 }
957
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500958 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400959 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500960 Vector4f row0 = fetchRegisterF(src1, 0);
961 Vector4f row1 = fetchRegisterF(src1, 1);
962 Vector4f row2 = fetchRegisterF(src1, 2);
963 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400964
965 dst.x = dot4(src0, row0);
966 dst.y = dot4(src0, row1);
967 dst.z = dot4(src0, row2);
968 dst.w = dot4(src0, row3);
969 }
970
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500971 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -0400972 {
973 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
974 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
975
976 if(breakDepth == 0)
977 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500978 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400979 Nucleus::createBr(endBlock);
980 }
981 else
982 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500983 enableBreak = enableBreak & ~enableStack[enableIndex];
984 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -0400985
Nicolas Capens7551ac62016-01-20 17:11:53 -0500986 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400987 branch(allBreak, endBlock, deadBlock);
988 }
989
990 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -0500991 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400992 }
993
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500994 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400995 {
996 Int4 condition;
997
998 switch(control)
999 {
John Bauman19bac1e2014-05-06 15:23:49 -04001000 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1001 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1002 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1003 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1004 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1005 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001006 default:
1007 ASSERT(false);
1008 }
1009
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001010 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001011 }
1012
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001013 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001014 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001015 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001016
John Bauman19bac1e2014-05-06 15:23:49 -04001017 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001018 {
1019 condition = ~condition;
1020 }
1021
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001022 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001023 }
1024
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001025 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001026 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001027 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001028
1029 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1030 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1031
Nicolas Capens7551ac62016-01-20 17:11:53 -05001032 enableBreak = enableBreak & ~condition;
1033 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001034
Nicolas Capens7551ac62016-01-20 17:11:53 -05001035 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001036 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001037
John Bauman89401822014-05-06 15:04:28 -04001038 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001039 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001040 }
1041
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001042 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001043 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001044 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001045 }
1046
1047 void VertexProgram::TEST()
1048 {
1049 whileTest = true;
1050 }
1051
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001052 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001053 {
1054 if(!labelBlock[labelIndex])
1055 {
1056 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1057 }
1058
John Bauman19bac1e2014-05-06 15:23:49 -04001059 if(callRetBlock[labelIndex].size() > 1)
1060 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001061 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001062 }
John Bauman89401822014-05-06 15:04:28 -04001063
Nicolas Capens7551ac62016-01-20 17:11:53 -05001064 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001065
1066 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001067 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1068
Nicolas Capens7551ac62016-01-20 17:11:53 -05001069 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001070 }
1071
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001072 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001073 {
John Bauman19bac1e2014-05-06 15:23:49 -04001074 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001075 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001076 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001077 }
John Bauman19bac1e2014-05-06 15:23:49 -04001078 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001079 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001080 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001081 }
1082 else ASSERT(false);
1083 }
1084
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001085 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001086 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001087 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001088
John Bauman19bac1e2014-05-06 15:23:49 -04001089 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001090 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001091 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001092 }
1093
1094 if(!labelBlock[labelIndex])
1095 {
1096 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1097 }
1098
John Bauman19bac1e2014-05-06 15:23:49 -04001099 if(callRetBlock[labelIndex].size() > 1)
1100 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001101 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001102 }
John Bauman89401822014-05-06 15:04:28 -04001103
Nicolas Capens7551ac62016-01-20 17:11:53 -05001104 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001105
John Bauman19bac1e2014-05-06 15:23:49 -04001106 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1107 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1108
Nicolas Capens7551ac62016-01-20 17:11:53 -05001109 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001110 }
1111
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001112 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001113 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001114 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001115
John Bauman19bac1e2014-05-06 15:23:49 -04001116 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001117 {
1118 condition = ~condition;
1119 }
1120
Nicolas Capens7551ac62016-01-20 17:11:53 -05001121 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001122
1123 if(!labelBlock[labelIndex])
1124 {
1125 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1126 }
1127
John Bauman19bac1e2014-05-06 15:23:49 -04001128 if(callRetBlock[labelIndex].size() > 1)
1129 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001130 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001131 }
John Bauman89401822014-05-06 15:04:28 -04001132
Nicolas Capens7551ac62016-01-20 17:11:53 -05001133 enableIndex++;
1134 enableStack[enableIndex] = condition;
1135 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001136
John Bauman19bac1e2014-05-06 15:23:49 -04001137 Bool notAllFalse = SignMask(condition) != 0;
1138 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1139 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001140
Nicolas Capens7551ac62016-01-20 17:11:53 -05001141 enableIndex--;
1142 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001143 }
1144
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001145 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001146 {
1147 ifDepth--;
1148
1149 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1150 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1151
1152 if(isConditionalIf[ifDepth])
1153 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001154 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001155 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001156
1157 branch(notAllFalse, falseBlock, endBlock);
1158
Nicolas Capens7551ac62016-01-20 17:11:53 -05001159 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001160 }
1161 else
1162 {
1163 Nucleus::createBr(endBlock);
1164 Nucleus::setInsertBlock(falseBlock);
1165 }
1166
1167 ifFalseBlock[ifDepth] = endBlock;
1168
1169 ifDepth++;
1170 }
1171
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001172 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001173 {
1174 ifDepth--;
1175
1176 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1177
1178 Nucleus::createBr(endBlock);
1179 Nucleus::setInsertBlock(endBlock);
1180
1181 if(isConditionalIf[ifDepth])
1182 {
1183 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001184 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001185 }
1186 }
1187
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001188 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001189 {
1190 loopRepDepth--;
1191
Nicolas Capens7551ac62016-01-20 17:11:53 -05001192 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001193
1194 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1195 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1196
1197 Nucleus::createBr(testBlock);
1198 Nucleus::setInsertBlock(endBlock);
1199
Nicolas Capens7551ac62016-01-20 17:11:53 -05001200 loopDepth--;
1201 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001202 }
1203
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001204 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001205 {
1206 loopRepDepth--;
1207
1208 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1209 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1210
1211 Nucleus::createBr(testBlock);
1212 Nucleus::setInsertBlock(endBlock);
1213
Nicolas Capens7551ac62016-01-20 17:11:53 -05001214 loopDepth--;
1215 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001216 }
1217
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001218 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001219 {
1220 loopRepDepth--;
1221
1222 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1223 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1224
1225 Nucleus::createBr(testBlock);
1226 Nucleus::setInsertBlock(endBlock);
1227
Nicolas Capens7551ac62016-01-20 17:11:53 -05001228 enableIndex--;
1229 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001230 whileTest = false;
1231 }
1232
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001233 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001234 {
John Bauman19bac1e2014-05-06 15:23:49 -04001235 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001236 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001237 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001238 }
John Bauman19bac1e2014-05-06 15:23:49 -04001239 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001240 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001241 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001242 }
John Bauman19bac1e2014-05-06 15:23:49 -04001243 else
1244 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001245 Int4 condition = As<Int4>(fetchRegisterF(src).x);
1246 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001247 }
John Bauman89401822014-05-06 15:04:28 -04001248 }
1249
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001250 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001251 {
1252 ASSERT(ifDepth < 24 + 4);
1253
Nicolas Capens7551ac62016-01-20 17:11:53 -05001254 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001255
John Bauman19bac1e2014-05-06 15:23:49 -04001256 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001257 {
John Bauman19bac1e2014-05-06 15:23:49 -04001258 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001259 }
1260
1261 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1262 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1263
1264 branch(condition, trueBlock, falseBlock);
1265
1266 isConditionalIf[ifDepth] = false;
1267 ifFalseBlock[ifDepth] = falseBlock;
1268
1269 ifDepth++;
1270 }
1271
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001272 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001273 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001274 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001275
John Bauman19bac1e2014-05-06 15:23:49 -04001276 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001277 {
1278 condition = ~condition;
1279 }
1280
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001281 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001282 }
1283
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001284 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001285 {
1286 Int4 condition;
1287
1288 switch(control)
1289 {
John Bauman19bac1e2014-05-06 15:23:49 -04001290 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1291 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1292 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1293 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1294 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1295 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001296 default:
1297 ASSERT(false);
1298 }
1299
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001300 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001301 }
1302
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001303 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001304 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001305 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001306
Nicolas Capens7551ac62016-01-20 17:11:53 -05001307 enableIndex++;
1308 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001309
1310 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1311 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1312
John Bauman19bac1e2014-05-06 15:23:49 -04001313 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001314
1315 branch(notAllFalse, trueBlock, falseBlock);
1316
1317 isConditionalIf[ifDepth] = true;
1318 ifFalseBlock[ifDepth] = falseBlock;
1319
1320 ifDepth++;
1321 breakDepth++;
1322 }
1323
1324 void VertexProgram::LABEL(int labelIndex)
1325 {
John Bauman19bac1e2014-05-06 15:23:49 -04001326 if(!labelBlock[labelIndex])
1327 {
1328 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1329 }
1330
John Bauman89401822014-05-06 15:04:28 -04001331 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001332 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001333 }
1334
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001335 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001336 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001337 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001338
Nicolas Capens7551ac62016-01-20 17:11:53 -05001339 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1340 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1341 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001342
1343 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001344 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001345 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001346 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001347 }
1348
1349 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1350 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1351 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1352
1353 loopRepTestBlock[loopRepDepth] = testBlock;
1354 loopRepEndBlock[loopRepDepth] = endBlock;
1355
1356 // FIXME: jump(testBlock)
1357 Nucleus::createBr(testBlock);
1358 Nucleus::setInsertBlock(testBlock);
1359
Nicolas Capens7551ac62016-01-20 17:11:53 -05001360 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001361 Nucleus::setInsertBlock(loopBlock);
1362
Nicolas Capens7551ac62016-01-20 17:11:53 -05001363 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001364
John Bauman89401822014-05-06 15:04:28 -04001365 loopRepDepth++;
1366 breakDepth = 0;
1367 }
1368
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001369 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001370 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001371 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001372
Nicolas Capens7551ac62016-01-20 17:11:53 -05001373 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1374 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001375
1376 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1377 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1378 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1379
1380 loopRepTestBlock[loopRepDepth] = testBlock;
1381 loopRepEndBlock[loopRepDepth] = endBlock;
1382
1383 // FIXME: jump(testBlock)
1384 Nucleus::createBr(testBlock);
1385 Nucleus::setInsertBlock(testBlock);
1386
Nicolas Capens7551ac62016-01-20 17:11:53 -05001387 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001388 Nucleus::setInsertBlock(loopBlock);
1389
Nicolas Capens7551ac62016-01-20 17:11:53 -05001390 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001391
1392 loopRepDepth++;
1393 breakDepth = 0;
1394 }
1395
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001396 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001397 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001398 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001399
1400 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1401 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1402 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001403
John Bauman19bac1e2014-05-06 15:23:49 -04001404 loopRepTestBlock[loopRepDepth] = testBlock;
1405 loopRepEndBlock[loopRepDepth] = endBlock;
1406
Nicolas Capens7551ac62016-01-20 17:11:53 -05001407 Int4 restoreBreak = enableBreak;
1408 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001409
1410 // FIXME: jump(testBlock)
1411 Nucleus::createBr(testBlock);
1412 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001413 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001414
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001415 const Vector4f &src = fetchRegisterF(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001416 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001417 condition &= enableStack[enableIndex - 1];
1418 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001419
1420 Bool notAllFalse = SignMask(condition) != 0;
1421 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001422
John Bauman19bac1e2014-05-06 15:23:49 -04001423 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001424 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001425
John Bauman19bac1e2014-05-06 15:23:49 -04001426 Nucleus::setInsertBlock(loopBlock);
1427
1428 loopRepDepth++;
1429 breakDepth = 0;
1430 }
1431
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001432 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001433 {
John Bauman19bac1e2014-05-06 15:23:49 -04001434 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001435 {
1436 returnBlock = Nucleus::createBasicBlock();
1437 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001438 }
1439 else
1440 {
John Bauman89401822014-05-06 15:04:28 -04001441 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001442
John Bauman19bac1e2014-05-06 15:23:49 -04001443 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001444 {
John Bauman19bac1e2014-05-06 15:23:49 -04001445 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001446 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001447
John Bauman66b8ab22014-05-06 15:57:45 -04001448 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001449 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1450
1451 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1452 {
1453 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1454 }
1455 }
1456 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1457 {
1458 Nucleus::createBr(callRetBlock[currentLabel][0]);
1459 }
1460 else // Function isn't called
1461 {
1462 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001463 }
1464
1465 Nucleus::setInsertBlock(unreachableBlock);
1466 Nucleus::createUnreachable();
1467 }
1468 }
1469
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001470 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001471 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001472 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001473
John Bauman19bac1e2014-05-06 15:23:49 -04001474 // FIXME: Return from function if all instances left
1475 // FIXME: Use enableLeave in other control-flow constructs
1476 }
John Bauman89401822014-05-06 15:04:28 -04001477
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001478 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001479 {
1480 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001481 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001482
1483 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1484 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1485 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1486 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1487 }
John Bauman19bac1e2014-05-06 15:23:49 -04001488
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001489 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001490 {
1491 Float4 lod = Float4(0.0f);
1492 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001493 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001494
1495 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1496 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1497 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1498 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1499 }
1500
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001501 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001502 {
1503 UNIMPLEMENTED();
1504 }
1505
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001506 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001507 {
1508 UNIMPLEMENTED();
1509 }
1510
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001511 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001512 {
1513 UNIMPLEMENTED();
1514 }
1515
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001516 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001517 {
1518 UNIMPLEMENTED();
1519 }
1520
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001521 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001522 {
1523 UNIMPLEMENTED();
1524 }
1525
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001526 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001527 {
1528 UNIMPLEMENTED();
1529 }
1530
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001531 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001532 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001533 Pointer<Byte> textureMipmap = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001534 for(int i = 0; i < 4; ++i)
1535 {
1536 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1537 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1538 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1539 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1540 }
1541 }
1542
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001543 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
John Bauman19bac1e2014-05-06 15:23:49 -04001544 {
1545 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1546 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001547 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1548 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, a0, a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001549 }
1550 else
1551 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001552 Int index = As<Int>(Float(fetchRegisterF(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001553
1554 for(int i = 0; i < 16; i++)
1555 {
1556 if(shader->usesSampler(i))
1557 {
1558 If(index == i)
1559 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001560 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1561 sampler[i]->sampleTexture(texture, c, u, v, w, q, a0, a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001562 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1563 }
1564 }
1565 }
1566 }
1567 }
John Bauman89401822014-05-06 15:04:28 -04001568}