blob: d492c651e1ac0f7ceb1d0213fec83869e4e7bcbf [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)
Nicolas Capens5bff4052018-05-28 13:18:59 -040027 : VertexRoutine(state, shader), shader(shader), r(shader->indirectAddressableTemporaries)
John Bauman89401822014-05-06 15:04:28 -040028 {
John Bauman89401822014-05-06 15:04:28 -040029 ifDepth = 0;
30 loopRepDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040031 currentLabel = -1;
John Bauman89401822014-05-06 15:04:28 -040032
33 for(int i = 0; i < 2048; i++)
34 {
35 labelBlock[i] = 0;
36 }
Nicolas Capens7551ac62016-01-20 17:11:53 -050037
38 loopDepth = -1;
39 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
40
Nicolas Capensc62fad32018-01-26 01:55:31 +000041 if(shader->containsBreakInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050042 {
43 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
44 }
45
Nicolas Capensc62fad32018-01-26 01:55:31 +000046 if(shader->containsContinueInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050047 {
48 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
49 }
50
Alexis Hetu02ad0aa2016-08-02 11:18:14 -040051 if(shader->isInstanceIdDeclared())
Nicolas Capens7551ac62016-01-20 17:11:53 -050052 {
53 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
54 }
John Bauman89401822014-05-06 15:04:28 -040055 }
56
57 VertexProgram::~VertexProgram()
58 {
John Bauman89401822014-05-06 15:04:28 -040059 }
60
Nicolas Capens4b743732018-05-28 13:22:07 -040061 void VertexProgram::pipeline(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040062 {
John Bauman89401822014-05-06 15:04:28 -040063 if(!state.preTransformed)
64 {
Alexis Hetu877ddfc2017-07-25 17:48:00 -040065 program(index);
John Bauman89401822014-05-06 15:04:28 -040066 }
67 else
68 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050069 passThrough();
John Bauman89401822014-05-06 15:04:28 -040070 }
71 }
72
Nicolas Capens4b743732018-05-28 13:22:07 -040073 void VertexProgram::program(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040074 {
John Bauman19bac1e2014-05-06 15:23:49 -040075 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040076
Alexis Hetu53ad4af2017-12-06 14:49:07 -050077 unsigned short shaderModel = shader->getShaderModel();
John Bauman89401822014-05-06 15:04:28 -040078
Nicolas Capens7551ac62016-01-20 17:11:53 -050079 enableIndex = 0;
80 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040081
Nicolas Capens4677a5f2014-05-06 16:42:26 -040082 if(shader->containsLeaveInstruction())
83 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050084 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040085 }
86
Alexis Hetu877ddfc2017-07-25 17:48:00 -040087 if(shader->isVertexIdDeclared())
88 {
89 if(state.textureSampling)
90 {
91 vertexID = Int4(index);
92 }
93 else
94 {
95 vertexID = Insert(vertexID, As<Int>(index), 0);
96 vertexID = Insert(vertexID, As<Int>(index + 1), 1);
97 vertexID = Insert(vertexID, As<Int>(index + 2), 2);
98 vertexID = Insert(vertexID, As<Int>(index + 3), 3);
99 }
100 }
101
John Bauman19bac1e2014-05-06 15:23:49 -0400102 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -0500103 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400104 {
John Bauman19bac1e2014-05-06 15:23:49 -0400105 const Shader::Instruction *instruction = shader->getInstruction(i);
106 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400107
John Bauman19bac1e2014-05-06 15:23:49 -0400108 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
109 {
110 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400111
John Bauman19bac1e2014-05-06 15:23:49 -0400112 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
113 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
114 }
115 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500116
Alexis Hetu903e0252014-11-25 14:25:32 -0500117 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400118 {
119 const Shader::Instruction *instruction = shader->getInstruction(i);
120 Shader::Opcode opcode = instruction->opcode;
121
122 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400123 {
124 continue;
125 }
126
John Bauman19bac1e2014-05-06 15:23:49 -0400127 Dst dst = instruction->dst;
128 Src src0 = instruction->src[0];
129 Src src1 = instruction->src[1];
130 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400131 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400132 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400133
John Bauman19bac1e2014-05-06 15:23:49 -0400134 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400135 Control control = instruction->control;
136 bool integer = dst.type == Shader::PARAMETER_ADDR;
137 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400138
John Bauman19bac1e2014-05-06 15:23:49 -0400139 Vector4f d;
140 Vector4f s0;
141 Vector4f s1;
142 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400143 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400144 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400145
Nicolas Capensc2534f42016-04-04 11:13:24 -0400146 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
147 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
148 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
149 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
150 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400151
152 switch(opcode)
153 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500154 case Shader::OPCODE_VS_1_0: break;
155 case Shader::OPCODE_VS_1_1: break;
156 case Shader::OPCODE_VS_2_0: break;
157 case Shader::OPCODE_VS_2_x: break;
158 case Shader::OPCODE_VS_2_sw: break;
159 case Shader::OPCODE_VS_3_0: break;
160 case Shader::OPCODE_VS_3_sw: break;
161 case Shader::OPCODE_DCL: break;
162 case Shader::OPCODE_DEF: break;
163 case Shader::OPCODE_DEFI: break;
164 case Shader::OPCODE_DEFB: break;
165 case Shader::OPCODE_NOP: break;
166 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400167 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500168 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400169 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500170 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
171 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
172 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
173 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
174 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
175 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
176 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
177 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
178 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
179 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
180 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
181 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
182 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
183 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
184 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
185 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
186 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400187 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
188 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
189 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500190 case Shader::OPCODE_ATT: att(d, s0, s1); break;
191 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
192 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500193 case Shader::OPCODE_EXPP: expp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500194 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
195 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400196 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
197 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400198 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500199 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400200 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500201 case Shader::OPCODE_LIT: lit(d, s0); break;
202 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
203 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500204 case Shader::OPCODE_LOGP: logp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500205 case Shader::OPCODE_LOG: log(d, s0, pp); break;
206 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
207 case Shader::OPCODE_STEP: step(d, s0, s1); break;
208 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8ef6d102017-11-09 15:49:09 -0500209 case Shader::OPCODE_ISINF: isinf(d, s0); break;
210 case Shader::OPCODE_ISNAN: isnan(d, s0); 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.saturate)
John Bauman89401822014-05-06 15:04:28 -0400347 {
John Bauman19bac1e2014-05-06 15:23:49 -0400348 if(dst.x) d.x = Max(d.x, Float4(0.0f));
349 if(dst.y) d.y = Max(d.y, Float4(0.0f));
350 if(dst.z) d.z = Max(d.z, Float4(0.0f));
351 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400352
John Bauman19bac1e2014-05-06 15:23:49 -0400353 if(dst.x) d.x = Min(d.x, Float4(1.0f));
354 if(dst.y) d.y = Min(d.y, Float4(1.0f));
355 if(dst.z) d.z = Min(d.z, Float4(1.0f));
356 if(dst.w) d.w = Min(d.w, Float4(1.0f));
357 }
358
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400359 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400360 {
361 Vector4f pDst; // FIXME: Rename
362
363 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400364 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500365 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400366 case Shader::PARAMETER_TEMP:
367 if(dst.rel.type == Shader::PARAMETER_VOID)
368 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500369 if(dst.x) pDst.x = r[dst.index].x;
370 if(dst.y) pDst.y = r[dst.index].y;
371 if(dst.z) pDst.z = r[dst.index].z;
372 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400373 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400374 else if(!dst.rel.dynamic)
375 {
376 Int a = dst.index + relativeAddress(dst.rel);
377
378 if(dst.x) pDst.x = r[a].x;
379 if(dst.y) pDst.y = r[a].y;
380 if(dst.z) pDst.z = r[a].z;
381 if(dst.w) pDst.w = r[a].w;
382 }
John Bauman19bac1e2014-05-06 15:23:49 -0400383 else
384 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400385 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400386
Nicolas Capens4b743732018-05-28 13:22:07 -0400387 if(dst.x) pDst.x = r[a].x;
388 if(dst.y) pDst.y = r[a].y;
389 if(dst.z) pDst.z = r[a].z;
390 if(dst.w) pDst.w = r[a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400391 }
392 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500393 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400394 case Shader::PARAMETER_RASTOUT:
395 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400396 {
397 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500398 if(dst.x) pDst.x = o[Pos].x;
399 if(dst.y) pDst.y = o[Pos].y;
400 if(dst.z) pDst.z = o[Pos].z;
401 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400402 break;
403 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500404 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400405 break;
406 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500407 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400408 break;
409 default:
410 ASSERT(false);
411 }
412 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400413 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400414 if(dst.x) pDst.x = o[C0 + dst.index].x;
415 if(dst.y) pDst.y = o[C0 + dst.index].y;
416 if(dst.z) pDst.z = o[C0 + dst.index].z;
417 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400418 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400419 case Shader::PARAMETER_TEXCRDOUT:
420 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500421 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400422 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500423 if(dst.x) pDst.x = o[T0 + dst.index].x;
424 if(dst.y) pDst.y = o[T0 + dst.index].y;
425 if(dst.z) pDst.z = o[T0 + dst.index].z;
426 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400427 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400428 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
429 {
430 if(dst.x) pDst.x = o[dst.index].x;
431 if(dst.y) pDst.y = o[dst.index].y;
432 if(dst.z) pDst.z = o[dst.index].z;
433 if(dst.w) pDst.w = o[dst.index].w;
434 }
435 else if(!dst.rel.dynamic)
436 {
437 Int a = dst.index + relativeAddress(dst.rel);
438
439 if(dst.x) pDst.x = o[a].x;
440 if(dst.y) pDst.y = o[a].y;
441 if(dst.z) pDst.z = o[a].z;
442 if(dst.w) pDst.w = o[a].w;
443 }
John Bauman89401822014-05-06 15:04:28 -0400444 else
445 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400446 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400447
Nicolas Capens4b743732018-05-28 13:22:07 -0400448 if(dst.x) pDst.x = o[a].x;
449 if(dst.y) pDst.y = o[a].y;
450 if(dst.z) pDst.z = o[a].z;
451 if(dst.w) pDst.w = o[a].w;
John Bauman89401822014-05-06 15:04:28 -0400452 }
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 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400516 else if(!dst.rel.dynamic)
517 {
518 Int a = dst.index + relativeAddress(dst.rel);
519
520 if(dst.x) r[a].x = d.x;
521 if(dst.y) r[a].y = d.y;
522 if(dst.z) r[a].z = d.z;
523 if(dst.w) r[a].w = d.w;
524 }
John Bauman19bac1e2014-05-06 15:23:49 -0400525 else
526 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400527 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400528
Nicolas Capens4b743732018-05-28 13:22:07 -0400529 if(dst.x) r.scatter_x(a, d.x);
530 if(dst.y) r.scatter_y(a, d.y);
531 if(dst.z) r.scatter_z(a, d.z);
532 if(dst.w) r.scatter_w(a, d.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400533 }
John Bauman89401822014-05-06 15:04:28 -0400534 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400535 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500536 if(dst.x) a0.x = d.x;
537 if(dst.y) a0.y = d.y;
538 if(dst.z) a0.z = d.z;
539 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400540 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400541 case Shader::PARAMETER_RASTOUT:
542 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400543 {
544 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500545 if(dst.x) o[Pos].x = d.x;
546 if(dst.y) o[Pos].y = d.y;
547 if(dst.z) o[Pos].z = d.z;
548 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400549 break;
550 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500551 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400552 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500553 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500554 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400555 break;
556 default: ASSERT(false);
557 }
558 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500559 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400560 if(dst.x) o[C0 + dst.index].x = d.x;
561 if(dst.y) o[C0 + dst.index].y = d.y;
562 if(dst.z) o[C0 + dst.index].z = d.z;
563 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400564 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400565 case Shader::PARAMETER_TEXCRDOUT:
566 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500567 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400568 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500569 if(dst.x) o[T0 + dst.index].x = d.x;
570 if(dst.y) o[T0 + dst.index].y = d.y;
571 if(dst.z) o[T0 + dst.index].z = d.z;
572 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400573 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400574 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
575 {
576 if(dst.x) o[dst.index].x = d.x;
577 if(dst.y) o[dst.index].y = d.y;
578 if(dst.z) o[dst.index].z = d.z;
579 if(dst.w) o[dst.index].w = d.w;
580 }
581 else if(!dst.rel.dynamic)
582 {
583 Int a = dst.index + relativeAddress(dst.rel);
584
585 if(dst.x) o[a].x = d.x;
586 if(dst.y) o[a].y = d.y;
587 if(dst.z) o[a].z = d.z;
588 if(dst.w) o[a].w = d.w;
589 }
John Bauman89401822014-05-06 15:04:28 -0400590 else
591 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400592 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400593
Nicolas Capens4b743732018-05-28 13:22:07 -0400594 if(dst.x) o.scatter_x(a, d.x);
595 if(dst.y) o.scatter_y(a, d.y);
596 if(dst.z) o.scatter_z(a, d.z);
597 if(dst.w) o.scatter_w(a, d.w);
John Bauman89401822014-05-06 15:04:28 -0400598 }
599 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500600 case Shader::PARAMETER_LABEL: break;
601 case Shader::PARAMETER_PREDICATE: p0 = d; break;
602 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400603 default:
604 ASSERT(false);
605 }
606 }
607 }
608
John Bauman19bac1e2014-05-06 15:23:49 -0400609 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400610 {
611 Nucleus::setInsertBlock(returnBlock);
612 }
613 }
614
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500615 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400616 {
John Bauman19bac1e2014-05-06 15:23:49 -0400617 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400618 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400619 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400620 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400621 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400622
623 switch(usage)
624 {
625 case 0xFF:
626 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400627 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500628 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400629 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400630 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500631 o[i].x = v[i].x;
632 o[i].y = v[i].y;
633 o[i].z = v[i].z;
634 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400635 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400636 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500637 o[i].x = v[i].x;
638 o[i].y = v[i].y;
639 o[i].z = v[i].z;
640 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400641 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400642 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500643 o[i].x = v[i].x;
644 o[i].y = v[i].y;
645 o[i].z = v[i].z;
646 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400647 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400648 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500649 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400650 break;
651 default:
652 ASSERT(false);
653 }
654 }
655 }
656 else
657 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500658 o[Pos].x = v[PositionT].x;
659 o[Pos].y = v[PositionT].y;
660 o[Pos].z = v[PositionT].z;
661 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400662
663 for(int i = 0; i < 2; i++)
664 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400665 o[C0 + i].x = v[Color0 + i].x;
666 o[C0 + i].y = v[Color0 + i].y;
667 o[C0 + i].z = v[Color0 + i].z;
668 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400669 }
670
671 for(int i = 0; i < 8; i++)
672 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500673 o[T0 + i].x = v[TexCoord0 + i].x;
674 o[T0 + i].y = v[TexCoord0 + i].y;
675 o[T0 + i].z = v[TexCoord0 + i].z;
676 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400677 }
678
Nicolas Capens7551ac62016-01-20 17:11:53 -0500679 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400680 }
681 }
682
Nicolas Capensc2534f42016-04-04 11:13:24 -0400683 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400684 {
John Bauman19bac1e2014-05-06 15:23:49 -0400685 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500686 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400687
John Bauman89401822014-05-06 15:04:28 -0400688 switch(src.type)
689 {
John Bauman19bac1e2014-05-06 15:23:49 -0400690 case Shader::PARAMETER_TEMP:
691 if(src.rel.type == Shader::PARAMETER_VOID)
692 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500693 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400694 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400695 else if(!src.rel.dynamic)
696 {
697 reg = r[i + relativeAddress(src.rel, src.bufferIndex)];
698 }
John Bauman19bac1e2014-05-06 15:23:49 -0400699 else
700 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400701 reg = r[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400702 }
703 break;
704 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500705 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400706 break;
707 case Shader::PARAMETER_INPUT:
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 = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400711 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400712 else if(!src.rel.dynamic)
713 {
714 reg = v[i + relativeAddress(src.rel, src.bufferIndex)];
715 }
John Bauman19bac1e2014-05-06 15:23:49 -0400716 else
717 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400718 reg = v[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400719 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400720 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500721 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400722 case Shader::PARAMETER_FLOAT4LITERAL:
723 reg.x = Float4(src.value[0]);
724 reg.y = Float4(src.value[1]);
725 reg.z = Float4(src.value[2]);
726 reg.w = Float4(src.value[3]);
727 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500728 case Shader::PARAMETER_ADDR: reg = a0; break;
729 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
730 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
731 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
732 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400733 case Shader::PARAMETER_SAMPLER:
734 if(src.rel.type == Shader::PARAMETER_VOID)
735 {
736 reg.x = As<Float4>(Int4(i));
737 }
738 else if(src.rel.type == Shader::PARAMETER_TEMP)
739 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500740 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400741 }
742 return reg;
743 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400744 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400745 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500746 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400747 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400748 else if(!src.rel.dynamic)
749 {
750 reg = o[i + relativeAddress(src.rel, src.bufferIndex)];
751 }
John Bauman19bac1e2014-05-06 15:23:49 -0400752 else
753 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400754 reg = o[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400755 }
756 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400757 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400758 if(src.index == Shader::InstanceIDIndex)
759 {
760 reg.x = As<Float>(instanceID);
761 }
762 else if(src.index == Shader::VertexIDIndex)
763 {
764 reg.x = As<Float4>(vertexID);
765 }
766 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400767 return reg;
John Bauman89401822014-05-06 15:04:28 -0400768 default:
769 ASSERT(false);
770 }
771
John Bauman66b8ab22014-05-06 15:57:45 -0400772 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
773 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
774 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
775 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400776
John Bauman66b8ab22014-05-06 15:57:45 -0400777 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400778
779 switch(src.modifier)
780 {
John Bauman19bac1e2014-05-06 15:23:49 -0400781 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400782 mod.x = x;
783 mod.y = y;
784 mod.z = z;
785 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400786 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400787 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400788 mod.x = -x;
789 mod.y = -y;
790 mod.z = -z;
791 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400792 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400793 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400794 mod.x = Abs(x);
795 mod.y = Abs(y);
796 mod.z = Abs(z);
797 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400798 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400799 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400800 mod.x = -Abs(x);
801 mod.y = -Abs(y);
802 mod.z = -Abs(z);
803 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400804 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400805 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400806 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
807 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
808 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
809 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400810 break;
811 default:
812 ASSERT(false);
813 }
814
815 return mod;
816 }
817
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400818 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
819 {
820 if(bufferIndex == -1)
821 {
822 return data + OFFSET(DrawData, vs.c[index]);
823 }
824 else
825 {
826 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
827 }
828 }
829
Nicolas Capens4b743732018-05-28 13:22:07 -0400830 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int &offset)
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400831 {
832 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
833 }
834
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500835 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400836 {
John Bauman19bac1e2014-05-06 15:23:49 -0400837 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500838 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400839
840 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
841 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400842 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400843
844 c.x = c.x.xxxx;
845 c.y = c.y.yyyy;
846 c.z = c.z.zzzz;
847 c.w = c.w.wwww;
848
Nicolas Capenseafdb222015-05-15 15:24:08 -0400849 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400850 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500851 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400852 {
853 const Shader::Instruction &instruction = *shader->getInstruction(j);
854
855 if(instruction.opcode == Shader::OPCODE_DEF)
856 {
857 if(instruction.dst.index == i)
858 {
859 c.x = Float4(instruction.src[0].value[0]);
860 c.y = Float4(instruction.src[0].value[1]);
861 c.z = Float4(instruction.src[0].value[2]);
862 c.w = Float4(instruction.src[0].value[3]);
863
864 break;
865 }
866 }
867 }
868 }
869 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400870 else if(!src.rel.dynamic || src.rel.type == Shader::PARAMETER_LOOP)
John Bauman19bac1e2014-05-06 15:23:49 -0400871 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400872 Int a = relativeAddress(src.rel, src.bufferIndex);
John Bauman19bac1e2014-05-06 15:23:49 -0400873
Nicolas Capens4b743732018-05-28 13:22:07 -0400874 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
John Bauman19bac1e2014-05-06 15:23:49 -0400875
876 c.x = c.x.xxxx;
877 c.y = c.y.yyyy;
878 c.z = c.z.zzzz;
879 c.w = c.w.wwww;
880 }
881 else
882 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400883 int component = src.rel.swizzle & 0x03;
884 Float4 a;
885
886 switch(src.rel.type)
John Bauman19bac1e2014-05-06 15:23:49 -0400887 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400888 case Shader::PARAMETER_ADDR: a = a0[component]; break;
889 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
890 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
891 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
892 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
893 case Shader::PARAMETER_MISCTYPE:
894 switch(src.rel.index)
John Bauman19bac1e2014-05-06 15:23:49 -0400895 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400896 case Shader::InstanceIDIndex: a = As<Float4>(Int4(instanceID)); break;
897 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400898 default: ASSERT(false);
899 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400900 break;
901 default: ASSERT(false);
John Bauman19bac1e2014-05-06 15:23:49 -0400902 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400903
904 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
905
906 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
907
908 Int index0 = Extract(index, 0);
909 Int index1 = Extract(index, 1);
910 Int index2 = Extract(index, 2);
911 Int index3 = Extract(index, 3);
912
913 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
914 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
915 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
916 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
917
918 transpose4x4(c.x, c.y, c.z, c.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400919 }
920
921 return c;
922 }
923
Nicolas Capens4b743732018-05-28 13:22:07 -0400924 Int VertexProgram::relativeAddress(const Shader::Relative &rel, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400925 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400926 ASSERT(!rel.dynamic);
John Bauman19bac1e2014-05-06 15:23:49 -0400927
Nicolas Capens4b743732018-05-28 13:22:07 -0400928 if(rel.type == Shader::PARAMETER_TEMP)
John Bauman19bac1e2014-05-06 15:23:49 -0400929 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400930 return As<Int>(Extract(r[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400931 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400932 else if(rel.type == Shader::PARAMETER_INPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400933 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400934 return As<Int>(Extract(v[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400935 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400936 else if(rel.type == Shader::PARAMETER_OUTPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400937 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400938 return As<Int>(Extract(o[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400939 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400940 else if(rel.type == Shader::PARAMETER_CONST)
John Bauman19bac1e2014-05-06 15:23:49 -0400941 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400942 return *Pointer<Int>(uniformAddress(bufferIndex, rel.index)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400943 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400944 else if(rel.type == Shader::PARAMETER_LOOP)
Nicolas Capens907700d2016-01-20 17:09:28 -0500945 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500946 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500947 }
John Bauman19bac1e2014-05-06 15:23:49 -0400948 else ASSERT(false);
949
950 return 0;
951 }
952
Nicolas Capens4b743732018-05-28 13:22:07 -0400953 Int4 VertexProgram::dynamicAddress(const Shader::Relative &rel)
954 {
955 int component = rel.swizzle & 0x03;
956 Float4 a;
957
958 switch(rel.type)
959 {
960 case Shader::PARAMETER_ADDR: a = a0[component]; break;
961 case Shader::PARAMETER_TEMP: a = r[rel.index][component]; break;
962 case Shader::PARAMETER_INPUT: a = v[rel.index][component]; break;
963 case Shader::PARAMETER_OUTPUT: a = o[rel.index][component]; break;
964 case Shader::PARAMETER_MISCTYPE:
965 switch(rel.index)
966 {
967 case Shader::InstanceIDIndex: a = As<Float>(instanceID); break;
968 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
969 default: ASSERT(false);
970 }
971 break;
972 default: ASSERT(false);
973 }
974
975 return As<Int4>(a) * Int4(rel.scale);
976 }
977
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500978 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400979 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500980 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500981
Nicolas Capens8a587712018-10-20 14:17:49 -0400982 if(shader->containsBreakInstruction() && instruction->analysisBreak)
John Bauman19bac1e2014-05-06 15:23:49 -0400983 {
Nicolas Capens8a587712018-10-20 14:17:49 -0400984 enable &= enableBreak;
985 }
John Bauman19bac1e2014-05-06 15:23:49 -0400986
Nicolas Capens8a587712018-10-20 14:17:49 -0400987 if(shader->containsContinueInstruction() && instruction->analysisContinue)
988 {
989 enable &= enableContinue;
990 }
John Bauman19bac1e2014-05-06 15:23:49 -0400991
Nicolas Capens8a587712018-10-20 14:17:49 -0400992 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
993 {
994 enable &= enableLeave;
John Bauman19bac1e2014-05-06 15:23:49 -0400995 }
996
997 return enable;
998 }
999
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001000 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001001 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001002 Vector4f row0 = fetchRegister(src1, 0);
1003 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -04001004
1005 dst.x = dot3(src0, row0);
1006 dst.y = dot3(src0, row1);
1007 }
1008
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001009 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001010 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001011 Vector4f row0 = fetchRegister(src1, 0);
1012 Vector4f row1 = fetchRegister(src1, 1);
1013 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001014
1015 dst.x = dot3(src0, row0);
1016 dst.y = dot3(src0, row1);
1017 dst.z = dot3(src0, row2);
1018 }
1019
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001020 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001021 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001022 Vector4f row0 = fetchRegister(src1, 0);
1023 Vector4f row1 = fetchRegister(src1, 1);
1024 Vector4f row2 = fetchRegister(src1, 2);
1025 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001026
1027 dst.x = dot3(src0, row0);
1028 dst.y = dot3(src0, row1);
1029 dst.z = dot3(src0, row2);
1030 dst.w = dot3(src0, row3);
1031 }
1032
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001033 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001034 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001035 Vector4f row0 = fetchRegister(src1, 0);
1036 Vector4f row1 = fetchRegister(src1, 1);
1037 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001038
1039 dst.x = dot4(src0, row0);
1040 dst.y = dot4(src0, row1);
1041 dst.z = dot4(src0, row2);
1042 }
1043
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001044 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001045 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001046 Vector4f row0 = fetchRegister(src1, 0);
1047 Vector4f row1 = fetchRegister(src1, 1);
1048 Vector4f row2 = fetchRegister(src1, 2);
1049 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001050
1051 dst.x = dot4(src0, row0);
1052 dst.y = dot4(src0, row1);
1053 dst.z = dot4(src0, row2);
1054 dst.w = dot4(src0, row3);
1055 }
1056
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001057 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001058 {
Nicolas Capens6d123312018-01-08 12:57:52 -05001059 enableBreak = enableBreak & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001060 }
1061
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001062 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001063 {
1064 Int4 condition;
1065
1066 switch(control)
1067 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001068 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1069 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1070 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1071 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1072 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1073 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001074 default:
1075 ASSERT(false);
1076 }
1077
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001078 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001079 }
1080
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001081 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001082 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001083 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001084
John Bauman19bac1e2014-05-06 15:23:49 -04001085 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001086 {
1087 condition = ~condition;
1088 }
1089
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001090 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001091 }
1092
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001093 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001094 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001095 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001096
Nicolas Capens7551ac62016-01-20 17:11:53 -05001097 enableBreak = enableBreak & ~condition;
John Bauman89401822014-05-06 15:04:28 -04001098 }
1099
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001100 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001101 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001102 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001103 }
1104
1105 void VertexProgram::TEST()
1106 {
Nicolas Capens2f490f02018-11-01 16:53:36 -04001107 enableContinue = restoreContinue.back();
1108 restoreContinue.pop_back();
John Bauman19bac1e2014-05-06 15:23:49 -04001109 }
1110
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001111 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001112 {
1113 if(!labelBlock[labelIndex])
1114 {
1115 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1116 }
1117
John Bauman19bac1e2014-05-06 15:23:49 -04001118 if(callRetBlock[labelIndex].size() > 1)
1119 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001120 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001121 }
John Bauman89401822014-05-06 15:04:28 -04001122
Nicolas Capens7551ac62016-01-20 17:11:53 -05001123 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001124
1125 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001126 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1127
Nicolas Capens7551ac62016-01-20 17:11:53 -05001128 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001129 }
1130
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001131 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001132 {
John Bauman19bac1e2014-05-06 15:23:49 -04001133 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001134 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001135 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001136 }
John Bauman19bac1e2014-05-06 15:23:49 -04001137 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001138 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001139 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001140 }
1141 else ASSERT(false);
1142 }
1143
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001144 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001145 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001146 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001147
John Bauman19bac1e2014-05-06 15:23:49 -04001148 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001149 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001150 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001151 }
1152
1153 if(!labelBlock[labelIndex])
1154 {
1155 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1156 }
1157
John Bauman19bac1e2014-05-06 15:23:49 -04001158 if(callRetBlock[labelIndex].size() > 1)
1159 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001160 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001161 }
John Bauman89401822014-05-06 15:04:28 -04001162
Nicolas Capens7551ac62016-01-20 17:11:53 -05001163 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001164
John Bauman19bac1e2014-05-06 15:23:49 -04001165 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1166 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1167
Nicolas Capens7551ac62016-01-20 17:11:53 -05001168 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001169 }
1170
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001171 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001172 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001173 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001174
John Bauman19bac1e2014-05-06 15:23:49 -04001175 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001176 {
1177 condition = ~condition;
1178 }
1179
Nicolas Capens7551ac62016-01-20 17:11:53 -05001180 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001181
1182 if(!labelBlock[labelIndex])
1183 {
1184 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1185 }
1186
John Bauman19bac1e2014-05-06 15:23:49 -04001187 if(callRetBlock[labelIndex].size() > 1)
1188 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001189 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001190 }
John Bauman89401822014-05-06 15:04:28 -04001191
Nicolas Capens7551ac62016-01-20 17:11:53 -05001192 enableIndex++;
1193 enableStack[enableIndex] = condition;
1194 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001195
John Bauman19bac1e2014-05-06 15:23:49 -04001196 Bool notAllFalse = SignMask(condition) != 0;
1197 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1198 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001199
Nicolas Capens7551ac62016-01-20 17:11:53 -05001200 enableIndex--;
1201 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001202 }
1203
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001204 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001205 {
1206 ifDepth--;
1207
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001208 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1209 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001210
1211 if(isConditionalIf[ifDepth])
1212 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001213 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001214 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001215
1216 branch(notAllFalse, falseBlock, endBlock);
1217
Nicolas Capens7551ac62016-01-20 17:11:53 -05001218 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001219 }
1220 else
1221 {
1222 Nucleus::createBr(endBlock);
1223 Nucleus::setInsertBlock(falseBlock);
1224 }
1225
1226 ifFalseBlock[ifDepth] = endBlock;
1227
1228 ifDepth++;
1229 }
1230
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001231 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001232 {
1233 ifDepth--;
1234
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001235 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001236
1237 Nucleus::createBr(endBlock);
1238 Nucleus::setInsertBlock(endBlock);
1239
1240 if(isConditionalIf[ifDepth])
1241 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001242 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001243 }
1244 }
1245
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001246 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001247 {
1248 loopRepDepth--;
1249
Nicolas Capens7551ac62016-01-20 17:11:53 -05001250 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001251
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001252 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1253 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001254
1255 Nucleus::createBr(testBlock);
1256 Nucleus::setInsertBlock(endBlock);
1257
Nicolas Capens7551ac62016-01-20 17:11:53 -05001258 loopDepth--;
1259 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001260 }
1261
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001262 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001263 {
1264 loopRepDepth--;
1265
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001266 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1267 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001268
1269 Nucleus::createBr(testBlock);
1270 Nucleus::setInsertBlock(endBlock);
1271
Nicolas Capens7551ac62016-01-20 17:11:53 -05001272 loopDepth--;
1273 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001274 }
1275
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001276 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001277 {
1278 loopRepDepth--;
1279
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001280 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1281 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001282
1283 Nucleus::createBr(testBlock);
1284 Nucleus::setInsertBlock(endBlock);
1285
Nicolas Capens7551ac62016-01-20 17:11:53 -05001286 enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001287 }
1288
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001289 void VertexProgram::ENDSWITCH()
1290 {
1291 loopRepDepth--;
1292
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001293 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001294
Nicolas Capensac6d5052018-01-05 15:34:00 -05001295 Nucleus::createBr(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001296 Nucleus::setInsertBlock(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001297 }
1298
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001299 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001300 {
John Bauman19bac1e2014-05-06 15:23:49 -04001301 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001302 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001303 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001304 }
John Bauman19bac1e2014-05-06 15:23:49 -04001305 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001306 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001307 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001308 }
John Bauman19bac1e2014-05-06 15:23:49 -04001309 else
1310 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001311 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001312 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001313 }
John Bauman89401822014-05-06 15:04:28 -04001314 }
1315
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001316 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001317 {
1318 ASSERT(ifDepth < 24 + 4);
1319
Nicolas Capens7551ac62016-01-20 17:11:53 -05001320 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001321
John Bauman19bac1e2014-05-06 15:23:49 -04001322 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001323 {
John Bauman19bac1e2014-05-06 15:23:49 -04001324 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001325 }
1326
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001327 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1328 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001329
1330 branch(condition, trueBlock, falseBlock);
1331
1332 isConditionalIf[ifDepth] = false;
1333 ifFalseBlock[ifDepth] = falseBlock;
1334
1335 ifDepth++;
1336 }
1337
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001338 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001339 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001340 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001341
John Bauman19bac1e2014-05-06 15:23:49 -04001342 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001343 {
1344 condition = ~condition;
1345 }
1346
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001347 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001348 }
1349
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001350 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001351 {
1352 Int4 condition;
1353
1354 switch(control)
1355 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001356 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1357 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1358 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1359 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1360 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1361 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001362 default:
1363 ASSERT(false);
1364 }
1365
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001366 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001367 }
1368
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001369 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001370 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001371 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001372
Nicolas Capens7551ac62016-01-20 17:11:53 -05001373 enableIndex++;
1374 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001375
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001376 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1377 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001378
John Bauman19bac1e2014-05-06 15:23:49 -04001379 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001380
1381 branch(notAllFalse, trueBlock, falseBlock);
1382
1383 isConditionalIf[ifDepth] = true;
1384 ifFalseBlock[ifDepth] = falseBlock;
1385
1386 ifDepth++;
John Bauman89401822014-05-06 15:04:28 -04001387 }
1388
1389 void VertexProgram::LABEL(int labelIndex)
1390 {
John Bauman19bac1e2014-05-06 15:23:49 -04001391 if(!labelBlock[labelIndex])
1392 {
1393 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1394 }
1395
John Bauman89401822014-05-06 15:04:28 -04001396 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001397 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001398 }
1399
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001400 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001401 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001402 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001403
Nicolas Capens7551ac62016-01-20 17:11:53 -05001404 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1405 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1406 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001407
1408 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001409 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001410 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001411 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001412 }
1413
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001414 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1415 BasicBlock *testBlock = Nucleus::createBasicBlock();
1416 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001417
1418 loopRepTestBlock[loopRepDepth] = testBlock;
1419 loopRepEndBlock[loopRepDepth] = endBlock;
1420
1421 // FIXME: jump(testBlock)
1422 Nucleus::createBr(testBlock);
1423 Nucleus::setInsertBlock(testBlock);
1424
Nicolas Capens7551ac62016-01-20 17:11:53 -05001425 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001426 Nucleus::setInsertBlock(loopBlock);
1427
Nicolas Capens7551ac62016-01-20 17:11:53 -05001428 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001429
John Bauman89401822014-05-06 15:04:28 -04001430 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001431 }
1432
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001433 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001434 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001435 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001436
Nicolas Capens7551ac62016-01-20 17:11:53 -05001437 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1438 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001439
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001440 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1441 BasicBlock *testBlock = Nucleus::createBasicBlock();
1442 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001443
1444 loopRepTestBlock[loopRepDepth] = testBlock;
1445 loopRepEndBlock[loopRepDepth] = endBlock;
1446
1447 // FIXME: jump(testBlock)
1448 Nucleus::createBr(testBlock);
1449 Nucleus::setInsertBlock(testBlock);
1450
Nicolas Capens7551ac62016-01-20 17:11:53 -05001451 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001452 Nucleus::setInsertBlock(loopBlock);
1453
Nicolas Capens7551ac62016-01-20 17:11:53 -05001454 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001455
1456 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001457 }
1458
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001459 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001460 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001461 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001462
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001463 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1464 BasicBlock *testBlock = Nucleus::createBasicBlock();
1465 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001466
John Bauman19bac1e2014-05-06 15:23:49 -04001467 loopRepTestBlock[loopRepDepth] = testBlock;
1468 loopRepEndBlock[loopRepDepth] = endBlock;
1469
Nicolas Capens7551ac62016-01-20 17:11:53 -05001470 Int4 restoreBreak = enableBreak;
Nicolas Capens2f490f02018-11-01 16:53:36 -04001471 restoreContinue.push_back(enableContinue);
John Bauman19bac1e2014-05-06 15:23:49 -04001472
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001473 // TODO: jump(testBlock)
John Bauman19bac1e2014-05-06 15:23:49 -04001474 Nucleus::createBr(testBlock);
1475 Nucleus::setInsertBlock(testBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001476
Nicolas Capensc2534f42016-04-04 11:13:24 -04001477 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001478 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001479 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001480 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens6d123312018-01-08 12:57:52 -05001481 if(shader->containsBreakInstruction()) condition &= enableBreak;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001482 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001483
1484 Bool notAllFalse = SignMask(condition) != 0;
1485 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001486
John Bauman19bac1e2014-05-06 15:23:49 -04001487 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001488 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001489
John Bauman19bac1e2014-05-06 15:23:49 -04001490 Nucleus::setInsertBlock(loopBlock);
1491
1492 loopRepDepth++;
John Bauman19bac1e2014-05-06 15:23:49 -04001493 }
1494
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001495 void VertexProgram::SWITCH()
1496 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001497 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001498
1499 loopRepTestBlock[loopRepDepth] = nullptr;
1500 loopRepEndBlock[loopRepDepth] = endBlock;
1501
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001502 Int4 restoreBreak = enableBreak;
1503
1504 BasicBlock *currentBlock = Nucleus::getInsertBlock();
1505
1506 Nucleus::setInsertBlock(endBlock);
1507 enableBreak = restoreBreak;
1508
1509 Nucleus::setInsertBlock(currentBlock);
1510
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001511 loopRepDepth++;
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001512 }
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 Hetuda163ed2018-01-03 16:36:14 -05001602 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + 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}