blob: c3f5de89e1c1b475beb500c634cf9c42037d6b8d [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:
Nicolas Capensb1d3bbf2019-08-31 01:32:52 -0400728 // This is used for all literal types, and since Reactor doesn't guarantee
729 // preserving the bit pattern of float constants, we must construct them
730 // as integer constants and bitcast.
731 reg.x = As<Float4>(Int4(src.integer[0]));
732 reg.y = As<Float4>(Int4(src.integer[1]));
733 reg.z = As<Float4>(Int4(src.integer[2]));
734 reg.w = As<Float4>(Int4(src.integer[3]));
John Bauman19bac1e2014-05-06 15:23:49 -0400735 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500736 case Shader::PARAMETER_ADDR: reg = a0; break;
737 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
738 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
739 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
740 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400741 case Shader::PARAMETER_SAMPLER:
742 if(src.rel.type == Shader::PARAMETER_VOID)
743 {
744 reg.x = As<Float4>(Int4(i));
745 }
746 else if(src.rel.type == Shader::PARAMETER_TEMP)
747 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500748 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400749 }
750 return reg;
751 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400752 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400753 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500754 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400755 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400756 else if(!src.rel.dynamic)
757 {
758 reg = o[i + relativeAddress(src.rel, src.bufferIndex)];
759 }
John Bauman19bac1e2014-05-06 15:23:49 -0400760 else
761 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400762 reg = o[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400763 }
764 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400765 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400766 if(src.index == Shader::InstanceIDIndex)
767 {
768 reg.x = As<Float>(instanceID);
769 }
770 else if(src.index == Shader::VertexIDIndex)
771 {
772 reg.x = As<Float4>(vertexID);
773 }
774 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400775 return reg;
John Bauman89401822014-05-06 15:04:28 -0400776 default:
777 ASSERT(false);
778 }
779
John Bauman66b8ab22014-05-06 15:57:45 -0400780 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
781 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
782 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
783 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400784
John Bauman66b8ab22014-05-06 15:57:45 -0400785 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400786
787 switch(src.modifier)
788 {
John Bauman19bac1e2014-05-06 15:23:49 -0400789 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400790 mod.x = x;
791 mod.y = y;
792 mod.z = z;
793 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400794 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400795 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400796 mod.x = -x;
797 mod.y = -y;
798 mod.z = -z;
799 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400800 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400801 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400802 mod.x = Abs(x);
803 mod.y = Abs(y);
804 mod.z = Abs(z);
805 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400806 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400807 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400808 mod.x = -Abs(x);
809 mod.y = -Abs(y);
810 mod.z = -Abs(z);
811 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400812 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400813 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400814 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
815 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
816 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
817 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400818 break;
819 default:
820 ASSERT(false);
821 }
822
823 return mod;
824 }
825
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400826 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
827 {
828 if(bufferIndex == -1)
829 {
830 return data + OFFSET(DrawData, vs.c[index]);
831 }
832 else
833 {
834 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
835 }
836 }
837
Nicolas Capens4b743732018-05-28 13:22:07 -0400838 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int &offset)
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400839 {
840 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
841 }
842
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500843 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400844 {
John Bauman19bac1e2014-05-06 15:23:49 -0400845 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500846 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400847
848 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
849 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400850 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400851
852 c.x = c.x.xxxx;
853 c.y = c.y.yyyy;
854 c.z = c.z.zzzz;
855 c.w = c.w.wwww;
856
Nicolas Capenseafdb222015-05-15 15:24:08 -0400857 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400858 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500859 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400860 {
861 const Shader::Instruction &instruction = *shader->getInstruction(j);
862
863 if(instruction.opcode == Shader::OPCODE_DEF)
864 {
865 if(instruction.dst.index == i)
866 {
867 c.x = Float4(instruction.src[0].value[0]);
868 c.y = Float4(instruction.src[0].value[1]);
869 c.z = Float4(instruction.src[0].value[2]);
870 c.w = Float4(instruction.src[0].value[3]);
871
872 break;
873 }
874 }
875 }
876 }
877 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400878 else if(!src.rel.dynamic || src.rel.type == Shader::PARAMETER_LOOP)
John Bauman19bac1e2014-05-06 15:23:49 -0400879 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400880 Int a = relativeAddress(src.rel, src.bufferIndex);
John Bauman19bac1e2014-05-06 15:23:49 -0400881
Nicolas Capens4b743732018-05-28 13:22:07 -0400882 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
John Bauman19bac1e2014-05-06 15:23:49 -0400883
884 c.x = c.x.xxxx;
885 c.y = c.y.yyyy;
886 c.z = c.z.zzzz;
887 c.w = c.w.wwww;
888 }
889 else
890 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400891 int component = src.rel.swizzle & 0x03;
892 Float4 a;
893
894 switch(src.rel.type)
John Bauman19bac1e2014-05-06 15:23:49 -0400895 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400896 case Shader::PARAMETER_ADDR: a = a0[component]; break;
897 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
898 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
899 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
900 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
901 case Shader::PARAMETER_MISCTYPE:
902 switch(src.rel.index)
John Bauman19bac1e2014-05-06 15:23:49 -0400903 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400904 case Shader::InstanceIDIndex: a = As<Float4>(Int4(instanceID)); break;
905 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400906 default: ASSERT(false);
907 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400908 break;
909 default: ASSERT(false);
John Bauman19bac1e2014-05-06 15:23:49 -0400910 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400911
912 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
913
Chris Forbesac3a67c2019-11-26 11:49:09 -0800914 if (src.bufferIndex == -1)
915 {
916 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
917 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400918
919 Int index0 = Extract(index, 0);
920 Int index1 = Extract(index, 1);
921 Int index2 = Extract(index, 2);
922 Int index3 = Extract(index, 3);
923
924 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
925 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
926 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
927 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
928
929 transpose4x4(c.x, c.y, c.z, c.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400930 }
931
932 return c;
933 }
934
Nicolas Capens4b743732018-05-28 13:22:07 -0400935 Int VertexProgram::relativeAddress(const Shader::Relative &rel, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400936 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400937 ASSERT(!rel.dynamic);
John Bauman19bac1e2014-05-06 15:23:49 -0400938
Nicolas Capens4b743732018-05-28 13:22:07 -0400939 if(rel.type == Shader::PARAMETER_TEMP)
John Bauman19bac1e2014-05-06 15:23:49 -0400940 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400941 return As<Int>(Extract(r[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400942 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400943 else if(rel.type == Shader::PARAMETER_INPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400944 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400945 return As<Int>(Extract(v[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400946 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400947 else if(rel.type == Shader::PARAMETER_OUTPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400948 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400949 return As<Int>(Extract(o[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400950 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400951 else if(rel.type == Shader::PARAMETER_CONST)
John Bauman19bac1e2014-05-06 15:23:49 -0400952 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400953 return *Pointer<Int>(uniformAddress(bufferIndex, rel.index)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400954 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400955 else if(rel.type == Shader::PARAMETER_LOOP)
Nicolas Capens907700d2016-01-20 17:09:28 -0500956 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500957 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500958 }
John Bauman19bac1e2014-05-06 15:23:49 -0400959 else ASSERT(false);
960
961 return 0;
962 }
963
Nicolas Capens4b743732018-05-28 13:22:07 -0400964 Int4 VertexProgram::dynamicAddress(const Shader::Relative &rel)
965 {
966 int component = rel.swizzle & 0x03;
967 Float4 a;
968
969 switch(rel.type)
970 {
971 case Shader::PARAMETER_ADDR: a = a0[component]; break;
972 case Shader::PARAMETER_TEMP: a = r[rel.index][component]; break;
973 case Shader::PARAMETER_INPUT: a = v[rel.index][component]; break;
974 case Shader::PARAMETER_OUTPUT: a = o[rel.index][component]; break;
975 case Shader::PARAMETER_MISCTYPE:
976 switch(rel.index)
977 {
978 case Shader::InstanceIDIndex: a = As<Float>(instanceID); break;
979 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
980 default: ASSERT(false);
981 }
982 break;
983 default: ASSERT(false);
984 }
985
986 return As<Int4>(a) * Int4(rel.scale);
987 }
988
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500989 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400990 {
Nicolas Capens6e8ec332018-11-06 11:56:21 -0500991 if(scalar)
992 {
993 return Int4(0xFFFFFFFF);
994 }
995
Alexis Hetu48d47a42019-01-10 14:04:26 -0500996 Int4 enable = instruction->analysisBranch ? Int4(enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500997
Nicolas Capens8a587712018-10-20 14:17:49 -0400998 if(shader->containsBreakInstruction() && instruction->analysisBreak)
John Bauman19bac1e2014-05-06 15:23:49 -0400999 {
Nicolas Capens8a587712018-10-20 14:17:49 -04001000 enable &= enableBreak;
1001 }
John Bauman19bac1e2014-05-06 15:23:49 -04001002
Nicolas Capens8a587712018-10-20 14:17:49 -04001003 if(shader->containsContinueInstruction() && instruction->analysisContinue)
1004 {
1005 enable &= enableContinue;
1006 }
John Bauman19bac1e2014-05-06 15:23:49 -04001007
Nicolas Capens8a587712018-10-20 14:17:49 -04001008 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
1009 {
1010 enable &= enableLeave;
John Bauman19bac1e2014-05-06 15:23:49 -04001011 }
1012
1013 return enable;
1014 }
1015
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001016 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001017 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001018 Vector4f row0 = fetchRegister(src1, 0);
1019 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -04001020
1021 dst.x = dot3(src0, row0);
1022 dst.y = dot3(src0, row1);
1023 }
1024
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001025 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001026 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001027 Vector4f row0 = fetchRegister(src1, 0);
1028 Vector4f row1 = fetchRegister(src1, 1);
1029 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001030
1031 dst.x = dot3(src0, row0);
1032 dst.y = dot3(src0, row1);
1033 dst.z = dot3(src0, row2);
1034 }
1035
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001036 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001037 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001038 Vector4f row0 = fetchRegister(src1, 0);
1039 Vector4f row1 = fetchRegister(src1, 1);
1040 Vector4f row2 = fetchRegister(src1, 2);
1041 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001042
1043 dst.x = dot3(src0, row0);
1044 dst.y = dot3(src0, row1);
1045 dst.z = dot3(src0, row2);
1046 dst.w = dot3(src0, row3);
1047 }
1048
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001049 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001050 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001051 Vector4f row0 = fetchRegister(src1, 0);
1052 Vector4f row1 = fetchRegister(src1, 1);
1053 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001054
1055 dst.x = dot4(src0, row0);
1056 dst.y = dot4(src0, row1);
1057 dst.z = dot4(src0, row2);
1058 }
1059
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001060 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001061 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001062 Vector4f row0 = fetchRegister(src1, 0);
1063 Vector4f row1 = fetchRegister(src1, 1);
1064 Vector4f row2 = fetchRegister(src1, 2);
1065 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001066
1067 dst.x = dot4(src0, row0);
1068 dst.y = dot4(src0, row1);
1069 dst.z = dot4(src0, row2);
1070 dst.w = dot4(src0, row3);
1071 }
1072
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001073 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001074 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001075 enableBreak = enableBreak & ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001076 }
1077
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001078 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001079 {
1080 Int4 condition;
1081
1082 switch(control)
1083 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001084 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1085 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1086 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1087 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1088 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1089 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001090 default:
1091 ASSERT(false);
1092 }
1093
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001094 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001095 }
1096
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001097 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001098 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001099 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001100
John Bauman19bac1e2014-05-06 15:23:49 -04001101 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001102 {
1103 condition = ~condition;
1104 }
1105
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001106 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001107 }
1108
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001109 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001110 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001111 condition &= enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001112
Nicolas Capens7551ac62016-01-20 17:11:53 -05001113 enableBreak = enableBreak & ~condition;
John Bauman89401822014-05-06 15:04:28 -04001114 }
1115
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001116 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001117 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001118 enableContinue = enableContinue & ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman19bac1e2014-05-06 15:23:49 -04001119 }
1120
1121 void VertexProgram::TEST()
1122 {
Nicolas Capens2f490f02018-11-01 16:53:36 -04001123 enableContinue = restoreContinue.back();
1124 restoreContinue.pop_back();
John Bauman19bac1e2014-05-06 15:23:49 -04001125 }
1126
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001127 void VertexProgram::SCALAR()
1128 {
1129 scalar = true;
1130 }
1131
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001132 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001133 {
1134 if(!labelBlock[labelIndex])
1135 {
1136 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1137 }
1138
John Bauman19bac1e2014-05-06 15:23:49 -04001139 if(callRetBlock[labelIndex].size() > 1)
1140 {
Nicolas Capens0f34a982019-02-11 16:02:23 -05001141 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001142 }
John Bauman89401822014-05-06 15:04:28 -04001143
Nicolas Capens7551ac62016-01-20 17:11:53 -05001144 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001145
1146 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001147 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1148
Nicolas Capens7551ac62016-01-20 17:11:53 -05001149 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001150 }
1151
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001152 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001153 {
John Bauman19bac1e2014-05-06 15:23:49 -04001154 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001155 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001156 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001157 }
John Bauman19bac1e2014-05-06 15:23:49 -04001158 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001159 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001160 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001161 }
1162 else ASSERT(false);
1163 }
1164
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001165 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001166 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001167 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001168
John Bauman19bac1e2014-05-06 15:23:49 -04001169 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001170 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001171 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001172 }
1173
1174 if(!labelBlock[labelIndex])
1175 {
1176 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1177 }
1178
John Bauman19bac1e2014-05-06 15:23:49 -04001179 if(callRetBlock[labelIndex].size() > 1)
1180 {
Nicolas Capens0f34a982019-02-11 16:02:23 -05001181 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001182 }
John Bauman89401822014-05-06 15:04:28 -04001183
Nicolas Capens7551ac62016-01-20 17:11:53 -05001184 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001185
John Bauman19bac1e2014-05-06 15:23:49 -04001186 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1187 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1188
Nicolas Capens7551ac62016-01-20 17:11:53 -05001189 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001190 }
1191
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001192 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001193 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001194 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001195
John Bauman19bac1e2014-05-06 15:23:49 -04001196 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001197 {
1198 condition = ~condition;
1199 }
1200
Alexis Hetu48d47a42019-01-10 14:04:26 -05001201 condition &= enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001202
1203 if(!labelBlock[labelIndex])
1204 {
1205 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1206 }
1207
John Bauman19bac1e2014-05-06 15:23:49 -04001208 if(callRetBlock[labelIndex].size() > 1)
1209 {
Nicolas Capens0f34a982019-02-11 16:02:23 -05001210 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001211 }
John Bauman89401822014-05-06 15:04:28 -04001212
Nicolas Capens7551ac62016-01-20 17:11:53 -05001213 enableIndex++;
Alexis Hetu48d47a42019-01-10 14:04:26 -05001214 enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] = condition;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001215 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001216
John Bauman19bac1e2014-05-06 15:23:49 -04001217 Bool notAllFalse = SignMask(condition) != 0;
1218 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1219 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001220
Nicolas Capens7551ac62016-01-20 17:11:53 -05001221 enableIndex--;
1222 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001223 }
1224
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001225 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001226 {
1227 ifDepth--;
1228
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001229 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1230 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001231
1232 if(isConditionalIf[ifDepth])
1233 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001234 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 -04001235 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001236
1237 branch(notAllFalse, falseBlock, endBlock);
1238
Alexis Hetu48d47a42019-01-10 14:04:26 -05001239 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 -04001240 }
1241 else
1242 {
1243 Nucleus::createBr(endBlock);
1244 Nucleus::setInsertBlock(falseBlock);
1245 }
1246
1247 ifFalseBlock[ifDepth] = endBlock;
1248
1249 ifDepth++;
1250 }
1251
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001252 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001253 {
1254 ifDepth--;
1255
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001256 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001257
1258 Nucleus::createBr(endBlock);
1259 Nucleus::setInsertBlock(endBlock);
1260
1261 if(isConditionalIf[ifDepth])
1262 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001263 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001264 }
1265 }
1266
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001267 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001268 {
1269 loopRepDepth--;
1270
Nicolas Capens7551ac62016-01-20 17:11:53 -05001271 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001272
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001273 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1274 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001275
1276 Nucleus::createBr(testBlock);
1277 Nucleus::setInsertBlock(endBlock);
1278
Nicolas Capens7551ac62016-01-20 17:11:53 -05001279 loopDepth--;
1280 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001281 }
1282
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001283 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001284 {
1285 loopRepDepth--;
1286
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001287 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1288 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001289
1290 Nucleus::createBr(testBlock);
1291 Nucleus::setInsertBlock(endBlock);
1292
Nicolas Capens7551ac62016-01-20 17:11:53 -05001293 loopDepth--;
1294 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001295 }
1296
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001297 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001298 {
1299 loopRepDepth--;
1300
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001301 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1302 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001303
1304 Nucleus::createBr(testBlock);
1305 Nucleus::setInsertBlock(endBlock);
1306
Nicolas Capens7551ac62016-01-20 17:11:53 -05001307 enableIndex--;
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001308 scalar = false;
John Bauman19bac1e2014-05-06 15:23:49 -04001309 }
1310
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001311 void VertexProgram::ENDSWITCH()
1312 {
1313 loopRepDepth--;
1314
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001315 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001316
Nicolas Capensac6d5052018-01-05 15:34:00 -05001317 Nucleus::createBr(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001318 Nucleus::setInsertBlock(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001319 }
1320
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001321 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001322 {
John Bauman19bac1e2014-05-06 15:23:49 -04001323 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001324 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001325 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001326 }
John Bauman19bac1e2014-05-06 15:23:49 -04001327 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001328 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001329 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001330 }
John Bauman19bac1e2014-05-06 15:23:49 -04001331 else
1332 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001333 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001334 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001335 }
John Bauman89401822014-05-06 15:04:28 -04001336 }
1337
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001338 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001339 {
1340 ASSERT(ifDepth < 24 + 4);
1341
Nicolas Capens7551ac62016-01-20 17:11:53 -05001342 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001343
John Bauman19bac1e2014-05-06 15:23:49 -04001344 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001345 {
John Bauman19bac1e2014-05-06 15:23:49 -04001346 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001347 }
1348
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001349 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1350 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001351
1352 branch(condition, trueBlock, falseBlock);
1353
1354 isConditionalIf[ifDepth] = false;
1355 ifFalseBlock[ifDepth] = falseBlock;
1356
1357 ifDepth++;
1358 }
1359
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001360 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001361 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001362 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001363
John Bauman19bac1e2014-05-06 15:23:49 -04001364 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001365 {
1366 condition = ~condition;
1367 }
1368
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001369 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001370 }
1371
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001372 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001373 {
1374 Int4 condition;
1375
1376 switch(control)
1377 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001378 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1379 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1380 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1381 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1382 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1383 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001384 default:
1385 ASSERT(false);
1386 }
1387
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001388 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001389 }
1390
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001391 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001392 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001393 condition &= enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001394
Nicolas Capens7551ac62016-01-20 17:11:53 -05001395 enableIndex++;
Alexis Hetu48d47a42019-01-10 14:04:26 -05001396 enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] = condition;
John Bauman89401822014-05-06 15:04:28 -04001397
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001398 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1399 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001400
John Bauman19bac1e2014-05-06 15:23:49 -04001401 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001402
1403 branch(notAllFalse, trueBlock, falseBlock);
1404
1405 isConditionalIf[ifDepth] = true;
1406 ifFalseBlock[ifDepth] = falseBlock;
1407
1408 ifDepth++;
John Bauman89401822014-05-06 15:04:28 -04001409 }
1410
1411 void VertexProgram::LABEL(int labelIndex)
1412 {
John Bauman19bac1e2014-05-06 15:23:49 -04001413 if(!labelBlock[labelIndex])
1414 {
1415 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1416 }
1417
John Bauman89401822014-05-06 15:04:28 -04001418 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001419 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001420 }
1421
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001422 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001423 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001424 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001425
Nicolas Capens7551ac62016-01-20 17:11:53 -05001426 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1427 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1428 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001429
1430 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001431 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001432 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001433 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001434 }
1435
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001436 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1437 BasicBlock *testBlock = Nucleus::createBasicBlock();
1438 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001439
1440 loopRepTestBlock[loopRepDepth] = testBlock;
1441 loopRepEndBlock[loopRepDepth] = endBlock;
1442
1443 // FIXME: jump(testBlock)
1444 Nucleus::createBr(testBlock);
1445 Nucleus::setInsertBlock(testBlock);
1446
Nicolas Capens7551ac62016-01-20 17:11:53 -05001447 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001448 Nucleus::setInsertBlock(loopBlock);
1449
Nicolas Capens7551ac62016-01-20 17:11:53 -05001450 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001451
John Bauman89401822014-05-06 15:04:28 -04001452 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001453 }
1454
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001455 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001456 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001457 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001458
Nicolas Capens7551ac62016-01-20 17:11:53 -05001459 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1460 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001461
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001462 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1463 BasicBlock *testBlock = Nucleus::createBasicBlock();
1464 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001465
1466 loopRepTestBlock[loopRepDepth] = testBlock;
1467 loopRepEndBlock[loopRepDepth] = endBlock;
1468
1469 // FIXME: jump(testBlock)
1470 Nucleus::createBr(testBlock);
1471 Nucleus::setInsertBlock(testBlock);
1472
Nicolas Capens7551ac62016-01-20 17:11:53 -05001473 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001474 Nucleus::setInsertBlock(loopBlock);
1475
Nicolas Capens7551ac62016-01-20 17:11:53 -05001476 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001477
1478 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001479 }
1480
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001481 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001482 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001483 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001484
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001485 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1486 BasicBlock *testBlock = Nucleus::createBasicBlock();
1487 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001488
John Bauman19bac1e2014-05-06 15:23:49 -04001489 loopRepTestBlock[loopRepDepth] = testBlock;
1490 loopRepEndBlock[loopRepDepth] = endBlock;
1491
Nicolas Capens7551ac62016-01-20 17:11:53 -05001492 Int4 restoreBreak = enableBreak;
Nicolas Capens2f490f02018-11-01 16:53:36 -04001493 restoreContinue.push_back(enableContinue);
John Bauman19bac1e2014-05-06 15:23:49 -04001494
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001495 // TODO: jump(testBlock)
John Bauman19bac1e2014-05-06 15:23:49 -04001496 Nucleus::createBr(testBlock);
1497 Nucleus::setInsertBlock(testBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001498
Nicolas Capensc2534f42016-04-04 11:13:24 -04001499 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001500 Int4 condition = As<Int4>(src.x);
Alexis Hetu48d47a42019-01-10 14:04:26 -05001501 condition &= enableStack[Min(enableIndex - 1, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001502 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens6d123312018-01-08 12:57:52 -05001503 if(shader->containsBreakInstruction()) condition &= enableBreak;
Alexis Hetu48d47a42019-01-10 14:04:26 -05001504 enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001505
1506 Bool notAllFalse = SignMask(condition) != 0;
1507 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001508
John Bauman19bac1e2014-05-06 15:23:49 -04001509 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001510 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001511
John Bauman19bac1e2014-05-06 15:23:49 -04001512 Nucleus::setInsertBlock(loopBlock);
1513
1514 loopRepDepth++;
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001515 scalar = false;
John Bauman19bac1e2014-05-06 15:23:49 -04001516 }
1517
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001518 void VertexProgram::SWITCH()
1519 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001520 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001521
1522 loopRepTestBlock[loopRepDepth] = nullptr;
1523 loopRepEndBlock[loopRepDepth] = endBlock;
1524
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001525 Int4 restoreBreak = enableBreak;
1526
1527 BasicBlock *currentBlock = Nucleus::getInsertBlock();
1528
1529 Nucleus::setInsertBlock(endBlock);
1530 enableBreak = restoreBreak;
1531
1532 Nucleus::setInsertBlock(currentBlock);
1533
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001534 loopRepDepth++;
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001535 }
1536
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001537 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001538 {
John Bauman19bac1e2014-05-06 15:23:49 -04001539 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001540 {
1541 returnBlock = Nucleus::createBasicBlock();
1542 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001543 }
1544 else
1545 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001546 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001547
John Bauman19bac1e2014-05-06 15:23:49 -04001548 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001549 {
John Bauman19bac1e2014-05-06 15:23:49 -04001550 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001551 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001552
Nicolas Capens19336542016-09-26 10:32:29 -04001553 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001554 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001555
1556 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1557 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001558 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001559 }
1560 }
1561 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1562 {
1563 Nucleus::createBr(callRetBlock[currentLabel][0]);
1564 }
1565 else // Function isn't called
1566 {
1567 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001568 }
1569
1570 Nucleus::setInsertBlock(unreachableBlock);
1571 Nucleus::createUnreachable();
1572 }
1573 }
1574
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001575 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001576 {
Alexis Hetu48d47a42019-01-10 14:04:26 -05001577 enableLeave = enableLeave & ~enableStack[Min(enableIndex, Int(MAX_SHADER_ENABLE_STACK_SIZE))];
John Bauman89401822014-05-06 15:04:28 -04001578
John Bauman19bac1e2014-05-06 15:23:49 -04001579 // FIXME: Return from function if all instances left
1580 // FIXME: Use enableLeave in other control-flow constructs
1581 }
John Bauman89401822014-05-06 15:04:28 -04001582
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001583 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001584 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001585 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001586 }
1587
Nicolas Capensa0b57832017-11-07 13:07:53 -05001588 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001589 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001590 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001591 }
1592
Nicolas Capensa0b57832017-11-07 13:07:53 -05001593 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001594 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001595 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001596 }
1597
Nicolas Capensa0b57832017-11-07 13:07:53 -05001598 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001599 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001600 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001601 }
1602
Nicolas Capensa0b57832017-11-07 13:07:53 -05001603 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001604 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001605 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001606 }
1607
Nicolas Capensa0b57832017-11-07 13:07:53 -05001608 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001609 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001610 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001611 }
1612
Nicolas Capensa0b57832017-11-07 13:07:53 -05001613 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001614 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001615 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1616 }
1617
1618 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1619 {
1620 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001621 }
1622
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001623 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001624 {
Nicolas Capens79d0e562018-11-27 17:07:49 -05001625 bool uniformSampler = (src1.type == Shader::PARAMETER_SAMPLER && src1.rel.type == Shader::PARAMETER_VOID);
Nicolas Capensa9727582018-11-28 17:33:24 -05001626 Int offset = uniformSampler ? src1.index * sizeof(Texture) : As<Int>(Float(fetchRegister(src1).x.x)) * sizeof(Texture);
1627 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + offset;
Nicolas Capens79d0e562018-11-27 17:07:49 -05001628
Nicolas Capens89a218b2017-11-07 13:05:20 -05001629 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001630 }
1631
Nicolas Capensa0b57832017-11-07 13:07:53 -05001632 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 -04001633 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001634 Vector4f tmp;
1635
John Bauman19bac1e2014-05-06 15:23:49 -04001636 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1637 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001638 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001639 }
1640 else
1641 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001642 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001643
Nicolas Capensc2534f42016-04-04 11:13:24 -04001644 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001645 {
1646 if(shader->usesSampler(i))
1647 {
1648 If(index == i)
1649 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001650 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001651 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1652 }
1653 }
1654 }
1655 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001656
Nicolas Capens89a218b2017-11-07 13:05:20 -05001657 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001658 c.x = tmp[(s.swizzle >> 0) & 0x3];
1659 c.y = tmp[(s.swizzle >> 2) & 0x3];
1660 c.z = tmp[(s.swizzle >> 4) & 0x3];
1661 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001662
1663 return c;
1664 }
1665
Nicolas Capensa0b57832017-11-07 13:07:53 -05001666 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001667 {
1668 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001669 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 -04001670 }
John Bauman89401822014-05-06 15:04:28 -04001671}