blob: a6126dbd9708186cd375e2e898669546a6ada0dd [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
405 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500406 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400407
408 if(dst.x) pDst.x = r.o[dst.index + a].x;
409 if(dst.y) pDst.y = r.o[dst.index + a].y;
410 if(dst.z) pDst.z = r.o[dst.index + a].z;
411 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400412 }
413 }
414 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500415 case Shader::PARAMETER_LABEL: break;
416 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
417 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400418 default:
419 ASSERT(false);
420 }
421
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500422 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400423
424 Int4 xEnable = enable;
425 Int4 yEnable = enable;
426 Int4 zEnable = enable;
427 Int4 wEnable = enable;
428
429 if(predicate)
430 {
John Bauman19bac1e2014-05-06 15:23:49 -0400431 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400432
433 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
434 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
435 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
436 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
437
John Bauman19bac1e2014-05-06 15:23:49 -0400438 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400439 {
John Bauman19bac1e2014-05-06 15:23:49 -0400440 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
441 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
442 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
443 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400444 }
445 else
446 {
John Bauman19bac1e2014-05-06 15:23:49 -0400447 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
448 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
449 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
450 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400451 }
452 }
453
John Bauman19bac1e2014-05-06 15:23:49 -0400454 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
455 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
456 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
457 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400458
John Bauman19bac1e2014-05-06 15:23:49 -0400459 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
460 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
461 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
462 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400463 }
464
John Bauman19bac1e2014-05-06 15:23:49 -0400465 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400466 {
John Bauman19bac1e2014-05-06 15:23:49 -0400467 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400468 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400469 case Shader::PARAMETER_TEMP:
470 if(dst.rel.type == Shader::PARAMETER_VOID)
471 {
472 if(dst.x) r.r[dst.index].x = d.x;
473 if(dst.y) r.r[dst.index].y = d.y;
474 if(dst.z) r.r[dst.index].z = d.z;
475 if(dst.w) r.r[dst.index].w = d.w;
476 }
477 else
478 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500479 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400480
481 if(dst.x) r.r[dst.index + a].x = d.x;
482 if(dst.y) r.r[dst.index + a].y = d.y;
483 if(dst.z) r.r[dst.index + a].z = d.z;
484 if(dst.w) r.r[dst.index + a].w = d.w;
485 }
John Bauman89401822014-05-06 15:04:28 -0400486 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400487 case Shader::PARAMETER_ADDR:
488 if(dst.x) r.a0.x = d.x;
489 if(dst.y) r.a0.y = d.y;
490 if(dst.z) r.a0.z = d.z;
491 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400492 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400493 case Shader::PARAMETER_RASTOUT:
494 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400495 {
496 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400497 if(dst.x) r.o[Pos].x = d.x;
498 if(dst.y) r.o[Pos].y = d.y;
499 if(dst.z) r.o[Pos].z = d.z;
500 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400501 break;
502 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400503 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400504 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500505 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400506 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400507 break;
508 default: ASSERT(false);
509 }
510 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500511 case Shader::PARAMETER_ATTROUT:
John Bauman19bac1e2014-05-06 15:23:49 -0400512 if(dst.x) r.o[D0 + dst.index].x = d.x;
513 if(dst.y) r.o[D0 + dst.index].y = d.y;
514 if(dst.z) r.o[D0 + dst.index].z = d.z;
515 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400516 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400517 case Shader::PARAMETER_TEXCRDOUT:
518 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400519 if(version < 0x0300)
520 {
John Bauman19bac1e2014-05-06 15:23:49 -0400521 if(dst.x) r.o[T0 + dst.index].x = d.x;
522 if(dst.y) r.o[T0 + dst.index].y = d.y;
523 if(dst.z) r.o[T0 + dst.index].z = d.z;
524 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400525 }
526 else
527 {
John Bauman19bac1e2014-05-06 15:23:49 -0400528 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400529 {
John Bauman19bac1e2014-05-06 15:23:49 -0400530 if(dst.x) r.o[dst.index].x = d.x;
531 if(dst.y) r.o[dst.index].y = d.y;
532 if(dst.z) r.o[dst.index].z = d.z;
533 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400534 }
John Bauman19bac1e2014-05-06 15:23:49 -0400535 else
536 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500537 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400538
539 if(dst.x) r.o[dst.index + a].x = d.x;
540 if(dst.y) r.o[dst.index + a].y = d.y;
541 if(dst.z) r.o[dst.index + a].z = d.z;
542 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400543 }
544 }
545 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500546 case Shader::PARAMETER_LABEL: break;
547 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
548 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400549 default:
550 ASSERT(false);
551 }
552 }
553 }
554
John Bauman19bac1e2014-05-06 15:23:49 -0400555 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400556 {
557 Nucleus::setInsertBlock(returnBlock);
558 }
559 }
560
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500561 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400562 {
John Bauman19bac1e2014-05-06 15:23:49 -0400563 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400564 {
565 for(int i = 0; i < 12; i++)
566 {
John Bauman19bac1e2014-05-06 15:23:49 -0400567 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400568
569 switch(usage)
570 {
571 case 0xFF:
572 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400573 case Shader::USAGE_PSIZE:
574 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400575 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400576 case Shader::USAGE_TEXCOORD:
577 r.o[i].x = r.v[i].x;
578 r.o[i].y = r.v[i].y;
579 r.o[i].z = r.v[i].z;
580 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400581 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400582 case Shader::USAGE_POSITION:
583 r.o[i].x = r.v[i].x;
584 r.o[i].y = r.v[i].y;
585 r.o[i].z = r.v[i].z;
586 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400587 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400588 case Shader::USAGE_COLOR:
589 r.o[i].x = r.v[i].x;
590 r.o[i].y = r.v[i].y;
591 r.o[i].z = r.v[i].z;
592 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400593 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400594 case Shader::USAGE_FOG:
595 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400596 break;
597 default:
598 ASSERT(false);
599 }
600 }
601 }
602 else
603 {
John Bauman19bac1e2014-05-06 15:23:49 -0400604 r.o[Pos].x = r.v[PositionT].x;
605 r.o[Pos].y = r.v[PositionT].y;
606 r.o[Pos].z = r.v[PositionT].z;
607 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400608
609 for(int i = 0; i < 2; i++)
610 {
John Bauman19bac1e2014-05-06 15:23:49 -0400611 r.o[D0 + i].x = r.v[Color0 + i].x;
612 r.o[D0 + i].y = r.v[Color0 + i].y;
613 r.o[D0 + i].z = r.v[Color0 + i].z;
614 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400615 }
616
617 for(int i = 0; i < 8; i++)
618 {
John Bauman19bac1e2014-05-06 15:23:49 -0400619 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
620 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
621 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
622 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400623 }
624
John Bauman66b8ab22014-05-06 15:57:45 -0400625 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400626 }
627 }
628
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500629 Vector4f VertexProgram::fetchRegisterF(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400630 {
John Bauman19bac1e2014-05-06 15:23:49 -0400631 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500632 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400633
John Bauman89401822014-05-06 15:04:28 -0400634 switch(src.type)
635 {
John Bauman19bac1e2014-05-06 15:23:49 -0400636 case Shader::PARAMETER_TEMP:
637 if(src.rel.type == Shader::PARAMETER_VOID)
638 {
639 reg = r.r[i];
640 }
641 else
642 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500643 reg = r.r[i + relativeAddress(src)];
John Bauman19bac1e2014-05-06 15:23:49 -0400644 }
645 break;
646 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500647 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400648 break;
649 case Shader::PARAMETER_INPUT:
650 if(src.rel.type == Shader::PARAMETER_VOID)
651 {
652 reg = r.v[i];
653 }
654 else
655 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500656 reg = r.v[i + relativeAddress(src)];
John Bauman19bac1e2014-05-06 15:23:49 -0400657 }
658 break;
659 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
660 case Shader::PARAMETER_FLOAT4LITERAL:
661 reg.x = Float4(src.value[0]);
662 reg.y = Float4(src.value[1]);
663 reg.z = Float4(src.value[2]);
664 reg.w = Float4(src.value[3]);
665 break;
666 case Shader::PARAMETER_ADDR: reg = r.a0; break;
667 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
668 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
669 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
670 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
671 case Shader::PARAMETER_SAMPLER:
672 if(src.rel.type == Shader::PARAMETER_VOID)
673 {
674 reg.x = As<Float4>(Int4(i));
675 }
676 else if(src.rel.type == Shader::PARAMETER_TEMP)
677 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400678 reg.x = As<Float4>(Int4(i) + As<Int4>(r.r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400679 }
680 return reg;
681 case Shader::PARAMETER_OUTPUT:
682 if(src.rel.type == Shader::PARAMETER_VOID)
683 {
684 reg = r.o[i];
685 }
686 else
687 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500688 reg = r.o[i + relativeAddress(src)];
John Bauman19bac1e2014-05-06 15:23:49 -0400689 }
690 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400691 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400692 reg.x = As<Float>(Int(r.instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400693 return reg;
John Bauman89401822014-05-06 15:04:28 -0400694 default:
695 ASSERT(false);
696 }
697
John Bauman66b8ab22014-05-06 15:57:45 -0400698 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
699 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
700 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
701 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400702
John Bauman66b8ab22014-05-06 15:57:45 -0400703 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400704
705 switch(src.modifier)
706 {
John Bauman19bac1e2014-05-06 15:23:49 -0400707 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400708 mod.x = x;
709 mod.y = y;
710 mod.z = z;
711 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400712 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400713 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400714 mod.x = -x;
715 mod.y = -y;
716 mod.z = -z;
717 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400718 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400719 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400720 mod.x = Abs(x);
721 mod.y = Abs(y);
722 mod.z = Abs(z);
723 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400724 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400725 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400726 mod.x = -Abs(x);
727 mod.y = -Abs(y);
728 mod.z = -Abs(z);
729 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400730 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400731 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400732 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
733 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
734 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
735 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400736 break;
737 default:
738 ASSERT(false);
739 }
740
741 return mod;
742 }
743
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500744 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400745 {
John Bauman19bac1e2014-05-06 15:23:49 -0400746 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500747 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400748
749 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
750 {
751 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
752
753 c.x = c.x.xxxx;
754 c.y = c.y.yyyy;
755 c.z = c.z.zzzz;
756 c.w = c.w.wwww;
757
Nicolas Capenseafdb222015-05-15 15:24:08 -0400758 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400759 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500760 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400761 {
762 const Shader::Instruction &instruction = *shader->getInstruction(j);
763
764 if(instruction.opcode == Shader::OPCODE_DEF)
765 {
766 if(instruction.dst.index == i)
767 {
768 c.x = Float4(instruction.src[0].value[0]);
769 c.y = Float4(instruction.src[0].value[1]);
770 c.z = Float4(instruction.src[0].value[2]);
771 c.w = Float4(instruction.src[0].value[3]);
772
773 break;
774 }
775 }
776 }
777 }
778 }
779 else if(src.rel.type == Shader::PARAMETER_LOOP)
780 {
781 Int loopCounter = r.aL[r.loopDepth];
782
783 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
784
785 c.x = c.x.xxxx;
786 c.y = c.y.yyyy;
787 c.z = c.z.zzzz;
788 c.w = c.w.wwww;
789 }
790 else
791 {
792 if(src.rel.deterministic)
793 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500794 Int a = relativeAddress(src);
795
John Bauman19bac1e2014-05-06 15:23:49 -0400796 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
797
798 c.x = c.x.xxxx;
799 c.y = c.y.yyyy;
800 c.z = c.z.zzzz;
801 c.w = c.w.wwww;
802 }
803 else
804 {
805 int component = src.rel.swizzle & 0x03;
806 Float4 a;
807
808 switch(src.rel.type)
809 {
810 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
811 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
812 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
813 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400814 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 -0400815 default: ASSERT(false);
816 }
817
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400818 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400819
Alexis Hetu028f41b2016-01-13 14:40:47 -0500820 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 -0500821
John Bauman19bac1e2014-05-06 15:23:49 -0400822 Int index0 = Extract(index, 0);
823 Int index1 = Extract(index, 1);
824 Int index2 = Extract(index, 2);
825 Int index3 = Extract(index, 3);
826
827 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
828 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
829 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
830 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
831
832 transpose4x4(c.x, c.y, c.z, c.w);
833 }
834 }
835
836 return c;
837 }
838
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500839 Int VertexProgram::relativeAddress(const Shader::Parameter &var)
John Bauman19bac1e2014-05-06 15:23:49 -0400840 {
841 ASSERT(var.rel.deterministic);
842
843 if(var.rel.type == Shader::PARAMETER_TEMP)
844 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400845 return As<Int>(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400846 }
847 else if(var.rel.type == Shader::PARAMETER_INPUT)
848 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400849 return As<Int>(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400850 }
851 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
852 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400853 return As<Int>(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400854 }
855 else if(var.rel.type == Shader::PARAMETER_CONST)
856 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400857 RValue<Int4> c = *Pointer<Int4>(r.data + OFFSET(DrawData, vs.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -0400858
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400859 return Extract(c, 0) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400860 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500861 else if(var.rel.type == Shader::PARAMETER_LOOP)
862 {
863 return r.aL[r.loopDepth];
864 }
John Bauman19bac1e2014-05-06 15:23:49 -0400865 else ASSERT(false);
866
867 return 0;
868 }
869
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500870 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400871 {
872 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500873
John Baumand4ae8632014-05-06 16:18:33 -0400874 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400875 {
John Baumand4ae8632014-05-06 16:18:33 -0400876 if(shader->containsBreakInstruction() && instruction->analysisBreak)
877 {
878 enable &= r.enableBreak;
879 }
John Bauman19bac1e2014-05-06 15:23:49 -0400880
John Baumand4ae8632014-05-06 16:18:33 -0400881 if(shader->containsContinueInstruction() && instruction->analysisContinue)
882 {
883 enable &= r.enableContinue;
884 }
John Bauman19bac1e2014-05-06 15:23:49 -0400885
John Baumand4ae8632014-05-06 16:18:33 -0400886 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
887 {
888 enable &= r.enableLeave;
889 }
John Bauman19bac1e2014-05-06 15:23:49 -0400890 }
891
892 return enable;
893 }
894
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500895 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400896 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500897 Vector4f row0 = fetchRegisterF(src1, 0);
898 Vector4f row1 = fetchRegisterF(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400899
900 dst.x = dot3(src0, row0);
901 dst.y = dot3(src0, row1);
902 }
903
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500904 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400905 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500906 Vector4f row0 = fetchRegisterF(src1, 0);
907 Vector4f row1 = fetchRegisterF(src1, 1);
908 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400909
910 dst.x = dot3(src0, row0);
911 dst.y = dot3(src0, row1);
912 dst.z = dot3(src0, row2);
913 }
914
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500915 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400916 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500917 Vector4f row0 = fetchRegisterF(src1, 0);
918 Vector4f row1 = fetchRegisterF(src1, 1);
919 Vector4f row2 = fetchRegisterF(src1, 2);
920 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400921
922 dst.x = dot3(src0, row0);
923 dst.y = dot3(src0, row1);
924 dst.z = dot3(src0, row2);
925 dst.w = dot3(src0, row3);
926 }
927
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500928 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400929 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500930 Vector4f row0 = fetchRegisterF(src1, 0);
931 Vector4f row1 = fetchRegisterF(src1, 1);
932 Vector4f row2 = fetchRegisterF(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400933
934 dst.x = dot4(src0, row0);
935 dst.y = dot4(src0, row1);
936 dst.z = dot4(src0, row2);
937 }
938
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500939 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400940 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500941 Vector4f row0 = fetchRegisterF(src1, 0);
942 Vector4f row1 = fetchRegisterF(src1, 1);
943 Vector4f row2 = fetchRegisterF(src1, 2);
944 Vector4f row3 = fetchRegisterF(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400945
946 dst.x = dot4(src0, row0);
947 dst.y = dot4(src0, row1);
948 dst.z = dot4(src0, row2);
949 dst.w = dot4(src0, row3);
950 }
951
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500952 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -0400953 {
954 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
955 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
956
957 if(breakDepth == 0)
958 {
John Bauman19bac1e2014-05-06 15:23:49 -0400959 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400960 Nucleus::createBr(endBlock);
961 }
962 else
963 {
964 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
965 Bool allBreak = SignMask(r.enableBreak) == 0x0;
966
John Bauman19bac1e2014-05-06 15:23:49 -0400967 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400968 branch(allBreak, endBlock, deadBlock);
969 }
970
971 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400972 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400973 }
974
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500975 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400976 {
977 Int4 condition;
978
979 switch(control)
980 {
John Bauman19bac1e2014-05-06 15:23:49 -0400981 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
982 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
983 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
984 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
985 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
986 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400987 default:
988 ASSERT(false);
989 }
990
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500991 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -0400992 }
993
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500994 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -0400995 {
996 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
997
John Bauman19bac1e2014-05-06 15:23:49 -0400998 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -0400999 {
1000 condition = ~condition;
1001 }
1002
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001003 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001004 }
1005
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001006 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001007 {
John Bauman89401822014-05-06 15:04:28 -04001008 condition &= r.enableStack[r.enableIndex];
1009
1010 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1011 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1012
1013 r.enableBreak = r.enableBreak & ~condition;
1014 Bool allBreak = SignMask(r.enableBreak) == 0x0;
1015
John Bauman19bac1e2014-05-06 15:23:49 -04001016 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001017 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001018
John Bauman89401822014-05-06 15:04:28 -04001019 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001020 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001021 }
1022
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001023 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001024 {
1025 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1026 }
1027
1028 void VertexProgram::TEST()
1029 {
1030 whileTest = true;
1031 }
1032
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001033 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001034 {
1035 if(!labelBlock[labelIndex])
1036 {
1037 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1038 }
1039
John Bauman19bac1e2014-05-06 15:23:49 -04001040 if(callRetBlock[labelIndex].size() > 1)
1041 {
1042 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1043 }
John Bauman89401822014-05-06 15:04:28 -04001044
John Bauman19bac1e2014-05-06 15:23:49 -04001045 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001046
1047 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001048 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1049
1050 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001051 }
1052
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001053 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001054 {
John Bauman19bac1e2014-05-06 15:23:49 -04001055 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001056 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001057 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001058 }
John Bauman19bac1e2014-05-06 15:23:49 -04001059 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001060 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001061 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001062 }
1063 else ASSERT(false);
1064 }
1065
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001066 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001067 {
1068 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001069
John Bauman19bac1e2014-05-06 15:23:49 -04001070 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001071 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001072 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001073 }
1074
1075 if(!labelBlock[labelIndex])
1076 {
1077 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1078 }
1079
John Bauman19bac1e2014-05-06 15:23:49 -04001080 if(callRetBlock[labelIndex].size() > 1)
1081 {
1082 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1083 }
John Bauman89401822014-05-06 15:04:28 -04001084
John Bauman19bac1e2014-05-06 15:23:49 -04001085 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001086
John Bauman19bac1e2014-05-06 15:23:49 -04001087 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1088 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1089
1090 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001091 }
1092
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001093 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001094 {
1095 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1096
John Bauman19bac1e2014-05-06 15:23:49 -04001097 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001098 {
1099 condition = ~condition;
1100 }
1101
1102 condition &= r.enableStack[r.enableIndex];
1103
1104 if(!labelBlock[labelIndex])
1105 {
1106 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1107 }
1108
John Bauman19bac1e2014-05-06 15:23:49 -04001109 if(callRetBlock[labelIndex].size() > 1)
1110 {
1111 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1112 }
John Bauman89401822014-05-06 15:04:28 -04001113
1114 r.enableIndex++;
1115 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001116 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001117
John Bauman19bac1e2014-05-06 15:23:49 -04001118 Bool notAllFalse = SignMask(condition) != 0;
1119 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1120 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001121
1122 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001123 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001124 }
1125
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001126 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001127 {
1128 ifDepth--;
1129
1130 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1131 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1132
1133 if(isConditionalIf[ifDepth])
1134 {
1135 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001136 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001137
1138 branch(notAllFalse, falseBlock, endBlock);
1139
1140 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1141 }
1142 else
1143 {
1144 Nucleus::createBr(endBlock);
1145 Nucleus::setInsertBlock(falseBlock);
1146 }
1147
1148 ifFalseBlock[ifDepth] = endBlock;
1149
1150 ifDepth++;
1151 }
1152
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001153 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001154 {
1155 ifDepth--;
1156
1157 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1158
1159 Nucleus::createBr(endBlock);
1160 Nucleus::setInsertBlock(endBlock);
1161
1162 if(isConditionalIf[ifDepth])
1163 {
1164 breakDepth--;
1165 r.enableIndex--;
1166 }
1167 }
1168
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001169 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001170 {
1171 loopRepDepth--;
1172
1173 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1174
1175 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1176 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1177
1178 Nucleus::createBr(testBlock);
1179 Nucleus::setInsertBlock(endBlock);
1180
1181 r.loopDepth--;
1182 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1183 }
1184
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001185 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001186 {
1187 loopRepDepth--;
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::ENDWHILE()
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.enableIndex--;
1210 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1211 whileTest = false;
1212 }
1213
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001214 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001215 {
John Bauman19bac1e2014-05-06 15:23:49 -04001216 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001217 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001218 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001219 }
John Bauman19bac1e2014-05-06 15:23:49 -04001220 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001221 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001222 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001223 }
John Bauman19bac1e2014-05-06 15:23:49 -04001224 else
1225 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001226 Int4 condition = As<Int4>(fetchRegisterF(src).x);
1227 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001228 }
John Bauman89401822014-05-06 15:04:28 -04001229 }
1230
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001231 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001232 {
1233 ASSERT(ifDepth < 24 + 4);
1234
1235 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1236
John Bauman19bac1e2014-05-06 15:23:49 -04001237 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001238 {
John Bauman19bac1e2014-05-06 15:23:49 -04001239 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001240 }
1241
1242 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1243 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1244
1245 branch(condition, trueBlock, falseBlock);
1246
1247 isConditionalIf[ifDepth] = false;
1248 ifFalseBlock[ifDepth] = falseBlock;
1249
1250 ifDepth++;
1251 }
1252
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001253 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001254 {
1255 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1256
John Bauman19bac1e2014-05-06 15:23:49 -04001257 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001258 {
1259 condition = ~condition;
1260 }
1261
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001262 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001263 }
1264
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001265 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001266 {
1267 Int4 condition;
1268
1269 switch(control)
1270 {
John Bauman19bac1e2014-05-06 15:23:49 -04001271 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1272 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1273 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1274 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1275 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1276 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001277 default:
1278 ASSERT(false);
1279 }
1280
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001281 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001282 }
1283
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001284 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001285 {
John Bauman89401822014-05-06 15:04:28 -04001286 condition &= r.enableStack[r.enableIndex];
1287
1288 r.enableIndex++;
1289 r.enableStack[r.enableIndex] = condition;
1290
1291 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1292 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1293
John Bauman19bac1e2014-05-06 15:23:49 -04001294 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001295
1296 branch(notAllFalse, trueBlock, falseBlock);
1297
1298 isConditionalIf[ifDepth] = true;
1299 ifFalseBlock[ifDepth] = falseBlock;
1300
1301 ifDepth++;
1302 breakDepth++;
1303 }
1304
1305 void VertexProgram::LABEL(int labelIndex)
1306 {
John Bauman19bac1e2014-05-06 15:23:49 -04001307 if(!labelBlock[labelIndex])
1308 {
1309 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1310 }
1311
John Bauman89401822014-05-06 15:04:28 -04001312 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001313 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001314 }
1315
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001316 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001317 {
1318 r.loopDepth++;
1319
1320 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1321 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1322 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1323
1324 // FIXME: Compiles to two instructions?
1325 If(r.increment[r.loopDepth] == 0)
1326 {
1327 r.increment[r.loopDepth] = 1;
1328 }
1329
1330 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1331 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1332 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1333
1334 loopRepTestBlock[loopRepDepth] = testBlock;
1335 loopRepEndBlock[loopRepDepth] = endBlock;
1336
1337 // FIXME: jump(testBlock)
1338 Nucleus::createBr(testBlock);
1339 Nucleus::setInsertBlock(testBlock);
1340
1341 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1342 Nucleus::setInsertBlock(loopBlock);
1343
1344 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001345
John Bauman89401822014-05-06 15:04:28 -04001346 loopRepDepth++;
1347 breakDepth = 0;
1348 }
1349
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001350 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001351 {
1352 r.loopDepth++;
1353
1354 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1355 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1356
1357 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1358 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1359 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1360
1361 loopRepTestBlock[loopRepDepth] = testBlock;
1362 loopRepEndBlock[loopRepDepth] = endBlock;
1363
1364 // FIXME: jump(testBlock)
1365 Nucleus::createBr(testBlock);
1366 Nucleus::setInsertBlock(testBlock);
1367
1368 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1369 Nucleus::setInsertBlock(loopBlock);
1370
1371 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1372
1373 loopRepDepth++;
1374 breakDepth = 0;
1375 }
1376
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001377 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001378 {
1379 r.enableIndex++;
1380
1381 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1382 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1383 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001384
John Bauman19bac1e2014-05-06 15:23:49 -04001385 loopRepTestBlock[loopRepDepth] = testBlock;
1386 loopRepEndBlock[loopRepDepth] = endBlock;
1387
1388 Int4 restoreBreak = r.enableBreak;
1389 Int4 restoreContinue = r.enableContinue;
1390
1391 // FIXME: jump(testBlock)
1392 Nucleus::createBr(testBlock);
1393 Nucleus::setInsertBlock(testBlock);
1394 r.enableContinue = restoreContinue;
1395
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001396 const Vector4f &src = fetchRegisterF(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001397 Int4 condition = As<Int4>(src.x);
1398 condition &= r.enableStack[r.enableIndex - 1];
1399 r.enableStack[r.enableIndex] = condition;
1400
1401 Bool notAllFalse = SignMask(condition) != 0;
1402 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001403
John Bauman19bac1e2014-05-06 15:23:49 -04001404 Nucleus::setInsertBlock(endBlock);
1405 r.enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001406
John Bauman19bac1e2014-05-06 15:23:49 -04001407 Nucleus::setInsertBlock(loopBlock);
1408
1409 loopRepDepth++;
1410 breakDepth = 0;
1411 }
1412
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001413 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001414 {
John Bauman19bac1e2014-05-06 15:23:49 -04001415 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001416 {
1417 returnBlock = Nucleus::createBasicBlock();
1418 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001419 }
1420 else
1421 {
John Bauman89401822014-05-06 15:04:28 -04001422 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001423
John Bauman19bac1e2014-05-06 15:23:49 -04001424 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001425 {
John Bauman19bac1e2014-05-06 15:23:49 -04001426 // FIXME: Encapsulate
1427 UInt index = r.callStack[--r.stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001428
John Bauman66b8ab22014-05-06 15:57:45 -04001429 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001430 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1431
1432 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1433 {
1434 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1435 }
1436 }
1437 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1438 {
1439 Nucleus::createBr(callRetBlock[currentLabel][0]);
1440 }
1441 else // Function isn't called
1442 {
1443 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001444 }
1445
1446 Nucleus::setInsertBlock(unreachableBlock);
1447 Nucleus::createUnreachable();
1448 }
1449 }
1450
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001451 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001452 {
John Bauman19bac1e2014-05-06 15:23:49 -04001453 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001454
John Bauman19bac1e2014-05-06 15:23:49 -04001455 // FIXME: Return from function if all instances left
1456 // FIXME: Use enableLeave in other control-flow constructs
1457 }
John Bauman89401822014-05-06 15:04:28 -04001458
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001459 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001460 {
1461 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001462 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001463
1464 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1465 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1466 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1467 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1468 }
John Bauman19bac1e2014-05-06 15:23:49 -04001469
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001470 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001471 {
1472 Float4 lod = Float4(0.0f);
1473 Vector4f tmp;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001474 sampleTexture(tmp, src1, src0.x, src0.y, src0.z, lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001475
1476 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1477 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1478 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1479 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1480 }
1481
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001482 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001483 {
1484 UNIMPLEMENTED();
1485 }
1486
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001487 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001488 {
1489 UNIMPLEMENTED();
1490 }
1491
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001492 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001493 {
1494 UNIMPLEMENTED();
1495 }
1496
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001497 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001498 {
1499 UNIMPLEMENTED();
1500 }
1501
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001502 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001503 {
1504 UNIMPLEMENTED();
1505 }
1506
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001507 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001508 {
1509 UNIMPLEMENTED();
1510 }
1511
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001512 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001513 {
1514 Pointer<Byte> textureMipmap = r.data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
1515 for(int i = 0; i < 4; ++i)
1516 {
1517 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1518 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1519 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1520 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1521 }
1522 }
1523
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001524 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
John Bauman19bac1e2014-05-06 15:23:49 -04001525 {
1526 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1527 {
1528 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001529 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
John Bauman19bac1e2014-05-06 15:23:49 -04001530 }
1531 else
1532 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001533 Int index = As<Int>(Float(fetchRegisterF(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001534
1535 for(int i = 0; i < 16; i++)
1536 {
1537 if(shader->usesSampler(i))
1538 {
1539 If(index == i)
1540 {
1541 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1542 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1543 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1544 }
1545 }
1546 }
1547 }
1548 }
John Bauman89401822014-05-06 15:04:28 -04001549}