blob: 20404804b0133dbf8da6cb6382d69ae38b1cb55d [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];
John Bauman89401822014-05-06 15:04:28 -0400106
John Bauman19bac1e2014-05-06 15:23:49 -0400107 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400108 Control control = instruction->control;
109 bool integer = dst.type == Shader::PARAMETER_ADDR;
110 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400111
John Bauman19bac1e2014-05-06 15:23:49 -0400112 Vector4f d;
113 Vector4f s0;
114 Vector4f s1;
115 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400116 Vector4f s3;
John Bauman89401822014-05-06 15:04:28 -0400117
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400118 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(r, src0);
119 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(r, src1);
120 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(r, src2);
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400121 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegisterF(r, src3);
John Bauman89401822014-05-06 15:04:28 -0400122
123 switch(opcode)
124 {
John Bauman19bac1e2014-05-06 15:23:49 -0400125 case Shader::OPCODE_VS_1_0: break;
126 case Shader::OPCODE_VS_1_1: break;
127 case Shader::OPCODE_VS_2_0: break;
128 case Shader::OPCODE_VS_2_x: break;
129 case Shader::OPCODE_VS_2_sw: break;
130 case Shader::OPCODE_VS_3_0: break;
131 case Shader::OPCODE_VS_3_sw: break;
132 case Shader::OPCODE_DCL: break;
133 case Shader::OPCODE_DEF: break;
134 case Shader::OPCODE_DEFI: break;
135 case Shader::OPCODE_DEFB: break;
136 case Shader::OPCODE_NOP: break;
137 case Shader::OPCODE_ABS: abs(d, s0); break;
138 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400139 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400140 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
141 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
142 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
143 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
144 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
145 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
146 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
147 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
148 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
149 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
150 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
151 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
152 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
153 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
154 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
155 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
156 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400157 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
158 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
159 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400160 case Shader::OPCODE_ATT: att(d, s0, s1); break;
161 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
162 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
163 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
164 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
165 case Shader::OPCODE_FRC: frc(d, s0); break;
166 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
167 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400168 case Shader::OPCODE_ROUND: round(d, s0); break;
Alexis Hetu8e851c12015-06-04 11:30:54 -0400169 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400170 case Shader::OPCODE_CEIL: ceil(d, s0); break;
171 case Shader::OPCODE_LIT: lit(d, s0); break;
172 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
173 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
174 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
175 case Shader::OPCODE_LOG: log(d, s0, pp); break;
176 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
177 case Shader::OPCODE_STEP: step(d, s0, s1); break;
178 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400179 case Shader::OPCODE_FLOATBITSTOINT:
180 case Shader::OPCODE_FLOATBITSTOUINT:
181 case Shader::OPCODE_INTBITSTOFLOAT:
182 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400183 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
184 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
185 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
186 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
187 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
188 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400189 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400190 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400191 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
192 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400193 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400194 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
195 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400196 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400197 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400198 case Shader::OPCODE_NEG: neg(d, s0); break;
199 case Shader::OPCODE_INEG: ineg(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400200 case Shader::OPCODE_F2B: f2b(d, s0); break;
201 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400202 case Shader::OPCODE_F2I: f2i(d, s0); break;
203 case Shader::OPCODE_I2F: i2f(d, s0); break;
204 case Shader::OPCODE_F2U: f2u(d, s0); break;
205 case Shader::OPCODE_U2F: u2f(d, s0); break;
206 case Shader::OPCODE_I2B: i2b(d, s0); break;
207 case Shader::OPCODE_B2I: b2i(d, s0); break;
208 case Shader::OPCODE_U2B: u2b(d, s0); break;
209 case Shader::OPCODE_B2U: b2u(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400210 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400211 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400212 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
213 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
214 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
215 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
216 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
217 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
218 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400219 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
220 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400221 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400222 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
223 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
224 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
225 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
226 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400227 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
228 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
229 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
230 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
231 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
232 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
233 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
234 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
235 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
236 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
237 case Shader::OPCODE_SGE: step(d, s1, s0); break;
238 case Shader::OPCODE_SGN: sgn(d, s0); break;
239 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
240 case Shader::OPCODE_COS: cos(d, s0, pp); break;
241 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
242 case Shader::OPCODE_TAN: tan(d, s0); break;
243 case Shader::OPCODE_ACOS: acos(d, s0); break;
244 case Shader::OPCODE_ASIN: asin(d, s0); break;
245 case Shader::OPCODE_ATAN: atan(d, s0); break;
246 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400247 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
248 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
249 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
250 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
251 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
252 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400253 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
254 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400255 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400256 case Shader::OPCODE_BREAK: BREAK(r); break;
257 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
258 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
259 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
260 case Shader::OPCODE_TEST: TEST(); break;
261 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
262 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
263 case Shader::OPCODE_ELSE: ELSE(r); break;
264 case Shader::OPCODE_ENDIF: ENDIF(r); break;
265 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
266 case Shader::OPCODE_ENDREP: ENDREP(r); break;
267 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
268 case Shader::OPCODE_IF: IF(r, src0); break;
269 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
270 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
271 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
272 case Shader::OPCODE_REP: REP(r, src0); break;
273 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
274 case Shader::OPCODE_RET: RET(r); break;
275 case Shader::OPCODE_LEAVE: LEAVE(r); break;
276 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
277 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400278 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400279 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
280 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
281 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
282 case Shader::OPCODE_ALL: all(d.x, s0); break;
283 case Shader::OPCODE_ANY: any(d.x, s0); break;
284 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400285 case Shader::OPCODE_OR: or(d, s0, s1); break;
286 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
287 case Shader::OPCODE_AND: and(d, s0, s1); break;
288 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
289 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400290 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1); break;
291 case Shader::OPCODE_TEX: TEX(r, d, s0, src1); break;
292 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400293 default:
294 ASSERT(false);
295 }
296
John Bauman19bac1e2014-05-06 15:23:49 -0400297 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400298 {
John Bauman19bac1e2014-05-06 15:23:49 -0400299 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400300 {
John Bauman19bac1e2014-05-06 15:23:49 -0400301 switch(opcode)
302 {
303 case Shader::OPCODE_DIV:
304 if(dst.x) d.x = Trunc(d.x);
305 if(dst.y) d.y = Trunc(d.y);
306 if(dst.z) d.z = Trunc(d.z);
307 if(dst.w) d.w = Trunc(d.w);
308 break;
309 default:
310 break; // No truncation to integer required when arguments are integer
311 }
John Bauman89401822014-05-06 15:04:28 -0400312 }
313
John Bauman19bac1e2014-05-06 15:23:49 -0400314 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400315 {
John Bauman19bac1e2014-05-06 15:23:49 -0400316 if(dst.x) d.x = Max(d.x, Float4(0.0f));
317 if(dst.y) d.y = Max(d.y, Float4(0.0f));
318 if(dst.z) d.z = Max(d.z, Float4(0.0f));
319 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400320
John Bauman19bac1e2014-05-06 15:23:49 -0400321 if(dst.x) d.x = Min(d.x, Float4(1.0f));
322 if(dst.y) d.y = Min(d.y, Float4(1.0f));
323 if(dst.z) d.z = Min(d.z, Float4(1.0f));
324 if(dst.w) d.w = Min(d.w, Float4(1.0f));
325 }
326
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400327 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400328 {
329 Vector4f pDst; // FIXME: Rename
330
331 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400332 {
John Bauman19bac1e2014-05-06 15:23:49 -0400333 case Shader::PARAMETER_VOID: break;
334 case Shader::PARAMETER_TEMP:
335 if(dst.rel.type == Shader::PARAMETER_VOID)
336 {
337 if(dst.x) pDst.x = r.r[dst.index].x;
338 if(dst.y) pDst.y = r.r[dst.index].y;
339 if(dst.z) pDst.z = r.r[dst.index].z;
340 if(dst.w) pDst.w = r.r[dst.index].w;
341 }
342 else
343 {
344 Int a = relativeAddress(r, dst);
345
346 if(dst.x) pDst.x = r.r[dst.index + a].x;
347 if(dst.y) pDst.y = r.r[dst.index + a].y;
348 if(dst.z) pDst.z = r.r[dst.index + a].z;
349 if(dst.w) pDst.w = r.r[dst.index + a].w;
350 }
351 break;
352 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
353 case Shader::PARAMETER_RASTOUT:
354 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400355 {
356 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400357 if(dst.x) pDst.x = r.o[Pos].x;
358 if(dst.y) pDst.y = r.o[Pos].y;
359 if(dst.z) pDst.z = r.o[Pos].z;
360 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400361 break;
362 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400363 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400364 break;
365 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400366 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400367 break;
368 default:
369 ASSERT(false);
370 }
371 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400372 case Shader::PARAMETER_ATTROUT:
373 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
374 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
375 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
376 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400377 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400378 case Shader::PARAMETER_TEXCRDOUT:
379 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400380 if(version < 0x0300)
381 {
John Bauman19bac1e2014-05-06 15:23:49 -0400382 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
383 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
384 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
385 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400386 }
387 else
388 {
John Bauman19bac1e2014-05-06 15:23:49 -0400389 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400390 {
John Bauman19bac1e2014-05-06 15:23:49 -0400391 if(dst.x) pDst.x = r.o[dst.index].x;
392 if(dst.y) pDst.y = r.o[dst.index].y;
393 if(dst.z) pDst.z = r.o[dst.index].z;
394 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400395 }
John Bauman19bac1e2014-05-06 15:23:49 -0400396 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400397 {
398 Int aL = r.aL[r.loopDepth];
399
John Bauman19bac1e2014-05-06 15:23:49 -0400400 if(dst.x) pDst.x = r.o[dst.index + aL].x;
401 if(dst.y) pDst.y = r.o[dst.index + aL].y;
402 if(dst.z) pDst.z = r.o[dst.index + aL].z;
403 if(dst.w) pDst.w = r.o[dst.index + aL].w;
404 }
405 else
406 {
407 Int a = relativeAddress(r, dst);
408
409 if(dst.x) pDst.x = r.o[dst.index + a].x;
410 if(dst.y) pDst.y = r.o[dst.index + a].y;
411 if(dst.z) pDst.z = r.o[dst.index + a].z;
412 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400413 }
414 }
415 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400416 case Shader::PARAMETER_LABEL: break;
417 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
418 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400419 default:
420 ASSERT(false);
421 }
422
John Bauman19bac1e2014-05-06 15:23:49 -0400423 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -0400424
425 Int4 xEnable = enable;
426 Int4 yEnable = enable;
427 Int4 zEnable = enable;
428 Int4 wEnable = enable;
429
430 if(predicate)
431 {
John Bauman19bac1e2014-05-06 15:23:49 -0400432 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400433
434 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
435 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
436 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
437 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
438
John Bauman19bac1e2014-05-06 15:23:49 -0400439 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400440 {
John Bauman19bac1e2014-05-06 15:23:49 -0400441 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
442 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
443 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
444 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400445 }
446 else
447 {
John Bauman19bac1e2014-05-06 15:23:49 -0400448 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
449 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
450 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
451 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400452 }
453 }
454
John Bauman19bac1e2014-05-06 15:23:49 -0400455 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
456 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
457 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
458 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400459
John Bauman19bac1e2014-05-06 15:23:49 -0400460 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
461 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
462 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
463 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400464 }
465
John Bauman19bac1e2014-05-06 15:23:49 -0400466 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400467 {
John Bauman19bac1e2014-05-06 15:23:49 -0400468 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400469 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400470 case Shader::PARAMETER_TEMP:
471 if(dst.rel.type == Shader::PARAMETER_VOID)
472 {
473 if(dst.x) r.r[dst.index].x = d.x;
474 if(dst.y) r.r[dst.index].y = d.y;
475 if(dst.z) r.r[dst.index].z = d.z;
476 if(dst.w) r.r[dst.index].w = d.w;
477 }
478 else
479 {
480 Int a = relativeAddress(r, dst);
481
482 if(dst.x) r.r[dst.index + a].x = d.x;
483 if(dst.y) r.r[dst.index + a].y = d.y;
484 if(dst.z) r.r[dst.index + a].z = d.z;
485 if(dst.w) r.r[dst.index + a].w = d.w;
486 }
John Bauman89401822014-05-06 15:04:28 -0400487 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400488 case Shader::PARAMETER_ADDR:
489 if(dst.x) r.a0.x = d.x;
490 if(dst.y) r.a0.y = d.y;
491 if(dst.z) r.a0.z = d.z;
492 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400493 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400494 case Shader::PARAMETER_RASTOUT:
495 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400496 {
497 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400498 if(dst.x) r.o[Pos].x = d.x;
499 if(dst.y) r.o[Pos].y = d.y;
500 if(dst.z) r.o[Pos].z = d.z;
501 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400502 break;
503 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400504 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400505 break;
506 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400507 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400508 break;
509 default: ASSERT(false);
510 }
511 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400512 case Shader::PARAMETER_ATTROUT:
513 if(dst.x) r.o[D0 + dst.index].x = d.x;
514 if(dst.y) r.o[D0 + dst.index].y = d.y;
515 if(dst.z) r.o[D0 + dst.index].z = d.z;
516 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400517 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400518 case Shader::PARAMETER_TEXCRDOUT:
519 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400520 if(version < 0x0300)
521 {
John Bauman19bac1e2014-05-06 15:23:49 -0400522 if(dst.x) r.o[T0 + dst.index].x = d.x;
523 if(dst.y) r.o[T0 + dst.index].y = d.y;
524 if(dst.z) r.o[T0 + dst.index].z = d.z;
525 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400526 }
527 else
528 {
John Bauman19bac1e2014-05-06 15:23:49 -0400529 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400530 {
John Bauman19bac1e2014-05-06 15:23:49 -0400531 if(dst.x) r.o[dst.index].x = d.x;
532 if(dst.y) r.o[dst.index].y = d.y;
533 if(dst.z) r.o[dst.index].z = d.z;
534 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400535 }
John Bauman19bac1e2014-05-06 15:23:49 -0400536 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400537 {
538 Int aL = r.aL[r.loopDepth];
539
John Bauman19bac1e2014-05-06 15:23:49 -0400540 if(dst.x) r.o[dst.index + aL].x = d.x;
541 if(dst.y) r.o[dst.index + aL].y = d.y;
542 if(dst.z) r.o[dst.index + aL].z = d.z;
543 if(dst.w) r.o[dst.index + aL].w = d.w;
544 }
545 else
546 {
547 Int a = relativeAddress(r, dst);
548
549 if(dst.x) r.o[dst.index + a].x = d.x;
550 if(dst.y) r.o[dst.index + a].y = d.y;
551 if(dst.z) r.o[dst.index + a].z = d.z;
552 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400553 }
554 }
555 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400556 case Shader::PARAMETER_LABEL: break;
557 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
558 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400559 default:
560 ASSERT(false);
561 }
562 }
563 }
564
John Bauman19bac1e2014-05-06 15:23:49 -0400565 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400566 {
567 Nucleus::setInsertBlock(returnBlock);
568 }
569 }
570
571 void VertexProgram::passThrough(Registers &r)
572 {
John Bauman19bac1e2014-05-06 15:23:49 -0400573 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400574 {
575 for(int i = 0; i < 12; i++)
576 {
John Bauman19bac1e2014-05-06 15:23:49 -0400577 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400578
579 switch(usage)
580 {
581 case 0xFF:
582 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400583 case Shader::USAGE_PSIZE:
584 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400585 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400586 case Shader::USAGE_TEXCOORD:
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_POSITION:
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_COLOR:
599 r.o[i].x = r.v[i].x;
600 r.o[i].y = r.v[i].y;
601 r.o[i].z = r.v[i].z;
602 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400603 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400604 case Shader::USAGE_FOG:
605 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400606 break;
607 default:
608 ASSERT(false);
609 }
610 }
611 }
612 else
613 {
John Bauman19bac1e2014-05-06 15:23:49 -0400614 r.o[Pos].x = r.v[PositionT].x;
615 r.o[Pos].y = r.v[PositionT].y;
616 r.o[Pos].z = r.v[PositionT].z;
617 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400618
619 for(int i = 0; i < 2; i++)
620 {
John Bauman19bac1e2014-05-06 15:23:49 -0400621 r.o[D0 + i].x = r.v[Color0 + i].x;
622 r.o[D0 + i].y = r.v[Color0 + i].y;
623 r.o[D0 + i].z = r.v[Color0 + i].z;
624 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400625 }
626
627 for(int i = 0; i < 8; i++)
628 {
John Bauman19bac1e2014-05-06 15:23:49 -0400629 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
630 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
631 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
632 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400633 }
634
John Bauman66b8ab22014-05-06 15:57:45 -0400635 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400636 }
637 }
638
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400639 Vector4f VertexProgram::fetchRegisterF(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400640 {
641 int i = src.index + offset;
642
John Bauman19bac1e2014-05-06 15:23:49 -0400643 Vector4f reg;
John Bauman89401822014-05-06 15:04:28 -0400644
John Bauman89401822014-05-06 15:04:28 -0400645 switch(src.type)
646 {
John Bauman19bac1e2014-05-06 15:23:49 -0400647 case Shader::PARAMETER_TEMP:
648 if(src.rel.type == Shader::PARAMETER_VOID)
649 {
650 reg = r.r[i];
651 }
652 else
653 {
654 reg = r.r[i + relativeAddress(r, src)];
655 }
656 break;
657 case Shader::PARAMETER_CONST:
658 reg = readConstant(r, src, offset);
659 break;
660 case Shader::PARAMETER_INPUT:
661 if(src.rel.type == Shader::PARAMETER_VOID)
662 {
663 reg = r.v[i];
664 }
665 else
666 {
667 reg = r.v[i + relativeAddress(r, src)];
668 }
669 break;
670 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
671 case Shader::PARAMETER_FLOAT4LITERAL:
672 reg.x = Float4(src.value[0]);
673 reg.y = Float4(src.value[1]);
674 reg.z = Float4(src.value[2]);
675 reg.w = Float4(src.value[3]);
676 break;
677 case Shader::PARAMETER_ADDR: reg = r.a0; break;
678 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
679 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
680 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
681 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
682 case Shader::PARAMETER_SAMPLER:
683 if(src.rel.type == Shader::PARAMETER_VOID)
684 {
685 reg.x = As<Float4>(Int4(i));
686 }
687 else if(src.rel.type == Shader::PARAMETER_TEMP)
688 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400689 reg.x = As<Float4>(Int4(i) + As<Int4>(r.r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400690 }
691 return reg;
692 case Shader::PARAMETER_OUTPUT:
693 if(src.rel.type == Shader::PARAMETER_VOID)
694 {
695 reg = r.o[i];
696 }
697 else
698 {
699 reg = r.o[i + relativeAddress(r, src)];
700 }
701 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400702 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400703 reg.x = As<Float>(Int(r.instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400704 return reg;
John Bauman89401822014-05-06 15:04:28 -0400705 default:
706 ASSERT(false);
707 }
708
John Bauman66b8ab22014-05-06 15:57:45 -0400709 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
710 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
711 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
712 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400713
John Bauman66b8ab22014-05-06 15:57:45 -0400714 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400715
716 switch(src.modifier)
717 {
John Bauman19bac1e2014-05-06 15:23:49 -0400718 case Shader::MODIFIER_NONE:
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_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400725 mod.x = -x;
726 mod.y = -y;
727 mod.z = -z;
728 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400729 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400730 case Shader::MODIFIER_ABS:
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_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400737 mod.x = -Abs(x);
738 mod.y = -Abs(y);
739 mod.z = -Abs(z);
740 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400741 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400742 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400743 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
744 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
745 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
746 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400747 break;
748 default:
749 ASSERT(false);
750 }
751
752 return mod;
753 }
754
John Bauman19bac1e2014-05-06 15:23:49 -0400755 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, int offset)
John Bauman89401822014-05-06 15:04:28 -0400756 {
John Bauman19bac1e2014-05-06 15:23:49 -0400757 Vector4f c;
758
759 int i = src.index + offset;
760
761 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
762 {
763 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
764
765 c.x = c.x.xxxx;
766 c.y = c.y.yyyy;
767 c.z = c.z.zzzz;
768 c.w = c.w.wwww;
769
Nicolas Capenseafdb222015-05-15 15:24:08 -0400770 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400771 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500772 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400773 {
774 const Shader::Instruction &instruction = *shader->getInstruction(j);
775
776 if(instruction.opcode == Shader::OPCODE_DEF)
777 {
778 if(instruction.dst.index == i)
779 {
780 c.x = Float4(instruction.src[0].value[0]);
781 c.y = Float4(instruction.src[0].value[1]);
782 c.z = Float4(instruction.src[0].value[2]);
783 c.w = Float4(instruction.src[0].value[3]);
784
785 break;
786 }
787 }
788 }
789 }
790 }
791 else if(src.rel.type == Shader::PARAMETER_LOOP)
792 {
793 Int loopCounter = r.aL[r.loopDepth];
794
795 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
796
797 c.x = c.x.xxxx;
798 c.y = c.y.yyyy;
799 c.z = c.z.zzzz;
800 c.w = c.w.wwww;
801 }
802 else
803 {
804 if(src.rel.deterministic)
805 {
806 Int a = relativeAddress(r, src);
807
808 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
809
810 c.x = c.x.xxxx;
811 c.y = c.y.yyyy;
812 c.z = c.z.zzzz;
813 c.w = c.w.wwww;
814 }
815 else
816 {
817 int component = src.rel.swizzle & 0x03;
818 Float4 a;
819
820 switch(src.rel.type)
821 {
822 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
823 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
824 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
825 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400826 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 -0400827 default: ASSERT(false);
828 }
829
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400830 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400831
832 index = Min(As<UInt4>(index), UInt4(256)); // Clamp to constant register range, c[256] = {0, 0, 0, 0}
833
834 Int index0 = Extract(index, 0);
835 Int index1 = Extract(index, 1);
836 Int index2 = Extract(index, 2);
837 Int index3 = Extract(index, 3);
838
839 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
840 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
841 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
842 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
843
844 transpose4x4(c.x, c.y, c.z, c.w);
845 }
846 }
847
848 return c;
849 }
850
851 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
852 {
853 ASSERT(var.rel.deterministic);
854
855 if(var.rel.type == Shader::PARAMETER_TEMP)
856 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400857 return As<Int>(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400858 }
859 else if(var.rel.type == Shader::PARAMETER_INPUT)
860 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400861 return As<Int>(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400862 }
863 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
864 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400865 return As<Int>(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400866 }
867 else if(var.rel.type == Shader::PARAMETER_CONST)
868 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400869 RValue<Int4> c = *Pointer<Int4>(r.data + OFFSET(DrawData, vs.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -0400870
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400871 return Extract(c, 0) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400872 }
873 else ASSERT(false);
874
875 return 0;
876 }
877
878 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
879 {
880 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -0400881
882 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400883 {
John Baumand4ae8632014-05-06 16:18:33 -0400884 if(shader->containsBreakInstruction() && instruction->analysisBreak)
885 {
886 enable &= r.enableBreak;
887 }
John Bauman19bac1e2014-05-06 15:23:49 -0400888
John Baumand4ae8632014-05-06 16:18:33 -0400889 if(shader->containsContinueInstruction() && instruction->analysisContinue)
890 {
891 enable &= r.enableContinue;
892 }
John Bauman19bac1e2014-05-06 15:23:49 -0400893
John Baumand4ae8632014-05-06 16:18:33 -0400894 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
895 {
896 enable &= r.enableLeave;
897 }
John Bauman19bac1e2014-05-06 15:23:49 -0400898 }
899
900 return enable;
901 }
902
903 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
904 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400905 Vector4f row0 = fetchRegisterF(r, src1, 0);
906 Vector4f row1 = fetchRegisterF(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400907
908 dst.x = dot3(src0, row0);
909 dst.y = dot3(src0, row1);
910 }
911
John Bauman19bac1e2014-05-06 15:23:49 -0400912 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400913 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400914 Vector4f row0 = fetchRegisterF(r, src1, 0);
915 Vector4f row1 = fetchRegisterF(r, src1, 1);
916 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400917
918 dst.x = dot3(src0, row0);
919 dst.y = dot3(src0, row1);
920 dst.z = dot3(src0, row2);
921 }
922
John Bauman19bac1e2014-05-06 15:23:49 -0400923 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400924 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400925 Vector4f row0 = fetchRegisterF(r, src1, 0);
926 Vector4f row1 = fetchRegisterF(r, src1, 1);
927 Vector4f row2 = fetchRegisterF(r, src1, 2);
928 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400929
930 dst.x = dot3(src0, row0);
931 dst.y = dot3(src0, row1);
932 dst.z = dot3(src0, row2);
933 dst.w = dot3(src0, row3);
934 }
935
John Bauman19bac1e2014-05-06 15:23:49 -0400936 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400937 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400938 Vector4f row0 = fetchRegisterF(r, src1, 0);
939 Vector4f row1 = fetchRegisterF(r, src1, 1);
940 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400941
942 dst.x = dot4(src0, row0);
943 dst.y = dot4(src0, row1);
944 dst.z = dot4(src0, row2);
945 }
946
John Bauman19bac1e2014-05-06 15:23:49 -0400947 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400948 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400949 Vector4f row0 = fetchRegisterF(r, src1, 0);
950 Vector4f row1 = fetchRegisterF(r, src1, 1);
951 Vector4f row2 = fetchRegisterF(r, src1, 2);
952 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400953
954 dst.x = dot4(src0, row0);
955 dst.y = dot4(src0, row1);
956 dst.z = dot4(src0, row2);
957 dst.w = dot4(src0, row3);
958 }
959
960 void VertexProgram::BREAK(Registers &r)
961 {
962 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
963 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
964
965 if(breakDepth == 0)
966 {
John Bauman19bac1e2014-05-06 15:23:49 -0400967 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400968 Nucleus::createBr(endBlock);
969 }
970 else
971 {
972 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
973 Bool allBreak = SignMask(r.enableBreak) == 0x0;
974
John Bauman19bac1e2014-05-06 15:23:49 -0400975 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400976 branch(allBreak, endBlock, deadBlock);
977 }
978
979 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400980 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400981 }
982
John Bauman19bac1e2014-05-06 15:23:49 -0400983 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400984 {
985 Int4 condition;
986
987 switch(control)
988 {
John Bauman19bac1e2014-05-06 15:23:49 -0400989 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
990 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
991 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
992 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
993 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
994 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -0400995 default:
996 ASSERT(false);
997 }
998
John Bauman19bac1e2014-05-06 15:23:49 -0400999 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001000 }
1001
1002 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
1003 {
1004 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1005
John Bauman19bac1e2014-05-06 15:23:49 -04001006 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001007 {
1008 condition = ~condition;
1009 }
1010
John Bauman19bac1e2014-05-06 15:23:49 -04001011 BREAK(r, condition);
1012 }
1013
1014 void VertexProgram::BREAK(Registers &r, Int4 &condition)
1015 {
John Bauman89401822014-05-06 15:04:28 -04001016 condition &= r.enableStack[r.enableIndex];
1017
1018 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1019 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1020
1021 r.enableBreak = r.enableBreak & ~condition;
1022 Bool allBreak = SignMask(r.enableBreak) == 0x0;
1023
John Bauman19bac1e2014-05-06 15:23:49 -04001024 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001025 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001026
John Bauman89401822014-05-06 15:04:28 -04001027 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001028 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001029 }
1030
John Bauman19bac1e2014-05-06 15:23:49 -04001031 void VertexProgram::CONTINUE(Registers &r)
1032 {
1033 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1034 }
1035
1036 void VertexProgram::TEST()
1037 {
1038 whileTest = true;
1039 }
1040
1041 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001042 {
1043 if(!labelBlock[labelIndex])
1044 {
1045 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1046 }
1047
John Bauman19bac1e2014-05-06 15:23:49 -04001048 if(callRetBlock[labelIndex].size() > 1)
1049 {
1050 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1051 }
John Bauman89401822014-05-06 15:04:28 -04001052
John Bauman19bac1e2014-05-06 15:23:49 -04001053 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001054
1055 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001056 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1057
1058 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001059 }
1060
John Bauman19bac1e2014-05-06 15:23:49 -04001061 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001062 {
John Bauman19bac1e2014-05-06 15:23:49 -04001063 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001064 {
John Bauman19bac1e2014-05-06 15:23:49 -04001065 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001066 }
John Bauman19bac1e2014-05-06 15:23:49 -04001067 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001068 {
John Bauman19bac1e2014-05-06 15:23:49 -04001069 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001070 }
1071 else ASSERT(false);
1072 }
1073
John Bauman19bac1e2014-05-06 15:23:49 -04001074 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001075 {
1076 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1077
John Bauman19bac1e2014-05-06 15:23:49 -04001078 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001079 {
1080 condition = !condition;
1081 }
1082
1083 if(!labelBlock[labelIndex])
1084 {
1085 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1086 }
1087
John Bauman19bac1e2014-05-06 15:23:49 -04001088 if(callRetBlock[labelIndex].size() > 1)
1089 {
1090 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1091 }
John Bauman89401822014-05-06 15:04:28 -04001092
John Bauman19bac1e2014-05-06 15:23:49 -04001093 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001094
John Bauman19bac1e2014-05-06 15:23:49 -04001095 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1096 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1097
1098 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001099 }
1100
John Bauman19bac1e2014-05-06 15:23:49 -04001101 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001102 {
1103 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1104
John Bauman19bac1e2014-05-06 15:23:49 -04001105 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001106 {
1107 condition = ~condition;
1108 }
1109
1110 condition &= r.enableStack[r.enableIndex];
1111
1112 if(!labelBlock[labelIndex])
1113 {
1114 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1115 }
1116
John Bauman19bac1e2014-05-06 15:23:49 -04001117 if(callRetBlock[labelIndex].size() > 1)
1118 {
1119 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1120 }
John Bauman89401822014-05-06 15:04:28 -04001121
1122 r.enableIndex++;
1123 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001124 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001125
John Bauman19bac1e2014-05-06 15:23:49 -04001126 Bool notAllFalse = SignMask(condition) != 0;
1127 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1128 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001129
1130 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001131 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001132 }
1133
1134 void VertexProgram::ELSE(Registers &r)
1135 {
1136 ifDepth--;
1137
1138 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1139 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1140
1141 if(isConditionalIf[ifDepth])
1142 {
1143 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001144 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001145
1146 branch(notAllFalse, falseBlock, endBlock);
1147
1148 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1149 }
1150 else
1151 {
1152 Nucleus::createBr(endBlock);
1153 Nucleus::setInsertBlock(falseBlock);
1154 }
1155
1156 ifFalseBlock[ifDepth] = endBlock;
1157
1158 ifDepth++;
1159 }
1160
1161 void VertexProgram::ENDIF(Registers &r)
1162 {
1163 ifDepth--;
1164
1165 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1166
1167 Nucleus::createBr(endBlock);
1168 Nucleus::setInsertBlock(endBlock);
1169
1170 if(isConditionalIf[ifDepth])
1171 {
1172 breakDepth--;
1173 r.enableIndex--;
1174 }
1175 }
1176
John Bauman89401822014-05-06 15:04:28 -04001177 void VertexProgram::ENDLOOP(Registers &r)
1178 {
1179 loopRepDepth--;
1180
1181 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
1182
1183 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1184 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1185
1186 Nucleus::createBr(testBlock);
1187 Nucleus::setInsertBlock(endBlock);
1188
1189 r.loopDepth--;
1190 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1191 }
1192
John Bauman19bac1e2014-05-06 15:23:49 -04001193 void VertexProgram::ENDREP(Registers &r)
1194 {
1195 loopRepDepth--;
1196
1197 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1198 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1199
1200 Nucleus::createBr(testBlock);
1201 Nucleus::setInsertBlock(endBlock);
1202
1203 r.loopDepth--;
1204 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1205 }
1206
1207 void VertexProgram::ENDWHILE(Registers &r)
1208 {
1209 loopRepDepth--;
1210
1211 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1212 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1213
1214 Nucleus::createBr(testBlock);
1215 Nucleus::setInsertBlock(endBlock);
1216
1217 r.enableIndex--;
1218 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1219 whileTest = false;
1220 }
1221
John Bauman89401822014-05-06 15:04:28 -04001222 void VertexProgram::IF(Registers &r, const Src &src)
1223 {
John Bauman19bac1e2014-05-06 15:23:49 -04001224 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001225 {
1226 IFb(r, src);
1227 }
John Bauman19bac1e2014-05-06 15:23:49 -04001228 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001229 {
1230 IFp(r, src);
1231 }
John Bauman19bac1e2014-05-06 15:23:49 -04001232 else
1233 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001234 Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
John Bauman19bac1e2014-05-06 15:23:49 -04001235 IF(r, condition);
1236 }
John Bauman89401822014-05-06 15:04:28 -04001237 }
1238
1239 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1240 {
1241 ASSERT(ifDepth < 24 + 4);
1242
1243 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1244
John Bauman19bac1e2014-05-06 15:23:49 -04001245 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001246 {
John Bauman19bac1e2014-05-06 15:23:49 -04001247 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001248 }
1249
1250 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1251 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1252
1253 branch(condition, trueBlock, falseBlock);
1254
1255 isConditionalIf[ifDepth] = false;
1256 ifFalseBlock[ifDepth] = falseBlock;
1257
1258 ifDepth++;
1259 }
1260
John Bauman19bac1e2014-05-06 15:23:49 -04001261 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001262 {
1263 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1264
John Bauman19bac1e2014-05-06 15:23:49 -04001265 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001266 {
1267 condition = ~condition;
1268 }
1269
John Bauman19bac1e2014-05-06 15:23:49 -04001270 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001271 }
1272
John Bauman19bac1e2014-05-06 15:23:49 -04001273 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001274 {
1275 Int4 condition;
1276
1277 switch(control)
1278 {
John Bauman19bac1e2014-05-06 15:23:49 -04001279 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1280 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1281 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1282 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1283 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1284 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001285 default:
1286 ASSERT(false);
1287 }
1288
John Bauman19bac1e2014-05-06 15:23:49 -04001289 IF(r, condition);
1290 }
1291
1292 void VertexProgram::IF(Registers &r, Int4 &condition)
1293 {
John Bauman89401822014-05-06 15:04:28 -04001294 condition &= r.enableStack[r.enableIndex];
1295
1296 r.enableIndex++;
1297 r.enableStack[r.enableIndex] = condition;
1298
1299 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1300 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1301
John Bauman19bac1e2014-05-06 15:23:49 -04001302 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001303
1304 branch(notAllFalse, trueBlock, falseBlock);
1305
1306 isConditionalIf[ifDepth] = true;
1307 ifFalseBlock[ifDepth] = falseBlock;
1308
1309 ifDepth++;
1310 breakDepth++;
1311 }
1312
1313 void VertexProgram::LABEL(int labelIndex)
1314 {
John Bauman19bac1e2014-05-06 15:23:49 -04001315 if(!labelBlock[labelIndex])
1316 {
1317 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1318 }
1319
John Bauman89401822014-05-06 15:04:28 -04001320 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001321 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001322 }
1323
1324 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1325 {
1326 r.loopDepth++;
1327
1328 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1329 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1330 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1331
1332 // FIXME: Compiles to two instructions?
1333 If(r.increment[r.loopDepth] == 0)
1334 {
1335 r.increment[r.loopDepth] = 1;
1336 }
1337
1338 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1339 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1340 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1341
1342 loopRepTestBlock[loopRepDepth] = testBlock;
1343 loopRepEndBlock[loopRepDepth] = endBlock;
1344
1345 // FIXME: jump(testBlock)
1346 Nucleus::createBr(testBlock);
1347 Nucleus::setInsertBlock(testBlock);
1348
1349 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1350 Nucleus::setInsertBlock(loopBlock);
1351
1352 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1353
1354 loopRepDepth++;
1355 breakDepth = 0;
1356 }
1357
1358 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1359 {
1360 r.loopDepth++;
1361
1362 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1363 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1364
1365 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1366 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1367 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1368
1369 loopRepTestBlock[loopRepDepth] = testBlock;
1370 loopRepEndBlock[loopRepDepth] = endBlock;
1371
1372 // FIXME: jump(testBlock)
1373 Nucleus::createBr(testBlock);
1374 Nucleus::setInsertBlock(testBlock);
1375
1376 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1377 Nucleus::setInsertBlock(loopBlock);
1378
1379 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1380
1381 loopRepDepth++;
1382 breakDepth = 0;
1383 }
1384
John Bauman19bac1e2014-05-06 15:23:49 -04001385 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1386 {
1387 r.enableIndex++;
1388
1389 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1390 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1391 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1392
1393 loopRepTestBlock[loopRepDepth] = testBlock;
1394 loopRepEndBlock[loopRepDepth] = endBlock;
1395
1396 Int4 restoreBreak = r.enableBreak;
1397 Int4 restoreContinue = r.enableContinue;
1398
1399 // FIXME: jump(testBlock)
1400 Nucleus::createBr(testBlock);
1401 Nucleus::setInsertBlock(testBlock);
1402 r.enableContinue = restoreContinue;
1403
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001404 const Vector4f &src = fetchRegisterF(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001405 Int4 condition = As<Int4>(src.x);
1406 condition &= r.enableStack[r.enableIndex - 1];
1407 r.enableStack[r.enableIndex] = condition;
1408
1409 Bool notAllFalse = SignMask(condition) != 0;
1410 branch(notAllFalse, loopBlock, endBlock);
1411
1412 Nucleus::setInsertBlock(endBlock);
1413 r.enableBreak = restoreBreak;
1414
1415 Nucleus::setInsertBlock(loopBlock);
1416
1417 loopRepDepth++;
1418 breakDepth = 0;
1419 }
1420
John Bauman89401822014-05-06 15:04:28 -04001421 void VertexProgram::RET(Registers &r)
1422 {
John Bauman19bac1e2014-05-06 15:23:49 -04001423 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001424 {
1425 returnBlock = Nucleus::createBasicBlock();
1426 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001427 }
1428 else
1429 {
John Bauman89401822014-05-06 15:04:28 -04001430 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001431
John Bauman19bac1e2014-05-06 15:23:49 -04001432 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001433 {
John Bauman19bac1e2014-05-06 15:23:49 -04001434 // FIXME: Encapsulate
1435 UInt index = r.callStack[--r.stackIndex];
1436
John Bauman66b8ab22014-05-06 15:57:45 -04001437 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001438 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1439
1440 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1441 {
1442 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1443 }
1444 }
1445 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1446 {
1447 Nucleus::createBr(callRetBlock[currentLabel][0]);
1448 }
1449 else // Function isn't called
1450 {
1451 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001452 }
1453
1454 Nucleus::setInsertBlock(unreachableBlock);
1455 Nucleus::createUnreachable();
1456 }
1457 }
1458
John Bauman19bac1e2014-05-06 15:23:49 -04001459 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001460 {
John Bauman19bac1e2014-05-06 15:23:49 -04001461 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001462
John Bauman19bac1e2014-05-06 15:23:49 -04001463 // FIXME: Return from function if all instances left
1464 // FIXME: Use enableLeave in other control-flow constructs
1465 }
John Bauman89401822014-05-06 15:04:28 -04001466
John Bauman19bac1e2014-05-06 15:23:49 -04001467 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1468 {
1469 Vector4f tmp;
1470 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001471
1472 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1473 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1474 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1475 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1476 }
John Bauman19bac1e2014-05-06 15:23:49 -04001477
1478 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1479 {
1480 Float4 lod = Float4(0.0f);
1481 Vector4f tmp;
1482 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1483
1484 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1485 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1486 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1487 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1488 }
1489
1490 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1491 {
1492 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1493 {
1494 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1495 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1496 }
1497 else
1498 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001499 Int index = As<Int>(Float(fetchRegisterF(r, s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001500
1501 for(int i = 0; i < 16; i++)
1502 {
1503 if(shader->usesSampler(i))
1504 {
1505 If(index == i)
1506 {
1507 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1508 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1509 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1510 }
1511 }
1512 }
1513 }
1514 }
John Bauman89401822014-05-06 15:04:28 -04001515}