blob: a74a37fe0db6019b6094f9a1af8686952e865ca8 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "VertexProgram.hpp"
16
John Bauman89401822014-05-06 15:04:28 -040017#include "VertexShader.hpp"
John Bauman89401822014-05-06 15:04:28 -040018#include "SamplerCore.hpp"
Nicolas Capens708c24b2017-10-26 13:07:10 -040019#include "Renderer/Renderer.hpp"
20#include "Renderer/Vertex.hpp"
21#include "Common/Half.hpp"
22#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040023
John Bauman89401822014-05-06 15:04:28 -040024namespace sw
25{
Nicolas Capens7551ac62016-01-20 17:11:53 -050026 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader)
Nicolas Capens5bff4052018-05-28 13:18:59 -040027 : VertexRoutine(state, shader), shader(shader), r(shader->indirectAddressableTemporaries)
John Bauman89401822014-05-06 15:04:28 -040028 {
John Bauman89401822014-05-06 15:04:28 -040029 for(int i = 0; i < 2048; i++)
30 {
31 labelBlock[i] = 0;
32 }
Nicolas Capens7551ac62016-01-20 17:11:53 -050033
34 loopDepth = -1;
35 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
36
Nicolas Capensc62fad32018-01-26 01:55:31 +000037 if(shader->containsBreakInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050038 {
39 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
40 }
41
Nicolas Capensc62fad32018-01-26 01:55:31 +000042 if(shader->containsContinueInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050043 {
44 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
45 }
46
Alexis Hetu02ad0aa2016-08-02 11:18:14 -040047 if(shader->isInstanceIdDeclared())
Nicolas Capens7551ac62016-01-20 17:11:53 -050048 {
49 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
50 }
John Bauman89401822014-05-06 15:04:28 -040051 }
52
53 VertexProgram::~VertexProgram()
54 {
John Bauman89401822014-05-06 15:04:28 -040055 }
56
Nicolas Capens4b743732018-05-28 13:22:07 -040057 void VertexProgram::pipeline(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040058 {
John Bauman89401822014-05-06 15:04:28 -040059 if(!state.preTransformed)
60 {
Alexis Hetu877ddfc2017-07-25 17:48:00 -040061 program(index);
John Bauman89401822014-05-06 15:04:28 -040062 }
63 else
64 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050065 passThrough();
John Bauman89401822014-05-06 15:04:28 -040066 }
67 }
68
Nicolas Capens4b743732018-05-28 13:22:07 -040069 void VertexProgram::program(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040070 {
John Bauman19bac1e2014-05-06 15:23:49 -040071 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040072
Alexis Hetu53ad4af2017-12-06 14:49:07 -050073 unsigned short shaderModel = shader->getShaderModel();
John Bauman89401822014-05-06 15:04:28 -040074
Nicolas Capens7551ac62016-01-20 17:11:53 -050075 enableIndex = 0;
76 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040077
Nicolas Capens4677a5f2014-05-06 16:42:26 -040078 if(shader->containsLeaveInstruction())
79 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050080 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040081 }
82
Alexis Hetu877ddfc2017-07-25 17:48:00 -040083 if(shader->isVertexIdDeclared())
84 {
85 if(state.textureSampling)
86 {
87 vertexID = Int4(index);
88 }
89 else
90 {
91 vertexID = Insert(vertexID, As<Int>(index), 0);
92 vertexID = Insert(vertexID, As<Int>(index + 1), 1);
93 vertexID = Insert(vertexID, As<Int>(index + 2), 2);
94 vertexID = Insert(vertexID, As<Int>(index + 3), 3);
95 }
96 }
97
John Bauman19bac1e2014-05-06 15:23:49 -040098 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050099 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400100 {
John Bauman19bac1e2014-05-06 15:23:49 -0400101 const Shader::Instruction *instruction = shader->getInstruction(i);
102 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400103
John Bauman19bac1e2014-05-06 15:23:49 -0400104 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
105 {
106 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400107
John Bauman19bac1e2014-05-06 15:23:49 -0400108 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
109 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
110 }
111 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500112
Alexis Hetu903e0252014-11-25 14:25:32 -0500113 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400114 {
115 const Shader::Instruction *instruction = shader->getInstruction(i);
116 Shader::Opcode opcode = instruction->opcode;
117
118 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400119 {
120 continue;
121 }
122
John Bauman19bac1e2014-05-06 15:23:49 -0400123 Dst dst = instruction->dst;
124 Src src0 = instruction->src[0];
125 Src src1 = instruction->src[1];
126 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400127 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400128 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400129
John Bauman19bac1e2014-05-06 15:23:49 -0400130 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400131 Control control = instruction->control;
132 bool integer = dst.type == Shader::PARAMETER_ADDR;
133 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400134
John Bauman19bac1e2014-05-06 15:23:49 -0400135 Vector4f d;
136 Vector4f s0;
137 Vector4f s1;
138 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400139 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400140 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400141
Nicolas Capensc2534f42016-04-04 11:13:24 -0400142 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
143 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
144 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
145 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
146 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400147
148 switch(opcode)
149 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500150 case Shader::OPCODE_VS_1_0: break;
151 case Shader::OPCODE_VS_1_1: break;
152 case Shader::OPCODE_VS_2_0: break;
153 case Shader::OPCODE_VS_2_x: break;
154 case Shader::OPCODE_VS_2_sw: break;
155 case Shader::OPCODE_VS_3_0: break;
156 case Shader::OPCODE_VS_3_sw: break;
157 case Shader::OPCODE_DCL: break;
158 case Shader::OPCODE_DEF: break;
159 case Shader::OPCODE_DEFI: break;
160 case Shader::OPCODE_DEFB: break;
161 case Shader::OPCODE_NOP: break;
162 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400163 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500164 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400165 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500166 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
167 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
168 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
169 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
170 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
171 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
172 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
173 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
174 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
175 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
176 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
177 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
178 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
179 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
180 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
181 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
182 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400183 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
184 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
185 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500186 case Shader::OPCODE_ATT: att(d, s0, s1); break;
187 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
188 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500189 case Shader::OPCODE_EXPP: expp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500190 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
191 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400192 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
193 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400194 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500195 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400196 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500197 case Shader::OPCODE_LIT: lit(d, s0); break;
198 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
199 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500200 case Shader::OPCODE_LOGP: logp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500201 case Shader::OPCODE_LOG: log(d, s0, pp); break;
202 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
203 case Shader::OPCODE_STEP: step(d, s0, s1); break;
204 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8ef6d102017-11-09 15:49:09 -0500205 case Shader::OPCODE_ISINF: isinf(d, s0); break;
206 case Shader::OPCODE_ISNAN: isnan(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400207 case Shader::OPCODE_FLOATBITSTOINT:
208 case Shader::OPCODE_FLOATBITSTOUINT:
209 case Shader::OPCODE_INTBITSTOFLOAT:
210 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400211 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
212 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400213 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400214 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
215 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400216 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500217 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
218 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
219 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
220 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
221 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
222 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
223 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
224 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400225 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
226 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500227 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400228 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
229 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500230 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400231 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400232 case Shader::OPCODE_NEG: neg(d, s0); break;
233 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500234 case Shader::OPCODE_F2B: f2b(d, s0); break;
235 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400236 case Shader::OPCODE_F2I: f2i(d, s0); break;
237 case Shader::OPCODE_I2F: i2f(d, s0); break;
238 case Shader::OPCODE_F2U: f2u(d, s0); break;
239 case Shader::OPCODE_U2F: u2f(d, s0); break;
240 case Shader::OPCODE_I2B: i2b(d, s0); break;
241 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500242 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400243 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500244 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
245 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
246 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
247 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
248 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
249 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
250 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400251 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
252 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500253 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400254 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
255 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
256 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500257 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
258 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
259 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
260 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
261 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
262 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
263 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
264 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
265 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
266 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
267 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
268 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
269 case Shader::OPCODE_SGE: step(d, s1, s0); break;
270 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400271 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500272 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
273 case Shader::OPCODE_COS: cos(d, s0, pp); break;
274 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
275 case Shader::OPCODE_TAN: tan(d, s0); break;
276 case Shader::OPCODE_ACOS: acos(d, s0); break;
277 case Shader::OPCODE_ASIN: asin(d, s0); break;
278 case Shader::OPCODE_ATAN: atan(d, s0); break;
279 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
280 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
281 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
282 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
283 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
284 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
285 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
286 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
287 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400288 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500289 case Shader::OPCODE_BREAK: BREAK(); break;
290 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
291 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
292 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
293 case Shader::OPCODE_TEST: TEST(); break;
Nicolas Capens6e8ec332018-11-06 11:56:21 -0500294 case Shader::OPCODE_SCALAR: SCALAR(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500295 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
296 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
297 case Shader::OPCODE_ELSE: ELSE(); break;
298 case Shader::OPCODE_ENDIF: ENDIF(); break;
299 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
300 case Shader::OPCODE_ENDREP: ENDREP(); break;
301 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400302 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500303 case Shader::OPCODE_IF: IF(src0); break;
304 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
305 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
306 case Shader::OPCODE_LOOP: LOOP(src1); break;
307 case Shader::OPCODE_REP: REP(src0); break;
308 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400309 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500310 case Shader::OPCODE_RET: RET(); break;
311 case Shader::OPCODE_LEAVE: LEAVE(); break;
312 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
313 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400314 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500315 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
316 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
317 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
318 case Shader::OPCODE_ALL: all(d.x, s0); break;
319 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400320 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
321 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
322 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
323 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400324 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
325 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500326 case Shader::OPCODE_TEXLDL: TEXLOD(d, s0, src1, s0.w); break;
327 case Shader::OPCODE_TEXLOD: TEXLOD(d, s0, src1, s2.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500328 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400329 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500330 case Shader::OPCODE_TEXLODOFFSET: TEXLODOFFSET(d, s0, src1, s2, s3.x); break;
331 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2.x); break;
332 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCHOFFSET(d, s0, src1, s2, s3.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500333 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500334 case Shader::OPCODE_TEXGRADOFFSET: TEXGRADOFFSET(d, s0, src1, s2, s3, s4); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500335 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
336 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400337 default:
338 ASSERT(false);
339 }
340
John Bauman19bac1e2014-05-06 15:23:49 -0400341 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400342 {
John Bauman19bac1e2014-05-06 15:23:49 -0400343 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400344 {
John Bauman19bac1e2014-05-06 15:23:49 -0400345 if(dst.x) d.x = Max(d.x, Float4(0.0f));
346 if(dst.y) d.y = Max(d.y, Float4(0.0f));
347 if(dst.z) d.z = Max(d.z, Float4(0.0f));
348 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400349
John Bauman19bac1e2014-05-06 15:23:49 -0400350 if(dst.x) d.x = Min(d.x, Float4(1.0f));
351 if(dst.y) d.y = Min(d.y, Float4(1.0f));
352 if(dst.z) d.z = Min(d.z, Float4(1.0f));
353 if(dst.w) d.w = Min(d.w, Float4(1.0f));
354 }
355
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400356 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400357 {
358 Vector4f pDst; // FIXME: Rename
359
360 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400361 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500362 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400363 case Shader::PARAMETER_TEMP:
364 if(dst.rel.type == Shader::PARAMETER_VOID)
365 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500366 if(dst.x) pDst.x = r[dst.index].x;
367 if(dst.y) pDst.y = r[dst.index].y;
368 if(dst.z) pDst.z = r[dst.index].z;
369 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400370 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400371 else if(!dst.rel.dynamic)
372 {
373 Int a = dst.index + relativeAddress(dst.rel);
374
375 if(dst.x) pDst.x = r[a].x;
376 if(dst.y) pDst.y = r[a].y;
377 if(dst.z) pDst.z = r[a].z;
378 if(dst.w) pDst.w = r[a].w;
379 }
John Bauman19bac1e2014-05-06 15:23:49 -0400380 else
381 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400382 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400383
Nicolas Capens4b743732018-05-28 13:22:07 -0400384 if(dst.x) pDst.x = r[a].x;
385 if(dst.y) pDst.y = r[a].y;
386 if(dst.z) pDst.z = r[a].z;
387 if(dst.w) pDst.w = r[a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400388 }
389 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500390 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400391 case Shader::PARAMETER_RASTOUT:
392 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400393 {
394 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500395 if(dst.x) pDst.x = o[Pos].x;
396 if(dst.y) pDst.y = o[Pos].y;
397 if(dst.z) pDst.z = o[Pos].z;
398 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400399 break;
400 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500401 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400402 break;
403 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500404 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400405 break;
406 default:
407 ASSERT(false);
408 }
409 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400410 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400411 if(dst.x) pDst.x = o[C0 + dst.index].x;
412 if(dst.y) pDst.y = o[C0 + dst.index].y;
413 if(dst.z) pDst.z = o[C0 + dst.index].z;
414 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400415 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400416 case Shader::PARAMETER_TEXCRDOUT:
417 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500418 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400419 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500420 if(dst.x) pDst.x = o[T0 + dst.index].x;
421 if(dst.y) pDst.y = o[T0 + dst.index].y;
422 if(dst.z) pDst.z = o[T0 + dst.index].z;
423 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400424 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400425 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
426 {
427 if(dst.x) pDst.x = o[dst.index].x;
428 if(dst.y) pDst.y = o[dst.index].y;
429 if(dst.z) pDst.z = o[dst.index].z;
430 if(dst.w) pDst.w = o[dst.index].w;
431 }
432 else if(!dst.rel.dynamic)
433 {
434 Int a = dst.index + relativeAddress(dst.rel);
435
436 if(dst.x) pDst.x = o[a].x;
437 if(dst.y) pDst.y = o[a].y;
438 if(dst.z) pDst.z = o[a].z;
439 if(dst.w) pDst.w = o[a].w;
440 }
John Bauman89401822014-05-06 15:04:28 -0400441 else
442 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400443 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400444
Nicolas Capens4b743732018-05-28 13:22:07 -0400445 if(dst.x) pDst.x = o[a].x;
446 if(dst.y) pDst.y = o[a].y;
447 if(dst.z) pDst.z = o[a].z;
448 if(dst.w) pDst.w = o[a].w;
John Bauman89401822014-05-06 15:04:28 -0400449 }
450 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500451 case Shader::PARAMETER_LABEL: break;
452 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
453 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400454 default:
455 ASSERT(false);
456 }
457
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500458 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400459
460 Int4 xEnable = enable;
461 Int4 yEnable = enable;
462 Int4 zEnable = enable;
463 Int4 wEnable = enable;
464
465 if(predicate)
466 {
John Bauman19bac1e2014-05-06 15:23:49 -0400467 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400468
Nicolas Capens7551ac62016-01-20 17:11:53 -0500469 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
470 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
471 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
472 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400473
John Bauman19bac1e2014-05-06 15:23:49 -0400474 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400475 {
John Bauman19bac1e2014-05-06 15:23:49 -0400476 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
477 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
478 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
479 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400480 }
481 else
482 {
John Bauman19bac1e2014-05-06 15:23:49 -0400483 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
484 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
485 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
486 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400487 }
488 }
489
John Bauman19bac1e2014-05-06 15:23:49 -0400490 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
491 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
492 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
493 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400494
John Bauman19bac1e2014-05-06 15:23:49 -0400495 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
496 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
497 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
498 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400499 }
500
John Bauman19bac1e2014-05-06 15:23:49 -0400501 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400502 {
John Bauman19bac1e2014-05-06 15:23:49 -0400503 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400504 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400505 case Shader::PARAMETER_TEMP:
506 if(dst.rel.type == Shader::PARAMETER_VOID)
507 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500508 if(dst.x) r[dst.index].x = d.x;
509 if(dst.y) r[dst.index].y = d.y;
510 if(dst.z) r[dst.index].z = d.z;
511 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400512 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400513 else if(!dst.rel.dynamic)
514 {
515 Int a = dst.index + relativeAddress(dst.rel);
516
517 if(dst.x) r[a].x = d.x;
518 if(dst.y) r[a].y = d.y;
519 if(dst.z) r[a].z = d.z;
520 if(dst.w) r[a].w = d.w;
521 }
John Bauman19bac1e2014-05-06 15:23:49 -0400522 else
523 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400524 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400525
Nicolas Capens4b743732018-05-28 13:22:07 -0400526 if(dst.x) r.scatter_x(a, d.x);
527 if(dst.y) r.scatter_y(a, d.y);
528 if(dst.z) r.scatter_z(a, d.z);
529 if(dst.w) r.scatter_w(a, d.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400530 }
John Bauman89401822014-05-06 15:04:28 -0400531 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400532 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500533 if(dst.x) a0.x = d.x;
534 if(dst.y) a0.y = d.y;
535 if(dst.z) a0.z = d.z;
536 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400537 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400538 case Shader::PARAMETER_RASTOUT:
539 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400540 {
541 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500542 if(dst.x) o[Pos].x = d.x;
543 if(dst.y) o[Pos].y = d.y;
544 if(dst.z) o[Pos].z = d.z;
545 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400546 break;
547 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500548 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400549 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500550 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500551 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400552 break;
553 default: ASSERT(false);
554 }
555 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500556 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400557 if(dst.x) o[C0 + dst.index].x = d.x;
558 if(dst.y) o[C0 + dst.index].y = d.y;
559 if(dst.z) o[C0 + dst.index].z = d.z;
560 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400561 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400562 case Shader::PARAMETER_TEXCRDOUT:
563 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500564 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400565 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500566 if(dst.x) o[T0 + dst.index].x = d.x;
567 if(dst.y) o[T0 + dst.index].y = d.y;
568 if(dst.z) o[T0 + dst.index].z = d.z;
569 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400570 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400571 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
572 {
573 if(dst.x) o[dst.index].x = d.x;
574 if(dst.y) o[dst.index].y = d.y;
575 if(dst.z) o[dst.index].z = d.z;
576 if(dst.w) o[dst.index].w = d.w;
577 }
578 else if(!dst.rel.dynamic)
579 {
580 Int a = dst.index + relativeAddress(dst.rel);
581
582 if(dst.x) o[a].x = d.x;
583 if(dst.y) o[a].y = d.y;
584 if(dst.z) o[a].z = d.z;
585 if(dst.w) o[a].w = d.w;
586 }
John Bauman89401822014-05-06 15:04:28 -0400587 else
588 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400589 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400590
Nicolas Capens4b743732018-05-28 13:22:07 -0400591 if(dst.x) o.scatter_x(a, d.x);
592 if(dst.y) o.scatter_y(a, d.y);
593 if(dst.z) o.scatter_z(a, d.z);
594 if(dst.w) o.scatter_w(a, d.w);
John Bauman89401822014-05-06 15:04:28 -0400595 }
596 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500597 case Shader::PARAMETER_LABEL: break;
598 case Shader::PARAMETER_PREDICATE: p0 = d; break;
599 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400600 default:
601 ASSERT(false);
602 }
603 }
604 }
605
John Bauman19bac1e2014-05-06 15:23:49 -0400606 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400607 {
608 Nucleus::setInsertBlock(returnBlock);
609 }
610 }
611
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500612 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400613 {
John Bauman19bac1e2014-05-06 15:23:49 -0400614 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400615 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400616 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400617 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400618 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400619
620 switch(usage)
621 {
622 case 0xFF:
623 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400624 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500625 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400626 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400627 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500628 o[i].x = v[i].x;
629 o[i].y = v[i].y;
630 o[i].z = v[i].z;
631 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400632 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400633 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500634 o[i].x = v[i].x;
635 o[i].y = v[i].y;
636 o[i].z = v[i].z;
637 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400638 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400639 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500640 o[i].x = v[i].x;
641 o[i].y = v[i].y;
642 o[i].z = v[i].z;
643 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400644 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400645 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500646 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400647 break;
648 default:
649 ASSERT(false);
650 }
651 }
652 }
653 else
654 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500655 o[Pos].x = v[PositionT].x;
656 o[Pos].y = v[PositionT].y;
657 o[Pos].z = v[PositionT].z;
658 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400659
660 for(int i = 0; i < 2; i++)
661 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400662 o[C0 + i].x = v[Color0 + i].x;
663 o[C0 + i].y = v[Color0 + i].y;
664 o[C0 + i].z = v[Color0 + i].z;
665 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400666 }
667
668 for(int i = 0; i < 8; i++)
669 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500670 o[T0 + i].x = v[TexCoord0 + i].x;
671 o[T0 + i].y = v[TexCoord0 + i].y;
672 o[T0 + i].z = v[TexCoord0 + i].z;
673 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400674 }
675
Nicolas Capens7551ac62016-01-20 17:11:53 -0500676 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400677 }
678 }
679
Nicolas Capensc2534f42016-04-04 11:13:24 -0400680 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400681 {
John Bauman19bac1e2014-05-06 15:23:49 -0400682 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500683 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400684
John Bauman89401822014-05-06 15:04:28 -0400685 switch(src.type)
686 {
John Bauman19bac1e2014-05-06 15:23:49 -0400687 case Shader::PARAMETER_TEMP:
688 if(src.rel.type == Shader::PARAMETER_VOID)
689 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500690 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400691 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400692 else if(!src.rel.dynamic)
693 {
694 reg = r[i + relativeAddress(src.rel, src.bufferIndex)];
695 }
John Bauman19bac1e2014-05-06 15:23:49 -0400696 else
697 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400698 reg = r[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400699 }
700 break;
701 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500702 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400703 break;
704 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400705 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400706 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500707 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400708 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400709 else if(!src.rel.dynamic)
710 {
711 reg = v[i + relativeAddress(src.rel, src.bufferIndex)];
712 }
John Bauman19bac1e2014-05-06 15:23:49 -0400713 else
714 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400715 reg = v[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400716 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400717 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500718 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400719 case Shader::PARAMETER_FLOAT4LITERAL:
720 reg.x = Float4(src.value[0]);
721 reg.y = Float4(src.value[1]);
722 reg.z = Float4(src.value[2]);
723 reg.w = Float4(src.value[3]);
724 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500725 case Shader::PARAMETER_ADDR: reg = a0; break;
726 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
727 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
728 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
729 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400730 case Shader::PARAMETER_SAMPLER:
731 if(src.rel.type == Shader::PARAMETER_VOID)
732 {
733 reg.x = As<Float4>(Int4(i));
734 }
735 else if(src.rel.type == Shader::PARAMETER_TEMP)
736 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500737 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400738 }
739 return reg;
740 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400741 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400742 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500743 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400744 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400745 else if(!src.rel.dynamic)
746 {
747 reg = o[i + relativeAddress(src.rel, src.bufferIndex)];
748 }
John Bauman19bac1e2014-05-06 15:23:49 -0400749 else
750 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400751 reg = o[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400752 }
753 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400754 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400755 if(src.index == Shader::InstanceIDIndex)
756 {
757 reg.x = As<Float>(instanceID);
758 }
759 else if(src.index == Shader::VertexIDIndex)
760 {
761 reg.x = As<Float4>(vertexID);
762 }
763 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400764 return reg;
John Bauman89401822014-05-06 15:04:28 -0400765 default:
766 ASSERT(false);
767 }
768
John Bauman66b8ab22014-05-06 15:57:45 -0400769 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
770 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
771 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
772 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400773
John Bauman66b8ab22014-05-06 15:57:45 -0400774 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400775
776 switch(src.modifier)
777 {
John Bauman19bac1e2014-05-06 15:23:49 -0400778 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400779 mod.x = x;
780 mod.y = y;
781 mod.z = z;
782 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400783 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400784 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400785 mod.x = -x;
786 mod.y = -y;
787 mod.z = -z;
788 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400789 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400790 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400791 mod.x = Abs(x);
792 mod.y = Abs(y);
793 mod.z = Abs(z);
794 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400795 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400796 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400797 mod.x = -Abs(x);
798 mod.y = -Abs(y);
799 mod.z = -Abs(z);
800 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400801 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400802 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400803 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
804 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
805 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
806 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400807 break;
808 default:
809 ASSERT(false);
810 }
811
812 return mod;
813 }
814
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400815 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
816 {
817 if(bufferIndex == -1)
818 {
819 return data + OFFSET(DrawData, vs.c[index]);
820 }
821 else
822 {
823 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
824 }
825 }
826
Nicolas Capens4b743732018-05-28 13:22:07 -0400827 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int &offset)
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400828 {
829 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
830 }
831
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500832 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400833 {
John Bauman19bac1e2014-05-06 15:23:49 -0400834 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500835 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400836
837 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
838 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400839 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400840
841 c.x = c.x.xxxx;
842 c.y = c.y.yyyy;
843 c.z = c.z.zzzz;
844 c.w = c.w.wwww;
845
Nicolas Capenseafdb222015-05-15 15:24:08 -0400846 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400847 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500848 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400849 {
850 const Shader::Instruction &instruction = *shader->getInstruction(j);
851
852 if(instruction.opcode == Shader::OPCODE_DEF)
853 {
854 if(instruction.dst.index == i)
855 {
856 c.x = Float4(instruction.src[0].value[0]);
857 c.y = Float4(instruction.src[0].value[1]);
858 c.z = Float4(instruction.src[0].value[2]);
859 c.w = Float4(instruction.src[0].value[3]);
860
861 break;
862 }
863 }
864 }
865 }
866 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400867 else if(!src.rel.dynamic || src.rel.type == Shader::PARAMETER_LOOP)
John Bauman19bac1e2014-05-06 15:23:49 -0400868 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400869 Int a = relativeAddress(src.rel, src.bufferIndex);
John Bauman19bac1e2014-05-06 15:23:49 -0400870
Nicolas Capens4b743732018-05-28 13:22:07 -0400871 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
John Bauman19bac1e2014-05-06 15:23:49 -0400872
873 c.x = c.x.xxxx;
874 c.y = c.y.yyyy;
875 c.z = c.z.zzzz;
876 c.w = c.w.wwww;
877 }
878 else
879 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400880 int component = src.rel.swizzle & 0x03;
881 Float4 a;
882
883 switch(src.rel.type)
John Bauman19bac1e2014-05-06 15:23:49 -0400884 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400885 case Shader::PARAMETER_ADDR: a = a0[component]; break;
886 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
887 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
888 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
889 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
890 case Shader::PARAMETER_MISCTYPE:
891 switch(src.rel.index)
John Bauman19bac1e2014-05-06 15:23:49 -0400892 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400893 case Shader::InstanceIDIndex: a = As<Float4>(Int4(instanceID)); break;
894 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400895 default: ASSERT(false);
896 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400897 break;
898 default: ASSERT(false);
John Bauman19bac1e2014-05-06 15:23:49 -0400899 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400900
901 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
902
903 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
904
905 Int index0 = Extract(index, 0);
906 Int index1 = Extract(index, 1);
907 Int index2 = Extract(index, 2);
908 Int index3 = Extract(index, 3);
909
910 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
911 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
912 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
913 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
914
915 transpose4x4(c.x, c.y, c.z, c.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400916 }
917
918 return c;
919 }
920
Nicolas Capens4b743732018-05-28 13:22:07 -0400921 Int VertexProgram::relativeAddress(const Shader::Relative &rel, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400922 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400923 ASSERT(!rel.dynamic);
John Bauman19bac1e2014-05-06 15:23:49 -0400924
Nicolas Capens4b743732018-05-28 13:22:07 -0400925 if(rel.type == Shader::PARAMETER_TEMP)
John Bauman19bac1e2014-05-06 15:23:49 -0400926 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400927 return As<Int>(Extract(r[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400928 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400929 else if(rel.type == Shader::PARAMETER_INPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400930 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400931 return As<Int>(Extract(v[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400932 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400933 else if(rel.type == Shader::PARAMETER_OUTPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400934 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400935 return As<Int>(Extract(o[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_CONST)
John Bauman19bac1e2014-05-06 15:23:49 -0400938 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400939 return *Pointer<Int>(uniformAddress(bufferIndex, rel.index)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400940 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400941 else if(rel.type == Shader::PARAMETER_LOOP)
Nicolas Capens907700d2016-01-20 17:09:28 -0500942 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500943 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500944 }
John Bauman19bac1e2014-05-06 15:23:49 -0400945 else ASSERT(false);
946
947 return 0;
948 }
949
Nicolas Capens4b743732018-05-28 13:22:07 -0400950 Int4 VertexProgram::dynamicAddress(const Shader::Relative &rel)
951 {
952 int component = rel.swizzle & 0x03;
953 Float4 a;
954
955 switch(rel.type)
956 {
957 case Shader::PARAMETER_ADDR: a = a0[component]; break;
958 case Shader::PARAMETER_TEMP: a = r[rel.index][component]; break;
959 case Shader::PARAMETER_INPUT: a = v[rel.index][component]; break;
960 case Shader::PARAMETER_OUTPUT: a = o[rel.index][component]; break;
961 case Shader::PARAMETER_MISCTYPE:
962 switch(rel.index)
963 {
964 case Shader::InstanceIDIndex: a = As<Float>(instanceID); break;
965 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
966 default: ASSERT(false);
967 }
968 break;
969 default: ASSERT(false);
970 }
971
972 return As<Int4>(a) * Int4(rel.scale);
973 }
974
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500975 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400976 {
Nicolas Capens6e8ec332018-11-06 11:56:21 -0500977 if(scalar)
978 {
979 return Int4(0xFFFFFFFF);
980 }
981
Nicolas Capens7551ac62016-01-20 17:11:53 -0500982 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500983
Nicolas Capens8a587712018-10-20 14:17:49 -0400984 if(shader->containsBreakInstruction() && instruction->analysisBreak)
John Bauman19bac1e2014-05-06 15:23:49 -0400985 {
Nicolas Capens8a587712018-10-20 14:17:49 -0400986 enable &= enableBreak;
987 }
John Bauman19bac1e2014-05-06 15:23:49 -0400988
Nicolas Capens8a587712018-10-20 14:17:49 -0400989 if(shader->containsContinueInstruction() && instruction->analysisContinue)
990 {
991 enable &= enableContinue;
992 }
John Bauman19bac1e2014-05-06 15:23:49 -0400993
Nicolas Capens8a587712018-10-20 14:17:49 -0400994 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
995 {
996 enable &= enableLeave;
John Bauman19bac1e2014-05-06 15:23:49 -0400997 }
998
999 return enable;
1000 }
1001
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001002 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001003 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001004 Vector4f row0 = fetchRegister(src1, 0);
1005 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -04001006
1007 dst.x = dot3(src0, row0);
1008 dst.y = dot3(src0, row1);
1009 }
1010
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001011 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001012 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001013 Vector4f row0 = fetchRegister(src1, 0);
1014 Vector4f row1 = fetchRegister(src1, 1);
1015 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001016
1017 dst.x = dot3(src0, row0);
1018 dst.y = dot3(src0, row1);
1019 dst.z = dot3(src0, row2);
1020 }
1021
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001022 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001023 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001024 Vector4f row0 = fetchRegister(src1, 0);
1025 Vector4f row1 = fetchRegister(src1, 1);
1026 Vector4f row2 = fetchRegister(src1, 2);
1027 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001028
1029 dst.x = dot3(src0, row0);
1030 dst.y = dot3(src0, row1);
1031 dst.z = dot3(src0, row2);
1032 dst.w = dot3(src0, row3);
1033 }
1034
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001035 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001036 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001037 Vector4f row0 = fetchRegister(src1, 0);
1038 Vector4f row1 = fetchRegister(src1, 1);
1039 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001040
1041 dst.x = dot4(src0, row0);
1042 dst.y = dot4(src0, row1);
1043 dst.z = dot4(src0, row2);
1044 }
1045
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001046 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001047 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001048 Vector4f row0 = fetchRegister(src1, 0);
1049 Vector4f row1 = fetchRegister(src1, 1);
1050 Vector4f row2 = fetchRegister(src1, 2);
1051 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001052
1053 dst.x = dot4(src0, row0);
1054 dst.y = dot4(src0, row1);
1055 dst.z = dot4(src0, row2);
1056 dst.w = dot4(src0, row3);
1057 }
1058
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001059 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001060 {
Nicolas Capens6d123312018-01-08 12:57:52 -05001061 enableBreak = enableBreak & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001062 }
1063
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001064 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001065 {
1066 Int4 condition;
1067
1068 switch(control)
1069 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001070 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1071 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1072 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1073 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1074 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1075 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001076 default:
1077 ASSERT(false);
1078 }
1079
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001080 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001081 }
1082
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001083 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001084 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001085 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001086
John Bauman19bac1e2014-05-06 15:23:49 -04001087 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001088 {
1089 condition = ~condition;
1090 }
1091
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001092 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001093 }
1094
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001095 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001096 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001097 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001098
Nicolas Capens7551ac62016-01-20 17:11:53 -05001099 enableBreak = enableBreak & ~condition;
John Bauman89401822014-05-06 15:04:28 -04001100 }
1101
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001102 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001103 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001104 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001105 }
1106
1107 void VertexProgram::TEST()
1108 {
Nicolas Capens2f490f02018-11-01 16:53:36 -04001109 enableContinue = restoreContinue.back();
1110 restoreContinue.pop_back();
John Bauman19bac1e2014-05-06 15:23:49 -04001111 }
1112
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001113 void VertexProgram::SCALAR()
1114 {
1115 scalar = true;
1116 }
1117
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001118 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001119 {
1120 if(!labelBlock[labelIndex])
1121 {
1122 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1123 }
1124
John Bauman19bac1e2014-05-06 15:23:49 -04001125 if(callRetBlock[labelIndex].size() > 1)
1126 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001127 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001128 }
John Bauman89401822014-05-06 15:04:28 -04001129
Nicolas Capens7551ac62016-01-20 17:11:53 -05001130 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001131
1132 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001133 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1134
Nicolas Capens7551ac62016-01-20 17:11:53 -05001135 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001136 }
1137
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001138 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001139 {
John Bauman19bac1e2014-05-06 15:23:49 -04001140 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001141 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001142 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001143 }
John Bauman19bac1e2014-05-06 15:23:49 -04001144 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001145 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001146 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001147 }
1148 else ASSERT(false);
1149 }
1150
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001151 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001152 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001153 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001154
John Bauman19bac1e2014-05-06 15:23:49 -04001155 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001156 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001157 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001158 }
1159
1160 if(!labelBlock[labelIndex])
1161 {
1162 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1163 }
1164
John Bauman19bac1e2014-05-06 15:23:49 -04001165 if(callRetBlock[labelIndex].size() > 1)
1166 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001167 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001168 }
John Bauman89401822014-05-06 15:04:28 -04001169
Nicolas Capens7551ac62016-01-20 17:11:53 -05001170 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001171
John Bauman19bac1e2014-05-06 15:23:49 -04001172 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1173 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1174
Nicolas Capens7551ac62016-01-20 17:11:53 -05001175 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001176 }
1177
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001178 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001179 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001180 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001181
John Bauman19bac1e2014-05-06 15:23:49 -04001182 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001183 {
1184 condition = ~condition;
1185 }
1186
Nicolas Capens7551ac62016-01-20 17:11:53 -05001187 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001188
1189 if(!labelBlock[labelIndex])
1190 {
1191 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1192 }
1193
John Bauman19bac1e2014-05-06 15:23:49 -04001194 if(callRetBlock[labelIndex].size() > 1)
1195 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001196 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001197 }
John Bauman89401822014-05-06 15:04:28 -04001198
Nicolas Capens7551ac62016-01-20 17:11:53 -05001199 enableIndex++;
1200 enableStack[enableIndex] = condition;
1201 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001202
John Bauman19bac1e2014-05-06 15:23:49 -04001203 Bool notAllFalse = SignMask(condition) != 0;
1204 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1205 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001206
Nicolas Capens7551ac62016-01-20 17:11:53 -05001207 enableIndex--;
1208 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001209 }
1210
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001211 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001212 {
1213 ifDepth--;
1214
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001215 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1216 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001217
1218 if(isConditionalIf[ifDepth])
1219 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001220 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001221 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001222
1223 branch(notAllFalse, falseBlock, endBlock);
1224
Nicolas Capens7551ac62016-01-20 17:11:53 -05001225 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001226 }
1227 else
1228 {
1229 Nucleus::createBr(endBlock);
1230 Nucleus::setInsertBlock(falseBlock);
1231 }
1232
1233 ifFalseBlock[ifDepth] = endBlock;
1234
1235 ifDepth++;
1236 }
1237
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001238 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001239 {
1240 ifDepth--;
1241
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001242 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001243
1244 Nucleus::createBr(endBlock);
1245 Nucleus::setInsertBlock(endBlock);
1246
1247 if(isConditionalIf[ifDepth])
1248 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001249 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001250 }
1251 }
1252
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001253 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001254 {
1255 loopRepDepth--;
1256
Nicolas Capens7551ac62016-01-20 17:11:53 -05001257 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001258
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001259 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1260 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001261
1262 Nucleus::createBr(testBlock);
1263 Nucleus::setInsertBlock(endBlock);
1264
Nicolas Capens7551ac62016-01-20 17:11:53 -05001265 loopDepth--;
1266 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001267 }
1268
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001269 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001270 {
1271 loopRepDepth--;
1272
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001273 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1274 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -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 Bauman19bac1e2014-05-06 15:23:49 -04001281 }
1282
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001283 void VertexProgram::ENDWHILE()
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 enableIndex--;
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001294 scalar = false;
John Bauman19bac1e2014-05-06 15:23:49 -04001295 }
1296
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001297 void VertexProgram::ENDSWITCH()
1298 {
1299 loopRepDepth--;
1300
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001301 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001302
Nicolas Capensac6d5052018-01-05 15:34:00 -05001303 Nucleus::createBr(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001304 Nucleus::setInsertBlock(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001305 }
1306
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001307 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001308 {
John Bauman19bac1e2014-05-06 15:23:49 -04001309 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001310 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001311 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001312 }
John Bauman19bac1e2014-05-06 15:23:49 -04001313 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001314 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001315 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001316 }
John Bauman19bac1e2014-05-06 15:23:49 -04001317 else
1318 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001319 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001320 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001321 }
John Bauman89401822014-05-06 15:04:28 -04001322 }
1323
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001324 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001325 {
1326 ASSERT(ifDepth < 24 + 4);
1327
Nicolas Capens7551ac62016-01-20 17:11:53 -05001328 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001329
John Bauman19bac1e2014-05-06 15:23:49 -04001330 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001331 {
John Bauman19bac1e2014-05-06 15:23:49 -04001332 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001333 }
1334
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001335 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1336 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001337
1338 branch(condition, trueBlock, falseBlock);
1339
1340 isConditionalIf[ifDepth] = false;
1341 ifFalseBlock[ifDepth] = falseBlock;
1342
1343 ifDepth++;
1344 }
1345
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001346 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001347 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001348 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001349
John Bauman19bac1e2014-05-06 15:23:49 -04001350 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001351 {
1352 condition = ~condition;
1353 }
1354
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001355 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001356 }
1357
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001358 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001359 {
1360 Int4 condition;
1361
1362 switch(control)
1363 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001364 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1365 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1366 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1367 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1368 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1369 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001370 default:
1371 ASSERT(false);
1372 }
1373
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001374 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001375 }
1376
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001377 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001378 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001379 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001380
Nicolas Capens7551ac62016-01-20 17:11:53 -05001381 enableIndex++;
1382 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001383
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001384 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1385 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001386
John Bauman19bac1e2014-05-06 15:23:49 -04001387 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001388
1389 branch(notAllFalse, trueBlock, falseBlock);
1390
1391 isConditionalIf[ifDepth] = true;
1392 ifFalseBlock[ifDepth] = falseBlock;
1393
1394 ifDepth++;
John Bauman89401822014-05-06 15:04:28 -04001395 }
1396
1397 void VertexProgram::LABEL(int labelIndex)
1398 {
John Bauman19bac1e2014-05-06 15:23:49 -04001399 if(!labelBlock[labelIndex])
1400 {
1401 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1402 }
1403
John Bauman89401822014-05-06 15:04:28 -04001404 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001405 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001406 }
1407
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001408 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001409 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001410 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001411
Nicolas Capens7551ac62016-01-20 17:11:53 -05001412 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1413 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1414 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001415
1416 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001417 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001418 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001419 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001420 }
1421
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001422 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1423 BasicBlock *testBlock = Nucleus::createBasicBlock();
1424 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001425
1426 loopRepTestBlock[loopRepDepth] = testBlock;
1427 loopRepEndBlock[loopRepDepth] = endBlock;
1428
1429 // FIXME: jump(testBlock)
1430 Nucleus::createBr(testBlock);
1431 Nucleus::setInsertBlock(testBlock);
1432
Nicolas Capens7551ac62016-01-20 17:11:53 -05001433 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001434 Nucleus::setInsertBlock(loopBlock);
1435
Nicolas Capens7551ac62016-01-20 17:11:53 -05001436 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001437
John Bauman89401822014-05-06 15:04:28 -04001438 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001439 }
1440
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001441 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001442 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001443 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001444
Nicolas Capens7551ac62016-01-20 17:11:53 -05001445 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1446 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001447
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001448 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1449 BasicBlock *testBlock = Nucleus::createBasicBlock();
1450 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001451
1452 loopRepTestBlock[loopRepDepth] = testBlock;
1453 loopRepEndBlock[loopRepDepth] = endBlock;
1454
1455 // FIXME: jump(testBlock)
1456 Nucleus::createBr(testBlock);
1457 Nucleus::setInsertBlock(testBlock);
1458
Nicolas Capens7551ac62016-01-20 17:11:53 -05001459 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001460 Nucleus::setInsertBlock(loopBlock);
1461
Nicolas Capens7551ac62016-01-20 17:11:53 -05001462 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001463
1464 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001465 }
1466
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001467 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001468 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001469 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001470
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001471 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1472 BasicBlock *testBlock = Nucleus::createBasicBlock();
1473 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001474
John Bauman19bac1e2014-05-06 15:23:49 -04001475 loopRepTestBlock[loopRepDepth] = testBlock;
1476 loopRepEndBlock[loopRepDepth] = endBlock;
1477
Nicolas Capens7551ac62016-01-20 17:11:53 -05001478 Int4 restoreBreak = enableBreak;
Nicolas Capens2f490f02018-11-01 16:53:36 -04001479 restoreContinue.push_back(enableContinue);
John Bauman19bac1e2014-05-06 15:23:49 -04001480
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001481 // TODO: jump(testBlock)
John Bauman19bac1e2014-05-06 15:23:49 -04001482 Nucleus::createBr(testBlock);
1483 Nucleus::setInsertBlock(testBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001484
Nicolas Capensc2534f42016-04-04 11:13:24 -04001485 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001486 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001487 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001488 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens6d123312018-01-08 12:57:52 -05001489 if(shader->containsBreakInstruction()) condition &= enableBreak;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001490 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001491
1492 Bool notAllFalse = SignMask(condition) != 0;
1493 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001494
John Bauman19bac1e2014-05-06 15:23:49 -04001495 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001496 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001497
John Bauman19bac1e2014-05-06 15:23:49 -04001498 Nucleus::setInsertBlock(loopBlock);
1499
1500 loopRepDepth++;
Nicolas Capens6e8ec332018-11-06 11:56:21 -05001501 scalar = false;
John Bauman19bac1e2014-05-06 15:23:49 -04001502 }
1503
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001504 void VertexProgram::SWITCH()
1505 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001506 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001507
1508 loopRepTestBlock[loopRepDepth] = nullptr;
1509 loopRepEndBlock[loopRepDepth] = endBlock;
1510
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001511 Int4 restoreBreak = enableBreak;
1512
1513 BasicBlock *currentBlock = Nucleus::getInsertBlock();
1514
1515 Nucleus::setInsertBlock(endBlock);
1516 enableBreak = restoreBreak;
1517
1518 Nucleus::setInsertBlock(currentBlock);
1519
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001520 loopRepDepth++;
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001521 }
1522
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001523 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001524 {
John Bauman19bac1e2014-05-06 15:23:49 -04001525 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001526 {
1527 returnBlock = Nucleus::createBasicBlock();
1528 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001529 }
1530 else
1531 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001532 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001533
John Bauman19bac1e2014-05-06 15:23:49 -04001534 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001535 {
John Bauman19bac1e2014-05-06 15:23:49 -04001536 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001537 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001538
Nicolas Capens19336542016-09-26 10:32:29 -04001539 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001540 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001541
1542 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1543 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001544 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001545 }
1546 }
1547 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1548 {
1549 Nucleus::createBr(callRetBlock[currentLabel][0]);
1550 }
1551 else // Function isn't called
1552 {
1553 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001554 }
1555
1556 Nucleus::setInsertBlock(unreachableBlock);
1557 Nucleus::createUnreachable();
1558 }
1559 }
1560
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001561 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001562 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001563 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001564
John Bauman19bac1e2014-05-06 15:23:49 -04001565 // FIXME: Return from function if all instances left
1566 // FIXME: Use enableLeave in other control-flow constructs
1567 }
John Bauman89401822014-05-06 15:04:28 -04001568
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001569 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001570 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001571 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001572 }
1573
Nicolas Capensa0b57832017-11-07 13:07:53 -05001574 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001575 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001576 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001577 }
1578
Nicolas Capensa0b57832017-11-07 13:07:53 -05001579 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001580 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001581 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001582 }
1583
Nicolas Capensa0b57832017-11-07 13:07:53 -05001584 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001585 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001586 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001587 }
1588
Nicolas Capensa0b57832017-11-07 13:07:53 -05001589 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001590 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001591 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001592 }
1593
Nicolas Capensa0b57832017-11-07 13:07:53 -05001594 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001595 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001596 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001597 }
1598
Nicolas Capensa0b57832017-11-07 13:07:53 -05001599 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001600 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001601 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1602 }
1603
1604 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1605 {
1606 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001607 }
1608
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001609 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001610 {
Nicolas Capens79d0e562018-11-27 17:07:49 -05001611 bool uniformSampler = (src1.type == Shader::PARAMETER_SAMPLER && src1.rel.type == Shader::PARAMETER_VOID);
1612 Int index = uniformSampler ? src1.index : As<Int>(Float(fetchRegister(src1).x.x));
1613 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + index * sizeof(Texture);
1614
Nicolas Capens89a218b2017-11-07 13:05:20 -05001615 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001616 }
1617
Nicolas Capensa0b57832017-11-07 13:07:53 -05001618 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 -04001619 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001620 Vector4f tmp;
1621
John Bauman19bac1e2014-05-06 15:23:49 -04001622 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1623 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001624 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001625 }
1626 else
1627 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001628 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001629
Nicolas Capensc2534f42016-04-04 11:13:24 -04001630 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001631 {
1632 if(shader->usesSampler(i))
1633 {
1634 If(index == i)
1635 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001636 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001637 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1638 }
1639 }
1640 }
1641 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001642
Nicolas Capens89a218b2017-11-07 13:05:20 -05001643 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001644 c.x = tmp[(s.swizzle >> 0) & 0x3];
1645 c.y = tmp[(s.swizzle >> 2) & 0x3];
1646 c.z = tmp[(s.swizzle >> 4) & 0x3];
1647 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001648
1649 return c;
1650 }
1651
Nicolas Capensa0b57832017-11-07 13:07:53 -05001652 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001653 {
1654 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001655 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 -04001656 }
John Bauman89401822014-05-06 15:04:28 -04001657}