blob: e6153b134db1e99b96e968e8351dbd26ae50fbbf [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
John Bauman89401822014-05-06 15:04:28 -040017#include "VertexShader.hpp"
John Bauman89401822014-05-06 15:04:28 -040018#include "SamplerCore.hpp"
Nicolas Capens708c24b2017-10-26 13:07:10 -040019#include "Renderer/Renderer.hpp"
20#include "Renderer/Vertex.hpp"
21#include "Common/Half.hpp"
22#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040023
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
Alexis Hetu02ad0aa2016-08-02 11:18:14 -040053 if(shader->isInstanceIdDeclared())
Nicolas Capens7551ac62016-01-20 17:11:53 -050054 {
55 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
56 }
John Bauman89401822014-05-06 15:04:28 -040057 }
58
59 VertexProgram::~VertexProgram()
60 {
John Bauman89401822014-05-06 15:04:28 -040061 }
62
Alexis Hetu877ddfc2017-07-25 17:48:00 -040063 void VertexProgram::pipeline(UInt& index)
John Bauman89401822014-05-06 15:04:28 -040064 {
John Bauman89401822014-05-06 15:04:28 -040065 if(!state.preTransformed)
66 {
Alexis Hetu877ddfc2017-07-25 17:48:00 -040067 program(index);
John Bauman89401822014-05-06 15:04:28 -040068 }
69 else
70 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050071 passThrough();
John Bauman89401822014-05-06 15:04:28 -040072 }
73 }
74
Alexis Hetu877ddfc2017-07-25 17:48:00 -040075 void VertexProgram::program(UInt& index)
John Bauman89401822014-05-06 15:04:28 -040076 {
John Bauman19bac1e2014-05-06 15:23:49 -040077 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040078
John Bauman19bac1e2014-05-06 15:23:49 -040079 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040080
Nicolas Capens7551ac62016-01-20 17:11:53 -050081 enableIndex = 0;
82 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040083
Nicolas Capens4677a5f2014-05-06 16:42:26 -040084 if(shader->containsLeaveInstruction())
85 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050086 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040087 }
88
Alexis Hetu877ddfc2017-07-25 17:48:00 -040089 if(shader->isVertexIdDeclared())
90 {
91 if(state.textureSampling)
92 {
93 vertexID = Int4(index);
94 }
95 else
96 {
97 vertexID = Insert(vertexID, As<Int>(index), 0);
98 vertexID = Insert(vertexID, As<Int>(index + 1), 1);
99 vertexID = Insert(vertexID, As<Int>(index + 2), 2);
100 vertexID = Insert(vertexID, As<Int>(index + 3), 3);
101 }
102 }
103
John Bauman19bac1e2014-05-06 15:23:49 -0400104 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -0500105 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400106 {
John Bauman19bac1e2014-05-06 15:23:49 -0400107 const Shader::Instruction *instruction = shader->getInstruction(i);
108 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400109
John Bauman19bac1e2014-05-06 15:23:49 -0400110 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
111 {
112 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400113
John Bauman19bac1e2014-05-06 15:23:49 -0400114 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
115 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
116 }
117 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500118
Alexis Hetu903e0252014-11-25 14:25:32 -0500119 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400120 {
121 const Shader::Instruction *instruction = shader->getInstruction(i);
122 Shader::Opcode opcode = instruction->opcode;
123
124 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400125 {
126 continue;
127 }
128
John Bauman19bac1e2014-05-06 15:23:49 -0400129 Dst dst = instruction->dst;
130 Src src0 = instruction->src[0];
131 Src src1 = instruction->src[1];
132 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400133 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400134 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400135
John Bauman19bac1e2014-05-06 15:23:49 -0400136 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400137 Control control = instruction->control;
138 bool integer = dst.type == Shader::PARAMETER_ADDR;
139 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400140
John Bauman19bac1e2014-05-06 15:23:49 -0400141 Vector4f d;
142 Vector4f s0;
143 Vector4f s1;
144 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400145 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400146 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400147
Nicolas Capensc2534f42016-04-04 11:13:24 -0400148 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
149 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
150 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
151 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
152 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400153
154 switch(opcode)
155 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500156 case Shader::OPCODE_VS_1_0: break;
157 case Shader::OPCODE_VS_1_1: break;
158 case Shader::OPCODE_VS_2_0: break;
159 case Shader::OPCODE_VS_2_x: break;
160 case Shader::OPCODE_VS_2_sw: break;
161 case Shader::OPCODE_VS_3_0: break;
162 case Shader::OPCODE_VS_3_sw: break;
163 case Shader::OPCODE_DCL: break;
164 case Shader::OPCODE_DEF: break;
165 case Shader::OPCODE_DEFI: break;
166 case Shader::OPCODE_DEFB: break;
167 case Shader::OPCODE_NOP: break;
168 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400169 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500170 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400171 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500172 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
173 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
174 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
175 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
176 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
177 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
178 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
179 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
180 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
181 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
182 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
183 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
184 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
185 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
186 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
187 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
188 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400189 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
190 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
191 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500192 case Shader::OPCODE_ATT: att(d, s0, s1); break;
193 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
194 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
195 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
196 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
197 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400198 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
199 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400200 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500201 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400202 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500203 case Shader::OPCODE_LIT: lit(d, s0); break;
204 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
205 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
206 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
207 case Shader::OPCODE_LOG: log(d, s0, pp); break;
208 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
209 case Shader::OPCODE_STEP: step(d, s0, s1); break;
210 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8ef6d102017-11-09 15:49:09 -0500211 case Shader::OPCODE_ISINF: isinf(d, s0); break;
212 case Shader::OPCODE_ISNAN: isnan(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400213 case Shader::OPCODE_FLOATBITSTOINT:
214 case Shader::OPCODE_FLOATBITSTOUINT:
215 case Shader::OPCODE_INTBITSTOFLOAT:
216 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400217 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
218 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400219 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400220 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
221 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400222 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500223 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
224 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
225 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
226 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
227 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
228 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
229 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
230 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400231 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
232 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500233 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400234 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
235 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500236 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400237 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400238 case Shader::OPCODE_NEG: neg(d, s0); break;
239 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500240 case Shader::OPCODE_F2B: f2b(d, s0); break;
241 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400242 case Shader::OPCODE_F2I: f2i(d, s0); break;
243 case Shader::OPCODE_I2F: i2f(d, s0); break;
244 case Shader::OPCODE_F2U: f2u(d, s0); break;
245 case Shader::OPCODE_U2F: u2f(d, s0); break;
246 case Shader::OPCODE_I2B: i2b(d, s0); break;
247 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500248 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400249 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500250 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
251 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
252 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
253 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
254 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
255 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
256 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400257 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
258 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500259 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400260 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
261 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
262 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500263 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
264 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
265 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
266 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
267 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
268 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
269 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
270 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
271 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
272 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
273 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
274 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
275 case Shader::OPCODE_SGE: step(d, s1, s0); break;
276 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400277 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500278 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
279 case Shader::OPCODE_COS: cos(d, s0, pp); break;
280 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
281 case Shader::OPCODE_TAN: tan(d, s0); break;
282 case Shader::OPCODE_ACOS: acos(d, s0); break;
283 case Shader::OPCODE_ASIN: asin(d, s0); break;
284 case Shader::OPCODE_ATAN: atan(d, s0); break;
285 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
286 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
287 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
288 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
289 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
290 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
291 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
292 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
293 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400294 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500295 case Shader::OPCODE_BREAK: BREAK(); break;
296 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
297 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
298 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
299 case Shader::OPCODE_TEST: TEST(); break;
300 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
301 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
302 case Shader::OPCODE_ELSE: ELSE(); break;
303 case Shader::OPCODE_ENDIF: ENDIF(); break;
304 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
305 case Shader::OPCODE_ENDREP: ENDREP(); break;
306 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400307 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500308 case Shader::OPCODE_IF: IF(src0); break;
309 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
310 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
311 case Shader::OPCODE_LOOP: LOOP(src1); break;
312 case Shader::OPCODE_REP: REP(src0); break;
313 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400314 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500315 case Shader::OPCODE_RET: RET(); break;
316 case Shader::OPCODE_LEAVE: LEAVE(); break;
317 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
318 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400319 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500320 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
321 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
322 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
323 case Shader::OPCODE_ALL: all(d.x, s0); break;
324 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400325 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
326 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
327 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
328 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400329 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
330 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500331 case Shader::OPCODE_TEXLDL: TEXLOD(d, s0, src1, s0.w); break;
332 case Shader::OPCODE_TEXLOD: TEXLOD(d, s0, src1, s2.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500333 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400334 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500335 case Shader::OPCODE_TEXLODOFFSET: TEXLODOFFSET(d, s0, src1, s2, s3.x); break;
336 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2.x); break;
337 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCHOFFSET(d, s0, src1, s2, s3.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500338 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500339 case Shader::OPCODE_TEXGRADOFFSET: TEXGRADOFFSET(d, s0, src1, s2, s3, s4); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500340 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
341 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400342 default:
343 ASSERT(false);
344 }
345
John Bauman19bac1e2014-05-06 15:23:49 -0400346 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400347 {
John Bauman19bac1e2014-05-06 15:23:49 -0400348 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400349 {
John Bauman19bac1e2014-05-06 15:23:49 -0400350 switch(opcode)
351 {
352 case Shader::OPCODE_DIV:
353 if(dst.x) d.x = Trunc(d.x);
354 if(dst.y) d.y = Trunc(d.y);
355 if(dst.z) d.z = Trunc(d.z);
356 if(dst.w) d.w = Trunc(d.w);
357 break;
358 default:
359 break; // No truncation to integer required when arguments are integer
360 }
John Bauman89401822014-05-06 15:04:28 -0400361 }
362
John Bauman19bac1e2014-05-06 15:23:49 -0400363 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400364 {
John Bauman19bac1e2014-05-06 15:23:49 -0400365 if(dst.x) d.x = Max(d.x, Float4(0.0f));
366 if(dst.y) d.y = Max(d.y, Float4(0.0f));
367 if(dst.z) d.z = Max(d.z, Float4(0.0f));
368 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400369
John Bauman19bac1e2014-05-06 15:23:49 -0400370 if(dst.x) d.x = Min(d.x, Float4(1.0f));
371 if(dst.y) d.y = Min(d.y, Float4(1.0f));
372 if(dst.z) d.z = Min(d.z, Float4(1.0f));
373 if(dst.w) d.w = Min(d.w, Float4(1.0f));
374 }
375
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400376 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400377 {
378 Vector4f pDst; // FIXME: Rename
379
380 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400381 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500382 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400383 case Shader::PARAMETER_TEMP:
384 if(dst.rel.type == Shader::PARAMETER_VOID)
385 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500386 if(dst.x) pDst.x = r[dst.index].x;
387 if(dst.y) pDst.y = r[dst.index].y;
388 if(dst.z) pDst.z = r[dst.index].z;
389 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400390 }
391 else
392 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500393 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400394
Nicolas Capens7551ac62016-01-20 17:11:53 -0500395 if(dst.x) pDst.x = r[dst.index + a].x;
396 if(dst.y) pDst.y = r[dst.index + a].y;
397 if(dst.z) pDst.z = r[dst.index + a].z;
398 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400399 }
400 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500401 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400402 case Shader::PARAMETER_RASTOUT:
403 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400404 {
405 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500406 if(dst.x) pDst.x = o[Pos].x;
407 if(dst.y) pDst.y = o[Pos].y;
408 if(dst.z) pDst.z = o[Pos].z;
409 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400410 break;
411 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500412 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400413 break;
414 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500415 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400416 break;
417 default:
418 ASSERT(false);
419 }
420 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400421 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400422 if(dst.x) pDst.x = o[C0 + dst.index].x;
423 if(dst.y) pDst.y = o[C0 + dst.index].y;
424 if(dst.z) pDst.z = o[C0 + dst.index].z;
425 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400426 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400427 case Shader::PARAMETER_TEXCRDOUT:
428 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400429 if(version < 0x0300)
430 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500431 if(dst.x) pDst.x = o[T0 + dst.index].x;
432 if(dst.y) pDst.y = o[T0 + dst.index].y;
433 if(dst.z) pDst.z = o[T0 + dst.index].z;
434 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400435 }
436 else
437 {
John Bauman19bac1e2014-05-06 15:23:49 -0400438 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400439 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500440 if(dst.x) pDst.x = o[dst.index].x;
441 if(dst.y) pDst.y = o[dst.index].y;
442 if(dst.z) pDst.z = o[dst.index].z;
443 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400444 }
John Bauman19bac1e2014-05-06 15:23:49 -0400445 else
446 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500447 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400448
Nicolas Capens7551ac62016-01-20 17:11:53 -0500449 if(dst.x) pDst.x = o[dst.index + a].x;
450 if(dst.y) pDst.y = o[dst.index + a].y;
451 if(dst.z) pDst.z = o[dst.index + a].z;
452 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400453 }
454 }
455 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500456 case Shader::PARAMETER_LABEL: break;
457 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
458 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400459 default:
460 ASSERT(false);
461 }
462
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500463 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400464
465 Int4 xEnable = enable;
466 Int4 yEnable = enable;
467 Int4 zEnable = enable;
468 Int4 wEnable = enable;
469
470 if(predicate)
471 {
John Bauman19bac1e2014-05-06 15:23:49 -0400472 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400473
Nicolas Capens7551ac62016-01-20 17:11:53 -0500474 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
475 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
476 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
477 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400478
John Bauman19bac1e2014-05-06 15:23:49 -0400479 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400480 {
John Bauman19bac1e2014-05-06 15:23:49 -0400481 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
482 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
483 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
484 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400485 }
486 else
487 {
John Bauman19bac1e2014-05-06 15:23:49 -0400488 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
489 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
490 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
491 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400492 }
493 }
494
John Bauman19bac1e2014-05-06 15:23:49 -0400495 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
496 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
497 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
498 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400499
John Bauman19bac1e2014-05-06 15:23:49 -0400500 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
501 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
502 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
503 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400504 }
505
John Bauman19bac1e2014-05-06 15:23:49 -0400506 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400507 {
John Bauman19bac1e2014-05-06 15:23:49 -0400508 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400509 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400510 case Shader::PARAMETER_TEMP:
511 if(dst.rel.type == Shader::PARAMETER_VOID)
512 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500513 if(dst.x) r[dst.index].x = d.x;
514 if(dst.y) r[dst.index].y = d.y;
515 if(dst.z) r[dst.index].z = d.z;
516 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400517 }
518 else
519 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500520 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400521
Nicolas Capens7551ac62016-01-20 17:11:53 -0500522 if(dst.x) r[dst.index + a].x = d.x;
523 if(dst.y) r[dst.index + a].y = d.y;
524 if(dst.z) r[dst.index + a].z = d.z;
525 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400526 }
John Bauman89401822014-05-06 15:04:28 -0400527 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400528 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500529 if(dst.x) a0.x = d.x;
530 if(dst.y) a0.y = d.y;
531 if(dst.z) a0.z = d.z;
532 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400533 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400534 case Shader::PARAMETER_RASTOUT:
535 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400536 {
537 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500538 if(dst.x) o[Pos].x = d.x;
539 if(dst.y) o[Pos].y = d.y;
540 if(dst.z) o[Pos].z = d.z;
541 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400542 break;
543 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500544 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400545 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500546 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500547 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400548 break;
549 default: ASSERT(false);
550 }
551 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500552 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400553 if(dst.x) o[C0 + dst.index].x = d.x;
554 if(dst.y) o[C0 + dst.index].y = d.y;
555 if(dst.z) o[C0 + dst.index].z = d.z;
556 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400557 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400558 case Shader::PARAMETER_TEXCRDOUT:
559 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400560 if(version < 0x0300)
561 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500562 if(dst.x) o[T0 + dst.index].x = d.x;
563 if(dst.y) o[T0 + dst.index].y = d.y;
564 if(dst.z) o[T0 + dst.index].z = d.z;
565 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400566 }
567 else
568 {
John Bauman19bac1e2014-05-06 15:23:49 -0400569 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400570 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500571 if(dst.x) o[dst.index].x = d.x;
572 if(dst.y) o[dst.index].y = d.y;
573 if(dst.z) o[dst.index].z = d.z;
574 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400575 }
John Bauman19bac1e2014-05-06 15:23:49 -0400576 else
577 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500578 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400579
Nicolas Capens7551ac62016-01-20 17:11:53 -0500580 if(dst.x) o[dst.index + a].x = d.x;
581 if(dst.y) o[dst.index + a].y = d.y;
582 if(dst.z) o[dst.index + a].z = d.z;
583 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400584 }
585 }
586 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500587 case Shader::PARAMETER_LABEL: break;
588 case Shader::PARAMETER_PREDICATE: p0 = d; break;
589 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400590 default:
591 ASSERT(false);
592 }
593 }
594 }
595
John Bauman19bac1e2014-05-06 15:23:49 -0400596 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400597 {
598 Nucleus::setInsertBlock(returnBlock);
599 }
600 }
601
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500602 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400603 {
John Bauman19bac1e2014-05-06 15:23:49 -0400604 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400605 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400606 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400607 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400608 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400609
610 switch(usage)
611 {
612 case 0xFF:
613 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400614 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500615 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400616 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400617 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500618 o[i].x = v[i].x;
619 o[i].y = v[i].y;
620 o[i].z = v[i].z;
621 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400622 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400623 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500624 o[i].x = v[i].x;
625 o[i].y = v[i].y;
626 o[i].z = v[i].z;
627 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400628 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400629 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500630 o[i].x = v[i].x;
631 o[i].y = v[i].y;
632 o[i].z = v[i].z;
633 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400634 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400635 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500636 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400637 break;
638 default:
639 ASSERT(false);
640 }
641 }
642 }
643 else
644 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500645 o[Pos].x = v[PositionT].x;
646 o[Pos].y = v[PositionT].y;
647 o[Pos].z = v[PositionT].z;
648 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400649
650 for(int i = 0; i < 2; i++)
651 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400652 o[C0 + i].x = v[Color0 + i].x;
653 o[C0 + i].y = v[Color0 + i].y;
654 o[C0 + i].z = v[Color0 + i].z;
655 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400656 }
657
658 for(int i = 0; i < 8; i++)
659 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500660 o[T0 + i].x = v[TexCoord0 + i].x;
661 o[T0 + i].y = v[TexCoord0 + i].y;
662 o[T0 + i].z = v[TexCoord0 + i].z;
663 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400664 }
665
Nicolas Capens7551ac62016-01-20 17:11:53 -0500666 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400667 }
668 }
669
Nicolas Capensc2534f42016-04-04 11:13:24 -0400670 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400671 {
John Bauman19bac1e2014-05-06 15:23:49 -0400672 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500673 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400674
John Bauman89401822014-05-06 15:04:28 -0400675 switch(src.type)
676 {
John Bauman19bac1e2014-05-06 15:23:49 -0400677 case Shader::PARAMETER_TEMP:
678 if(src.rel.type == Shader::PARAMETER_VOID)
679 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500680 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400681 }
682 else
683 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400684 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400685 }
686 break;
687 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500688 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400689 break;
690 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400691 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400692 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500693 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400694 }
695 else
696 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400697 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400698 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400699 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500700 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400701 case Shader::PARAMETER_FLOAT4LITERAL:
702 reg.x = Float4(src.value[0]);
703 reg.y = Float4(src.value[1]);
704 reg.z = Float4(src.value[2]);
705 reg.w = Float4(src.value[3]);
706 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500707 case Shader::PARAMETER_ADDR: reg = a0; break;
708 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
709 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
710 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
711 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400712 case Shader::PARAMETER_SAMPLER:
713 if(src.rel.type == Shader::PARAMETER_VOID)
714 {
715 reg.x = As<Float4>(Int4(i));
716 }
717 else if(src.rel.type == Shader::PARAMETER_TEMP)
718 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500719 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400720 }
721 return reg;
722 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400723 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400724 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500725 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400726 }
727 else
728 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400729 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400730 }
731 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400732 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400733 if(src.index == Shader::InstanceIDIndex)
734 {
735 reg.x = As<Float>(instanceID);
736 }
737 else if(src.index == Shader::VertexIDIndex)
738 {
739 reg.x = As<Float4>(vertexID);
740 }
741 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400742 return reg;
John Bauman89401822014-05-06 15:04:28 -0400743 default:
744 ASSERT(false);
745 }
746
John Bauman66b8ab22014-05-06 15:57:45 -0400747 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
748 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
749 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
750 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400751
John Bauman66b8ab22014-05-06 15:57:45 -0400752 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400753
754 switch(src.modifier)
755 {
John Bauman19bac1e2014-05-06 15:23:49 -0400756 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400757 mod.x = x;
758 mod.y = y;
759 mod.z = z;
760 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400761 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400762 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400763 mod.x = -x;
764 mod.y = -y;
765 mod.z = -z;
766 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400767 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400768 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400769 mod.x = Abs(x);
770 mod.y = Abs(y);
771 mod.z = Abs(z);
772 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400773 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400774 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400775 mod.x = -Abs(x);
776 mod.y = -Abs(y);
777 mod.z = -Abs(z);
778 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400779 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400780 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400781 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
782 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
783 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
784 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400785 break;
786 default:
787 ASSERT(false);
788 }
789
790 return mod;
791 }
792
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400793 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
794 {
795 if(bufferIndex == -1)
796 {
797 return data + OFFSET(DrawData, vs.c[index]);
798 }
799 else
800 {
801 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
802 }
803 }
804
805 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
806 {
807 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
808 }
809
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500810 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400811 {
John Bauman19bac1e2014-05-06 15:23:49 -0400812 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500813 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400814
815 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
816 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400817 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400818
819 c.x = c.x.xxxx;
820 c.y = c.y.yyyy;
821 c.z = c.z.zzzz;
822 c.w = c.w.wwww;
823
Nicolas Capenseafdb222015-05-15 15:24:08 -0400824 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400825 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500826 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400827 {
828 const Shader::Instruction &instruction = *shader->getInstruction(j);
829
830 if(instruction.opcode == Shader::OPCODE_DEF)
831 {
832 if(instruction.dst.index == i)
833 {
834 c.x = Float4(instruction.src[0].value[0]);
835 c.y = Float4(instruction.src[0].value[1]);
836 c.z = Float4(instruction.src[0].value[2]);
837 c.w = Float4(instruction.src[0].value[3]);
838
839 break;
840 }
841 }
842 }
843 }
844 }
845 else if(src.rel.type == Shader::PARAMETER_LOOP)
846 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500847 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400848
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400849 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400850
851 c.x = c.x.xxxx;
852 c.y = c.y.yyyy;
853 c.z = c.z.zzzz;
854 c.w = c.w.wwww;
855 }
856 else
857 {
858 if(src.rel.deterministic)
859 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400860 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400861
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400862 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500863
John Bauman19bac1e2014-05-06 15:23:49 -0400864 c.x = c.x.xxxx;
865 c.y = c.y.yyyy;
866 c.z = c.z.zzzz;
867 c.w = c.w.wwww;
868 }
869 else
870 {
871 int component = src.rel.swizzle & 0x03;
872 Float4 a;
873
874 switch(src.rel.type)
875 {
Nicolas Capens32980ac2016-08-15 15:33:58 -0400876 case Shader::PARAMETER_ADDR: a = a0[component]; break;
877 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
878 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
879 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
880 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400881 case Shader::PARAMETER_MISCTYPE:
882 if(src.rel.index == Shader::InstanceIDIndex)
883 {
884 a = As<Float4>(Int4(instanceID)); break;
885 }
886 else if(src.rel.index == Shader::VertexIDIndex)
887 {
888 a = As<Float4>(vertexID); break;
889 }
890 else ASSERT(false);
891 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400892 default: ASSERT(false);
893 }
894
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400895 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400896
Alexis Hetu028f41b2016-01-13 14:40:47 -0500897 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 -0500898
John Bauman19bac1e2014-05-06 15:23:49 -0400899 Int index0 = Extract(index, 0);
900 Int index1 = Extract(index, 1);
901 Int index2 = Extract(index, 2);
902 Int index3 = Extract(index, 3);
903
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400904 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
905 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
906 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
907 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400908
909 transpose4x4(c.x, c.y, c.z, c.w);
910 }
911 }
912
913 return c;
914 }
915
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400916 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400917 {
918 ASSERT(var.rel.deterministic);
919
920 if(var.rel.type == Shader::PARAMETER_TEMP)
921 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500922 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400923 }
924 else if(var.rel.type == Shader::PARAMETER_INPUT)
925 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500926 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400927 }
928 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
929 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500930 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400931 }
932 else if(var.rel.type == Shader::PARAMETER_CONST)
933 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500934 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400935 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500936 else if(var.rel.type == Shader::PARAMETER_LOOP)
937 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500938 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500939 }
John Bauman19bac1e2014-05-06 15:23:49 -0400940 else ASSERT(false);
941
942 return 0;
943 }
944
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500945 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400946 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500947 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500948
John Baumand4ae8632014-05-06 16:18:33 -0400949 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400950 {
John Baumand4ae8632014-05-06 16:18:33 -0400951 if(shader->containsBreakInstruction() && instruction->analysisBreak)
952 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500953 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400954 }
John Bauman19bac1e2014-05-06 15:23:49 -0400955
John Baumand4ae8632014-05-06 16:18:33 -0400956 if(shader->containsContinueInstruction() && instruction->analysisContinue)
957 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500958 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400959 }
John Bauman19bac1e2014-05-06 15:23:49 -0400960
John Baumand4ae8632014-05-06 16:18:33 -0400961 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
962 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500963 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400964 }
John Bauman19bac1e2014-05-06 15:23:49 -0400965 }
966
967 return enable;
968 }
969
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500970 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400971 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400972 Vector4f row0 = fetchRegister(src1, 0);
973 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400974
975 dst.x = dot3(src0, row0);
976 dst.y = dot3(src0, row1);
977 }
978
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500979 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400980 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400981 Vector4f row0 = fetchRegister(src1, 0);
982 Vector4f row1 = fetchRegister(src1, 1);
983 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400984
985 dst.x = dot3(src0, row0);
986 dst.y = dot3(src0, row1);
987 dst.z = dot3(src0, row2);
988 }
989
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500990 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400991 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400992 Vector4f row0 = fetchRegister(src1, 0);
993 Vector4f row1 = fetchRegister(src1, 1);
994 Vector4f row2 = fetchRegister(src1, 2);
995 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400996
997 dst.x = dot3(src0, row0);
998 dst.y = dot3(src0, row1);
999 dst.z = dot3(src0, row2);
1000 dst.w = dot3(src0, row3);
1001 }
1002
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001003 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001004 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001005 Vector4f row0 = fetchRegister(src1, 0);
1006 Vector4f row1 = fetchRegister(src1, 1);
1007 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001008
1009 dst.x = dot4(src0, row0);
1010 dst.y = dot4(src0, row1);
1011 dst.z = dot4(src0, row2);
1012 }
1013
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001014 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001015 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001016 Vector4f row0 = fetchRegister(src1, 0);
1017 Vector4f row1 = fetchRegister(src1, 1);
1018 Vector4f row2 = fetchRegister(src1, 2);
1019 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001020
1021 dst.x = dot4(src0, row0);
1022 dst.y = dot4(src0, row1);
1023 dst.z = dot4(src0, row2);
1024 dst.w = dot4(src0, row3);
1025 }
1026
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001027 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001028 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001029 BasicBlock *deadBlock = Nucleus::createBasicBlock();
1030 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001031
1032 if(breakDepth == 0)
1033 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001034 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001035 Nucleus::createBr(endBlock);
1036 }
1037 else
1038 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001039 enableBreak = enableBreak & ~enableStack[enableIndex];
1040 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001041
Nicolas Capens7551ac62016-01-20 17:11:53 -05001042 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001043 branch(allBreak, endBlock, deadBlock);
1044 }
1045
1046 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001047 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001048 }
1049
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001050 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001051 {
1052 Int4 condition;
1053
1054 switch(control)
1055 {
John Bauman19bac1e2014-05-06 15:23:49 -04001056 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1057 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1058 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1059 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1060 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1061 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001062 default:
1063 ASSERT(false);
1064 }
1065
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001066 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001067 }
1068
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001069 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001070 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001071 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001072
John Bauman19bac1e2014-05-06 15:23:49 -04001073 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001074 {
1075 condition = ~condition;
1076 }
1077
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001078 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001079 }
1080
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001081 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001082 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001083 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001084
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001085 BasicBlock *continueBlock = Nucleus::createBasicBlock();
1086 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001087
Nicolas Capens7551ac62016-01-20 17:11:53 -05001088 enableBreak = enableBreak & ~condition;
1089 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001090
Nicolas Capens7551ac62016-01-20 17:11:53 -05001091 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001092 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001093
John Bauman89401822014-05-06 15:04:28 -04001094 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001095 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001096 }
1097
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001098 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001099 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001100 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001101 }
1102
1103 void VertexProgram::TEST()
1104 {
1105 whileTest = true;
1106 }
1107
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001108 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001109 {
1110 if(!labelBlock[labelIndex])
1111 {
1112 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1113 }
1114
John Bauman19bac1e2014-05-06 15:23:49 -04001115 if(callRetBlock[labelIndex].size() > 1)
1116 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001117 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001118 }
John Bauman89401822014-05-06 15:04:28 -04001119
Nicolas Capens7551ac62016-01-20 17:11:53 -05001120 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001121
1122 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001123 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1124
Nicolas Capens7551ac62016-01-20 17:11:53 -05001125 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001126 }
1127
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001128 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001129 {
John Bauman19bac1e2014-05-06 15:23:49 -04001130 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001131 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001132 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001133 }
John Bauman19bac1e2014-05-06 15:23:49 -04001134 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001135 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001136 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001137 }
1138 else ASSERT(false);
1139 }
1140
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001141 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001142 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001143 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001144
John Bauman19bac1e2014-05-06 15:23:49 -04001145 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001146 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001147 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001148 }
1149
1150 if(!labelBlock[labelIndex])
1151 {
1152 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1153 }
1154
John Bauman19bac1e2014-05-06 15:23:49 -04001155 if(callRetBlock[labelIndex].size() > 1)
1156 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001157 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001158 }
John Bauman89401822014-05-06 15:04:28 -04001159
Nicolas Capens7551ac62016-01-20 17:11:53 -05001160 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001161
John Bauman19bac1e2014-05-06 15:23:49 -04001162 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1163 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1164
Nicolas Capens7551ac62016-01-20 17:11:53 -05001165 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001166 }
1167
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001168 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001169 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001170 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001171
John Bauman19bac1e2014-05-06 15:23:49 -04001172 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001173 {
1174 condition = ~condition;
1175 }
1176
Nicolas Capens7551ac62016-01-20 17:11:53 -05001177 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001178
1179 if(!labelBlock[labelIndex])
1180 {
1181 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1182 }
1183
John Bauman19bac1e2014-05-06 15:23:49 -04001184 if(callRetBlock[labelIndex].size() > 1)
1185 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001186 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001187 }
John Bauman89401822014-05-06 15:04:28 -04001188
Nicolas Capens7551ac62016-01-20 17:11:53 -05001189 enableIndex++;
1190 enableStack[enableIndex] = condition;
1191 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001192
John Bauman19bac1e2014-05-06 15:23:49 -04001193 Bool notAllFalse = SignMask(condition) != 0;
1194 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1195 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001196
Nicolas Capens7551ac62016-01-20 17:11:53 -05001197 enableIndex--;
1198 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001199 }
1200
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001201 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001202 {
1203 ifDepth--;
1204
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001205 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1206 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001207
1208 if(isConditionalIf[ifDepth])
1209 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001210 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001211 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001212
1213 branch(notAllFalse, falseBlock, endBlock);
1214
Nicolas Capens7551ac62016-01-20 17:11:53 -05001215 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001216 }
1217 else
1218 {
1219 Nucleus::createBr(endBlock);
1220 Nucleus::setInsertBlock(falseBlock);
1221 }
1222
1223 ifFalseBlock[ifDepth] = endBlock;
1224
1225 ifDepth++;
1226 }
1227
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001228 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001229 {
1230 ifDepth--;
1231
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001232 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001233
1234 Nucleus::createBr(endBlock);
1235 Nucleus::setInsertBlock(endBlock);
1236
1237 if(isConditionalIf[ifDepth])
1238 {
1239 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001240 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001241 }
1242 }
1243
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001244 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001245 {
1246 loopRepDepth--;
1247
Nicolas Capens7551ac62016-01-20 17:11:53 -05001248 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001249
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001250 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1251 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001252
1253 Nucleus::createBr(testBlock);
1254 Nucleus::setInsertBlock(endBlock);
1255
Nicolas Capens7551ac62016-01-20 17:11:53 -05001256 loopDepth--;
1257 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001258 }
1259
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001260 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001261 {
1262 loopRepDepth--;
1263
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001264 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1265 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001266
1267 Nucleus::createBr(testBlock);
1268 Nucleus::setInsertBlock(endBlock);
1269
Nicolas Capens7551ac62016-01-20 17:11:53 -05001270 loopDepth--;
1271 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001272 }
1273
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001274 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001275 {
1276 loopRepDepth--;
1277
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001278 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1279 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001280
1281 Nucleus::createBr(testBlock);
1282 Nucleus::setInsertBlock(endBlock);
1283
Nicolas Capens7551ac62016-01-20 17:11:53 -05001284 enableIndex--;
1285 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001286 whileTest = false;
1287 }
1288
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001289 void VertexProgram::ENDSWITCH()
1290 {
1291 loopRepDepth--;
1292
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001293 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001294
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001295 Nucleus::createBr(loopRepEndBlock[loopRepDepth]);
1296 Nucleus::setInsertBlock(endBlock);
1297
1298 enableIndex--;
1299 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1300 }
1301
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001302 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001303 {
John Bauman19bac1e2014-05-06 15:23:49 -04001304 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001305 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001306 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001307 }
John Bauman19bac1e2014-05-06 15:23:49 -04001308 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001309 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001310 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001311 }
John Bauman19bac1e2014-05-06 15:23:49 -04001312 else
1313 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001314 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001315 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001316 }
John Bauman89401822014-05-06 15:04:28 -04001317 }
1318
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001319 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001320 {
1321 ASSERT(ifDepth < 24 + 4);
1322
Nicolas Capens7551ac62016-01-20 17:11:53 -05001323 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001324
John Bauman19bac1e2014-05-06 15:23:49 -04001325 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001326 {
John Bauman19bac1e2014-05-06 15:23:49 -04001327 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001328 }
1329
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001330 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1331 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001332
1333 branch(condition, trueBlock, falseBlock);
1334
1335 isConditionalIf[ifDepth] = false;
1336 ifFalseBlock[ifDepth] = falseBlock;
1337
1338 ifDepth++;
1339 }
1340
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001341 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001342 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001343 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001344
John Bauman19bac1e2014-05-06 15:23:49 -04001345 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001346 {
1347 condition = ~condition;
1348 }
1349
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001350 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001351 }
1352
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001353 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001354 {
1355 Int4 condition;
1356
1357 switch(control)
1358 {
John Bauman19bac1e2014-05-06 15:23:49 -04001359 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1360 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1361 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1362 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1363 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1364 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001365 default:
1366 ASSERT(false);
1367 }
1368
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001369 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001370 }
1371
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001372 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001373 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001374 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001375
Nicolas Capens7551ac62016-01-20 17:11:53 -05001376 enableIndex++;
1377 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001378
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001379 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1380 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001381
John Bauman19bac1e2014-05-06 15:23:49 -04001382 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001383
1384 branch(notAllFalse, trueBlock, falseBlock);
1385
1386 isConditionalIf[ifDepth] = true;
1387 ifFalseBlock[ifDepth] = falseBlock;
1388
1389 ifDepth++;
1390 breakDepth++;
1391 }
1392
1393 void VertexProgram::LABEL(int labelIndex)
1394 {
John Bauman19bac1e2014-05-06 15:23:49 -04001395 if(!labelBlock[labelIndex])
1396 {
1397 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1398 }
1399
John Bauman89401822014-05-06 15:04:28 -04001400 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001401 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001402 }
1403
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001404 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001405 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001406 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001407
Nicolas Capens7551ac62016-01-20 17:11:53 -05001408 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1409 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1410 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001411
1412 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001413 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001414 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001415 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001416 }
1417
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001418 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1419 BasicBlock *testBlock = Nucleus::createBasicBlock();
1420 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001421
1422 loopRepTestBlock[loopRepDepth] = testBlock;
1423 loopRepEndBlock[loopRepDepth] = endBlock;
1424
1425 // FIXME: jump(testBlock)
1426 Nucleus::createBr(testBlock);
1427 Nucleus::setInsertBlock(testBlock);
1428
Nicolas Capens7551ac62016-01-20 17:11:53 -05001429 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001430 Nucleus::setInsertBlock(loopBlock);
1431
Nicolas Capens7551ac62016-01-20 17:11:53 -05001432 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001433
John Bauman89401822014-05-06 15:04:28 -04001434 loopRepDepth++;
1435 breakDepth = 0;
1436 }
1437
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001438 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001439 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001440 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001441
Nicolas Capens7551ac62016-01-20 17:11:53 -05001442 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1443 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001444
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001445 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1446 BasicBlock *testBlock = Nucleus::createBasicBlock();
1447 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001448
1449 loopRepTestBlock[loopRepDepth] = testBlock;
1450 loopRepEndBlock[loopRepDepth] = endBlock;
1451
1452 // FIXME: jump(testBlock)
1453 Nucleus::createBr(testBlock);
1454 Nucleus::setInsertBlock(testBlock);
1455
Nicolas Capens7551ac62016-01-20 17:11:53 -05001456 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001457 Nucleus::setInsertBlock(loopBlock);
1458
Nicolas Capens7551ac62016-01-20 17:11:53 -05001459 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001460
1461 loopRepDepth++;
1462 breakDepth = 0;
1463 }
1464
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001465 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001466 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001467 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001468
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001469 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1470 BasicBlock *testBlock = Nucleus::createBasicBlock();
1471 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001472
John Bauman19bac1e2014-05-06 15:23:49 -04001473 loopRepTestBlock[loopRepDepth] = testBlock;
1474 loopRepEndBlock[loopRepDepth] = endBlock;
1475
Nicolas Capens7551ac62016-01-20 17:11:53 -05001476 Int4 restoreBreak = enableBreak;
1477 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001478
1479 // FIXME: jump(testBlock)
1480 Nucleus::createBr(testBlock);
1481 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001482 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001483
Nicolas Capensc2534f42016-04-04 11:13:24 -04001484 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001485 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001486 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001487 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001488 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001489
1490 Bool notAllFalse = SignMask(condition) != 0;
1491 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001492
John Bauman19bac1e2014-05-06 15:23:49 -04001493 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001494 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001495
John Bauman19bac1e2014-05-06 15:23:49 -04001496 Nucleus::setInsertBlock(loopBlock);
1497
1498 loopRepDepth++;
1499 breakDepth = 0;
1500 }
1501
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001502 void VertexProgram::SWITCH()
1503 {
1504 enableIndex++;
1505 enableStack[enableIndex] = Int4(0xFFFFFFFF);
1506
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001507 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001508
1509 loopRepTestBlock[loopRepDepth] = nullptr;
1510 loopRepEndBlock[loopRepDepth] = endBlock;
1511
1512 loopRepDepth++;
1513 breakDepth = 0;
1514 }
1515
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001516 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001517 {
John Bauman19bac1e2014-05-06 15:23:49 -04001518 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001519 {
1520 returnBlock = Nucleus::createBasicBlock();
1521 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001522 }
1523 else
1524 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001525 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001526
John Bauman19bac1e2014-05-06 15:23:49 -04001527 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001528 {
John Bauman19bac1e2014-05-06 15:23:49 -04001529 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001530 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001531
Nicolas Capens19336542016-09-26 10:32:29 -04001532 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001533 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001534
1535 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1536 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001537 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001538 }
1539 }
1540 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1541 {
1542 Nucleus::createBr(callRetBlock[currentLabel][0]);
1543 }
1544 else // Function isn't called
1545 {
1546 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001547 }
1548
1549 Nucleus::setInsertBlock(unreachableBlock);
1550 Nucleus::createUnreachable();
1551 }
1552 }
1553
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001554 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001555 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001556 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001557
John Bauman19bac1e2014-05-06 15:23:49 -04001558 // FIXME: Return from function if all instances left
1559 // FIXME: Use enableLeave in other control-flow constructs
1560 }
John Bauman89401822014-05-06 15:04:28 -04001561
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001562 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001563 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001564 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001565 }
1566
Nicolas Capensa0b57832017-11-07 13:07:53 -05001567 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001568 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001569 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001570 }
1571
Nicolas Capensa0b57832017-11-07 13:07:53 -05001572 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001573 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001574 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001575 }
1576
Nicolas Capensa0b57832017-11-07 13:07:53 -05001577 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001578 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001579 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001580 }
1581
Nicolas Capensa0b57832017-11-07 13:07:53 -05001582 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001583 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001584 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001585 }
1586
Nicolas Capensa0b57832017-11-07 13:07:53 -05001587 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001588 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001589 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001590 }
1591
Nicolas Capensa0b57832017-11-07 13:07:53 -05001592 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001593 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001594 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1595 }
1596
1597 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1598 {
1599 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001600 }
1601
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001602 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001603 {
Alexis Hetu95ac1872016-06-06 13:26:52 -04001604 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
Nicolas Capens89a218b2017-11-07 13:05:20 -05001605 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001606 }
1607
Nicolas Capensa0b57832017-11-07 13:07:53 -05001608 Vector4f VertexProgram::sampleTexture(const Src &s, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
John Bauman19bac1e2014-05-06 15:23:49 -04001609 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001610 Vector4f tmp;
1611
John Bauman19bac1e2014-05-06 15:23:49 -04001612 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1613 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001614 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001615 }
1616 else
1617 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001618 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001619
Nicolas Capensc2534f42016-04-04 11:13:24 -04001620 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001621 {
1622 if(shader->usesSampler(i))
1623 {
1624 If(index == i)
1625 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001626 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001627 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1628 }
1629 }
1630 }
1631 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001632
Nicolas Capens89a218b2017-11-07 13:05:20 -05001633 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001634 c.x = tmp[(s.swizzle >> 0) & 0x3];
1635 c.y = tmp[(s.swizzle >> 2) & 0x3];
1636 c.z = tmp[(s.swizzle >> 4) & 0x3];
1637 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001638
1639 return c;
1640 }
1641
Nicolas Capensa0b57832017-11-07 13:07:53 -05001642 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001643 {
1644 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001645 return SamplerCore(constants, state.sampler[sampler]).sampleTexture(texture, uvwq.x, uvwq.y, uvwq.z, uvwq.w, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001646 }
John Bauman89401822014-05-06 15:04:28 -04001647}