blob: 9db65ed42ef40f574bc5081fbe2312d26f6df600 [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 Hetu8d78cf72015-08-28 14:24:45 -0400211 case Shader::OPCODE_FLOATBITSTOINT:
212 case Shader::OPCODE_FLOATBITSTOUINT:
213 case Shader::OPCODE_INTBITSTOFLOAT:
214 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400215 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
216 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400217 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400218 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
219 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400220 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500221 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
222 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
223 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
224 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
225 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
226 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
227 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
228 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400229 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
230 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500231 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400232 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
233 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500234 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400235 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400236 case Shader::OPCODE_NEG: neg(d, s0); break;
237 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500238 case Shader::OPCODE_F2B: f2b(d, s0); break;
239 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400240 case Shader::OPCODE_F2I: f2i(d, s0); break;
241 case Shader::OPCODE_I2F: i2f(d, s0); break;
242 case Shader::OPCODE_F2U: f2u(d, s0); break;
243 case Shader::OPCODE_U2F: u2f(d, s0); break;
244 case Shader::OPCODE_I2B: i2b(d, s0); break;
245 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500246 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400247 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500248 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
249 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
250 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
251 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
252 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
253 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
254 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400255 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
256 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500257 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400258 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
259 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
260 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500261 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
262 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
263 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
264 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
265 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
266 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
267 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
268 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
269 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
270 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
271 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
272 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
273 case Shader::OPCODE_SGE: step(d, s1, s0); break;
274 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400275 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500276 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
277 case Shader::OPCODE_COS: cos(d, s0, pp); break;
278 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
279 case Shader::OPCODE_TAN: tan(d, s0); break;
280 case Shader::OPCODE_ACOS: acos(d, s0); break;
281 case Shader::OPCODE_ASIN: asin(d, s0); break;
282 case Shader::OPCODE_ATAN: atan(d, s0); break;
283 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
284 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
285 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
286 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
287 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
288 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
289 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
290 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
291 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400292 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500293 case Shader::OPCODE_BREAK: BREAK(); break;
294 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
295 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
296 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
297 case Shader::OPCODE_TEST: TEST(); break;
298 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
299 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
300 case Shader::OPCODE_ELSE: ELSE(); break;
301 case Shader::OPCODE_ENDIF: ENDIF(); break;
302 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
303 case Shader::OPCODE_ENDREP: ENDREP(); break;
304 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400305 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500306 case Shader::OPCODE_IF: IF(src0); break;
307 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
308 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
309 case Shader::OPCODE_LOOP: LOOP(src1); break;
310 case Shader::OPCODE_REP: REP(src0); break;
311 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400312 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500313 case Shader::OPCODE_RET: RET(); break;
314 case Shader::OPCODE_LEAVE: LEAVE(); break;
315 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
316 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400317 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500318 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
319 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
320 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
321 case Shader::OPCODE_ALL: all(d.x, s0); break;
322 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400323 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
324 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
325 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
326 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400327 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
328 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500329 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
330 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400331 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500332 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
Meng-Lin Wu9d62c482016-06-14 11:11:25 -0400333 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1); break;
334 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500335 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
336 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
337 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
338 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400339 default:
340 ASSERT(false);
341 }
342
John Bauman19bac1e2014-05-06 15:23:49 -0400343 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400344 {
John Bauman19bac1e2014-05-06 15:23:49 -0400345 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400346 {
John Bauman19bac1e2014-05-06 15:23:49 -0400347 switch(opcode)
348 {
349 case Shader::OPCODE_DIV:
350 if(dst.x) d.x = Trunc(d.x);
351 if(dst.y) d.y = Trunc(d.y);
352 if(dst.z) d.z = Trunc(d.z);
353 if(dst.w) d.w = Trunc(d.w);
354 break;
355 default:
356 break; // No truncation to integer required when arguments are integer
357 }
John Bauman89401822014-05-06 15:04:28 -0400358 }
359
John Bauman19bac1e2014-05-06 15:23:49 -0400360 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400361 {
John Bauman19bac1e2014-05-06 15:23:49 -0400362 if(dst.x) d.x = Max(d.x, Float4(0.0f));
363 if(dst.y) d.y = Max(d.y, Float4(0.0f));
364 if(dst.z) d.z = Max(d.z, Float4(0.0f));
365 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400366
John Bauman19bac1e2014-05-06 15:23:49 -0400367 if(dst.x) d.x = Min(d.x, Float4(1.0f));
368 if(dst.y) d.y = Min(d.y, Float4(1.0f));
369 if(dst.z) d.z = Min(d.z, Float4(1.0f));
370 if(dst.w) d.w = Min(d.w, Float4(1.0f));
371 }
372
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400373 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400374 {
375 Vector4f pDst; // FIXME: Rename
376
377 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400378 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500379 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400380 case Shader::PARAMETER_TEMP:
381 if(dst.rel.type == Shader::PARAMETER_VOID)
382 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500383 if(dst.x) pDst.x = r[dst.index].x;
384 if(dst.y) pDst.y = r[dst.index].y;
385 if(dst.z) pDst.z = r[dst.index].z;
386 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400387 }
388 else
389 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500390 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400391
Nicolas Capens7551ac62016-01-20 17:11:53 -0500392 if(dst.x) pDst.x = r[dst.index + a].x;
393 if(dst.y) pDst.y = r[dst.index + a].y;
394 if(dst.z) pDst.z = r[dst.index + a].z;
395 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400396 }
397 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500398 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400399 case Shader::PARAMETER_RASTOUT:
400 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400401 {
402 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500403 if(dst.x) pDst.x = o[Pos].x;
404 if(dst.y) pDst.y = o[Pos].y;
405 if(dst.z) pDst.z = o[Pos].z;
406 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400407 break;
408 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500409 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400410 break;
411 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500412 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400413 break;
414 default:
415 ASSERT(false);
416 }
417 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400418 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400419 if(dst.x) pDst.x = o[C0 + dst.index].x;
420 if(dst.y) pDst.y = o[C0 + dst.index].y;
421 if(dst.z) pDst.z = o[C0 + dst.index].z;
422 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400423 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400424 case Shader::PARAMETER_TEXCRDOUT:
425 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400426 if(version < 0x0300)
427 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500428 if(dst.x) pDst.x = o[T0 + dst.index].x;
429 if(dst.y) pDst.y = o[T0 + dst.index].y;
430 if(dst.z) pDst.z = o[T0 + dst.index].z;
431 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400432 }
433 else
434 {
John Bauman19bac1e2014-05-06 15:23:49 -0400435 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400436 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500437 if(dst.x) pDst.x = o[dst.index].x;
438 if(dst.y) pDst.y = o[dst.index].y;
439 if(dst.z) pDst.z = o[dst.index].z;
440 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400441 }
John Bauman19bac1e2014-05-06 15:23:49 -0400442 else
443 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500444 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400445
Nicolas Capens7551ac62016-01-20 17:11:53 -0500446 if(dst.x) pDst.x = o[dst.index + a].x;
447 if(dst.y) pDst.y = o[dst.index + a].y;
448 if(dst.z) pDst.z = o[dst.index + a].z;
449 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400450 }
451 }
452 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500453 case Shader::PARAMETER_LABEL: break;
454 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
455 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400456 default:
457 ASSERT(false);
458 }
459
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500460 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400461
462 Int4 xEnable = enable;
463 Int4 yEnable = enable;
464 Int4 zEnable = enable;
465 Int4 wEnable = enable;
466
467 if(predicate)
468 {
John Bauman19bac1e2014-05-06 15:23:49 -0400469 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400470
Nicolas Capens7551ac62016-01-20 17:11:53 -0500471 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
472 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
473 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
474 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400475
John Bauman19bac1e2014-05-06 15:23:49 -0400476 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400477 {
John Bauman19bac1e2014-05-06 15:23:49 -0400478 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
479 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
480 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
481 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400482 }
483 else
484 {
John Bauman19bac1e2014-05-06 15:23:49 -0400485 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
486 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
487 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
488 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400489 }
490 }
491
John Bauman19bac1e2014-05-06 15:23:49 -0400492 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
493 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
494 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
495 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400496
John Bauman19bac1e2014-05-06 15:23:49 -0400497 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
498 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
499 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
500 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400501 }
502
John Bauman19bac1e2014-05-06 15:23:49 -0400503 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400504 {
John Bauman19bac1e2014-05-06 15:23:49 -0400505 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400506 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400507 case Shader::PARAMETER_TEMP:
508 if(dst.rel.type == Shader::PARAMETER_VOID)
509 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500510 if(dst.x) r[dst.index].x = d.x;
511 if(dst.y) r[dst.index].y = d.y;
512 if(dst.z) r[dst.index].z = d.z;
513 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400514 }
515 else
516 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500517 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400518
Nicolas Capens7551ac62016-01-20 17:11:53 -0500519 if(dst.x) r[dst.index + a].x = d.x;
520 if(dst.y) r[dst.index + a].y = d.y;
521 if(dst.z) r[dst.index + a].z = d.z;
522 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400523 }
John Bauman89401822014-05-06 15:04:28 -0400524 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400525 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500526 if(dst.x) a0.x = d.x;
527 if(dst.y) a0.y = d.y;
528 if(dst.z) a0.z = d.z;
529 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400530 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400531 case Shader::PARAMETER_RASTOUT:
532 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400533 {
534 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500535 if(dst.x) o[Pos].x = d.x;
536 if(dst.y) o[Pos].y = d.y;
537 if(dst.z) o[Pos].z = d.z;
538 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400539 break;
540 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500541 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400542 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500543 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500544 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400545 break;
546 default: ASSERT(false);
547 }
548 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500549 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400550 if(dst.x) o[C0 + dst.index].x = d.x;
551 if(dst.y) o[C0 + dst.index].y = d.y;
552 if(dst.z) o[C0 + dst.index].z = d.z;
553 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400554 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400555 case Shader::PARAMETER_TEXCRDOUT:
556 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400557 if(version < 0x0300)
558 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500559 if(dst.x) o[T0 + dst.index].x = d.x;
560 if(dst.y) o[T0 + dst.index].y = d.y;
561 if(dst.z) o[T0 + dst.index].z = d.z;
562 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400563 }
564 else
565 {
John Bauman19bac1e2014-05-06 15:23:49 -0400566 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400567 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500568 if(dst.x) o[dst.index].x = d.x;
569 if(dst.y) o[dst.index].y = d.y;
570 if(dst.z) o[dst.index].z = d.z;
571 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400572 }
John Bauman19bac1e2014-05-06 15:23:49 -0400573 else
574 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500575 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400576
Nicolas Capens7551ac62016-01-20 17:11:53 -0500577 if(dst.x) o[dst.index + a].x = d.x;
578 if(dst.y) o[dst.index + a].y = d.y;
579 if(dst.z) o[dst.index + a].z = d.z;
580 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400581 }
582 }
583 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500584 case Shader::PARAMETER_LABEL: break;
585 case Shader::PARAMETER_PREDICATE: p0 = d; break;
586 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400587 default:
588 ASSERT(false);
589 }
590 }
591 }
592
John Bauman19bac1e2014-05-06 15:23:49 -0400593 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400594 {
595 Nucleus::setInsertBlock(returnBlock);
596 }
597 }
598
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500599 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400600 {
John Bauman19bac1e2014-05-06 15:23:49 -0400601 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400602 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400603 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400604 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400605 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400606
607 switch(usage)
608 {
609 case 0xFF:
610 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400611 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500612 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400613 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400614 case Shader::USAGE_TEXCOORD:
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_POSITION:
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_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500627 o[i].x = v[i].x;
628 o[i].y = v[i].y;
629 o[i].z = v[i].z;
630 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400631 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400632 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500633 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400634 break;
635 default:
636 ASSERT(false);
637 }
638 }
639 }
640 else
641 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500642 o[Pos].x = v[PositionT].x;
643 o[Pos].y = v[PositionT].y;
644 o[Pos].z = v[PositionT].z;
645 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400646
647 for(int i = 0; i < 2; i++)
648 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400649 o[C0 + i].x = v[Color0 + i].x;
650 o[C0 + i].y = v[Color0 + i].y;
651 o[C0 + i].z = v[Color0 + i].z;
652 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400653 }
654
655 for(int i = 0; i < 8; i++)
656 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500657 o[T0 + i].x = v[TexCoord0 + i].x;
658 o[T0 + i].y = v[TexCoord0 + i].y;
659 o[T0 + i].z = v[TexCoord0 + i].z;
660 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400661 }
662
Nicolas Capens7551ac62016-01-20 17:11:53 -0500663 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400664 }
665 }
666
Nicolas Capensc2534f42016-04-04 11:13:24 -0400667 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400668 {
John Bauman19bac1e2014-05-06 15:23:49 -0400669 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500670 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400671
John Bauman89401822014-05-06 15:04:28 -0400672 switch(src.type)
673 {
John Bauman19bac1e2014-05-06 15:23:49 -0400674 case Shader::PARAMETER_TEMP:
675 if(src.rel.type == Shader::PARAMETER_VOID)
676 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500677 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400678 }
679 else
680 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400681 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400682 }
683 break;
684 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500685 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400686 break;
687 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400688 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400689 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500690 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400691 }
692 else
693 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400694 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400695 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400696 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500697 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400698 case Shader::PARAMETER_FLOAT4LITERAL:
699 reg.x = Float4(src.value[0]);
700 reg.y = Float4(src.value[1]);
701 reg.z = Float4(src.value[2]);
702 reg.w = Float4(src.value[3]);
703 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500704 case Shader::PARAMETER_ADDR: reg = a0; break;
705 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
706 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
707 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
708 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400709 case Shader::PARAMETER_SAMPLER:
710 if(src.rel.type == Shader::PARAMETER_VOID)
711 {
712 reg.x = As<Float4>(Int4(i));
713 }
714 else if(src.rel.type == Shader::PARAMETER_TEMP)
715 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500716 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400717 }
718 return reg;
719 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400720 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400721 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500722 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400723 }
724 else
725 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400726 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400727 }
728 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400729 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400730 if(src.index == Shader::InstanceIDIndex)
731 {
732 reg.x = As<Float>(instanceID);
733 }
734 else if(src.index == Shader::VertexIDIndex)
735 {
736 reg.x = As<Float4>(vertexID);
737 }
738 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400739 return reg;
John Bauman89401822014-05-06 15:04:28 -0400740 default:
741 ASSERT(false);
742 }
743
John Bauman66b8ab22014-05-06 15:57:45 -0400744 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
745 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
746 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
747 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400748
John Bauman66b8ab22014-05-06 15:57:45 -0400749 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400750
751 switch(src.modifier)
752 {
John Bauman19bac1e2014-05-06 15:23:49 -0400753 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400754 mod.x = x;
755 mod.y = y;
756 mod.z = z;
757 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400758 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400759 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400760 mod.x = -x;
761 mod.y = -y;
762 mod.z = -z;
763 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400764 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400765 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400766 mod.x = Abs(x);
767 mod.y = Abs(y);
768 mod.z = Abs(z);
769 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400770 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400771 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400772 mod.x = -Abs(x);
773 mod.y = -Abs(y);
774 mod.z = -Abs(z);
775 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400776 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400777 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400778 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
779 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
780 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
781 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400782 break;
783 default:
784 ASSERT(false);
785 }
786
787 return mod;
788 }
789
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400790 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
791 {
792 if(bufferIndex == -1)
793 {
794 return data + OFFSET(DrawData, vs.c[index]);
795 }
796 else
797 {
798 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
799 }
800 }
801
802 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
803 {
804 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
805 }
806
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500807 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400808 {
John Bauman19bac1e2014-05-06 15:23:49 -0400809 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500810 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400811
812 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
813 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400814 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400815
816 c.x = c.x.xxxx;
817 c.y = c.y.yyyy;
818 c.z = c.z.zzzz;
819 c.w = c.w.wwww;
820
Nicolas Capenseafdb222015-05-15 15:24:08 -0400821 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400822 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500823 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400824 {
825 const Shader::Instruction &instruction = *shader->getInstruction(j);
826
827 if(instruction.opcode == Shader::OPCODE_DEF)
828 {
829 if(instruction.dst.index == i)
830 {
831 c.x = Float4(instruction.src[0].value[0]);
832 c.y = Float4(instruction.src[0].value[1]);
833 c.z = Float4(instruction.src[0].value[2]);
834 c.w = Float4(instruction.src[0].value[3]);
835
836 break;
837 }
838 }
839 }
840 }
841 }
842 else if(src.rel.type == Shader::PARAMETER_LOOP)
843 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500844 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400845
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400846 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400847
848 c.x = c.x.xxxx;
849 c.y = c.y.yyyy;
850 c.z = c.z.zzzz;
851 c.w = c.w.wwww;
852 }
853 else
854 {
855 if(src.rel.deterministic)
856 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400857 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400858
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400859 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500860
John Bauman19bac1e2014-05-06 15:23:49 -0400861 c.x = c.x.xxxx;
862 c.y = c.y.yyyy;
863 c.z = c.z.zzzz;
864 c.w = c.w.wwww;
865 }
866 else
867 {
868 int component = src.rel.swizzle & 0x03;
869 Float4 a;
870
871 switch(src.rel.type)
872 {
Nicolas Capens32980ac2016-08-15 15:33:58 -0400873 case Shader::PARAMETER_ADDR: a = a0[component]; break;
874 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
875 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
876 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
877 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400878 case Shader::PARAMETER_MISCTYPE:
879 if(src.rel.index == Shader::InstanceIDIndex)
880 {
881 a = As<Float4>(Int4(instanceID)); break;
882 }
883 else if(src.rel.index == Shader::VertexIDIndex)
884 {
885 a = As<Float4>(vertexID); break;
886 }
887 else ASSERT(false);
888 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400889 default: ASSERT(false);
890 }
891
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400892 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400893
Alexis Hetu028f41b2016-01-13 14:40:47 -0500894 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 -0500895
John Bauman19bac1e2014-05-06 15:23:49 -0400896 Int index0 = Extract(index, 0);
897 Int index1 = Extract(index, 1);
898 Int index2 = Extract(index, 2);
899 Int index3 = Extract(index, 3);
900
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400901 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
902 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
903 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
904 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400905
906 transpose4x4(c.x, c.y, c.z, c.w);
907 }
908 }
909
910 return c;
911 }
912
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400913 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400914 {
915 ASSERT(var.rel.deterministic);
916
917 if(var.rel.type == Shader::PARAMETER_TEMP)
918 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500919 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400920 }
921 else if(var.rel.type == Shader::PARAMETER_INPUT)
922 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500923 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400924 }
925 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
926 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500927 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400928 }
929 else if(var.rel.type == Shader::PARAMETER_CONST)
930 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500931 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400932 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500933 else if(var.rel.type == Shader::PARAMETER_LOOP)
934 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500935 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500936 }
John Bauman19bac1e2014-05-06 15:23:49 -0400937 else ASSERT(false);
938
939 return 0;
940 }
941
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500942 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400943 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500944 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500945
John Baumand4ae8632014-05-06 16:18:33 -0400946 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400947 {
John Baumand4ae8632014-05-06 16:18:33 -0400948 if(shader->containsBreakInstruction() && instruction->analysisBreak)
949 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500950 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400951 }
John Bauman19bac1e2014-05-06 15:23:49 -0400952
John Baumand4ae8632014-05-06 16:18:33 -0400953 if(shader->containsContinueInstruction() && instruction->analysisContinue)
954 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500955 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400956 }
John Bauman19bac1e2014-05-06 15:23:49 -0400957
John Baumand4ae8632014-05-06 16:18:33 -0400958 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
959 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500960 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400961 }
John Bauman19bac1e2014-05-06 15:23:49 -0400962 }
963
964 return enable;
965 }
966
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500967 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400968 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400969 Vector4f row0 = fetchRegister(src1, 0);
970 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400971
972 dst.x = dot3(src0, row0);
973 dst.y = dot3(src0, row1);
974 }
975
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500976 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400977 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400978 Vector4f row0 = fetchRegister(src1, 0);
979 Vector4f row1 = fetchRegister(src1, 1);
980 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400981
982 dst.x = dot3(src0, row0);
983 dst.y = dot3(src0, row1);
984 dst.z = dot3(src0, row2);
985 }
986
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500987 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400988 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400989 Vector4f row0 = fetchRegister(src1, 0);
990 Vector4f row1 = fetchRegister(src1, 1);
991 Vector4f row2 = fetchRegister(src1, 2);
992 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400993
994 dst.x = dot3(src0, row0);
995 dst.y = dot3(src0, row1);
996 dst.z = dot3(src0, row2);
997 dst.w = dot3(src0, row3);
998 }
999
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001000 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001001 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001002 Vector4f row0 = fetchRegister(src1, 0);
1003 Vector4f row1 = fetchRegister(src1, 1);
1004 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001005
1006 dst.x = dot4(src0, row0);
1007 dst.y = dot4(src0, row1);
1008 dst.z = dot4(src0, row2);
1009 }
1010
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001011 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001012 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001013 Vector4f row0 = fetchRegister(src1, 0);
1014 Vector4f row1 = fetchRegister(src1, 1);
1015 Vector4f row2 = fetchRegister(src1, 2);
1016 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001017
1018 dst.x = dot4(src0, row0);
1019 dst.y = dot4(src0, row1);
1020 dst.z = dot4(src0, row2);
1021 dst.w = dot4(src0, row3);
1022 }
1023
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001024 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001025 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001026 BasicBlock *deadBlock = Nucleus::createBasicBlock();
1027 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001028
1029 if(breakDepth == 0)
1030 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001031 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001032 Nucleus::createBr(endBlock);
1033 }
1034 else
1035 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001036 enableBreak = enableBreak & ~enableStack[enableIndex];
1037 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001038
Nicolas Capens7551ac62016-01-20 17:11:53 -05001039 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001040 branch(allBreak, endBlock, deadBlock);
1041 }
1042
1043 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001044 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001045 }
1046
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001047 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001048 {
1049 Int4 condition;
1050
1051 switch(control)
1052 {
John Bauman19bac1e2014-05-06 15:23:49 -04001053 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1054 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1055 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1056 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1057 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1058 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001059 default:
1060 ASSERT(false);
1061 }
1062
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001063 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001064 }
1065
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001066 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001067 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001068 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001069
John Bauman19bac1e2014-05-06 15:23:49 -04001070 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001071 {
1072 condition = ~condition;
1073 }
1074
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001075 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001076 }
1077
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001078 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001079 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001080 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001081
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001082 BasicBlock *continueBlock = Nucleus::createBasicBlock();
1083 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001084
Nicolas Capens7551ac62016-01-20 17:11:53 -05001085 enableBreak = enableBreak & ~condition;
1086 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001087
Nicolas Capens7551ac62016-01-20 17:11:53 -05001088 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001089 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001090
John Bauman89401822014-05-06 15:04:28 -04001091 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001092 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001093 }
1094
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001095 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001096 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001097 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001098 }
1099
1100 void VertexProgram::TEST()
1101 {
1102 whileTest = true;
1103 }
1104
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001105 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001106 {
1107 if(!labelBlock[labelIndex])
1108 {
1109 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1110 }
1111
John Bauman19bac1e2014-05-06 15:23:49 -04001112 if(callRetBlock[labelIndex].size() > 1)
1113 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001114 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001115 }
John Bauman89401822014-05-06 15:04:28 -04001116
Nicolas Capens7551ac62016-01-20 17:11:53 -05001117 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001118
1119 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001120 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1121
Nicolas Capens7551ac62016-01-20 17:11:53 -05001122 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001123 }
1124
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001125 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001126 {
John Bauman19bac1e2014-05-06 15:23:49 -04001127 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001128 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001129 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001130 }
John Bauman19bac1e2014-05-06 15:23:49 -04001131 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001132 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001133 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001134 }
1135 else ASSERT(false);
1136 }
1137
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001138 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001139 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001140 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001141
John Bauman19bac1e2014-05-06 15:23:49 -04001142 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001143 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001144 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001145 }
1146
1147 if(!labelBlock[labelIndex])
1148 {
1149 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1150 }
1151
John Bauman19bac1e2014-05-06 15:23:49 -04001152 if(callRetBlock[labelIndex].size() > 1)
1153 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001154 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001155 }
John Bauman89401822014-05-06 15:04:28 -04001156
Nicolas Capens7551ac62016-01-20 17:11:53 -05001157 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001158
John Bauman19bac1e2014-05-06 15:23:49 -04001159 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1160 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1161
Nicolas Capens7551ac62016-01-20 17:11:53 -05001162 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001163 }
1164
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001165 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001166 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001167 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001168
John Bauman19bac1e2014-05-06 15:23:49 -04001169 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001170 {
1171 condition = ~condition;
1172 }
1173
Nicolas Capens7551ac62016-01-20 17:11:53 -05001174 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001175
1176 if(!labelBlock[labelIndex])
1177 {
1178 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1179 }
1180
John Bauman19bac1e2014-05-06 15:23:49 -04001181 if(callRetBlock[labelIndex].size() > 1)
1182 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001183 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001184 }
John Bauman89401822014-05-06 15:04:28 -04001185
Nicolas Capens7551ac62016-01-20 17:11:53 -05001186 enableIndex++;
1187 enableStack[enableIndex] = condition;
1188 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001189
John Bauman19bac1e2014-05-06 15:23:49 -04001190 Bool notAllFalse = SignMask(condition) != 0;
1191 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1192 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001193
Nicolas Capens7551ac62016-01-20 17:11:53 -05001194 enableIndex--;
1195 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001196 }
1197
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001198 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001199 {
1200 ifDepth--;
1201
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001202 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1203 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001204
1205 if(isConditionalIf[ifDepth])
1206 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001207 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001208 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001209
1210 branch(notAllFalse, falseBlock, endBlock);
1211
Nicolas Capens7551ac62016-01-20 17:11:53 -05001212 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001213 }
1214 else
1215 {
1216 Nucleus::createBr(endBlock);
1217 Nucleus::setInsertBlock(falseBlock);
1218 }
1219
1220 ifFalseBlock[ifDepth] = endBlock;
1221
1222 ifDepth++;
1223 }
1224
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001225 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001226 {
1227 ifDepth--;
1228
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001229 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001230
1231 Nucleus::createBr(endBlock);
1232 Nucleus::setInsertBlock(endBlock);
1233
1234 if(isConditionalIf[ifDepth])
1235 {
1236 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001237 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001238 }
1239 }
1240
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001241 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001242 {
1243 loopRepDepth--;
1244
Nicolas Capens7551ac62016-01-20 17:11:53 -05001245 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001246
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001247 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1248 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001249
1250 Nucleus::createBr(testBlock);
1251 Nucleus::setInsertBlock(endBlock);
1252
Nicolas Capens7551ac62016-01-20 17:11:53 -05001253 loopDepth--;
1254 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001255 }
1256
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001257 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001258 {
1259 loopRepDepth--;
1260
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001261 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1262 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001263
1264 Nucleus::createBr(testBlock);
1265 Nucleus::setInsertBlock(endBlock);
1266
Nicolas Capens7551ac62016-01-20 17:11:53 -05001267 loopDepth--;
1268 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001269 }
1270
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001271 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001272 {
1273 loopRepDepth--;
1274
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001275 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1276 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001277
1278 Nucleus::createBr(testBlock);
1279 Nucleus::setInsertBlock(endBlock);
1280
Nicolas Capens7551ac62016-01-20 17:11:53 -05001281 enableIndex--;
1282 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001283 whileTest = false;
1284 }
1285
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001286 void VertexProgram::ENDSWITCH()
1287 {
1288 loopRepDepth--;
1289
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001290 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001291
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001292 Nucleus::createBr(loopRepEndBlock[loopRepDepth]);
1293 Nucleus::setInsertBlock(endBlock);
1294
1295 enableIndex--;
1296 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1297 }
1298
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001299 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001300 {
John Bauman19bac1e2014-05-06 15:23:49 -04001301 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001302 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001303 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001304 }
John Bauman19bac1e2014-05-06 15:23:49 -04001305 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001306 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001307 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001308 }
John Bauman19bac1e2014-05-06 15:23:49 -04001309 else
1310 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001311 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001312 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001313 }
John Bauman89401822014-05-06 15:04:28 -04001314 }
1315
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001316 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001317 {
1318 ASSERT(ifDepth < 24 + 4);
1319
Nicolas Capens7551ac62016-01-20 17:11:53 -05001320 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001321
John Bauman19bac1e2014-05-06 15:23:49 -04001322 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001323 {
John Bauman19bac1e2014-05-06 15:23:49 -04001324 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001325 }
1326
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001327 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1328 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001329
1330 branch(condition, trueBlock, falseBlock);
1331
1332 isConditionalIf[ifDepth] = false;
1333 ifFalseBlock[ifDepth] = falseBlock;
1334
1335 ifDepth++;
1336 }
1337
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001338 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001339 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001340 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001341
John Bauman19bac1e2014-05-06 15:23:49 -04001342 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001343 {
1344 condition = ~condition;
1345 }
1346
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001347 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001348 }
1349
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001350 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001351 {
1352 Int4 condition;
1353
1354 switch(control)
1355 {
John Bauman19bac1e2014-05-06 15:23:49 -04001356 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1357 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1358 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1359 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1360 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1361 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001362 default:
1363 ASSERT(false);
1364 }
1365
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001366 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001367 }
1368
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001369 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001370 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001371 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001372
Nicolas Capens7551ac62016-01-20 17:11:53 -05001373 enableIndex++;
1374 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001375
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001376 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1377 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001378
John Bauman19bac1e2014-05-06 15:23:49 -04001379 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001380
1381 branch(notAllFalse, trueBlock, falseBlock);
1382
1383 isConditionalIf[ifDepth] = true;
1384 ifFalseBlock[ifDepth] = falseBlock;
1385
1386 ifDepth++;
1387 breakDepth++;
1388 }
1389
1390 void VertexProgram::LABEL(int labelIndex)
1391 {
John Bauman19bac1e2014-05-06 15:23:49 -04001392 if(!labelBlock[labelIndex])
1393 {
1394 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1395 }
1396
John Bauman89401822014-05-06 15:04:28 -04001397 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001398 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001399 }
1400
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001401 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001402 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001403 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001404
Nicolas Capens7551ac62016-01-20 17:11:53 -05001405 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1406 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1407 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001408
1409 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001410 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001411 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001412 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001413 }
1414
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001415 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1416 BasicBlock *testBlock = Nucleus::createBasicBlock();
1417 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001418
1419 loopRepTestBlock[loopRepDepth] = testBlock;
1420 loopRepEndBlock[loopRepDepth] = endBlock;
1421
1422 // FIXME: jump(testBlock)
1423 Nucleus::createBr(testBlock);
1424 Nucleus::setInsertBlock(testBlock);
1425
Nicolas Capens7551ac62016-01-20 17:11:53 -05001426 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001427 Nucleus::setInsertBlock(loopBlock);
1428
Nicolas Capens7551ac62016-01-20 17:11:53 -05001429 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001430
John Bauman89401822014-05-06 15:04:28 -04001431 loopRepDepth++;
1432 breakDepth = 0;
1433 }
1434
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001435 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001436 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001437 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001438
Nicolas Capens7551ac62016-01-20 17:11:53 -05001439 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1440 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001441
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001442 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1443 BasicBlock *testBlock = Nucleus::createBasicBlock();
1444 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001445
1446 loopRepTestBlock[loopRepDepth] = testBlock;
1447 loopRepEndBlock[loopRepDepth] = endBlock;
1448
1449 // FIXME: jump(testBlock)
1450 Nucleus::createBr(testBlock);
1451 Nucleus::setInsertBlock(testBlock);
1452
Nicolas Capens7551ac62016-01-20 17:11:53 -05001453 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001454 Nucleus::setInsertBlock(loopBlock);
1455
Nicolas Capens7551ac62016-01-20 17:11:53 -05001456 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001457
1458 loopRepDepth++;
1459 breakDepth = 0;
1460 }
1461
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001462 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001463 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001464 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001465
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001466 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1467 BasicBlock *testBlock = Nucleus::createBasicBlock();
1468 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001469
John Bauman19bac1e2014-05-06 15:23:49 -04001470 loopRepTestBlock[loopRepDepth] = testBlock;
1471 loopRepEndBlock[loopRepDepth] = endBlock;
1472
Nicolas Capens7551ac62016-01-20 17:11:53 -05001473 Int4 restoreBreak = enableBreak;
1474 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001475
1476 // FIXME: jump(testBlock)
1477 Nucleus::createBr(testBlock);
1478 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001479 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001480
Nicolas Capensc2534f42016-04-04 11:13:24 -04001481 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001482 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001483 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001484 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001485 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001486
1487 Bool notAllFalse = SignMask(condition) != 0;
1488 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001489
John Bauman19bac1e2014-05-06 15:23:49 -04001490 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001491 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001492
John Bauman19bac1e2014-05-06 15:23:49 -04001493 Nucleus::setInsertBlock(loopBlock);
1494
1495 loopRepDepth++;
1496 breakDepth = 0;
1497 }
1498
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001499 void VertexProgram::SWITCH()
1500 {
1501 enableIndex++;
1502 enableStack[enableIndex] = Int4(0xFFFFFFFF);
1503
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001504 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001505
1506 loopRepTestBlock[loopRepDepth] = nullptr;
1507 loopRepEndBlock[loopRepDepth] = endBlock;
1508
1509 loopRepDepth++;
1510 breakDepth = 0;
1511 }
1512
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001513 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001514 {
John Bauman19bac1e2014-05-06 15:23:49 -04001515 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001516 {
1517 returnBlock = Nucleus::createBasicBlock();
1518 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001519 }
1520 else
1521 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001522 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001523
John Bauman19bac1e2014-05-06 15:23:49 -04001524 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001525 {
John Bauman19bac1e2014-05-06 15:23:49 -04001526 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001527 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001528
Nicolas Capens19336542016-09-26 10:32:29 -04001529 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001530 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001531
1532 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1533 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001534 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001535 }
1536 }
1537 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1538 {
1539 Nucleus::createBr(callRetBlock[currentLabel][0]);
1540 }
1541 else // Function isn't called
1542 {
1543 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001544 }
1545
1546 Nucleus::setInsertBlock(unreachableBlock);
1547 Nucleus::createUnreachable();
1548 }
1549 }
1550
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001551 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001552 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001553 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001554
John Bauman19bac1e2014-05-06 15:23:49 -04001555 // FIXME: Return from function if all instances left
1556 // FIXME: Use enableLeave in other control-flow constructs
1557 }
John Bauman89401822014-05-06 15:04:28 -04001558
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001559 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001560 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001561 sampleTexture(dst, src1, src0, a0, a0, src0, Lod);
John Bauman89401822014-05-06 15:04:28 -04001562 }
John Bauman19bac1e2014-05-06 15:23:49 -04001563
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001564 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001565 {
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001566 src0.w = Float(0.0f);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001567 sampleTexture(dst, src1, src0, a0, a0, src0, Lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001568 }
1569
Meng-Lin Wu2337a192016-06-01 13:54:07 -04001570 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001571 {
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001572 src0.w = Float(0.0f);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001573 sampleTexture(dst, src1, src0, a0, a0, src2, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001574 }
1575
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001576 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001577 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001578 sampleTexture(dst, src1, src0, a0, a0, offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001579 }
1580
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001581 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001582 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001583 sampleTexture(dst, src1, src0, src0, src0, src0, Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001584 }
1585
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001586 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001587 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001588 sampleTexture(dst, src1, src0, src0, src0, offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001589 }
1590
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001591 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001592 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001593 sampleTexture(dst, src1, src0, src2, src3, src0, Grad);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001594 }
1595
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001596 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001597 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001598 sampleTexture(dst, src1, src0, src2, src3, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001599 }
1600
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001601 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001602 {
Alexis Hetu95ac1872016-06-06 13:26:52 -04001603 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
Nicolas Capensde903442017-11-07 09:20:53 -05001604 SamplerCore::textureSize(texture, dst, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001605 }
1606
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001607 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Vector4f &uvwq, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
John Bauman19bac1e2014-05-06 15:23:49 -04001608 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001609 Vector4f tmp;
1610
John Bauman19bac1e2014-05-06 15:23:49 -04001611 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1612 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001613 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + s.index * sizeof(Texture);
Nicolas Capensde903442017-11-07 09:20:53 -05001614 SamplerCore(constants, state.sampler[s.index]).sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, 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 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001626 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + i * sizeof(Texture);
Nicolas Capensde903442017-11-07 09:20:53 -05001627 SamplerCore(constants, state.sampler[i]).sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001628 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1629 }
1630 }
1631 }
1632 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001633
1634 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];
John Bauman19bac1e2014-05-06 15:23:49 -04001638 }
John Bauman89401822014-05-06 15:04:28 -04001639}