blob: 1aeb683e279df2ddc9effeccb7a194a29b76065e [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "VertexProgram.hpp"
16
17#include "Renderer.hpp"
18#include "VertexShader.hpp"
19#include "Vertex.hpp"
20#include "Half.hpp"
21#include "SamplerCore.hpp"
22#include "Debug.hpp"
23
John Bauman89401822014-05-06 15:04:28 -040024namespace sw
25{
Nicolas Capens7551ac62016-01-20 17:11:53 -050026 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader)
27 : VertexRoutine(state, shader), shader(shader), r(shader->dynamicallyIndexedTemporaries)
John Bauman89401822014-05-06 15:04:28 -040028 {
John Bauman89401822014-05-06 15:04:28 -040029 ifDepth = 0;
30 loopRepDepth = 0;
31 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040032 currentLabel = -1;
33 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040034
35 for(int i = 0; i < 2048; i++)
36 {
37 labelBlock[i] = 0;
38 }
Nicolas Capens7551ac62016-01-20 17:11:53 -050039
40 loopDepth = -1;
41 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
42
43 if(shader && shader->containsBreakInstruction())
44 {
45 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
46 }
47
48 if(shader && shader->containsContinueInstruction())
49 {
50 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
51 }
52
53 if(shader->instanceIdDeclared)
54 {
55 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
56 }
John Bauman89401822014-05-06 15:04:28 -040057 }
58
59 VertexProgram::~VertexProgram()
60 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040061 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040062 {
63 delete sampler[i];
64 }
65 }
66
Nicolas Capensb4fb3672016-01-15 17:02:41 -050067 void VertexProgram::pipeline()
John Bauman89401822014-05-06 15:04:28 -040068 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040069 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040070 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050071 sampler[i] = new SamplerCore(constants, state.samplerState[i]);
John Bauman89401822014-05-06 15:04:28 -040072 }
73
74 if(!state.preTransformed)
75 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050076 program();
John Bauman89401822014-05-06 15:04:28 -040077 }
78 else
79 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050080 passThrough();
John Bauman89401822014-05-06 15:04:28 -040081 }
82 }
83
Nicolas Capensb4fb3672016-01-15 17:02:41 -050084 void VertexProgram::program()
John Bauman89401822014-05-06 15:04:28 -040085 {
John Bauman19bac1e2014-05-06 15:23:49 -040086 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040087
John Bauman19bac1e2014-05-06 15:23:49 -040088 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040089
Nicolas Capens7551ac62016-01-20 17:11:53 -050090 enableIndex = 0;
91 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040092
Nicolas Capens4677a5f2014-05-06 16:42:26 -040093 if(shader->containsLeaveInstruction())
94 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050095 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040096 }
97
John Bauman19bac1e2014-05-06 15:23:49 -040098 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050099 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400100 {
John Bauman19bac1e2014-05-06 15:23:49 -0400101 const Shader::Instruction *instruction = shader->getInstruction(i);
102 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400103
John Bauman19bac1e2014-05-06 15:23:49 -0400104 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
105 {
106 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400107
John Bauman19bac1e2014-05-06 15:23:49 -0400108 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
109 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
110 }
111 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500112
Alexis Hetu903e0252014-11-25 14:25:32 -0500113 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400114 {
115 const Shader::Instruction *instruction = shader->getInstruction(i);
116 Shader::Opcode opcode = instruction->opcode;
117
118 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400119 {
120 continue;
121 }
122
John Bauman19bac1e2014-05-06 15:23:49 -0400123 Dst dst = instruction->dst;
124 Src src0 = instruction->src[0];
125 Src src1 = instruction->src[1];
126 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400127 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400128 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400129
John Bauman19bac1e2014-05-06 15:23:49 -0400130 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400131 Control control = instruction->control;
132 bool integer = dst.type == Shader::PARAMETER_ADDR;
133 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400134
John Bauman19bac1e2014-05-06 15:23:49 -0400135 Vector4f d;
136 Vector4f s0;
137 Vector4f s1;
138 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400139 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400140 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400141
Nicolas Capensc2534f42016-04-04 11:13:24 -0400142 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
143 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
144 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
145 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
146 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400147
148 switch(opcode)
149 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500150 case Shader::OPCODE_VS_1_0: break;
151 case Shader::OPCODE_VS_1_1: break;
152 case Shader::OPCODE_VS_2_0: break;
153 case Shader::OPCODE_VS_2_x: break;
154 case Shader::OPCODE_VS_2_sw: break;
155 case Shader::OPCODE_VS_3_0: break;
156 case Shader::OPCODE_VS_3_sw: break;
157 case Shader::OPCODE_DCL: break;
158 case Shader::OPCODE_DEF: break;
159 case Shader::OPCODE_DEFI: break;
160 case Shader::OPCODE_DEFB: break;
161 case Shader::OPCODE_NOP: break;
162 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400163 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500164 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400165 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500166 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
167 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
168 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
169 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
170 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
171 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
172 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
173 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
174 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
175 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
176 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
177 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
178 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
179 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
180 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
181 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
182 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400183 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
184 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
185 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500186 case Shader::OPCODE_ATT: att(d, s0, s1); break;
187 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
188 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
189 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
190 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
191 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400192 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
193 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400194 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500195 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400196 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500197 case Shader::OPCODE_LIT: lit(d, s0); break;
198 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
199 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
200 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
201 case Shader::OPCODE_LOG: log(d, s0, pp); break;
202 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
203 case Shader::OPCODE_STEP: step(d, s0, s1); break;
204 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400205 case Shader::OPCODE_FLOATBITSTOINT:
206 case Shader::OPCODE_FLOATBITSTOUINT:
207 case Shader::OPCODE_INTBITSTOFLOAT:
208 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400209 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
210 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400211 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400212 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
213 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400214 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500215 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
216 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
217 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
218 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
219 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
220 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
221 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
222 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400223 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
224 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500225 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400226 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
227 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500228 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400229 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400230 case Shader::OPCODE_NEG: neg(d, s0); break;
231 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500232 case Shader::OPCODE_F2B: f2b(d, s0); break;
233 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400234 case Shader::OPCODE_F2I: f2i(d, s0); break;
235 case Shader::OPCODE_I2F: i2f(d, s0); break;
236 case Shader::OPCODE_F2U: f2u(d, s0); break;
237 case Shader::OPCODE_U2F: u2f(d, s0); break;
238 case Shader::OPCODE_I2B: i2b(d, s0); break;
239 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500240 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400241 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500242 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
243 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
244 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
245 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
246 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
247 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
248 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400249 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
250 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500251 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400252 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
253 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
254 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500255 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
256 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
257 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
258 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
259 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
260 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
261 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
262 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
263 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
264 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
265 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
266 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
267 case Shader::OPCODE_SGE: step(d, s1, s0); break;
268 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400269 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500270 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
271 case Shader::OPCODE_COS: cos(d, s0, pp); break;
272 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
273 case Shader::OPCODE_TAN: tan(d, s0); break;
274 case Shader::OPCODE_ACOS: acos(d, s0); break;
275 case Shader::OPCODE_ASIN: asin(d, s0); break;
276 case Shader::OPCODE_ATAN: atan(d, s0); break;
277 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
278 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
279 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
280 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
281 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
282 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
283 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
284 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
285 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400286 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500287 case Shader::OPCODE_BREAK: BREAK(); break;
288 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
289 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
290 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
291 case Shader::OPCODE_TEST: TEST(); break;
292 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
293 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
294 case Shader::OPCODE_ELSE: ELSE(); break;
295 case Shader::OPCODE_ENDIF: ENDIF(); break;
296 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
297 case Shader::OPCODE_ENDREP: ENDREP(); break;
298 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400299 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500300 case Shader::OPCODE_IF: IF(src0); break;
301 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
302 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
303 case Shader::OPCODE_LOOP: LOOP(src1); break;
304 case Shader::OPCODE_REP: REP(src0); break;
305 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400306 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500307 case Shader::OPCODE_RET: RET(); break;
308 case Shader::OPCODE_LEAVE: LEAVE(); break;
309 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
310 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400311 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500312 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
313 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
314 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
315 case Shader::OPCODE_ALL: all(d.x, s0); break;
316 case Shader::OPCODE_ANY: any(d.x, s0); break;
317 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400318 case Shader::OPCODE_OR: or(d, s0, s1); break;
319 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
320 case Shader::OPCODE_AND: and(d, s0, s1); break;
321 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
322 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500323 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
324 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400325 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500326 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
327 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2); break;
328 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2, s3); break;
329 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
330 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
331 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
332 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400333 default:
334 ASSERT(false);
335 }
336
John Bauman19bac1e2014-05-06 15:23:49 -0400337 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400338 {
John Bauman19bac1e2014-05-06 15:23:49 -0400339 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400340 {
John Bauman19bac1e2014-05-06 15:23:49 -0400341 switch(opcode)
342 {
343 case Shader::OPCODE_DIV:
344 if(dst.x) d.x = Trunc(d.x);
345 if(dst.y) d.y = Trunc(d.y);
346 if(dst.z) d.z = Trunc(d.z);
347 if(dst.w) d.w = Trunc(d.w);
348 break;
349 default:
350 break; // No truncation to integer required when arguments are integer
351 }
John Bauman89401822014-05-06 15:04:28 -0400352 }
353
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400355 {
John Bauman19bac1e2014-05-06 15:23:49 -0400356 if(dst.x) d.x = Max(d.x, Float4(0.0f));
357 if(dst.y) d.y = Max(d.y, Float4(0.0f));
358 if(dst.z) d.z = Max(d.z, Float4(0.0f));
359 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400360
John Bauman19bac1e2014-05-06 15:23:49 -0400361 if(dst.x) d.x = Min(d.x, Float4(1.0f));
362 if(dst.y) d.y = Min(d.y, Float4(1.0f));
363 if(dst.z) d.z = Min(d.z, Float4(1.0f));
364 if(dst.w) d.w = Min(d.w, Float4(1.0f));
365 }
366
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400367 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400368 {
369 Vector4f pDst; // FIXME: Rename
370
371 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400372 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500373 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400374 case Shader::PARAMETER_TEMP:
375 if(dst.rel.type == Shader::PARAMETER_VOID)
376 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500377 if(dst.x) pDst.x = r[dst.index].x;
378 if(dst.y) pDst.y = r[dst.index].y;
379 if(dst.z) pDst.z = r[dst.index].z;
380 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400381 }
382 else
383 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500384 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400385
Nicolas Capens7551ac62016-01-20 17:11:53 -0500386 if(dst.x) pDst.x = r[dst.index + a].x;
387 if(dst.y) pDst.y = r[dst.index + a].y;
388 if(dst.z) pDst.z = r[dst.index + a].z;
389 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400390 }
391 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500392 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400393 case Shader::PARAMETER_RASTOUT:
394 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400395 {
396 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500397 if(dst.x) pDst.x = o[Pos].x;
398 if(dst.y) pDst.y = o[Pos].y;
399 if(dst.z) pDst.z = o[Pos].z;
400 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400401 break;
402 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500403 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400404 break;
405 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500406 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400407 break;
408 default:
409 ASSERT(false);
410 }
411 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400412 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400413 if(dst.x) pDst.x = o[C0 + dst.index].x;
414 if(dst.y) pDst.y = o[C0 + dst.index].y;
415 if(dst.z) pDst.z = o[C0 + dst.index].z;
416 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400417 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400418 case Shader::PARAMETER_TEXCRDOUT:
419 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400420 if(version < 0x0300)
421 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500422 if(dst.x) pDst.x = o[T0 + dst.index].x;
423 if(dst.y) pDst.y = o[T0 + dst.index].y;
424 if(dst.z) pDst.z = o[T0 + dst.index].z;
425 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400426 }
427 else
428 {
John Bauman19bac1e2014-05-06 15:23:49 -0400429 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400430 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500431 if(dst.x) pDst.x = o[dst.index].x;
432 if(dst.y) pDst.y = o[dst.index].y;
433 if(dst.z) pDst.z = o[dst.index].z;
434 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400435 }
John Bauman19bac1e2014-05-06 15:23:49 -0400436 else
437 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500438 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400439
Nicolas Capens7551ac62016-01-20 17:11:53 -0500440 if(dst.x) pDst.x = o[dst.index + a].x;
441 if(dst.y) pDst.y = o[dst.index + a].y;
442 if(dst.z) pDst.z = o[dst.index + a].z;
443 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400444 }
445 }
446 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500447 case Shader::PARAMETER_LABEL: break;
448 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
449 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400450 default:
451 ASSERT(false);
452 }
453
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500454 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400455
456 Int4 xEnable = enable;
457 Int4 yEnable = enable;
458 Int4 zEnable = enable;
459 Int4 wEnable = enable;
460
461 if(predicate)
462 {
John Bauman19bac1e2014-05-06 15:23:49 -0400463 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400464
Nicolas Capens7551ac62016-01-20 17:11:53 -0500465 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
466 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
467 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
468 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400469
John Bauman19bac1e2014-05-06 15:23:49 -0400470 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400471 {
John Bauman19bac1e2014-05-06 15:23:49 -0400472 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
473 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
474 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
475 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400476 }
477 else
478 {
John Bauman19bac1e2014-05-06 15:23:49 -0400479 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
480 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
481 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
482 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400483 }
484 }
485
John Bauman19bac1e2014-05-06 15:23:49 -0400486 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
487 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
488 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
489 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400490
John Bauman19bac1e2014-05-06 15:23:49 -0400491 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
492 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
493 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
494 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400495 }
496
John Bauman19bac1e2014-05-06 15:23:49 -0400497 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400498 {
John Bauman19bac1e2014-05-06 15:23:49 -0400499 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400500 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400501 case Shader::PARAMETER_TEMP:
502 if(dst.rel.type == Shader::PARAMETER_VOID)
503 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500504 if(dst.x) r[dst.index].x = d.x;
505 if(dst.y) r[dst.index].y = d.y;
506 if(dst.z) r[dst.index].z = d.z;
507 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400508 }
509 else
510 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500511 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400512
Nicolas Capens7551ac62016-01-20 17:11:53 -0500513 if(dst.x) r[dst.index + a].x = d.x;
514 if(dst.y) r[dst.index + a].y = d.y;
515 if(dst.z) r[dst.index + a].z = d.z;
516 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400517 }
John Bauman89401822014-05-06 15:04:28 -0400518 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400519 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500520 if(dst.x) a0.x = d.x;
521 if(dst.y) a0.y = d.y;
522 if(dst.z) a0.z = d.z;
523 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400524 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400525 case Shader::PARAMETER_RASTOUT:
526 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400527 {
528 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500529 if(dst.x) o[Pos].x = d.x;
530 if(dst.y) o[Pos].y = d.y;
531 if(dst.z) o[Pos].z = d.z;
532 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400533 break;
534 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500535 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400536 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500537 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500538 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400539 break;
540 default: ASSERT(false);
541 }
542 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500543 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400544 if(dst.x) o[C0 + dst.index].x = d.x;
545 if(dst.y) o[C0 + dst.index].y = d.y;
546 if(dst.z) o[C0 + dst.index].z = d.z;
547 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400548 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400549 case Shader::PARAMETER_TEXCRDOUT:
550 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400551 if(version < 0x0300)
552 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500553 if(dst.x) o[T0 + dst.index].x = d.x;
554 if(dst.y) o[T0 + dst.index].y = d.y;
555 if(dst.z) o[T0 + dst.index].z = d.z;
556 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400557 }
558 else
559 {
John Bauman19bac1e2014-05-06 15:23:49 -0400560 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400561 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500562 if(dst.x) o[dst.index].x = d.x;
563 if(dst.y) o[dst.index].y = d.y;
564 if(dst.z) o[dst.index].z = d.z;
565 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400566 }
John Bauman19bac1e2014-05-06 15:23:49 -0400567 else
568 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500569 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400570
Nicolas Capens7551ac62016-01-20 17:11:53 -0500571 if(dst.x) o[dst.index + a].x = d.x;
572 if(dst.y) o[dst.index + a].y = d.y;
573 if(dst.z) o[dst.index + a].z = d.z;
574 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400575 }
576 }
577 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500578 case Shader::PARAMETER_LABEL: break;
579 case Shader::PARAMETER_PREDICATE: p0 = d; break;
580 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400581 default:
582 ASSERT(false);
583 }
584 }
585 }
586
John Bauman19bac1e2014-05-06 15:23:49 -0400587 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400588 {
589 Nucleus::setInsertBlock(returnBlock);
590 }
591 }
592
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500593 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400594 {
John Bauman19bac1e2014-05-06 15:23:49 -0400595 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400596 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400597 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400598 {
John Bauman19bac1e2014-05-06 15:23:49 -0400599 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400600
601 switch(usage)
602 {
603 case 0xFF:
604 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400605 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500606 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400607 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400608 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500609 o[i].x = v[i].x;
610 o[i].y = v[i].y;
611 o[i].z = v[i].z;
612 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400613 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400614 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500615 o[i].x = v[i].x;
616 o[i].y = v[i].y;
617 o[i].z = v[i].z;
618 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400619 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400620 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500621 o[i].x = v[i].x;
622 o[i].y = v[i].y;
623 o[i].z = v[i].z;
624 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400625 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400626 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500627 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400628 break;
629 default:
630 ASSERT(false);
631 }
632 }
633 }
634 else
635 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500636 o[Pos].x = v[PositionT].x;
637 o[Pos].y = v[PositionT].y;
638 o[Pos].z = v[PositionT].z;
639 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400640
641 for(int i = 0; i < 2; i++)
642 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400643 o[C0 + i].x = v[Color0 + i].x;
644 o[C0 + i].y = v[Color0 + i].y;
645 o[C0 + i].z = v[Color0 + i].z;
646 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400647 }
648
649 for(int i = 0; i < 8; i++)
650 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500651 o[T0 + i].x = v[TexCoord0 + i].x;
652 o[T0 + i].y = v[TexCoord0 + i].y;
653 o[T0 + i].z = v[TexCoord0 + i].z;
654 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400655 }
656
Nicolas Capens7551ac62016-01-20 17:11:53 -0500657 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400658 }
659 }
660
Nicolas Capensc2534f42016-04-04 11:13:24 -0400661 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400662 {
John Bauman19bac1e2014-05-06 15:23:49 -0400663 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500664 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400665
John Bauman89401822014-05-06 15:04:28 -0400666 switch(src.type)
667 {
John Bauman19bac1e2014-05-06 15:23:49 -0400668 case Shader::PARAMETER_TEMP:
669 if(src.rel.type == Shader::PARAMETER_VOID)
670 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500671 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400672 }
673 else
674 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400675 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400676 }
677 break;
678 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500679 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400680 break;
681 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400682 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400683 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500684 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400685 }
686 else
687 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400688 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400689 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400690 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500691 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400692 case Shader::PARAMETER_FLOAT4LITERAL:
693 reg.x = Float4(src.value[0]);
694 reg.y = Float4(src.value[1]);
695 reg.z = Float4(src.value[2]);
696 reg.w = Float4(src.value[3]);
697 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500698 case Shader::PARAMETER_ADDR: reg = a0; break;
699 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
700 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
701 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
702 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400703 case Shader::PARAMETER_SAMPLER:
704 if(src.rel.type == Shader::PARAMETER_VOID)
705 {
706 reg.x = As<Float4>(Int4(i));
707 }
708 else if(src.rel.type == Shader::PARAMETER_TEMP)
709 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500710 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400711 }
712 return reg;
713 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400714 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400715 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500716 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400717 }
718 else
719 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400720 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400721 }
722 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400723 case Shader::PARAMETER_MISCTYPE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500724 reg.x = As<Float>(Int(instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400725 return reg;
John Bauman89401822014-05-06 15:04:28 -0400726 default:
727 ASSERT(false);
728 }
729
John Bauman66b8ab22014-05-06 15:57:45 -0400730 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
731 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
732 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
733 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400734
John Bauman66b8ab22014-05-06 15:57:45 -0400735 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400736
737 switch(src.modifier)
738 {
John Bauman19bac1e2014-05-06 15:23:49 -0400739 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400740 mod.x = x;
741 mod.y = y;
742 mod.z = z;
743 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400744 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400745 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400746 mod.x = -x;
747 mod.y = -y;
748 mod.z = -z;
749 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400750 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400751 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400752 mod.x = Abs(x);
753 mod.y = Abs(y);
754 mod.z = Abs(z);
755 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400756 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400757 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400758 mod.x = -Abs(x);
759 mod.y = -Abs(y);
760 mod.z = -Abs(z);
761 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400762 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400763 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400764 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
765 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
766 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
767 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400768 break;
769 default:
770 ASSERT(false);
771 }
772
773 return mod;
774 }
775
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400776 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
777 {
778 if(bufferIndex == -1)
779 {
780 return data + OFFSET(DrawData, vs.c[index]);
781 }
782 else
783 {
784 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
785 }
786 }
787
788 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
789 {
790 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
791 }
792
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500793 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400794 {
John Bauman19bac1e2014-05-06 15:23:49 -0400795 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500796 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400797
798 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
799 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400800 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400801
802 c.x = c.x.xxxx;
803 c.y = c.y.yyyy;
804 c.z = c.z.zzzz;
805 c.w = c.w.wwww;
806
Nicolas Capenseafdb222015-05-15 15:24:08 -0400807 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400808 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500809 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400810 {
811 const Shader::Instruction &instruction = *shader->getInstruction(j);
812
813 if(instruction.opcode == Shader::OPCODE_DEF)
814 {
815 if(instruction.dst.index == i)
816 {
817 c.x = Float4(instruction.src[0].value[0]);
818 c.y = Float4(instruction.src[0].value[1]);
819 c.z = Float4(instruction.src[0].value[2]);
820 c.w = Float4(instruction.src[0].value[3]);
821
822 break;
823 }
824 }
825 }
826 }
827 }
828 else if(src.rel.type == Shader::PARAMETER_LOOP)
829 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500830 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400831
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400832 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400833
834 c.x = c.x.xxxx;
835 c.y = c.y.yyyy;
836 c.z = c.z.zzzz;
837 c.w = c.w.wwww;
838 }
839 else
840 {
841 if(src.rel.deterministic)
842 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400843 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400844
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400845 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500846
John Bauman19bac1e2014-05-06 15:23:49 -0400847 c.x = c.x.xxxx;
848 c.y = c.y.yyyy;
849 c.z = c.z.zzzz;
850 c.w = c.w.wwww;
851 }
852 else
853 {
854 int component = src.rel.swizzle & 0x03;
855 Float4 a;
856
857 switch(src.rel.type)
858 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500859 case Shader::PARAMETER_ADDR: a = a0[component]; break;
860 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
861 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
862 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400863 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400864 default: ASSERT(false);
865 }
866
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400867 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400868
Alexis Hetu028f41b2016-01-13 14:40:47 -0500869 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500870
John Bauman19bac1e2014-05-06 15:23:49 -0400871 Int index0 = Extract(index, 0);
872 Int index1 = Extract(index, 1);
873 Int index2 = Extract(index, 2);
874 Int index3 = Extract(index, 3);
875
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400876 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
877 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
878 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
879 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400880
881 transpose4x4(c.x, c.y, c.z, c.w);
882 }
883 }
884
885 return c;
886 }
887
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400888 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400889 {
890 ASSERT(var.rel.deterministic);
891
892 if(var.rel.type == Shader::PARAMETER_TEMP)
893 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500894 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400895 }
896 else if(var.rel.type == Shader::PARAMETER_INPUT)
897 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500898 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400899 }
900 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
901 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500902 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400903 }
904 else if(var.rel.type == Shader::PARAMETER_CONST)
905 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500906 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400907 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500908 else if(var.rel.type == Shader::PARAMETER_LOOP)
909 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500910 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500911 }
John Bauman19bac1e2014-05-06 15:23:49 -0400912 else ASSERT(false);
913
914 return 0;
915 }
916
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500917 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400918 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500919 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500920
John Baumand4ae8632014-05-06 16:18:33 -0400921 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400922 {
John Baumand4ae8632014-05-06 16:18:33 -0400923 if(shader->containsBreakInstruction() && instruction->analysisBreak)
924 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500925 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400926 }
John Bauman19bac1e2014-05-06 15:23:49 -0400927
John Baumand4ae8632014-05-06 16:18:33 -0400928 if(shader->containsContinueInstruction() && instruction->analysisContinue)
929 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500930 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400931 }
John Bauman19bac1e2014-05-06 15:23:49 -0400932
John Baumand4ae8632014-05-06 16:18:33 -0400933 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
934 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500935 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400936 }
John Bauman19bac1e2014-05-06 15:23:49 -0400937 }
938
939 return enable;
940 }
941
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500942 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400943 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400944 Vector4f row0 = fetchRegister(src1, 0);
945 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400946
947 dst.x = dot3(src0, row0);
948 dst.y = dot3(src0, row1);
949 }
950
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500951 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400952 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400953 Vector4f row0 = fetchRegister(src1, 0);
954 Vector4f row1 = fetchRegister(src1, 1);
955 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400956
957 dst.x = dot3(src0, row0);
958 dst.y = dot3(src0, row1);
959 dst.z = dot3(src0, row2);
960 }
961
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500962 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400963 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400964 Vector4f row0 = fetchRegister(src1, 0);
965 Vector4f row1 = fetchRegister(src1, 1);
966 Vector4f row2 = fetchRegister(src1, 2);
967 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400968
969 dst.x = dot3(src0, row0);
970 dst.y = dot3(src0, row1);
971 dst.z = dot3(src0, row2);
972 dst.w = dot3(src0, row3);
973 }
974
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500975 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400976 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400977 Vector4f row0 = fetchRegister(src1, 0);
978 Vector4f row1 = fetchRegister(src1, 1);
979 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400980
981 dst.x = dot4(src0, row0);
982 dst.y = dot4(src0, row1);
983 dst.z = dot4(src0, row2);
984 }
985
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500986 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400987 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400988 Vector4f row0 = fetchRegister(src1, 0);
989 Vector4f row1 = fetchRegister(src1, 1);
990 Vector4f row2 = fetchRegister(src1, 2);
991 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400992
993 dst.x = dot4(src0, row0);
994 dst.y = dot4(src0, row1);
995 dst.z = dot4(src0, row2);
996 dst.w = dot4(src0, row3);
997 }
998
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500999 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001000 {
1001 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
1002 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1003
1004 if(breakDepth == 0)
1005 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001006 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001007 Nucleus::createBr(endBlock);
1008 }
1009 else
1010 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001011 enableBreak = enableBreak & ~enableStack[enableIndex];
1012 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001013
Nicolas Capens7551ac62016-01-20 17:11:53 -05001014 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001015 branch(allBreak, endBlock, deadBlock);
1016 }
1017
1018 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001019 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001020 }
1021
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001022 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001023 {
1024 Int4 condition;
1025
1026 switch(control)
1027 {
John Bauman19bac1e2014-05-06 15:23:49 -04001028 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1029 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1030 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1031 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1032 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1033 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001034 default:
1035 ASSERT(false);
1036 }
1037
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001038 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001039 }
1040
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001041 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001042 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001043 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001044
John Bauman19bac1e2014-05-06 15:23:49 -04001045 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001046 {
1047 condition = ~condition;
1048 }
1049
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001050 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001051 }
1052
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001053 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001054 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001055 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001056
1057 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1058 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1059
Nicolas Capens7551ac62016-01-20 17:11:53 -05001060 enableBreak = enableBreak & ~condition;
1061 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001062
Nicolas Capens7551ac62016-01-20 17:11:53 -05001063 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001064 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001065
John Bauman89401822014-05-06 15:04:28 -04001066 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001067 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001068 }
1069
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001070 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001071 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001072 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001073 }
1074
1075 void VertexProgram::TEST()
1076 {
1077 whileTest = true;
1078 }
1079
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001080 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001081 {
1082 if(!labelBlock[labelIndex])
1083 {
1084 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1085 }
1086
John Bauman19bac1e2014-05-06 15:23:49 -04001087 if(callRetBlock[labelIndex].size() > 1)
1088 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001089 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001090 }
John Bauman89401822014-05-06 15:04:28 -04001091
Nicolas Capens7551ac62016-01-20 17:11:53 -05001092 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001093
1094 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001095 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1096
Nicolas Capens7551ac62016-01-20 17:11:53 -05001097 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001098 }
1099
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001100 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001101 {
John Bauman19bac1e2014-05-06 15:23:49 -04001102 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001103 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001104 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001105 }
John Bauman19bac1e2014-05-06 15:23:49 -04001106 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001107 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001108 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001109 }
1110 else ASSERT(false);
1111 }
1112
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001113 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001114 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001115 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001116
John Bauman19bac1e2014-05-06 15:23:49 -04001117 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001118 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001119 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001120 }
1121
1122 if(!labelBlock[labelIndex])
1123 {
1124 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1125 }
1126
John Bauman19bac1e2014-05-06 15:23:49 -04001127 if(callRetBlock[labelIndex].size() > 1)
1128 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001129 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001130 }
John Bauman89401822014-05-06 15:04:28 -04001131
Nicolas Capens7551ac62016-01-20 17:11:53 -05001132 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001133
John Bauman19bac1e2014-05-06 15:23:49 -04001134 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1135 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1136
Nicolas Capens7551ac62016-01-20 17:11:53 -05001137 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001138 }
1139
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001140 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001141 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001142 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001143
John Bauman19bac1e2014-05-06 15:23:49 -04001144 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001145 {
1146 condition = ~condition;
1147 }
1148
Nicolas Capens7551ac62016-01-20 17:11:53 -05001149 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001150
1151 if(!labelBlock[labelIndex])
1152 {
1153 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1154 }
1155
John Bauman19bac1e2014-05-06 15:23:49 -04001156 if(callRetBlock[labelIndex].size() > 1)
1157 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001158 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001159 }
John Bauman89401822014-05-06 15:04:28 -04001160
Nicolas Capens7551ac62016-01-20 17:11:53 -05001161 enableIndex++;
1162 enableStack[enableIndex] = condition;
1163 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001164
John Bauman19bac1e2014-05-06 15:23:49 -04001165 Bool notAllFalse = SignMask(condition) != 0;
1166 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1167 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001168
Nicolas Capens7551ac62016-01-20 17:11:53 -05001169 enableIndex--;
1170 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001171 }
1172
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001173 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001174 {
1175 ifDepth--;
1176
1177 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1178 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1179
1180 if(isConditionalIf[ifDepth])
1181 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001182 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001183 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001184
1185 branch(notAllFalse, falseBlock, endBlock);
1186
Nicolas Capens7551ac62016-01-20 17:11:53 -05001187 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001188 }
1189 else
1190 {
1191 Nucleus::createBr(endBlock);
1192 Nucleus::setInsertBlock(falseBlock);
1193 }
1194
1195 ifFalseBlock[ifDepth] = endBlock;
1196
1197 ifDepth++;
1198 }
1199
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001200 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001201 {
1202 ifDepth--;
1203
1204 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1205
1206 Nucleus::createBr(endBlock);
1207 Nucleus::setInsertBlock(endBlock);
1208
1209 if(isConditionalIf[ifDepth])
1210 {
1211 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001212 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001213 }
1214 }
1215
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001216 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001217 {
1218 loopRepDepth--;
1219
Nicolas Capens7551ac62016-01-20 17:11:53 -05001220 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001221
1222 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1223 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1224
1225 Nucleus::createBr(testBlock);
1226 Nucleus::setInsertBlock(endBlock);
1227
Nicolas Capens7551ac62016-01-20 17:11:53 -05001228 loopDepth--;
1229 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001230 }
1231
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001232 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001233 {
1234 loopRepDepth--;
1235
1236 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1237 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1238
1239 Nucleus::createBr(testBlock);
1240 Nucleus::setInsertBlock(endBlock);
1241
Nicolas Capens7551ac62016-01-20 17:11:53 -05001242 loopDepth--;
1243 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001244 }
1245
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001246 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001247 {
1248 loopRepDepth--;
1249
1250 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1251 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1252
1253 Nucleus::createBr(testBlock);
1254 Nucleus::setInsertBlock(endBlock);
1255
Nicolas Capens7551ac62016-01-20 17:11:53 -05001256 enableIndex--;
1257 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001258 whileTest = false;
1259 }
1260
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001261 void VertexProgram::ENDSWITCH()
1262 {
1263 loopRepDepth--;
1264
1265 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001266
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001267 Nucleus::createBr(loopRepEndBlock[loopRepDepth]);
1268 Nucleus::setInsertBlock(endBlock);
1269
1270 enableIndex--;
1271 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1272 }
1273
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001274 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001275 {
John Bauman19bac1e2014-05-06 15:23:49 -04001276 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001277 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001278 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001279 }
John Bauman19bac1e2014-05-06 15:23:49 -04001280 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001281 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001282 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001283 }
John Bauman19bac1e2014-05-06 15:23:49 -04001284 else
1285 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001286 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001287 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001288 }
John Bauman89401822014-05-06 15:04:28 -04001289 }
1290
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001291 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001292 {
1293 ASSERT(ifDepth < 24 + 4);
1294
Nicolas Capens7551ac62016-01-20 17:11:53 -05001295 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001296
John Bauman19bac1e2014-05-06 15:23:49 -04001297 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001298 {
John Bauman19bac1e2014-05-06 15:23:49 -04001299 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001300 }
1301
1302 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1303 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1304
1305 branch(condition, trueBlock, falseBlock);
1306
1307 isConditionalIf[ifDepth] = false;
1308 ifFalseBlock[ifDepth] = falseBlock;
1309
1310 ifDepth++;
1311 }
1312
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001313 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001314 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001315 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001316
John Bauman19bac1e2014-05-06 15:23:49 -04001317 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001318 {
1319 condition = ~condition;
1320 }
1321
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001322 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001323 }
1324
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001325 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001326 {
1327 Int4 condition;
1328
1329 switch(control)
1330 {
John Bauman19bac1e2014-05-06 15:23:49 -04001331 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1332 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1333 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1334 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1335 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1336 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001337 default:
1338 ASSERT(false);
1339 }
1340
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001341 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001342 }
1343
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001344 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001345 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001346 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001347
Nicolas Capens7551ac62016-01-20 17:11:53 -05001348 enableIndex++;
1349 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001350
1351 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1352 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1353
John Bauman19bac1e2014-05-06 15:23:49 -04001354 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001355
1356 branch(notAllFalse, trueBlock, falseBlock);
1357
1358 isConditionalIf[ifDepth] = true;
1359 ifFalseBlock[ifDepth] = falseBlock;
1360
1361 ifDepth++;
1362 breakDepth++;
1363 }
1364
1365 void VertexProgram::LABEL(int labelIndex)
1366 {
John Bauman19bac1e2014-05-06 15:23:49 -04001367 if(!labelBlock[labelIndex])
1368 {
1369 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1370 }
1371
John Bauman89401822014-05-06 15:04:28 -04001372 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001373 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001374 }
1375
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001376 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001377 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001378 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001379
Nicolas Capens7551ac62016-01-20 17:11:53 -05001380 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1381 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1382 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001383
1384 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001385 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001386 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001387 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001388 }
1389
1390 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1391 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1392 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1393
1394 loopRepTestBlock[loopRepDepth] = testBlock;
1395 loopRepEndBlock[loopRepDepth] = endBlock;
1396
1397 // FIXME: jump(testBlock)
1398 Nucleus::createBr(testBlock);
1399 Nucleus::setInsertBlock(testBlock);
1400
Nicolas Capens7551ac62016-01-20 17:11:53 -05001401 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001402 Nucleus::setInsertBlock(loopBlock);
1403
Nicolas Capens7551ac62016-01-20 17:11:53 -05001404 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001405
John Bauman89401822014-05-06 15:04:28 -04001406 loopRepDepth++;
1407 breakDepth = 0;
1408 }
1409
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001410 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001411 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001412 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001413
Nicolas Capens7551ac62016-01-20 17:11:53 -05001414 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1415 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001416
1417 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1418 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1419 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1420
1421 loopRepTestBlock[loopRepDepth] = testBlock;
1422 loopRepEndBlock[loopRepDepth] = endBlock;
1423
1424 // FIXME: jump(testBlock)
1425 Nucleus::createBr(testBlock);
1426 Nucleus::setInsertBlock(testBlock);
1427
Nicolas Capens7551ac62016-01-20 17:11:53 -05001428 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001429 Nucleus::setInsertBlock(loopBlock);
1430
Nicolas Capens7551ac62016-01-20 17:11:53 -05001431 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001432
1433 loopRepDepth++;
1434 breakDepth = 0;
1435 }
1436
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001437 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001438 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001439 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001440
1441 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1442 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1443 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001444
John Bauman19bac1e2014-05-06 15:23:49 -04001445 loopRepTestBlock[loopRepDepth] = testBlock;
1446 loopRepEndBlock[loopRepDepth] = endBlock;
1447
Nicolas Capens7551ac62016-01-20 17:11:53 -05001448 Int4 restoreBreak = enableBreak;
1449 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001450
1451 // FIXME: jump(testBlock)
1452 Nucleus::createBr(testBlock);
1453 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001454 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001455
Nicolas Capensc2534f42016-04-04 11:13:24 -04001456 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001457 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001458 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001459 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001460 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001461
1462 Bool notAllFalse = SignMask(condition) != 0;
1463 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001464
John Bauman19bac1e2014-05-06 15:23:49 -04001465 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001466 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001467
John Bauman19bac1e2014-05-06 15:23:49 -04001468 Nucleus::setInsertBlock(loopBlock);
1469
1470 loopRepDepth++;
1471 breakDepth = 0;
1472 }
1473
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001474 void VertexProgram::SWITCH()
1475 {
1476 enableIndex++;
1477 enableStack[enableIndex] = Int4(0xFFFFFFFF);
1478
1479 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1480
1481 loopRepTestBlock[loopRepDepth] = nullptr;
1482 loopRepEndBlock[loopRepDepth] = endBlock;
1483
1484 loopRepDepth++;
1485 breakDepth = 0;
1486 }
1487
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001488 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001489 {
John Bauman19bac1e2014-05-06 15:23:49 -04001490 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001491 {
1492 returnBlock = Nucleus::createBasicBlock();
1493 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001494 }
1495 else
1496 {
John Bauman89401822014-05-06 15:04:28 -04001497 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001498
John Bauman19bac1e2014-05-06 15:23:49 -04001499 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001500 {
John Bauman19bac1e2014-05-06 15:23:49 -04001501 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001502 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001503
John Bauman66b8ab22014-05-06 15:57:45 -04001504 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001505 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1506
1507 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1508 {
1509 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1510 }
1511 }
1512 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1513 {
1514 Nucleus::createBr(callRetBlock[currentLabel][0]);
1515 }
1516 else // Function isn't called
1517 {
1518 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001519 }
1520
1521 Nucleus::setInsertBlock(unreachableBlock);
1522 Nucleus::createUnreachable();
1523 }
1524 }
1525
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001526 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001527 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001528 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001529
John Bauman19bac1e2014-05-06 15:23:49 -04001530 // FIXME: Return from function if all instances left
1531 // FIXME: Use enableLeave in other control-flow constructs
1532 }
John Bauman89401822014-05-06 15:04:28 -04001533
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001534 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001535 {
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001536 sampleTexture(dst, src1, src0.x, src0.y, src0.z, src0.w, a0, a0, src0, Lod, None);
John Bauman89401822014-05-06 15:04:28 -04001537 }
John Bauman19bac1e2014-05-06 15:23:49 -04001538
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001539 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001540 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001541 Float4 lod0 = Float4(0.0f);
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001542 sampleTexture(dst, src1, src0.x, src0.y, src0.z, lod0, a0, a0, src0, Lod, None);
John Bauman19bac1e2014-05-06 15:23:49 -04001543 }
1544
Meng-Lin Wu2337a192016-06-01 13:54:07 -04001545 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001546 {
Meng-Lin Wu2337a192016-06-01 13:54:07 -04001547 Float4 lod0 = Float4(0.0f);
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001548 sampleTexture(dst, src1, src0.x, src0.y, src0.z, lod0, a0, a0, src2, Lod, Offset);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001549 }
1550
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001551 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001552 {
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001553 sampleTexture(dst, src1, src0.x, src0.y, src0.z, src0.w, a0, a0, offset, Lod, Offset);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001554 }
1555
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001556 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001557 {
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001558 sampleTexture(dst, src1, src0.x, src0.y, src0.z, Float4(As<Int4>(src2.x)), src0, src0, src0, Lod, Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001559 }
1560
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001561 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001562 {
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001563 sampleTexture(dst, src1, src0.x, src0.y, src0.z, Float4(As<Int4>(src2.x)), src0, src0, offset, Lod, Fetch | Offset);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001564 }
1565
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001566 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001567 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001568 Float4 lod0 = Float4(0.0f);
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001569 sampleTexture(dst, src1, src0.x, src0.y, src0.z, lod0, src2, src3, src0, Grad, None);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001570 }
1571
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001572 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001573 {
Meng-Lin Wu2337a192016-06-01 13:54:07 -04001574 Float4 lod0 = Float4(0.0f);
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001575 sampleTexture(dst, src1, src0.x, src0.y, src0.z, lod0, src2, src3, offset, Grad, Offset);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001576 }
1577
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001578 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001579 {
Alexis Hetu95ac1872016-06-06 13:26:52 -04001580 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
1581 sampler[src1.index]->textureSize(texture, dst, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001582 }
1583
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001584 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerMethod method, unsigned int options)
John Bauman19bac1e2014-05-06 15:23:49 -04001585 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001586 Vector4f tmp;
1587
John Bauman19bac1e2014-05-06 15:23:49 -04001588 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1589 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001590 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + s.index * sizeof(Texture);
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001591 sampler[s.index]->sampleTexture(texture, tmp, u, v, w, q, dsx, dsy, offset, options, method);
John Bauman19bac1e2014-05-06 15:23:49 -04001592 }
1593 else
1594 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001595 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001596
Nicolas Capensc2534f42016-04-04 11:13:24 -04001597 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001598 {
1599 if(shader->usesSampler(i))
1600 {
1601 If(index == i)
1602 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001603 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + i * sizeof(Texture);
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001604 sampler[i]->sampleTexture(texture, tmp, u, v, w, q, dsx, dsy, offset, options, method);
John Bauman19bac1e2014-05-06 15:23:49 -04001605 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1606 }
1607 }
1608 }
1609 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001610
1611 c.x = tmp[(s.swizzle >> 0) & 0x3];
1612 c.y = tmp[(s.swizzle >> 2) & 0x3];
1613 c.z = tmp[(s.swizzle >> 4) & 0x3];
1614 c.w = tmp[(s.swizzle >> 6) & 0x3];
John Bauman19bac1e2014-05-06 15:23:49 -04001615 }
John Bauman89401822014-05-06 15:04:28 -04001616}