blob: ad4e37bd4e5c1e93bc6728d5eaba19caf7f95e92 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
15#include "VertexProgram.hpp"
16
John Bauman89401822014-05-06 15:04:28 -040017#include "VertexShader.hpp"
John Bauman89401822014-05-06 15:04:28 -040018#include "SamplerCore.hpp"
Nicolas Capens708c24b2017-10-26 13:07:10 -040019#include "Renderer/Renderer.hpp"
20#include "Renderer/Vertex.hpp"
21#include "Common/Half.hpp"
22#include "Common/Debug.hpp"
John Bauman89401822014-05-06 15:04:28 -040023
John Bauman89401822014-05-06 15:04:28 -040024namespace sw
25{
Nicolas Capens7551ac62016-01-20 17:11:53 -050026 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader)
Nicolas Capens5bff4052018-05-28 13:18:59 -040027 : VertexRoutine(state, shader), shader(shader), r(shader->indirectAddressableTemporaries)
John Bauman89401822014-05-06 15:04:28 -040028 {
John Bauman89401822014-05-06 15:04:28 -040029 ifDepth = 0;
30 loopRepDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040031 currentLabel = -1;
32 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040033
34 for(int i = 0; i < 2048; i++)
35 {
36 labelBlock[i] = 0;
37 }
Nicolas Capens7551ac62016-01-20 17:11:53 -050038
39 loopDepth = -1;
40 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
41
Nicolas Capensc62fad32018-01-26 01:55:31 +000042 if(shader->containsBreakInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050043 {
44 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
45 }
46
Nicolas Capensc62fad32018-01-26 01:55:31 +000047 if(shader->containsContinueInstruction())
Nicolas Capens7551ac62016-01-20 17:11:53 -050048 {
49 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
50 }
51
Alexis Hetu02ad0aa2016-08-02 11:18:14 -040052 if(shader->isInstanceIdDeclared())
Nicolas Capens7551ac62016-01-20 17:11:53 -050053 {
54 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
55 }
John Bauman89401822014-05-06 15:04:28 -040056 }
57
58 VertexProgram::~VertexProgram()
59 {
John Bauman89401822014-05-06 15:04:28 -040060 }
61
Nicolas Capens4b743732018-05-28 13:22:07 -040062 void VertexProgram::pipeline(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040063 {
John Bauman89401822014-05-06 15:04:28 -040064 if(!state.preTransformed)
65 {
Alexis Hetu877ddfc2017-07-25 17:48:00 -040066 program(index);
John Bauman89401822014-05-06 15:04:28 -040067 }
68 else
69 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050070 passThrough();
John Bauman89401822014-05-06 15:04:28 -040071 }
72 }
73
Nicolas Capens4b743732018-05-28 13:22:07 -040074 void VertexProgram::program(UInt &index)
John Bauman89401822014-05-06 15:04:28 -040075 {
John Bauman19bac1e2014-05-06 15:23:49 -040076 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040077
Alexis Hetu53ad4af2017-12-06 14:49:07 -050078 unsigned short shaderModel = shader->getShaderModel();
John Bauman89401822014-05-06 15:04:28 -040079
Nicolas Capens7551ac62016-01-20 17:11:53 -050080 enableIndex = 0;
81 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040082
Nicolas Capens4677a5f2014-05-06 16:42:26 -040083 if(shader->containsLeaveInstruction())
84 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050085 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040086 }
87
Alexis Hetu877ddfc2017-07-25 17:48:00 -040088 if(shader->isVertexIdDeclared())
89 {
90 if(state.textureSampling)
91 {
92 vertexID = Int4(index);
93 }
94 else
95 {
96 vertexID = Insert(vertexID, As<Int>(index), 0);
97 vertexID = Insert(vertexID, As<Int>(index + 1), 1);
98 vertexID = Insert(vertexID, As<Int>(index + 2), 2);
99 vertexID = Insert(vertexID, As<Int>(index + 3), 3);
100 }
101 }
102
John Bauman19bac1e2014-05-06 15:23:49 -0400103 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -0500104 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400105 {
John Bauman19bac1e2014-05-06 15:23:49 -0400106 const Shader::Instruction *instruction = shader->getInstruction(i);
107 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400108
John Bauman19bac1e2014-05-06 15:23:49 -0400109 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
110 {
111 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400112
John Bauman19bac1e2014-05-06 15:23:49 -0400113 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
114 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
115 }
116 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500117
Alexis Hetu903e0252014-11-25 14:25:32 -0500118 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400119 {
120 const Shader::Instruction *instruction = shader->getInstruction(i);
121 Shader::Opcode opcode = instruction->opcode;
122
123 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400124 {
125 continue;
126 }
127
John Bauman19bac1e2014-05-06 15:23:49 -0400128 Dst dst = instruction->dst;
129 Src src0 = instruction->src[0];
130 Src src1 = instruction->src[1];
131 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400132 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400133 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400134
John Bauman19bac1e2014-05-06 15:23:49 -0400135 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400136 Control control = instruction->control;
137 bool integer = dst.type == Shader::PARAMETER_ADDR;
138 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400139
John Bauman19bac1e2014-05-06 15:23:49 -0400140 Vector4f d;
141 Vector4f s0;
142 Vector4f s1;
143 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400144 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400145 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400146
Nicolas Capensc2534f42016-04-04 11:13:24 -0400147 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
148 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
149 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
150 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
151 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400152
153 switch(opcode)
154 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500155 case Shader::OPCODE_VS_1_0: break;
156 case Shader::OPCODE_VS_1_1: break;
157 case Shader::OPCODE_VS_2_0: break;
158 case Shader::OPCODE_VS_2_x: break;
159 case Shader::OPCODE_VS_2_sw: break;
160 case Shader::OPCODE_VS_3_0: break;
161 case Shader::OPCODE_VS_3_sw: break;
162 case Shader::OPCODE_DCL: break;
163 case Shader::OPCODE_DEF: break;
164 case Shader::OPCODE_DEFI: break;
165 case Shader::OPCODE_DEFB: break;
166 case Shader::OPCODE_NOP: break;
167 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400168 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500169 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400170 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500171 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
172 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
173 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
174 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
175 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
176 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
177 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
178 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
179 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
180 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
181 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
182 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
183 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
184 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
185 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
186 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
187 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400188 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
189 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
190 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500191 case Shader::OPCODE_ATT: att(d, s0, s1); break;
192 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
193 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500194 case Shader::OPCODE_EXPP: expp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500195 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
196 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400197 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
198 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400199 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500200 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400201 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500202 case Shader::OPCODE_LIT: lit(d, s0); break;
203 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
204 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500205 case Shader::OPCODE_LOGP: logp(d, s0, shaderModel); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500206 case Shader::OPCODE_LOG: log(d, s0, pp); break;
207 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
208 case Shader::OPCODE_STEP: step(d, s0, s1); break;
209 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8ef6d102017-11-09 15:49:09 -0500210 case Shader::OPCODE_ISINF: isinf(d, s0); break;
211 case Shader::OPCODE_ISNAN: isnan(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400212 case Shader::OPCODE_FLOATBITSTOINT:
213 case Shader::OPCODE_FLOATBITSTOUINT:
214 case Shader::OPCODE_INTBITSTOFLOAT:
215 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400216 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
217 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400218 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400219 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
220 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400221 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500222 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
223 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
224 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
225 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
226 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
227 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
228 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
229 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400230 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
231 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500232 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400233 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
234 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500235 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400236 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400237 case Shader::OPCODE_NEG: neg(d, s0); break;
238 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500239 case Shader::OPCODE_F2B: f2b(d, s0); break;
240 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400241 case Shader::OPCODE_F2I: f2i(d, s0); break;
242 case Shader::OPCODE_I2F: i2f(d, s0); break;
243 case Shader::OPCODE_F2U: f2u(d, s0); break;
244 case Shader::OPCODE_U2F: u2f(d, s0); break;
245 case Shader::OPCODE_I2B: i2b(d, s0); break;
246 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500247 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400248 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500249 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
250 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
251 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
252 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
253 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
254 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
255 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400256 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
257 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500258 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400259 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
260 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
261 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500262 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
263 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
264 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
265 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
266 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
267 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
268 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
269 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
270 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
271 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
272 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
273 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
274 case Shader::OPCODE_SGE: step(d, s1, s0); break;
275 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400276 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500277 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
278 case Shader::OPCODE_COS: cos(d, s0, pp); break;
279 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
280 case Shader::OPCODE_TAN: tan(d, s0); break;
281 case Shader::OPCODE_ACOS: acos(d, s0); break;
282 case Shader::OPCODE_ASIN: asin(d, s0); break;
283 case Shader::OPCODE_ATAN: atan(d, s0); break;
284 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
285 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
286 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
287 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
288 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
289 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
290 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
291 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
292 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400293 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500294 case Shader::OPCODE_BREAK: BREAK(); break;
295 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
296 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
297 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
298 case Shader::OPCODE_TEST: TEST(); break;
299 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
300 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
301 case Shader::OPCODE_ELSE: ELSE(); break;
302 case Shader::OPCODE_ENDIF: ENDIF(); break;
303 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
304 case Shader::OPCODE_ENDREP: ENDREP(); break;
305 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400306 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500307 case Shader::OPCODE_IF: IF(src0); break;
308 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
309 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
310 case Shader::OPCODE_LOOP: LOOP(src1); break;
311 case Shader::OPCODE_REP: REP(src0); break;
312 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400313 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500314 case Shader::OPCODE_RET: RET(); break;
315 case Shader::OPCODE_LEAVE: LEAVE(); break;
316 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
317 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400318 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500319 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
320 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
321 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
322 case Shader::OPCODE_ALL: all(d.x, s0); break;
323 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400324 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
325 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
326 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
327 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400328 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
329 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500330 case Shader::OPCODE_TEXLDL: TEXLOD(d, s0, src1, s0.w); break;
331 case Shader::OPCODE_TEXLOD: TEXLOD(d, s0, src1, s2.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500332 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400333 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500334 case Shader::OPCODE_TEXLODOFFSET: TEXLODOFFSET(d, s0, src1, s2, s3.x); break;
335 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2.x); break;
336 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCHOFFSET(d, s0, src1, s2, s3.x); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500337 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
Nicolas Capensa0b57832017-11-07 13:07:53 -0500338 case Shader::OPCODE_TEXGRADOFFSET: TEXGRADOFFSET(d, s0, src1, s2, s3, s4); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500339 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
340 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400341 default:
342 ASSERT(false);
343 }
344
John Bauman19bac1e2014-05-06 15:23:49 -0400345 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400346 {
John Bauman19bac1e2014-05-06 15:23:49 -0400347 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400348 {
John Bauman19bac1e2014-05-06 15:23:49 -0400349 if(dst.x) d.x = Max(d.x, Float4(0.0f));
350 if(dst.y) d.y = Max(d.y, Float4(0.0f));
351 if(dst.z) d.z = Max(d.z, Float4(0.0f));
352 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400353
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.x) d.x = Min(d.x, Float4(1.0f));
355 if(dst.y) d.y = Min(d.y, Float4(1.0f));
356 if(dst.z) d.z = Min(d.z, Float4(1.0f));
357 if(dst.w) d.w = Min(d.w, Float4(1.0f));
358 }
359
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400360 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400361 {
362 Vector4f pDst; // FIXME: Rename
363
364 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400365 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500366 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400367 case Shader::PARAMETER_TEMP:
368 if(dst.rel.type == Shader::PARAMETER_VOID)
369 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500370 if(dst.x) pDst.x = r[dst.index].x;
371 if(dst.y) pDst.y = r[dst.index].y;
372 if(dst.z) pDst.z = r[dst.index].z;
373 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400374 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400375 else if(!dst.rel.dynamic)
376 {
377 Int a = dst.index + relativeAddress(dst.rel);
378
379 if(dst.x) pDst.x = r[a].x;
380 if(dst.y) pDst.y = r[a].y;
381 if(dst.z) pDst.z = r[a].z;
382 if(dst.w) pDst.w = r[a].w;
383 }
John Bauman19bac1e2014-05-06 15:23:49 -0400384 else
385 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400386 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400387
Nicolas Capens4b743732018-05-28 13:22:07 -0400388 if(dst.x) pDst.x = r[a].x;
389 if(dst.y) pDst.y = r[a].y;
390 if(dst.z) pDst.z = r[a].z;
391 if(dst.w) pDst.w = r[a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400392 }
393 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500394 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400395 case Shader::PARAMETER_RASTOUT:
396 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400397 {
398 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500399 if(dst.x) pDst.x = o[Pos].x;
400 if(dst.y) pDst.y = o[Pos].y;
401 if(dst.z) pDst.z = o[Pos].z;
402 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400403 break;
404 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500405 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400406 break;
407 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500408 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400409 break;
410 default:
411 ASSERT(false);
412 }
413 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400414 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400415 if(dst.x) pDst.x = o[C0 + dst.index].x;
416 if(dst.y) pDst.y = o[C0 + dst.index].y;
417 if(dst.z) pDst.z = o[C0 + dst.index].z;
418 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400419 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400420 case Shader::PARAMETER_TEXCRDOUT:
421 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500422 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400423 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500424 if(dst.x) pDst.x = o[T0 + dst.index].x;
425 if(dst.y) pDst.y = o[T0 + dst.index].y;
426 if(dst.z) pDst.z = o[T0 + dst.index].z;
427 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400428 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400429 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
430 {
431 if(dst.x) pDst.x = o[dst.index].x;
432 if(dst.y) pDst.y = o[dst.index].y;
433 if(dst.z) pDst.z = o[dst.index].z;
434 if(dst.w) pDst.w = o[dst.index].w;
435 }
436 else if(!dst.rel.dynamic)
437 {
438 Int a = dst.index + relativeAddress(dst.rel);
439
440 if(dst.x) pDst.x = o[a].x;
441 if(dst.y) pDst.y = o[a].y;
442 if(dst.z) pDst.z = o[a].z;
443 if(dst.w) pDst.w = o[a].w;
444 }
John Bauman89401822014-05-06 15:04:28 -0400445 else
446 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400447 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400448
Nicolas Capens4b743732018-05-28 13:22:07 -0400449 if(dst.x) pDst.x = o[a].x;
450 if(dst.y) pDst.y = o[a].y;
451 if(dst.z) pDst.z = o[a].z;
452 if(dst.w) pDst.w = o[a].w;
John Bauman89401822014-05-06 15:04:28 -0400453 }
454 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500455 case Shader::PARAMETER_LABEL: break;
456 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
457 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400458 default:
459 ASSERT(false);
460 }
461
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500462 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400463
464 Int4 xEnable = enable;
465 Int4 yEnable = enable;
466 Int4 zEnable = enable;
467 Int4 wEnable = enable;
468
469 if(predicate)
470 {
John Bauman19bac1e2014-05-06 15:23:49 -0400471 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400472
Nicolas Capens7551ac62016-01-20 17:11:53 -0500473 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
474 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
475 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
476 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400477
John Bauman19bac1e2014-05-06 15:23:49 -0400478 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400479 {
John Bauman19bac1e2014-05-06 15:23:49 -0400480 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
481 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
482 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
483 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400484 }
485 else
486 {
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 }
493
John Bauman19bac1e2014-05-06 15:23:49 -0400494 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
495 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
496 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
497 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400498
John Bauman19bac1e2014-05-06 15:23:49 -0400499 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
500 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
501 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
502 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400503 }
504
John Bauman19bac1e2014-05-06 15:23:49 -0400505 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400506 {
John Bauman19bac1e2014-05-06 15:23:49 -0400507 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400508 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400509 case Shader::PARAMETER_TEMP:
510 if(dst.rel.type == Shader::PARAMETER_VOID)
511 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500512 if(dst.x) r[dst.index].x = d.x;
513 if(dst.y) r[dst.index].y = d.y;
514 if(dst.z) r[dst.index].z = d.z;
515 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400516 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400517 else if(!dst.rel.dynamic)
518 {
519 Int a = dst.index + relativeAddress(dst.rel);
520
521 if(dst.x) r[a].x = d.x;
522 if(dst.y) r[a].y = d.y;
523 if(dst.z) r[a].z = d.z;
524 if(dst.w) r[a].w = d.w;
525 }
John Bauman19bac1e2014-05-06 15:23:49 -0400526 else
527 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400528 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400529
Nicolas Capens4b743732018-05-28 13:22:07 -0400530 if(dst.x) r.scatter_x(a, d.x);
531 if(dst.y) r.scatter_y(a, d.y);
532 if(dst.z) r.scatter_z(a, d.z);
533 if(dst.w) r.scatter_w(a, d.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400534 }
John Bauman89401822014-05-06 15:04:28 -0400535 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400536 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500537 if(dst.x) a0.x = d.x;
538 if(dst.y) a0.y = d.y;
539 if(dst.z) a0.z = d.z;
540 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400541 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400542 case Shader::PARAMETER_RASTOUT:
543 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400544 {
545 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500546 if(dst.x) o[Pos].x = d.x;
547 if(dst.y) o[Pos].y = d.y;
548 if(dst.z) o[Pos].z = d.z;
549 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400550 break;
551 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500552 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400553 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500554 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500555 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400556 break;
557 default: ASSERT(false);
558 }
559 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500560 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400561 if(dst.x) o[C0 + dst.index].x = d.x;
562 if(dst.y) o[C0 + dst.index].y = d.y;
563 if(dst.z) o[C0 + dst.index].z = d.z;
564 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400565 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400566 case Shader::PARAMETER_TEXCRDOUT:
567 // case Shader::PARAMETER_OUTPUT:
Alexis Hetu53ad4af2017-12-06 14:49:07 -0500568 if(shaderModel < 0x0300)
John Bauman89401822014-05-06 15:04:28 -0400569 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500570 if(dst.x) o[T0 + dst.index].x = d.x;
571 if(dst.y) o[T0 + dst.index].y = d.y;
572 if(dst.z) o[T0 + dst.index].z = d.z;
573 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400574 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400575 else if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
576 {
577 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;
581 }
582 else if(!dst.rel.dynamic)
583 {
584 Int a = dst.index + relativeAddress(dst.rel);
585
586 if(dst.x) o[a].x = d.x;
587 if(dst.y) o[a].y = d.y;
588 if(dst.z) o[a].z = d.z;
589 if(dst.w) o[a].w = d.w;
590 }
John Bauman89401822014-05-06 15:04:28 -0400591 else
592 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400593 Int4 a = dst.index + dynamicAddress(dst.rel);
John Bauman19bac1e2014-05-06 15:23:49 -0400594
Nicolas Capens4b743732018-05-28 13:22:07 -0400595 if(dst.x) o.scatter_x(a, d.x);
596 if(dst.y) o.scatter_y(a, d.y);
597 if(dst.z) o.scatter_z(a, d.z);
598 if(dst.w) o.scatter_w(a, d.w);
John Bauman89401822014-05-06 15:04:28 -0400599 }
600 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500601 case Shader::PARAMETER_LABEL: break;
602 case Shader::PARAMETER_PREDICATE: p0 = d; break;
603 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400604 default:
605 ASSERT(false);
606 }
607 }
608 }
609
John Bauman19bac1e2014-05-06 15:23:49 -0400610 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400611 {
612 Nucleus::setInsertBlock(returnBlock);
613 }
614 }
615
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500616 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400617 {
John Bauman19bac1e2014-05-06 15:23:49 -0400618 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400619 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400620 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400621 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400622 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400623
624 switch(usage)
625 {
626 case 0xFF:
627 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400628 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500629 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400630 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400631 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500632 o[i].x = v[i].x;
633 o[i].y = v[i].y;
634 o[i].z = v[i].z;
635 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400636 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400637 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500638 o[i].x = v[i].x;
639 o[i].y = v[i].y;
640 o[i].z = v[i].z;
641 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400642 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400643 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500644 o[i].x = v[i].x;
645 o[i].y = v[i].y;
646 o[i].z = v[i].z;
647 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400648 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400649 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500650 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400651 break;
652 default:
653 ASSERT(false);
654 }
655 }
656 }
657 else
658 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500659 o[Pos].x = v[PositionT].x;
660 o[Pos].y = v[PositionT].y;
661 o[Pos].z = v[PositionT].z;
662 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400663
664 for(int i = 0; i < 2; i++)
665 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400666 o[C0 + i].x = v[Color0 + i].x;
667 o[C0 + i].y = v[Color0 + i].y;
668 o[C0 + i].z = v[Color0 + i].z;
669 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400670 }
671
672 for(int i = 0; i < 8; i++)
673 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500674 o[T0 + i].x = v[TexCoord0 + i].x;
675 o[T0 + i].y = v[TexCoord0 + i].y;
676 o[T0 + i].z = v[TexCoord0 + i].z;
677 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400678 }
679
Nicolas Capens7551ac62016-01-20 17:11:53 -0500680 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400681 }
682 }
683
Nicolas Capensc2534f42016-04-04 11:13:24 -0400684 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400685 {
John Bauman19bac1e2014-05-06 15:23:49 -0400686 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500687 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400688
John Bauman89401822014-05-06 15:04:28 -0400689 switch(src.type)
690 {
John Bauman19bac1e2014-05-06 15:23:49 -0400691 case Shader::PARAMETER_TEMP:
692 if(src.rel.type == Shader::PARAMETER_VOID)
693 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500694 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400695 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400696 else if(!src.rel.dynamic)
697 {
698 reg = r[i + relativeAddress(src.rel, src.bufferIndex)];
699 }
John Bauman19bac1e2014-05-06 15:23:49 -0400700 else
701 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400702 reg = r[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400703 }
704 break;
705 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500706 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400707 break;
708 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400709 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400710 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500711 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400712 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400713 else if(!src.rel.dynamic)
714 {
715 reg = v[i + relativeAddress(src.rel, src.bufferIndex)];
716 }
John Bauman19bac1e2014-05-06 15:23:49 -0400717 else
718 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400719 reg = v[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400720 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400721 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500722 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400723 case Shader::PARAMETER_FLOAT4LITERAL:
724 reg.x = Float4(src.value[0]);
725 reg.y = Float4(src.value[1]);
726 reg.z = Float4(src.value[2]);
727 reg.w = Float4(src.value[3]);
728 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500729 case Shader::PARAMETER_ADDR: reg = a0; break;
730 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
731 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
732 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
733 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400734 case Shader::PARAMETER_SAMPLER:
735 if(src.rel.type == Shader::PARAMETER_VOID)
736 {
737 reg.x = As<Float4>(Int4(i));
738 }
739 else if(src.rel.type == Shader::PARAMETER_TEMP)
740 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500741 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400742 }
743 return reg;
744 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400745 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400746 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500747 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400748 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400749 else if(!src.rel.dynamic)
750 {
751 reg = o[i + relativeAddress(src.rel, src.bufferIndex)];
752 }
John Bauman19bac1e2014-05-06 15:23:49 -0400753 else
754 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400755 reg = o[i + dynamicAddress(src.rel)];
John Bauman19bac1e2014-05-06 15:23:49 -0400756 }
757 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400758 case Shader::PARAMETER_MISCTYPE:
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400759 if(src.index == Shader::InstanceIDIndex)
760 {
761 reg.x = As<Float>(instanceID);
762 }
763 else if(src.index == Shader::VertexIDIndex)
764 {
765 reg.x = As<Float4>(vertexID);
766 }
767 else ASSERT(false);
Alexis Hetudd8df682015-06-05 17:08:39 -0400768 return reg;
John Bauman89401822014-05-06 15:04:28 -0400769 default:
770 ASSERT(false);
771 }
772
John Bauman66b8ab22014-05-06 15:57:45 -0400773 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
774 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
775 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
776 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400777
John Bauman66b8ab22014-05-06 15:57:45 -0400778 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400779
780 switch(src.modifier)
781 {
John Bauman19bac1e2014-05-06 15:23:49 -0400782 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400783 mod.x = x;
784 mod.y = y;
785 mod.z = z;
786 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400787 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400788 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400789 mod.x = -x;
790 mod.y = -y;
791 mod.z = -z;
792 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400793 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400794 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400795 mod.x = Abs(x);
796 mod.y = Abs(y);
797 mod.z = Abs(z);
798 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400799 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400800 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400801 mod.x = -Abs(x);
802 mod.y = -Abs(y);
803 mod.z = -Abs(z);
804 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400805 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400806 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400807 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
808 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
809 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
810 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400811 break;
812 default:
813 ASSERT(false);
814 }
815
816 return mod;
817 }
818
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400819 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
820 {
821 if(bufferIndex == -1)
822 {
823 return data + OFFSET(DrawData, vs.c[index]);
824 }
825 else
826 {
827 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
828 }
829 }
830
Nicolas Capens4b743732018-05-28 13:22:07 -0400831 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int &offset)
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400832 {
833 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
834 }
835
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500836 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400837 {
John Bauman19bac1e2014-05-06 15:23:49 -0400838 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500839 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400840
841 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
842 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400843 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400844
845 c.x = c.x.xxxx;
846 c.y = c.y.yyyy;
847 c.z = c.z.zzzz;
848 c.w = c.w.wwww;
849
Nicolas Capenseafdb222015-05-15 15:24:08 -0400850 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400851 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500852 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400853 {
854 const Shader::Instruction &instruction = *shader->getInstruction(j);
855
856 if(instruction.opcode == Shader::OPCODE_DEF)
857 {
858 if(instruction.dst.index == i)
859 {
860 c.x = Float4(instruction.src[0].value[0]);
861 c.y = Float4(instruction.src[0].value[1]);
862 c.z = Float4(instruction.src[0].value[2]);
863 c.w = Float4(instruction.src[0].value[3]);
864
865 break;
866 }
867 }
868 }
869 }
870 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400871 else if(!src.rel.dynamic || src.rel.type == Shader::PARAMETER_LOOP)
John Bauman19bac1e2014-05-06 15:23:49 -0400872 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400873 Int a = relativeAddress(src.rel, src.bufferIndex);
John Bauman19bac1e2014-05-06 15:23:49 -0400874
Nicolas Capens4b743732018-05-28 13:22:07 -0400875 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
John Bauman19bac1e2014-05-06 15:23:49 -0400876
877 c.x = c.x.xxxx;
878 c.y = c.y.yyyy;
879 c.z = c.z.zzzz;
880 c.w = c.w.wwww;
881 }
882 else
883 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400884 int component = src.rel.swizzle & 0x03;
885 Float4 a;
886
887 switch(src.rel.type)
John Bauman19bac1e2014-05-06 15:23:49 -0400888 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400889 case Shader::PARAMETER_ADDR: a = a0[component]; break;
890 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
891 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
892 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
893 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
894 case Shader::PARAMETER_MISCTYPE:
895 switch(src.rel.index)
John Bauman19bac1e2014-05-06 15:23:49 -0400896 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400897 case Shader::InstanceIDIndex: a = As<Float4>(Int4(instanceID)); break;
898 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400899 default: ASSERT(false);
900 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400901 break;
902 default: ASSERT(false);
John Bauman19bac1e2014-05-06 15:23:49 -0400903 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400904
905 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
906
907 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
908
909 Int index0 = Extract(index, 0);
910 Int index1 = Extract(index, 1);
911 Int index2 = Extract(index, 2);
912 Int index3 = Extract(index, 3);
913
914 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
915 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
916 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
917 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
918
919 transpose4x4(c.x, c.y, c.z, c.w);
John Bauman19bac1e2014-05-06 15:23:49 -0400920 }
921
922 return c;
923 }
924
Nicolas Capens4b743732018-05-28 13:22:07 -0400925 Int VertexProgram::relativeAddress(const Shader::Relative &rel, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400926 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400927 ASSERT(!rel.dynamic);
John Bauman19bac1e2014-05-06 15:23:49 -0400928
Nicolas Capens4b743732018-05-28 13:22:07 -0400929 if(rel.type == Shader::PARAMETER_TEMP)
John Bauman19bac1e2014-05-06 15:23:49 -0400930 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400931 return As<Int>(Extract(r[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_INPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400934 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400935 return As<Int>(Extract(v[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_OUTPUT)
John Bauman19bac1e2014-05-06 15:23:49 -0400938 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400939 return As<Int>(Extract(o[rel.index].x, 0)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400940 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400941 else if(rel.type == Shader::PARAMETER_CONST)
John Bauman19bac1e2014-05-06 15:23:49 -0400942 {
Nicolas Capens4b743732018-05-28 13:22:07 -0400943 return *Pointer<Int>(uniformAddress(bufferIndex, rel.index)) * rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400944 }
Nicolas Capens4b743732018-05-28 13:22:07 -0400945 else if(rel.type == Shader::PARAMETER_LOOP)
Nicolas Capens907700d2016-01-20 17:09:28 -0500946 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500947 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500948 }
John Bauman19bac1e2014-05-06 15:23:49 -0400949 else ASSERT(false);
950
951 return 0;
952 }
953
Nicolas Capens4b743732018-05-28 13:22:07 -0400954 Int4 VertexProgram::dynamicAddress(const Shader::Relative &rel)
955 {
956 int component = rel.swizzle & 0x03;
957 Float4 a;
958
959 switch(rel.type)
960 {
961 case Shader::PARAMETER_ADDR: a = a0[component]; break;
962 case Shader::PARAMETER_TEMP: a = r[rel.index][component]; break;
963 case Shader::PARAMETER_INPUT: a = v[rel.index][component]; break;
964 case Shader::PARAMETER_OUTPUT: a = o[rel.index][component]; break;
965 case Shader::PARAMETER_MISCTYPE:
966 switch(rel.index)
967 {
968 case Shader::InstanceIDIndex: a = As<Float>(instanceID); break;
969 case Shader::VertexIDIndex: a = As<Float4>(vertexID); break;
970 default: ASSERT(false);
971 }
972 break;
973 default: ASSERT(false);
974 }
975
976 return As<Int4>(a) * Int4(rel.scale);
977 }
978
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500979 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400980 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500981 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500982
John Baumand4ae8632014-05-06 16:18:33 -0400983 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400984 {
John Baumand4ae8632014-05-06 16:18:33 -0400985 if(shader->containsBreakInstruction() && instruction->analysisBreak)
986 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500987 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400988 }
John Bauman19bac1e2014-05-06 15:23:49 -0400989
John Baumand4ae8632014-05-06 16:18:33 -0400990 if(shader->containsContinueInstruction() && instruction->analysisContinue)
991 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500992 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400993 }
John Bauman19bac1e2014-05-06 15:23:49 -0400994
John Baumand4ae8632014-05-06 16:18:33 -0400995 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
996 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500997 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400998 }
John Bauman19bac1e2014-05-06 15:23:49 -0400999 }
1000
1001 return enable;
1002 }
1003
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001004 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001005 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001006 Vector4f row0 = fetchRegister(src1, 0);
1007 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -04001008
1009 dst.x = dot3(src0, row0);
1010 dst.y = dot3(src0, row1);
1011 }
1012
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001013 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001014 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001015 Vector4f row0 = fetchRegister(src1, 0);
1016 Vector4f row1 = fetchRegister(src1, 1);
1017 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001018
1019 dst.x = dot3(src0, row0);
1020 dst.y = dot3(src0, row1);
1021 dst.z = dot3(src0, row2);
1022 }
1023
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001024 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001025 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001026 Vector4f row0 = fetchRegister(src1, 0);
1027 Vector4f row1 = fetchRegister(src1, 1);
1028 Vector4f row2 = fetchRegister(src1, 2);
1029 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001030
1031 dst.x = dot3(src0, row0);
1032 dst.y = dot3(src0, row1);
1033 dst.z = dot3(src0, row2);
1034 dst.w = dot3(src0, row3);
1035 }
1036
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001037 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001038 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001039 Vector4f row0 = fetchRegister(src1, 0);
1040 Vector4f row1 = fetchRegister(src1, 1);
1041 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001042
1043 dst.x = dot4(src0, row0);
1044 dst.y = dot4(src0, row1);
1045 dst.z = dot4(src0, row2);
1046 }
1047
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001048 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001049 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001050 Vector4f row0 = fetchRegister(src1, 0);
1051 Vector4f row1 = fetchRegister(src1, 1);
1052 Vector4f row2 = fetchRegister(src1, 2);
1053 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001054
1055 dst.x = dot4(src0, row0);
1056 dst.y = dot4(src0, row1);
1057 dst.z = dot4(src0, row2);
1058 dst.w = dot4(src0, row3);
1059 }
1060
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001061 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001062 {
Nicolas Capens6d123312018-01-08 12:57:52 -05001063 enableBreak = enableBreak & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001064 }
1065
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001066 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001067 {
1068 Int4 condition;
1069
1070 switch(control)
1071 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001072 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1073 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1074 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1075 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1076 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1077 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001078 default:
1079 ASSERT(false);
1080 }
1081
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001082 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001083 }
1084
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001085 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001086 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001087 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001088
John Bauman19bac1e2014-05-06 15:23:49 -04001089 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001090 {
1091 condition = ~condition;
1092 }
1093
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001094 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001095 }
1096
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001097 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001098 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001099 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001100
Nicolas Capens7551ac62016-01-20 17:11:53 -05001101 enableBreak = enableBreak & ~condition;
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 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001245 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001246 }
1247 }
1248
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001249 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001250 {
1251 loopRepDepth--;
1252
Nicolas Capens7551ac62016-01-20 17:11:53 -05001253 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001254
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001255 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1256 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001257
1258 Nucleus::createBr(testBlock);
1259 Nucleus::setInsertBlock(endBlock);
1260
Nicolas Capens7551ac62016-01-20 17:11:53 -05001261 loopDepth--;
1262 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001263 }
1264
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001265 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001266 {
1267 loopRepDepth--;
1268
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001269 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1270 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001271
1272 Nucleus::createBr(testBlock);
1273 Nucleus::setInsertBlock(endBlock);
1274
Nicolas Capens7551ac62016-01-20 17:11:53 -05001275 loopDepth--;
1276 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001277 }
1278
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001279 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001280 {
1281 loopRepDepth--;
1282
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001283 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1284 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001285
1286 Nucleus::createBr(testBlock);
1287 Nucleus::setInsertBlock(endBlock);
1288
Nicolas Capens7551ac62016-01-20 17:11:53 -05001289 enableIndex--;
John Bauman19bac1e2014-05-06 15:23:49 -04001290 whileTest = false;
1291 }
1292
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001293 void VertexProgram::ENDSWITCH()
1294 {
1295 loopRepDepth--;
1296
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001297 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001298
Nicolas Capensac6d5052018-01-05 15:34:00 -05001299 Nucleus::createBr(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001300 Nucleus::setInsertBlock(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001301 }
1302
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001303 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001304 {
John Bauman19bac1e2014-05-06 15:23:49 -04001305 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001306 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001307 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001308 }
John Bauman19bac1e2014-05-06 15:23:49 -04001309 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001310 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001311 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001312 }
John Bauman19bac1e2014-05-06 15:23:49 -04001313 else
1314 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001315 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001316 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001317 }
John Bauman89401822014-05-06 15:04:28 -04001318 }
1319
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001320 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001321 {
1322 ASSERT(ifDepth < 24 + 4);
1323
Nicolas Capens7551ac62016-01-20 17:11:53 -05001324 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001325
John Bauman19bac1e2014-05-06 15:23:49 -04001326 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001327 {
John Bauman19bac1e2014-05-06 15:23:49 -04001328 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001329 }
1330
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001331 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1332 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001333
1334 branch(condition, trueBlock, falseBlock);
1335
1336 isConditionalIf[ifDepth] = false;
1337 ifFalseBlock[ifDepth] = falseBlock;
1338
1339 ifDepth++;
1340 }
1341
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001342 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001343 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001344 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001345
John Bauman19bac1e2014-05-06 15:23:49 -04001346 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001347 {
1348 condition = ~condition;
1349 }
1350
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001351 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001352 }
1353
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001354 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001355 {
1356 Int4 condition;
1357
1358 switch(control)
1359 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001360 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1361 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1362 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1363 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1364 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1365 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001366 default:
1367 ASSERT(false);
1368 }
1369
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001370 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001371 }
1372
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001373 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001374 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001375 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001376
Nicolas Capens7551ac62016-01-20 17:11:53 -05001377 enableIndex++;
1378 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001379
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001380 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1381 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001382
John Bauman19bac1e2014-05-06 15:23:49 -04001383 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001384
1385 branch(notAllFalse, trueBlock, falseBlock);
1386
1387 isConditionalIf[ifDepth] = true;
1388 ifFalseBlock[ifDepth] = falseBlock;
1389
1390 ifDepth++;
John Bauman89401822014-05-06 15:04:28 -04001391 }
1392
1393 void VertexProgram::LABEL(int labelIndex)
1394 {
John Bauman19bac1e2014-05-06 15:23:49 -04001395 if(!labelBlock[labelIndex])
1396 {
1397 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1398 }
1399
John Bauman89401822014-05-06 15:04:28 -04001400 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001401 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001402 }
1403
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001404 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001405 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001406 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001407
Nicolas Capens7551ac62016-01-20 17:11:53 -05001408 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1409 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1410 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001411
1412 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001413 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001414 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001415 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001416 }
1417
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001418 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1419 BasicBlock *testBlock = Nucleus::createBasicBlock();
1420 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001421
1422 loopRepTestBlock[loopRepDepth] = testBlock;
1423 loopRepEndBlock[loopRepDepth] = endBlock;
1424
1425 // FIXME: jump(testBlock)
1426 Nucleus::createBr(testBlock);
1427 Nucleus::setInsertBlock(testBlock);
1428
Nicolas Capens7551ac62016-01-20 17:11:53 -05001429 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001430 Nucleus::setInsertBlock(loopBlock);
1431
Nicolas Capens7551ac62016-01-20 17:11:53 -05001432 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001433
John Bauman89401822014-05-06 15:04:28 -04001434 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001435 }
1436
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001437 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001438 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001439 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001440
Nicolas Capens7551ac62016-01-20 17:11:53 -05001441 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1442 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001443
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001444 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1445 BasicBlock *testBlock = Nucleus::createBasicBlock();
1446 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001447
1448 loopRepTestBlock[loopRepDepth] = testBlock;
1449 loopRepEndBlock[loopRepDepth] = endBlock;
1450
1451 // FIXME: jump(testBlock)
1452 Nucleus::createBr(testBlock);
1453 Nucleus::setInsertBlock(testBlock);
1454
Nicolas Capens7551ac62016-01-20 17:11:53 -05001455 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001456 Nucleus::setInsertBlock(loopBlock);
1457
Nicolas Capens7551ac62016-01-20 17:11:53 -05001458 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001459
1460 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001461 }
1462
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001463 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001464 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001465 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001466
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001467 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1468 BasicBlock *testBlock = Nucleus::createBasicBlock();
1469 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001470
John Bauman19bac1e2014-05-06 15:23:49 -04001471 loopRepTestBlock[loopRepDepth] = testBlock;
1472 loopRepEndBlock[loopRepDepth] = endBlock;
1473
Nicolas Capens7551ac62016-01-20 17:11:53 -05001474 Int4 restoreBreak = enableBreak;
1475 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001476
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001477 // TODO: jump(testBlock)
John Bauman19bac1e2014-05-06 15:23:49 -04001478 Nucleus::createBr(testBlock);
1479 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001480 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001481
Nicolas Capensc2534f42016-04-04 11:13:24 -04001482 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001483 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001484 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001485 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens6d123312018-01-08 12:57:52 -05001486 if(shader->containsBreakInstruction()) condition &= enableBreak;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001487 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001488
1489 Bool notAllFalse = SignMask(condition) != 0;
1490 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001491
John Bauman19bac1e2014-05-06 15:23:49 -04001492 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001493 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001494
John Bauman19bac1e2014-05-06 15:23:49 -04001495 Nucleus::setInsertBlock(loopBlock);
1496
1497 loopRepDepth++;
John Bauman19bac1e2014-05-06 15:23:49 -04001498 }
1499
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001500 void VertexProgram::SWITCH()
1501 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001502 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001503
1504 loopRepTestBlock[loopRepDepth] = nullptr;
1505 loopRepEndBlock[loopRepDepth] = endBlock;
1506
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001507 Int4 restoreBreak = enableBreak;
1508
1509 BasicBlock *currentBlock = Nucleus::getInsertBlock();
1510
1511 Nucleus::setInsertBlock(endBlock);
1512 enableBreak = restoreBreak;
1513
1514 Nucleus::setInsertBlock(currentBlock);
1515
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001516 loopRepDepth++;
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001517 }
1518
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001519 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001520 {
John Bauman19bac1e2014-05-06 15:23:49 -04001521 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001522 {
1523 returnBlock = Nucleus::createBasicBlock();
1524 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001525 }
1526 else
1527 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001528 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001529
John Bauman19bac1e2014-05-06 15:23:49 -04001530 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001531 {
John Bauman19bac1e2014-05-06 15:23:49 -04001532 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001533 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001534
Nicolas Capens19336542016-09-26 10:32:29 -04001535 Value *value = index.loadValue();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001536 SwitchCases *switchCases = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001537
1538 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1539 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001540 Nucleus::addSwitchCase(switchCases, i, callRetBlock[currentLabel][i]);
John Bauman19bac1e2014-05-06 15:23:49 -04001541 }
1542 }
1543 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1544 {
1545 Nucleus::createBr(callRetBlock[currentLabel][0]);
1546 }
1547 else // Function isn't called
1548 {
1549 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001550 }
1551
1552 Nucleus::setInsertBlock(unreachableBlock);
1553 Nucleus::createUnreachable();
1554 }
1555 }
1556
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001557 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001558 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001559 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001560
John Bauman19bac1e2014-05-06 15:23:49 -04001561 // FIXME: Return from function if all instances left
1562 // FIXME: Use enableLeave in other control-flow constructs
1563 }
John Bauman89401822014-05-06 15:04:28 -04001564
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001565 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001566 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001567 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001568 }
1569
Nicolas Capensa0b57832017-11-07 13:07:53 -05001570 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001571 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001572 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001573 }
1574
Nicolas Capensa0b57832017-11-07 13:07:53 -05001575 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001576 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001577 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001578 }
1579
Nicolas Capensa0b57832017-11-07 13:07:53 -05001580 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001581 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001582 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001583 }
1584
Nicolas Capensa0b57832017-11-07 13:07:53 -05001585 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001586 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001587 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001588 }
1589
Nicolas Capensa0b57832017-11-07 13:07:53 -05001590 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001591 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001592 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001593 }
1594
Nicolas Capensa0b57832017-11-07 13:07:53 -05001595 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001596 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001597 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1598 }
1599
1600 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1601 {
1602 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001603 }
1604
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001605 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001606 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05001607 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + src1.index * sizeof(Texture);
Nicolas Capens89a218b2017-11-07 13:05:20 -05001608 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001609 }
1610
Nicolas Capensa0b57832017-11-07 13:07:53 -05001611 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 -04001612 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001613 Vector4f tmp;
1614
John Bauman19bac1e2014-05-06 15:23:49 -04001615 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1616 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001617 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001618 }
1619 else
1620 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001621 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001622
Nicolas Capensc2534f42016-04-04 11:13:24 -04001623 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001624 {
1625 if(shader->usesSampler(i))
1626 {
1627 If(index == i)
1628 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001629 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001630 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1631 }
1632 }
1633 }
1634 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001635
Nicolas Capens89a218b2017-11-07 13:05:20 -05001636 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001637 c.x = tmp[(s.swizzle >> 0) & 0x3];
1638 c.y = tmp[(s.swizzle >> 2) & 0x3];
1639 c.z = tmp[(s.swizzle >> 4) & 0x3];
1640 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001641
1642 return c;
1643 }
1644
Nicolas Capensa0b57832017-11-07 13:07:53 -05001645 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001646 {
1647 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001648 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 -04001649 }
John Bauman89401822014-05-06 15:04:28 -04001650}