blob: 40c09105752b92ffdcfa2fe7f3f92341f0746eb9 [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
45 void VertexProgram::pipeline(Registers &r)
46 {
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 {
John Bauman19bac1e2014-05-06 15:23:49 -040054 program(r);
John Bauman89401822014-05-06 15:04:28 -040055 }
56 else
57 {
58 passThrough(r);
59 }
60 }
61
John Bauman19bac1e2014-05-06 15:23:49 -040062 void VertexProgram::program(Registers &r)
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 }
90
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
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400120 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(r, src0);
121 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(r, src1);
122 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(r, src2);
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400123 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegisterF(r, src3);
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400124 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegisterF(r, src4);
John Bauman89401822014-05-06 15:04:28 -0400125
126 switch(opcode)
127 {
John Bauman19bac1e2014-05-06 15:23:49 -0400128 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;
John Bauman19bac1e2014-05-06 15:23:49 -0400143 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;
John Bauman19bac1e2014-05-06 15:23:49 -0400163 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;
169 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;
Alexis Hetu8e851c12015-06-04 11:30:54 -0400172 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400173 case Shader::OPCODE_CEIL: ceil(d, s0); break;
174 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;
John Bauman19bac1e2014-05-06 15:23:49 -0400186 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
187 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
188 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
189 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
190 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
191 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400192 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400193 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;
John Bauman19bac1e2014-05-06 15:23:49 -0400196 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;
John Bauman19bac1e2014-05-06 15:23:49 -0400199 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;
John Bauman19bac1e2014-05-06 15:23:49 -0400203 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;
211 case Shader::OPCODE_U2B: u2b(d, s0); break;
212 case Shader::OPCODE_B2U: b2u(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400213 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400214 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400215 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
216 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
217 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
218 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
219 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
220 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
221 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400222 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
223 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400224 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400225 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
226 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
227 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
228 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
229 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400230 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
231 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
232 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
233 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
234 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
235 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
236 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
237 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
238 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
239 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
240 case Shader::OPCODE_SGE: step(d, s1, s0); break;
241 case Shader::OPCODE_SGN: sgn(d, s0); break;
242 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
243 case Shader::OPCODE_COS: cos(d, s0, pp); break;
244 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
245 case Shader::OPCODE_TAN: tan(d, s0); break;
246 case Shader::OPCODE_ACOS: acos(d, s0); break;
247 case Shader::OPCODE_ASIN: asin(d, s0); break;
248 case Shader::OPCODE_ATAN: atan(d, s0); break;
249 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400250 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
251 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
252 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
253 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
254 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
255 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400256 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
257 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400258 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400259 case Shader::OPCODE_BREAK: BREAK(r); break;
260 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
261 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
262 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
263 case Shader::OPCODE_TEST: TEST(); break;
264 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
265 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
266 case Shader::OPCODE_ELSE: ELSE(r); break;
267 case Shader::OPCODE_ENDIF: ENDIF(r); break;
268 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
269 case Shader::OPCODE_ENDREP: ENDREP(r); break;
270 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
271 case Shader::OPCODE_IF: IF(r, src0); break;
272 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
273 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
274 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
275 case Shader::OPCODE_REP: REP(r, src0); break;
276 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
277 case Shader::OPCODE_RET: RET(r); break;
278 case Shader::OPCODE_LEAVE: LEAVE(r); break;
279 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
280 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400281 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400282 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
283 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
284 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
285 case Shader::OPCODE_ALL: all(d.x, s0); break;
286 case Shader::OPCODE_ANY: any(d.x, s0); break;
287 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400288 case Shader::OPCODE_OR: or(d, s0, s1); break;
289 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
290 case Shader::OPCODE_AND: and(d, s0, s1); break;
291 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
292 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400293 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1); break;
294 case Shader::OPCODE_TEX: TEX(r, d, s0, src1); break;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400295 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(r, d, s0, src1, s2, s3); break;
296 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(r, d, s0, src1, s2); break;
297 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(r, d, s0, src1, s2); break;
298 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(r, d, s0, src1, s2, s3); break;
299 case Shader::OPCODE_TEXGRAD: TEXGRAD(r, d, s0, src1, s2, s3); break;
300 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(r, d, s0, src1, s2, s3, s4); break;
Alexis Hetu9bcb31d2015-07-22 17:03:26 -0400301 case Shader::OPCODE_TEXSIZE: TEXSIZE(r, d, s0.x, src1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400302 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400303 default:
304 ASSERT(false);
305 }
306
John Bauman19bac1e2014-05-06 15:23:49 -0400307 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400308 {
John Bauman19bac1e2014-05-06 15:23:49 -0400309 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400310 {
John Bauman19bac1e2014-05-06 15:23:49 -0400311 switch(opcode)
312 {
313 case Shader::OPCODE_DIV:
314 if(dst.x) d.x = Trunc(d.x);
315 if(dst.y) d.y = Trunc(d.y);
316 if(dst.z) d.z = Trunc(d.z);
317 if(dst.w) d.w = Trunc(d.w);
318 break;
319 default:
320 break; // No truncation to integer required when arguments are integer
321 }
John Bauman89401822014-05-06 15:04:28 -0400322 }
323
John Bauman19bac1e2014-05-06 15:23:49 -0400324 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400325 {
John Bauman19bac1e2014-05-06 15:23:49 -0400326 if(dst.x) d.x = Max(d.x, Float4(0.0f));
327 if(dst.y) d.y = Max(d.y, Float4(0.0f));
328 if(dst.z) d.z = Max(d.z, Float4(0.0f));
329 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400330
John Bauman19bac1e2014-05-06 15:23:49 -0400331 if(dst.x) d.x = Min(d.x, Float4(1.0f));
332 if(dst.y) d.y = Min(d.y, Float4(1.0f));
333 if(dst.z) d.z = Min(d.z, Float4(1.0f));
334 if(dst.w) d.w = Min(d.w, Float4(1.0f));
335 }
336
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400337 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400338 {
339 Vector4f pDst; // FIXME: Rename
340
341 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400342 {
John Bauman19bac1e2014-05-06 15:23:49 -0400343 case Shader::PARAMETER_VOID: break;
344 case Shader::PARAMETER_TEMP:
345 if(dst.rel.type == Shader::PARAMETER_VOID)
346 {
347 if(dst.x) pDst.x = r.r[dst.index].x;
348 if(dst.y) pDst.y = r.r[dst.index].y;
349 if(dst.z) pDst.z = r.r[dst.index].z;
350 if(dst.w) pDst.w = r.r[dst.index].w;
351 }
352 else
353 {
354 Int a = relativeAddress(r, dst);
355
356 if(dst.x) pDst.x = r.r[dst.index + a].x;
357 if(dst.y) pDst.y = r.r[dst.index + a].y;
358 if(dst.z) pDst.z = r.r[dst.index + a].z;
359 if(dst.w) pDst.w = r.r[dst.index + a].w;
360 }
361 break;
362 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
363 case Shader::PARAMETER_RASTOUT:
364 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400365 {
366 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400367 if(dst.x) pDst.x = r.o[Pos].x;
368 if(dst.y) pDst.y = r.o[Pos].y;
369 if(dst.z) pDst.z = r.o[Pos].z;
370 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400371 break;
372 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400373 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400374 break;
375 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400376 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400377 break;
378 default:
379 ASSERT(false);
380 }
381 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400382 case Shader::PARAMETER_ATTROUT:
383 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
384 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
385 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
386 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400387 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400388 case Shader::PARAMETER_TEXCRDOUT:
389 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400390 if(version < 0x0300)
391 {
John Bauman19bac1e2014-05-06 15:23:49 -0400392 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
393 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
394 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
395 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400396 }
397 else
398 {
John Bauman19bac1e2014-05-06 15:23:49 -0400399 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400400 {
John Bauman19bac1e2014-05-06 15:23:49 -0400401 if(dst.x) pDst.x = r.o[dst.index].x;
402 if(dst.y) pDst.y = r.o[dst.index].y;
403 if(dst.z) pDst.z = r.o[dst.index].z;
404 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400405 }
John Bauman19bac1e2014-05-06 15:23:49 -0400406 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400407 {
408 Int aL = r.aL[r.loopDepth];
409
John Bauman19bac1e2014-05-06 15:23:49 -0400410 if(dst.x) pDst.x = r.o[dst.index + aL].x;
411 if(dst.y) pDst.y = r.o[dst.index + aL].y;
412 if(dst.z) pDst.z = r.o[dst.index + aL].z;
413 if(dst.w) pDst.w = r.o[dst.index + aL].w;
414 }
415 else
416 {
417 Int a = relativeAddress(r, dst);
418
419 if(dst.x) pDst.x = r.o[dst.index + a].x;
420 if(dst.y) pDst.y = r.o[dst.index + a].y;
421 if(dst.z) pDst.z = r.o[dst.index + a].z;
422 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400423 }
424 }
425 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400426 case Shader::PARAMETER_LABEL: break;
427 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
428 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400429 default:
430 ASSERT(false);
431 }
432
John Bauman19bac1e2014-05-06 15:23:49 -0400433 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -0400434
435 Int4 xEnable = enable;
436 Int4 yEnable = enable;
437 Int4 zEnable = enable;
438 Int4 wEnable = enable;
439
440 if(predicate)
441 {
John Bauman19bac1e2014-05-06 15:23:49 -0400442 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400443
444 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
445 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
446 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
447 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
448
John Bauman19bac1e2014-05-06 15:23:49 -0400449 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400450 {
John Bauman19bac1e2014-05-06 15:23:49 -0400451 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
452 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
453 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
454 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400455 }
456 else
457 {
John Bauman19bac1e2014-05-06 15:23:49 -0400458 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
459 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
460 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
461 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400462 }
463 }
464
John Bauman19bac1e2014-05-06 15:23:49 -0400465 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
466 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
467 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
468 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400469
John Bauman19bac1e2014-05-06 15:23:49 -0400470 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
471 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
472 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
473 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400474 }
475
John Bauman19bac1e2014-05-06 15:23:49 -0400476 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400477 {
John Bauman19bac1e2014-05-06 15:23:49 -0400478 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400479 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400480 case Shader::PARAMETER_TEMP:
481 if(dst.rel.type == Shader::PARAMETER_VOID)
482 {
483 if(dst.x) r.r[dst.index].x = d.x;
484 if(dst.y) r.r[dst.index].y = d.y;
485 if(dst.z) r.r[dst.index].z = d.z;
486 if(dst.w) r.r[dst.index].w = d.w;
487 }
488 else
489 {
490 Int a = relativeAddress(r, dst);
491
492 if(dst.x) r.r[dst.index + a].x = d.x;
493 if(dst.y) r.r[dst.index + a].y = d.y;
494 if(dst.z) r.r[dst.index + a].z = d.z;
495 if(dst.w) r.r[dst.index + a].w = d.w;
496 }
John Bauman89401822014-05-06 15:04:28 -0400497 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400498 case Shader::PARAMETER_ADDR:
499 if(dst.x) r.a0.x = d.x;
500 if(dst.y) r.a0.y = d.y;
501 if(dst.z) r.a0.z = d.z;
502 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400503 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400504 case Shader::PARAMETER_RASTOUT:
505 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400506 {
507 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400508 if(dst.x) r.o[Pos].x = d.x;
509 if(dst.y) r.o[Pos].y = d.y;
510 if(dst.z) r.o[Pos].z = d.z;
511 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400512 break;
513 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400514 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400515 break;
516 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400517 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400518 break;
519 default: ASSERT(false);
520 }
521 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400522 case Shader::PARAMETER_ATTROUT:
523 if(dst.x) r.o[D0 + dst.index].x = d.x;
524 if(dst.y) r.o[D0 + dst.index].y = d.y;
525 if(dst.z) r.o[D0 + dst.index].z = d.z;
526 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400527 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400528 case Shader::PARAMETER_TEXCRDOUT:
529 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400530 if(version < 0x0300)
531 {
John Bauman19bac1e2014-05-06 15:23:49 -0400532 if(dst.x) r.o[T0 + dst.index].x = d.x;
533 if(dst.y) r.o[T0 + dst.index].y = d.y;
534 if(dst.z) r.o[T0 + dst.index].z = d.z;
535 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400536 }
537 else
538 {
John Bauman19bac1e2014-05-06 15:23:49 -0400539 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400540 {
John Bauman19bac1e2014-05-06 15:23:49 -0400541 if(dst.x) r.o[dst.index].x = d.x;
542 if(dst.y) r.o[dst.index].y = d.y;
543 if(dst.z) r.o[dst.index].z = d.z;
544 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400545 }
John Bauman19bac1e2014-05-06 15:23:49 -0400546 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400547 {
548 Int aL = r.aL[r.loopDepth];
549
John Bauman19bac1e2014-05-06 15:23:49 -0400550 if(dst.x) r.o[dst.index + aL].x = d.x;
551 if(dst.y) r.o[dst.index + aL].y = d.y;
552 if(dst.z) r.o[dst.index + aL].z = d.z;
553 if(dst.w) r.o[dst.index + aL].w = d.w;
554 }
555 else
556 {
557 Int a = relativeAddress(r, dst);
558
559 if(dst.x) r.o[dst.index + a].x = d.x;
560 if(dst.y) r.o[dst.index + a].y = d.y;
561 if(dst.z) r.o[dst.index + a].z = d.z;
562 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400563 }
564 }
565 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400566 case Shader::PARAMETER_LABEL: break;
567 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
568 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400569 default:
570 ASSERT(false);
571 }
572 }
573 }
574
John Bauman19bac1e2014-05-06 15:23:49 -0400575 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400576 {
577 Nucleus::setInsertBlock(returnBlock);
578 }
579 }
580
581 void VertexProgram::passThrough(Registers &r)
582 {
John Bauman19bac1e2014-05-06 15:23:49 -0400583 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400584 {
585 for(int i = 0; i < 12; i++)
586 {
John Bauman19bac1e2014-05-06 15:23:49 -0400587 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400588
589 switch(usage)
590 {
591 case 0xFF:
592 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400593 case Shader::USAGE_PSIZE:
594 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400595 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400596 case Shader::USAGE_TEXCOORD:
597 r.o[i].x = r.v[i].x;
598 r.o[i].y = r.v[i].y;
599 r.o[i].z = r.v[i].z;
600 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400601 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400602 case Shader::USAGE_POSITION:
603 r.o[i].x = r.v[i].x;
604 r.o[i].y = r.v[i].y;
605 r.o[i].z = r.v[i].z;
606 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400607 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400608 case Shader::USAGE_COLOR:
609 r.o[i].x = r.v[i].x;
610 r.o[i].y = r.v[i].y;
611 r.o[i].z = r.v[i].z;
612 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400613 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400614 case Shader::USAGE_FOG:
615 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400616 break;
617 default:
618 ASSERT(false);
619 }
620 }
621 }
622 else
623 {
John Bauman19bac1e2014-05-06 15:23:49 -0400624 r.o[Pos].x = r.v[PositionT].x;
625 r.o[Pos].y = r.v[PositionT].y;
626 r.o[Pos].z = r.v[PositionT].z;
627 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400628
629 for(int i = 0; i < 2; i++)
630 {
John Bauman19bac1e2014-05-06 15:23:49 -0400631 r.o[D0 + i].x = r.v[Color0 + i].x;
632 r.o[D0 + i].y = r.v[Color0 + i].y;
633 r.o[D0 + i].z = r.v[Color0 + i].z;
634 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400635 }
636
637 for(int i = 0; i < 8; i++)
638 {
John Bauman19bac1e2014-05-06 15:23:49 -0400639 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
640 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
641 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
642 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400643 }
644
John Bauman66b8ab22014-05-06 15:57:45 -0400645 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400646 }
647 }
648
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400649 Vector4f VertexProgram::fetchRegisterF(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400650 {
651 int i = src.index + offset;
652
John Bauman19bac1e2014-05-06 15:23:49 -0400653 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -0400654
John Bauman89401822014-05-06 15:04:28 -0400655 switch(src.type)
656 {
John Bauman19bac1e2014-05-06 15:23:49 -0400657 case Shader::PARAMETER_TEMP:
658 if(src.rel.type == Shader::PARAMETER_VOID)
659 {
660 reg = r.r[i];
661 }
662 else
663 {
664 reg = r.r[i + relativeAddress(r, src)];
665 }
666 break;
667 case Shader::PARAMETER_CONST:
668 reg = readConstant(r, src, offset);
669 break;
670 case Shader::PARAMETER_INPUT:
671 if(src.rel.type == Shader::PARAMETER_VOID)
672 {
673 reg = r.v[i];
674 }
675 else
676 {
677 reg = r.v[i + relativeAddress(r, src)];
678 }
679 break;
680 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
681 case Shader::PARAMETER_FLOAT4LITERAL:
682 reg.x = Float4(src.value[0]);
683 reg.y = Float4(src.value[1]);
684 reg.z = Float4(src.value[2]);
685 reg.w = Float4(src.value[3]);
686 break;
687 case Shader::PARAMETER_ADDR: reg = r.a0; break;
688 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
689 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
690 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
691 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
692 case Shader::PARAMETER_SAMPLER:
693 if(src.rel.type == Shader::PARAMETER_VOID)
694 {
695 reg.x = As<Float4>(Int4(i));
696 }
697 else if(src.rel.type == Shader::PARAMETER_TEMP)
698 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400699 reg.x = As<Float4>(Int4(i) + As<Int4>(r.r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400700 }
701 return reg;
702 case Shader::PARAMETER_OUTPUT:
703 if(src.rel.type == Shader::PARAMETER_VOID)
704 {
705 reg = r.o[i];
706 }
707 else
708 {
709 reg = r.o[i + relativeAddress(r, src)];
710 }
711 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400712 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400713 reg.x = As<Float>(Int(r.instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400714 return reg;
John Bauman89401822014-05-06 15:04:28 -0400715 default:
716 ASSERT(false);
717 }
718
John Bauman66b8ab22014-05-06 15:57:45 -0400719 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
720 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
721 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
722 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400723
John Bauman66b8ab22014-05-06 15:57:45 -0400724 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400725
726 switch(src.modifier)
727 {
John Bauman19bac1e2014-05-06 15:23:49 -0400728 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400729 mod.x = x;
730 mod.y = y;
731 mod.z = z;
732 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400733 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400734 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400735 mod.x = -x;
736 mod.y = -y;
737 mod.z = -z;
738 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400739 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400740 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400741 mod.x = Abs(x);
742 mod.y = Abs(y);
743 mod.z = Abs(z);
744 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400745 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400746 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400747 mod.x = -Abs(x);
748 mod.y = -Abs(y);
749 mod.z = -Abs(z);
750 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400751 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400752 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400753 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
754 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
755 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
756 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400757 break;
758 default:
759 ASSERT(false);
760 }
761
762 return mod;
763 }
764
John Bauman19bac1e2014-05-06 15:23:49 -0400765 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400766 {
John Bauman19bac1e2014-05-06 15:23:49 -0400767 Vector4f c;
768
769 int i = src.index + offset;
770
771 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
772 {
773 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
774
775 c.x = c.x.xxxx;
776 c.y = c.y.yyyy;
777 c.z = c.z.zzzz;
778 c.w = c.w.wwww;
779
Nicolas Capenseafdb222015-05-15 15:24:08 -0400780 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400781 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500782 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400783 {
784 const Shader::Instruction &instruction = *shader->getInstruction(j);
785
786 if(instruction.opcode == Shader::OPCODE_DEF)
787 {
788 if(instruction.dst.index == i)
789 {
790 c.x = Float4(instruction.src[0].value[0]);
791 c.y = Float4(instruction.src[0].value[1]);
792 c.z = Float4(instruction.src[0].value[2]);
793 c.w = Float4(instruction.src[0].value[3]);
794
795 break;
796 }
797 }
798 }
799 }
800 }
801 else if(src.rel.type == Shader::PARAMETER_LOOP)
802 {
803 Int loopCounter = r.aL[r.loopDepth];
804
805 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
806
807 c.x = c.x.xxxx;
808 c.y = c.y.yyyy;
809 c.z = c.z.zzzz;
810 c.w = c.w.wwww;
811 }
812 else
813 {
814 if(src.rel.deterministic)
815 {
816 Int a = relativeAddress(r, src);
817
818 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
819
820 c.x = c.x.xxxx;
821 c.y = c.y.yyyy;
822 c.z = c.z.zzzz;
823 c.w = c.w.wwww;
824 }
825 else
826 {
827 int component = src.rel.swizzle & 0x03;
828 Float4 a;
829
830 switch(src.rel.type)
831 {
832 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
833 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
834 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
835 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400836 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 -0400837 default: ASSERT(false);
838 }
839
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400840 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400841
842 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
843
844 Int index0 = Extract(index, 0);
845 Int index1 = Extract(index, 1);
846 Int index2 = Extract(index, 2);
847 Int index3 = Extract(index, 3);
848
849 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
850 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
851 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
852 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
853
854 transpose4x4(c.x, c.y, c.z, c.w);
855 }
856 }
857
858 return c;
859 }
860
861 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
862 {
863 ASSERT(var.rel.deterministic);
864
865 if(var.rel.type == Shader::PARAMETER_TEMP)
866 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400867 return As<Int>(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400868 }
869 else if(var.rel.type == Shader::PARAMETER_INPUT)
870 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400871 return As<Int>(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400872 }
873 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
874 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400875 return As<Int>(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400876 }
877 else if(var.rel.type == Shader::PARAMETER_CONST)
878 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400879 RValue<Int4> c = *Pointer<Int4>(r.data + OFFSET(DrawData, vs.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -0400880
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400881 return Extract(c, 0) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400882 }
883 else ASSERT(false);
884
885 return 0;
886 }
887
888 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
889 {
890 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -0400891
892 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400893 {
John Baumand4ae8632014-05-06 16:18:33 -0400894 if(shader->containsBreakInstruction() && instruction->analysisBreak)
895 {
896 enable &= r.enableBreak;
897 }
John Bauman19bac1e2014-05-06 15:23:49 -0400898
John Baumand4ae8632014-05-06 16:18:33 -0400899 if(shader->containsContinueInstruction() && instruction->analysisContinue)
900 {
901 enable &= r.enableContinue;
902 }
John Bauman19bac1e2014-05-06 15:23:49 -0400903
John Baumand4ae8632014-05-06 16:18:33 -0400904 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
905 {
906 enable &= r.enableLeave;
907 }
John Bauman19bac1e2014-05-06 15:23:49 -0400908 }
909
910 return enable;
911 }
912
913 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
914 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400915 Vector4f row0 = fetchRegisterF(r, src1, 0);
916 Vector4f row1 = fetchRegisterF(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400917
918 dst.x = dot3(src0, row0);
919 dst.y = dot3(src0, row1);
920 }
921
John Bauman19bac1e2014-05-06 15:23:49 -0400922 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400923 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400924 Vector4f row0 = fetchRegisterF(r, src1, 0);
925 Vector4f row1 = fetchRegisterF(r, src1, 1);
926 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400927
928 dst.x = dot3(src0, row0);
929 dst.y = dot3(src0, row1);
930 dst.z = dot3(src0, row2);
931 }
932
John Bauman19bac1e2014-05-06 15:23:49 -0400933 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400934 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400935 Vector4f row0 = fetchRegisterF(r, src1, 0);
936 Vector4f row1 = fetchRegisterF(r, src1, 1);
937 Vector4f row2 = fetchRegisterF(r, src1, 2);
938 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400939
940 dst.x = dot3(src0, row0);
941 dst.y = dot3(src0, row1);
942 dst.z = dot3(src0, row2);
943 dst.w = dot3(src0, row3);
944 }
945
John Bauman19bac1e2014-05-06 15:23:49 -0400946 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400947 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400948 Vector4f row0 = fetchRegisterF(r, src1, 0);
949 Vector4f row1 = fetchRegisterF(r, src1, 1);
950 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400951
952 dst.x = dot4(src0, row0);
953 dst.y = dot4(src0, row1);
954 dst.z = dot4(src0, row2);
955 }
956
John Bauman19bac1e2014-05-06 15:23:49 -0400957 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400958 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400959 Vector4f row0 = fetchRegisterF(r, src1, 0);
960 Vector4f row1 = fetchRegisterF(r, src1, 1);
961 Vector4f row2 = fetchRegisterF(r, src1, 2);
962 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400963
964 dst.x = dot4(src0, row0);
965 dst.y = dot4(src0, row1);
966 dst.z = dot4(src0, row2);
967 dst.w = dot4(src0, row3);
968 }
969
970 void VertexProgram::BREAK(Registers &r)
971 {
972 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
973 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
974
975 if(breakDepth == 0)
976 {
John Bauman19bac1e2014-05-06 15:23:49 -0400977 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400978 Nucleus::createBr(endBlock);
979 }
980 else
981 {
982 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
983 Bool allBreak = SignMask(r.enableBreak) == 0x0;
984
John Bauman19bac1e2014-05-06 15:23:49 -0400985 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400986 branch(allBreak, endBlock, deadBlock);
987 }
988
989 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400990 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400991 }
992
John Bauman19bac1e2014-05-06 15:23:49 -0400993 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400994 {
995 Int4 condition;
996
997 switch(control)
998 {
John Bauman19bac1e2014-05-06 15:23:49 -0400999 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1000 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1001 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1002 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1003 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1004 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001005 default:
1006 ASSERT(false);
1007 }
1008
John Bauman19bac1e2014-05-06 15:23:49 -04001009 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001010 }
1011
1012 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
1013 {
1014 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1015
John Bauman19bac1e2014-05-06 15:23:49 -04001016 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001017 {
1018 condition = ~condition;
1019 }
1020
John Bauman19bac1e2014-05-06 15:23:49 -04001021 BREAK(r, condition);
1022 }
1023
1024 void VertexProgram::BREAK(Registers &r, Int4 &condition)
1025 {
John Bauman89401822014-05-06 15:04:28 -04001026 condition &= r.enableStack[r.enableIndex];
1027
1028 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1029 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1030
1031 r.enableBreak = r.enableBreak & ~condition;
1032 Bool allBreak = SignMask(r.enableBreak) == 0x0;
1033
John Bauman19bac1e2014-05-06 15:23:49 -04001034 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001035 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001036
John Bauman89401822014-05-06 15:04:28 -04001037 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001038 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001039 }
1040
John Bauman19bac1e2014-05-06 15:23:49 -04001041 void VertexProgram::CONTINUE(Registers &r)
1042 {
1043 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1044 }
1045
1046 void VertexProgram::TEST()
1047 {
1048 whileTest = true;
1049 }
1050
1051 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001052 {
1053 if(!labelBlock[labelIndex])
1054 {
1055 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1056 }
1057
John Bauman19bac1e2014-05-06 15:23:49 -04001058 if(callRetBlock[labelIndex].size() > 1)
1059 {
1060 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1061 }
John Bauman89401822014-05-06 15:04:28 -04001062
John Bauman19bac1e2014-05-06 15:23:49 -04001063 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001064
1065 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001066 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1067
1068 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001069 }
1070
John Bauman19bac1e2014-05-06 15:23:49 -04001071 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001072 {
John Bauman19bac1e2014-05-06 15:23:49 -04001073 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001074 {
John Bauman19bac1e2014-05-06 15:23:49 -04001075 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001076 }
John Bauman19bac1e2014-05-06 15:23:49 -04001077 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001078 {
John Bauman19bac1e2014-05-06 15:23:49 -04001079 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001080 }
1081 else ASSERT(false);
1082 }
1083
John Bauman19bac1e2014-05-06 15:23:49 -04001084 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001085 {
1086 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1087
John Bauman19bac1e2014-05-06 15:23:49 -04001088 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001089 {
1090 condition = !condition;
1091 }
1092
1093 if(!labelBlock[labelIndex])
1094 {
1095 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1096 }
1097
John Bauman19bac1e2014-05-06 15:23:49 -04001098 if(callRetBlock[labelIndex].size() > 1)
1099 {
1100 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1101 }
John Bauman89401822014-05-06 15:04:28 -04001102
John Bauman19bac1e2014-05-06 15:23:49 -04001103 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001104
John Bauman19bac1e2014-05-06 15:23:49 -04001105 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1106 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1107
1108 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001109 }
1110
John Bauman19bac1e2014-05-06 15:23:49 -04001111 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001112 {
1113 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1114
John Bauman19bac1e2014-05-06 15:23:49 -04001115 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001116 {
1117 condition = ~condition;
1118 }
1119
1120 condition &= r.enableStack[r.enableIndex];
1121
1122 if(!labelBlock[labelIndex])
1123 {
1124 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1125 }
1126
John Bauman19bac1e2014-05-06 15:23:49 -04001127 if(callRetBlock[labelIndex].size() > 1)
1128 {
1129 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1130 }
John Bauman89401822014-05-06 15:04:28 -04001131
1132 r.enableIndex++;
1133 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001134 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001135
John Bauman19bac1e2014-05-06 15:23:49 -04001136 Bool notAllFalse = SignMask(condition) != 0;
1137 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1138 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001139
1140 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001141 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001142 }
1143
1144 void VertexProgram::ELSE(Registers &r)
1145 {
1146 ifDepth--;
1147
1148 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1149 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1150
1151 if(isConditionalIf[ifDepth])
1152 {
1153 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001154 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001155
1156 branch(notAllFalse, falseBlock, endBlock);
1157
1158 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1159 }
1160 else
1161 {
1162 Nucleus::createBr(endBlock);
1163 Nucleus::setInsertBlock(falseBlock);
1164 }
1165
1166 ifFalseBlock[ifDepth] = endBlock;
1167
1168 ifDepth++;
1169 }
1170
1171 void VertexProgram::ENDIF(Registers &r)
1172 {
1173 ifDepth--;
1174
1175 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1176
1177 Nucleus::createBr(endBlock);
1178 Nucleus::setInsertBlock(endBlock);
1179
1180 if(isConditionalIf[ifDepth])
1181 {
1182 breakDepth--;
1183 r.enableIndex--;
1184 }
1185 }
1186
John Bauman89401822014-05-06 15:04:28 -04001187 void VertexProgram::ENDLOOP(Registers &r)
1188 {
1189 loopRepDepth--;
1190
1191 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1192
1193 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1194 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1195
1196 Nucleus::createBr(testBlock);
1197 Nucleus::setInsertBlock(endBlock);
1198
1199 r.loopDepth--;
1200 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1201 }
1202
John Bauman19bac1e2014-05-06 15:23:49 -04001203 void VertexProgram::ENDREP(Registers &r)
1204 {
1205 loopRepDepth--;
1206
1207 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1208 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1209
1210 Nucleus::createBr(testBlock);
1211 Nucleus::setInsertBlock(endBlock);
1212
1213 r.loopDepth--;
1214 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1215 }
1216
1217 void VertexProgram::ENDWHILE(Registers &r)
1218 {
1219 loopRepDepth--;
1220
1221 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1222 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1223
1224 Nucleus::createBr(testBlock);
1225 Nucleus::setInsertBlock(endBlock);
1226
1227 r.enableIndex--;
1228 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1229 whileTest = false;
1230 }
1231
John Bauman89401822014-05-06 15:04:28 -04001232 void VertexProgram::IF(Registers &r, const Src &src)
1233 {
John Bauman19bac1e2014-05-06 15:23:49 -04001234 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001235 {
1236 IFb(r, src);
1237 }
John Bauman19bac1e2014-05-06 15:23:49 -04001238 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001239 {
1240 IFp(r, src);
1241 }
John Bauman19bac1e2014-05-06 15:23:49 -04001242 else
1243 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001244 Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
John Bauman19bac1e2014-05-06 15:23:49 -04001245 IF(r, condition);
1246 }
John Bauman89401822014-05-06 15:04:28 -04001247 }
1248
1249 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1250 {
1251 ASSERT(ifDepth < 24 + 4);
1252
1253 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1254
John Bauman19bac1e2014-05-06 15:23:49 -04001255 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001256 {
John Bauman19bac1e2014-05-06 15:23:49 -04001257 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001258 }
1259
1260 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1261 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1262
1263 branch(condition, trueBlock, falseBlock);
1264
1265 isConditionalIf[ifDepth] = false;
1266 ifFalseBlock[ifDepth] = falseBlock;
1267
1268 ifDepth++;
1269 }
1270
John Bauman19bac1e2014-05-06 15:23:49 -04001271 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001272 {
1273 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1274
John Bauman19bac1e2014-05-06 15:23:49 -04001275 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001276 {
1277 condition = ~condition;
1278 }
1279
John Bauman19bac1e2014-05-06 15:23:49 -04001280 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001281 }
1282
John Bauman19bac1e2014-05-06 15:23:49 -04001283 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001284 {
1285 Int4 condition;
1286
1287 switch(control)
1288 {
John Bauman19bac1e2014-05-06 15:23:49 -04001289 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1290 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1291 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1292 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1293 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1294 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001295 default:
1296 ASSERT(false);
1297 }
1298
John Bauman19bac1e2014-05-06 15:23:49 -04001299 IF(r, condition);
1300 }
1301
1302 void VertexProgram::IF(Registers &r, Int4 &condition)
1303 {
John Bauman89401822014-05-06 15:04:28 -04001304 condition &= r.enableStack[r.enableIndex];
1305
1306 r.enableIndex++;
1307 r.enableStack[r.enableIndex] = condition;
1308
1309 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1310 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1311
John Bauman19bac1e2014-05-06 15:23:49 -04001312 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001313
1314 branch(notAllFalse, trueBlock, falseBlock);
1315
1316 isConditionalIf[ifDepth] = true;
1317 ifFalseBlock[ifDepth] = falseBlock;
1318
1319 ifDepth++;
1320 breakDepth++;
1321 }
1322
1323 void VertexProgram::LABEL(int labelIndex)
1324 {
John Bauman19bac1e2014-05-06 15:23:49 -04001325 if(!labelBlock[labelIndex])
1326 {
1327 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1328 }
1329
John Bauman89401822014-05-06 15:04:28 -04001330 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001331 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001332 }
1333
1334 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1335 {
1336 r.loopDepth++;
1337
1338 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1339 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1340 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1341
1342 // FIXME: Compiles to two instructions?
1343 If(r.increment[r.loopDepth] == 0)
1344 {
1345 r.increment[r.loopDepth] = 1;
1346 }
1347
1348 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1349 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1350 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1351
1352 loopRepTestBlock[loopRepDepth] = testBlock;
1353 loopRepEndBlock[loopRepDepth] = endBlock;
1354
1355 // FIXME: jump(testBlock)
1356 Nucleus::createBr(testBlock);
1357 Nucleus::setInsertBlock(testBlock);
1358
1359 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1360 Nucleus::setInsertBlock(loopBlock);
1361
1362 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1363
1364 loopRepDepth++;
1365 breakDepth = 0;
1366 }
1367
1368 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1369 {
1370 r.loopDepth++;
1371
1372 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1373 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1374
1375 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1376 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1377 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1378
1379 loopRepTestBlock[loopRepDepth] = testBlock;
1380 loopRepEndBlock[loopRepDepth] = endBlock;
1381
1382 // FIXME: jump(testBlock)
1383 Nucleus::createBr(testBlock);
1384 Nucleus::setInsertBlock(testBlock);
1385
1386 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1387 Nucleus::setInsertBlock(loopBlock);
1388
1389 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1390
1391 loopRepDepth++;
1392 breakDepth = 0;
1393 }
1394
John Bauman19bac1e2014-05-06 15:23:49 -04001395 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1396 {
1397 r.enableIndex++;
1398
1399 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1400 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1401 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1402
1403 loopRepTestBlock[loopRepDepth] = testBlock;
1404 loopRepEndBlock[loopRepDepth] = endBlock;
1405
1406 Int4 restoreBreak = r.enableBreak;
1407 Int4 restoreContinue = r.enableContinue;
1408
1409 // FIXME: jump(testBlock)
1410 Nucleus::createBr(testBlock);
1411 Nucleus::setInsertBlock(testBlock);
1412 r.enableContinue = restoreContinue;
1413
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001414 const Vector4f &src = fetchRegisterF(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001415 Int4 condition = As<Int4>(src.x);
1416 condition &= r.enableStack[r.enableIndex - 1];
1417 r.enableStack[r.enableIndex] = condition;
1418
1419 Bool notAllFalse = SignMask(condition) != 0;
1420 branch(notAllFalse, loopBlock, endBlock);
1421
1422 Nucleus::setInsertBlock(endBlock);
1423 r.enableBreak = restoreBreak;
1424
1425 Nucleus::setInsertBlock(loopBlock);
1426
1427 loopRepDepth++;
1428 breakDepth = 0;
1429 }
1430
John Bauman89401822014-05-06 15:04:28 -04001431 void VertexProgram::RET(Registers &r)
1432 {
John Bauman19bac1e2014-05-06 15:23:49 -04001433 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001434 {
1435 returnBlock = Nucleus::createBasicBlock();
1436 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001437 }
1438 else
1439 {
John Bauman89401822014-05-06 15:04:28 -04001440 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001441
John Bauman19bac1e2014-05-06 15:23:49 -04001442 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001443 {
John Bauman19bac1e2014-05-06 15:23:49 -04001444 // FIXME: Encapsulate
1445 UInt index = r.callStack[--r.stackIndex];
1446
John Bauman66b8ab22014-05-06 15:57:45 -04001447 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001448 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1449
1450 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1451 {
1452 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1453 }
1454 }
1455 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1456 {
1457 Nucleus::createBr(callRetBlock[currentLabel][0]);
1458 }
1459 else // Function isn't called
1460 {
1461 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001462 }
1463
1464 Nucleus::setInsertBlock(unreachableBlock);
1465 Nucleus::createUnreachable();
1466 }
1467 }
1468
John Bauman19bac1e2014-05-06 15:23:49 -04001469 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001470 {
John Bauman19bac1e2014-05-06 15:23:49 -04001471 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001472
John Bauman19bac1e2014-05-06 15:23:49 -04001473 // FIXME: Return from function if all instances left
1474 // FIXME: Use enableLeave in other control-flow constructs
1475 }
John Bauman89401822014-05-06 15:04:28 -04001476
John Bauman19bac1e2014-05-06 15:23:49 -04001477 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1478 {
1479 Vector4f tmp;
1480 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001481
1482 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1483 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1484 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1485 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1486 }
John Bauman19bac1e2014-05-06 15:23:49 -04001487
1488 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1489 {
1490 Float4 lod = Float4(0.0f);
1491 Vector4f tmp;
1492 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1493
1494 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1495 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1496 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1497 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1498 }
1499
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001500 void VertexProgram::TEXOFFSET(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
1501 {
1502 UNIMPLEMENTED();
1503 }
1504
1505 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
1506 {
1507 UNIMPLEMENTED();
1508 }
1509
1510 void VertexProgram::TEXELFETCH(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
1511 {
1512 UNIMPLEMENTED();
1513 }
1514
1515 void VertexProgram::TEXELFETCH(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
1516 {
1517 UNIMPLEMENTED();
1518 }
1519
1520 void VertexProgram::TEXGRAD(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
1521 {
1522 UNIMPLEMENTED();
1523 }
1524
1525 void VertexProgram::TEXGRAD(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
1526 {
1527 UNIMPLEMENTED();
1528 }
1529
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001530 void VertexProgram::TEXSIZE(Registers &r, Vector4f &dst, Float4 &lod, const Src &src1)
1531 {
1532 Pointer<Byte> textureMipmap = r.data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
1533 for(int i = 0; i < 4; ++i)
1534 {
1535 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1536 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1537 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1538 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1539 }
1540 }
1541
John Bauman19bac1e2014-05-06 15:23:49 -04001542 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1543 {
1544 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1545 {
1546 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1547 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1548 }
1549 else
1550 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001551 Int index = As<Int>(Float(fetchRegisterF(r, s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001552
1553 for(int i = 0; i < 16; i++)
1554 {
1555 if(shader->usesSampler(i))
1556 {
1557 If(index == i)
1558 {
1559 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1560 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1561 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1562 }
1563 }
1564 }
1565 }
1566 }
John Bauman89401822014-05-06 15:04:28 -04001567}