blob: eaa5b12e925e5514ca6ead1916744a525b4408e4 [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];
John Bauman89401822014-05-06 15:04:28 -0400105
John Bauman19bac1e2014-05-06 15:23:49 -0400106 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400107 Control control = instruction->control;
108 bool integer = dst.type == Shader::PARAMETER_ADDR;
109 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400110
John Bauman19bac1e2014-05-06 15:23:49 -0400111 Vector4f d;
112 Vector4f s0;
113 Vector4f s1;
114 Vector4f s2;
John Bauman89401822014-05-06 15:04:28 -0400115
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400116 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(r, src0);
117 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(r, src1);
118 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(r, src2);
John Bauman89401822014-05-06 15:04:28 -0400119
120 switch(opcode)
121 {
John Bauman19bac1e2014-05-06 15:23:49 -0400122 case Shader::OPCODE_VS_1_0: break;
123 case Shader::OPCODE_VS_1_1: break;
124 case Shader::OPCODE_VS_2_0: break;
125 case Shader::OPCODE_VS_2_x: break;
126 case Shader::OPCODE_VS_2_sw: break;
127 case Shader::OPCODE_VS_3_0: break;
128 case Shader::OPCODE_VS_3_sw: break;
129 case Shader::OPCODE_DCL: break;
130 case Shader::OPCODE_DEF: break;
131 case Shader::OPCODE_DEFI: break;
132 case Shader::OPCODE_DEFB: break;
133 case Shader::OPCODE_NOP: break;
134 case Shader::OPCODE_ABS: abs(d, s0); break;
135 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400136 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400137 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
138 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
139 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
140 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
141 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
142 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
143 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
144 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
145 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
146 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
147 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
148 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
149 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
150 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
151 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
152 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
153 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
154 case Shader::OPCODE_ATT: att(d, s0, s1); break;
155 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
156 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
157 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
158 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
159 case Shader::OPCODE_FRC: frc(d, s0); break;
160 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
161 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400162 case Shader::OPCODE_ROUND: round(d, s0); break;
Alexis Hetu8e851c12015-06-04 11:30:54 -0400163 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400164 case Shader::OPCODE_CEIL: ceil(d, s0); break;
165 case Shader::OPCODE_LIT: lit(d, s0); break;
166 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
167 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
168 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
169 case Shader::OPCODE_LOG: log(d, s0, pp); break;
170 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
171 case Shader::OPCODE_STEP: step(d, s0, s1); break;
172 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400173 case Shader::OPCODE_FLOATBITSTOINT:
174 case Shader::OPCODE_FLOATBITSTOUINT:
175 case Shader::OPCODE_INTBITSTOFLOAT:
176 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400177 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
178 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
179 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
180 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
181 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
182 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400183 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400184 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400185 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
186 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400187 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400188 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
189 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400190 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
191 case Shader::OPCODE_MOVA: mov(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400192 case Shader::OPCODE_NEG: neg(d, s0); break;
193 case Shader::OPCODE_INEG: ineg(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400194 case Shader::OPCODE_F2B: f2b(d, s0); break;
195 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400196 case Shader::OPCODE_F2I: f2i(d, s0); break;
197 case Shader::OPCODE_I2F: i2f(d, s0); break;
198 case Shader::OPCODE_F2U: f2u(d, s0); break;
199 case Shader::OPCODE_U2F: u2f(d, s0); break;
200 case Shader::OPCODE_I2B: i2b(d, s0); break;
201 case Shader::OPCODE_B2I: b2i(d, s0); break;
202 case Shader::OPCODE_U2B: u2b(d, s0); break;
203 case Shader::OPCODE_B2U: b2u(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400204 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400205 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400206 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
207 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
208 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
209 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
210 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
211 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
212 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400213 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
214 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400215 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400216 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
217 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
218 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
219 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
220 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400221 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
222 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
223 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
224 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
225 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
226 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
227 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
228 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
229 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
230 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
231 case Shader::OPCODE_SGE: step(d, s1, s0); break;
232 case Shader::OPCODE_SGN: sgn(d, s0); break;
233 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
234 case Shader::OPCODE_COS: cos(d, s0, pp); break;
235 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
236 case Shader::OPCODE_TAN: tan(d, s0); break;
237 case Shader::OPCODE_ACOS: acos(d, s0); break;
238 case Shader::OPCODE_ASIN: asin(d, s0); break;
239 case Shader::OPCODE_ATAN: atan(d, s0); break;
240 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400241 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
242 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
243 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
244 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
245 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
246 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400247 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
248 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400249 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400250 case Shader::OPCODE_BREAK: BREAK(r); break;
251 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
252 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
253 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
254 case Shader::OPCODE_TEST: TEST(); break;
255 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
256 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
257 case Shader::OPCODE_ELSE: ELSE(r); break;
258 case Shader::OPCODE_ENDIF: ENDIF(r); break;
259 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
260 case Shader::OPCODE_ENDREP: ENDREP(r); break;
261 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
262 case Shader::OPCODE_IF: IF(r, src0); break;
263 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
264 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
265 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
266 case Shader::OPCODE_REP: REP(r, src0); break;
267 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
268 case Shader::OPCODE_RET: RET(r); break;
269 case Shader::OPCODE_LEAVE: LEAVE(r); break;
270 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
271 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400272 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400273 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
274 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
275 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
276 case Shader::OPCODE_ALL: all(d.x, s0); break;
277 case Shader::OPCODE_ANY: any(d.x, s0); break;
278 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400279 case Shader::OPCODE_OR: or(d, s0, s1); break;
280 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
281 case Shader::OPCODE_AND: and(d, s0, s1); break;
282 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
283 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400284 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1); break;
285 case Shader::OPCODE_TEX: TEX(r, d, s0, src1); break;
286 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400287 default:
288 ASSERT(false);
289 }
290
John Bauman19bac1e2014-05-06 15:23:49 -0400291 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400292 {
John Bauman19bac1e2014-05-06 15:23:49 -0400293 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400294 {
John Bauman19bac1e2014-05-06 15:23:49 -0400295 switch(opcode)
296 {
297 case Shader::OPCODE_DIV:
298 if(dst.x) d.x = Trunc(d.x);
299 if(dst.y) d.y = Trunc(d.y);
300 if(dst.z) d.z = Trunc(d.z);
301 if(dst.w) d.w = Trunc(d.w);
302 break;
303 default:
304 break; // No truncation to integer required when arguments are integer
305 }
John Bauman89401822014-05-06 15:04:28 -0400306 }
307
John Bauman19bac1e2014-05-06 15:23:49 -0400308 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400309 {
John Bauman19bac1e2014-05-06 15:23:49 -0400310 if(dst.x) d.x = Max(d.x, Float4(0.0f));
311 if(dst.y) d.y = Max(d.y, Float4(0.0f));
312 if(dst.z) d.z = Max(d.z, Float4(0.0f));
313 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400314
John Bauman19bac1e2014-05-06 15:23:49 -0400315 if(dst.x) d.x = Min(d.x, Float4(1.0f));
316 if(dst.y) d.y = Min(d.y, Float4(1.0f));
317 if(dst.z) d.z = Min(d.z, Float4(1.0f));
318 if(dst.w) d.w = Min(d.w, Float4(1.0f));
319 }
320
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400321 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400322 {
323 Vector4f pDst; // FIXME: Rename
324
325 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400326 {
John Bauman19bac1e2014-05-06 15:23:49 -0400327 case Shader::PARAMETER_VOID: break;
328 case Shader::PARAMETER_TEMP:
329 if(dst.rel.type == Shader::PARAMETER_VOID)
330 {
331 if(dst.x) pDst.x = r.r[dst.index].x;
332 if(dst.y) pDst.y = r.r[dst.index].y;
333 if(dst.z) pDst.z = r.r[dst.index].z;
334 if(dst.w) pDst.w = r.r[dst.index].w;
335 }
336 else
337 {
338 Int a = relativeAddress(r, dst);
339
340 if(dst.x) pDst.x = r.r[dst.index + a].x;
341 if(dst.y) pDst.y = r.r[dst.index + a].y;
342 if(dst.z) pDst.z = r.r[dst.index + a].z;
343 if(dst.w) pDst.w = r.r[dst.index + a].w;
344 }
345 break;
346 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
347 case Shader::PARAMETER_RASTOUT:
348 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400349 {
350 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400351 if(dst.x) pDst.x = r.o[Pos].x;
352 if(dst.y) pDst.y = r.o[Pos].y;
353 if(dst.z) pDst.z = r.o[Pos].z;
354 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400355 break;
356 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400357 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400358 break;
359 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400360 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400361 break;
362 default:
363 ASSERT(false);
364 }
365 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400366 case Shader::PARAMETER_ATTROUT:
367 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
368 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
369 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
370 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400371 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400372 case Shader::PARAMETER_TEXCRDOUT:
373 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400374 if(version < 0x0300)
375 {
John Bauman19bac1e2014-05-06 15:23:49 -0400376 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
377 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
378 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
379 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400380 }
381 else
382 {
John Bauman19bac1e2014-05-06 15:23:49 -0400383 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400384 {
John Bauman19bac1e2014-05-06 15:23:49 -0400385 if(dst.x) pDst.x = r.o[dst.index].x;
386 if(dst.y) pDst.y = r.o[dst.index].y;
387 if(dst.z) pDst.z = r.o[dst.index].z;
388 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400389 }
John Bauman19bac1e2014-05-06 15:23:49 -0400390 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400391 {
392 Int aL = r.aL[r.loopDepth];
393
John Bauman19bac1e2014-05-06 15:23:49 -0400394 if(dst.x) pDst.x = r.o[dst.index + aL].x;
395 if(dst.y) pDst.y = r.o[dst.index + aL].y;
396 if(dst.z) pDst.z = r.o[dst.index + aL].z;
397 if(dst.w) pDst.w = r.o[dst.index + aL].w;
398 }
399 else
400 {
401 Int a = relativeAddress(r, dst);
402
403 if(dst.x) pDst.x = r.o[dst.index + a].x;
404 if(dst.y) pDst.y = r.o[dst.index + a].y;
405 if(dst.z) pDst.z = r.o[dst.index + a].z;
406 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400407 }
408 }
409 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400410 case Shader::PARAMETER_LABEL: break;
411 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
412 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400413 default:
414 ASSERT(false);
415 }
416
John Bauman19bac1e2014-05-06 15:23:49 -0400417 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -0400418
419 Int4 xEnable = enable;
420 Int4 yEnable = enable;
421 Int4 zEnable = enable;
422 Int4 wEnable = enable;
423
424 if(predicate)
425 {
John Bauman19bac1e2014-05-06 15:23:49 -0400426 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400427
428 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
429 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
430 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
431 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
432
John Bauman19bac1e2014-05-06 15:23:49 -0400433 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400434 {
John Bauman19bac1e2014-05-06 15:23:49 -0400435 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
436 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
437 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
438 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400439 }
440 else
441 {
John Bauman19bac1e2014-05-06 15:23:49 -0400442 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
443 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
444 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
445 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400446 }
447 }
448
John Bauman19bac1e2014-05-06 15:23:49 -0400449 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
450 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
451 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
452 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400453
John Bauman19bac1e2014-05-06 15:23:49 -0400454 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
455 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
456 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
457 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400458 }
459
John Bauman19bac1e2014-05-06 15:23:49 -0400460 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400461 {
John Bauman19bac1e2014-05-06 15:23:49 -0400462 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400463 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400464 case Shader::PARAMETER_TEMP:
465 if(dst.rel.type == Shader::PARAMETER_VOID)
466 {
467 if(dst.x) r.r[dst.index].x = d.x;
468 if(dst.y) r.r[dst.index].y = d.y;
469 if(dst.z) r.r[dst.index].z = d.z;
470 if(dst.w) r.r[dst.index].w = d.w;
471 }
472 else
473 {
474 Int a = relativeAddress(r, dst);
475
476 if(dst.x) r.r[dst.index + a].x = d.x;
477 if(dst.y) r.r[dst.index + a].y = d.y;
478 if(dst.z) r.r[dst.index + a].z = d.z;
479 if(dst.w) r.r[dst.index + a].w = d.w;
480 }
John Bauman89401822014-05-06 15:04:28 -0400481 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400482 case Shader::PARAMETER_ADDR:
483 if(dst.x) r.a0.x = d.x;
484 if(dst.y) r.a0.y = d.y;
485 if(dst.z) r.a0.z = d.z;
486 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400487 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400488 case Shader::PARAMETER_RASTOUT:
489 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400490 {
491 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400492 if(dst.x) r.o[Pos].x = d.x;
493 if(dst.y) r.o[Pos].y = d.y;
494 if(dst.z) r.o[Pos].z = d.z;
495 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400496 break;
497 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400498 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400499 break;
500 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400501 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400502 break;
503 default: ASSERT(false);
504 }
505 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400506 case Shader::PARAMETER_ATTROUT:
507 if(dst.x) r.o[D0 + dst.index].x = d.x;
508 if(dst.y) r.o[D0 + dst.index].y = d.y;
509 if(dst.z) r.o[D0 + dst.index].z = d.z;
510 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400511 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400512 case Shader::PARAMETER_TEXCRDOUT:
513 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400514 if(version < 0x0300)
515 {
John Bauman19bac1e2014-05-06 15:23:49 -0400516 if(dst.x) r.o[T0 + dst.index].x = d.x;
517 if(dst.y) r.o[T0 + dst.index].y = d.y;
518 if(dst.z) r.o[T0 + dst.index].z = d.z;
519 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400520 }
521 else
522 {
John Bauman19bac1e2014-05-06 15:23:49 -0400523 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400524 {
John Bauman19bac1e2014-05-06 15:23:49 -0400525 if(dst.x) r.o[dst.index].x = d.x;
526 if(dst.y) r.o[dst.index].y = d.y;
527 if(dst.z) r.o[dst.index].z = d.z;
528 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400529 }
John Bauman19bac1e2014-05-06 15:23:49 -0400530 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400531 {
532 Int aL = r.aL[r.loopDepth];
533
John Bauman19bac1e2014-05-06 15:23:49 -0400534 if(dst.x) r.o[dst.index + aL].x = d.x;
535 if(dst.y) r.o[dst.index + aL].y = d.y;
536 if(dst.z) r.o[dst.index + aL].z = d.z;
537 if(dst.w) r.o[dst.index + aL].w = d.w;
538 }
539 else
540 {
541 Int a = relativeAddress(r, dst);
542
543 if(dst.x) r.o[dst.index + a].x = d.x;
544 if(dst.y) r.o[dst.index + a].y = d.y;
545 if(dst.z) r.o[dst.index + a].z = d.z;
546 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400547 }
548 }
549 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400550 case Shader::PARAMETER_LABEL: break;
551 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
552 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400553 default:
554 ASSERT(false);
555 }
556 }
557 }
558
John Bauman19bac1e2014-05-06 15:23:49 -0400559 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400560 {
561 Nucleus::setInsertBlock(returnBlock);
562 }
563 }
564
565 void VertexProgram::passThrough(Registers &r)
566 {
John Bauman19bac1e2014-05-06 15:23:49 -0400567 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400568 {
569 for(int i = 0; i < 12; i++)
570 {
John Bauman19bac1e2014-05-06 15:23:49 -0400571 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400572
573 switch(usage)
574 {
575 case 0xFF:
576 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400577 case Shader::USAGE_PSIZE:
578 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400579 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400580 case Shader::USAGE_TEXCOORD:
581 r.o[i].x = r.v[i].x;
582 r.o[i].y = r.v[i].y;
583 r.o[i].z = r.v[i].z;
584 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400585 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400586 case Shader::USAGE_POSITION:
587 r.o[i].x = r.v[i].x;
588 r.o[i].y = r.v[i].y;
589 r.o[i].z = r.v[i].z;
590 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400591 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400592 case Shader::USAGE_COLOR:
593 r.o[i].x = r.v[i].x;
594 r.o[i].y = r.v[i].y;
595 r.o[i].z = r.v[i].z;
596 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400597 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400598 case Shader::USAGE_FOG:
599 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400600 break;
601 default:
602 ASSERT(false);
603 }
604 }
605 }
606 else
607 {
John Bauman19bac1e2014-05-06 15:23:49 -0400608 r.o[Pos].x = r.v[PositionT].x;
609 r.o[Pos].y = r.v[PositionT].y;
610 r.o[Pos].z = r.v[PositionT].z;
611 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400612
613 for(int i = 0; i < 2; i++)
614 {
John Bauman19bac1e2014-05-06 15:23:49 -0400615 r.o[D0 + i].x = r.v[Color0 + i].x;
616 r.o[D0 + i].y = r.v[Color0 + i].y;
617 r.o[D0 + i].z = r.v[Color0 + i].z;
618 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400619 }
620
621 for(int i = 0; i < 8; i++)
622 {
John Bauman19bac1e2014-05-06 15:23:49 -0400623 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
624 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
625 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
626 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400627 }
628
John Bauman66b8ab22014-05-06 15:57:45 -0400629 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400630 }
631 }
632
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400633 Vector4f VertexProgram::fetchRegisterF(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400634 {
635 int i = src.index + offset;
636
John Bauman19bac1e2014-05-06 15:23:49 -0400637 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -0400638
John Bauman89401822014-05-06 15:04:28 -0400639 switch(src.type)
640 {
John Bauman19bac1e2014-05-06 15:23:49 -0400641 case Shader::PARAMETER_TEMP:
642 if(src.rel.type == Shader::PARAMETER_VOID)
643 {
644 reg = r.r[i];
645 }
646 else
647 {
648 reg = r.r[i + relativeAddress(r, src)];
649 }
650 break;
651 case Shader::PARAMETER_CONST:
652 reg = readConstant(r, src, offset);
653 break;
654 case Shader::PARAMETER_INPUT:
655 if(src.rel.type == Shader::PARAMETER_VOID)
656 {
657 reg = r.v[i];
658 }
659 else
660 {
661 reg = r.v[i + relativeAddress(r, src)];
662 }
663 break;
664 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
665 case Shader::PARAMETER_FLOAT4LITERAL:
666 reg.x = Float4(src.value[0]);
667 reg.y = Float4(src.value[1]);
668 reg.z = Float4(src.value[2]);
669 reg.w = Float4(src.value[3]);
670 break;
671 case Shader::PARAMETER_ADDR: reg = r.a0; break;
672 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
673 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
674 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
675 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
676 case Shader::PARAMETER_SAMPLER:
677 if(src.rel.type == Shader::PARAMETER_VOID)
678 {
679 reg.x = As<Float4>(Int4(i));
680 }
681 else if(src.rel.type == Shader::PARAMETER_TEMP)
682 {
683 reg.x = As<Float4>(Int4(i) + RoundInt(r.r[src.rel.index].x));
684 }
685 return reg;
686 case Shader::PARAMETER_OUTPUT:
687 if(src.rel.type == Shader::PARAMETER_VOID)
688 {
689 reg = r.o[i];
690 }
691 else
692 {
693 reg = r.o[i + relativeAddress(r, src)];
694 }
695 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400696 case Shader::PARAMETER_MISCTYPE:
697 reg.x = Float(r.instanceID);
698 return reg;
John Bauman89401822014-05-06 15:04:28 -0400699 default:
700 ASSERT(false);
701 }
702
John Bauman66b8ab22014-05-06 15:57:45 -0400703 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
704 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
705 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
706 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400707
John Bauman66b8ab22014-05-06 15:57:45 -0400708 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400709
710 switch(src.modifier)
711 {
John Bauman19bac1e2014-05-06 15:23:49 -0400712 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400713 mod.x = x;
714 mod.y = y;
715 mod.z = z;
716 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400717 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400718 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400719 mod.x = -x;
720 mod.y = -y;
721 mod.z = -z;
722 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400723 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400724 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400725 mod.x = Abs(x);
726 mod.y = Abs(y);
727 mod.z = Abs(z);
728 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400729 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400730 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400731 mod.x = -Abs(x);
732 mod.y = -Abs(y);
733 mod.z = -Abs(z);
734 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400735 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400736 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400737 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
738 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
739 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
740 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400741 break;
742 default:
743 ASSERT(false);
744 }
745
746 return mod;
747 }
748
John Bauman19bac1e2014-05-06 15:23:49 -0400749 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400750 {
John Bauman19bac1e2014-05-06 15:23:49 -0400751 Vector4f c;
752
753 int i = src.index + offset;
754
755 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
756 {
757 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
758
759 c.x = c.x.xxxx;
760 c.y = c.y.yyyy;
761 c.z = c.z.zzzz;
762 c.w = c.w.wwww;
763
Nicolas Capenseafdb222015-05-15 15:24:08 -0400764 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400765 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500766 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400767 {
768 const Shader::Instruction &instruction = *shader->getInstruction(j);
769
770 if(instruction.opcode == Shader::OPCODE_DEF)
771 {
772 if(instruction.dst.index == i)
773 {
774 c.x = Float4(instruction.src[0].value[0]);
775 c.y = Float4(instruction.src[0].value[1]);
776 c.z = Float4(instruction.src[0].value[2]);
777 c.w = Float4(instruction.src[0].value[3]);
778
779 break;
780 }
781 }
782 }
783 }
784 }
785 else if(src.rel.type == Shader::PARAMETER_LOOP)
786 {
787 Int loopCounter = r.aL[r.loopDepth];
788
789 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
790
791 c.x = c.x.xxxx;
792 c.y = c.y.yyyy;
793 c.z = c.z.zzzz;
794 c.w = c.w.wwww;
795 }
796 else
797 {
798 if(src.rel.deterministic)
799 {
800 Int a = relativeAddress(r, src);
801
802 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
803
804 c.x = c.x.xxxx;
805 c.y = c.y.yyyy;
806 c.z = c.z.zzzz;
807 c.w = c.w.wwww;
808 }
809 else
810 {
811 int component = src.rel.swizzle & 0x03;
812 Float4 a;
813
814 switch(src.rel.type)
815 {
816 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
817 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
818 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
819 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400820 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 -0400821 default: ASSERT(false);
822 }
823
824 Int4 index = Int4(i) + RoundInt(a) * Int4(src.rel.scale);
825
826 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
827
828 Int index0 = Extract(index, 0);
829 Int index1 = Extract(index, 1);
830 Int index2 = Extract(index, 2);
831 Int index3 = Extract(index, 3);
832
833 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
834 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
835 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
836 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
837
838 transpose4x4(c.x, c.y, c.z, c.w);
839 }
840 }
841
842 return c;
843 }
844
845 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
846 {
847 ASSERT(var.rel.deterministic);
848
849 if(var.rel.type == Shader::PARAMETER_TEMP)
850 {
851 return RoundInt(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
852 }
853 else if(var.rel.type == Shader::PARAMETER_INPUT)
854 {
855 return RoundInt(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
856 }
857 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
858 {
859 return RoundInt(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
860 }
861 else if(var.rel.type == Shader::PARAMETER_CONST)
862 {
863 RValue<Float4> c = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[var.rel.index]));
864
865 return RoundInt(Extract(c, 0)) * var.rel.scale;
866 }
867 else ASSERT(false);
868
869 return 0;
870 }
871
872 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
873 {
874 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -0400875
876 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400877 {
John Baumand4ae8632014-05-06 16:18:33 -0400878 if(shader->containsBreakInstruction() && instruction->analysisBreak)
879 {
880 enable &= r.enableBreak;
881 }
John Bauman19bac1e2014-05-06 15:23:49 -0400882
John Baumand4ae8632014-05-06 16:18:33 -0400883 if(shader->containsContinueInstruction() && instruction->analysisContinue)
884 {
885 enable &= r.enableContinue;
886 }
John Bauman19bac1e2014-05-06 15:23:49 -0400887
John Baumand4ae8632014-05-06 16:18:33 -0400888 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
889 {
890 enable &= r.enableLeave;
891 }
John Bauman19bac1e2014-05-06 15:23:49 -0400892 }
893
894 return enable;
895 }
896
897 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
898 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400899 Vector4f row0 = fetchRegisterF(r, src1, 0);
900 Vector4f row1 = fetchRegisterF(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400901
902 dst.x = dot3(src0, row0);
903 dst.y = dot3(src0, row1);
904 }
905
John Bauman19bac1e2014-05-06 15:23:49 -0400906 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400907 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400908 Vector4f row0 = fetchRegisterF(r, src1, 0);
909 Vector4f row1 = fetchRegisterF(r, src1, 1);
910 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400911
912 dst.x = dot3(src0, row0);
913 dst.y = dot3(src0, row1);
914 dst.z = dot3(src0, row2);
915 }
916
John Bauman19bac1e2014-05-06 15:23:49 -0400917 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400918 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400919 Vector4f row0 = fetchRegisterF(r, src1, 0);
920 Vector4f row1 = fetchRegisterF(r, src1, 1);
921 Vector4f row2 = fetchRegisterF(r, src1, 2);
922 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400923
924 dst.x = dot3(src0, row0);
925 dst.y = dot3(src0, row1);
926 dst.z = dot3(src0, row2);
927 dst.w = dot3(src0, row3);
928 }
929
John Bauman19bac1e2014-05-06 15:23:49 -0400930 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400931 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400932 Vector4f row0 = fetchRegisterF(r, src1, 0);
933 Vector4f row1 = fetchRegisterF(r, src1, 1);
934 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400935
936 dst.x = dot4(src0, row0);
937 dst.y = dot4(src0, row1);
938 dst.z = dot4(src0, row2);
939 }
940
John Bauman19bac1e2014-05-06 15:23:49 -0400941 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400942 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400943 Vector4f row0 = fetchRegisterF(r, src1, 0);
944 Vector4f row1 = fetchRegisterF(r, src1, 1);
945 Vector4f row2 = fetchRegisterF(r, src1, 2);
946 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400947
948 dst.x = dot4(src0, row0);
949 dst.y = dot4(src0, row1);
950 dst.z = dot4(src0, row2);
951 dst.w = dot4(src0, row3);
952 }
953
954 void VertexProgram::BREAK(Registers &r)
955 {
956 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
957 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
958
959 if(breakDepth == 0)
960 {
John Bauman19bac1e2014-05-06 15:23:49 -0400961 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400962 Nucleus::createBr(endBlock);
963 }
964 else
965 {
966 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
967 Bool allBreak = SignMask(r.enableBreak) == 0x0;
968
John Bauman19bac1e2014-05-06 15:23:49 -0400969 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400970 branch(allBreak, endBlock, deadBlock);
971 }
972
973 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400974 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400975 }
976
John Bauman19bac1e2014-05-06 15:23:49 -0400977 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400978 {
979 Int4 condition;
980
981 switch(control)
982 {
John Bauman19bac1e2014-05-06 15:23:49 -0400983 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
984 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
985 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
986 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
987 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
988 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400989 default:
990 ASSERT(false);
991 }
992
John Bauman19bac1e2014-05-06 15:23:49 -0400993 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -0400994 }
995
996 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
997 {
998 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
999
John Bauman19bac1e2014-05-06 15:23:49 -04001000 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001001 {
1002 condition = ~condition;
1003 }
1004
John Bauman19bac1e2014-05-06 15:23:49 -04001005 BREAK(r, condition);
1006 }
1007
1008 void VertexProgram::BREAK(Registers &r, Int4 &condition)
1009 {
John Bauman89401822014-05-06 15:04:28 -04001010 condition &= r.enableStack[r.enableIndex];
1011
1012 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1013 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1014
1015 r.enableBreak = r.enableBreak & ~condition;
1016 Bool allBreak = SignMask(r.enableBreak) == 0x0;
1017
John Bauman19bac1e2014-05-06 15:23:49 -04001018 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001019 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001020
John Bauman89401822014-05-06 15:04:28 -04001021 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001022 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001023 }
1024
John Bauman19bac1e2014-05-06 15:23:49 -04001025 void VertexProgram::CONTINUE(Registers &r)
1026 {
1027 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1028 }
1029
1030 void VertexProgram::TEST()
1031 {
1032 whileTest = true;
1033 }
1034
1035 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001036 {
1037 if(!labelBlock[labelIndex])
1038 {
1039 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1040 }
1041
John Bauman19bac1e2014-05-06 15:23:49 -04001042 if(callRetBlock[labelIndex].size() > 1)
1043 {
1044 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1045 }
John Bauman89401822014-05-06 15:04:28 -04001046
John Bauman19bac1e2014-05-06 15:23:49 -04001047 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001048
1049 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001050 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1051
1052 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001053 }
1054
John Bauman19bac1e2014-05-06 15:23:49 -04001055 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001056 {
John Bauman19bac1e2014-05-06 15:23:49 -04001057 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001058 {
John Bauman19bac1e2014-05-06 15:23:49 -04001059 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001060 }
John Bauman19bac1e2014-05-06 15:23:49 -04001061 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001062 {
John Bauman19bac1e2014-05-06 15:23:49 -04001063 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001064 }
1065 else ASSERT(false);
1066 }
1067
John Bauman19bac1e2014-05-06 15:23:49 -04001068 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001069 {
1070 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1071
John Bauman19bac1e2014-05-06 15:23:49 -04001072 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001073 {
1074 condition = !condition;
1075 }
1076
1077 if(!labelBlock[labelIndex])
1078 {
1079 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1080 }
1081
John Bauman19bac1e2014-05-06 15:23:49 -04001082 if(callRetBlock[labelIndex].size() > 1)
1083 {
1084 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1085 }
John Bauman89401822014-05-06 15:04:28 -04001086
John Bauman19bac1e2014-05-06 15:23:49 -04001087 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001088
John Bauman19bac1e2014-05-06 15:23:49 -04001089 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1090 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1091
1092 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001093 }
1094
John Bauman19bac1e2014-05-06 15:23:49 -04001095 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001096 {
1097 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1098
John Bauman19bac1e2014-05-06 15:23:49 -04001099 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001100 {
1101 condition = ~condition;
1102 }
1103
1104 condition &= r.enableStack[r.enableIndex];
1105
1106 if(!labelBlock[labelIndex])
1107 {
1108 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1109 }
1110
John Bauman19bac1e2014-05-06 15:23:49 -04001111 if(callRetBlock[labelIndex].size() > 1)
1112 {
1113 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1114 }
John Bauman89401822014-05-06 15:04:28 -04001115
1116 r.enableIndex++;
1117 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001118 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001119
John Bauman19bac1e2014-05-06 15:23:49 -04001120 Bool notAllFalse = SignMask(condition) != 0;
1121 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1122 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001123
1124 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001125 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001126 }
1127
1128 void VertexProgram::ELSE(Registers &r)
1129 {
1130 ifDepth--;
1131
1132 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1133 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1134
1135 if(isConditionalIf[ifDepth])
1136 {
1137 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001138 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001139
1140 branch(notAllFalse, falseBlock, endBlock);
1141
1142 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1143 }
1144 else
1145 {
1146 Nucleus::createBr(endBlock);
1147 Nucleus::setInsertBlock(falseBlock);
1148 }
1149
1150 ifFalseBlock[ifDepth] = endBlock;
1151
1152 ifDepth++;
1153 }
1154
1155 void VertexProgram::ENDIF(Registers &r)
1156 {
1157 ifDepth--;
1158
1159 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1160
1161 Nucleus::createBr(endBlock);
1162 Nucleus::setInsertBlock(endBlock);
1163
1164 if(isConditionalIf[ifDepth])
1165 {
1166 breakDepth--;
1167 r.enableIndex--;
1168 }
1169 }
1170
John Bauman89401822014-05-06 15:04:28 -04001171 void VertexProgram::ENDLOOP(Registers &r)
1172 {
1173 loopRepDepth--;
1174
1175 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1176
1177 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1178 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1179
1180 Nucleus::createBr(testBlock);
1181 Nucleus::setInsertBlock(endBlock);
1182
1183 r.loopDepth--;
1184 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1185 }
1186
John Bauman19bac1e2014-05-06 15:23:49 -04001187 void VertexProgram::ENDREP(Registers &r)
1188 {
1189 loopRepDepth--;
1190
1191 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1192 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1193
1194 Nucleus::createBr(testBlock);
1195 Nucleus::setInsertBlock(endBlock);
1196
1197 r.loopDepth--;
1198 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1199 }
1200
1201 void VertexProgram::ENDWHILE(Registers &r)
1202 {
1203 loopRepDepth--;
1204
1205 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1206 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1207
1208 Nucleus::createBr(testBlock);
1209 Nucleus::setInsertBlock(endBlock);
1210
1211 r.enableIndex--;
1212 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1213 whileTest = false;
1214 }
1215
John Bauman89401822014-05-06 15:04:28 -04001216 void VertexProgram::IF(Registers &r, const Src &src)
1217 {
John Bauman19bac1e2014-05-06 15:23:49 -04001218 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001219 {
1220 IFb(r, src);
1221 }
John Bauman19bac1e2014-05-06 15:23:49 -04001222 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001223 {
1224 IFp(r, src);
1225 }
John Bauman19bac1e2014-05-06 15:23:49 -04001226 else
1227 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001228 Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
John Bauman19bac1e2014-05-06 15:23:49 -04001229 IF(r, condition);
1230 }
John Bauman89401822014-05-06 15:04:28 -04001231 }
1232
1233 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1234 {
1235 ASSERT(ifDepth < 24 + 4);
1236
1237 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1238
John Bauman19bac1e2014-05-06 15:23:49 -04001239 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001240 {
John Bauman19bac1e2014-05-06 15:23:49 -04001241 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001242 }
1243
1244 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1245 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1246
1247 branch(condition, trueBlock, falseBlock);
1248
1249 isConditionalIf[ifDepth] = false;
1250 ifFalseBlock[ifDepth] = falseBlock;
1251
1252 ifDepth++;
1253 }
1254
John Bauman19bac1e2014-05-06 15:23:49 -04001255 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001256 {
1257 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1258
John Bauman19bac1e2014-05-06 15:23:49 -04001259 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001260 {
1261 condition = ~condition;
1262 }
1263
John Bauman19bac1e2014-05-06 15:23:49 -04001264 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001265 }
1266
John Bauman19bac1e2014-05-06 15:23:49 -04001267 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001268 {
1269 Int4 condition;
1270
1271 switch(control)
1272 {
John Bauman19bac1e2014-05-06 15:23:49 -04001273 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1274 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1275 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1276 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1277 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1278 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001279 default:
1280 ASSERT(false);
1281 }
1282
John Bauman19bac1e2014-05-06 15:23:49 -04001283 IF(r, condition);
1284 }
1285
1286 void VertexProgram::IF(Registers &r, Int4 &condition)
1287 {
John Bauman89401822014-05-06 15:04:28 -04001288 condition &= r.enableStack[r.enableIndex];
1289
1290 r.enableIndex++;
1291 r.enableStack[r.enableIndex] = condition;
1292
1293 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1294 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1295
John Bauman19bac1e2014-05-06 15:23:49 -04001296 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001297
1298 branch(notAllFalse, trueBlock, falseBlock);
1299
1300 isConditionalIf[ifDepth] = true;
1301 ifFalseBlock[ifDepth] = falseBlock;
1302
1303 ifDepth++;
1304 breakDepth++;
1305 }
1306
1307 void VertexProgram::LABEL(int labelIndex)
1308 {
John Bauman19bac1e2014-05-06 15:23:49 -04001309 if(!labelBlock[labelIndex])
1310 {
1311 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1312 }
1313
John Bauman89401822014-05-06 15:04:28 -04001314 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001315 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001316 }
1317
1318 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1319 {
1320 r.loopDepth++;
1321
1322 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1323 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1324 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1325
1326 // FIXME: Compiles to two instructions?
1327 If(r.increment[r.loopDepth] == 0)
1328 {
1329 r.increment[r.loopDepth] = 1;
1330 }
1331
1332 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1333 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1334 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1335
1336 loopRepTestBlock[loopRepDepth] = testBlock;
1337 loopRepEndBlock[loopRepDepth] = endBlock;
1338
1339 // FIXME: jump(testBlock)
1340 Nucleus::createBr(testBlock);
1341 Nucleus::setInsertBlock(testBlock);
1342
1343 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1344 Nucleus::setInsertBlock(loopBlock);
1345
1346 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1347
1348 loopRepDepth++;
1349 breakDepth = 0;
1350 }
1351
1352 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1353 {
1354 r.loopDepth++;
1355
1356 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1357 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1358
1359 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1360 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1361 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1362
1363 loopRepTestBlock[loopRepDepth] = testBlock;
1364 loopRepEndBlock[loopRepDepth] = endBlock;
1365
1366 // FIXME: jump(testBlock)
1367 Nucleus::createBr(testBlock);
1368 Nucleus::setInsertBlock(testBlock);
1369
1370 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1371 Nucleus::setInsertBlock(loopBlock);
1372
1373 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1374
1375 loopRepDepth++;
1376 breakDepth = 0;
1377 }
1378
John Bauman19bac1e2014-05-06 15:23:49 -04001379 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1380 {
1381 r.enableIndex++;
1382
1383 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1384 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1385 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1386
1387 loopRepTestBlock[loopRepDepth] = testBlock;
1388 loopRepEndBlock[loopRepDepth] = endBlock;
1389
1390 Int4 restoreBreak = r.enableBreak;
1391 Int4 restoreContinue = r.enableContinue;
1392
1393 // FIXME: jump(testBlock)
1394 Nucleus::createBr(testBlock);
1395 Nucleus::setInsertBlock(testBlock);
1396 r.enableContinue = restoreContinue;
1397
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001398 const Vector4f &src = fetchRegisterF(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001399 Int4 condition = As<Int4>(src.x);
1400 condition &= r.enableStack[r.enableIndex - 1];
1401 r.enableStack[r.enableIndex] = condition;
1402
1403 Bool notAllFalse = SignMask(condition) != 0;
1404 branch(notAllFalse, loopBlock, endBlock);
1405
1406 Nucleus::setInsertBlock(endBlock);
1407 r.enableBreak = restoreBreak;
1408
1409 Nucleus::setInsertBlock(loopBlock);
1410
1411 loopRepDepth++;
1412 breakDepth = 0;
1413 }
1414
John Bauman89401822014-05-06 15:04:28 -04001415 void VertexProgram::RET(Registers &r)
1416 {
John Bauman19bac1e2014-05-06 15:23:49 -04001417 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001418 {
1419 returnBlock = Nucleus::createBasicBlock();
1420 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001421 }
1422 else
1423 {
John Bauman89401822014-05-06 15:04:28 -04001424 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001425
John Bauman19bac1e2014-05-06 15:23:49 -04001426 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001427 {
John Bauman19bac1e2014-05-06 15:23:49 -04001428 // FIXME: Encapsulate
1429 UInt index = r.callStack[--r.stackIndex];
1430
John Bauman66b8ab22014-05-06 15:57:45 -04001431 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001432 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1433
1434 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1435 {
1436 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1437 }
1438 }
1439 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1440 {
1441 Nucleus::createBr(callRetBlock[currentLabel][0]);
1442 }
1443 else // Function isn't called
1444 {
1445 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001446 }
1447
1448 Nucleus::setInsertBlock(unreachableBlock);
1449 Nucleus::createUnreachable();
1450 }
1451 }
1452
John Bauman19bac1e2014-05-06 15:23:49 -04001453 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001454 {
John Bauman19bac1e2014-05-06 15:23:49 -04001455 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001456
John Bauman19bac1e2014-05-06 15:23:49 -04001457 // FIXME: Return from function if all instances left
1458 // FIXME: Use enableLeave in other control-flow constructs
1459 }
John Bauman89401822014-05-06 15:04:28 -04001460
John Bauman19bac1e2014-05-06 15:23:49 -04001461 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1462 {
1463 Vector4f tmp;
1464 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001465
1466 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1467 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1468 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1469 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1470 }
John Bauman19bac1e2014-05-06 15:23:49 -04001471
1472 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1473 {
1474 Float4 lod = Float4(0.0f);
1475 Vector4f tmp;
1476 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1477
1478 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1479 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1480 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1481 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1482 }
1483
1484 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1485 {
1486 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1487 {
1488 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1489 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1490 }
1491 else
1492 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001493 Int index = As<Int>(Float(fetchRegisterF(r, s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001494
1495 for(int i = 0; i < 16; i++)
1496 {
1497 if(shader->usesSampler(i))
1498 {
1499 If(index == i)
1500 {
1501 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1502 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1503 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1504 }
1505 }
1506 }
1507 }
1508 }
John Bauman89401822014-05-06 15:04:28 -04001509}