blob: 94c2b9e511f80de82529308365f3a884e8b27f51 [file] [log] [blame]
John Bauman89401822014-05-06 15:04:28 -04001// SwiftShader Software Renderer
2//
John Baumand4ae8632014-05-06 16:18:33 -04003// Copyright(c) 2005-2013 TransGaming Inc.
John Bauman89401822014-05-06 15:04:28 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#include "VertexProgram.hpp"
13
14#include "Renderer.hpp"
15#include "VertexShader.hpp"
16#include "Vertex.hpp"
17#include "Half.hpp"
18#include "SamplerCore.hpp"
19#include "Debug.hpp"
20
John Bauman89401822014-05-06 15:04:28 -040021namespace sw
22{
John Bauman19bac1e2014-05-06 15:23:49 -040023 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader) : VertexRoutine(state, shader)
John Bauman89401822014-05-06 15:04:28 -040024 {
John Bauman89401822014-05-06 15:04:28 -040025 ifDepth = 0;
26 loopRepDepth = 0;
27 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040028 currentLabel = -1;
29 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040030
31 for(int i = 0; i < 2048; i++)
32 {
33 labelBlock[i] = 0;
34 }
35 }
36
37 VertexProgram::~VertexProgram()
38 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040039 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040040 {
41 delete sampler[i];
42 }
43 }
44
45 void VertexProgram::pipeline(Registers &r)
46 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040047 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040048 {
49 sampler[i] = new SamplerCore(r.constants, state.samplerState[i]);
50 }
51
52 if(!state.preTransformed)
53 {
John Bauman19bac1e2014-05-06 15:23:49 -040054 program(r);
John Bauman89401822014-05-06 15:04:28 -040055 }
56 else
57 {
58 passThrough(r);
59 }
60 }
61
John Bauman19bac1e2014-05-06 15:23:49 -040062 void VertexProgram::program(Registers &r)
John Bauman89401822014-05-06 15:04:28 -040063 {
John Bauman19bac1e2014-05-06 15:23:49 -040064 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040065
John Bauman19bac1e2014-05-06 15:23:49 -040066 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040067
68 r.enableIndex = 0;
69 r.stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040070
Nicolas Capens4677a5f2014-05-06 16:42:26 -040071 if(shader->containsLeaveInstruction())
72 {
73 r.enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
74 }
75
John Bauman19bac1e2014-05-06 15:23:49 -040076 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050077 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -040078 {
John Bauman19bac1e2014-05-06 15:23:49 -040079 const Shader::Instruction *instruction = shader->getInstruction(i);
80 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -040081
John Bauman19bac1e2014-05-06 15:23:49 -040082 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
83 {
84 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -040085
John Bauman19bac1e2014-05-06 15:23:49 -040086 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
87 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
88 }
89 }
90
Alexis Hetu903e0252014-11-25 14:25:32 -050091 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -040092 {
93 const Shader::Instruction *instruction = shader->getInstruction(i);
94 Shader::Opcode opcode = instruction->opcode;
95
96 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -040097 {
98 continue;
99 }
100
John Bauman19bac1e2014-05-06 15:23:49 -0400101 Dst dst = instruction->dst;
102 Src src0 = instruction->src[0];
103 Src src1 = instruction->src[1];
104 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400105 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400106 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400107
John Bauman19bac1e2014-05-06 15:23:49 -0400108 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400109 Control control = instruction->control;
110 bool integer = dst.type == Shader::PARAMETER_ADDR;
111 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400112
John Bauman19bac1e2014-05-06 15:23:49 -0400113 Vector4f d;
114 Vector4f s0;
115 Vector4f s1;
116 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400117 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400118 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400119
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400120 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegisterF(r, src0);
121 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegisterF(r, src1);
122 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegisterF(r, src2);
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400123 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegisterF(r, src3);
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400124 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegisterF(r, src4);
John Bauman89401822014-05-06 15:04:28 -0400125
126 switch(opcode)
127 {
John Bauman19bac1e2014-05-06 15:23:49 -0400128 case Shader::OPCODE_VS_1_0: break;
129 case Shader::OPCODE_VS_1_1: break;
130 case Shader::OPCODE_VS_2_0: break;
131 case Shader::OPCODE_VS_2_x: break;
132 case Shader::OPCODE_VS_2_sw: break;
133 case Shader::OPCODE_VS_3_0: break;
134 case Shader::OPCODE_VS_3_sw: break;
135 case Shader::OPCODE_DCL: break;
136 case Shader::OPCODE_DEF: break;
137 case Shader::OPCODE_DEFI: break;
138 case Shader::OPCODE_DEFB: break;
139 case Shader::OPCODE_NOP: break;
140 case Shader::OPCODE_ABS: abs(d, s0); break;
141 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400142 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400143 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
144 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
145 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
146 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
147 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
148 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
149 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
150 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
151 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
152 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
153 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
154 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
155 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
156 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
157 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
158 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
159 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400160 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
161 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
162 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400163 case Shader::OPCODE_ATT: att(d, s0, s1); break;
164 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
165 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
166 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
167 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
168 case Shader::OPCODE_FRC: frc(d, s0); break;
169 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
170 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400171 case Shader::OPCODE_ROUND: round(d, s0); break;
Alexis Hetu8e851c12015-06-04 11:30:54 -0400172 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400173 case Shader::OPCODE_CEIL: ceil(d, s0); break;
174 case Shader::OPCODE_LIT: lit(d, s0); break;
175 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
176 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
177 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
178 case Shader::OPCODE_LOG: log(d, s0, pp); break;
179 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
180 case Shader::OPCODE_STEP: step(d, s0, s1); break;
181 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400182 case Shader::OPCODE_FLOATBITSTOINT:
183 case Shader::OPCODE_FLOATBITSTOUINT:
184 case Shader::OPCODE_INTBITSTOFLOAT:
185 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400186 case Shader::OPCODE_M3X2: M3X2(r, d, s0, src1); break;
187 case Shader::OPCODE_M3X3: M3X3(r, d, s0, src1); break;
188 case Shader::OPCODE_M3X4: M3X4(r, d, s0, src1); break;
189 case Shader::OPCODE_M4X3: M4X3(r, d, s0, src1); break;
190 case Shader::OPCODE_M4X4: M4X4(r, d, s0, src1); break;
191 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400192 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400193 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400194 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
195 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400196 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400197 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
198 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400199 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400200 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400201 case Shader::OPCODE_NEG: neg(d, s0); break;
202 case Shader::OPCODE_INEG: ineg(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400203 case Shader::OPCODE_F2B: f2b(d, s0); break;
204 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400205 case Shader::OPCODE_F2I: f2i(d, s0); break;
206 case Shader::OPCODE_I2F: i2f(d, s0); break;
207 case Shader::OPCODE_F2U: f2u(d, s0); break;
208 case Shader::OPCODE_U2F: u2f(d, s0); break;
209 case Shader::OPCODE_I2B: i2b(d, s0); break;
210 case Shader::OPCODE_B2I: b2i(d, s0); break;
211 case Shader::OPCODE_U2B: u2b(d, s0); break;
212 case Shader::OPCODE_B2U: b2u(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400213 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400214 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400215 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
216 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
217 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
218 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
219 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
220 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
221 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400222 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
223 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400224 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400225 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
226 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
227 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
228 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
229 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400230 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
231 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
232 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
233 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
234 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
235 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
236 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
237 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
238 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
239 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
240 case Shader::OPCODE_SGE: step(d, s1, s0); break;
241 case Shader::OPCODE_SGN: sgn(d, s0); break;
242 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
243 case Shader::OPCODE_COS: cos(d, s0, pp); break;
244 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
245 case Shader::OPCODE_TAN: tan(d, s0); break;
246 case Shader::OPCODE_ACOS: acos(d, s0); break;
247 case Shader::OPCODE_ASIN: asin(d, s0); break;
248 case Shader::OPCODE_ATAN: atan(d, s0); break;
249 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400250 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
251 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
252 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
253 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
254 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
255 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400256 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
257 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400258 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400259 case Shader::OPCODE_BREAK: BREAK(r); break;
260 case Shader::OPCODE_BREAKC: BREAKC(r, s0, s1, control); break;
261 case Shader::OPCODE_BREAKP: BREAKP(r, src0); break;
262 case Shader::OPCODE_CONTINUE: CONTINUE(r); break;
263 case Shader::OPCODE_TEST: TEST(); break;
264 case Shader::OPCODE_CALL: CALL(r, dst.label, dst.callSite); break;
265 case Shader::OPCODE_CALLNZ: CALLNZ(r, dst.label, dst.callSite, src0); break;
266 case Shader::OPCODE_ELSE: ELSE(r); break;
267 case Shader::OPCODE_ENDIF: ENDIF(r); break;
268 case Shader::OPCODE_ENDLOOP: ENDLOOP(r); break;
269 case Shader::OPCODE_ENDREP: ENDREP(r); break;
270 case Shader::OPCODE_ENDWHILE: ENDWHILE(r); break;
271 case Shader::OPCODE_IF: IF(r, src0); break;
272 case Shader::OPCODE_IFC: IFC(r, s0, s1, control); break;
273 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
274 case Shader::OPCODE_LOOP: LOOP(r, src1); break;
275 case Shader::OPCODE_REP: REP(r, src0); break;
276 case Shader::OPCODE_WHILE: WHILE(r, src0); break;
277 case Shader::OPCODE_RET: RET(r); break;
278 case Shader::OPCODE_LEAVE: LEAVE(r); break;
279 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
280 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400281 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400282 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
283 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
284 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
285 case Shader::OPCODE_ALL: all(d.x, s0); break;
286 case Shader::OPCODE_ANY: any(d.x, s0); break;
287 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400288 case Shader::OPCODE_OR: or(d, s0, s1); break;
289 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
290 case Shader::OPCODE_AND: and(d, s0, s1); break;
291 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
292 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400293 case Shader::OPCODE_TEXLDL: TEXLDL(r, d, s0, src1); break;
294 case Shader::OPCODE_TEX: TEX(r, d, s0, src1); break;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400295 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(r, d, s0, src1, s2, s3); break;
296 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(r, d, s0, src1, s2); break;
297 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(r, d, s0, src1, s2); break;
298 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(r, d, s0, src1, s2, s3); break;
299 case Shader::OPCODE_TEXGRAD: TEXGRAD(r, d, s0, src1, s2, s3); break;
300 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(r, d, s0, src1, s2, s3, s4); break;
Alexis Hetu9bcb31d2015-07-22 17:03:26 -0400301 case Shader::OPCODE_TEXSIZE: TEXSIZE(r, d, s0.x, src1); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400302 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400303 default:
304 ASSERT(false);
305 }
306
John Bauman19bac1e2014-05-06 15:23:49 -0400307 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400308 {
John Bauman19bac1e2014-05-06 15:23:49 -0400309 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400310 {
John Bauman19bac1e2014-05-06 15:23:49 -0400311 switch(opcode)
312 {
313 case Shader::OPCODE_DIV:
314 if(dst.x) d.x = Trunc(d.x);
315 if(dst.y) d.y = Trunc(d.y);
316 if(dst.z) d.z = Trunc(d.z);
317 if(dst.w) d.w = Trunc(d.w);
318 break;
319 default:
320 break; // No truncation to integer required when arguments are integer
321 }
John Bauman89401822014-05-06 15:04:28 -0400322 }
323
John Bauman19bac1e2014-05-06 15:23:49 -0400324 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400325 {
John Bauman19bac1e2014-05-06 15:23:49 -0400326 if(dst.x) d.x = Max(d.x, Float4(0.0f));
327 if(dst.y) d.y = Max(d.y, Float4(0.0f));
328 if(dst.z) d.z = Max(d.z, Float4(0.0f));
329 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400330
John Bauman19bac1e2014-05-06 15:23:49 -0400331 if(dst.x) d.x = Min(d.x, Float4(1.0f));
332 if(dst.y) d.y = Min(d.y, Float4(1.0f));
333 if(dst.z) d.z = Min(d.z, Float4(1.0f));
334 if(dst.w) d.w = Min(d.w, Float4(1.0f));
335 }
336
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400337 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400338 {
339 Vector4f pDst; // FIXME: Rename
340
341 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400342 {
John Bauman19bac1e2014-05-06 15:23:49 -0400343 case Shader::PARAMETER_VOID: break;
344 case Shader::PARAMETER_TEMP:
345 if(dst.rel.type == Shader::PARAMETER_VOID)
346 {
347 if(dst.x) pDst.x = r.r[dst.index].x;
348 if(dst.y) pDst.y = r.r[dst.index].y;
349 if(dst.z) pDst.z = r.r[dst.index].z;
350 if(dst.w) pDst.w = r.r[dst.index].w;
351 }
352 else
353 {
354 Int a = relativeAddress(r, dst);
355
356 if(dst.x) pDst.x = r.r[dst.index + a].x;
357 if(dst.y) pDst.y = r.r[dst.index + a].y;
358 if(dst.z) pDst.z = r.r[dst.index + a].z;
359 if(dst.w) pDst.w = r.r[dst.index + a].w;
360 }
361 break;
362 case Shader::PARAMETER_ADDR: pDst = r.a0; break;
363 case Shader::PARAMETER_RASTOUT:
364 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400365 {
366 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400367 if(dst.x) pDst.x = r.o[Pos].x;
368 if(dst.y) pDst.y = r.o[Pos].y;
369 if(dst.z) pDst.z = r.o[Pos].z;
370 if(dst.w) pDst.w = r.o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400371 break;
372 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400373 pDst.x = r.o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400374 break;
375 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400376 pDst.x = r.o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400377 break;
378 default:
379 ASSERT(false);
380 }
381 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400382 case Shader::PARAMETER_ATTROUT:
383 if(dst.x) pDst.x = r.o[D0 + dst.index].x;
384 if(dst.y) pDst.y = r.o[D0 + dst.index].y;
385 if(dst.z) pDst.z = r.o[D0 + dst.index].z;
386 if(dst.w) pDst.w = r.o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400387 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400388 case Shader::PARAMETER_TEXCRDOUT:
389 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400390 if(version < 0x0300)
391 {
John Bauman19bac1e2014-05-06 15:23:49 -0400392 if(dst.x) pDst.x = r.o[T0 + dst.index].x;
393 if(dst.y) pDst.y = r.o[T0 + dst.index].y;
394 if(dst.z) pDst.z = r.o[T0 + dst.index].z;
395 if(dst.w) pDst.w = r.o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400396 }
397 else
398 {
John Bauman19bac1e2014-05-06 15:23:49 -0400399 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400400 {
John Bauman19bac1e2014-05-06 15:23:49 -0400401 if(dst.x) pDst.x = r.o[dst.index].x;
402 if(dst.y) pDst.y = r.o[dst.index].y;
403 if(dst.z) pDst.z = r.o[dst.index].z;
404 if(dst.w) pDst.w = r.o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400405 }
John Bauman19bac1e2014-05-06 15:23:49 -0400406 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400407 {
408 Int aL = r.aL[r.loopDepth];
409
John Bauman19bac1e2014-05-06 15:23:49 -0400410 if(dst.x) pDst.x = r.o[dst.index + aL].x;
411 if(dst.y) pDst.y = r.o[dst.index + aL].y;
412 if(dst.z) pDst.z = r.o[dst.index + aL].z;
413 if(dst.w) pDst.w = r.o[dst.index + aL].w;
414 }
415 else
416 {
417 Int a = relativeAddress(r, dst);
418
419 if(dst.x) pDst.x = r.o[dst.index + a].x;
420 if(dst.y) pDst.y = r.o[dst.index + a].y;
421 if(dst.z) pDst.z = r.o[dst.index + a].z;
422 if(dst.w) pDst.w = r.o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400423 }
424 }
425 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400426 case Shader::PARAMETER_LABEL: break;
427 case Shader::PARAMETER_PREDICATE: pDst = r.p0; break;
428 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400429 default:
430 ASSERT(false);
431 }
432
John Bauman19bac1e2014-05-06 15:23:49 -0400433 Int4 enable = enableMask(r, instruction);
John Bauman89401822014-05-06 15:04:28 -0400434
435 Int4 xEnable = enable;
436 Int4 yEnable = enable;
437 Int4 zEnable = enable;
438 Int4 wEnable = enable;
439
440 if(predicate)
441 {
John Bauman19bac1e2014-05-06 15:23:49 -0400442 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400443
444 Float4 xPredicate = r.p0[(pSwizzle >> 0) & 0x03];
445 Float4 yPredicate = r.p0[(pSwizzle >> 2) & 0x03];
446 Float4 zPredicate = r.p0[(pSwizzle >> 4) & 0x03];
447 Float4 wPredicate = r.p0[(pSwizzle >> 6) & 0x03];
448
John Bauman19bac1e2014-05-06 15:23:49 -0400449 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400450 {
John Bauman19bac1e2014-05-06 15:23:49 -0400451 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
452 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
453 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
454 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400455 }
456 else
457 {
John Bauman19bac1e2014-05-06 15:23:49 -0400458 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
459 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
460 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
461 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400462 }
463 }
464
John Bauman19bac1e2014-05-06 15:23:49 -0400465 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
466 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
467 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
468 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400469
John Bauman19bac1e2014-05-06 15:23:49 -0400470 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
471 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
472 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
473 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400474 }
475
John Bauman19bac1e2014-05-06 15:23:49 -0400476 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400477 {
John Bauman19bac1e2014-05-06 15:23:49 -0400478 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400479 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400480 case Shader::PARAMETER_TEMP:
481 if(dst.rel.type == Shader::PARAMETER_VOID)
482 {
483 if(dst.x) r.r[dst.index].x = d.x;
484 if(dst.y) r.r[dst.index].y = d.y;
485 if(dst.z) r.r[dst.index].z = d.z;
486 if(dst.w) r.r[dst.index].w = d.w;
487 }
488 else
489 {
490 Int a = relativeAddress(r, dst);
491
492 if(dst.x) r.r[dst.index + a].x = d.x;
493 if(dst.y) r.r[dst.index + a].y = d.y;
494 if(dst.z) r.r[dst.index + a].z = d.z;
495 if(dst.w) r.r[dst.index + a].w = d.w;
496 }
John Bauman89401822014-05-06 15:04:28 -0400497 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400498 case Shader::PARAMETER_ADDR:
499 if(dst.x) r.a0.x = d.x;
500 if(dst.y) r.a0.y = d.y;
501 if(dst.z) r.a0.z = d.z;
502 if(dst.w) r.a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400503 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400504 case Shader::PARAMETER_RASTOUT:
505 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400506 {
507 case 0:
John Bauman19bac1e2014-05-06 15:23:49 -0400508 if(dst.x) r.o[Pos].x = d.x;
509 if(dst.y) r.o[Pos].y = d.y;
510 if(dst.z) r.o[Pos].z = d.z;
511 if(dst.w) r.o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400512 break;
513 case 1:
John Bauman19bac1e2014-05-06 15:23:49 -0400514 r.o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400515 break;
516 case 2:
John Bauman19bac1e2014-05-06 15:23:49 -0400517 r.o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400518 break;
519 default: ASSERT(false);
520 }
521 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400522 case Shader::PARAMETER_ATTROUT:
523 if(dst.x) r.o[D0 + dst.index].x = d.x;
524 if(dst.y) r.o[D0 + dst.index].y = d.y;
525 if(dst.z) r.o[D0 + dst.index].z = d.z;
526 if(dst.w) r.o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400527 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400528 case Shader::PARAMETER_TEXCRDOUT:
529 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400530 if(version < 0x0300)
531 {
John Bauman19bac1e2014-05-06 15:23:49 -0400532 if(dst.x) r.o[T0 + dst.index].x = d.x;
533 if(dst.y) r.o[T0 + dst.index].y = d.y;
534 if(dst.z) r.o[T0 + dst.index].z = d.z;
535 if(dst.w) r.o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400536 }
537 else
538 {
John Bauman19bac1e2014-05-06 15:23:49 -0400539 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400540 {
John Bauman19bac1e2014-05-06 15:23:49 -0400541 if(dst.x) r.o[dst.index].x = d.x;
542 if(dst.y) r.o[dst.index].y = d.y;
543 if(dst.z) r.o[dst.index].z = d.z;
544 if(dst.w) r.o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400545 }
John Bauman19bac1e2014-05-06 15:23:49 -0400546 else if(dst.rel.type == Shader::PARAMETER_LOOP)
John Bauman89401822014-05-06 15:04:28 -0400547 {
548 Int aL = r.aL[r.loopDepth];
549
John Bauman19bac1e2014-05-06 15:23:49 -0400550 if(dst.x) r.o[dst.index + aL].x = d.x;
551 if(dst.y) r.o[dst.index + aL].y = d.y;
552 if(dst.z) r.o[dst.index + aL].z = d.z;
553 if(dst.w) r.o[dst.index + aL].w = d.w;
554 }
555 else
556 {
557 Int a = relativeAddress(r, dst);
558
559 if(dst.x) r.o[dst.index + a].x = d.x;
560 if(dst.y) r.o[dst.index + a].y = d.y;
561 if(dst.z) r.o[dst.index + a].z = d.z;
562 if(dst.w) r.o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400563 }
564 }
565 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400566 case Shader::PARAMETER_LABEL: break;
567 case Shader::PARAMETER_PREDICATE: r.p0 = d; break;
568 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400569 default:
570 ASSERT(false);
571 }
572 }
573 }
574
John Bauman19bac1e2014-05-06 15:23:49 -0400575 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400576 {
577 Nucleus::setInsertBlock(returnBlock);
578 }
579 }
580
581 void VertexProgram::passThrough(Registers &r)
582 {
John Bauman19bac1e2014-05-06 15:23:49 -0400583 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400584 {
585 for(int i = 0; i < 12; i++)
586 {
John Bauman19bac1e2014-05-06 15:23:49 -0400587 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400588
589 switch(usage)
590 {
591 case 0xFF:
592 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400593 case Shader::USAGE_PSIZE:
594 r.o[i].y = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400595 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400596 case Shader::USAGE_TEXCOORD:
597 r.o[i].x = r.v[i].x;
598 r.o[i].y = r.v[i].y;
599 r.o[i].z = r.v[i].z;
600 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400601 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400602 case Shader::USAGE_POSITION:
603 r.o[i].x = r.v[i].x;
604 r.o[i].y = r.v[i].y;
605 r.o[i].z = r.v[i].z;
606 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400607 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400608 case Shader::USAGE_COLOR:
609 r.o[i].x = r.v[i].x;
610 r.o[i].y = r.v[i].y;
611 r.o[i].z = r.v[i].z;
612 r.o[i].w = r.v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400613 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400614 case Shader::USAGE_FOG:
615 r.o[i].x = r.v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400616 break;
617 default:
618 ASSERT(false);
619 }
620 }
621 }
622 else
623 {
John Bauman19bac1e2014-05-06 15:23:49 -0400624 r.o[Pos].x = r.v[PositionT].x;
625 r.o[Pos].y = r.v[PositionT].y;
626 r.o[Pos].z = r.v[PositionT].z;
627 r.o[Pos].w = r.v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400628
629 for(int i = 0; i < 2; i++)
630 {
John Bauman19bac1e2014-05-06 15:23:49 -0400631 r.o[D0 + i].x = r.v[Color0 + i].x;
632 r.o[D0 + i].y = r.v[Color0 + i].y;
633 r.o[D0 + i].z = r.v[Color0 + i].z;
634 r.o[D0 + i].w = r.v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400635 }
636
637 for(int i = 0; i < 8; i++)
638 {
John Bauman19bac1e2014-05-06 15:23:49 -0400639 r.o[T0 + i].x = r.v[TexCoord0 + i].x;
640 r.o[T0 + i].y = r.v[TexCoord0 + i].y;
641 r.o[T0 + i].z = r.v[TexCoord0 + i].z;
642 r.o[T0 + i].w = r.v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400643 }
644
John Bauman66b8ab22014-05-06 15:57:45 -0400645 r.o[Pts].y = r.v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400646 }
647 }
648
Nicolas Capens5d961882016-01-01 23:18:14 -0500649 Vector4f VertexProgram::fetchRegisterF(Registers &r, const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400650 {
John Bauman19bac1e2014-05-06 15:23:49 -0400651 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500652 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400653
John Bauman89401822014-05-06 15:04:28 -0400654 switch(src.type)
655 {
John Bauman19bac1e2014-05-06 15:23:49 -0400656 case Shader::PARAMETER_TEMP:
657 if(src.rel.type == Shader::PARAMETER_VOID)
658 {
659 reg = r.r[i];
660 }
661 else
662 {
663 reg = r.r[i + relativeAddress(r, src)];
664 }
665 break;
666 case Shader::PARAMETER_CONST:
667 reg = readConstant(r, src, offset);
668 break;
669 case Shader::PARAMETER_INPUT:
670 if(src.rel.type == Shader::PARAMETER_VOID)
671 {
672 reg = r.v[i];
673 }
674 else
675 {
676 reg = r.v[i + relativeAddress(r, src)];
677 }
678 break;
679 case Shader::PARAMETER_VOID: return r.r[0]; // Dummy
680 case Shader::PARAMETER_FLOAT4LITERAL:
681 reg.x = Float4(src.value[0]);
682 reg.y = Float4(src.value[1]);
683 reg.z = Float4(src.value[2]);
684 reg.w = Float4(src.value[3]);
685 break;
686 case Shader::PARAMETER_ADDR: reg = r.a0; break;
687 case Shader::PARAMETER_CONSTBOOL: return r.r[0]; // Dummy
688 case Shader::PARAMETER_CONSTINT: return r.r[0]; // Dummy
689 case Shader::PARAMETER_LOOP: return r.r[0]; // Dummy
690 case Shader::PARAMETER_PREDICATE: return r.r[0]; // Dummy
691 case Shader::PARAMETER_SAMPLER:
692 if(src.rel.type == Shader::PARAMETER_VOID)
693 {
694 reg.x = As<Float4>(Int4(i));
695 }
696 else if(src.rel.type == Shader::PARAMETER_TEMP)
697 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400698 reg.x = As<Float4>(Int4(i) + As<Int4>(r.r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400699 }
700 return reg;
701 case Shader::PARAMETER_OUTPUT:
702 if(src.rel.type == Shader::PARAMETER_VOID)
703 {
704 reg = r.o[i];
705 }
706 else
707 {
708 reg = r.o[i + relativeAddress(r, src)];
709 }
710 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400711 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400712 reg.x = As<Float>(Int(r.instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400713 return reg;
John Bauman89401822014-05-06 15:04:28 -0400714 default:
715 ASSERT(false);
716 }
717
John Bauman66b8ab22014-05-06 15:57:45 -0400718 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
719 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
720 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
721 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400722
John Bauman66b8ab22014-05-06 15:57:45 -0400723 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400724
725 switch(src.modifier)
726 {
John Bauman19bac1e2014-05-06 15:23:49 -0400727 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400728 mod.x = x;
729 mod.y = y;
730 mod.z = z;
731 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400732 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400733 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400734 mod.x = -x;
735 mod.y = -y;
736 mod.z = -z;
737 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400738 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400739 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400740 mod.x = Abs(x);
741 mod.y = Abs(y);
742 mod.z = Abs(z);
743 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400744 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400745 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400746 mod.x = -Abs(x);
747 mod.y = -Abs(y);
748 mod.z = -Abs(z);
749 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400750 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400751 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400752 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
753 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
754 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
755 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400756 break;
757 default:
758 ASSERT(false);
759 }
760
761 return mod;
762 }
763
Nicolas Capens5d961882016-01-01 23:18:14 -0500764 Vector4f VertexProgram::readConstant(Registers &r, const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400765 {
John Bauman19bac1e2014-05-06 15:23:49 -0400766 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500767 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400768
769 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
770 {
771 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]));
772
773 c.x = c.x.xxxx;
774 c.y = c.y.yyyy;
775 c.z = c.z.zzzz;
776 c.w = c.w.wwww;
777
Nicolas Capenseafdb222015-05-15 15:24:08 -0400778 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400779 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500780 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400781 {
782 const Shader::Instruction &instruction = *shader->getInstruction(j);
783
784 if(instruction.opcode == Shader::OPCODE_DEF)
785 {
786 if(instruction.dst.index == i)
787 {
788 c.x = Float4(instruction.src[0].value[0]);
789 c.y = Float4(instruction.src[0].value[1]);
790 c.z = Float4(instruction.src[0].value[2]);
791 c.w = Float4(instruction.src[0].value[3]);
792
793 break;
794 }
795 }
796 }
797 }
798 }
799 else if(src.rel.type == Shader::PARAMETER_LOOP)
800 {
801 Int loopCounter = r.aL[r.loopDepth];
802
803 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + loopCounter * 16);
804
805 c.x = c.x.xxxx;
806 c.y = c.y.yyyy;
807 c.z = c.z.zzzz;
808 c.w = c.w.wwww;
809 }
810 else
811 {
812 if(src.rel.deterministic)
813 {
814 Int a = relativeAddress(r, src);
815
816 c.x = c.y = c.z = c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c[i]) + a * 16);
817
818 c.x = c.x.xxxx;
819 c.y = c.y.yyyy;
820 c.z = c.z.zzzz;
821 c.w = c.w.wwww;
822 }
823 else
824 {
825 int component = src.rel.swizzle & 0x03;
826 Float4 a;
827
828 switch(src.rel.type)
829 {
830 case Shader::PARAMETER_ADDR: a = r.a0[component]; break;
831 case Shader::PARAMETER_TEMP: a = r.r[src.rel.index][component]; break;
832 case Shader::PARAMETER_INPUT: a = r.v[src.rel.index][component]; break;
833 case Shader::PARAMETER_OUTPUT: a = r.o[src.rel.index][component]; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400834 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 -0400835 default: ASSERT(false);
836 }
837
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400838 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400839
Alexis Hetu028f41b2016-01-13 14:40:47 -0500840 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
John Bauman19bac1e2014-05-06 15:23:49 -0400841
842 Int index0 = Extract(index, 0);
843 Int index1 = Extract(index, 1);
844 Int index2 = Extract(index, 2);
845 Int index3 = Extract(index, 3);
846
847 c.x = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index0 * 16, 16);
848 c.y = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index1 * 16, 16);
849 c.z = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index2 * 16, 16);
850 c.w = *Pointer<Float4>(r.data + OFFSET(DrawData,vs.c) + index3 * 16, 16);
851
852 transpose4x4(c.x, c.y, c.z, c.w);
853 }
854 }
855
856 return c;
857 }
858
859 Int VertexProgram::relativeAddress(Registers &r, const Shader::Parameter &var)
860 {
861 ASSERT(var.rel.deterministic);
862
863 if(var.rel.type == Shader::PARAMETER_TEMP)
864 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400865 return As<Int>(Extract(r.r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400866 }
867 else if(var.rel.type == Shader::PARAMETER_INPUT)
868 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400869 return As<Int>(Extract(r.v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400870 }
871 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
872 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400873 return As<Int>(Extract(r.o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400874 }
875 else if(var.rel.type == Shader::PARAMETER_CONST)
876 {
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400877 RValue<Int4> c = *Pointer<Int4>(r.data + OFFSET(DrawData, vs.c[var.rel.index]));
John Bauman19bac1e2014-05-06 15:23:49 -0400878
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400879 return Extract(c, 0) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400880 }
881 else ASSERT(false);
882
883 return 0;
884 }
885
886 Int4 VertexProgram::enableMask(Registers &r, const Shader::Instruction *instruction)
887 {
888 Int4 enable = instruction->analysisBranch ? Int4(r.enableStack[r.enableIndex]) : Int4(0xFFFFFFFF);
John Baumand4ae8632014-05-06 16:18:33 -0400889
890 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400891 {
John Baumand4ae8632014-05-06 16:18:33 -0400892 if(shader->containsBreakInstruction() && instruction->analysisBreak)
893 {
894 enable &= r.enableBreak;
895 }
John Bauman19bac1e2014-05-06 15:23:49 -0400896
John Baumand4ae8632014-05-06 16:18:33 -0400897 if(shader->containsContinueInstruction() && instruction->analysisContinue)
898 {
899 enable &= r.enableContinue;
900 }
John Bauman19bac1e2014-05-06 15:23:49 -0400901
John Baumand4ae8632014-05-06 16:18:33 -0400902 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
903 {
904 enable &= r.enableLeave;
905 }
John Bauman19bac1e2014-05-06 15:23:49 -0400906 }
907
908 return enable;
909 }
910
911 void VertexProgram::M3X2(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
912 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400913 Vector4f row0 = fetchRegisterF(r, src1, 0);
914 Vector4f row1 = fetchRegisterF(r, src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400915
916 dst.x = dot3(src0, row0);
917 dst.y = dot3(src0, row1);
918 }
919
John Bauman19bac1e2014-05-06 15:23:49 -0400920 void VertexProgram::M3X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400921 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400922 Vector4f row0 = fetchRegisterF(r, src1, 0);
923 Vector4f row1 = fetchRegisterF(r, src1, 1);
924 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400925
926 dst.x = dot3(src0, row0);
927 dst.y = dot3(src0, row1);
928 dst.z = dot3(src0, row2);
929 }
930
John Bauman19bac1e2014-05-06 15:23:49 -0400931 void VertexProgram::M3X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400932 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400933 Vector4f row0 = fetchRegisterF(r, src1, 0);
934 Vector4f row1 = fetchRegisterF(r, src1, 1);
935 Vector4f row2 = fetchRegisterF(r, src1, 2);
936 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400937
938 dst.x = dot3(src0, row0);
939 dst.y = dot3(src0, row1);
940 dst.z = dot3(src0, row2);
941 dst.w = dot3(src0, row3);
942 }
943
John Bauman19bac1e2014-05-06 15:23:49 -0400944 void VertexProgram::M4X3(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400945 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400946 Vector4f row0 = fetchRegisterF(r, src1, 0);
947 Vector4f row1 = fetchRegisterF(r, src1, 1);
948 Vector4f row2 = fetchRegisterF(r, src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400949
950 dst.x = dot4(src0, row0);
951 dst.y = dot4(src0, row1);
952 dst.z = dot4(src0, row2);
953 }
954
John Bauman19bac1e2014-05-06 15:23:49 -0400955 void VertexProgram::M4X4(Registers &r, Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400956 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400957 Vector4f row0 = fetchRegisterF(r, src1, 0);
958 Vector4f row1 = fetchRegisterF(r, src1, 1);
959 Vector4f row2 = fetchRegisterF(r, src1, 2);
960 Vector4f row3 = fetchRegisterF(r, src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400961
962 dst.x = dot4(src0, row0);
963 dst.y = dot4(src0, row1);
964 dst.z = dot4(src0, row2);
965 dst.w = dot4(src0, row3);
966 }
967
968 void VertexProgram::BREAK(Registers &r)
969 {
970 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
971 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
972
973 if(breakDepth == 0)
974 {
John Bauman19bac1e2014-05-06 15:23:49 -0400975 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400976 Nucleus::createBr(endBlock);
977 }
978 else
979 {
980 r.enableBreak = r.enableBreak & ~r.enableStack[r.enableIndex];
981 Bool allBreak = SignMask(r.enableBreak) == 0x0;
982
John Bauman19bac1e2014-05-06 15:23:49 -0400983 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400984 branch(allBreak, endBlock, deadBlock);
985 }
986
987 Nucleus::setInsertBlock(deadBlock);
John Bauman19bac1e2014-05-06 15:23:49 -0400988 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -0400989 }
990
John Bauman19bac1e2014-05-06 15:23:49 -0400991 void VertexProgram::BREAKC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -0400992 {
993 Int4 condition;
994
995 switch(control)
996 {
John Bauman19bac1e2014-05-06 15:23:49 -0400997 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
998 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
999 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1000 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1001 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1002 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001003 default:
1004 ASSERT(false);
1005 }
1006
John Bauman19bac1e2014-05-06 15:23:49 -04001007 BREAK(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001008 }
1009
1010 void VertexProgram::BREAKP(Registers &r, const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
1011 {
1012 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1013
John Bauman19bac1e2014-05-06 15:23:49 -04001014 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001015 {
1016 condition = ~condition;
1017 }
1018
John Bauman19bac1e2014-05-06 15:23:49 -04001019 BREAK(r, condition);
1020 }
1021
1022 void VertexProgram::BREAK(Registers &r, Int4 &condition)
1023 {
John Bauman89401822014-05-06 15:04:28 -04001024 condition &= r.enableStack[r.enableIndex];
1025
1026 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1027 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1028
1029 r.enableBreak = r.enableBreak & ~condition;
1030 Bool allBreak = SignMask(r.enableBreak) == 0x0;
1031
John Bauman19bac1e2014-05-06 15:23:49 -04001032 r.enableIndex = r.enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001033 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001034
John Bauman89401822014-05-06 15:04:28 -04001035 Nucleus::setInsertBlock(continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001036 r.enableIndex = r.enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001037 }
1038
John Bauman19bac1e2014-05-06 15:23:49 -04001039 void VertexProgram::CONTINUE(Registers &r)
1040 {
1041 r.enableContinue = r.enableContinue & ~r.enableStack[r.enableIndex];
1042 }
1043
1044 void VertexProgram::TEST()
1045 {
1046 whileTest = true;
1047 }
1048
1049 void VertexProgram::CALL(Registers &r, int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001050 {
1051 if(!labelBlock[labelIndex])
1052 {
1053 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1054 }
1055
John Bauman19bac1e2014-05-06 15:23:49 -04001056 if(callRetBlock[labelIndex].size() > 1)
1057 {
1058 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1059 }
John Bauman89401822014-05-06 15:04:28 -04001060
John Bauman19bac1e2014-05-06 15:23:49 -04001061 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001062
1063 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001064 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1065
1066 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001067 }
1068
John Bauman19bac1e2014-05-06 15:23:49 -04001069 void VertexProgram::CALLNZ(Registers &r, int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001070 {
John Bauman19bac1e2014-05-06 15:23:49 -04001071 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001072 {
John Bauman19bac1e2014-05-06 15:23:49 -04001073 CALLNZb(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001074 }
John Bauman19bac1e2014-05-06 15:23:49 -04001075 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001076 {
John Bauman19bac1e2014-05-06 15:23:49 -04001077 CALLNZp(r, labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001078 }
1079 else ASSERT(false);
1080 }
1081
John Bauman19bac1e2014-05-06 15:23:49 -04001082 void VertexProgram::CALLNZb(Registers &r, int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001083 {
1084 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1085
John Bauman19bac1e2014-05-06 15:23:49 -04001086 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001087 {
1088 condition = !condition;
1089 }
1090
1091 if(!labelBlock[labelIndex])
1092 {
1093 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1094 }
1095
John Bauman19bac1e2014-05-06 15:23:49 -04001096 if(callRetBlock[labelIndex].size() > 1)
1097 {
1098 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1099 }
John Bauman89401822014-05-06 15:04:28 -04001100
John Bauman19bac1e2014-05-06 15:23:49 -04001101 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001102
John Bauman19bac1e2014-05-06 15:23:49 -04001103 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1104 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1105
1106 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001107 }
1108
John Bauman19bac1e2014-05-06 15:23:49 -04001109 void VertexProgram::CALLNZp(Registers &r, int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001110 {
1111 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1112
John Bauman19bac1e2014-05-06 15:23:49 -04001113 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001114 {
1115 condition = ~condition;
1116 }
1117
1118 condition &= r.enableStack[r.enableIndex];
1119
1120 if(!labelBlock[labelIndex])
1121 {
1122 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1123 }
1124
John Bauman19bac1e2014-05-06 15:23:49 -04001125 if(callRetBlock[labelIndex].size() > 1)
1126 {
1127 r.callStack[r.stackIndex++] = UInt(callSiteIndex);
1128 }
John Bauman89401822014-05-06 15:04:28 -04001129
1130 r.enableIndex++;
1131 r.enableStack[r.enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001132 Int4 restoreLeave = r.enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001133
John Bauman19bac1e2014-05-06 15:23:49 -04001134 Bool notAllFalse = SignMask(condition) != 0;
1135 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1136 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001137
1138 r.enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001139 r.enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001140 }
1141
1142 void VertexProgram::ELSE(Registers &r)
1143 {
1144 ifDepth--;
1145
1146 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1147 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1148
1149 if(isConditionalIf[ifDepth])
1150 {
1151 Int4 condition = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001152 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001153
1154 branch(notAllFalse, falseBlock, endBlock);
1155
1156 r.enableStack[r.enableIndex] = ~r.enableStack[r.enableIndex] & r.enableStack[r.enableIndex - 1];
1157 }
1158 else
1159 {
1160 Nucleus::createBr(endBlock);
1161 Nucleus::setInsertBlock(falseBlock);
1162 }
1163
1164 ifFalseBlock[ifDepth] = endBlock;
1165
1166 ifDepth++;
1167 }
1168
1169 void VertexProgram::ENDIF(Registers &r)
1170 {
1171 ifDepth--;
1172
1173 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1174
1175 Nucleus::createBr(endBlock);
1176 Nucleus::setInsertBlock(endBlock);
1177
1178 if(isConditionalIf[ifDepth])
1179 {
1180 breakDepth--;
1181 r.enableIndex--;
1182 }
1183 }
1184
John Bauman89401822014-05-06 15:04:28 -04001185 void VertexProgram::ENDLOOP(Registers &r)
1186 {
1187 loopRepDepth--;
1188
1189 r.aL[r.loopDepth] = r.aL[r.loopDepth] + r.increment[r.loopDepth]; // FIXME: +=
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
John Bauman19bac1e2014-05-06 15:23:49 -04001201 void VertexProgram::ENDREP(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.loopDepth--;
1212 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1213 }
1214
1215 void VertexProgram::ENDWHILE(Registers &r)
1216 {
1217 loopRepDepth--;
1218
1219 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1220 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1221
1222 Nucleus::createBr(testBlock);
1223 Nucleus::setInsertBlock(endBlock);
1224
1225 r.enableIndex--;
1226 r.enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1227 whileTest = false;
1228 }
1229
John Bauman89401822014-05-06 15:04:28 -04001230 void VertexProgram::IF(Registers &r, const Src &src)
1231 {
John Bauman19bac1e2014-05-06 15:23:49 -04001232 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001233 {
1234 IFb(r, src);
1235 }
John Bauman19bac1e2014-05-06 15:23:49 -04001236 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001237 {
1238 IFp(r, src);
1239 }
John Bauman19bac1e2014-05-06 15:23:49 -04001240 else
1241 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001242 Int4 condition = As<Int4>(fetchRegisterF(r, src).x);
John Bauman19bac1e2014-05-06 15:23:49 -04001243 IF(r, condition);
1244 }
John Bauman89401822014-05-06 15:04:28 -04001245 }
1246
1247 void VertexProgram::IFb(Registers &r, const Src &boolRegister)
1248 {
1249 ASSERT(ifDepth < 24 + 4);
1250
1251 Bool condition = (*Pointer<Byte>(r.data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
1252
John Bauman19bac1e2014-05-06 15:23:49 -04001253 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001254 {
John Bauman19bac1e2014-05-06 15:23:49 -04001255 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001256 }
1257
1258 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1259 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1260
1261 branch(condition, trueBlock, falseBlock);
1262
1263 isConditionalIf[ifDepth] = false;
1264 ifFalseBlock[ifDepth] = falseBlock;
1265
1266 ifDepth++;
1267 }
1268
John Bauman19bac1e2014-05-06 15:23:49 -04001269 void VertexProgram::IFp(Registers &r, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001270 {
1271 Int4 condition = As<Int4>(r.p0[predicateRegister.swizzle & 0x3]);
1272
John Bauman19bac1e2014-05-06 15:23:49 -04001273 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001274 {
1275 condition = ~condition;
1276 }
1277
John Bauman19bac1e2014-05-06 15:23:49 -04001278 IF(r, condition);
John Bauman89401822014-05-06 15:04:28 -04001279 }
1280
John Bauman19bac1e2014-05-06 15:23:49 -04001281 void VertexProgram::IFC(Registers &r, Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001282 {
1283 Int4 condition;
1284
1285 switch(control)
1286 {
John Bauman19bac1e2014-05-06 15:23:49 -04001287 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1288 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1289 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1290 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1291 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1292 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001293 default:
1294 ASSERT(false);
1295 }
1296
John Bauman19bac1e2014-05-06 15:23:49 -04001297 IF(r, condition);
1298 }
1299
1300 void VertexProgram::IF(Registers &r, Int4 &condition)
1301 {
John Bauman89401822014-05-06 15:04:28 -04001302 condition &= r.enableStack[r.enableIndex];
1303
1304 r.enableIndex++;
1305 r.enableStack[r.enableIndex] = condition;
1306
1307 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1308 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1309
John Bauman19bac1e2014-05-06 15:23:49 -04001310 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001311
1312 branch(notAllFalse, trueBlock, falseBlock);
1313
1314 isConditionalIf[ifDepth] = true;
1315 ifFalseBlock[ifDepth] = falseBlock;
1316
1317 ifDepth++;
1318 breakDepth++;
1319 }
1320
1321 void VertexProgram::LABEL(int labelIndex)
1322 {
John Bauman19bac1e2014-05-06 15:23:49 -04001323 if(!labelBlock[labelIndex])
1324 {
1325 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1326 }
1327
John Bauman89401822014-05-06 15:04:28 -04001328 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001329 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001330 }
1331
1332 void VertexProgram::LOOP(Registers &r, const Src &integerRegister)
1333 {
1334 r.loopDepth++;
1335
1336 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1337 r.aL[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1338 r.increment[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
1339
1340 // FIXME: Compiles to two instructions?
1341 If(r.increment[r.loopDepth] == 0)
1342 {
1343 r.increment[r.loopDepth] = 1;
1344 }
1345
1346 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1347 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1348 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1349
1350 loopRepTestBlock[loopRepDepth] = testBlock;
1351 loopRepEndBlock[loopRepDepth] = endBlock;
1352
1353 // FIXME: jump(testBlock)
1354 Nucleus::createBr(testBlock);
1355 Nucleus::setInsertBlock(testBlock);
1356
1357 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1358 Nucleus::setInsertBlock(loopBlock);
1359
1360 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1361
1362 loopRepDepth++;
1363 breakDepth = 0;
1364 }
1365
1366 void VertexProgram::REP(Registers &r, const Src &integerRegister)
1367 {
1368 r.loopDepth++;
1369
1370 r.iteration[r.loopDepth] = *Pointer<Int>(r.data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1371 r.aL[r.loopDepth] = r.aL[r.loopDepth - 1];
1372
1373 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1374 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1375 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1376
1377 loopRepTestBlock[loopRepDepth] = testBlock;
1378 loopRepEndBlock[loopRepDepth] = endBlock;
1379
1380 // FIXME: jump(testBlock)
1381 Nucleus::createBr(testBlock);
1382 Nucleus::setInsertBlock(testBlock);
1383
1384 branch(r.iteration[r.loopDepth] > 0, loopBlock, endBlock);
1385 Nucleus::setInsertBlock(loopBlock);
1386
1387 r.iteration[r.loopDepth] = r.iteration[r.loopDepth] - 1; // FIXME: --
1388
1389 loopRepDepth++;
1390 breakDepth = 0;
1391 }
1392
John Bauman19bac1e2014-05-06 15:23:49 -04001393 void VertexProgram::WHILE(Registers &r, const Src &temporaryRegister)
1394 {
1395 r.enableIndex++;
1396
1397 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1398 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1399 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1400
1401 loopRepTestBlock[loopRepDepth] = testBlock;
1402 loopRepEndBlock[loopRepDepth] = endBlock;
1403
1404 Int4 restoreBreak = r.enableBreak;
1405 Int4 restoreContinue = r.enableContinue;
1406
1407 // FIXME: jump(testBlock)
1408 Nucleus::createBr(testBlock);
1409 Nucleus::setInsertBlock(testBlock);
1410 r.enableContinue = restoreContinue;
1411
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001412 const Vector4f &src = fetchRegisterF(r, temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001413 Int4 condition = As<Int4>(src.x);
1414 condition &= r.enableStack[r.enableIndex - 1];
1415 r.enableStack[r.enableIndex] = condition;
1416
1417 Bool notAllFalse = SignMask(condition) != 0;
1418 branch(notAllFalse, loopBlock, endBlock);
1419
1420 Nucleus::setInsertBlock(endBlock);
1421 r.enableBreak = restoreBreak;
1422
1423 Nucleus::setInsertBlock(loopBlock);
1424
1425 loopRepDepth++;
1426 breakDepth = 0;
1427 }
1428
John Bauman89401822014-05-06 15:04:28 -04001429 void VertexProgram::RET(Registers &r)
1430 {
John Bauman19bac1e2014-05-06 15:23:49 -04001431 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001432 {
1433 returnBlock = Nucleus::createBasicBlock();
1434 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001435 }
1436 else
1437 {
John Bauman89401822014-05-06 15:04:28 -04001438 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001439
John Bauman19bac1e2014-05-06 15:23:49 -04001440 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001441 {
John Bauman19bac1e2014-05-06 15:23:49 -04001442 // FIXME: Encapsulate
1443 UInt index = r.callStack[--r.stackIndex];
1444
John Bauman66b8ab22014-05-06 15:57:45 -04001445 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001446 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1447
1448 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1449 {
1450 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1451 }
1452 }
1453 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1454 {
1455 Nucleus::createBr(callRetBlock[currentLabel][0]);
1456 }
1457 else // Function isn't called
1458 {
1459 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001460 }
1461
1462 Nucleus::setInsertBlock(unreachableBlock);
1463 Nucleus::createUnreachable();
1464 }
1465 }
1466
John Bauman19bac1e2014-05-06 15:23:49 -04001467 void VertexProgram::LEAVE(Registers &r)
John Bauman89401822014-05-06 15:04:28 -04001468 {
John Bauman19bac1e2014-05-06 15:23:49 -04001469 r.enableLeave = r.enableLeave & ~r.enableStack[r.enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001470
John Bauman19bac1e2014-05-06 15:23:49 -04001471 // FIXME: Return from function if all instances left
1472 // FIXME: Use enableLeave in other control-flow constructs
1473 }
John Bauman89401822014-05-06 15:04:28 -04001474
John Bauman19bac1e2014-05-06 15:23:49 -04001475 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1476 {
1477 Vector4f tmp;
1478 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, src0.w);
John Bauman89401822014-05-06 15:04:28 -04001479
1480 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1481 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1482 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1483 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1484 }
John Bauman19bac1e2014-05-06 15:23:49 -04001485
1486 void VertexProgram::TEX(Registers &r, Vector4f &dst, Vector4f &src0, const Src &src1)
1487 {
1488 Float4 lod = Float4(0.0f);
1489 Vector4f tmp;
1490 sampleTexture(r, tmp, src1, src0.x, src0.y, src0.z, lod);
1491
1492 dst.x = tmp[(src1.swizzle >> 0) & 0x3];
1493 dst.y = tmp[(src1.swizzle >> 2) & 0x3];
1494 dst.z = tmp[(src1.swizzle >> 4) & 0x3];
1495 dst.w = tmp[(src1.swizzle >> 6) & 0x3];
1496 }
1497
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001498 void VertexProgram::TEXOFFSET(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
1499 {
1500 UNIMPLEMENTED();
1501 }
1502
1503 void VertexProgram::TEXLDL(Registers &r, Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
1504 {
1505 UNIMPLEMENTED();
1506 }
1507
1508 void VertexProgram::TEXELFETCH(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
1509 {
1510 UNIMPLEMENTED();
1511 }
1512
1513 void VertexProgram::TEXELFETCH(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
1514 {
1515 UNIMPLEMENTED();
1516 }
1517
1518 void VertexProgram::TEXGRAD(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
1519 {
1520 UNIMPLEMENTED();
1521 }
1522
1523 void VertexProgram::TEXGRAD(Registers &r, Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
1524 {
1525 UNIMPLEMENTED();
1526 }
1527
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001528 void VertexProgram::TEXSIZE(Registers &r, Vector4f &dst, Float4 &lod, const Src &src1)
1529 {
1530 Pointer<Byte> textureMipmap = r.data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
1531 for(int i = 0; i < 4; ++i)
1532 {
1533 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1534 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1535 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1536 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1537 }
1538 }
1539
John Bauman19bac1e2014-05-06 15:23:49 -04001540 void VertexProgram::sampleTexture(Registers &r, Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q)
1541 {
1542 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1543 {
1544 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + s.index * sizeof(Texture);
1545 sampler[s.index]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1546 }
1547 else
1548 {
Alexis Hetuaf1970c2015-04-17 14:26:07 -04001549 Int index = As<Int>(Float(fetchRegisterF(r, s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001550
1551 for(int i = 0; i < 16; i++)
1552 {
1553 if(shader->usesSampler(i))
1554 {
1555 If(index == i)
1556 {
1557 Pointer<Byte> texture = r.data + OFFSET(DrawData,mipmap[16]) + i * sizeof(Texture);
1558 sampler[i]->sampleTexture(texture, c, u, v, w, q, r.a0, r.a0, false, false, true);
1559 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1560 }
1561 }
1562 }
1563 }
1564 }
John Bauman89401822014-05-06 15:04:28 -04001565}