blob: 9a8495f4aaa7f275193219ce921da45739457465 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "VertexProgram.hpp"
16
John Bauman89401822014-05-06 15:04:28 -040017#include "VertexShader.hpp"
John Bauman89401822014-05-06 15:04:28 -040018#include "SamplerCore.hpp"
Nicolas Capens708c24b2017-10-26 13:07:10 -040019#include "Renderer/Renderer.hpp"
20#include "Renderer/Vertex.hpp"
21#include "Common/Half.hpp"
22#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040023
John Bauman89401822014-05-06 15:04:28 -040024namespace sw
25{
Nicolas Capens7551ac62016-01-20 17:11:53 -050026 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader)
27 : VertexRoutine(state, shader), shader(shader), r(shader->dynamicallyIndexedTemporaries)
John Bauman89401822014-05-06 15:04:28 -040028 {
John Bauman89401822014-05-06 15:04:28 -040029 ifDepth = 0;
30 loopRepDepth = 0;
31 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040032 currentLabel = -1;
33 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040034
35 for(int i = 0; i < 2048; i++)
36 {
37 labelBlock[i] = 0;
38 }
Nicolas Capens7551ac62016-01-20 17:11:53 -050039
40 loopDepth = -1;
41 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
42
43 if(shader && shader->containsBreakInstruction())
44 {
45 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
46 }
47
48 if(shader && shader->containsContinueInstruction())
49 {
50 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
51 }
52
Alexis Hetu02ad0aa2016-08-02 11:18:14 -040053 if(shader->isInstanceIdDeclared())
Nicolas Capens7551ac62016-01-20 17:11:53 -050054 {
55 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
56 }
John Bauman89401822014-05-06 15:04:28 -040057 }
58
59 VertexProgram::~VertexProgram()
60 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040061 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040062 {
63 delete sampler[i];
64 }
65 }
66
Alexis Hetu877ddfc2017-07-25 17:48:00 -040067 void VertexProgram::pipeline(UInt& index)
John Bauman89401822014-05-06 15:04:28 -040068 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040069 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040070 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050071 sampler[i] = new SamplerCore(constants, state.samplerState[i]);
John Bauman89401822014-05-06 15:04:28 -040072 }
73
74 if(!state.preTransformed)
75 {
Alexis Hetu877ddfc2017-07-25 17:48:00 -040076 program(index);
John Bauman89401822014-05-06 15:04:28 -040077 }
78 else
79 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050080 passThrough();
John Bauman89401822014-05-06 15:04:28 -040081 }
82 }
83
Alexis Hetu877ddfc2017-07-25 17:48:00 -040084 void VertexProgram::program(UInt& index)
John Bauman89401822014-05-06 15:04:28 -040085 {
John Bauman19bac1e2014-05-06 15:23:49 -040086 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040087
John Bauman19bac1e2014-05-06 15:23:49 -040088 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040089
Nicolas Capens7551ac62016-01-20 17:11:53 -050090 enableIndex = 0;
91 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040092
Nicolas Capens4677a5f2014-05-06 16:42:26 -040093 if(shader->containsLeaveInstruction())
94 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050095 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040096 }
97
Alexis Hetu877ddfc2017-07-25 17:48:00 -040098 if(shader->isVertexIdDeclared())
99 {
100 if(state.textureSampling)
101 {
102 vertexID = Int4(index);
103 }
104 else
105 {
106 vertexID = Insert(vertexID, As<Int>(index), 0);
107 vertexID = Insert(vertexID, As<Int>(index + 1), 1);
108 vertexID = Insert(vertexID, As<Int>(index + 2), 2);
109 vertexID = Insert(vertexID, As<Int>(index + 3), 3);
110 }
111 }
112
John Bauman19bac1e2014-05-06 15:23:49 -0400113 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -0500114 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400115 {
John Bauman19bac1e2014-05-06 15:23:49 -0400116 const Shader::Instruction *instruction = shader->getInstruction(i);
117 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400118
John Bauman19bac1e2014-05-06 15:23:49 -0400119 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
120 {
121 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400122
John Bauman19bac1e2014-05-06 15:23:49 -0400123 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
124 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
125 }
126 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500127
Alexis Hetu903e0252014-11-25 14:25:32 -0500128 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400129 {
130 const Shader::Instruction *instruction = shader->getInstruction(i);
131 Shader::Opcode opcode = instruction->opcode;
132
133 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400134 {
135 continue;
136 }
137
John Bauman19bac1e2014-05-06 15:23:49 -0400138 Dst dst = instruction->dst;
139 Src src0 = instruction->src[0];
140 Src src1 = instruction->src[1];
141 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400142 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400143 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400144
John Bauman19bac1e2014-05-06 15:23:49 -0400145 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400146 Control control = instruction->control;
147 bool integer = dst.type == Shader::PARAMETER_ADDR;
148 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400149
John Bauman19bac1e2014-05-06 15:23:49 -0400150 Vector4f d;
151 Vector4f s0;
152 Vector4f s1;
153 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400154 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400155 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400156
Nicolas Capensc2534f42016-04-04 11:13:24 -0400157 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
158 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
159 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
160 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
161 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400162
163 switch(opcode)
164 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500165 case Shader::OPCODE_VS_1_0: break;
166 case Shader::OPCODE_VS_1_1: break;
167 case Shader::OPCODE_VS_2_0: break;
168 case Shader::OPCODE_VS_2_x: break;
169 case Shader::OPCODE_VS_2_sw: break;
170 case Shader::OPCODE_VS_3_0: break;
171 case Shader::OPCODE_VS_3_sw: break;
172 case Shader::OPCODE_DCL: break;
173 case Shader::OPCODE_DEF: break;
174 case Shader::OPCODE_DEFI: break;
175 case Shader::OPCODE_DEFB: break;
176 case Shader::OPCODE_NOP: break;
177 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400178 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500179 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400180 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500181 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
182 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
183 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
184 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
185 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
186 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
187 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
188 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
189 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
190 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
191 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
192 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
193 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
194 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
195 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
196 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
197 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400198 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
199 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
200 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500201 case Shader::OPCODE_ATT: att(d, s0, s1); break;
202 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
203 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
204 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
205 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
206 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400207 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
208 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400209 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500210 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400211 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500212 case Shader::OPCODE_LIT: lit(d, s0); break;
213 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
214 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
215 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
216 case Shader::OPCODE_LOG: log(d, s0, pp); break;
217 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
218 case Shader::OPCODE_STEP: step(d, s0, s1); break;
219 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400220 case Shader::OPCODE_FLOATBITSTOINT:
221 case Shader::OPCODE_FLOATBITSTOUINT:
222 case Shader::OPCODE_INTBITSTOFLOAT:
223 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400224 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
225 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400226 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400227 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
228 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400229 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500230 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
231 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
232 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
233 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
234 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
235 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
236 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
237 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400238 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
239 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500240 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400241 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
242 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500243 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400244 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400245 case Shader::OPCODE_NEG: neg(d, s0); break;
246 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500247 case Shader::OPCODE_F2B: f2b(d, s0); break;
248 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400249 case Shader::OPCODE_F2I: f2i(d, s0); break;
250 case Shader::OPCODE_I2F: i2f(d, s0); break;
251 case Shader::OPCODE_F2U: f2u(d, s0); break;
252 case Shader::OPCODE_U2F: u2f(d, s0); break;
253 case Shader::OPCODE_I2B: i2b(d, s0); break;
254 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500255 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400256 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500257 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
258 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
259 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
260 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
261 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
262 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
263 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400264 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
265 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500266 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400267 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
268 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
269 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500270 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
271 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
272 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
273 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
274 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
275 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
276 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
277 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
278 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
279 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
280 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
281 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
282 case Shader::OPCODE_SGE: step(d, s1, s0); break;
283 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400284 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500285 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
286 case Shader::OPCODE_COS: cos(d, s0, pp); break;
287 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
288 case Shader::OPCODE_TAN: tan(d, s0); break;
289 case Shader::OPCODE_ACOS: acos(d, s0); break;
290 case Shader::OPCODE_ASIN: asin(d, s0); break;
291 case Shader::OPCODE_ATAN: atan(d, s0); break;
292 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
293 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
294 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
295 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
296 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
297 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
298 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
299 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
300 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400301 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500302 case Shader::OPCODE_BREAK: BREAK(); break;
303 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
304 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
305 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
306 case Shader::OPCODE_TEST: TEST(); break;
307 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
308 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
309 case Shader::OPCODE_ELSE: ELSE(); break;
310 case Shader::OPCODE_ENDIF: ENDIF(); break;
311 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
312 case Shader::OPCODE_ENDREP: ENDREP(); break;
313 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400314 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500315 case Shader::OPCODE_IF: IF(src0); break;
316 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
317 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
318 case Shader::OPCODE_LOOP: LOOP(src1); break;
319 case Shader::OPCODE_REP: REP(src0); break;
320 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400321 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500322 case Shader::OPCODE_RET: RET(); break;
323 case Shader::OPCODE_LEAVE: LEAVE(); break;
324 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
325 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400326 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500327 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
328 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
329 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
330 case Shader::OPCODE_ALL: all(d.x, s0); break;
331 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400332 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
333 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
334 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
335 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400336 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
337 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500338 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
339 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400340 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500341 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
Meng-Lin Wu9d62c482016-06-14 11:11:25 -0400342 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1); break;
343 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500344 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
345 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
346 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
347 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400348 default:
349 ASSERT(false);
350 }
351
John Bauman19bac1e2014-05-06 15:23:49 -0400352 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400353 {
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400355 {
John Bauman19bac1e2014-05-06 15:23:49 -0400356 switch(opcode)
357 {
358 case Shader::OPCODE_DIV:
359 if(dst.x) d.x = Trunc(d.x);
360 if(dst.y) d.y = Trunc(d.y);
361 if(dst.z) d.z = Trunc(d.z);
362 if(dst.w) d.w = Trunc(d.w);
363 break;
364 default:
365 break; // No truncation to integer required when arguments are integer
366 }
John Bauman89401822014-05-06 15:04:28 -0400367 }
368
John Bauman19bac1e2014-05-06 15:23:49 -0400369 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400370 {
John Bauman19bac1e2014-05-06 15:23:49 -0400371 if(dst.x) d.x = Max(d.x, Float4(0.0f));
372 if(dst.y) d.y = Max(d.y, Float4(0.0f));
373 if(dst.z) d.z = Max(d.z, Float4(0.0f));
374 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400375
John Bauman19bac1e2014-05-06 15:23:49 -0400376 if(dst.x) d.x = Min(d.x, Float4(1.0f));
377 if(dst.y) d.y = Min(d.y, Float4(1.0f));
378 if(dst.z) d.z = Min(d.z, Float4(1.0f));
379 if(dst.w) d.w = Min(d.w, Float4(1.0f));
380 }
381
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400382 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400383 {
384 Vector4f pDst; // FIXME: Rename
385
386 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400387 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500388 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400389 case Shader::PARAMETER_TEMP:
390 if(dst.rel.type == Shader::PARAMETER_VOID)
391 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500392 if(dst.x) pDst.x = r[dst.index].x;
393 if(dst.y) pDst.y = r[dst.index].y;
394 if(dst.z) pDst.z = r[dst.index].z;
395 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400396 }
397 else
398 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500399 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400400
Nicolas Capens7551ac62016-01-20 17:11:53 -0500401 if(dst.x) pDst.x = r[dst.index + a].x;
402 if(dst.y) pDst.y = r[dst.index + a].y;
403 if(dst.z) pDst.z = r[dst.index + a].z;
404 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400405 }
406 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500407 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400408 case Shader::PARAMETER_RASTOUT:
409 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400410 {
411 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500412 if(dst.x) pDst.x = o[Pos].x;
413 if(dst.y) pDst.y = o[Pos].y;
414 if(dst.z) pDst.z = o[Pos].z;
415 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400416 break;
417 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500418 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400419 break;
420 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500421 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400422 break;
423 default:
424 ASSERT(false);
425 }
426 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400427 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400428 if(dst.x) pDst.x = o[C0 + dst.index].x;
429 if(dst.y) pDst.y = o[C0 + dst.index].y;
430 if(dst.z) pDst.z = o[C0 + dst.index].z;
431 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400432 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400433 case Shader::PARAMETER_TEXCRDOUT:
434 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400435 if(version < 0x0300)
436 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500437 if(dst.x) pDst.x = o[T0 + dst.index].x;
438 if(dst.y) pDst.y = o[T0 + dst.index].y;
439 if(dst.z) pDst.z = o[T0 + dst.index].z;
440 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400441 }
442 else
443 {
John Bauman19bac1e2014-05-06 15:23:49 -0400444 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400445 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500446 if(dst.x) pDst.x = o[dst.index].x;
447 if(dst.y) pDst.y = o[dst.index].y;
448 if(dst.z) pDst.z = o[dst.index].z;
449 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400450 }
John Bauman19bac1e2014-05-06 15:23:49 -0400451 else
452 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500453 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400454
Nicolas Capens7551ac62016-01-20 17:11:53 -0500455 if(dst.x) pDst.x = o[dst.index + a].x;
456 if(dst.y) pDst.y = o[dst.index + a].y;
457 if(dst.z) pDst.z = o[dst.index + a].z;
458 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400459 }
460 }
461 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500462 case Shader::PARAMETER_LABEL: break;
463 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
464 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400465 default:
466 ASSERT(false);
467 }
468
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500469 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400470
471 Int4 xEnable = enable;
472 Int4 yEnable = enable;
473 Int4 zEnable = enable;
474 Int4 wEnable = enable;
475
476 if(predicate)
477 {
John Bauman19bac1e2014-05-06 15:23:49 -0400478 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400479
Nicolas Capens7551ac62016-01-20 17:11:53 -0500480 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
481 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
482 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
483 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400484
John Bauman19bac1e2014-05-06 15:23:49 -0400485 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400486 {
John Bauman19bac1e2014-05-06 15:23:49 -0400487 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
488 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
489 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
490 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400491 }
492 else
493 {
John Bauman19bac1e2014-05-06 15:23:49 -0400494 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
495 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
496 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
497 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400498 }
499 }
500
John Bauman19bac1e2014-05-06 15:23:49 -0400501 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
502 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
503 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
504 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400505
John Bauman19bac1e2014-05-06 15:23:49 -0400506 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
507 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
508 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
509 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400510 }
511
John Bauman19bac1e2014-05-06 15:23:49 -0400512 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400513 {
John Bauman19bac1e2014-05-06 15:23:49 -0400514 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400515 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400516 case Shader::PARAMETER_TEMP:
517 if(dst.rel.type == Shader::PARAMETER_VOID)
518 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500519 if(dst.x) r[dst.index].x = d.x;
520 if(dst.y) r[dst.index].y = d.y;
521 if(dst.z) r[dst.index].z = d.z;
522 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400523 }
524 else
525 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500526 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400527
Nicolas Capens7551ac62016-01-20 17:11:53 -0500528 if(dst.x) r[dst.index + a].x = d.x;
529 if(dst.y) r[dst.index + a].y = d.y;
530 if(dst.z) r[dst.index + a].z = d.z;
531 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400532 }
John Bauman89401822014-05-06 15:04:28 -0400533 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400534 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500535 if(dst.x) a0.x = d.x;
536 if(dst.y) a0.y = d.y;
537 if(dst.z) a0.z = d.z;
538 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400539 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400540 case Shader::PARAMETER_RASTOUT:
541 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400542 {
543 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500544 if(dst.x) o[Pos].x = d.x;
545 if(dst.y) o[Pos].y = d.y;
546 if(dst.z) o[Pos].z = d.z;
547 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400548 break;
549 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500550 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400551 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500552 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500553 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400554 break;
555 default: ASSERT(false);
556 }
557 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500558 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400559 if(dst.x) o[C0 + dst.index].x = d.x;
560 if(dst.y) o[C0 + dst.index].y = d.y;
561 if(dst.z) o[C0 + dst.index].z = d.z;
562 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400563 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400564 case Shader::PARAMETER_TEXCRDOUT:
565 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400566 if(version < 0x0300)
567 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500568 if(dst.x) o[T0 + dst.index].x = d.x;
569 if(dst.y) o[T0 + dst.index].y = d.y;
570 if(dst.z) o[T0 + dst.index].z = d.z;
571 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400572 }
573 else
574 {
John Bauman19bac1e2014-05-06 15:23:49 -0400575 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400576 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500577 if(dst.x) o[dst.index].x = d.x;
578 if(dst.y) o[dst.index].y = d.y;
579 if(dst.z) o[dst.index].z = d.z;
580 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400581 }
John Bauman19bac1e2014-05-06 15:23:49 -0400582 else
583 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500584 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400585
Nicolas Capens7551ac62016-01-20 17:11:53 -0500586 if(dst.x) o[dst.index + a].x = d.x;
587 if(dst.y) o[dst.index + a].y = d.y;
588 if(dst.z) o[dst.index + a].z = d.z;
589 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400590 }
591 }
592 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500593 case Shader::PARAMETER_LABEL: break;
594 case Shader::PARAMETER_PREDICATE: p0 = d; break;
595 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400596 default:
597 ASSERT(false);
598 }
599 }
600 }
601
John Bauman19bac1e2014-05-06 15:23:49 -0400602 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400603 {
604 Nucleus::setInsertBlock(returnBlock);
605 }
606 }
607
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500608 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400609 {
John Bauman19bac1e2014-05-06 15:23:49 -0400610 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400611 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400612 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400613 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400614 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400615
616 switch(usage)
617 {
618 case 0xFF:
619 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400620 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500621 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400622 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400623 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500624 o[i].x = v[i].x;
625 o[i].y = v[i].y;
626 o[i].z = v[i].z;
627 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400628 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400629 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500630 o[i].x = v[i].x;
631 o[i].y = v[i].y;
632 o[i].z = v[i].z;
633 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400634 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400635 case Shader::USAGE_COLOR:
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_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500642 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400643 break;
644 default:
645 ASSERT(false);
646 }
647 }
648 }
649 else
650 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500651 o[Pos].x = v[PositionT].x;
652 o[Pos].y = v[PositionT].y;
653 o[Pos].z = v[PositionT].z;
654 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400655
656 for(int i = 0; i < 2; i++)
657 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400658 o[C0 + i].x = v[Color0 + i].x;
659 o[C0 + i].y = v[Color0 + i].y;
660 o[C0 + i].z = v[Color0 + i].z;
661 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400662 }
663
664 for(int i = 0; i < 8; i++)
665 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500666 o[T0 + i].x = v[TexCoord0 + i].x;
667 o[T0 + i].y = v[TexCoord0 + i].y;
668 o[T0 + i].z = v[TexCoord0 + i].z;
669 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400670 }
671
Nicolas Capens7551ac62016-01-20 17:11:53 -0500672 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400673 }
674 }
675
Nicolas Capensc2534f42016-04-04 11:13:24 -0400676 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400677 {
John Bauman19bac1e2014-05-06 15:23:49 -0400678 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500679 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400680
John Bauman89401822014-05-06 15:04:28 -0400681 switch(src.type)
682 {
John Bauman19bac1e2014-05-06 15:23:49 -0400683 case Shader::PARAMETER_TEMP:
684 if(src.rel.type == Shader::PARAMETER_VOID)
685 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500686 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400687 }
688 else
689 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400690 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400691 }
692 break;
693 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500694 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400695 break;
696 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400697 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400698 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500699 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400700 }
701 else
702 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400703 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400704 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400705 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500706 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400707 case Shader::PARAMETER_FLOAT4LITERAL:
708 reg.x = Float4(src.value[0]);
709 reg.y = Float4(src.value[1]);
710 reg.z = Float4(src.value[2]);
711 reg.w = Float4(src.value[3]);
712 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500713 case Shader::PARAMETER_ADDR: reg = a0; break;
714 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
715 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
716 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
717 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400718 case Shader::PARAMETER_SAMPLER:
719 if(src.rel.type == Shader::PARAMETER_VOID)
720 {
721 reg.x = As<Float4>(Int4(i));
722 }
723 else if(src.rel.type == Shader::PARAMETER_TEMP)
724 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500725 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400726 }
727 return reg;
728 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400729 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400730 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500731 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400732 }
733 else
734 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400735 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400736 }
737 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400738 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400739 if(src.index == Shader::InstanceIDIndex)
740 {
741 reg.x = As<Float>(instanceID);
742 }
743 else if(src.index == Shader::VertexIDIndex)
744 {
745 reg.x = As<Float4>(vertexID);
746 }
747 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400748 return reg;
John Bauman89401822014-05-06 15:04:28 -0400749 default:
750 ASSERT(false);
751 }
752
John Bauman66b8ab22014-05-06 15:57:45 -0400753 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
754 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
755 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
756 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400757
John Bauman66b8ab22014-05-06 15:57:45 -0400758 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400759
760 switch(src.modifier)
761 {
John Bauman19bac1e2014-05-06 15:23:49 -0400762 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400763 mod.x = x;
764 mod.y = y;
765 mod.z = z;
766 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400767 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400768 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400769 mod.x = -x;
770 mod.y = -y;
771 mod.z = -z;
772 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400773 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400774 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400775 mod.x = Abs(x);
776 mod.y = Abs(y);
777 mod.z = Abs(z);
778 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400779 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400780 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400781 mod.x = -Abs(x);
782 mod.y = -Abs(y);
783 mod.z = -Abs(z);
784 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400785 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400786 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400787 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
788 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
789 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
790 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400791 break;
792 default:
793 ASSERT(false);
794 }
795
796 return mod;
797 }
798
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400799 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
800 {
801 if(bufferIndex == -1)
802 {
803 return data + OFFSET(DrawData, vs.c[index]);
804 }
805 else
806 {
807 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
808 }
809 }
810
811 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
812 {
813 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
814 }
815
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500816 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400817 {
John Bauman19bac1e2014-05-06 15:23:49 -0400818 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500819 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400820
821 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
822 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400823 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400824
825 c.x = c.x.xxxx;
826 c.y = c.y.yyyy;
827 c.z = c.z.zzzz;
828 c.w = c.w.wwww;
829
Nicolas Capenseafdb222015-05-15 15:24:08 -0400830 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400831 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500832 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400833 {
834 const Shader::Instruction &instruction = *shader->getInstruction(j);
835
836 if(instruction.opcode == Shader::OPCODE_DEF)
837 {
838 if(instruction.dst.index == i)
839 {
840 c.x = Float4(instruction.src[0].value[0]);
841 c.y = Float4(instruction.src[0].value[1]);
842 c.z = Float4(instruction.src[0].value[2]);
843 c.w = Float4(instruction.src[0].value[3]);
844
845 break;
846 }
847 }
848 }
849 }
850 }
851 else if(src.rel.type == Shader::PARAMETER_LOOP)
852 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500853 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400854
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400855 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400856
857 c.x = c.x.xxxx;
858 c.y = c.y.yyyy;
859 c.z = c.z.zzzz;
860 c.w = c.w.wwww;
861 }
862 else
863 {
864 if(src.rel.deterministic)
865 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400866 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400867
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400868 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500869
John Bauman19bac1e2014-05-06 15:23:49 -0400870 c.x = c.x.xxxx;
871 c.y = c.y.yyyy;
872 c.z = c.z.zzzz;
873 c.w = c.w.wwww;
874 }
875 else
876 {
877 int component = src.rel.swizzle & 0x03;
878 Float4 a;
879
880 switch(src.rel.type)
881 {
Nicolas Capens32980ac2016-08-15 15:33:58 -0400882 case Shader::PARAMETER_ADDR: a = a0[component]; break;
883 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
884 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
885 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
886 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400887 case Shader::PARAMETER_MISCTYPE:
888 if(src.rel.index == Shader::InstanceIDIndex)
889 {
890 a = As<Float4>(Int4(instanceID)); break;
891 }
892 else if(src.rel.index == Shader::VertexIDIndex)
893 {
894 a = As<Float4>(vertexID); break;
895 }
896 else ASSERT(false);
897 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400898 default: ASSERT(false);
899 }
900
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400901 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400902
Alexis Hetu028f41b2016-01-13 14:40:47 -0500903 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500904
John Bauman19bac1e2014-05-06 15:23:49 -0400905 Int index0 = Extract(index, 0);
906 Int index1 = Extract(index, 1);
907 Int index2 = Extract(index, 2);
908 Int index3 = Extract(index, 3);
909
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400910 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);
John Bauman19bac1e2014-05-06 15:23:49 -0400914
915 transpose4x4(c.x, c.y, c.z, c.w);
916 }
917 }
918
919 return c;
920 }
921
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400922 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400923 {
924 ASSERT(var.rel.deterministic);
925
926 if(var.rel.type == Shader::PARAMETER_TEMP)
927 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500928 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400929 }
930 else if(var.rel.type == Shader::PARAMETER_INPUT)
931 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500932 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400933 }
934 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
935 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500936 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400937 }
938 else if(var.rel.type == Shader::PARAMETER_CONST)
939 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500940 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400941 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500942 else if(var.rel.type == Shader::PARAMETER_LOOP)
943 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500944 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500945 }
John Bauman19bac1e2014-05-06 15:23:49 -0400946 else ASSERT(false);
947
948 return 0;
949 }
950
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500951 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400952 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500953 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500954
John Baumand4ae8632014-05-06 16:18:33 -0400955 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400956 {
John Baumand4ae8632014-05-06 16:18:33 -0400957 if(shader->containsBreakInstruction() && instruction->analysisBreak)
958 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500959 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400960 }
John Bauman19bac1e2014-05-06 15:23:49 -0400961
John Baumand4ae8632014-05-06 16:18:33 -0400962 if(shader->containsContinueInstruction() && instruction->analysisContinue)
963 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500964 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400965 }
John Bauman19bac1e2014-05-06 15:23:49 -0400966
John Baumand4ae8632014-05-06 16:18:33 -0400967 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
968 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500969 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400970 }
John Bauman19bac1e2014-05-06 15:23:49 -0400971 }
972
973 return enable;
974 }
975
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500976 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400977 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400978 Vector4f row0 = fetchRegister(src1, 0);
979 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400980
981 dst.x = dot3(src0, row0);
982 dst.y = dot3(src0, row1);
983 }
984
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500985 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400986 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400987 Vector4f row0 = fetchRegister(src1, 0);
988 Vector4f row1 = fetchRegister(src1, 1);
989 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400990
991 dst.x = dot3(src0, row0);
992 dst.y = dot3(src0, row1);
993 dst.z = dot3(src0, row2);
994 }
995
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500996 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400997 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400998 Vector4f row0 = fetchRegister(src1, 0);
999 Vector4f row1 = fetchRegister(src1, 1);
1000 Vector4f row2 = fetchRegister(src1, 2);
1001 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001002
1003 dst.x = dot3(src0, row0);
1004 dst.y = dot3(src0, row1);
1005 dst.z = dot3(src0, row2);
1006 dst.w = dot3(src0, row3);
1007 }
1008
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001009 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001010 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001011 Vector4f row0 = fetchRegister(src1, 0);
1012 Vector4f row1 = fetchRegister(src1, 1);
1013 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001014
1015 dst.x = dot4(src0, row0);
1016 dst.y = dot4(src0, row1);
1017 dst.z = dot4(src0, row2);
1018 }
1019
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001020 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001021 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001022 Vector4f row0 = fetchRegister(src1, 0);
1023 Vector4f row1 = fetchRegister(src1, 1);
1024 Vector4f row2 = fetchRegister(src1, 2);
1025 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001026
1027 dst.x = dot4(src0, row0);
1028 dst.y = dot4(src0, row1);
1029 dst.z = dot4(src0, row2);
1030 dst.w = dot4(src0, row3);
1031 }
1032
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001033 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001034 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001035 BasicBlock *deadBlock = Nucleus::createBasicBlock();
1036 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001037
1038 if(breakDepth == 0)
1039 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001040 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001041 Nucleus::createBr(endBlock);
1042 }
1043 else
1044 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001045 enableBreak = enableBreak & ~enableStack[enableIndex];
1046 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001047
Nicolas Capens7551ac62016-01-20 17:11:53 -05001048 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001049 branch(allBreak, endBlock, deadBlock);
1050 }
1051
1052 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001053 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001054 }
1055
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001056 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001057 {
1058 Int4 condition;
1059
1060 switch(control)
1061 {
John Bauman19bac1e2014-05-06 15:23:49 -04001062 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1063 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1064 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1065 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1066 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1067 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001068 default:
1069 ASSERT(false);
1070 }
1071
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001072 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001073 }
1074
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001075 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001076 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001077 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001078
John Bauman19bac1e2014-05-06 15:23:49 -04001079 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001080 {
1081 condition = ~condition;
1082 }
1083
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001084 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001085 }
1086
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001087 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001088 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001089 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001090
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001091 BasicBlock *continueBlock = Nucleus::createBasicBlock();
1092 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001093
Nicolas Capens7551ac62016-01-20 17:11:53 -05001094 enableBreak = enableBreak & ~condition;
1095 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001096
Nicolas Capens7551ac62016-01-20 17:11:53 -05001097 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001098 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001099
John Bauman89401822014-05-06 15:04:28 -04001100 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001101 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001102 }
1103
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001104 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001105 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001106 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001107 }
1108
1109 void VertexProgram::TEST()
1110 {
1111 whileTest = true;
1112 }
1113
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001114 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001115 {
1116 if(!labelBlock[labelIndex])
1117 {
1118 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1119 }
1120
John Bauman19bac1e2014-05-06 15:23:49 -04001121 if(callRetBlock[labelIndex].size() > 1)
1122 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001123 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001124 }
John Bauman89401822014-05-06 15:04:28 -04001125
Nicolas Capens7551ac62016-01-20 17:11:53 -05001126 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001127
1128 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001129 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1130
Nicolas Capens7551ac62016-01-20 17:11:53 -05001131 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001132 }
1133
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001134 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001135 {
John Bauman19bac1e2014-05-06 15:23:49 -04001136 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001137 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001138 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001139 }
John Bauman19bac1e2014-05-06 15:23:49 -04001140 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001141 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001142 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001143 }
1144 else ASSERT(false);
1145 }
1146
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001147 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001148 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001149 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001150
John Bauman19bac1e2014-05-06 15:23:49 -04001151 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001152 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001153 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001154 }
1155
1156 if(!labelBlock[labelIndex])
1157 {
1158 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1159 }
1160
John Bauman19bac1e2014-05-06 15:23:49 -04001161 if(callRetBlock[labelIndex].size() > 1)
1162 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001163 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001164 }
John Bauman89401822014-05-06 15:04:28 -04001165
Nicolas Capens7551ac62016-01-20 17:11:53 -05001166 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001167
John Bauman19bac1e2014-05-06 15:23:49 -04001168 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1169 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1170
Nicolas Capens7551ac62016-01-20 17:11:53 -05001171 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001172 }
1173
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001174 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001175 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001176 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001177
John Bauman19bac1e2014-05-06 15:23:49 -04001178 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001179 {
1180 condition = ~condition;
1181 }
1182
Nicolas Capens7551ac62016-01-20 17:11:53 -05001183 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001184
1185 if(!labelBlock[labelIndex])
1186 {
1187 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1188 }
1189
John Bauman19bac1e2014-05-06 15:23:49 -04001190 if(callRetBlock[labelIndex].size() > 1)
1191 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001192 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001193 }
John Bauman89401822014-05-06 15:04:28 -04001194
Nicolas Capens7551ac62016-01-20 17:11:53 -05001195 enableIndex++;
1196 enableStack[enableIndex] = condition;
1197 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001198
John Bauman19bac1e2014-05-06 15:23:49 -04001199 Bool notAllFalse = SignMask(condition) != 0;
1200 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1201 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001202
Nicolas Capens7551ac62016-01-20 17:11:53 -05001203 enableIndex--;
1204 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001205 }
1206
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001207 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001208 {
1209 ifDepth--;
1210
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001211 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1212 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001213
1214 if(isConditionalIf[ifDepth])
1215 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001216 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001217 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001218
1219 branch(notAllFalse, falseBlock, endBlock);
1220
Nicolas Capens7551ac62016-01-20 17:11:53 -05001221 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001222 }
1223 else
1224 {
1225 Nucleus::createBr(endBlock);
1226 Nucleus::setInsertBlock(falseBlock);
1227 }
1228
1229 ifFalseBlock[ifDepth] = endBlock;
1230
1231 ifDepth++;
1232 }
1233
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001234 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001235 {
1236 ifDepth--;
1237
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001238 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001239
1240 Nucleus::createBr(endBlock);
1241 Nucleus::setInsertBlock(endBlock);
1242
1243 if(isConditionalIf[ifDepth])
1244 {
1245 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001246 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001247 }
1248 }
1249
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001250 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001251 {
1252 loopRepDepth--;
1253
Nicolas Capens7551ac62016-01-20 17:11:53 -05001254 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001255
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001256 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1257 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001258
1259 Nucleus::createBr(testBlock);
1260 Nucleus::setInsertBlock(endBlock);
1261
Nicolas Capens7551ac62016-01-20 17:11:53 -05001262 loopDepth--;
1263 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001264 }
1265
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001266 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001267 {
1268 loopRepDepth--;
1269
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001270 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1271 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001272
1273 Nucleus::createBr(testBlock);
1274 Nucleus::setInsertBlock(endBlock);
1275
Nicolas Capens7551ac62016-01-20 17:11:53 -05001276 loopDepth--;
1277 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001278 }
1279
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001280 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001281 {
1282 loopRepDepth--;
1283
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001284 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1285 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001286
1287 Nucleus::createBr(testBlock);
1288 Nucleus::setInsertBlock(endBlock);
1289
Nicolas Capens7551ac62016-01-20 17:11:53 -05001290 enableIndex--;
1291 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001292 whileTest = false;
1293 }
1294
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001295 void VertexProgram::ENDSWITCH()
1296 {
1297 loopRepDepth--;
1298
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001299 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001300
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001301 Nucleus::createBr(loopRepEndBlock[loopRepDepth]);
1302 Nucleus::setInsertBlock(endBlock);
1303
1304 enableIndex--;
1305 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1306 }
1307
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001308 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001309 {
John Bauman19bac1e2014-05-06 15:23:49 -04001310 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001311 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001312 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001313 }
John Bauman19bac1e2014-05-06 15:23:49 -04001314 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001315 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001316 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001317 }
John Bauman19bac1e2014-05-06 15:23:49 -04001318 else
1319 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001320 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001321 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001322 }
John Bauman89401822014-05-06 15:04:28 -04001323 }
1324
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001325 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001326 {
1327 ASSERT(ifDepth < 24 + 4);
1328
Nicolas Capens7551ac62016-01-20 17:11:53 -05001329 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001330
John Bauman19bac1e2014-05-06 15:23:49 -04001331 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001332 {
John Bauman19bac1e2014-05-06 15:23:49 -04001333 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001334 }
1335
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001336 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1337 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001338
1339 branch(condition, trueBlock, falseBlock);
1340
1341 isConditionalIf[ifDepth] = false;
1342 ifFalseBlock[ifDepth] = falseBlock;
1343
1344 ifDepth++;
1345 }
1346
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001347 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001348 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001349 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001350
John Bauman19bac1e2014-05-06 15:23:49 -04001351 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001352 {
1353 condition = ~condition;
1354 }
1355
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001356 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001357 }
1358
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001359 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001360 {
1361 Int4 condition;
1362
1363 switch(control)
1364 {
John Bauman19bac1e2014-05-06 15:23:49 -04001365 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1366 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1367 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1368 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1369 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1370 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001371 default:
1372 ASSERT(false);
1373 }
1374
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001375 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001376 }
1377
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001378 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001379 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001380 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001381
Nicolas Capens7551ac62016-01-20 17:11:53 -05001382 enableIndex++;
1383 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001384
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001385 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1386 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001387
John Bauman19bac1e2014-05-06 15:23:49 -04001388 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001389
1390 branch(notAllFalse, trueBlock, falseBlock);
1391
1392 isConditionalIf[ifDepth] = true;
1393 ifFalseBlock[ifDepth] = falseBlock;
1394
1395 ifDepth++;
1396 breakDepth++;
1397 }
1398
1399 void VertexProgram::LABEL(int labelIndex)
1400 {
John Bauman19bac1e2014-05-06 15:23:49 -04001401 if(!labelBlock[labelIndex])
1402 {
1403 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1404 }
1405
John Bauman89401822014-05-06 15:04:28 -04001406 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001407 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001408 }
1409
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001410 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001411 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001412 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001413
Nicolas Capens7551ac62016-01-20 17:11:53 -05001414 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1415 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1416 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001417
1418 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001419 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001420 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001421 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001422 }
1423
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001424 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1425 BasicBlock *testBlock = Nucleus::createBasicBlock();
1426 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001427
1428 loopRepTestBlock[loopRepDepth] = testBlock;
1429 loopRepEndBlock[loopRepDepth] = endBlock;
1430
1431 // FIXME: jump(testBlock)
1432 Nucleus::createBr(testBlock);
1433 Nucleus::setInsertBlock(testBlock);
1434
Nicolas Capens7551ac62016-01-20 17:11:53 -05001435 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001436 Nucleus::setInsertBlock(loopBlock);
1437
Nicolas Capens7551ac62016-01-20 17:11:53 -05001438 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001439
John Bauman89401822014-05-06 15:04:28 -04001440 loopRepDepth++;
1441 breakDepth = 0;
1442 }
1443
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001444 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001445 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001446 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001447
Nicolas Capens7551ac62016-01-20 17:11:53 -05001448 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1449 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001450
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001451 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1452 BasicBlock *testBlock = Nucleus::createBasicBlock();
1453 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001454
1455 loopRepTestBlock[loopRepDepth] = testBlock;
1456 loopRepEndBlock[loopRepDepth] = endBlock;
1457
1458 // FIXME: jump(testBlock)
1459 Nucleus::createBr(testBlock);
1460 Nucleus::setInsertBlock(testBlock);
1461
Nicolas Capens7551ac62016-01-20 17:11:53 -05001462 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001463 Nucleus::setInsertBlock(loopBlock);
1464
Nicolas Capens7551ac62016-01-20 17:11:53 -05001465 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001466
1467 loopRepDepth++;
1468 breakDepth = 0;
1469 }
1470
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001471 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001472 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001473 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001474
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001475 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1476 BasicBlock *testBlock = Nucleus::createBasicBlock();
1477 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001478
John Bauman19bac1e2014-05-06 15:23:49 -04001479 loopRepTestBlock[loopRepDepth] = testBlock;
1480 loopRepEndBlock[loopRepDepth] = endBlock;
1481
Nicolas Capens7551ac62016-01-20 17:11:53 -05001482 Int4 restoreBreak = enableBreak;
1483 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001484
1485 // FIXME: jump(testBlock)
1486 Nucleus::createBr(testBlock);
1487 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001488 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001489
Nicolas Capensc2534f42016-04-04 11:13:24 -04001490 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001491 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001492 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001493 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001494 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001495
1496 Bool notAllFalse = SignMask(condition) != 0;
1497 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001498
John Bauman19bac1e2014-05-06 15:23:49 -04001499 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001500 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001501
John Bauman19bac1e2014-05-06 15:23:49 -04001502 Nucleus::setInsertBlock(loopBlock);
1503
1504 loopRepDepth++;
1505 breakDepth = 0;
1506 }
1507
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001508 void VertexProgram::SWITCH()
1509 {
1510 enableIndex++;
1511 enableStack[enableIndex] = Int4(0xFFFFFFFF);
1512
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001513 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001514
1515 loopRepTestBlock[loopRepDepth] = nullptr;
1516 loopRepEndBlock[loopRepDepth] = endBlock;
1517
1518 loopRepDepth++;
1519 breakDepth = 0;
1520 }
1521
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001522 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001523 {
John Bauman19bac1e2014-05-06 15:23:49 -04001524 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001525 {
1526 returnBlock = Nucleus::createBasicBlock();
1527 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001528 }
1529 else
1530 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001531 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001532
John Bauman19bac1e2014-05-06 15:23:49 -04001533 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001534 {
John Bauman19bac1e2014-05-06 15:23:49 -04001535 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001536 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001537
Nicolas Capens19336542016-09-26 10:32:29 -04001538 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001539 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001540
1541 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1542 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001543 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001544 }
1545 }
1546 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1547 {
1548 Nucleus::createBr(callRetBlock[currentLabel][0]);
1549 }
1550 else // Function isn't called
1551 {
1552 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001553 }
1554
1555 Nucleus::setInsertBlock(unreachableBlock);
1556 Nucleus::createUnreachable();
1557 }
1558 }
1559
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001560 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001561 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001562 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001563
John Bauman19bac1e2014-05-06 15:23:49 -04001564 // FIXME: Return from function if all instances left
1565 // FIXME: Use enableLeave in other control-flow constructs
1566 }
John Bauman89401822014-05-06 15:04:28 -04001567
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001568 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001569 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001570 sampleTexture(dst, src1, src0, a0, a0, src0, Lod);
John Bauman89401822014-05-06 15:04:28 -04001571 }
John Bauman19bac1e2014-05-06 15:23:49 -04001572
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001573 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001574 {
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001575 src0.w = Float(0.0f);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001576 sampleTexture(dst, src1, src0, a0, a0, src0, Lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001577 }
1578
Meng-Lin Wu2337a192016-06-01 13:54:07 -04001579 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001580 {
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001581 src0.w = Float(0.0f);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001582 sampleTexture(dst, src1, src0, a0, a0, src2, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001583 }
1584
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001585 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001586 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001587 sampleTexture(dst, src1, src0, a0, a0, offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001588 }
1589
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001590 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001591 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001592 sampleTexture(dst, src1, src0, src0, src0, src0, Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001593 }
1594
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001595 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001596 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001597 sampleTexture(dst, src1, src0, src0, src0, offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001598 }
1599
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001600 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001601 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001602 sampleTexture(dst, src1, src0, src2, src3, src0, Grad);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001603 }
1604
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001605 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001606 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001607 sampleTexture(dst, src1, src0, src2, src3, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001608 }
1609
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001610 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001611 {
Alexis Hetu95ac1872016-06-06 13:26:52 -04001612 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
1613 sampler[src1.index]->textureSize(texture, dst, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001614 }
1615
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001616 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Vector4f &uvwq, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
John Bauman19bac1e2014-05-06 15:23:49 -04001617 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001618 Vector4f tmp;
1619
John Bauman19bac1e2014-05-06 15:23:49 -04001620 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1621 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001622 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + s.index * sizeof(Texture);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001623 sampler[s.index]->sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001624 }
1625 else
1626 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001627 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001628
Nicolas Capensc2534f42016-04-04 11:13:24 -04001629 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001630 {
1631 if(shader->usesSampler(i))
1632 {
1633 If(index == i)
1634 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001635 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + i * sizeof(Texture);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001636 sampler[i]->sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, 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
1643 c.x = tmp[(s.swizzle >> 0) & 0x3];
1644 c.y = tmp[(s.swizzle >> 2) & 0x3];
1645 c.z = tmp[(s.swizzle >> 4) & 0x3];
1646 c.w = tmp[(s.swizzle >> 6) & 0x3];
John Bauman19bac1e2014-05-06 15:23:49 -04001647 }
John Bauman89401822014-05-06 15:04:28 -04001648}