blob: a19cca60421fd88b887ed0b471ddd75522fa4ba5 [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{
John Bauman19bac1e2014-05-06 15:23:49 -040023 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader) : VertexRoutine(state, shader)
John Bauman89401822014-05-06 15:04:28 -040024 {
John Bauman89401822014-05-06 15:04:28 -040025 ifDepth = 0;
26 loopRepDepth = 0;
27 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040028 currentLabel = -1;
29 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040030
31 for(int i = 0; i < 2048; i++)
32 {
33 labelBlock[i] = 0;
34 }
35 }
36
37 VertexProgram::~VertexProgram()
38 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040039 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040040 {
41 delete sampler[i];
42 }
43 }
44
Nicolas Capensb4fb3672016-01-15 17:02:41 -050045 void VertexProgram::pipeline()
John Bauman89401822014-05-06 15:04:28 -040046 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040047 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040048 {
49 sampler[i] = new SamplerCore(r.constants, state.samplerState[i]);
50 }
51
52 if(!state.preTransformed)
53 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050054 program();
John Bauman89401822014-05-06 15:04:28 -040055 }
56 else
57 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050058 passThrough();
John Bauman89401822014-05-06 15:04:28 -040059 }
60 }
61
Nicolas Capensb4fb3672016-01-15 17:02:41 -050062 void VertexProgram::program()
John Bauman89401822014-05-06 15:04:28 -040063 {
John Bauman19bac1e2014-05-06 15:23:49 -040064 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040065
John Bauman19bac1e2014-05-06 15:23:49 -040066 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040067
68 r.enableIndex = 0;
69 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040070
Nicolas Capens4677a5f2014-05-06 16:42:26 -040071 if(shader->containsLeaveInstruction())
72 {
73 r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
74 }
75
John Bauman19bac1e2014-05-06 15:23:49 -040076 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050077 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -040078 {
John Bauman19bac1e2014-05-06 15:23:49 -040079 const Shader::Instruction *instruction = shader->getInstruction(i);
80 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -040081
John Bauman19bac1e2014-05-06 15:23:49 -040082 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
83 {
84 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -040085
John Bauman19bac1e2014-05-06 15:23:49 -040086 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
87 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
88 }
89 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -050090
Alexis Hetu903e0252014-11-25 14:25:32 -050091 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -040092 {
93 const Shader::Instruction *instruction = shader->getInstruction(i);
94 Shader::Opcode opcode = instruction->opcode;
95
96 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -040097 {
98 continue;
99 }
100
John Bauman19bac1e2014-05-06 15:23:49 -0400101 Dst dst = instruction->dst;
102 Src src0 = instruction->src[0];
103 Src src1 = instruction->src[1];
104 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400105 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400106 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400107
John Bauman19bac1e2014-05-06 15:23:49 -0400108 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400109 Control control = instruction->control;
110 bool integer = dst.type == Shader::PARAMETER_ADDR;
111 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400112
John Bauman19bac1e2014-05-06 15:23:49 -0400113 Vector4f d;
114 Vector4f s0;
115 Vector4f s1;
116 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400117 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400118 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400119
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500120 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(src0);
121 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(src1);
122 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(src2);
123 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegisterF(src3);
124 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegisterF(src4);
John Bauman89401822014-05-06 15:04:28 -0400125
126 switch(opcode)
127 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500128 case Shader::OPCODE_VS_1_0: break;
129 case Shader::OPCODE_VS_1_1: break;
130 case Shader::OPCODE_VS_2_0: break;
131 case Shader::OPCODE_VS_2_x: break;
132 case Shader::OPCODE_VS_2_sw: break;
133 case Shader::OPCODE_VS_3_0: break;
134 case Shader::OPCODE_VS_3_sw: break;
135 case Shader::OPCODE_DCL: break;
136 case Shader::OPCODE_DEF: break;
137 case Shader::OPCODE_DEFI: break;
138 case Shader::OPCODE_DEFB: break;
139 case Shader::OPCODE_NOP: break;
140 case Shader::OPCODE_ABS: abs(d, s0); break;
141 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400142 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500143 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
144 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
145 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
146 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
147 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
148 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
149 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
150 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
151 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
152 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
153 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
154 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
155 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
156 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
157 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
158 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
159 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400160 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
161 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
162 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500163 case Shader::OPCODE_ATT: att(d, s0, s1); break;
164 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
165 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
166 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
167 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
168 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400169 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
170 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400171 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500172 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400173 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500174 case Shader::OPCODE_LIT: lit(d, s0); break;
175 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
176 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
177 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
178 case Shader::OPCODE_LOG: log(d, s0, pp); break;
179 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
180 case Shader::OPCODE_STEP: step(d, s0, s1); break;
181 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400182 case Shader::OPCODE_FLOATBITSTOINT:
183 case Shader::OPCODE_FLOATBITSTOUINT:
184 case Shader::OPCODE_INTBITSTOFLOAT:
185 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500186 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
187 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
188 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
189 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
190 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
191 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
192 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
193 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400194 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
195 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500196 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400197 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
198 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500199 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400200 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400201 case Shader::OPCODE_NEG: neg(d, s0); break;
202 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500203 case Shader::OPCODE_F2B: f2b(d, s0); break;
204 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400205 case Shader::OPCODE_F2I: f2i(d, s0); break;
206 case Shader::OPCODE_I2F: i2f(d, s0); break;
207 case Shader::OPCODE_F2U: f2u(d, s0); break;
208 case Shader::OPCODE_U2F: u2f(d, s0); break;
209 case Shader::OPCODE_I2B: i2b(d, s0); break;
210 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500211 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400212 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500213 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
214 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
215 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
216 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
217 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
218 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
219 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400220 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
221 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500222 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400223 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
224 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
225 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500226 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
227 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
228 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
229 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
230 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
231 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
232 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
233 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
234 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
235 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
236 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
237 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
238 case Shader::OPCODE_SGE: step(d, s1, s0); break;
239 case Shader::OPCODE_SGN: sgn(d, s0); break;
240 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
241 case Shader::OPCODE_COS: cos(d, s0, pp); break;
242 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
243 case Shader::OPCODE_TAN: tan(d, s0); break;
244 case Shader::OPCODE_ACOS: acos(d, s0); break;
245 case Shader::OPCODE_ASIN: asin(d, s0); break;
246 case Shader::OPCODE_ATAN: atan(d, s0); break;
247 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
248 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
249 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
250 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
251 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
252 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
253 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
254 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
255 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400256 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500257 case Shader::OPCODE_BREAK: BREAK(); break;
258 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
259 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
260 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
261 case Shader::OPCODE_TEST: TEST(); break;
262 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
263 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
264 case Shader::OPCODE_ELSE: ELSE(); break;
265 case Shader::OPCODE_ENDIF: ENDIF(); break;
266 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
267 case Shader::OPCODE_ENDREP: ENDREP(); break;
268 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
269 case Shader::OPCODE_IF: IF(src0); break;
270 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
271 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
272 case Shader::OPCODE_LOOP: LOOP(src1); break;
273 case Shader::OPCODE_REP: REP(src0); break;
274 case Shader::OPCODE_WHILE: WHILE(src0); break;
275 case Shader::OPCODE_RET: RET(); break;
276 case Shader::OPCODE_LEAVE: LEAVE(); break;
277 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
278 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400279 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500280 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
281 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
282 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
283 case Shader::OPCODE_ALL: all(d.x, s0); break;
284 case Shader::OPCODE_ANY: any(d.x, s0); break;
285 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400286 case Shader::OPCODE_OR: or(d, s0, s1); break;
287 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
288 case Shader::OPCODE_AND: and(d, s0, s1); break;
289 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
290 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500291 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
292 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
293 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2, s3); break;
294 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
295 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2); break;
296 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2, s3); break;
297 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
298 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
299 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
300 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400301 default:
302 ASSERT(false);
303 }
304
John Bauman19bac1e2014-05-06 15:23:49 -0400305 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400306 {
John Bauman19bac1e2014-05-06 15:23:49 -0400307 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400308 {
John Bauman19bac1e2014-05-06 15:23:49 -0400309 switch(opcode)
310 {
311 case Shader::OPCODE_DIV:
312 if(dst.x) d.x = Trunc(d.x);
313 if(dst.y) d.y = Trunc(d.y);
314 if(dst.z) d.z = Trunc(d.z);
315 if(dst.w) d.w = Trunc(d.w);
316 break;
317 default:
318 break; // No truncation to integer required when arguments are integer
319 }
John Bauman89401822014-05-06 15:04:28 -0400320 }
321
John Bauman19bac1e2014-05-06 15:23:49 -0400322 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400323 {
John Bauman19bac1e2014-05-06 15:23:49 -0400324 if(dst.x) d.x = Max(d.x, Float4(0.0f));
325 if(dst.y) d.y = Max(d.y, Float4(0.0f));
326 if(dst.z) d.z = Max(d.z, Float4(0.0f));
327 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400328
John Bauman19bac1e2014-05-06 15:23:49 -0400329 if(dst.x) d.x = Min(d.x, Float4(1.0f));
330 if(dst.y) d.y = Min(d.y, Float4(1.0f));
331 if(dst.z) d.z = Min(d.z, Float4(1.0f));
332 if(dst.w) d.w = Min(d.w, Float4(1.0f));
333 }
334
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400335 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400336 {
337 Vector4f pDst; // FIXME: Rename
338
339 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400340 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500341 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400342 case Shader::PARAMETER_TEMP:
343 if(dst.rel.type == Shader::PARAMETER_VOID)
344 {
345 if(dst.x) pDst.x = r.r[dst.index].x;
346 if(dst.y) pDst.y = r.r[dst.index].y;
347 if(dst.z) pDst.z = r.r[dst.index].z;
348 if(dst.w) pDst.w = r.r[dst.index].w;
349 }
350 else
351 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500352 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400353
354 if(dst.x) pDst.x = r.r[dst.index + a].x;
355 if(dst.y) pDst.y = r.r[dst.index + a].y;
356 if(dst.z) pDst.z = r.r[dst.index + a].z;
357 if(dst.w) pDst.w = r.r[dst.index + a].w;
358 }
359 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500360 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400361 case Shader::PARAMETER_RASTOUT:
362 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400363 {
364 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400365 if(dst.x) pDst.x = r.o[Pos].x;
366 if(dst.y) pDst.y = r.o[Pos].y;
367 if(dst.z) pDst.z = r.o[Pos].z;
368 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400369 break;
370 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400371 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400372 break;
373 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400374 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400375 break;
376 default:
377 ASSERT(false);
378 }
379 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400380 case Shader::PARAMETER_ATTROUT:
381 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
382 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
383 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
384 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400385 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400386 case Shader::PARAMETER_TEXCRDOUT:
387 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400388 if(version < 0x0300)
389 {
John Bauman19bac1e2014-05-06 15:23:49 -0400390 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
391 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
392 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
393 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400394 }
395 else
396 {
John Bauman19bac1e2014-05-06 15:23:49 -0400397 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400398 {
John Bauman19bac1e2014-05-06 15:23:49 -0400399 if(dst.x) pDst.x = r.o[dst.index].x;
400 if(dst.y) pDst.y = r.o[dst.index].y;
401 if(dst.z) pDst.z = r.o[dst.index].z;
402 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400403 }
John Bauman19bac1e2014-05-06 15:23:49 -0400404 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400405 {
406 Int aL = r.aL[r.loopDepth];
407
John Bauman19bac1e2014-05-06 15:23:49 -0400408 if(dst.x) pDst.x = r.o[dst.index + aL].x;
409 if(dst.y) pDst.y = r.o[dst.index + aL].y;
410 if(dst.z) pDst.z = r.o[dst.index + aL].z;
411 if(dst.w) pDst.w = r.o[dst.index + aL].w;
412 }
413 else
414 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500415 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400416
417 if(dst.x) pDst.x = r.o[dst.index + a].x;
418 if(dst.y) pDst.y = r.o[dst.index + a].y;
419 if(dst.z) pDst.z = r.o[dst.index + a].z;
420 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400421 }
422 }
423 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500424 case Shader::PARAMETER_LABEL: break;
425 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
426 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400427 default:
428 ASSERT(false);
429 }
430
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500431 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400432
433 Int4 xEnable = enable;
434 Int4 yEnable = enable;
435 Int4 zEnable = enable;
436 Int4 wEnable = enable;
437
438 if(predicate)
439 {
John Bauman19bac1e2014-05-06 15:23:49 -0400440 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400441
442 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
443 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
444 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
445 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
446
John Bauman19bac1e2014-05-06 15:23:49 -0400447 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400448 {
John Bauman19bac1e2014-05-06 15:23:49 -0400449 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
450 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
451 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
452 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400453 }
454 else
455 {
John Bauman19bac1e2014-05-06 15:23:49 -0400456 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
457 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
458 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
459 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400460 }
461 }
462
John Bauman19bac1e2014-05-06 15:23:49 -0400463 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
464 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
465 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
466 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400467
John Bauman19bac1e2014-05-06 15:23:49 -0400468 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
469 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
470 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
471 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400472 }
473
John Bauman19bac1e2014-05-06 15:23:49 -0400474 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400475 {
John Bauman19bac1e2014-05-06 15:23:49 -0400476 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400477 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400478 case Shader::PARAMETER_TEMP:
479 if(dst.rel.type == Shader::PARAMETER_VOID)
480 {
481 if(dst.x) r.r[dst.index].x = d.x;
482 if(dst.y) r.r[dst.index].y = d.y;
483 if(dst.z) r.r[dst.index].z = d.z;
484 if(dst.w) r.r[dst.index].w = d.w;
485 }
486 else
487 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500488 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400489
490 if(dst.x) r.r[dst.index + a].x = d.x;
491 if(dst.y) r.r[dst.index + a].y = d.y;
492 if(dst.z) r.r[dst.index + a].z = d.z;
493 if(dst.w) r.r[dst.index + a].w = d.w;
494 }
John Bauman89401822014-05-06 15:04:28 -0400495 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400496 case Shader::PARAMETER_ADDR:
497 if(dst.x) r.a0.x = d.x;
498 if(dst.y) r.a0.y = d.y;
499 if(dst.z) r.a0.z = d.z;
500 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400501 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400502 case Shader::PARAMETER_RASTOUT:
503 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400504 {
505 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400506 if(dst.x) r.o[Pos].x = d.x;
507 if(dst.y) r.o[Pos].y = d.y;
508 if(dst.z) r.o[Pos].z = d.z;
509 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400510 break;
511 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400512 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400513 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500514 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400515 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400516 break;
517 default: ASSERT(false);
518 }
519 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500520 case Shader::PARAMETER_ATTROUT:
John Bauman19bac1e2014-05-06 15:23:49 -0400521 if(dst.x) r.o[D0 + dst.index].x = d.x;
522 if(dst.y) r.o[D0 + dst.index].y = d.y;
523 if(dst.z) r.o[D0 + dst.index].z = d.z;
524 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400525 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400526 case Shader::PARAMETER_TEXCRDOUT:
527 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400528 if(version < 0x0300)
529 {
John Bauman19bac1e2014-05-06 15:23:49 -0400530 if(dst.x) r.o[T0 + dst.index].x = d.x;
531 if(dst.y) r.o[T0 + dst.index].y = d.y;
532 if(dst.z) r.o[T0 + dst.index].z = d.z;
533 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400534 }
535 else
536 {
John Bauman19bac1e2014-05-06 15:23:49 -0400537 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400538 {
John Bauman19bac1e2014-05-06 15:23:49 -0400539 if(dst.x) r.o[dst.index].x = d.x;
540 if(dst.y) r.o[dst.index].y = d.y;
541 if(dst.z) r.o[dst.index].z = d.z;
542 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400543 }
John Bauman19bac1e2014-05-06 15:23:49 -0400544 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400545 {
546 Int aL = r.aL[r.loopDepth];
547
John Bauman19bac1e2014-05-06 15:23:49 -0400548 if(dst.x) r.o[dst.index + aL].x = d.x;
549 if(dst.y) r.o[dst.index + aL].y = d.y;
550 if(dst.z) r.o[dst.index + aL].z = d.z;
551 if(dst.w) r.o[dst.index + aL].w = d.w;
552 }
553 else
554 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500555 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400556
557 if(dst.x) r.o[dst.index + a].x = d.x;
558 if(dst.y) r.o[dst.index + a].y = d.y;
559 if(dst.z) r.o[dst.index + a].z = d.z;
560 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400561 }
562 }
563 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500564 case Shader::PARAMETER_LABEL: break;
565 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
566 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400567 default:
568 ASSERT(false);
569 }
570 }
571 }
572
John Bauman19bac1e2014-05-06 15:23:49 -0400573 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400574 {
575 Nucleus::setInsertBlock(returnBlock);
576 }
577 }
578
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500579 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400580 {
John Bauman19bac1e2014-05-06 15:23:49 -0400581 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400582 {
583 for(int i = 0; i < 12; i++)
584 {
John Bauman19bac1e2014-05-06 15:23:49 -0400585 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400586
587 switch(usage)
588 {
589 case 0xFF:
590 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400591 case Shader::USAGE_PSIZE:
592 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400593 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400594 case Shader::USAGE_TEXCOORD:
595 r.o[i].x = r.v[i].x;
596 r.o[i].y = r.v[i].y;
597 r.o[i].z = r.v[i].z;
598 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400599 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400600 case Shader::USAGE_POSITION:
601 r.o[i].x = r.v[i].x;
602 r.o[i].y = r.v[i].y;
603 r.o[i].z = r.v[i].z;
604 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400605 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400606 case Shader::USAGE_COLOR:
607 r.o[i].x = r.v[i].x;
608 r.o[i].y = r.v[i].y;
609 r.o[i].z = r.v[i].z;
610 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400611 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400612 case Shader::USAGE_FOG:
613 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400614 break;
615 default:
616 ASSERT(false);
617 }
618 }
619 }
620 else
621 {
John Bauman19bac1e2014-05-06 15:23:49 -0400622 r.o[Pos].x = r.v[PositionT].x;
623 r.o[Pos].y = r.v[PositionT].y;
624 r.o[Pos].z = r.v[PositionT].z;
625 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400626
627 for(int i = 0; i < 2; i++)
628 {
John Bauman19bac1e2014-05-06 15:23:49 -0400629 r.o[D0 + i].x = r.v[Color0 + i].x;
630 r.o[D0 + i].y = r.v[Color0 + i].y;
631 r.o[D0 + i].z = r.v[Color0 + i].z;
632 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400633 }
634
635 for(int i = 0; i < 8; i++)
636 {
John Bauman19bac1e2014-05-06 15:23:49 -0400637 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
638 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
639 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
640 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400641 }
642
John Bauman66b8ab22014-05-06 15:57:45 -0400643 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400644 }
645 }
646
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500647 Vector4f VertexProgram::fetchRegisterF(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400648 {
John Bauman19bac1e2014-05-06 15:23:49 -0400649 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500650 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400651
John Bauman89401822014-05-06 15:04:28 -0400652 switch(src.type)
653 {
John Bauman19bac1e2014-05-06 15:23:49 -0400654 case Shader::PARAMETER_TEMP:
655 if(src.rel.type == Shader::PARAMETER_VOID)
656 {
657 reg = r.r[i];
658 }
659 else
660 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500661 reg = r.r[i + relativeAddress(src)];
John Bauman19bac1e2014-05-06 15:23:49 -0400662 }
663 break;
664 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500665 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400666 break;
667 case Shader::PARAMETER_INPUT:
668 if(src.rel.type == Shader::PARAMETER_VOID)
669 {
670 reg = r.v[i];
671 }
672 else
673 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500674 reg = r.v[i + relativeAddress(src)];
John Bauman19bac1e2014-05-06 15:23:49 -0400675 }
676 break;
677 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
678 case Shader::PARAMETER_FLOAT4LITERAL:
679 reg.x = Float4(src.value[0]);
680 reg.y = Float4(src.value[1]);
681 reg.z = Float4(src.value[2]);
682 reg.w = Float4(src.value[3]);
683 break;
684 case Shader::PARAMETER_ADDR: reg = r.a0; break;
685 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
686 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
687 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
688 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
689 case Shader::PARAMETER_SAMPLER:
690 if(src.rel.type == Shader::PARAMETER_VOID)
691 {
692 reg.x = As<Float4>(Int4(i));
693 }
694 else if(src.rel.type == Shader::PARAMETER_TEMP)
695 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400696 reg.x = As<Float4>(Int4(i) + As<Int4>(r.r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400697 }
698 return reg;
699 case Shader::PARAMETER_OUTPUT:
700 if(src.rel.type == Shader::PARAMETER_VOID)
701 {
702 reg = r.o[i];
703 }
704 else
705 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500706 reg = r.o[i + relativeAddress(src)];
John Bauman19bac1e2014-05-06 15:23:49 -0400707 }
708 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400709 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400710 reg.x = As<Float>(Int(r.instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400711 return reg;
John Bauman89401822014-05-06 15:04:28 -0400712 default:
713 ASSERT(false);
714 }
715
John Bauman66b8ab22014-05-06 15:57:45 -0400716 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
717 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
718 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
719 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400720
John Bauman66b8ab22014-05-06 15:57:45 -0400721 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400722
723 switch(src.modifier)
724 {
John Bauman19bac1e2014-05-06 15:23:49 -0400725 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400726 mod.x = x;
727 mod.y = y;
728 mod.z = z;
729 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400730 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400731 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400732 mod.x = -x;
733 mod.y = -y;
734 mod.z = -z;
735 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400736 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400737 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400738 mod.x = Abs(x);
739 mod.y = Abs(y);
740 mod.z = Abs(z);
741 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400742 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400743 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400744 mod.x = -Abs(x);
745 mod.y = -Abs(y);
746 mod.z = -Abs(z);
747 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400748 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400749 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400750 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
751 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
752 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
753 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400754 break;
755 default:
756 ASSERT(false);
757 }
758
759 return mod;
760 }
761
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500762 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400763 {
John Bauman19bac1e2014-05-06 15:23:49 -0400764 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500765 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400766
767 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
768 {
769 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
770
771 c.x = c.x.xxxx;
772 c.y = c.y.yyyy;
773 c.z = c.z.zzzz;
774 c.w = c.w.wwww;
775
Nicolas Capenseafdb222015-05-15 15:24:08 -0400776 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400777 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500778 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400779 {
780 const Shader::Instruction &instruction = *shader->getInstruction(j);
781
782 if(instruction.opcode == Shader::OPCODE_DEF)
783 {
784 if(instruction.dst.index == i)
785 {
786 c.x = Float4(instruction.src[0].value[0]);
787 c.y = Float4(instruction.src[0].value[1]);
788 c.z = Float4(instruction.src[0].value[2]);
789 c.w = Float4(instruction.src[0].value[3]);
790
791 break;
792 }
793 }
794 }
795 }
796 }
797 else if(src.rel.type == Shader::PARAMETER_LOOP)
798 {
799 Int loopCounter = r.aL[r.loopDepth];
800
801 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
802
803 c.x = c.x.xxxx;
804 c.y = c.y.yyyy;
805 c.z = c.z.zzzz;
806 c.w = c.w.wwww;
807 }
808 else
809 {
810 if(src.rel.deterministic)
811 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500812 Int a = relativeAddress(src);
813
John Bauman19bac1e2014-05-06 15:23:49 -0400814 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
815
816 c.x = c.x.xxxx;
817 c.y = c.y.yyyy;
818 c.z = c.z.zzzz;
819 c.w = c.w.wwww;
820 }
821 else
822 {
823 int component = src.rel.swizzle & 0x03;
824 Float4 a;
825
826 switch(src.rel.type)
827 {
828 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
829 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
830 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
831 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400832 case Shader::PARAMETER_CONST: a = *Pointer<Float>(r.data + OFFSET(DrawData,vs.c[src.rel.index][component])); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400833 default: ASSERT(false);
834 }
835
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400836 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400837
Alexis Hetu028f41b2016-01-13 14:40:47 -0500838 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 -0500839
John Bauman19bac1e2014-05-06 15:23:49 -0400840 Int index0 = Extract(index, 0);
841 Int index1 = Extract(index, 1);
842 Int index2 = Extract(index, 2);
843 Int index3 = Extract(index, 3);
844
845 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
846 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
847 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
848 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
849
850 transpose4x4(c.x, c.y, c.z, c.w);
851 }
852 }
853
854 return c;
855 }
856
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500857 Int VertexProgram::relativeAddress(const Shader::Parameter &var)
John Bauman19bac1e2014-05-06 15:23:49 -0400858 {
859 ASSERT(var.rel.deterministic);
860
861 if(var.rel.type == Shader::PARAMETER_TEMP)
862 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400863 return As<Int>(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400864 }
865 else if(var.rel.type == Shader::PARAMETER_INPUT)
866 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400867 return As<Int>(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400868 }
869 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
870 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400871 return As<Int>(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400872 }
873 else if(var.rel.type == Shader::PARAMETER_CONST)
874 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400875 RValue<Int4> c = *Pointer<Int4>(r.data + OFFSET(DrawData, vs.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -0400876
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400877 return Extract(c, 0) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400878 }
879 else ASSERT(false);
880
881 return 0;
882 }
883
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500884 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400885 {
886 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500887
John Baumand4ae8632014-05-06 16:18:33 -0400888 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400889 {
John Baumand4ae8632014-05-06 16:18:33 -0400890 if(shader->containsBreakInstruction() && instruction->analysisBreak)
891 {
892 enable &= r.enableBreak;
893 }
John Bauman19bac1e2014-05-06 15:23:49 -0400894
John Baumand4ae8632014-05-06 16:18:33 -0400895 if(shader->containsContinueInstruction() && instruction->analysisContinue)
896 {
897 enable &= r.enableContinue;
898 }
John Bauman19bac1e2014-05-06 15:23:49 -0400899
John Baumand4ae8632014-05-06 16:18:33 -0400900 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
901 {
902 enable &= r.enableLeave;
903 }
John Bauman19bac1e2014-05-06 15:23:49 -0400904 }
905
906 return enable;
907 }
908
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500909 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400910 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500911 Vector4f row0 = fetchRegisterF(src1, 0);
912 Vector4f row1 = fetchRegisterF(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400913
914 dst.x = dot3(src0, row0);
915 dst.y = dot3(src0, row1);
916 }
917
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500918 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400919 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500920 Vector4f row0 = fetchRegisterF(src1, 0);
921 Vector4f row1 = fetchRegisterF(src1, 1);
922 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400923
924 dst.x = dot3(src0, row0);
925 dst.y = dot3(src0, row1);
926 dst.z = dot3(src0, row2);
927 }
928
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500929 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400930 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500931 Vector4f row0 = fetchRegisterF(src1, 0);
932 Vector4f row1 = fetchRegisterF(src1, 1);
933 Vector4f row2 = fetchRegisterF(src1, 2);
934 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400935
936 dst.x = dot3(src0, row0);
937 dst.y = dot3(src0, row1);
938 dst.z = dot3(src0, row2);
939 dst.w = dot3(src0, row3);
940 }
941
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500942 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400943 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500944 Vector4f row0 = fetchRegisterF(src1, 0);
945 Vector4f row1 = fetchRegisterF(src1, 1);
946 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400947
948 dst.x = dot4(src0, row0);
949 dst.y = dot4(src0, row1);
950 dst.z = dot4(src0, row2);
951 }
952
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500953 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400954 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500955 Vector4f row0 = fetchRegisterF(src1, 0);
956 Vector4f row1 = fetchRegisterF(src1, 1);
957 Vector4f row2 = fetchRegisterF(src1, 2);
958 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400959
960 dst.x = dot4(src0, row0);
961 dst.y = dot4(src0, row1);
962 dst.z = dot4(src0, row2);
963 dst.w = dot4(src0, row3);
964 }
965
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500966 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -0400967 {
968 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
969 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
970
971 if(breakDepth == 0)
972 {
John Bauman19bac1e2014-05-06 15:23:49 -0400973 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400974 Nucleus::createBr(endBlock);
975 }
976 else
977 {
978 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
979 Bool allBreak = SignMask(r.enableBreak) == 0x0;
980
John Bauman19bac1e2014-05-06 15:23:49 -0400981 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400982 branch(allBreak, endBlock, deadBlock);
983 }
984
985 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400986 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400987 }
988
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500989 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400990 {
991 Int4 condition;
992
993 switch(control)
994 {
John Bauman19bac1e2014-05-06 15:23:49 -0400995 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
996 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
997 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
998 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
999 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1000 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001001 default:
1002 ASSERT(false);
1003 }
1004
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001005 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001006 }
1007
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001008 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001009 {
1010 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1011
John Bauman19bac1e2014-05-06 15:23:49 -04001012 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001013 {
1014 condition = ~condition;
1015 }
1016
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001017 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001018 }
1019
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001020 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001021 {
John Bauman89401822014-05-06 15:04:28 -04001022 condition &= r.enableStack[r.enableIndex];
1023
1024 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1025 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1026
1027 r.enableBreak = r.enableBreak & ~condition;
1028 Bool allBreak = SignMask(r.enableBreak) == 0x0;
1029
John Bauman19bac1e2014-05-06 15:23:49 -04001030 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001031 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001032
John Bauman89401822014-05-06 15:04:28 -04001033 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001034 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001035 }
1036
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001037 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001038 {
1039 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1040 }
1041
1042 void VertexProgram::TEST()
1043 {
1044 whileTest = true;
1045 }
1046
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001047 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001048 {
1049 if(!labelBlock[labelIndex])
1050 {
1051 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1052 }
1053
John Bauman19bac1e2014-05-06 15:23:49 -04001054 if(callRetBlock[labelIndex].size() > 1)
1055 {
1056 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1057 }
John Bauman89401822014-05-06 15:04:28 -04001058
John Bauman19bac1e2014-05-06 15:23:49 -04001059 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001060
1061 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001062 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1063
1064 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001065 }
1066
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001067 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001068 {
John Bauman19bac1e2014-05-06 15:23:49 -04001069 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001070 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001071 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001072 }
John Bauman19bac1e2014-05-06 15:23:49 -04001073 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001074 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001075 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001076 }
1077 else ASSERT(false);
1078 }
1079
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001080 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001081 {
1082 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001083
John Bauman19bac1e2014-05-06 15:23:49 -04001084 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001085 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001086 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001087 }
1088
1089 if(!labelBlock[labelIndex])
1090 {
1091 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1092 }
1093
John Bauman19bac1e2014-05-06 15:23:49 -04001094 if(callRetBlock[labelIndex].size() > 1)
1095 {
1096 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1097 }
John Bauman89401822014-05-06 15:04:28 -04001098
John Bauman19bac1e2014-05-06 15:23:49 -04001099 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001100
John Bauman19bac1e2014-05-06 15:23:49 -04001101 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1102 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1103
1104 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001105 }
1106
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001107 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001108 {
1109 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1110
John Bauman19bac1e2014-05-06 15:23:49 -04001111 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001112 {
1113 condition = ~condition;
1114 }
1115
1116 condition &= r.enableStack[r.enableIndex];
1117
1118 if(!labelBlock[labelIndex])
1119 {
1120 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1121 }
1122
John Bauman19bac1e2014-05-06 15:23:49 -04001123 if(callRetBlock[labelIndex].size() > 1)
1124 {
1125 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1126 }
John Bauman89401822014-05-06 15:04:28 -04001127
1128 r.enableIndex++;
1129 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001130 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001131
John Bauman19bac1e2014-05-06 15:23:49 -04001132 Bool notAllFalse = SignMask(condition) != 0;
1133 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1134 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001135
1136 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001137 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001138 }
1139
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001140 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001141 {
1142 ifDepth--;
1143
1144 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1145 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1146
1147 if(isConditionalIf[ifDepth])
1148 {
1149 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001150 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001151
1152 branch(notAllFalse, falseBlock, endBlock);
1153
1154 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1155 }
1156 else
1157 {
1158 Nucleus::createBr(endBlock);
1159 Nucleus::setInsertBlock(falseBlock);
1160 }
1161
1162 ifFalseBlock[ifDepth] = endBlock;
1163
1164 ifDepth++;
1165 }
1166
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001167 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001168 {
1169 ifDepth--;
1170
1171 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1172
1173 Nucleus::createBr(endBlock);
1174 Nucleus::setInsertBlock(endBlock);
1175
1176 if(isConditionalIf[ifDepth])
1177 {
1178 breakDepth--;
1179 r.enableIndex--;
1180 }
1181 }
1182
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001183 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001184 {
1185 loopRepDepth--;
1186
1187 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1188
1189 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1190 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1191
1192 Nucleus::createBr(testBlock);
1193 Nucleus::setInsertBlock(endBlock);
1194
1195 r.loopDepth--;
1196 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1197 }
1198
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001199 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001200 {
1201 loopRepDepth--;
1202
1203 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1204 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1205
1206 Nucleus::createBr(testBlock);
1207 Nucleus::setInsertBlock(endBlock);
1208
1209 r.loopDepth--;
1210 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1211 }
1212
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001213 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001214 {
1215 loopRepDepth--;
1216
1217 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1218 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1219
1220 Nucleus::createBr(testBlock);
1221 Nucleus::setInsertBlock(endBlock);
1222
1223 r.enableIndex--;
1224 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1225 whileTest = false;
1226 }
1227
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001228 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001229 {
John Bauman19bac1e2014-05-06 15:23:49 -04001230 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001231 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001232 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001233 }
John Bauman19bac1e2014-05-06 15:23:49 -04001234 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001235 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001236 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001237 }
John Bauman19bac1e2014-05-06 15:23:49 -04001238 else
1239 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001240 Int4 condition = As<Int4>(fetchRegisterF(src).x);
1241 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001242 }
John Bauman89401822014-05-06 15:04:28 -04001243 }
1244
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001245 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001246 {
1247 ASSERT(ifDepth < 24 + 4);
1248
1249 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1250
John Bauman19bac1e2014-05-06 15:23:49 -04001251 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001252 {
John Bauman19bac1e2014-05-06 15:23:49 -04001253 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001254 }
1255
1256 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1257 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1258
1259 branch(condition, trueBlock, falseBlock);
1260
1261 isConditionalIf[ifDepth] = false;
1262 ifFalseBlock[ifDepth] = falseBlock;
1263
1264 ifDepth++;
1265 }
1266
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001267 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001268 {
1269 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1270
John Bauman19bac1e2014-05-06 15:23:49 -04001271 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001272 {
1273 condition = ~condition;
1274 }
1275
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001276 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001277 }
1278
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001279 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001280 {
1281 Int4 condition;
1282
1283 switch(control)
1284 {
John Bauman19bac1e2014-05-06 15:23:49 -04001285 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1286 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1287 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1288 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1289 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1290 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001291 default:
1292 ASSERT(false);
1293 }
1294
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001295 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001296 }
1297
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001298 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001299 {
John Bauman89401822014-05-06 15:04:28 -04001300 condition &= r.enableStack[r.enableIndex];
1301
1302 r.enableIndex++;
1303 r.enableStack[r.enableIndex] = condition;
1304
1305 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1306 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1307
John Bauman19bac1e2014-05-06 15:23:49 -04001308 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001309
1310 branch(notAllFalse, trueBlock, falseBlock);
1311
1312 isConditionalIf[ifDepth] = true;
1313 ifFalseBlock[ifDepth] = falseBlock;
1314
1315 ifDepth++;
1316 breakDepth++;
1317 }
1318
1319 void VertexProgram::LABEL(int labelIndex)
1320 {
John Bauman19bac1e2014-05-06 15:23:49 -04001321 if(!labelBlock[labelIndex])
1322 {
1323 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1324 }
1325
John Bauman89401822014-05-06 15:04:28 -04001326 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001327 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001328 }
1329
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001330 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001331 {
1332 r.loopDepth++;
1333
1334 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1335 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1336 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1337
1338 // FIXME: Compiles to two instructions?
1339 If(r.increment[r.loopDepth] == 0)
1340 {
1341 r.increment[r.loopDepth] = 1;
1342 }
1343
1344 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1345 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1346 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1347
1348 loopRepTestBlock[loopRepDepth] = testBlock;
1349 loopRepEndBlock[loopRepDepth] = endBlock;
1350
1351 // FIXME: jump(testBlock)
1352 Nucleus::createBr(testBlock);
1353 Nucleus::setInsertBlock(testBlock);
1354
1355 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1356 Nucleus::setInsertBlock(loopBlock);
1357
1358 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001359
John Bauman89401822014-05-06 15:04:28 -04001360 loopRepDepth++;
1361 breakDepth = 0;
1362 }
1363
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001364 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001365 {
1366 r.loopDepth++;
1367
1368 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1369 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1370
1371 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1372 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1373 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1374
1375 loopRepTestBlock[loopRepDepth] = testBlock;
1376 loopRepEndBlock[loopRepDepth] = endBlock;
1377
1378 // FIXME: jump(testBlock)
1379 Nucleus::createBr(testBlock);
1380 Nucleus::setInsertBlock(testBlock);
1381
1382 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1383 Nucleus::setInsertBlock(loopBlock);
1384
1385 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1386
1387 loopRepDepth++;
1388 breakDepth = 0;
1389 }
1390
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001391 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001392 {
1393 r.enableIndex++;
1394
1395 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1396 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1397 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001398
John Bauman19bac1e2014-05-06 15:23:49 -04001399 loopRepTestBlock[loopRepDepth] = testBlock;
1400 loopRepEndBlock[loopRepDepth] = endBlock;
1401
1402 Int4 restoreBreak = r.enableBreak;
1403 Int4 restoreContinue = r.enableContinue;
1404
1405 // FIXME: jump(testBlock)
1406 Nucleus::createBr(testBlock);
1407 Nucleus::setInsertBlock(testBlock);
1408 r.enableContinue = restoreContinue;
1409
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001410 const Vector4f &src = fetchRegisterF(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001411 Int4 condition = As<Int4>(src.x);
1412 condition &= r.enableStack[r.enableIndex - 1];
1413 r.enableStack[r.enableIndex] = condition;
1414
1415 Bool notAllFalse = SignMask(condition) != 0;
1416 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001417
John Bauman19bac1e2014-05-06 15:23:49 -04001418 Nucleus::setInsertBlock(endBlock);
1419 r.enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001420
John Bauman19bac1e2014-05-06 15:23:49 -04001421 Nucleus::setInsertBlock(loopBlock);
1422
1423 loopRepDepth++;
1424 breakDepth = 0;
1425 }
1426
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001427 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001428 {
John Bauman19bac1e2014-05-06 15:23:49 -04001429 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001430 {
1431 returnBlock = Nucleus::createBasicBlock();
1432 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001433 }
1434 else
1435 {
John Bauman89401822014-05-06 15:04:28 -04001436 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001437
John Bauman19bac1e2014-05-06 15:23:49 -04001438 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001439 {
John Bauman19bac1e2014-05-06 15:23:49 -04001440 // FIXME: Encapsulate
1441 UInt index = r.callStack[--r.stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001442
John Bauman66b8ab22014-05-06 15:57:45 -04001443 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001444 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1445
1446 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1447 {
1448 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1449 }
1450 }
1451 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1452 {
1453 Nucleus::createBr(callRetBlock[currentLabel][0]);
1454 }
1455 else // Function isn't called
1456 {
1457 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001458 }
1459
1460 Nucleus::setInsertBlock(unreachableBlock);
1461 Nucleus::createUnreachable();
1462 }
1463 }
1464
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001465 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001466 {
John Bauman19bac1e2014-05-06 15:23:49 -04001467 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001468
John Bauman19bac1e2014-05-06 15:23:49 -04001469 // FIXME: Return from function if all instances left
1470 // FIXME: Use enableLeave in other control-flow constructs
1471 }
John Bauman89401822014-05-06 15:04:28 -04001472
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001473 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001474 {
1475 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001476 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001477
1478 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1479 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1480 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1481 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1482 }
John Bauman19bac1e2014-05-06 15:23:49 -04001483
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001484 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001485 {
1486 Float4 lod = Float4(0.0f);
1487 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001488 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001489
1490 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1491 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1492 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1493 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1494 }
1495
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001496 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001497 {
1498 UNIMPLEMENTED();
1499 }
1500
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001501 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001502 {
1503 UNIMPLEMENTED();
1504 }
1505
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001506 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
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, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001512 {
1513 UNIMPLEMENTED();
1514 }
1515
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001516 void VertexProgram::TEXGRAD(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::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001522 {
1523 UNIMPLEMENTED();
1524 }
1525
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001526 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001527 {
1528 Pointer<Byte> textureMipmap = r.data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
1529 for(int i = 0; i < 4; ++i)
1530 {
1531 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1532 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1533 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1534 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1535 }
1536 }
1537
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001538 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
John Bauman19bac1e2014-05-06 15:23:49 -04001539 {
1540 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1541 {
1542 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001543 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001544 }
1545 else
1546 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001547 Int index = As<Int>(Float(fetchRegisterF(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001548
1549 for(int i = 0; i < 16; i++)
1550 {
1551 if(shader->usesSampler(i))
1552 {
1553 If(index == i)
1554 {
1555 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1556 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1557 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1558 }
1559 }
1560 }
1561 }
1562 }
John Bauman89401822014-05-06 15:04:28 -04001563}