blob: 0f5300590450db5359717748623c9a453d27b148 [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 Capensa0b57832017-11-07 13:07:53 -0500329 case Shader::OPCODE_TEXLDL: TEXLOD(d, s0, src1, s0.w); break;
330 case Shader::OPCODE_TEXLOD: TEXLOD(d, s0, src1, s2.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500331 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400332 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500333 case Shader::OPCODE_TEXLODOFFSET: TEXLODOFFSET(d, s0, src1, s2, s3.x); break;
334 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2.x); break;
335 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCHOFFSET(d, s0, src1, s2, s3.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500336 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500337 case Shader::OPCODE_TEXGRADOFFSET: TEXGRADOFFSET(d, s0, src1, s2, s3, s4); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500338 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
339 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400340 default:
341 ASSERT(false);
342 }
343
John Bauman19bac1e2014-05-06 15:23:49 -0400344 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400345 {
John Bauman19bac1e2014-05-06 15:23:49 -0400346 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400347 {
John Bauman19bac1e2014-05-06 15:23:49 -0400348 switch(opcode)
349 {
350 case Shader::OPCODE_DIV:
351 if(dst.x) d.x = Trunc(d.x);
352 if(dst.y) d.y = Trunc(d.y);
353 if(dst.z) d.z = Trunc(d.z);
354 if(dst.w) d.w = Trunc(d.w);
355 break;
356 default:
357 break; // No truncation to integer required when arguments are integer
358 }
John Bauman89401822014-05-06 15:04:28 -0400359 }
360
John Bauman19bac1e2014-05-06 15:23:49 -0400361 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400362 {
John Bauman19bac1e2014-05-06 15:23:49 -0400363 if(dst.x) d.x = Max(d.x, Float4(0.0f));
364 if(dst.y) d.y = Max(d.y, Float4(0.0f));
365 if(dst.z) d.z = Max(d.z, Float4(0.0f));
366 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400367
John Bauman19bac1e2014-05-06 15:23:49 -0400368 if(dst.x) d.x = Min(d.x, Float4(1.0f));
369 if(dst.y) d.y = Min(d.y, Float4(1.0f));
370 if(dst.z) d.z = Min(d.z, Float4(1.0f));
371 if(dst.w) d.w = Min(d.w, Float4(1.0f));
372 }
373
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400374 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400375 {
376 Vector4f pDst; // FIXME: Rename
377
378 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400379 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500380 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400381 case Shader::PARAMETER_TEMP:
382 if(dst.rel.type == Shader::PARAMETER_VOID)
383 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500384 if(dst.x) pDst.x = r[dst.index].x;
385 if(dst.y) pDst.y = r[dst.index].y;
386 if(dst.z) pDst.z = r[dst.index].z;
387 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400388 }
389 else
390 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500391 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400392
Nicolas Capens7551ac62016-01-20 17:11:53 -0500393 if(dst.x) pDst.x = r[dst.index + a].x;
394 if(dst.y) pDst.y = r[dst.index + a].y;
395 if(dst.z) pDst.z = r[dst.index + a].z;
396 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400397 }
398 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500399 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400400 case Shader::PARAMETER_RASTOUT:
401 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400402 {
403 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500404 if(dst.x) pDst.x = o[Pos].x;
405 if(dst.y) pDst.y = o[Pos].y;
406 if(dst.z) pDst.z = o[Pos].z;
407 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400408 break;
409 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500410 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400411 break;
412 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500413 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400414 break;
415 default:
416 ASSERT(false);
417 }
418 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400419 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400420 if(dst.x) pDst.x = o[C0 + dst.index].x;
421 if(dst.y) pDst.y = o[C0 + dst.index].y;
422 if(dst.z) pDst.z = o[C0 + dst.index].z;
423 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400424 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400425 case Shader::PARAMETER_TEXCRDOUT:
426 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400427 if(version < 0x0300)
428 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500429 if(dst.x) pDst.x = o[T0 + dst.index].x;
430 if(dst.y) pDst.y = o[T0 + dst.index].y;
431 if(dst.z) pDst.z = o[T0 + dst.index].z;
432 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400433 }
434 else
435 {
John Bauman19bac1e2014-05-06 15:23:49 -0400436 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400437 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500438 if(dst.x) pDst.x = o[dst.index].x;
439 if(dst.y) pDst.y = o[dst.index].y;
440 if(dst.z) pDst.z = o[dst.index].z;
441 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400442 }
John Bauman19bac1e2014-05-06 15:23:49 -0400443 else
444 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500445 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400446
Nicolas Capens7551ac62016-01-20 17:11:53 -0500447 if(dst.x) pDst.x = o[dst.index + a].x;
448 if(dst.y) pDst.y = o[dst.index + a].y;
449 if(dst.z) pDst.z = o[dst.index + a].z;
450 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400451 }
452 }
453 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500454 case Shader::PARAMETER_LABEL: break;
455 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
456 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400457 default:
458 ASSERT(false);
459 }
460
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500461 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400462
463 Int4 xEnable = enable;
464 Int4 yEnable = enable;
465 Int4 zEnable = enable;
466 Int4 wEnable = enable;
467
468 if(predicate)
469 {
John Bauman19bac1e2014-05-06 15:23:49 -0400470 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400471
Nicolas Capens7551ac62016-01-20 17:11:53 -0500472 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
473 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
474 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
475 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400476
John Bauman19bac1e2014-05-06 15:23:49 -0400477 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400478 {
John Bauman19bac1e2014-05-06 15:23:49 -0400479 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
480 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
481 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
482 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400483 }
484 else
485 {
John Bauman19bac1e2014-05-06 15:23:49 -0400486 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
487 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
488 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
489 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400490 }
491 }
492
John Bauman19bac1e2014-05-06 15:23:49 -0400493 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
494 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
495 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
496 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400497
John Bauman19bac1e2014-05-06 15:23:49 -0400498 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
499 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
500 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
501 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400502 }
503
John Bauman19bac1e2014-05-06 15:23:49 -0400504 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400505 {
John Bauman19bac1e2014-05-06 15:23:49 -0400506 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400507 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400508 case Shader::PARAMETER_TEMP:
509 if(dst.rel.type == Shader::PARAMETER_VOID)
510 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500511 if(dst.x) r[dst.index].x = d.x;
512 if(dst.y) r[dst.index].y = d.y;
513 if(dst.z) r[dst.index].z = d.z;
514 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400515 }
516 else
517 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500518 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400519
Nicolas Capens7551ac62016-01-20 17:11:53 -0500520 if(dst.x) r[dst.index + a].x = d.x;
521 if(dst.y) r[dst.index + a].y = d.y;
522 if(dst.z) r[dst.index + a].z = d.z;
523 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400524 }
John Bauman89401822014-05-06 15:04:28 -0400525 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400526 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500527 if(dst.x) a0.x = d.x;
528 if(dst.y) a0.y = d.y;
529 if(dst.z) a0.z = d.z;
530 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400531 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400532 case Shader::PARAMETER_RASTOUT:
533 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400534 {
535 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500536 if(dst.x) o[Pos].x = d.x;
537 if(dst.y) o[Pos].y = d.y;
538 if(dst.z) o[Pos].z = d.z;
539 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400540 break;
541 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500542 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400543 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500544 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500545 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400546 break;
547 default: ASSERT(false);
548 }
549 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500550 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400551 if(dst.x) o[C0 + dst.index].x = d.x;
552 if(dst.y) o[C0 + dst.index].y = d.y;
553 if(dst.z) o[C0 + dst.index].z = d.z;
554 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400555 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400556 case Shader::PARAMETER_TEXCRDOUT:
557 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400558 if(version < 0x0300)
559 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500560 if(dst.x) o[T0 + dst.index].x = d.x;
561 if(dst.y) o[T0 + dst.index].y = d.y;
562 if(dst.z) o[T0 + dst.index].z = d.z;
563 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400564 }
565 else
566 {
John Bauman19bac1e2014-05-06 15:23:49 -0400567 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400568 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500569 if(dst.x) o[dst.index].x = d.x;
570 if(dst.y) o[dst.index].y = d.y;
571 if(dst.z) o[dst.index].z = d.z;
572 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400573 }
John Bauman19bac1e2014-05-06 15:23:49 -0400574 else
575 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500576 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400577
Nicolas Capens7551ac62016-01-20 17:11:53 -0500578 if(dst.x) o[dst.index + a].x = d.x;
579 if(dst.y) o[dst.index + a].y = d.y;
580 if(dst.z) o[dst.index + a].z = d.z;
581 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400582 }
583 }
584 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500585 case Shader::PARAMETER_LABEL: break;
586 case Shader::PARAMETER_PREDICATE: p0 = d; break;
587 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400588 default:
589 ASSERT(false);
590 }
591 }
592 }
593
John Bauman19bac1e2014-05-06 15:23:49 -0400594 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400595 {
596 Nucleus::setInsertBlock(returnBlock);
597 }
598 }
599
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500600 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400601 {
John Bauman19bac1e2014-05-06 15:23:49 -0400602 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400603 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400604 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400605 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400606 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400607
608 switch(usage)
609 {
610 case 0xFF:
611 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400612 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500613 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400614 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400615 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500616 o[i].x = v[i].x;
617 o[i].y = v[i].y;
618 o[i].z = v[i].z;
619 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400620 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400621 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500622 o[i].x = v[i].x;
623 o[i].y = v[i].y;
624 o[i].z = v[i].z;
625 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400626 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400627 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500628 o[i].x = v[i].x;
629 o[i].y = v[i].y;
630 o[i].z = v[i].z;
631 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400632 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400633 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500634 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400635 break;
636 default:
637 ASSERT(false);
638 }
639 }
640 }
641 else
642 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500643 o[Pos].x = v[PositionT].x;
644 o[Pos].y = v[PositionT].y;
645 o[Pos].z = v[PositionT].z;
646 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400647
648 for(int i = 0; i < 2; i++)
649 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400650 o[C0 + i].x = v[Color0 + i].x;
651 o[C0 + i].y = v[Color0 + i].y;
652 o[C0 + i].z = v[Color0 + i].z;
653 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400654 }
655
656 for(int i = 0; i < 8; i++)
657 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500658 o[T0 + i].x = v[TexCoord0 + i].x;
659 o[T0 + i].y = v[TexCoord0 + i].y;
660 o[T0 + i].z = v[TexCoord0 + i].z;
661 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400662 }
663
Nicolas Capens7551ac62016-01-20 17:11:53 -0500664 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400665 }
666 }
667
Nicolas Capensc2534f42016-04-04 11:13:24 -0400668 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400669 {
John Bauman19bac1e2014-05-06 15:23:49 -0400670 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500671 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400672
John Bauman89401822014-05-06 15:04:28 -0400673 switch(src.type)
674 {
John Bauman19bac1e2014-05-06 15:23:49 -0400675 case Shader::PARAMETER_TEMP:
676 if(src.rel.type == Shader::PARAMETER_VOID)
677 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500678 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400679 }
680 else
681 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400682 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400683 }
684 break;
685 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500686 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400687 break;
688 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400689 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400690 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500691 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400692 }
693 else
694 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400695 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400696 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400697 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500698 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400699 case Shader::PARAMETER_FLOAT4LITERAL:
700 reg.x = Float4(src.value[0]);
701 reg.y = Float4(src.value[1]);
702 reg.z = Float4(src.value[2]);
703 reg.w = Float4(src.value[3]);
704 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500705 case Shader::PARAMETER_ADDR: reg = a0; break;
706 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
707 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
708 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
709 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400710 case Shader::PARAMETER_SAMPLER:
711 if(src.rel.type == Shader::PARAMETER_VOID)
712 {
713 reg.x = As<Float4>(Int4(i));
714 }
715 else if(src.rel.type == Shader::PARAMETER_TEMP)
716 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500717 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400718 }
719 return reg;
720 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400721 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400722 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500723 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400724 }
725 else
726 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400727 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400728 }
729 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400730 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400731 if(src.index == Shader::InstanceIDIndex)
732 {
733 reg.x = As<Float>(instanceID);
734 }
735 else if(src.index == Shader::VertexIDIndex)
736 {
737 reg.x = As<Float4>(vertexID);
738 }
739 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400740 return reg;
John Bauman89401822014-05-06 15:04:28 -0400741 default:
742 ASSERT(false);
743 }
744
John Bauman66b8ab22014-05-06 15:57:45 -0400745 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
746 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
747 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
748 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400749
John Bauman66b8ab22014-05-06 15:57:45 -0400750 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400751
752 switch(src.modifier)
753 {
John Bauman19bac1e2014-05-06 15:23:49 -0400754 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400755 mod.x = x;
756 mod.y = y;
757 mod.z = z;
758 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400759 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400760 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400761 mod.x = -x;
762 mod.y = -y;
763 mod.z = -z;
764 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400765 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400766 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400767 mod.x = Abs(x);
768 mod.y = Abs(y);
769 mod.z = Abs(z);
770 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400771 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400772 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400773 mod.x = -Abs(x);
774 mod.y = -Abs(y);
775 mod.z = -Abs(z);
776 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400777 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400778 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400779 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
780 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
781 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
782 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400783 break;
784 default:
785 ASSERT(false);
786 }
787
788 return mod;
789 }
790
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400791 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
792 {
793 if(bufferIndex == -1)
794 {
795 return data + OFFSET(DrawData, vs.c[index]);
796 }
797 else
798 {
799 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
800 }
801 }
802
803 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
804 {
805 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
806 }
807
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500808 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400809 {
John Bauman19bac1e2014-05-06 15:23:49 -0400810 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500811 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400812
813 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
814 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400815 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400816
817 c.x = c.x.xxxx;
818 c.y = c.y.yyyy;
819 c.z = c.z.zzzz;
820 c.w = c.w.wwww;
821
Nicolas Capenseafdb222015-05-15 15:24:08 -0400822 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400823 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500824 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400825 {
826 const Shader::Instruction &instruction = *shader->getInstruction(j);
827
828 if(instruction.opcode == Shader::OPCODE_DEF)
829 {
830 if(instruction.dst.index == i)
831 {
832 c.x = Float4(instruction.src[0].value[0]);
833 c.y = Float4(instruction.src[0].value[1]);
834 c.z = Float4(instruction.src[0].value[2]);
835 c.w = Float4(instruction.src[0].value[3]);
836
837 break;
838 }
839 }
840 }
841 }
842 }
843 else if(src.rel.type == Shader::PARAMETER_LOOP)
844 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500845 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400846
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400847 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400848
849 c.x = c.x.xxxx;
850 c.y = c.y.yyyy;
851 c.z = c.z.zzzz;
852 c.w = c.w.wwww;
853 }
854 else
855 {
856 if(src.rel.deterministic)
857 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400858 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400859
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400860 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500861
John Bauman19bac1e2014-05-06 15:23:49 -0400862 c.x = c.x.xxxx;
863 c.y = c.y.yyyy;
864 c.z = c.z.zzzz;
865 c.w = c.w.wwww;
866 }
867 else
868 {
869 int component = src.rel.swizzle & 0x03;
870 Float4 a;
871
872 switch(src.rel.type)
873 {
Nicolas Capens32980ac2016-08-15 15:33:58 -0400874 case Shader::PARAMETER_ADDR: a = a0[component]; break;
875 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
876 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
877 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
878 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400879 case Shader::PARAMETER_MISCTYPE:
880 if(src.rel.index == Shader::InstanceIDIndex)
881 {
882 a = As<Float4>(Int4(instanceID)); break;
883 }
884 else if(src.rel.index == Shader::VertexIDIndex)
885 {
886 a = As<Float4>(vertexID); break;
887 }
888 else ASSERT(false);
889 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400890 default: ASSERT(false);
891 }
892
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400893 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400894
Alexis Hetu028f41b2016-01-13 14:40:47 -0500895 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 -0500896
John Bauman19bac1e2014-05-06 15:23:49 -0400897 Int index0 = Extract(index, 0);
898 Int index1 = Extract(index, 1);
899 Int index2 = Extract(index, 2);
900 Int index3 = Extract(index, 3);
901
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400902 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
903 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
904 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
905 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400906
907 transpose4x4(c.x, c.y, c.z, c.w);
908 }
909 }
910
911 return c;
912 }
913
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400914 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400915 {
916 ASSERT(var.rel.deterministic);
917
918 if(var.rel.type == Shader::PARAMETER_TEMP)
919 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500920 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400921 }
922 else if(var.rel.type == Shader::PARAMETER_INPUT)
923 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500924 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400925 }
926 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
927 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500928 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400929 }
930 else if(var.rel.type == Shader::PARAMETER_CONST)
931 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500932 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400933 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500934 else if(var.rel.type == Shader::PARAMETER_LOOP)
935 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500936 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500937 }
John Bauman19bac1e2014-05-06 15:23:49 -0400938 else ASSERT(false);
939
940 return 0;
941 }
942
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500943 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400944 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500945 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500946
John Baumand4ae8632014-05-06 16:18:33 -0400947 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400948 {
John Baumand4ae8632014-05-06 16:18:33 -0400949 if(shader->containsBreakInstruction() && instruction->analysisBreak)
950 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500951 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400952 }
John Bauman19bac1e2014-05-06 15:23:49 -0400953
John Baumand4ae8632014-05-06 16:18:33 -0400954 if(shader->containsContinueInstruction() && instruction->analysisContinue)
955 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500956 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400957 }
John Bauman19bac1e2014-05-06 15:23:49 -0400958
John Baumand4ae8632014-05-06 16:18:33 -0400959 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
960 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500961 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400962 }
John Bauman19bac1e2014-05-06 15:23:49 -0400963 }
964
965 return enable;
966 }
967
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500968 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400969 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400970 Vector4f row0 = fetchRegister(src1, 0);
971 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400972
973 dst.x = dot3(src0, row0);
974 dst.y = dot3(src0, row1);
975 }
976
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500977 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400978 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400979 Vector4f row0 = fetchRegister(src1, 0);
980 Vector4f row1 = fetchRegister(src1, 1);
981 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400982
983 dst.x = dot3(src0, row0);
984 dst.y = dot3(src0, row1);
985 dst.z = dot3(src0, row2);
986 }
987
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500988 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400989 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400990 Vector4f row0 = fetchRegister(src1, 0);
991 Vector4f row1 = fetchRegister(src1, 1);
992 Vector4f row2 = fetchRegister(src1, 2);
993 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400994
995 dst.x = dot3(src0, row0);
996 dst.y = dot3(src0, row1);
997 dst.z = dot3(src0, row2);
998 dst.w = dot3(src0, row3);
999 }
1000
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001001 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001002 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001003 Vector4f row0 = fetchRegister(src1, 0);
1004 Vector4f row1 = fetchRegister(src1, 1);
1005 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001006
1007 dst.x = dot4(src0, row0);
1008 dst.y = dot4(src0, row1);
1009 dst.z = dot4(src0, row2);
1010 }
1011
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001012 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001013 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001014 Vector4f row0 = fetchRegister(src1, 0);
1015 Vector4f row1 = fetchRegister(src1, 1);
1016 Vector4f row2 = fetchRegister(src1, 2);
1017 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001018
1019 dst.x = dot4(src0, row0);
1020 dst.y = dot4(src0, row1);
1021 dst.z = dot4(src0, row2);
1022 dst.w = dot4(src0, row3);
1023 }
1024
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001025 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001026 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001027 BasicBlock *deadBlock = Nucleus::createBasicBlock();
1028 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001029
1030 if(breakDepth == 0)
1031 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001032 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001033 Nucleus::createBr(endBlock);
1034 }
1035 else
1036 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001037 enableBreak = enableBreak & ~enableStack[enableIndex];
1038 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001039
Nicolas Capens7551ac62016-01-20 17:11:53 -05001040 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001041 branch(allBreak, endBlock, deadBlock);
1042 }
1043
1044 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001045 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001046 }
1047
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001048 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001049 {
1050 Int4 condition;
1051
1052 switch(control)
1053 {
John Bauman19bac1e2014-05-06 15:23:49 -04001054 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1055 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1056 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1057 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1058 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1059 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001060 default:
1061 ASSERT(false);
1062 }
1063
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001064 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001065 }
1066
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001067 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001068 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001069 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001070
John Bauman19bac1e2014-05-06 15:23:49 -04001071 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001072 {
1073 condition = ~condition;
1074 }
1075
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001076 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001077 }
1078
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001079 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001080 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001081 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001082
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001083 BasicBlock *continueBlock = Nucleus::createBasicBlock();
1084 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001085
Nicolas Capens7551ac62016-01-20 17:11:53 -05001086 enableBreak = enableBreak & ~condition;
1087 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001088
Nicolas Capens7551ac62016-01-20 17:11:53 -05001089 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001090 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001091
John Bauman89401822014-05-06 15:04:28 -04001092 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001093 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001094 }
1095
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001096 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001097 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001098 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001099 }
1100
1101 void VertexProgram::TEST()
1102 {
1103 whileTest = true;
1104 }
1105
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001106 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001107 {
1108 if(!labelBlock[labelIndex])
1109 {
1110 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1111 }
1112
John Bauman19bac1e2014-05-06 15:23:49 -04001113 if(callRetBlock[labelIndex].size() > 1)
1114 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001115 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001116 }
John Bauman89401822014-05-06 15:04:28 -04001117
Nicolas Capens7551ac62016-01-20 17:11:53 -05001118 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001119
1120 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001121 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1122
Nicolas Capens7551ac62016-01-20 17:11:53 -05001123 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001124 }
1125
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001126 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001127 {
John Bauman19bac1e2014-05-06 15:23:49 -04001128 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001129 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001130 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001131 }
John Bauman19bac1e2014-05-06 15:23:49 -04001132 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001133 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001134 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001135 }
1136 else ASSERT(false);
1137 }
1138
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001139 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001140 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001141 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001142
John Bauman19bac1e2014-05-06 15:23:49 -04001143 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001144 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001145 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001146 }
1147
1148 if(!labelBlock[labelIndex])
1149 {
1150 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1151 }
1152
John Bauman19bac1e2014-05-06 15:23:49 -04001153 if(callRetBlock[labelIndex].size() > 1)
1154 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001155 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001156 }
John Bauman89401822014-05-06 15:04:28 -04001157
Nicolas Capens7551ac62016-01-20 17:11:53 -05001158 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001159
John Bauman19bac1e2014-05-06 15:23:49 -04001160 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1161 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1162
Nicolas Capens7551ac62016-01-20 17:11:53 -05001163 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001164 }
1165
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001166 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001167 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001168 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001169
John Bauman19bac1e2014-05-06 15:23:49 -04001170 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001171 {
1172 condition = ~condition;
1173 }
1174
Nicolas Capens7551ac62016-01-20 17:11:53 -05001175 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001176
1177 if(!labelBlock[labelIndex])
1178 {
1179 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1180 }
1181
John Bauman19bac1e2014-05-06 15:23:49 -04001182 if(callRetBlock[labelIndex].size() > 1)
1183 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001184 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001185 }
John Bauman89401822014-05-06 15:04:28 -04001186
Nicolas Capens7551ac62016-01-20 17:11:53 -05001187 enableIndex++;
1188 enableStack[enableIndex] = condition;
1189 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001190
John Bauman19bac1e2014-05-06 15:23:49 -04001191 Bool notAllFalse = SignMask(condition) != 0;
1192 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1193 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001194
Nicolas Capens7551ac62016-01-20 17:11:53 -05001195 enableIndex--;
1196 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001197 }
1198
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001199 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001200 {
1201 ifDepth--;
1202
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001203 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1204 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001205
1206 if(isConditionalIf[ifDepth])
1207 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001208 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001209 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001210
1211 branch(notAllFalse, falseBlock, endBlock);
1212
Nicolas Capens7551ac62016-01-20 17:11:53 -05001213 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001214 }
1215 else
1216 {
1217 Nucleus::createBr(endBlock);
1218 Nucleus::setInsertBlock(falseBlock);
1219 }
1220
1221 ifFalseBlock[ifDepth] = endBlock;
1222
1223 ifDepth++;
1224 }
1225
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001226 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001227 {
1228 ifDepth--;
1229
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001230 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001231
1232 Nucleus::createBr(endBlock);
1233 Nucleus::setInsertBlock(endBlock);
1234
1235 if(isConditionalIf[ifDepth])
1236 {
1237 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001238 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001239 }
1240 }
1241
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001242 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001243 {
1244 loopRepDepth--;
1245
Nicolas Capens7551ac62016-01-20 17:11:53 -05001246 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001247
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001248 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1249 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001250
1251 Nucleus::createBr(testBlock);
1252 Nucleus::setInsertBlock(endBlock);
1253
Nicolas Capens7551ac62016-01-20 17:11:53 -05001254 loopDepth--;
1255 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001256 }
1257
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001258 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001259 {
1260 loopRepDepth--;
1261
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001262 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1263 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001264
1265 Nucleus::createBr(testBlock);
1266 Nucleus::setInsertBlock(endBlock);
1267
Nicolas Capens7551ac62016-01-20 17:11:53 -05001268 loopDepth--;
1269 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001270 }
1271
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001272 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001273 {
1274 loopRepDepth--;
1275
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001276 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1277 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001278
1279 Nucleus::createBr(testBlock);
1280 Nucleus::setInsertBlock(endBlock);
1281
Nicolas Capens7551ac62016-01-20 17:11:53 -05001282 enableIndex--;
1283 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001284 whileTest = false;
1285 }
1286
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001287 void VertexProgram::ENDSWITCH()
1288 {
1289 loopRepDepth--;
1290
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001291 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001292
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001293 Nucleus::createBr(loopRepEndBlock[loopRepDepth]);
1294 Nucleus::setInsertBlock(endBlock);
1295
1296 enableIndex--;
1297 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1298 }
1299
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001300 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001301 {
John Bauman19bac1e2014-05-06 15:23:49 -04001302 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001303 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001304 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001305 }
John Bauman19bac1e2014-05-06 15:23:49 -04001306 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001307 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001308 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001309 }
John Bauman19bac1e2014-05-06 15:23:49 -04001310 else
1311 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001312 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001313 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001314 }
John Bauman89401822014-05-06 15:04:28 -04001315 }
1316
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001317 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001318 {
1319 ASSERT(ifDepth < 24 + 4);
1320
Nicolas Capens7551ac62016-01-20 17:11:53 -05001321 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001322
John Bauman19bac1e2014-05-06 15:23:49 -04001323 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001324 {
John Bauman19bac1e2014-05-06 15:23:49 -04001325 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001326 }
1327
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001328 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1329 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001330
1331 branch(condition, trueBlock, falseBlock);
1332
1333 isConditionalIf[ifDepth] = false;
1334 ifFalseBlock[ifDepth] = falseBlock;
1335
1336 ifDepth++;
1337 }
1338
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001339 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001340 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001341 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001342
John Bauman19bac1e2014-05-06 15:23:49 -04001343 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001344 {
1345 condition = ~condition;
1346 }
1347
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001348 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001349 }
1350
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001351 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001352 {
1353 Int4 condition;
1354
1355 switch(control)
1356 {
John Bauman19bac1e2014-05-06 15:23:49 -04001357 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1358 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1359 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1360 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1361 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1362 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001363 default:
1364 ASSERT(false);
1365 }
1366
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001367 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001368 }
1369
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001370 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001371 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001372 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001373
Nicolas Capens7551ac62016-01-20 17:11:53 -05001374 enableIndex++;
1375 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001376
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001377 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1378 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001379
John Bauman19bac1e2014-05-06 15:23:49 -04001380 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001381
1382 branch(notAllFalse, trueBlock, falseBlock);
1383
1384 isConditionalIf[ifDepth] = true;
1385 ifFalseBlock[ifDepth] = falseBlock;
1386
1387 ifDepth++;
1388 breakDepth++;
1389 }
1390
1391 void VertexProgram::LABEL(int labelIndex)
1392 {
John Bauman19bac1e2014-05-06 15:23:49 -04001393 if(!labelBlock[labelIndex])
1394 {
1395 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1396 }
1397
John Bauman89401822014-05-06 15:04:28 -04001398 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001399 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001400 }
1401
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001402 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001403 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001404 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001405
Nicolas Capens7551ac62016-01-20 17:11:53 -05001406 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1407 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1408 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001409
1410 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001411 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001412 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001413 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001414 }
1415
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001416 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1417 BasicBlock *testBlock = Nucleus::createBasicBlock();
1418 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001419
1420 loopRepTestBlock[loopRepDepth] = testBlock;
1421 loopRepEndBlock[loopRepDepth] = endBlock;
1422
1423 // FIXME: jump(testBlock)
1424 Nucleus::createBr(testBlock);
1425 Nucleus::setInsertBlock(testBlock);
1426
Nicolas Capens7551ac62016-01-20 17:11:53 -05001427 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001428 Nucleus::setInsertBlock(loopBlock);
1429
Nicolas Capens7551ac62016-01-20 17:11:53 -05001430 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001431
John Bauman89401822014-05-06 15:04:28 -04001432 loopRepDepth++;
1433 breakDepth = 0;
1434 }
1435
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001436 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001437 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001438 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001439
Nicolas Capens7551ac62016-01-20 17:11:53 -05001440 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1441 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001442
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001443 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1444 BasicBlock *testBlock = Nucleus::createBasicBlock();
1445 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001446
1447 loopRepTestBlock[loopRepDepth] = testBlock;
1448 loopRepEndBlock[loopRepDepth] = endBlock;
1449
1450 // FIXME: jump(testBlock)
1451 Nucleus::createBr(testBlock);
1452 Nucleus::setInsertBlock(testBlock);
1453
Nicolas Capens7551ac62016-01-20 17:11:53 -05001454 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001455 Nucleus::setInsertBlock(loopBlock);
1456
Nicolas Capens7551ac62016-01-20 17:11:53 -05001457 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001458
1459 loopRepDepth++;
1460 breakDepth = 0;
1461 }
1462
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001463 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001464 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001465 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001466
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001467 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1468 BasicBlock *testBlock = Nucleus::createBasicBlock();
1469 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001470
John Bauman19bac1e2014-05-06 15:23:49 -04001471 loopRepTestBlock[loopRepDepth] = testBlock;
1472 loopRepEndBlock[loopRepDepth] = endBlock;
1473
Nicolas Capens7551ac62016-01-20 17:11:53 -05001474 Int4 restoreBreak = enableBreak;
1475 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001476
1477 // FIXME: jump(testBlock)
1478 Nucleus::createBr(testBlock);
1479 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001480 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001481
Nicolas Capensc2534f42016-04-04 11:13:24 -04001482 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001483 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001484 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001485 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001486 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001487
1488 Bool notAllFalse = SignMask(condition) != 0;
1489 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001490
John Bauman19bac1e2014-05-06 15:23:49 -04001491 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001492 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001493
John Bauman19bac1e2014-05-06 15:23:49 -04001494 Nucleus::setInsertBlock(loopBlock);
1495
1496 loopRepDepth++;
1497 breakDepth = 0;
1498 }
1499
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001500 void VertexProgram::SWITCH()
1501 {
1502 enableIndex++;
1503 enableStack[enableIndex] = Int4(0xFFFFFFFF);
1504
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001505 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001506
1507 loopRepTestBlock[loopRepDepth] = nullptr;
1508 loopRepEndBlock[loopRepDepth] = endBlock;
1509
1510 loopRepDepth++;
1511 breakDepth = 0;
1512 }
1513
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001514 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001515 {
John Bauman19bac1e2014-05-06 15:23:49 -04001516 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001517 {
1518 returnBlock = Nucleus::createBasicBlock();
1519 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001520 }
1521 else
1522 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001523 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001524
John Bauman19bac1e2014-05-06 15:23:49 -04001525 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001526 {
John Bauman19bac1e2014-05-06 15:23:49 -04001527 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001528 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001529
Nicolas Capens19336542016-09-26 10:32:29 -04001530 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001531 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001532
1533 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1534 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001535 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001536 }
1537 }
1538 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1539 {
1540 Nucleus::createBr(callRetBlock[currentLabel][0]);
1541 }
1542 else // Function isn't called
1543 {
1544 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001545 }
1546
1547 Nucleus::setInsertBlock(unreachableBlock);
1548 Nucleus::createUnreachable();
1549 }
1550 }
1551
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001552 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001553 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001554 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001555
John Bauman19bac1e2014-05-06 15:23:49 -04001556 // FIXME: Return from function if all instances left
1557 // FIXME: Use enableLeave in other control-flow constructs
1558 }
John Bauman89401822014-05-06 15:04:28 -04001559
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001560 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001561 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001562 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001563 }
1564
Nicolas Capensa0b57832017-11-07 13:07:53 -05001565 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001566 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001567 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001568 }
1569
Nicolas Capensa0b57832017-11-07 13:07:53 -05001570 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001571 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001572 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001573 }
1574
Nicolas Capensa0b57832017-11-07 13:07:53 -05001575 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001576 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001577 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001578 }
1579
Nicolas Capensa0b57832017-11-07 13:07:53 -05001580 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001581 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001582 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001583 }
1584
Nicolas Capensa0b57832017-11-07 13:07:53 -05001585 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001586 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001587 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001588 }
1589
Nicolas Capensa0b57832017-11-07 13:07:53 -05001590 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001591 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001592 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1593 }
1594
1595 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1596 {
1597 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001598 }
1599
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001600 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001601 {
Alexis Hetu95ac1872016-06-06 13:26:52 -04001602 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
Nicolas Capens89a218b2017-11-07 13:05:20 -05001603 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001604 }
1605
Nicolas Capensa0b57832017-11-07 13:07:53 -05001606 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 -04001607 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001608 Vector4f tmp;
1609
John Bauman19bac1e2014-05-06 15:23:49 -04001610 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1611 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001612 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001613 }
1614 else
1615 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001616 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001617
Nicolas Capensc2534f42016-04-04 11:13:24 -04001618 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001619 {
1620 if(shader->usesSampler(i))
1621 {
1622 If(index == i)
1623 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001624 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001625 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1626 }
1627 }
1628 }
1629 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001630
Nicolas Capens89a218b2017-11-07 13:05:20 -05001631 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001632 c.x = tmp[(s.swizzle >> 0) & 0x3];
1633 c.y = tmp[(s.swizzle >> 2) & 0x3];
1634 c.z = tmp[(s.swizzle >> 4) & 0x3];
1635 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001636
1637 return c;
1638 }
1639
Nicolas Capensa0b57832017-11-07 13:07:53 -05001640 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001641 {
1642 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001643 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 -04001644 }
John Bauman89401822014-05-06 15:04:28 -04001645}