blob: b3f81a0ba7e9d9cceb73e2e349eb7ffdfed95caa [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)
Ben Claytond951f192019-02-11 20:59:19 +000027 : VertexRoutine(state, shader),
28 shader(shader),
29 r(shader->indirectAddressableTemporaries),
30 aL(shader->getLimits().loops),
31 increment(shader->getLimits().loops),
32 iteration(shader->getLimits().loops),
33 callStack(shader->getLimits().stack)
John Bauman89401822014-05-06 15:04:28 -040034 {
Ben Claytond951f192019-02-11 20:59:19 +000035 auto limits = shader->getLimits();
Ben Claytond951f192019-02-11 20:59:19 +000036 ifFalseBlock.resize(limits.ifs);
37 loopRepTestBlock.resize(limits.loops);
38 loopRepEndBlock.resize(limits.loops);
Ben Clayton3f48ecb2019-02-21 11:05:27 +000039 labelBlock.resize(limits.maxLabel + 1);
Ben Claytond951f192019-02-11 20:59:19 +000040 isConditionalIf.resize(limits.ifs);
Nicolas Capens7551ac62016-01-20 17:11:53 -050041
42 loopDepth = -1;
43 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
44
Nicolas Capensc62fad32018-01-26 01:55:31 +000045 if(shader->containsBreakInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050046 {
47 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
48 }
49
Nicolas Capensc62fad32018-01-26 01:55:31 +000050 if(shader->containsContinueInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050051 {
52 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
53 }
54
Alexis Hetu02ad0aa2016-08-02 11:18:14 -040055 if(shader->isInstanceIdDeclared())
Nicolas Capens7551ac62016-01-20 17:11:53 -050056 {
57 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
58 }
John Bauman89401822014-05-06 15:04:28 -040059 }
60
61 VertexProgram::~VertexProgram()
62 {
John Bauman89401822014-05-06 15:04:28 -040063 }
64
Nicolas Capens4b743732018-05-28 13:22:07 -040065 void VertexProgram::pipeline(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040066 {
John Bauman89401822014-05-06 15:04:28 -040067 if(!state.preTransformed)
68 {
Alexis Hetu877ddfc2017-07-25 17:48:00 -040069 program(index);
John Bauman89401822014-05-06 15:04:28 -040070 }
71 else
72 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050073 passThrough();
John Bauman89401822014-05-06 15:04:28 -040074 }
75 }
76
Nicolas Capens4b743732018-05-28 13:22:07 -040077 void VertexProgram::program(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040078 {
John Bauman19bac1e2014-05-06 15:23:49 -040079 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040080
Alexis Hetu53ad4af2017-12-06 14:49:07 -050081 unsigned short shaderModel = shader->getShaderModel();
John Bauman89401822014-05-06 15:04:28 -040082
Nicolas Capens7551ac62016-01-20 17:11:53 -050083 enableIndex = 0;
84 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040085
Nicolas Capens4677a5f2014-05-06 16:42:26 -040086 if(shader->containsLeaveInstruction())
87 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050088 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040089 }
90
Alexis Hetu877ddfc2017-07-25 17:48:00 -040091 if(shader->isVertexIdDeclared())
92 {
93 if(state.textureSampling)
94 {
Ben Clayton88816fa2019-05-15 17:08:14 +010095 vertexID = Int4(Int(index));
Alexis Hetu877ddfc2017-07-25 17:48:00 -040096 }
97 else
98 {
99 vertexID = Insert(vertexID, As<Int>(index), 0);
100 vertexID = Insert(vertexID, As<Int>(index + 1), 1);
101 vertexID = Insert(vertexID, As<Int>(index + 2), 2);
102 vertexID = Insert(vertexID, As<Int>(index + 3), 3);
103 }
104 }
105
John Bauman19bac1e2014-05-06 15:23:49 -0400106 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -0500107 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400108 {
John Bauman19bac1e2014-05-06 15:23:49 -0400109 const Shader::Instruction *instruction = shader->getInstruction(i);
110 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400111
John Bauman19bac1e2014-05-06 15:23:49 -0400112 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
113 {
114 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400115
John Bauman19bac1e2014-05-06 15:23:49 -0400116 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
117 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
118 }
119 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500120
Alexis Hetu903e0252014-11-25 14:25:32 -0500121 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400122 {
123 const Shader::Instruction *instruction = shader->getInstruction(i);
124 Shader::Opcode opcode = instruction->opcode;
125
126 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400127 {
128 continue;
129 }
130
John Bauman19bac1e2014-05-06 15:23:49 -0400131 Dst dst = instruction->dst;
132 Src src0 = instruction->src[0];
133 Src src1 = instruction->src[1];
134 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400135 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400136 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400137
John Bauman19bac1e2014-05-06 15:23:49 -0400138 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400139 Control control = instruction->control;
140 bool integer = dst.type == Shader::PARAMETER_ADDR;
141 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400142
John Bauman19bac1e2014-05-06 15:23:49 -0400143 Vector4f d;
144 Vector4f s0;
145 Vector4f s1;
146 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400147 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400148 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400149
Nicolas Capensc2534f42016-04-04 11:13:24 -0400150 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
151 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
152 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
153 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
154 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400155
156 switch(opcode)
157 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500158 case Shader::OPCODE_VS_1_0: break;
159 case Shader::OPCODE_VS_1_1: break;
160 case Shader::OPCODE_VS_2_0: break;
161 case Shader::OPCODE_VS_2_x: break;
162 case Shader::OPCODE_VS_2_sw: break;
163 case Shader::OPCODE_VS_3_0: break;
164 case Shader::OPCODE_VS_3_sw: break;
165 case Shader::OPCODE_DCL: break;
166 case Shader::OPCODE_DEF: break;
167 case Shader::OPCODE_DEFI: break;
168 case Shader::OPCODE_DEFB: break;
169 case Shader::OPCODE_NOP: break;
170 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400171 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500172 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400173 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500174 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
175 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
176 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
177 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
178 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
179 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
180 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
181 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
182 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
183 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
184 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
185 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
186 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
187 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
188 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
189 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
190 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400191 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
192 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
193 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500194 case Shader::OPCODE_ATT: att(d, s0, s1); break;
195 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
196 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500197 case Shader::OPCODE_EXPP: expp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500198 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
199 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400200 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
201 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400202 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500203 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400204 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500205 case Shader::OPCODE_LIT: lit(d, s0); break;
206 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
207 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500208 case Shader::OPCODE_LOGP: logp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500209 case Shader::OPCODE_LOG: log(d, s0, pp); break;
210 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
211 case Shader::OPCODE_STEP: step(d, s0, s1); break;
212 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8ef6d102017-11-09 15:49:09 -0500213 case Shader::OPCODE_ISINF: isinf(d, s0); break;
214 case Shader::OPCODE_ISNAN: isnan(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400215 case Shader::OPCODE_FLOATBITSTOINT:
216 case Shader::OPCODE_FLOATBITSTOUINT:
217 case Shader::OPCODE_INTBITSTOFLOAT:
218 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400219 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
220 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400221 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400222 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
223 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400224 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500225 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
226 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
227 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
228 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
229 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
230 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
231 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
232 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400233 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
234 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500235 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400236 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
237 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500238 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400239 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400240 case Shader::OPCODE_NEG: neg(d, s0); break;
241 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500242 case Shader::OPCODE_F2B: f2b(d, s0); break;
243 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400244 case Shader::OPCODE_F2I: f2i(d, s0); break;
245 case Shader::OPCODE_I2F: i2f(d, s0); break;
246 case Shader::OPCODE_F2U: f2u(d, s0); break;
247 case Shader::OPCODE_U2F: u2f(d, s0); break;
248 case Shader::OPCODE_I2B: i2b(d, s0); break;
249 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500250 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400251 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500252 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
253 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
254 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
255 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
256 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
257 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
258 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400259 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
260 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500261 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400262 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
263 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
264 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500265 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
266 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
267 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
268 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
269 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
270 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
271 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
272 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
273 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
274 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
275 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
276 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
277 case Shader::OPCODE_SGE: step(d, s1, s0); break;
278 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400279 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500280 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
281 case Shader::OPCODE_COS: cos(d, s0, pp); break;
282 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
283 case Shader::OPCODE_TAN: tan(d, s0); break;
284 case Shader::OPCODE_ACOS: acos(d, s0); break;
285 case Shader::OPCODE_ASIN: asin(d, s0); break;
286 case Shader::OPCODE_ATAN: atan(d, s0); break;
287 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
288 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
289 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
290 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
291 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
292 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
293 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
294 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
295 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400296 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500297 case Shader::OPCODE_BREAK: BREAK(); break;
298 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
299 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
300 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
301 case Shader::OPCODE_TEST: TEST(); break;
Nicolas Capens6e8ec332018-11-06 11:56:21 -0500302 case Shader::OPCODE_SCALAR: SCALAR(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500303 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
304 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
305 case Shader::OPCODE_ELSE: ELSE(); break;
306 case Shader::OPCODE_ENDIF: ENDIF(); break;
307 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
308 case Shader::OPCODE_ENDREP: ENDREP(); break;
309 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400310 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500311 case Shader::OPCODE_IF: IF(src0); break;
312 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
313 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
314 case Shader::OPCODE_LOOP: LOOP(src1); break;
315 case Shader::OPCODE_REP: REP(src0); break;
316 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400317 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500318 case Shader::OPCODE_RET: RET(); break;
319 case Shader::OPCODE_LEAVE: LEAVE(); break;
320 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
321 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400322 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500323 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
324 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
325 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
326 case Shader::OPCODE_ALL: all(d.x, s0); break;
327 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400328 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
329 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
330 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
331 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400332 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
333 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500334 case Shader::OPCODE_TEXLDL: TEXLOD(d, s0, src1, s0.w); break;
335 case Shader::OPCODE_TEXLOD: TEXLOD(d, s0, src1, s2.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500336 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400337 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500338 case Shader::OPCODE_TEXLODOFFSET: TEXLODOFFSET(d, s0, src1, s2, s3.x); break;
339 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2.x); break;
340 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCHOFFSET(d, s0, src1, s2, s3.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500341 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500342 case Shader::OPCODE_TEXGRADOFFSET: TEXGRADOFFSET(d, s0, src1, s2, s3, s4); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500343 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
344 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400345 default:
346 ASSERT(false);
347 }
348
John Bauman19bac1e2014-05-06 15:23:49 -0400349 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400350 {
John Bauman19bac1e2014-05-06 15:23:49 -0400351 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400352 {
John Bauman19bac1e2014-05-06 15:23:49 -0400353 if(dst.x) d.x = Max(d.x, Float4(0.0f));
354 if(dst.y) d.y = Max(d.y, Float4(0.0f));
355 if(dst.z) d.z = Max(d.z, Float4(0.0f));
356 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400357
John Bauman19bac1e2014-05-06 15:23:49 -0400358 if(dst.x) d.x = Min(d.x, Float4(1.0f));
359 if(dst.y) d.y = Min(d.y, Float4(1.0f));
360 if(dst.z) d.z = Min(d.z, Float4(1.0f));
361 if(dst.w) d.w = Min(d.w, Float4(1.0f));
362 }
363
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400364 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400365 {
366 Vector4f pDst; // FIXME: Rename
367
368 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400369 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500370 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400371 case Shader::PARAMETER_TEMP:
372 if(dst.rel.type == Shader::PARAMETER_VOID)
373 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500374 if(dst.x) pDst.x = r[dst.index].x;
375 if(dst.y) pDst.y = r[dst.index].y;
376 if(dst.z) pDst.z = r[dst.index].z;
377 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400378 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400379 else if(!dst.rel.dynamic)
380 {
381 Int a = dst.index + relativeAddress(dst.rel);
382
383 if(dst.x) pDst.x = r[a].x;
384 if(dst.y) pDst.y = r[a].y;
385 if(dst.z) pDst.z = r[a].z;
386 if(dst.w) pDst.w = r[a].w;
387 }
John Bauman19bac1e2014-05-06 15:23:49 -0400388 else
389 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400390 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400391
Nicolas Capens4b743732018-05-28 13:22:07 -0400392 if(dst.x) pDst.x = r[a].x;
393 if(dst.y) pDst.y = r[a].y;
394 if(dst.z) pDst.z = r[a].z;
395 if(dst.w) pDst.w = r[a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400396 }
397 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500398 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400399 case Shader::PARAMETER_RASTOUT:
400 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400401 {
402 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500403 if(dst.x) pDst.x = o[Pos].x;
404 if(dst.y) pDst.y = o[Pos].y;
405 if(dst.z) pDst.z = o[Pos].z;
406 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400407 break;
408 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500409 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400410 break;
411 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500412 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400413 break;
414 default:
415 ASSERT(false);
416 }
417 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400418 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400419 if(dst.x) pDst.x = o[C0 + dst.index].x;
420 if(dst.y) pDst.y = o[C0 + dst.index].y;
421 if(dst.z) pDst.z = o[C0 + dst.index].z;
422 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400423 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400424 case Shader::PARAMETER_TEXCRDOUT:
425 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500426 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400427 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500428 if(dst.x) pDst.x = o[T0 + dst.index].x;
429 if(dst.y) pDst.y = o[T0 + dst.index].y;
430 if(dst.z) pDst.z = o[T0 + dst.index].z;
431 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400432 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400433 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
434 {
435 if(dst.x) pDst.x = o[dst.index].x;
436 if(dst.y) pDst.y = o[dst.index].y;
437 if(dst.z) pDst.z = o[dst.index].z;
438 if(dst.w) pDst.w = o[dst.index].w;
439 }
440 else if(!dst.rel.dynamic)
441 {
442 Int a = dst.index + relativeAddress(dst.rel);
443
444 if(dst.x) pDst.x = o[a].x;
445 if(dst.y) pDst.y = o[a].y;
446 if(dst.z) pDst.z = o[a].z;
447 if(dst.w) pDst.w = o[a].w;
448 }
John Bauman89401822014-05-06 15:04:28 -0400449 else
450 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400451 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400452
Nicolas Capens4b743732018-05-28 13:22:07 -0400453 if(dst.x) pDst.x = o[a].x;
454 if(dst.y) pDst.y = o[a].y;
455 if(dst.z) pDst.z = o[a].z;
456 if(dst.w) pDst.w = o[a].w;
John Bauman89401822014-05-06 15:04:28 -0400457 }
458 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500459 case Shader::PARAMETER_LABEL: break;
460 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
461 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400462 default:
463 ASSERT(false);
464 }
465
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500466 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400467
468 Int4 xEnable = enable;
469 Int4 yEnable = enable;
470 Int4 zEnable = enable;
471 Int4 wEnable = enable;
472
473 if(predicate)
474 {
John Bauman19bac1e2014-05-06 15:23:49 -0400475 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400476
Nicolas Capens7551ac62016-01-20 17:11:53 -0500477 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
478 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
479 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
480 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400481
John Bauman19bac1e2014-05-06 15:23:49 -0400482 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400483 {
John Bauman19bac1e2014-05-06 15:23:49 -0400484 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
485 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
486 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
487 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400488 }
489 else
490 {
John Bauman19bac1e2014-05-06 15:23:49 -0400491 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
492 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
493 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
494 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400495 }
496 }
497
John Bauman19bac1e2014-05-06 15:23:49 -0400498 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
499 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
500 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
501 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400502
John Bauman19bac1e2014-05-06 15:23:49 -0400503 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
504 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
505 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
506 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400507 }
508
John Bauman19bac1e2014-05-06 15:23:49 -0400509 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400510 {
John Bauman19bac1e2014-05-06 15:23:49 -0400511 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400512 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400513 case Shader::PARAMETER_TEMP:
514 if(dst.rel.type == Shader::PARAMETER_VOID)
515 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500516 if(dst.x) r[dst.index].x = d.x;
517 if(dst.y) r[dst.index].y = d.y;
518 if(dst.z) r[dst.index].z = d.z;
519 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400520 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400521 else if(!dst.rel.dynamic)
522 {
523 Int a = dst.index + relativeAddress(dst.rel);
524
525 if(dst.x) r[a].x = d.x;
526 if(dst.y) r[a].y = d.y;
527 if(dst.z) r[a].z = d.z;
528 if(dst.w) r[a].w = d.w;
529 }
John Bauman19bac1e2014-05-06 15:23:49 -0400530 else
531 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400532 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400533
Nicolas Capens4b743732018-05-28 13:22:07 -0400534 if(dst.x) r.scatter_x(a, d.x);
535 if(dst.y) r.scatter_y(a, d.y);
536 if(dst.z) r.scatter_z(a, d.z);
537 if(dst.w) r.scatter_w(a, d.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400538 }
John Bauman89401822014-05-06 15:04:28 -0400539 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400540 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500541 if(dst.x) a0.x = d.x;
542 if(dst.y) a0.y = d.y;
543 if(dst.z) a0.z = d.z;
544 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400545 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400546 case Shader::PARAMETER_RASTOUT:
547 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400548 {
549 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500550 if(dst.x) o[Pos].x = d.x;
551 if(dst.y) o[Pos].y = d.y;
552 if(dst.z) o[Pos].z = d.z;
553 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400554 break;
555 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500556 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400557 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500558 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500559 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400560 break;
561 default: ASSERT(false);
562 }
563 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500564 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400565 if(dst.x) o[C0 + dst.index].x = d.x;
566 if(dst.y) o[C0 + dst.index].y = d.y;
567 if(dst.z) o[C0 + dst.index].z = d.z;
568 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400569 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400570 case Shader::PARAMETER_TEXCRDOUT:
571 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500572 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400573 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500574 if(dst.x) o[T0 + dst.index].x = d.x;
575 if(dst.y) o[T0 + dst.index].y = d.y;
576 if(dst.z) o[T0 + dst.index].z = d.z;
577 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400578 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400579 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
580 {
581 if(dst.x) o[dst.index].x = d.x;
582 if(dst.y) o[dst.index].y = d.y;
583 if(dst.z) o[dst.index].z = d.z;
584 if(dst.w) o[dst.index].w = d.w;
585 }
586 else if(!dst.rel.dynamic)
587 {
588 Int a = dst.index + relativeAddress(dst.rel);
589
590 if(dst.x) o[a].x = d.x;
591 if(dst.y) o[a].y = d.y;
592 if(dst.z) o[a].z = d.z;
593 if(dst.w) o[a].w = d.w;
594 }
John Bauman89401822014-05-06 15:04:28 -0400595 else
596 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400597 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400598
Nicolas Capens4b743732018-05-28 13:22:07 -0400599 if(dst.x) o.scatter_x(a, d.x);
600 if(dst.y) o.scatter_y(a, d.y);
601 if(dst.z) o.scatter_z(a, d.z);
602 if(dst.w) o.scatter_w(a, d.w);
John Bauman89401822014-05-06 15:04:28 -0400603 }
604 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500605 case Shader::PARAMETER_LABEL: break;
606 case Shader::PARAMETER_PREDICATE: p0 = d; break;
607 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400608 default:
609 ASSERT(false);
610 }
611 }
612 }
613
John Bauman19bac1e2014-05-06 15:23:49 -0400614 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400615 {
616 Nucleus::setInsertBlock(returnBlock);
617 }
618 }
619
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500620 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400621 {
John Bauman19bac1e2014-05-06 15:23:49 -0400622 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400623 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400624 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400625 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400626 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400627
628 switch(usage)
629 {
630 case 0xFF:
631 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400632 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500633 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400634 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400635 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500636 o[i].x = v[i].x;
637 o[i].y = v[i].y;
638 o[i].z = v[i].z;
639 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400640 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400641 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500642 o[i].x = v[i].x;
643 o[i].y = v[i].y;
644 o[i].z = v[i].z;
645 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400646 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400647 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500648 o[i].x = v[i].x;
649 o[i].y = v[i].y;
650 o[i].z = v[i].z;
651 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400652 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400653 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500654 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400655 break;
656 default:
657 ASSERT(false);
658 }
659 }
660 }
661 else
662 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500663 o[Pos].x = v[PositionT].x;
664 o[Pos].y = v[PositionT].y;
665 o[Pos].z = v[PositionT].z;
666 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400667
668 for(int i = 0; i < 2; i++)
669 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400670 o[C0 + i].x = v[Color0 + i].x;
671 o[C0 + i].y = v[Color0 + i].y;
672 o[C0 + i].z = v[Color0 + i].z;
673 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400674 }
675
676 for(int i = 0; i < 8; i++)
677 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500678 o[T0 + i].x = v[TexCoord0 + i].x;
679 o[T0 + i].y = v[TexCoord0 + i].y;
680 o[T0 + i].z = v[TexCoord0 + i].z;
681 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400682 }
683
Nicolas Capens7551ac62016-01-20 17:11:53 -0500684 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400685 }
686 }
687
Nicolas Capensc2534f42016-04-04 11:13:24 -0400688 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400689 {
John Bauman19bac1e2014-05-06 15:23:49 -0400690 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500691 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400692
John Bauman89401822014-05-06 15:04:28 -0400693 switch(src.type)
694 {
John Bauman19bac1e2014-05-06 15:23:49 -0400695 case Shader::PARAMETER_TEMP:
696 if(src.rel.type == Shader::PARAMETER_VOID)
697 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500698 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400699 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400700 else if(!src.rel.dynamic)
701 {
702 reg = r[i + relativeAddress(src.rel, src.bufferIndex)];
703 }
John Bauman19bac1e2014-05-06 15:23:49 -0400704 else
705 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400706 reg = r[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400707 }
708 break;
709 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500710 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400711 break;
712 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400713 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400714 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500715 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400716 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400717 else if(!src.rel.dynamic)
718 {
719 reg = v[i + relativeAddress(src.rel, src.bufferIndex)];
720 }
John Bauman19bac1e2014-05-06 15:23:49 -0400721 else
722 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400723 reg = v[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400724 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400725 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500726 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400727 case Shader::PARAMETER_FLOAT4LITERAL:
728 reg.x = Float4(src.value[0]);
729 reg.y = Float4(src.value[1]);
730 reg.z = Float4(src.value[2]);
731 reg.w = Float4(src.value[3]);
732 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500733 case Shader::PARAMETER_ADDR: reg = a0; break;
734 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
735 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
736 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
737 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400738 case Shader::PARAMETER_SAMPLER:
739 if(src.rel.type == Shader::PARAMETER_VOID)
740 {
741 reg.x = As<Float4>(Int4(i));
742 }
743 else if(src.rel.type == Shader::PARAMETER_TEMP)
744 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500745 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400746 }
747 return reg;
748 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400749 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400750 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500751 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400752 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400753 else if(!src.rel.dynamic)
754 {
755 reg = o[i + relativeAddress(src.rel, src.bufferIndex)];
756 }
John Bauman19bac1e2014-05-06 15:23:49 -0400757 else
758 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400759 reg = o[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400760 }
761 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400762 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400763 if(src.index == Shader::InstanceIDIndex)
764 {
765 reg.x = As<Float>(instanceID);
766 }
767 else if(src.index == Shader::VertexIDIndex)
768 {
769 reg.x = As<Float4>(vertexID);
770 }
771 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400772 return reg;
John Bauman89401822014-05-06 15:04:28 -0400773 default:
774 ASSERT(false);
775 }
776
John Bauman66b8ab22014-05-06 15:57:45 -0400777 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
778 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
779 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
780 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400781
John Bauman66b8ab22014-05-06 15:57:45 -0400782 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400783
784 switch(src.modifier)
785 {
John Bauman19bac1e2014-05-06 15:23:49 -0400786 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400787 mod.x = x;
788 mod.y = y;
789 mod.z = z;
790 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400791 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400792 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400793 mod.x = -x;
794 mod.y = -y;
795 mod.z = -z;
796 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400797 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400798 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400799 mod.x = Abs(x);
800 mod.y = Abs(y);
801 mod.z = Abs(z);
802 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400803 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400804 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400805 mod.x = -Abs(x);
806 mod.y = -Abs(y);
807 mod.z = -Abs(z);
808 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400809 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400810 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400811 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
812 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
813 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
814 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400815 break;
816 default:
817 ASSERT(false);
818 }
819
820 return mod;
821 }
822
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400823 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
824 {
825 if(bufferIndex == -1)
826 {
827 return data + OFFSET(DrawData, vs.c[index]);
828 }
829 else
830 {
831 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
832 }
833 }
834
Nicolas Capens4b743732018-05-28 13:22:07 -0400835 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int &offset)
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400836 {
837 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
838 }
839
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500840 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400841 {
John Bauman19bac1e2014-05-06 15:23:49 -0400842 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500843 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400844
845 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
846 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400847 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400848
849 c.x = c.x.xxxx;
850 c.y = c.y.yyyy;
851 c.z = c.z.zzzz;
852 c.w = c.w.wwww;
853
Nicolas Capenseafdb222015-05-15 15:24:08 -0400854 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400855 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500856 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400857 {
858 const Shader::Instruction &instruction = *shader->getInstruction(j);
859
860 if(instruction.opcode == Shader::OPCODE_DEF)
861 {
862 if(instruction.dst.index == i)
863 {
864 c.x = Float4(instruction.src[0].value[0]);
865 c.y = Float4(instruction.src[0].value[1]);
866 c.z = Float4(instruction.src[0].value[2]);
867 c.w = Float4(instruction.src[0].value[3]);
868
869 break;
870 }
871 }
872 }
873 }
874 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400875 else if(!src.rel.dynamic || src.rel.type == Shader::PARAMETER_LOOP)
John Bauman19bac1e2014-05-06 15:23:49 -0400876 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400877 Int a = relativeAddress(src.rel, src.bufferIndex);
John Bauman19bac1e2014-05-06 15:23:49 -0400878
Nicolas Capens4b743732018-05-28 13:22:07 -0400879 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
John Bauman19bac1e2014-05-06 15:23:49 -0400880
881 c.x = c.x.xxxx;
882 c.y = c.y.yyyy;
883 c.z = c.z.zzzz;
884 c.w = c.w.wwww;
885 }
886 else
887 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400888 int component = src.rel.swizzle & 0x03;
889 Float4 a;
890
891 switch(src.rel.type)
John Bauman19bac1e2014-05-06 15:23:49 -0400892 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400893 case Shader::PARAMETER_ADDR: a = a0[component]; break;
894 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
895 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
896 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
897 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
898 case Shader::PARAMETER_MISCTYPE:
899 switch(src.rel.index)
John Bauman19bac1e2014-05-06 15:23:49 -0400900 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400901 case Shader::InstanceIDIndex: a = As<Float4>(Int4(instanceID)); break;
902 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400903 default: ASSERT(false);
904 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400905 break;
906 default: ASSERT(false);
John Bauman19bac1e2014-05-06 15:23:49 -0400907 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400908
909 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
910
911 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
912
913 Int index0 = Extract(index, 0);
914 Int index1 = Extract(index, 1);
915 Int index2 = Extract(index, 2);
916 Int index3 = Extract(index, 3);
917
918 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
919 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
920 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
921 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
922
923 transpose4x4(c.x, c.y, c.z, c.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400924 }
925
926 return c;
927 }
928
Nicolas Capens4b743732018-05-28 13:22:07 -0400929 Int VertexProgram::relativeAddress(const Shader::Relative &rel, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400930 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400931 ASSERT(!rel.dynamic);
John Bauman19bac1e2014-05-06 15:23:49 -0400932
Nicolas Capens4b743732018-05-28 13:22:07 -0400933 if(rel.type == Shader::PARAMETER_TEMP)
John Bauman19bac1e2014-05-06 15:23:49 -0400934 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400935 return As<Int>(Extract(r[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400936 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400937 else if(rel.type == Shader::PARAMETER_INPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400938 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400939 return As<Int>(Extract(v[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400940 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400941 else if(rel.type == Shader::PARAMETER_OUTPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400942 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400943 return As<Int>(Extract(o[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400944 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400945 else if(rel.type == Shader::PARAMETER_CONST)
John Bauman19bac1e2014-05-06 15:23:49 -0400946 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400947 return *Pointer<Int>(uniformAddress(bufferIndex, rel.index)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400948 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400949 else if(rel.type == Shader::PARAMETER_LOOP)
Nicolas Capens907700d2016-01-20 17:09:28 -0500950 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500951 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500952 }
John Bauman19bac1e2014-05-06 15:23:49 -0400953 else ASSERT(false);
954
955 return 0;
956 }
957
Nicolas Capens4b743732018-05-28 13:22:07 -0400958 Int4 VertexProgram::dynamicAddress(const Shader::Relative &rel)
959 {
960 int component = rel.swizzle & 0x03;
961 Float4 a;
962
963 switch(rel.type)
964 {
965 case Shader::PARAMETER_ADDR: a = a0[component]; break;
966 case Shader::PARAMETER_TEMP: a = r[rel.index][component]; break;
967 case Shader::PARAMETER_INPUT: a = v[rel.index][component]; break;
968 case Shader::PARAMETER_OUTPUT: a = o[rel.index][component]; break;
969 case Shader::PARAMETER_MISCTYPE:
970 switch(rel.index)
971 {
972 case Shader::InstanceIDIndex: a = As<Float>(instanceID); break;
973 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
974 default: ASSERT(false);
975 }
976 break;
977 default: ASSERT(false);
978 }
979
980 return As<Int4>(a) * Int4(rel.scale);
981 }
982
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500983 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400984 {
Nicolas Capens6e8ec332018-11-06 11:56:21 -0500985 if(scalar)
986 {
987 return Int4(0xFFFFFFFF);
988 }
989
Alexis Hetu48d47a42019-01-10 14:04:26 -0500990 Int4 enable = instruction->analysisBranch ? Int4(enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500991
Nicolas Capens8a587712018-10-20 14:17:49 -0400992 if(shader->containsBreakInstruction() && instruction->analysisBreak)
John Bauman19bac1e2014-05-06 15:23:49 -0400993 {
Nicolas Capens8a587712018-10-20 14:17:49 -0400994 enable &= enableBreak;
995 }
John Bauman19bac1e2014-05-06 15:23:49 -0400996
Nicolas Capens8a587712018-10-20 14:17:49 -0400997 if(shader->containsContinueInstruction() && instruction->analysisContinue)
998 {
999 enable &= enableContinue;
1000 }
John Bauman19bac1e2014-05-06 15:23:49 -04001001
Nicolas Capens8a587712018-10-20 14:17:49 -04001002 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
1003 {
1004 enable &= enableLeave;
John Bauman19bac1e2014-05-06 15:23:49 -04001005 }
1006
1007 return enable;
1008 }
1009
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001010 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001011 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001012 Vector4f row0 = fetchRegister(src1, 0);
1013 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -04001014
1015 dst.x = dot3(src0, row0);
1016 dst.y = dot3(src0, row1);
1017 }
1018
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001019 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001020 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001021 Vector4f row0 = fetchRegister(src1, 0);
1022 Vector4f row1 = fetchRegister(src1, 1);
1023 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001024
1025 dst.x = dot3(src0, row0);
1026 dst.y = dot3(src0, row1);
1027 dst.z = dot3(src0, row2);
1028 }
1029
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001030 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001031 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001032 Vector4f row0 = fetchRegister(src1, 0);
1033 Vector4f row1 = fetchRegister(src1, 1);
1034 Vector4f row2 = fetchRegister(src1, 2);
1035 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001036
1037 dst.x = dot3(src0, row0);
1038 dst.y = dot3(src0, row1);
1039 dst.z = dot3(src0, row2);
1040 dst.w = dot3(src0, row3);
1041 }
1042
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001043 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001044 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001045 Vector4f row0 = fetchRegister(src1, 0);
1046 Vector4f row1 = fetchRegister(src1, 1);
1047 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001048
1049 dst.x = dot4(src0, row0);
1050 dst.y = dot4(src0, row1);
1051 dst.z = dot4(src0, row2);
1052 }
1053
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001054 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001055 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001056 Vector4f row0 = fetchRegister(src1, 0);
1057 Vector4f row1 = fetchRegister(src1, 1);
1058 Vector4f row2 = fetchRegister(src1, 2);
1059 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001060
1061 dst.x = dot4(src0, row0);
1062 dst.y = dot4(src0, row1);
1063 dst.z = dot4(src0, row2);
1064 dst.w = dot4(src0, row3);
1065 }
1066
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001067 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001068 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001069 enableBreak = enableBreak & ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001070 }
1071
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001072 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001073 {
1074 Int4 condition;
1075
1076 switch(control)
1077 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001078 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1079 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1080 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1081 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1082 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1083 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001084 default:
1085 ASSERT(false);
1086 }
1087
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001088 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001089 }
1090
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001091 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001092 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001093 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001094
John Bauman19bac1e2014-05-06 15:23:49 -04001095 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001096 {
1097 condition = ~condition;
1098 }
1099
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001100 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001101 }
1102
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001103 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001104 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001105 condition &= enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001106
Nicolas Capens7551ac62016-01-20 17:11:53 -05001107 enableBreak = enableBreak & ~condition;
John Bauman89401822014-05-06 15:04:28 -04001108 }
1109
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001110 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001111 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001112 enableContinue = enableContinue & ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman19bac1e2014-05-06 15:23:49 -04001113 }
1114
1115 void VertexProgram::TEST()
1116 {
Nicolas Capens2f490f02018-11-01 16:53:36 -04001117 enableContinue = restoreContinue.back();
1118 restoreContinue.pop_back();
John Bauman19bac1e2014-05-06 15:23:49 -04001119 }
1120
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001121 void VertexProgram::SCALAR()
1122 {
1123 scalar = true;
1124 }
1125
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001126 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001127 {
1128 if(!labelBlock[labelIndex])
1129 {
1130 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1131 }
1132
John Bauman19bac1e2014-05-06 15:23:49 -04001133 if(callRetBlock[labelIndex].size() > 1)
1134 {
Nicolas Capens0f34a982019-02-11 16:02:23 -05001135 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001136 }
John Bauman89401822014-05-06 15:04:28 -04001137
Nicolas Capens7551ac62016-01-20 17:11:53 -05001138 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001139
1140 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001141 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1142
Nicolas Capens7551ac62016-01-20 17:11:53 -05001143 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001144 }
1145
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001146 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001147 {
John Bauman19bac1e2014-05-06 15:23:49 -04001148 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001149 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001150 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001151 }
John Bauman19bac1e2014-05-06 15:23:49 -04001152 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001153 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001154 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001155 }
1156 else ASSERT(false);
1157 }
1158
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001159 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001160 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001161 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001162
John Bauman19bac1e2014-05-06 15:23:49 -04001163 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001164 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001165 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001166 }
1167
1168 if(!labelBlock[labelIndex])
1169 {
1170 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1171 }
1172
John Bauman19bac1e2014-05-06 15:23:49 -04001173 if(callRetBlock[labelIndex].size() > 1)
1174 {
Nicolas Capens0f34a982019-02-11 16:02:23 -05001175 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001176 }
John Bauman89401822014-05-06 15:04:28 -04001177
Nicolas Capens7551ac62016-01-20 17:11:53 -05001178 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001179
John Bauman19bac1e2014-05-06 15:23:49 -04001180 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1181 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1182
Nicolas Capens7551ac62016-01-20 17:11:53 -05001183 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001184 }
1185
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001186 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001187 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001188 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001189
John Bauman19bac1e2014-05-06 15:23:49 -04001190 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001191 {
1192 condition = ~condition;
1193 }
1194
Alexis Hetu48d47a42019-01-10 14:04:26 -05001195 condition &= enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001196
1197 if(!labelBlock[labelIndex])
1198 {
1199 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1200 }
1201
John Bauman19bac1e2014-05-06 15:23:49 -04001202 if(callRetBlock[labelIndex].size() > 1)
1203 {
Nicolas Capens0f34a982019-02-11 16:02:23 -05001204 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001205 }
John Bauman89401822014-05-06 15:04:28 -04001206
Nicolas Capens7551ac62016-01-20 17:11:53 -05001207 enableIndex++;
Alexis Hetu48d47a42019-01-10 14:04:26 -05001208 enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] = condition;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001209 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001210
John Bauman19bac1e2014-05-06 15:23:49 -04001211 Bool notAllFalse = SignMask(condition) != 0;
1212 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1213 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001214
Nicolas Capens7551ac62016-01-20 17:11:53 -05001215 enableIndex--;
1216 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001217 }
1218
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001219 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001220 {
1221 ifDepth--;
1222
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001223 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1224 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001225
1226 if(isConditionalIf[ifDepth])
1227 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001228 Int4 condition = ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] & enableStack[Min(enableIndex - 1, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman19bac1e2014-05-06 15:23:49 -04001229 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001230
1231 branch(notAllFalse, falseBlock, endBlock);
1232
Alexis Hetu48d47a42019-01-10 14:04:26 -05001233 enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] = ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] & enableStack[Min(enableIndex - 1, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001234 }
1235 else
1236 {
1237 Nucleus::createBr(endBlock);
1238 Nucleus::setInsertBlock(falseBlock);
1239 }
1240
1241 ifFalseBlock[ifDepth] = endBlock;
1242
1243 ifDepth++;
1244 }
1245
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001246 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001247 {
1248 ifDepth--;
1249
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001250 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001251
1252 Nucleus::createBr(endBlock);
1253 Nucleus::setInsertBlock(endBlock);
1254
1255 if(isConditionalIf[ifDepth])
1256 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001257 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001258 }
1259 }
1260
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001261 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001262 {
1263 loopRepDepth--;
1264
Nicolas Capens7551ac62016-01-20 17:11:53 -05001265 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001266
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001267 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1268 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001269
1270 Nucleus::createBr(testBlock);
1271 Nucleus::setInsertBlock(endBlock);
1272
Nicolas Capens7551ac62016-01-20 17:11:53 -05001273 loopDepth--;
1274 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001275 }
1276
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001277 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001278 {
1279 loopRepDepth--;
1280
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001281 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1282 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001283
1284 Nucleus::createBr(testBlock);
1285 Nucleus::setInsertBlock(endBlock);
1286
Nicolas Capens7551ac62016-01-20 17:11:53 -05001287 loopDepth--;
1288 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001289 }
1290
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001291 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001292 {
1293 loopRepDepth--;
1294
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001295 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1296 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001297
1298 Nucleus::createBr(testBlock);
1299 Nucleus::setInsertBlock(endBlock);
1300
Nicolas Capens7551ac62016-01-20 17:11:53 -05001301 enableIndex--;
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001302 scalar = false;
John Bauman19bac1e2014-05-06 15:23:49 -04001303 }
1304
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001305 void VertexProgram::ENDSWITCH()
1306 {
1307 loopRepDepth--;
1308
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001309 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001310
Nicolas Capensac6d5052018-01-05 15:34:00 -05001311 Nucleus::createBr(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001312 Nucleus::setInsertBlock(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001313 }
1314
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001315 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001316 {
John Bauman19bac1e2014-05-06 15:23:49 -04001317 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001318 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001319 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001320 }
John Bauman19bac1e2014-05-06 15:23:49 -04001321 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001322 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001323 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001324 }
John Bauman19bac1e2014-05-06 15:23:49 -04001325 else
1326 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001327 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001328 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001329 }
John Bauman89401822014-05-06 15:04:28 -04001330 }
1331
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001332 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001333 {
1334 ASSERT(ifDepth < 24 + 4);
1335
Nicolas Capens7551ac62016-01-20 17:11:53 -05001336 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001337
John Bauman19bac1e2014-05-06 15:23:49 -04001338 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001339 {
John Bauman19bac1e2014-05-06 15:23:49 -04001340 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001341 }
1342
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001343 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1344 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001345
1346 branch(condition, trueBlock, falseBlock);
1347
1348 isConditionalIf[ifDepth] = false;
1349 ifFalseBlock[ifDepth] = falseBlock;
1350
1351 ifDepth++;
1352 }
1353
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001354 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001355 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001356 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001357
John Bauman19bac1e2014-05-06 15:23:49 -04001358 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001359 {
1360 condition = ~condition;
1361 }
1362
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001363 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001364 }
1365
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001366 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001367 {
1368 Int4 condition;
1369
1370 switch(control)
1371 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001372 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1373 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1374 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1375 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1376 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1377 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001378 default:
1379 ASSERT(false);
1380 }
1381
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001382 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001383 }
1384
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001385 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001386 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001387 condition &= enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001388
Nicolas Capens7551ac62016-01-20 17:11:53 -05001389 enableIndex++;
Alexis Hetu48d47a42019-01-10 14:04:26 -05001390 enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] = condition;
John Bauman89401822014-05-06 15:04:28 -04001391
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001392 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1393 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001394
John Bauman19bac1e2014-05-06 15:23:49 -04001395 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001396
1397 branch(notAllFalse, trueBlock, falseBlock);
1398
1399 isConditionalIf[ifDepth] = true;
1400 ifFalseBlock[ifDepth] = falseBlock;
1401
1402 ifDepth++;
John Bauman89401822014-05-06 15:04:28 -04001403 }
1404
1405 void VertexProgram::LABEL(int labelIndex)
1406 {
John Bauman19bac1e2014-05-06 15:23:49 -04001407 if(!labelBlock[labelIndex])
1408 {
1409 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1410 }
1411
John Bauman89401822014-05-06 15:04:28 -04001412 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001413 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001414 }
1415
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001416 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001417 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001418 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001419
Nicolas Capens7551ac62016-01-20 17:11:53 -05001420 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1421 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1422 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001423
1424 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001425 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001426 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001427 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001428 }
1429
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: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001445
John Bauman89401822014-05-06 15:04:28 -04001446 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001447 }
1448
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001449 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001450 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001451 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001452
Nicolas Capens7551ac62016-01-20 17:11:53 -05001453 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1454 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001455
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001456 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1457 BasicBlock *testBlock = Nucleus::createBasicBlock();
1458 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001459
1460 loopRepTestBlock[loopRepDepth] = testBlock;
1461 loopRepEndBlock[loopRepDepth] = endBlock;
1462
1463 // FIXME: jump(testBlock)
1464 Nucleus::createBr(testBlock);
1465 Nucleus::setInsertBlock(testBlock);
1466
Nicolas Capens7551ac62016-01-20 17:11:53 -05001467 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001468 Nucleus::setInsertBlock(loopBlock);
1469
Nicolas Capens7551ac62016-01-20 17:11:53 -05001470 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001471
1472 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001473 }
1474
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001475 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001476 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001477 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001478
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001479 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1480 BasicBlock *testBlock = Nucleus::createBasicBlock();
1481 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001482
John Bauman19bac1e2014-05-06 15:23:49 -04001483 loopRepTestBlock[loopRepDepth] = testBlock;
1484 loopRepEndBlock[loopRepDepth] = endBlock;
1485
Nicolas Capens7551ac62016-01-20 17:11:53 -05001486 Int4 restoreBreak = enableBreak;
Nicolas Capens2f490f02018-11-01 16:53:36 -04001487 restoreContinue.push_back(enableContinue);
John Bauman19bac1e2014-05-06 15:23:49 -04001488
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001489 // TODO: jump(testBlock)
John Bauman19bac1e2014-05-06 15:23:49 -04001490 Nucleus::createBr(testBlock);
1491 Nucleus::setInsertBlock(testBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001492
Nicolas Capensc2534f42016-04-04 11:13:24 -04001493 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001494 Int4 condition = As<Int4>(src.x);
Alexis Hetu48d47a42019-01-10 14:04:26 -05001495 condition &= enableStack[Min(enableIndex - 1, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001496 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens6d123312018-01-08 12:57:52 -05001497 if(shader->containsBreakInstruction()) condition &= enableBreak;
Alexis Hetu48d47a42019-01-10 14:04:26 -05001498 enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001499
1500 Bool notAllFalse = SignMask(condition) != 0;
1501 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001502
John Bauman19bac1e2014-05-06 15:23:49 -04001503 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001504 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001505
John Bauman19bac1e2014-05-06 15:23:49 -04001506 Nucleus::setInsertBlock(loopBlock);
1507
1508 loopRepDepth++;
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001509 scalar = false;
John Bauman19bac1e2014-05-06 15:23:49 -04001510 }
1511
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001512 void VertexProgram::SWITCH()
1513 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001514 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001515
1516 loopRepTestBlock[loopRepDepth] = nullptr;
1517 loopRepEndBlock[loopRepDepth] = endBlock;
1518
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001519 Int4 restoreBreak = enableBreak;
1520
1521 BasicBlock *currentBlock = Nucleus::getInsertBlock();
1522
1523 Nucleus::setInsertBlock(endBlock);
1524 enableBreak = restoreBreak;
1525
1526 Nucleus::setInsertBlock(currentBlock);
1527
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001528 loopRepDepth++;
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001529 }
1530
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001531 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001532 {
John Bauman19bac1e2014-05-06 15:23:49 -04001533 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001534 {
1535 returnBlock = Nucleus::createBasicBlock();
1536 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001537 }
1538 else
1539 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001540 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001541
John Bauman19bac1e2014-05-06 15:23:49 -04001542 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001543 {
John Bauman19bac1e2014-05-06 15:23:49 -04001544 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001545 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001546
Nicolas Capens19336542016-09-26 10:32:29 -04001547 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001548 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001549
1550 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1551 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001552 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001553 }
1554 }
1555 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1556 {
1557 Nucleus::createBr(callRetBlock[currentLabel][0]);
1558 }
1559 else // Function isn't called
1560 {
1561 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001562 }
1563
1564 Nucleus::setInsertBlock(unreachableBlock);
1565 Nucleus::createUnreachable();
1566 }
1567 }
1568
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001569 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001570 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001571 enableLeave = enableLeave & ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001572
John Bauman19bac1e2014-05-06 15:23:49 -04001573 // FIXME: Return from function if all instances left
1574 // FIXME: Use enableLeave in other control-flow constructs
1575 }
John Bauman89401822014-05-06 15:04:28 -04001576
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001577 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001578 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001579 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001580 }
1581
Nicolas Capensa0b57832017-11-07 13:07:53 -05001582 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001583 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001584 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001585 }
1586
Nicolas Capensa0b57832017-11-07 13:07:53 -05001587 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001588 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001589 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001590 }
1591
Nicolas Capensa0b57832017-11-07 13:07:53 -05001592 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001593 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001594 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001595 }
1596
Nicolas Capensa0b57832017-11-07 13:07:53 -05001597 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001598 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001599 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001600 }
1601
Nicolas Capensa0b57832017-11-07 13:07:53 -05001602 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001603 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001604 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001605 }
1606
Nicolas Capensa0b57832017-11-07 13:07:53 -05001607 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001608 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001609 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1610 }
1611
1612 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1613 {
1614 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001615 }
1616
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001617 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001618 {
Nicolas Capens79d0e562018-11-27 17:07:49 -05001619 bool uniformSampler = (src1.type == Shader::PARAMETER_SAMPLER && src1.rel.type == Shader::PARAMETER_VOID);
Nicolas Capensa9727582018-11-28 17:33:24 -05001620 Int offset = uniformSampler ? src1.index * sizeof(Texture) : As<Int>(Float(fetchRegister(src1).x.x)) * sizeof(Texture);
1621 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + offset;
Nicolas Capens79d0e562018-11-27 17:07:49 -05001622
Nicolas Capens89a218b2017-11-07 13:05:20 -05001623 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001624 }
1625
Nicolas Capensa0b57832017-11-07 13:07:53 -05001626 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 -04001627 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001628 Vector4f tmp;
1629
John Bauman19bac1e2014-05-06 15:23:49 -04001630 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1631 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001632 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001633 }
1634 else
1635 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001636 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001637
Nicolas Capensc2534f42016-04-04 11:13:24 -04001638 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001639 {
1640 if(shader->usesSampler(i))
1641 {
1642 If(index == i)
1643 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001644 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001645 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1646 }
1647 }
1648 }
1649 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001650
Nicolas Capens89a218b2017-11-07 13:05:20 -05001651 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001652 c.x = tmp[(s.swizzle >> 0) & 0x3];
1653 c.y = tmp[(s.swizzle >> 2) & 0x3];
1654 c.z = tmp[(s.swizzle >> 4) & 0x3];
1655 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001656
1657 return c;
1658 }
1659
Nicolas Capensa0b57832017-11-07 13:07:53 -05001660 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001661 {
1662 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001663 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 -04001664 }
John Bauman89401822014-05-06 15:04:28 -04001665}