blob: 0eb84412c73ed72f4dd193b5445d7a0b0fd26ae5 [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 Capens8a587712018-10-20 14:17:49 -0400981 if(whileTest)
982 {
983 return Int4(0xFFFFFFFF);
984 }
985
Nicolas Capens7551ac62016-01-20 17:11:53 -0500986 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500987
Nicolas Capens8a587712018-10-20 14:17:49 -0400988 if(shader->containsBreakInstruction() && instruction->analysisBreak)
John Bauman19bac1e2014-05-06 15:23:49 -0400989 {
Nicolas Capens8a587712018-10-20 14:17:49 -0400990 enable &= enableBreak;
991 }
John Bauman19bac1e2014-05-06 15:23:49 -0400992
Nicolas Capens8a587712018-10-20 14:17:49 -0400993 if(shader->containsContinueInstruction() && instruction->analysisContinue)
994 {
995 enable &= enableContinue;
996 }
John Bauman19bac1e2014-05-06 15:23:49 -0400997
Nicolas Capens8a587712018-10-20 14:17:49 -0400998 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
999 {
1000 enable &= enableLeave;
John Bauman19bac1e2014-05-06 15:23:49 -04001001 }
1002
1003 return enable;
1004 }
1005
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001006 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001007 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001008 Vector4f row0 = fetchRegister(src1, 0);
1009 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -04001010
1011 dst.x = dot3(src0, row0);
1012 dst.y = dot3(src0, row1);
1013 }
1014
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001015 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001016 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001017 Vector4f row0 = fetchRegister(src1, 0);
1018 Vector4f row1 = fetchRegister(src1, 1);
1019 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001020
1021 dst.x = dot3(src0, row0);
1022 dst.y = dot3(src0, row1);
1023 dst.z = dot3(src0, row2);
1024 }
1025
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001026 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001027 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001028 Vector4f row0 = fetchRegister(src1, 0);
1029 Vector4f row1 = fetchRegister(src1, 1);
1030 Vector4f row2 = fetchRegister(src1, 2);
1031 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001032
1033 dst.x = dot3(src0, row0);
1034 dst.y = dot3(src0, row1);
1035 dst.z = dot3(src0, row2);
1036 dst.w = dot3(src0, row3);
1037 }
1038
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001039 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001040 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001041 Vector4f row0 = fetchRegister(src1, 0);
1042 Vector4f row1 = fetchRegister(src1, 1);
1043 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -04001044
1045 dst.x = dot4(src0, row0);
1046 dst.y = dot4(src0, row1);
1047 dst.z = dot4(src0, row2);
1048 }
1049
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001050 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -04001051 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001052 Vector4f row0 = fetchRegister(src1, 0);
1053 Vector4f row1 = fetchRegister(src1, 1);
1054 Vector4f row2 = fetchRegister(src1, 2);
1055 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -04001056
1057 dst.x = dot4(src0, row0);
1058 dst.y = dot4(src0, row1);
1059 dst.z = dot4(src0, row2);
1060 dst.w = dot4(src0, row3);
1061 }
1062
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001063 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001064 {
Nicolas Capens6d123312018-01-08 12:57:52 -05001065 enableBreak = enableBreak & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001066 }
1067
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001068 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001069 {
1070 Int4 condition;
1071
1072 switch(control)
1073 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001074 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1075 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1076 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1077 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1078 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1079 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001080 default:
1081 ASSERT(false);
1082 }
1083
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001084 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001085 }
1086
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001087 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001088 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001089 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001090
John Bauman19bac1e2014-05-06 15:23:49 -04001091 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001092 {
1093 condition = ~condition;
1094 }
1095
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001096 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001097 }
1098
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001099 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001100 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001101 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001102
Nicolas Capens7551ac62016-01-20 17:11:53 -05001103 enableBreak = enableBreak & ~condition;
John Bauman89401822014-05-06 15:04:28 -04001104 }
1105
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001106 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001107 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001108 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001109 }
1110
1111 void VertexProgram::TEST()
1112 {
1113 whileTest = true;
1114 }
1115
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001116 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001117 {
1118 if(!labelBlock[labelIndex])
1119 {
1120 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1121 }
1122
John Bauman19bac1e2014-05-06 15:23:49 -04001123 if(callRetBlock[labelIndex].size() > 1)
1124 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001125 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001126 }
John Bauman89401822014-05-06 15:04:28 -04001127
Nicolas Capens7551ac62016-01-20 17:11:53 -05001128 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001129
1130 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001131 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1132
Nicolas Capens7551ac62016-01-20 17:11:53 -05001133 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001134 }
1135
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001136 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001137 {
John Bauman19bac1e2014-05-06 15:23:49 -04001138 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001139 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001140 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001141 }
John Bauman19bac1e2014-05-06 15:23:49 -04001142 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001143 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001144 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001145 }
1146 else ASSERT(false);
1147 }
1148
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001149 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001150 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001151 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001152
John Bauman19bac1e2014-05-06 15:23:49 -04001153 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001154 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001155 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001156 }
1157
1158 if(!labelBlock[labelIndex])
1159 {
1160 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1161 }
1162
John Bauman19bac1e2014-05-06 15:23:49 -04001163 if(callRetBlock[labelIndex].size() > 1)
1164 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001165 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001166 }
John Bauman89401822014-05-06 15:04:28 -04001167
Nicolas Capens7551ac62016-01-20 17:11:53 -05001168 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001169
John Bauman19bac1e2014-05-06 15:23:49 -04001170 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1171 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1172
Nicolas Capens7551ac62016-01-20 17:11:53 -05001173 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001174 }
1175
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001176 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001177 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001178 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001179
John Bauman19bac1e2014-05-06 15:23:49 -04001180 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001181 {
1182 condition = ~condition;
1183 }
1184
Nicolas Capens7551ac62016-01-20 17:11:53 -05001185 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001186
1187 if(!labelBlock[labelIndex])
1188 {
1189 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1190 }
1191
John Bauman19bac1e2014-05-06 15:23:49 -04001192 if(callRetBlock[labelIndex].size() > 1)
1193 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001194 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001195 }
John Bauman89401822014-05-06 15:04:28 -04001196
Nicolas Capens7551ac62016-01-20 17:11:53 -05001197 enableIndex++;
1198 enableStack[enableIndex] = condition;
1199 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001200
John Bauman19bac1e2014-05-06 15:23:49 -04001201 Bool notAllFalse = SignMask(condition) != 0;
1202 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1203 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001204
Nicolas Capens7551ac62016-01-20 17:11:53 -05001205 enableIndex--;
1206 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001207 }
1208
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001209 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001210 {
1211 ifDepth--;
1212
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001213 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1214 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001215
1216 if(isConditionalIf[ifDepth])
1217 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001218 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001219 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001220
1221 branch(notAllFalse, falseBlock, endBlock);
1222
Nicolas Capens7551ac62016-01-20 17:11:53 -05001223 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001224 }
1225 else
1226 {
1227 Nucleus::createBr(endBlock);
1228 Nucleus::setInsertBlock(falseBlock);
1229 }
1230
1231 ifFalseBlock[ifDepth] = endBlock;
1232
1233 ifDepth++;
1234 }
1235
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001236 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001237 {
1238 ifDepth--;
1239
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001240 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001241
1242 Nucleus::createBr(endBlock);
1243 Nucleus::setInsertBlock(endBlock);
1244
1245 if(isConditionalIf[ifDepth])
1246 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001247 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001248 }
1249 }
1250
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001251 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001252 {
1253 loopRepDepth--;
1254
Nicolas Capens7551ac62016-01-20 17:11:53 -05001255 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001256
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001257 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1258 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001259
1260 Nucleus::createBr(testBlock);
1261 Nucleus::setInsertBlock(endBlock);
1262
Nicolas Capens7551ac62016-01-20 17:11:53 -05001263 loopDepth--;
1264 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001265 }
1266
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001267 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001268 {
1269 loopRepDepth--;
1270
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001271 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1272 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001273
1274 Nucleus::createBr(testBlock);
1275 Nucleus::setInsertBlock(endBlock);
1276
Nicolas Capens7551ac62016-01-20 17:11:53 -05001277 loopDepth--;
1278 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001279 }
1280
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001281 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001282 {
1283 loopRepDepth--;
1284
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001285 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1286 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001287
1288 Nucleus::createBr(testBlock);
1289 Nucleus::setInsertBlock(endBlock);
1290
Nicolas Capens7551ac62016-01-20 17:11:53 -05001291 enableIndex--;
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
Nicolas Capensac6d5052018-01-05 15:34:00 -05001301 Nucleus::createBr(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001302 Nucleus::setInsertBlock(endBlock);
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001303 }
1304
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001305 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001306 {
John Bauman19bac1e2014-05-06 15:23:49 -04001307 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001308 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001309 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001310 }
John Bauman19bac1e2014-05-06 15:23:49 -04001311 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001312 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001313 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001314 }
John Bauman19bac1e2014-05-06 15:23:49 -04001315 else
1316 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001317 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001318 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001319 }
John Bauman89401822014-05-06 15:04:28 -04001320 }
1321
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001322 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001323 {
1324 ASSERT(ifDepth < 24 + 4);
1325
Nicolas Capens7551ac62016-01-20 17:11:53 -05001326 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001327
John Bauman19bac1e2014-05-06 15:23:49 -04001328 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001329 {
John Bauman19bac1e2014-05-06 15:23:49 -04001330 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001331 }
1332
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001333 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1334 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001335
1336 branch(condition, trueBlock, falseBlock);
1337
1338 isConditionalIf[ifDepth] = false;
1339 ifFalseBlock[ifDepth] = falseBlock;
1340
1341 ifDepth++;
1342 }
1343
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001344 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001345 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001346 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001347
John Bauman19bac1e2014-05-06 15:23:49 -04001348 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001349 {
1350 condition = ~condition;
1351 }
1352
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001353 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001354 }
1355
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001356 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001357 {
1358 Int4 condition;
1359
1360 switch(control)
1361 {
Nicolas Capensac6d5052018-01-05 15:34:00 -05001362 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1363 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1364 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1365 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1366 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1367 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001368 default:
1369 ASSERT(false);
1370 }
1371
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001372 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001373 }
1374
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001375 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001376 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001377 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001378
Nicolas Capens7551ac62016-01-20 17:11:53 -05001379 enableIndex++;
1380 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001381
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001382 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1383 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001384
John Bauman19bac1e2014-05-06 15:23:49 -04001385 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001386
1387 branch(notAllFalse, trueBlock, falseBlock);
1388
1389 isConditionalIf[ifDepth] = true;
1390 ifFalseBlock[ifDepth] = falseBlock;
1391
1392 ifDepth++;
John Bauman89401822014-05-06 15:04:28 -04001393 }
1394
1395 void VertexProgram::LABEL(int labelIndex)
1396 {
John Bauman19bac1e2014-05-06 15:23:49 -04001397 if(!labelBlock[labelIndex])
1398 {
1399 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1400 }
1401
John Bauman89401822014-05-06 15:04:28 -04001402 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001403 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001404 }
1405
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001406 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001407 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001408 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001409
Nicolas Capens7551ac62016-01-20 17:11:53 -05001410 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1411 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1412 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001413
1414 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001415 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001416 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001417 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001418 }
1419
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001420 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1421 BasicBlock *testBlock = Nucleus::createBasicBlock();
1422 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001423
1424 loopRepTestBlock[loopRepDepth] = testBlock;
1425 loopRepEndBlock[loopRepDepth] = endBlock;
1426
1427 // FIXME: jump(testBlock)
1428 Nucleus::createBr(testBlock);
1429 Nucleus::setInsertBlock(testBlock);
1430
Nicolas Capens7551ac62016-01-20 17:11:53 -05001431 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001432 Nucleus::setInsertBlock(loopBlock);
1433
Nicolas Capens7551ac62016-01-20 17:11:53 -05001434 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001435
John Bauman89401822014-05-06 15:04:28 -04001436 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001437 }
1438
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001439 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001440 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001441 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001442
Nicolas Capens7551ac62016-01-20 17:11:53 -05001443 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1444 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001445
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001446 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1447 BasicBlock *testBlock = Nucleus::createBasicBlock();
1448 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001449
1450 loopRepTestBlock[loopRepDepth] = testBlock;
1451 loopRepEndBlock[loopRepDepth] = endBlock;
1452
1453 // FIXME: jump(testBlock)
1454 Nucleus::createBr(testBlock);
1455 Nucleus::setInsertBlock(testBlock);
1456
Nicolas Capens7551ac62016-01-20 17:11:53 -05001457 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001458 Nucleus::setInsertBlock(loopBlock);
1459
Nicolas Capens7551ac62016-01-20 17:11:53 -05001460 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001461
1462 loopRepDepth++;
John Bauman89401822014-05-06 15:04:28 -04001463 }
1464
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001465 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001466 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001467 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001468
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001469 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1470 BasicBlock *testBlock = Nucleus::createBasicBlock();
1471 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001472
John Bauman19bac1e2014-05-06 15:23:49 -04001473 loopRepTestBlock[loopRepDepth] = testBlock;
1474 loopRepEndBlock[loopRepDepth] = endBlock;
1475
Nicolas Capens7551ac62016-01-20 17:11:53 -05001476 Int4 restoreBreak = enableBreak;
1477 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001478
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001479 // TODO: jump(testBlock)
John Bauman19bac1e2014-05-06 15:23:49 -04001480 Nucleus::createBr(testBlock);
1481 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001482 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001483
Nicolas Capensc2534f42016-04-04 11:13:24 -04001484 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001485 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001486 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001487 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens6d123312018-01-08 12:57:52 -05001488 if(shader->containsBreakInstruction()) condition &= enableBreak;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001489 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001490
1491 Bool notAllFalse = SignMask(condition) != 0;
1492 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001493
John Bauman19bac1e2014-05-06 15:23:49 -04001494 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001495 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001496
John Bauman19bac1e2014-05-06 15:23:49 -04001497 Nucleus::setInsertBlock(loopBlock);
1498
1499 loopRepDepth++;
Nicolas Capens8a587712018-10-20 14:17:49 -04001500 whileTest = false;
John Bauman19bac1e2014-05-06 15:23:49 -04001501 }
1502
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001503 void VertexProgram::SWITCH()
1504 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001505 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001506
1507 loopRepTestBlock[loopRepDepth] = nullptr;
1508 loopRepEndBlock[loopRepDepth] = endBlock;
1509
Nicolas Capensd6bcc112018-01-08 14:09:51 -05001510 Int4 restoreBreak = enableBreak;
1511
1512 BasicBlock *currentBlock = Nucleus::getInsertBlock();
1513
1514 Nucleus::setInsertBlock(endBlock);
1515 enableBreak = restoreBreak;
1516
1517 Nucleus::setInsertBlock(currentBlock);
1518
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001519 loopRepDepth++;
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001520 }
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::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001569 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001570 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), (src0), Base);
John Bauman19bac1e2014-05-06 15:23:49 -04001571 }
1572
Nicolas Capensa0b57832017-11-07 13:07:53 -05001573 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001574 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001575 dst = sampleTexture(src1, src0, (src0.x), (src0), (src0), offset, {Base, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001576 }
1577
Nicolas Capensa0b57832017-11-07 13:07:53 -05001578 void VertexProgram::TEXLOD(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001579 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001580 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Lod);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001581 }
1582
Nicolas Capensa0b57832017-11-07 13:07:53 -05001583 void VertexProgram::TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001584 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001585 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001586 }
1587
Nicolas Capensa0b57832017-11-07 13:07:53 -05001588 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001589 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001590 dst = sampleTexture(src1, src0, lod, (src0), (src0), (src0), Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001591 }
1592
Nicolas Capensa0b57832017-11-07 13:07:53 -05001593 void VertexProgram::TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset, Float4 &lod)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001594 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001595 dst = sampleTexture(src1, src0, lod, (src0), (src0), offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001596 }
1597
Nicolas Capensa0b57832017-11-07 13:07:53 -05001598 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001599 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001600 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, src0, Grad);
1601 }
1602
1603 void VertexProgram::TEXGRADOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &dsx, Vector4f &dsy, Vector4f &offset)
1604 {
1605 dst = sampleTexture(src1, src0, (src0.x), dsx, dsy, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001606 }
1607
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001608 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001609 {
Alexis Hetuda163ed2018-01-03 16:36:14 -05001610 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + src1.index * sizeof(Texture);
Nicolas Capens89a218b2017-11-07 13:05:20 -05001611 dst = SamplerCore::textureSize(texture, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001612 }
1613
Nicolas Capensa0b57832017-11-07 13:07:53 -05001614 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 -04001615 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001616 Vector4f tmp;
1617
John Bauman19bac1e2014-05-06 15:23:49 -04001618 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1619 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001620 tmp = sampleTexture(s.index, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001621 }
1622 else
1623 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001624 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001625
Nicolas Capensc2534f42016-04-04 11:13:24 -04001626 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001627 {
1628 if(shader->usesSampler(i))
1629 {
1630 If(index == i)
1631 {
Nicolas Capensa0b57832017-11-07 13:07:53 -05001632 tmp = sampleTexture(i, uvwq, lod, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001633 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1634 }
1635 }
1636 }
1637 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001638
Nicolas Capens89a218b2017-11-07 13:05:20 -05001639 Vector4f c;
Nicolas Capensc2534f42016-04-04 11:13:24 -04001640 c.x = tmp[(s.swizzle >> 0) & 0x3];
1641 c.y = tmp[(s.swizzle >> 2) & 0x3];
1642 c.z = tmp[(s.swizzle >> 4) & 0x3];
1643 c.w = tmp[(s.swizzle >> 6) & 0x3];
Nicolas Capens89a218b2017-11-07 13:05:20 -05001644
1645 return c;
1646 }
1647
Nicolas Capensa0b57832017-11-07 13:07:53 -05001648 Vector4f VertexProgram::sampleTexture(int sampler, Vector4f &uvwq, Float4 &lod, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
Nicolas Capens89a218b2017-11-07 13:05:20 -05001649 {
1650 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + sampler * sizeof(Texture);
Nicolas Capensa0b57832017-11-07 13:07:53 -05001651 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 -04001652 }
John Bauman89401822014-05-06 15:04:28 -04001653}