blob: 53f36921b9b3475f80dc98398733a52b4738d3f9 [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
Alexis Hetu53ad4af2017-12-06 14:49:07 -050079 unsigned short shaderModel = shader->getShaderModel();
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;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500195 case Shader::OPCODE_EXPP: expp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500196 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;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500206 case Shader::OPCODE_LOGP: logp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500207 case Shader::OPCODE_LOG: log(d, s0, pp); break;
208 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
209 case Shader::OPCODE_STEP: step(d, s0, s1); break;
210 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8ef6d102017-11-09 15:49:09 -0500211 case Shader::OPCODE_ISINF: isinf(d, s0); break;
212 case Shader::OPCODE_ISNAN: isnan(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400213 case Shader::OPCODE_FLOATBITSTOINT:
214 case Shader::OPCODE_FLOATBITSTOUINT:
215 case Shader::OPCODE_INTBITSTOFLOAT:
216 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400217 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
218 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400219 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400220 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
221 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400222 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500223 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
224 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
225 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
226 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
227 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
228 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
229 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
230 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400231 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
232 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500233 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400234 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
235 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500236 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400237 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400238 case Shader::OPCODE_NEG: neg(d, s0); break;
239 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500240 case Shader::OPCODE_F2B: f2b(d, s0); break;
241 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400242 case Shader::OPCODE_F2I: f2i(d, s0); break;
243 case Shader::OPCODE_I2F: i2f(d, s0); break;
244 case Shader::OPCODE_F2U: f2u(d, s0); break;
245 case Shader::OPCODE_U2F: u2f(d, s0); break;
246 case Shader::OPCODE_I2B: i2b(d, s0); break;
247 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500248 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400249 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500250 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
251 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
252 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
253 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
254 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
255 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
256 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400257 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
258 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500259 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400260 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
261 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
262 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500263 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
264 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
265 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
266 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
267 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
268 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
269 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
270 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
271 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
272 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
273 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
274 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
275 case Shader::OPCODE_SGE: step(d, s1, s0); break;
276 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400277 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500278 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
279 case Shader::OPCODE_COS: cos(d, s0, pp); break;
280 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
281 case Shader::OPCODE_TAN: tan(d, s0); break;
282 case Shader::OPCODE_ACOS: acos(d, s0); break;
283 case Shader::OPCODE_ASIN: asin(d, s0); break;
284 case Shader::OPCODE_ATAN: atan(d, s0); break;
285 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
286 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
287 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
288 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
289 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
290 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
291 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
292 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
293 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400294 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500295 case Shader::OPCODE_BREAK: BREAK(); break;
296 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
297 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
298 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
299 case Shader::OPCODE_TEST: TEST(); break;
300 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
301 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
302 case Shader::OPCODE_ELSE: ELSE(); break;
303 case Shader::OPCODE_ENDIF: ENDIF(); break;
304 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
305 case Shader::OPCODE_ENDREP: ENDREP(); break;
306 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400307 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500308 case Shader::OPCODE_IF: IF(src0); break;
309 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
310 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
311 case Shader::OPCODE_LOOP: LOOP(src1); break;
312 case Shader::OPCODE_REP: REP(src0); break;
313 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400314 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500315 case Shader::OPCODE_RET: RET(); break;
316 case Shader::OPCODE_LEAVE: LEAVE(); break;
317 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
318 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400319 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500320 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
321 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
322 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
323 case Shader::OPCODE_ALL: all(d.x, s0); break;
324 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400325 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
326 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
327 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
328 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400329 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
330 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500331 case Shader::OPCODE_TEXLDL: TEXLOD(d, s0, src1, s0.w); break;
332 case Shader::OPCODE_TEXLOD: TEXLOD(d, s0, src1, s2.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500333 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400334 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500335 case Shader::OPCODE_TEXLODOFFSET: TEXLODOFFSET(d, s0, src1, s2, s3.x); break;
336 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2.x); break;
337 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCHOFFSET(d, s0, src1, s2, s3.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500338 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500339 case Shader::OPCODE_TEXGRADOFFSET: TEXGRADOFFSET(d, s0, src1, s2, s3, s4); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500340 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
341 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400342 default:
343 ASSERT(false);
344 }
345
John Bauman19bac1e2014-05-06 15:23:49 -0400346 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400347 {
John Bauman19bac1e2014-05-06 15:23:49 -0400348 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400349 {
John Bauman19bac1e2014-05-06 15:23:49 -0400350 if(dst.x) d.x = Max(d.x, Float4(0.0f));
351 if(dst.y) d.y = Max(d.y, Float4(0.0f));
352 if(dst.z) d.z = Max(d.z, Float4(0.0f));
353 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400354
John Bauman19bac1e2014-05-06 15:23:49 -0400355 if(dst.x) d.x = Min(d.x, Float4(1.0f));
356 if(dst.y) d.y = Min(d.y, Float4(1.0f));
357 if(dst.z) d.z = Min(d.z, Float4(1.0f));
358 if(dst.w) d.w = Min(d.w, Float4(1.0f));
359 }
360
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400361 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400362 {
363 Vector4f pDst; // FIXME: Rename
364
365 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400366 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500367 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400368 case Shader::PARAMETER_TEMP:
369 if(dst.rel.type == Shader::PARAMETER_VOID)
370 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500371 if(dst.x) pDst.x = r[dst.index].x;
372 if(dst.y) pDst.y = r[dst.index].y;
373 if(dst.z) pDst.z = r[dst.index].z;
374 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400375 }
376 else
377 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500378 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400379
Nicolas Capens7551ac62016-01-20 17:11:53 -0500380 if(dst.x) pDst.x = r[dst.index + a].x;
381 if(dst.y) pDst.y = r[dst.index + a].y;
382 if(dst.z) pDst.z = r[dst.index + a].z;
383 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400384 }
385 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500386 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400387 case Shader::PARAMETER_RASTOUT:
388 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400389 {
390 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500391 if(dst.x) pDst.x = o[Pos].x;
392 if(dst.y) pDst.y = o[Pos].y;
393 if(dst.z) pDst.z = o[Pos].z;
394 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400395 break;
396 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500397 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400398 break;
399 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500400 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400401 break;
402 default:
403 ASSERT(false);
404 }
405 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400406 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400407 if(dst.x) pDst.x = o[C0 + dst.index].x;
408 if(dst.y) pDst.y = o[C0 + dst.index].y;
409 if(dst.z) pDst.z = o[C0 + dst.index].z;
410 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400411 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400412 case Shader::PARAMETER_TEXCRDOUT:
413 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500414 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400415 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500416 if(dst.x) pDst.x = o[T0 + dst.index].x;
417 if(dst.y) pDst.y = o[T0 + dst.index].y;
418 if(dst.z) pDst.z = o[T0 + dst.index].z;
419 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400420 }
421 else
422 {
John Bauman19bac1e2014-05-06 15:23:49 -0400423 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400424 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500425 if(dst.x) pDst.x = o[dst.index].x;
426 if(dst.y) pDst.y = o[dst.index].y;
427 if(dst.z) pDst.z = o[dst.index].z;
428 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400429 }
John Bauman19bac1e2014-05-06 15:23:49 -0400430 else
431 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500432 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400433
Nicolas Capens7551ac62016-01-20 17:11:53 -0500434 if(dst.x) pDst.x = o[dst.index + a].x;
435 if(dst.y) pDst.y = o[dst.index + a].y;
436 if(dst.z) pDst.z = o[dst.index + a].z;
437 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400438 }
439 }
440 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500441 case Shader::PARAMETER_LABEL: break;
442 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
443 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400444 default:
445 ASSERT(false);
446 }
447
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500448 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400449
450 Int4 xEnable = enable;
451 Int4 yEnable = enable;
452 Int4 zEnable = enable;
453 Int4 wEnable = enable;
454
455 if(predicate)
456 {
John Bauman19bac1e2014-05-06 15:23:49 -0400457 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400458
Nicolas Capens7551ac62016-01-20 17:11:53 -0500459 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
460 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
461 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
462 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400463
John Bauman19bac1e2014-05-06 15:23:49 -0400464 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400465 {
John Bauman19bac1e2014-05-06 15:23:49 -0400466 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
467 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
468 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
469 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400470 }
471 else
472 {
John Bauman19bac1e2014-05-06 15:23:49 -0400473 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
474 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
475 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
476 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400477 }
478 }
479
John Bauman19bac1e2014-05-06 15:23:49 -0400480 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
481 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
482 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
483 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400484
John Bauman19bac1e2014-05-06 15:23:49 -0400485 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
486 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
487 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
488 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400489 }
490
John Bauman19bac1e2014-05-06 15:23:49 -0400491 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400492 {
John Bauman19bac1e2014-05-06 15:23:49 -0400493 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400494 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400495 case Shader::PARAMETER_TEMP:
496 if(dst.rel.type == Shader::PARAMETER_VOID)
497 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500498 if(dst.x) r[dst.index].x = d.x;
499 if(dst.y) r[dst.index].y = d.y;
500 if(dst.z) r[dst.index].z = d.z;
501 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400502 }
503 else
504 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500505 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400506
Nicolas Capens7551ac62016-01-20 17:11:53 -0500507 if(dst.x) r[dst.index + a].x = d.x;
508 if(dst.y) r[dst.index + a].y = d.y;
509 if(dst.z) r[dst.index + a].z = d.z;
510 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400511 }
John Bauman89401822014-05-06 15:04:28 -0400512 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400513 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500514 if(dst.x) a0.x = d.x;
515 if(dst.y) a0.y = d.y;
516 if(dst.z) a0.z = d.z;
517 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400518 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400519 case Shader::PARAMETER_RASTOUT:
520 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400521 {
522 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500523 if(dst.x) o[Pos].x = d.x;
524 if(dst.y) o[Pos].y = d.y;
525 if(dst.z) o[Pos].z = d.z;
526 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400527 break;
528 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500529 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400530 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500531 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500532 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400533 break;
534 default: ASSERT(false);
535 }
536 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500537 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400538 if(dst.x) o[C0 + dst.index].x = d.x;
539 if(dst.y) o[C0 + dst.index].y = d.y;
540 if(dst.z) o[C0 + dst.index].z = d.z;
541 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400542 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400543 case Shader::PARAMETER_TEXCRDOUT:
544 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500545 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400546 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500547 if(dst.x) o[T0 + dst.index].x = d.x;
548 if(dst.y) o[T0 + dst.index].y = d.y;
549 if(dst.z) o[T0 + dst.index].z = d.z;
550 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400551 }
552 else
553 {
John Bauman19bac1e2014-05-06 15:23:49 -0400554 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400555 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500556 if(dst.x) o[dst.index].x = d.x;
557 if(dst.y) o[dst.index].y = d.y;
558 if(dst.z) o[dst.index].z = d.z;
559 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400560 }
John Bauman19bac1e2014-05-06 15:23:49 -0400561 else
562 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500563 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400564
Nicolas Capens7551ac62016-01-20 17:11:53 -0500565 if(dst.x) o[dst.index + a].x = d.x;
566 if(dst.y) o[dst.index + a].y = d.y;
567 if(dst.z) o[dst.index + a].z = d.z;
568 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400569 }
570 }
571 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500572 case Shader::PARAMETER_LABEL: break;
573 case Shader::PARAMETER_PREDICATE: p0 = d; break;
574 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400575 default:
576 ASSERT(false);
577 }
578 }
579 }
580
John Bauman19bac1e2014-05-06 15:23:49 -0400581 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400582 {
583 Nucleus::setInsertBlock(returnBlock);
584 }
585 }
586
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500587 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400588 {
John Bauman19bac1e2014-05-06 15:23:49 -0400589 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400590 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400591 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400592 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400593 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400594
595 switch(usage)
596 {
597 case 0xFF:
598 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400599 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500600 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400601 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400602 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500603 o[i].x = v[i].x;
604 o[i].y = v[i].y;
605 o[i].z = v[i].z;
606 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400607 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400608 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500609 o[i].x = v[i].x;
610 o[i].y = v[i].y;
611 o[i].z = v[i].z;
612 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400613 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400614 case Shader::USAGE_COLOR:
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_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500621 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400622 break;
623 default:
624 ASSERT(false);
625 }
626 }
627 }
628 else
629 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500630 o[Pos].x = v[PositionT].x;
631 o[Pos].y = v[PositionT].y;
632 o[Pos].z = v[PositionT].z;
633 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400634
635 for(int i = 0; i < 2; i++)
636 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400637 o[C0 + i].x = v[Color0 + i].x;
638 o[C0 + i].y = v[Color0 + i].y;
639 o[C0 + i].z = v[Color0 + i].z;
640 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400641 }
642
643 for(int i = 0; i < 8; i++)
644 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500645 o[T0 + i].x = v[TexCoord0 + i].x;
646 o[T0 + i].y = v[TexCoord0 + i].y;
647 o[T0 + i].z = v[TexCoord0 + i].z;
648 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400649 }
650
Nicolas Capens7551ac62016-01-20 17:11:53 -0500651 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400652 }
653 }
654
Nicolas Capensc2534f42016-04-04 11:13:24 -0400655 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400656 {
John Bauman19bac1e2014-05-06 15:23:49 -0400657 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500658 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400659
John Bauman89401822014-05-06 15:04:28 -0400660 switch(src.type)
661 {
John Bauman19bac1e2014-05-06 15:23:49 -0400662 case Shader::PARAMETER_TEMP:
663 if(src.rel.type == Shader::PARAMETER_VOID)
664 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500665 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400666 }
667 else
668 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400669 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400670 }
671 break;
672 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500673 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400674 break;
675 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400676 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400677 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500678 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400679 }
680 else
681 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400682 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400683 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400684 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500685 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400686 case Shader::PARAMETER_FLOAT4LITERAL:
687 reg.x = Float4(src.value[0]);
688 reg.y = Float4(src.value[1]);
689 reg.z = Float4(src.value[2]);
690 reg.w = Float4(src.value[3]);
691 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500692 case Shader::PARAMETER_ADDR: reg = a0; break;
693 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
694 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
695 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
696 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400697 case Shader::PARAMETER_SAMPLER:
698 if(src.rel.type == Shader::PARAMETER_VOID)
699 {
700 reg.x = As<Float4>(Int4(i));
701 }
702 else if(src.rel.type == Shader::PARAMETER_TEMP)
703 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500704 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400705 }
706 return reg;
707 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400708 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400709 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500710 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400711 }
712 else
713 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400714 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400715 }
716 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400717 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400718 if(src.index == Shader::InstanceIDIndex)
719 {
720 reg.x = As<Float>(instanceID);
721 }
722 else if(src.index == Shader::VertexIDIndex)
723 {
724 reg.x = As<Float4>(vertexID);
725 }
726 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400727 return reg;
John Bauman89401822014-05-06 15:04:28 -0400728 default:
729 ASSERT(false);
730 }
731
John Bauman66b8ab22014-05-06 15:57:45 -0400732 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
733 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
734 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
735 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400736
John Bauman66b8ab22014-05-06 15:57:45 -0400737 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400738
739 switch(src.modifier)
740 {
John Bauman19bac1e2014-05-06 15:23:49 -0400741 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400742 mod.x = x;
743 mod.y = y;
744 mod.z = z;
745 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400746 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400747 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400748 mod.x = -x;
749 mod.y = -y;
750 mod.z = -z;
751 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400752 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400753 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400754 mod.x = Abs(x);
755 mod.y = Abs(y);
756 mod.z = Abs(z);
757 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400758 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400759 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400760 mod.x = -Abs(x);
761 mod.y = -Abs(y);
762 mod.z = -Abs(z);
763 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400764 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400765 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400766 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
767 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
768 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
769 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400770 break;
771 default:
772 ASSERT(false);
773 }
774
775 return mod;
776 }
777
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400778 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
779 {
780 if(bufferIndex == -1)
781 {
782 return data + OFFSET(DrawData, vs.c[index]);
783 }
784 else
785 {
786 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
787 }
788 }
789
790 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
791 {
792 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
793 }
794
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500795 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400796 {
John Bauman19bac1e2014-05-06 15:23:49 -0400797 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500798 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400799
800 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
801 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400802 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400803
804 c.x = c.x.xxxx;
805 c.y = c.y.yyyy;
806 c.z = c.z.zzzz;
807 c.w = c.w.wwww;
808
Nicolas Capenseafdb222015-05-15 15:24:08 -0400809 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400810 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500811 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400812 {
813 const Shader::Instruction &instruction = *shader->getInstruction(j);
814
815 if(instruction.opcode == Shader::OPCODE_DEF)
816 {
817 if(instruction.dst.index == i)
818 {
819 c.x = Float4(instruction.src[0].value[0]);
820 c.y = Float4(instruction.src[0].value[1]);
821 c.z = Float4(instruction.src[0].value[2]);
822 c.w = Float4(instruction.src[0].value[3]);
823
824 break;
825 }
826 }
827 }
828 }
829 }
830 else if(src.rel.type == Shader::PARAMETER_LOOP)
831 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500832 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400833
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400834 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400835
836 c.x = c.x.xxxx;
837 c.y = c.y.yyyy;
838 c.z = c.z.zzzz;
839 c.w = c.w.wwww;
840 }
841 else
842 {
843 if(src.rel.deterministic)
844 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400845 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400846
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400847 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500848
John Bauman19bac1e2014-05-06 15:23:49 -0400849 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 int component = src.rel.swizzle & 0x03;
857 Float4 a;
858
859 switch(src.rel.type)
860 {
Nicolas Capens32980ac2016-08-15 15:33:58 -0400861 case Shader::PARAMETER_ADDR: a = a0[component]; break;
862 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
863 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
864 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
865 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400866 case Shader::PARAMETER_MISCTYPE:
867 if(src.rel.index == Shader::InstanceIDIndex)
868 {
869 a = As<Float4>(Int4(instanceID)); break;
870 }
871 else if(src.rel.index == Shader::VertexIDIndex)
872 {
873 a = As<Float4>(vertexID); break;
874 }
875 else ASSERT(false);
876 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400877 default: ASSERT(false);
878 }
879
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400880 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400881
Alexis Hetu028f41b2016-01-13 14:40:47 -0500882 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 -0500883
John Bauman19bac1e2014-05-06 15:23:49 -0400884 Int index0 = Extract(index, 0);
885 Int index1 = Extract(index, 1);
886 Int index2 = Extract(index, 2);
887 Int index3 = Extract(index, 3);
888
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400889 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
890 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
891 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
892 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400893
894 transpose4x4(c.x, c.y, c.z, c.w);
895 }
896 }
897
898 return c;
899 }
900
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400901 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400902 {
903 ASSERT(var.rel.deterministic);
904
905 if(var.rel.type == Shader::PARAMETER_TEMP)
906 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500907 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400908 }
909 else if(var.rel.type == Shader::PARAMETER_INPUT)
910 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500911 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400912 }
913 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
914 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500915 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400916 }
917 else if(var.rel.type == Shader::PARAMETER_CONST)
918 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500919 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400920 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500921 else if(var.rel.type == Shader::PARAMETER_LOOP)
922 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500923 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500924 }
John Bauman19bac1e2014-05-06 15:23:49 -0400925 else ASSERT(false);
926
927 return 0;
928 }
929
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500930 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400931 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500932 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500933
John Baumand4ae8632014-05-06 16:18:33 -0400934 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400935 {
John Baumand4ae8632014-05-06 16:18:33 -0400936 if(shader->containsBreakInstruction() && instruction->analysisBreak)
937 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500938 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400939 }
John Bauman19bac1e2014-05-06 15:23:49 -0400940
John Baumand4ae8632014-05-06 16:18:33 -0400941 if(shader->containsContinueInstruction() && instruction->analysisContinue)
942 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500943 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400944 }
John Bauman19bac1e2014-05-06 15:23:49 -0400945
John Baumand4ae8632014-05-06 16:18:33 -0400946 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
947 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500948 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400949 }
John Bauman19bac1e2014-05-06 15:23:49 -0400950 }
951
952 return enable;
953 }
954
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500955 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400956 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400957 Vector4f row0 = fetchRegister(src1, 0);
958 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400959
960 dst.x = dot3(src0, row0);
961 dst.y = dot3(src0, row1);
962 }
963
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500964 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400965 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400966 Vector4f row0 = fetchRegister(src1, 0);
967 Vector4f row1 = fetchRegister(src1, 1);
968 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400969
970 dst.x = dot3(src0, row0);
971 dst.y = dot3(src0, row1);
972 dst.z = dot3(src0, row2);
973 }
974
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500975 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400976 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400977 Vector4f row0 = fetchRegister(src1, 0);
978 Vector4f row1 = fetchRegister(src1, 1);
979 Vector4f row2 = fetchRegister(src1, 2);
980 Vector4f row3 = fetchRegister(src1, 3);
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 dst.w = dot3(src0, row3);
986 }
987
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500988 void VertexProgram::M4X3(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);
John Bauman89401822014-05-06 15:04:28 -0400993
994 dst.x = dot4(src0, row0);
995 dst.y = dot4(src0, row1);
996 dst.z = dot4(src0, row2);
997 }
998
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500999 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001000 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001001 Vector4f row0 = fetchRegister(src1, 0);
1002 Vector4f row1 = fetchRegister(src1, 1);
1003 Vector4f row2 = fetchRegister(src1, 2);
1004 Vector4f row3 = fetchRegister(src1, 3);
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 dst.w = dot4(src0, row3);
1010 }
1011
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001012 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001013 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001014 BasicBlock *deadBlock = Nucleus::createBasicBlock();
1015 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001016
1017 if(breakDepth == 0)
1018 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001019 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001020 Nucleus::createBr(endBlock);
1021 }
1022 else
1023 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001024 enableBreak = enableBreak & ~enableStack[enableIndex];
1025 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001026
Nicolas Capens7551ac62016-01-20 17:11:53 -05001027 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001028 branch(allBreak, endBlock, deadBlock);
1029 }
1030
1031 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001032 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001033 }
1034
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001035 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001036 {
1037 Int4 condition;
1038
1039 switch(control)
1040 {
John Bauman19bac1e2014-05-06 15:23:49 -04001041 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1042 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1043 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1044 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1045 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1046 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001047 default:
1048 ASSERT(false);
1049 }
1050
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001051 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001052 }
1053
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001054 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001055 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001056 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001057
John Bauman19bac1e2014-05-06 15:23:49 -04001058 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001059 {
1060 condition = ~condition;
1061 }
1062
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001063 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001064 }
1065
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001066 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001067 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001068 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001069
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001070 BasicBlock *continueBlock = Nucleus::createBasicBlock();
1071 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001072
Nicolas Capens7551ac62016-01-20 17:11:53 -05001073 enableBreak = enableBreak & ~condition;
1074 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001075
Nicolas Capens7551ac62016-01-20 17:11:53 -05001076 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001077 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001078
John Bauman89401822014-05-06 15:04:28 -04001079 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001080 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001081 }
1082
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001083 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001084 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001085 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001086 }
1087
1088 void VertexProgram::TEST()
1089 {
1090 whileTest = true;
1091 }
1092
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001093 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001094 {
1095 if(!labelBlock[labelIndex])
1096 {
1097 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1098 }
1099
John Bauman19bac1e2014-05-06 15:23:49 -04001100 if(callRetBlock[labelIndex].size() > 1)
1101 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001102 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001103 }
John Bauman89401822014-05-06 15:04:28 -04001104
Nicolas Capens7551ac62016-01-20 17:11:53 -05001105 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001106
1107 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001108 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1109
Nicolas Capens7551ac62016-01-20 17:11:53 -05001110 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001111 }
1112
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001113 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001114 {
John Bauman19bac1e2014-05-06 15:23:49 -04001115 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001116 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001117 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001118 }
John Bauman19bac1e2014-05-06 15:23:49 -04001119 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001120 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001121 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001122 }
1123 else ASSERT(false);
1124 }
1125
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001126 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001127 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001128 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001129
John Bauman19bac1e2014-05-06 15:23:49 -04001130 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001131 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001132 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001133 }
1134
1135 if(!labelBlock[labelIndex])
1136 {
1137 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1138 }
1139
John Bauman19bac1e2014-05-06 15:23:49 -04001140 if(callRetBlock[labelIndex].size() > 1)
1141 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001142 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001143 }
John Bauman89401822014-05-06 15:04:28 -04001144
Nicolas Capens7551ac62016-01-20 17:11:53 -05001145 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001146
John Bauman19bac1e2014-05-06 15:23:49 -04001147 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1148 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1149
Nicolas Capens7551ac62016-01-20 17:11:53 -05001150 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001151 }
1152
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001153 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001154 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001155 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001156
John Bauman19bac1e2014-05-06 15:23:49 -04001157 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001158 {
1159 condition = ~condition;
1160 }
1161
Nicolas Capens7551ac62016-01-20 17:11:53 -05001162 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001163
1164 if(!labelBlock[labelIndex])
1165 {
1166 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1167 }
1168
John Bauman19bac1e2014-05-06 15:23:49 -04001169 if(callRetBlock[labelIndex].size() > 1)
1170 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001171 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001172 }
John Bauman89401822014-05-06 15:04:28 -04001173
Nicolas Capens7551ac62016-01-20 17:11:53 -05001174 enableIndex++;
1175 enableStack[enableIndex] = condition;
1176 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001177
John Bauman19bac1e2014-05-06 15:23:49 -04001178 Bool notAllFalse = SignMask(condition) != 0;
1179 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1180 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001181
Nicolas Capens7551ac62016-01-20 17:11:53 -05001182 enableIndex--;
1183 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001184 }
1185
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001186 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001187 {
1188 ifDepth--;
1189
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001190 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1191 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001192
1193 if(isConditionalIf[ifDepth])
1194 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001195 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001196 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001197
1198 branch(notAllFalse, falseBlock, endBlock);
1199
Nicolas Capens7551ac62016-01-20 17:11:53 -05001200 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001201 }
1202 else
1203 {
1204 Nucleus::createBr(endBlock);
1205 Nucleus::setInsertBlock(falseBlock);
1206 }
1207
1208 ifFalseBlock[ifDepth] = endBlock;
1209
1210 ifDepth++;
1211 }
1212
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001213 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001214 {
1215 ifDepth--;
1216
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001217 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001218
1219 Nucleus::createBr(endBlock);
1220 Nucleus::setInsertBlock(endBlock);
1221
1222 if(isConditionalIf[ifDepth])
1223 {
1224 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001225 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001226 }
1227 }
1228
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001229 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001230 {
1231 loopRepDepth--;
1232
Nicolas Capens7551ac62016-01-20 17:11:53 -05001233 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001234
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001235 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1236 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001237
1238 Nucleus::createBr(testBlock);
1239 Nucleus::setInsertBlock(endBlock);
1240
Nicolas Capens7551ac62016-01-20 17:11:53 -05001241 loopDepth--;
1242 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001243 }
1244
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001245 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001246 {
1247 loopRepDepth--;
1248
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001249 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1250 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001251
1252 Nucleus::createBr(testBlock);
1253 Nucleus::setInsertBlock(endBlock);
1254
Nicolas Capens7551ac62016-01-20 17:11:53 -05001255 loopDepth--;
1256 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001257 }
1258
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001259 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001260 {
1261 loopRepDepth--;
1262
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001263 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1264 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001265
1266 Nucleus::createBr(testBlock);
1267 Nucleus::setInsertBlock(endBlock);
1268
Nicolas Capens7551ac62016-01-20 17:11:53 -05001269 enableIndex--;
1270 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001271 whileTest = false;
1272 }
1273
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001274 void VertexProgram::ENDSWITCH()
1275 {
1276 loopRepDepth--;
1277
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001278 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001279
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001280 Nucleus::createBr(loopRepEndBlock[loopRepDepth]);
1281 Nucleus::setInsertBlock(endBlock);
1282
1283 enableIndex--;
1284 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1285 }
1286
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001287 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001288 {
John Bauman19bac1e2014-05-06 15:23:49 -04001289 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001290 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001291 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001292 }
John Bauman19bac1e2014-05-06 15:23:49 -04001293 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001294 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001295 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001296 }
John Bauman19bac1e2014-05-06 15:23:49 -04001297 else
1298 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001299 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001300 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001301 }
John Bauman89401822014-05-06 15:04:28 -04001302 }
1303
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001304 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001305 {
1306 ASSERT(ifDepth < 24 + 4);
1307
Nicolas Capens7551ac62016-01-20 17:11:53 -05001308 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001309
John Bauman19bac1e2014-05-06 15:23:49 -04001310 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001311 {
John Bauman19bac1e2014-05-06 15:23:49 -04001312 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001313 }
1314
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001315 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1316 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001317
1318 branch(condition, trueBlock, falseBlock);
1319
1320 isConditionalIf[ifDepth] = false;
1321 ifFalseBlock[ifDepth] = falseBlock;
1322
1323 ifDepth++;
1324 }
1325
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001326 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001327 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001328 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001329
John Bauman19bac1e2014-05-06 15:23:49 -04001330 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001331 {
1332 condition = ~condition;
1333 }
1334
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001335 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001336 }
1337
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001338 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001339 {
1340 Int4 condition;
1341
1342 switch(control)
1343 {
John Bauman19bac1e2014-05-06 15:23:49 -04001344 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1345 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1346 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1347 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1348 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1349 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001350 default:
1351 ASSERT(false);
1352 }
1353
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001354 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001355 }
1356
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001357 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001358 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001359 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001360
Nicolas Capens7551ac62016-01-20 17:11:53 -05001361 enableIndex++;
1362 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001363
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001364 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1365 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001366
John Bauman19bac1e2014-05-06 15:23:49 -04001367 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001368
1369 branch(notAllFalse, trueBlock, falseBlock);
1370
1371 isConditionalIf[ifDepth] = true;
1372 ifFalseBlock[ifDepth] = falseBlock;
1373
1374 ifDepth++;
1375 breakDepth++;
1376 }
1377
1378 void VertexProgram::LABEL(int labelIndex)
1379 {
John Bauman19bac1e2014-05-06 15:23:49 -04001380 if(!labelBlock[labelIndex])
1381 {
1382 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1383 }
1384
John Bauman89401822014-05-06 15:04:28 -04001385 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001386 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001387 }
1388
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001389 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001390 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001391 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001392
Nicolas Capens7551ac62016-01-20 17:11:53 -05001393 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1394 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1395 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001396
1397 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001398 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001399 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001400 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001401 }
1402
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001403 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1404 BasicBlock *testBlock = Nucleus::createBasicBlock();
1405 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001406
1407 loopRepTestBlock[loopRepDepth] = testBlock;
1408 loopRepEndBlock[loopRepDepth] = endBlock;
1409
1410 // FIXME: jump(testBlock)
1411 Nucleus::createBr(testBlock);
1412 Nucleus::setInsertBlock(testBlock);
1413
Nicolas Capens7551ac62016-01-20 17:11:53 -05001414 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001415 Nucleus::setInsertBlock(loopBlock);
1416
Nicolas Capens7551ac62016-01-20 17:11:53 -05001417 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001418
John Bauman89401822014-05-06 15:04:28 -04001419 loopRepDepth++;
1420 breakDepth = 0;
1421 }
1422
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001423 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001424 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001425 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001426
Nicolas Capens7551ac62016-01-20 17:11:53 -05001427 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1428 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001429
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001430 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1431 BasicBlock *testBlock = Nucleus::createBasicBlock();
1432 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001433
1434 loopRepTestBlock[loopRepDepth] = testBlock;
1435 loopRepEndBlock[loopRepDepth] = endBlock;
1436
1437 // FIXME: jump(testBlock)
1438 Nucleus::createBr(testBlock);
1439 Nucleus::setInsertBlock(testBlock);
1440
Nicolas Capens7551ac62016-01-20 17:11:53 -05001441 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001442 Nucleus::setInsertBlock(loopBlock);
1443
Nicolas Capens7551ac62016-01-20 17:11:53 -05001444 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001445
1446 loopRepDepth++;
1447 breakDepth = 0;
1448 }
1449
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001450 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001451 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001452 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001453
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001454 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1455 BasicBlock *testBlock = Nucleus::createBasicBlock();
1456 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001457
John Bauman19bac1e2014-05-06 15:23:49 -04001458 loopRepTestBlock[loopRepDepth] = testBlock;
1459 loopRepEndBlock[loopRepDepth] = endBlock;
1460
Nicolas Capens7551ac62016-01-20 17:11:53 -05001461 Int4 restoreBreak = enableBreak;
1462 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001463
1464 // FIXME: jump(testBlock)
1465 Nucleus::createBr(testBlock);
1466 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001467 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001468
Nicolas Capensc2534f42016-04-04 11:13:24 -04001469 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001470 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001471 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001472 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001473 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001474
1475 Bool notAllFalse = SignMask(condition) != 0;
1476 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001477
John Bauman19bac1e2014-05-06 15:23:49 -04001478 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001479 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001480
John Bauman19bac1e2014-05-06 15:23:49 -04001481 Nucleus::setInsertBlock(loopBlock);
1482
1483 loopRepDepth++;
1484 breakDepth = 0;
1485 }
1486
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001487 void VertexProgram::SWITCH()
1488 {
1489 enableIndex++;
1490 enableStack[enableIndex] = Int4(0xFFFFFFFF);
1491
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001492 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001493
1494 loopRepTestBlock[loopRepDepth] = nullptr;
1495 loopRepEndBlock[loopRepDepth] = endBlock;
1496
1497 loopRepDepth++;
1498 breakDepth = 0;
1499 }
1500
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001501 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001502 {
John Bauman19bac1e2014-05-06 15:23:49 -04001503 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001504 {
1505 returnBlock = Nucleus::createBasicBlock();
1506 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001507 }
1508 else
1509 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001510 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001511
John Bauman19bac1e2014-05-06 15:23:49 -04001512 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001513 {
John Bauman19bac1e2014-05-06 15:23:49 -04001514 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001515 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001516
Nicolas Capens19336542016-09-26 10:32:29 -04001517 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001518 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001519
1520 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1521 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001522 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001523 }
1524 }
1525 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1526 {
1527 Nucleus::createBr(callRetBlock[currentLabel][0]);
1528 }
1529 else // Function isn't called
1530 {
1531 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001532 }
1533
1534 Nucleus::setInsertBlock(unreachableBlock);
1535 Nucleus::createUnreachable();
1536 }
1537 }
1538
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001539 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001540 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001541 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001542
John Bauman19bac1e2014-05-06 15:23:49 -04001543 // FIXME: Return from function if all instances left
1544 // FIXME: Use enableLeave in other control-flow constructs
1545 }
John Bauman89401822014-05-06 15:04:28 -04001546
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001547 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001548 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001549 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001550 }
1551
Nicolas Capensa0b57832017-11-07 13:07:53 -05001552 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001553 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001554 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001555 }
1556
Nicolas Capensa0b57832017-11-07 13:07:53 -05001557 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001558 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001559 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001560 }
1561
Nicolas Capensa0b57832017-11-07 13:07:53 -05001562 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001563 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001564 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001565 }
1566
Nicolas Capensa0b57832017-11-07 13:07:53 -05001567 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001568 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001569 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001570 }
1571
Nicolas Capensa0b57832017-11-07 13:07:53 -05001572 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001573 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001574 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001575 }
1576
Nicolas Capensa0b57832017-11-07 13:07:53 -05001577 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001578 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001579 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1580 }
1581
1582 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1583 {
1584 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001585 }
1586
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001587 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001588 {
Alexis Hetu95ac1872016-06-06 13:26:52 -04001589 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
Nicolas Capens89a218b2017-11-07 13:05:20 -05001590 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001591 }
1592
Nicolas Capensa0b57832017-11-07 13:07:53 -05001593 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 -04001594 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001595 Vector4f tmp;
1596
John Bauman19bac1e2014-05-06 15:23:49 -04001597 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1598 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001599 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001600 }
1601 else
1602 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001603 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001604
Nicolas Capensc2534f42016-04-04 11:13:24 -04001605 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001606 {
1607 if(shader->usesSampler(i))
1608 {
1609 If(index == i)
1610 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001611 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001612 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1613 }
1614 }
1615 }
1616 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001617
Nicolas Capens89a218b2017-11-07 13:05:20 -05001618 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001619 c.x = tmp[(s.swizzle >> 0) & 0x3];
1620 c.y = tmp[(s.swizzle >> 2) & 0x3];
1621 c.z = tmp[(s.swizzle >> 4) & 0x3];
1622 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001623
1624 return c;
1625 }
1626
Nicolas Capensa0b57832017-11-07 13:07:53 -05001627 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001628 {
1629 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001630 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 -04001631 }
John Bauman89401822014-05-06 15:04:28 -04001632}