blob: 5c3f8b47d3729a93d9225c30f0aae0f6aa7147ed [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
17#include "Renderer.hpp"
18#include "VertexShader.hpp"
19#include "Vertex.hpp"
20#include "Half.hpp"
21#include "SamplerCore.hpp"
22#include "Debug.hpp"
23
John Bauman89401822014-05-06 15:04:28 -040024namespace sw
25{
Nicolas Capens7551ac62016-01-20 17:11:53 -050026 VertexProgram::VertexProgram(const VertexProcessor::State &state, const VertexShader *shader)
27 : VertexRoutine(state, shader), shader(shader), r(shader->dynamicallyIndexedTemporaries)
John Bauman89401822014-05-06 15:04:28 -040028 {
John Bauman89401822014-05-06 15:04:28 -040029 ifDepth = 0;
30 loopRepDepth = 0;
31 breakDepth = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040032 currentLabel = -1;
33 whileTest = false;
John Bauman89401822014-05-06 15:04:28 -040034
35 for(int i = 0; i < 2048; i++)
36 {
37 labelBlock[i] = 0;
38 }
Nicolas Capens7551ac62016-01-20 17:11:53 -050039
40 loopDepth = -1;
41 enableStack[0] = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
42
43 if(shader && shader->containsBreakInstruction())
44 {
45 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
46 }
47
48 if(shader && shader->containsContinueInstruction())
49 {
50 enableContinue = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
51 }
52
Alexis Hetu02ad0aa2016-08-02 11:18:14 -040053 if(shader->isInstanceIdDeclared())
Nicolas Capens7551ac62016-01-20 17:11:53 -050054 {
55 instanceID = *Pointer<Int>(data + OFFSET(DrawData,instanceID));
56 }
John Bauman89401822014-05-06 15:04:28 -040057 }
58
59 VertexProgram::~VertexProgram()
60 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040061 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040062 {
63 delete sampler[i];
64 }
65 }
66
Nicolas Capensb4fb3672016-01-15 17:02:41 -050067 void VertexProgram::pipeline()
John Bauman89401822014-05-06 15:04:28 -040068 {
Alexis Hetu0b65c5e2015-03-31 11:48:57 -040069 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman89401822014-05-06 15:04:28 -040070 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050071 sampler[i] = new SamplerCore(constants, state.samplerState[i]);
John Bauman89401822014-05-06 15:04:28 -040072 }
73
74 if(!state.preTransformed)
75 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050076 program();
John Bauman89401822014-05-06 15:04:28 -040077 }
78 else
79 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -050080 passThrough();
John Bauman89401822014-05-06 15:04:28 -040081 }
82 }
83
Nicolas Capensb4fb3672016-01-15 17:02:41 -050084 void VertexProgram::program()
John Bauman89401822014-05-06 15:04:28 -040085 {
John Bauman19bac1e2014-05-06 15:23:49 -040086 // shader->print("VertexShader-%0.8X.txt", state.shaderID);
John Bauman89401822014-05-06 15:04:28 -040087
John Bauman19bac1e2014-05-06 15:23:49 -040088 unsigned short version = shader->getVersion();
John Bauman89401822014-05-06 15:04:28 -040089
Nicolas Capens7551ac62016-01-20 17:11:53 -050090 enableIndex = 0;
91 stackIndex = 0;
John Bauman19bac1e2014-05-06 15:23:49 -040092
Nicolas Capens4677a5f2014-05-06 16:42:26 -040093 if(shader->containsLeaveInstruction())
94 {
Nicolas Capens7551ac62016-01-20 17:11:53 -050095 enableLeave = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
Nicolas Capens4677a5f2014-05-06 16:42:26 -040096 }
97
John Bauman19bac1e2014-05-06 15:23:49 -040098 // Create all call site return blocks up front
Alexis Hetu903e0252014-11-25 14:25:32 -050099 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman89401822014-05-06 15:04:28 -0400100 {
John Bauman19bac1e2014-05-06 15:23:49 -0400101 const Shader::Instruction *instruction = shader->getInstruction(i);
102 Shader::Opcode opcode = instruction->opcode;
John Bauman89401822014-05-06 15:04:28 -0400103
John Bauman19bac1e2014-05-06 15:23:49 -0400104 if(opcode == Shader::OPCODE_CALL || opcode == Shader::OPCODE_CALLNZ)
105 {
106 const Dst &dst = instruction->dst;
John Bauman89401822014-05-06 15:04:28 -0400107
John Bauman19bac1e2014-05-06 15:23:49 -0400108 ASSERT(callRetBlock[dst.label].size() == dst.callSite);
109 callRetBlock[dst.label].push_back(Nucleus::createBasicBlock());
110 }
111 }
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500112
Alexis Hetu903e0252014-11-25 14:25:32 -0500113 for(size_t i = 0; i < shader->getLength(); i++)
John Bauman19bac1e2014-05-06 15:23:49 -0400114 {
115 const Shader::Instruction *instruction = shader->getInstruction(i);
116 Shader::Opcode opcode = instruction->opcode;
117
118 if(opcode == Shader::OPCODE_DCL || opcode == Shader::OPCODE_DEF || opcode == Shader::OPCODE_DEFI || opcode == Shader::OPCODE_DEFB)
John Bauman89401822014-05-06 15:04:28 -0400119 {
120 continue;
121 }
122
John Bauman19bac1e2014-05-06 15:23:49 -0400123 Dst dst = instruction->dst;
124 Src src0 = instruction->src[0];
125 Src src1 = instruction->src[1];
126 Src src2 = instruction->src[2];
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400127 Src src3 = instruction->src[3];
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400128 Src src4 = instruction->src[4];
John Bauman89401822014-05-06 15:04:28 -0400129
John Bauman19bac1e2014-05-06 15:23:49 -0400130 bool predicate = instruction->predicate;
John Bauman19bac1e2014-05-06 15:23:49 -0400131 Control control = instruction->control;
132 bool integer = dst.type == Shader::PARAMETER_ADDR;
133 bool pp = dst.partialPrecision;
John Bauman89401822014-05-06 15:04:28 -0400134
John Bauman19bac1e2014-05-06 15:23:49 -0400135 Vector4f d;
136 Vector4f s0;
137 Vector4f s1;
138 Vector4f s2;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400139 Vector4f s3;
Alexis Hetu25d47fc2015-10-22 13:58:32 -0400140 Vector4f s4;
John Bauman89401822014-05-06 15:04:28 -0400141
Nicolas Capensc2534f42016-04-04 11:13:24 -0400142 if(src0.type != Shader::PARAMETER_VOID) s0 = fetchRegister(src0);
143 if(src1.type != Shader::PARAMETER_VOID) s1 = fetchRegister(src1);
144 if(src2.type != Shader::PARAMETER_VOID) s2 = fetchRegister(src2);
145 if(src3.type != Shader::PARAMETER_VOID) s3 = fetchRegister(src3);
146 if(src4.type != Shader::PARAMETER_VOID) s4 = fetchRegister(src4);
John Bauman89401822014-05-06 15:04:28 -0400147
148 switch(opcode)
149 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500150 case Shader::OPCODE_VS_1_0: break;
151 case Shader::OPCODE_VS_1_1: break;
152 case Shader::OPCODE_VS_2_0: break;
153 case Shader::OPCODE_VS_2_x: break;
154 case Shader::OPCODE_VS_2_sw: break;
155 case Shader::OPCODE_VS_3_0: break;
156 case Shader::OPCODE_VS_3_sw: break;
157 case Shader::OPCODE_DCL: break;
158 case Shader::OPCODE_DEF: break;
159 case Shader::OPCODE_DEFI: break;
160 case Shader::OPCODE_DEFB: break;
161 case Shader::OPCODE_NOP: break;
162 case Shader::OPCODE_ABS: abs(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400163 case Shader::OPCODE_IABS: iabs(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500164 case Shader::OPCODE_ADD: add(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400165 case Shader::OPCODE_IADD: iadd(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500166 case Shader::OPCODE_CRS: crs(d, s0, s1); break;
167 case Shader::OPCODE_FORWARD1: forward1(d, s0, s1, s2); break;
168 case Shader::OPCODE_FORWARD2: forward2(d, s0, s1, s2); break;
169 case Shader::OPCODE_FORWARD3: forward3(d, s0, s1, s2); break;
170 case Shader::OPCODE_FORWARD4: forward4(d, s0, s1, s2); break;
171 case Shader::OPCODE_REFLECT1: reflect1(d, s0, s1); break;
172 case Shader::OPCODE_REFLECT2: reflect2(d, s0, s1); break;
173 case Shader::OPCODE_REFLECT3: reflect3(d, s0, s1); break;
174 case Shader::OPCODE_REFLECT4: reflect4(d, s0, s1); break;
175 case Shader::OPCODE_REFRACT1: refract1(d, s0, s1, s2.x); break;
176 case Shader::OPCODE_REFRACT2: refract2(d, s0, s1, s2.x); break;
177 case Shader::OPCODE_REFRACT3: refract3(d, s0, s1, s2.x); break;
178 case Shader::OPCODE_REFRACT4: refract4(d, s0, s1, s2.x); break;
179 case Shader::OPCODE_DP1: dp1(d, s0, s1); break;
180 case Shader::OPCODE_DP2: dp2(d, s0, s1); break;
181 case Shader::OPCODE_DP3: dp3(d, s0, s1); break;
182 case Shader::OPCODE_DP4: dp4(d, s0, s1); break;
Alexis Hetuc3d95f32015-09-23 12:27:32 -0400183 case Shader::OPCODE_DET2: det2(d, s0, s1); break;
184 case Shader::OPCODE_DET3: det3(d, s0, s1, s2); break;
185 case Shader::OPCODE_DET4: det4(d, s0, s1, s2, s3); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500186 case Shader::OPCODE_ATT: att(d, s0, s1); break;
187 case Shader::OPCODE_EXP2X: exp2x(d, s0, pp); break;
188 case Shader::OPCODE_EXP2: exp2(d, s0, pp); break;
189 case Shader::OPCODE_EXPP: expp(d, s0, version); break;
190 case Shader::OPCODE_EXP: exp(d, s0, pp); break;
191 case Shader::OPCODE_FRC: frc(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400192 case Shader::OPCODE_TRUNC: trunc(d, s0); break;
193 case Shader::OPCODE_FLOOR: floor(d, s0); break;
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400194 case Shader::OPCODE_ROUND: round(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500195 case Shader::OPCODE_ROUNDEVEN: roundEven(d, s0); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400196 case Shader::OPCODE_CEIL: ceil(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500197 case Shader::OPCODE_LIT: lit(d, s0); break;
198 case Shader::OPCODE_LOG2X: log2x(d, s0, pp); break;
199 case Shader::OPCODE_LOG2: log2(d, s0, pp); break;
200 case Shader::OPCODE_LOGP: logp(d, s0, version); break;
201 case Shader::OPCODE_LOG: log(d, s0, pp); break;
202 case Shader::OPCODE_LRP: lrp(d, s0, s1, s2); break;
203 case Shader::OPCODE_STEP: step(d, s0, s1); break;
204 case Shader::OPCODE_SMOOTH: smooth(d, s0, s1, s2); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400205 case Shader::OPCODE_FLOATBITSTOINT:
206 case Shader::OPCODE_FLOATBITSTOUINT:
207 case Shader::OPCODE_INTBITSTOFLOAT:
208 case Shader::OPCODE_UINTBITSTOFLOAT: d = s0; break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400209 case Shader::OPCODE_PACKSNORM2x16: packSnorm2x16(d, s0); break;
210 case Shader::OPCODE_PACKUNORM2x16: packUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400211 case Shader::OPCODE_PACKHALF2x16: packHalf2x16(d, s0); break;
Alexis Hetu9cde9742016-04-06 13:03:38 -0400212 case Shader::OPCODE_UNPACKSNORM2x16: unpackSnorm2x16(d, s0); break;
213 case Shader::OPCODE_UNPACKUNORM2x16: unpackUnorm2x16(d, s0); break;
Alexis Hetuffb35eb2016-04-06 18:05:00 -0400214 case Shader::OPCODE_UNPACKHALF2x16: unpackHalf2x16(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500215 case Shader::OPCODE_M3X2: M3X2(d, s0, src1); break;
216 case Shader::OPCODE_M3X3: M3X3(d, s0, src1); break;
217 case Shader::OPCODE_M3X4: M3X4(d, s0, src1); break;
218 case Shader::OPCODE_M4X3: M4X3(d, s0, src1); break;
219 case Shader::OPCODE_M4X4: M4X4(d, s0, src1); break;
220 case Shader::OPCODE_MAD: mad(d, s0, s1, s2); break;
221 case Shader::OPCODE_IMAD: imad(d, s0, s1, s2); break;
222 case Shader::OPCODE_MAX: max(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400223 case Shader::OPCODE_IMAX: imax(d, s0, s1); break;
224 case Shader::OPCODE_UMAX: umax(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500225 case Shader::OPCODE_MIN: min(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400226 case Shader::OPCODE_IMIN: imin(d, s0, s1); break;
227 case Shader::OPCODE_UMIN: umin(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500228 case Shader::OPCODE_MOV: mov(d, s0, integer); break;
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400229 case Shader::OPCODE_MOVA: mov(d, s0, true); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400230 case Shader::OPCODE_NEG: neg(d, s0); break;
231 case Shader::OPCODE_INEG: ineg(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500232 case Shader::OPCODE_F2B: f2b(d, s0); break;
233 case Shader::OPCODE_B2F: b2f(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400234 case Shader::OPCODE_F2I: f2i(d, s0); break;
235 case Shader::OPCODE_I2F: i2f(d, s0); break;
236 case Shader::OPCODE_F2U: f2u(d, s0); break;
237 case Shader::OPCODE_U2F: u2f(d, s0); break;
238 case Shader::OPCODE_I2B: i2b(d, s0); break;
239 case Shader::OPCODE_B2I: b2i(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500240 case Shader::OPCODE_MUL: mul(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400241 case Shader::OPCODE_IMUL: imul(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500242 case Shader::OPCODE_NRM2: nrm2(d, s0, pp); break;
243 case Shader::OPCODE_NRM3: nrm3(d, s0, pp); break;
244 case Shader::OPCODE_NRM4: nrm4(d, s0, pp); break;
245 case Shader::OPCODE_POWX: powx(d, s0, s1, pp); break;
246 case Shader::OPCODE_POW: pow(d, s0, s1, pp); break;
247 case Shader::OPCODE_RCPX: rcpx(d, s0, pp); break;
248 case Shader::OPCODE_DIV: div(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400249 case Shader::OPCODE_IDIV: idiv(d, s0, s1); break;
250 case Shader::OPCODE_UDIV: udiv(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500251 case Shader::OPCODE_MOD: mod(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400252 case Shader::OPCODE_IMOD: imod(d, s0, s1); break;
253 case Shader::OPCODE_UMOD: umod(d, s0, s1); break;
254 case Shader::OPCODE_SHL: shl(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500255 case Shader::OPCODE_ISHR: ishr(d, s0, s1); break;
256 case Shader::OPCODE_USHR: ushr(d, s0, s1); break;
257 case Shader::OPCODE_RSQX: rsqx(d, s0, pp); break;
258 case Shader::OPCODE_SQRT: sqrt(d, s0, pp); break;
259 case Shader::OPCODE_RSQ: rsq(d, s0, pp); break;
260 case Shader::OPCODE_LEN2: len2(d.x, s0, pp); break;
261 case Shader::OPCODE_LEN3: len3(d.x, s0, pp); break;
262 case Shader::OPCODE_LEN4: len4(d.x, s0, pp); break;
263 case Shader::OPCODE_DIST1: dist1(d.x, s0, s1, pp); break;
264 case Shader::OPCODE_DIST2: dist2(d.x, s0, s1, pp); break;
265 case Shader::OPCODE_DIST3: dist3(d.x, s0, s1, pp); break;
266 case Shader::OPCODE_DIST4: dist4(d.x, s0, s1, pp); break;
267 case Shader::OPCODE_SGE: step(d, s1, s0); break;
268 case Shader::OPCODE_SGN: sgn(d, s0); break;
Alexis Hetu0f448072016-03-18 10:56:08 -0400269 case Shader::OPCODE_ISGN: isgn(d, s0); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500270 case Shader::OPCODE_SINCOS: sincos(d, s0, pp); break;
271 case Shader::OPCODE_COS: cos(d, s0, pp); break;
272 case Shader::OPCODE_SIN: sin(d, s0, pp); break;
273 case Shader::OPCODE_TAN: tan(d, s0); break;
274 case Shader::OPCODE_ACOS: acos(d, s0); break;
275 case Shader::OPCODE_ASIN: asin(d, s0); break;
276 case Shader::OPCODE_ATAN: atan(d, s0); break;
277 case Shader::OPCODE_ATAN2: atan2(d, s0, s1); break;
278 case Shader::OPCODE_COSH: cosh(d, s0, pp); break;
279 case Shader::OPCODE_SINH: sinh(d, s0, pp); break;
280 case Shader::OPCODE_TANH: tanh(d, s0, pp); break;
281 case Shader::OPCODE_ACOSH: acosh(d, s0, pp); break;
282 case Shader::OPCODE_ASINH: asinh(d, s0, pp); break;
283 case Shader::OPCODE_ATANH: atanh(d, s0, pp); break;
284 case Shader::OPCODE_SLT: slt(d, s0, s1); break;
285 case Shader::OPCODE_SUB: sub(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400286 case Shader::OPCODE_ISUB: isub(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500287 case Shader::OPCODE_BREAK: BREAK(); break;
288 case Shader::OPCODE_BREAKC: BREAKC(s0, s1, control); break;
289 case Shader::OPCODE_BREAKP: BREAKP(src0); break;
290 case Shader::OPCODE_CONTINUE: CONTINUE(); break;
291 case Shader::OPCODE_TEST: TEST(); break;
292 case Shader::OPCODE_CALL: CALL(dst.label, dst.callSite); break;
293 case Shader::OPCODE_CALLNZ: CALLNZ(dst.label, dst.callSite, src0); break;
294 case Shader::OPCODE_ELSE: ELSE(); break;
295 case Shader::OPCODE_ENDIF: ENDIF(); break;
296 case Shader::OPCODE_ENDLOOP: ENDLOOP(); break;
297 case Shader::OPCODE_ENDREP: ENDREP(); break;
298 case Shader::OPCODE_ENDWHILE: ENDWHILE(); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400299 case Shader::OPCODE_ENDSWITCH: ENDSWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500300 case Shader::OPCODE_IF: IF(src0); break;
301 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
302 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
303 case Shader::OPCODE_LOOP: LOOP(src1); break;
304 case Shader::OPCODE_REP: REP(src0); break;
305 case Shader::OPCODE_WHILE: WHILE(src0); break;
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400306 case Shader::OPCODE_SWITCH: SWITCH(); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500307 case Shader::OPCODE_RET: RET(); break;
308 case Shader::OPCODE_LEAVE: LEAVE(); break;
309 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
310 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400311 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500312 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
313 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
314 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
315 case Shader::OPCODE_ALL: all(d.x, s0); break;
316 case Shader::OPCODE_ANY: any(d.x, s0); break;
Alexis Hetu24f454e2016-08-31 17:22:13 -0400317 case Shader::OPCODE_NOT: bitwise_not(d, s0); break;
318 case Shader::OPCODE_OR: bitwise_or(d, s0, s1); break;
319 case Shader::OPCODE_XOR: bitwise_xor(d, s0, s1); break;
320 case Shader::OPCODE_AND: bitwise_and(d, s0, s1); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400321 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
322 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500323 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
324 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
Meng-Lin Wu2337a192016-06-01 13:54:07 -0400325 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500326 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
Meng-Lin Wu9d62c482016-06-14 11:11:25 -0400327 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1); break;
328 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500329 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
330 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
331 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
332 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400333 default:
334 ASSERT(false);
335 }
336
John Bauman19bac1e2014-05-06 15:23:49 -0400337 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400338 {
John Bauman19bac1e2014-05-06 15:23:49 -0400339 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400340 {
John Bauman19bac1e2014-05-06 15:23:49 -0400341 switch(opcode)
342 {
343 case Shader::OPCODE_DIV:
344 if(dst.x) d.x = Trunc(d.x);
345 if(dst.y) d.y = Trunc(d.y);
346 if(dst.z) d.z = Trunc(d.z);
347 if(dst.w) d.w = Trunc(d.w);
348 break;
349 default:
350 break; // No truncation to integer required when arguments are integer
351 }
John Bauman89401822014-05-06 15:04:28 -0400352 }
353
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400355 {
John Bauman19bac1e2014-05-06 15:23:49 -0400356 if(dst.x) d.x = Max(d.x, Float4(0.0f));
357 if(dst.y) d.y = Max(d.y, Float4(0.0f));
358 if(dst.z) d.z = Max(d.z, Float4(0.0f));
359 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400360
John Bauman19bac1e2014-05-06 15:23:49 -0400361 if(dst.x) d.x = Min(d.x, Float4(1.0f));
362 if(dst.y) d.y = Min(d.y, Float4(1.0f));
363 if(dst.z) d.z = Min(d.z, Float4(1.0f));
364 if(dst.w) d.w = Min(d.w, Float4(1.0f));
365 }
366
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400367 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400368 {
369 Vector4f pDst; // FIXME: Rename
370
371 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400372 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500373 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400374 case Shader::PARAMETER_TEMP:
375 if(dst.rel.type == Shader::PARAMETER_VOID)
376 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500377 if(dst.x) pDst.x = r[dst.index].x;
378 if(dst.y) pDst.y = r[dst.index].y;
379 if(dst.z) pDst.z = r[dst.index].z;
380 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400381 }
382 else
383 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500384 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400385
Nicolas Capens7551ac62016-01-20 17:11:53 -0500386 if(dst.x) pDst.x = r[dst.index + a].x;
387 if(dst.y) pDst.y = r[dst.index + a].y;
388 if(dst.z) pDst.z = r[dst.index + a].z;
389 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400390 }
391 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500392 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400393 case Shader::PARAMETER_RASTOUT:
394 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400395 {
396 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500397 if(dst.x) pDst.x = o[Pos].x;
398 if(dst.y) pDst.y = o[Pos].y;
399 if(dst.z) pDst.z = o[Pos].z;
400 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400401 break;
402 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500403 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400404 break;
405 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500406 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400407 break;
408 default:
409 ASSERT(false);
410 }
411 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400412 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400413 if(dst.x) pDst.x = o[C0 + dst.index].x;
414 if(dst.y) pDst.y = o[C0 + dst.index].y;
415 if(dst.z) pDst.z = o[C0 + dst.index].z;
416 if(dst.w) pDst.w = o[C0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400417 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400418 case Shader::PARAMETER_TEXCRDOUT:
419 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400420 if(version < 0x0300)
421 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500422 if(dst.x) pDst.x = o[T0 + dst.index].x;
423 if(dst.y) pDst.y = o[T0 + dst.index].y;
424 if(dst.z) pDst.z = o[T0 + dst.index].z;
425 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400426 }
427 else
428 {
John Bauman19bac1e2014-05-06 15:23:49 -0400429 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400430 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500431 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;
John Bauman89401822014-05-06 15:04:28 -0400435 }
John Bauman19bac1e2014-05-06 15:23:49 -0400436 else
437 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500438 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400439
Nicolas Capens7551ac62016-01-20 17:11:53 -0500440 if(dst.x) pDst.x = o[dst.index + a].x;
441 if(dst.y) pDst.y = o[dst.index + a].y;
442 if(dst.z) pDst.z = o[dst.index + a].z;
443 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400444 }
445 }
446 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500447 case Shader::PARAMETER_LABEL: break;
448 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
449 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400450 default:
451 ASSERT(false);
452 }
453
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500454 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400455
456 Int4 xEnable = enable;
457 Int4 yEnable = enable;
458 Int4 zEnable = enable;
459 Int4 wEnable = enable;
460
461 if(predicate)
462 {
John Bauman19bac1e2014-05-06 15:23:49 -0400463 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400464
Nicolas Capens7551ac62016-01-20 17:11:53 -0500465 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
466 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
467 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
468 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400469
John Bauman19bac1e2014-05-06 15:23:49 -0400470 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400471 {
John Bauman19bac1e2014-05-06 15:23:49 -0400472 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
473 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
474 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
475 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400476 }
477 else
478 {
John Bauman19bac1e2014-05-06 15:23:49 -0400479 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
480 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
481 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
482 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400483 }
484 }
485
John Bauman19bac1e2014-05-06 15:23:49 -0400486 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
487 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
488 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
489 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400490
John Bauman19bac1e2014-05-06 15:23:49 -0400491 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
492 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
493 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
494 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400495 }
496
John Bauman19bac1e2014-05-06 15:23:49 -0400497 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400498 {
John Bauman19bac1e2014-05-06 15:23:49 -0400499 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400500 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400501 case Shader::PARAMETER_TEMP:
502 if(dst.rel.type == Shader::PARAMETER_VOID)
503 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500504 if(dst.x) r[dst.index].x = d.x;
505 if(dst.y) r[dst.index].y = d.y;
506 if(dst.z) r[dst.index].z = d.z;
507 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400508 }
509 else
510 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500511 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400512
Nicolas Capens7551ac62016-01-20 17:11:53 -0500513 if(dst.x) r[dst.index + a].x = d.x;
514 if(dst.y) r[dst.index + a].y = d.y;
515 if(dst.z) r[dst.index + a].z = d.z;
516 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400517 }
John Bauman89401822014-05-06 15:04:28 -0400518 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400519 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500520 if(dst.x) a0.x = d.x;
521 if(dst.y) a0.y = d.y;
522 if(dst.z) a0.z = d.z;
523 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400524 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400525 case Shader::PARAMETER_RASTOUT:
526 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400527 {
528 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500529 if(dst.x) o[Pos].x = d.x;
530 if(dst.y) o[Pos].y = d.y;
531 if(dst.z) o[Pos].z = d.z;
532 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400533 break;
534 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500535 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400536 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500537 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500538 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400539 break;
540 default: ASSERT(false);
541 }
542 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500543 case Shader::PARAMETER_ATTROUT:
Nicolas Capens995ddea2016-05-17 11:48:56 -0400544 if(dst.x) o[C0 + dst.index].x = d.x;
545 if(dst.y) o[C0 + dst.index].y = d.y;
546 if(dst.z) o[C0 + dst.index].z = d.z;
547 if(dst.w) o[C0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400548 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400549 case Shader::PARAMETER_TEXCRDOUT:
550 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400551 if(version < 0x0300)
552 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500553 if(dst.x) o[T0 + dst.index].x = d.x;
554 if(dst.y) o[T0 + dst.index].y = d.y;
555 if(dst.z) o[T0 + dst.index].z = d.z;
556 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400557 }
558 else
559 {
John Bauman19bac1e2014-05-06 15:23:49 -0400560 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400561 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500562 if(dst.x) o[dst.index].x = d.x;
563 if(dst.y) o[dst.index].y = d.y;
564 if(dst.z) o[dst.index].z = d.z;
565 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400566 }
John Bauman19bac1e2014-05-06 15:23:49 -0400567 else
568 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500569 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400570
Nicolas Capens7551ac62016-01-20 17:11:53 -0500571 if(dst.x) o[dst.index + a].x = d.x;
572 if(dst.y) o[dst.index + a].y = d.y;
573 if(dst.z) o[dst.index + a].z = d.z;
574 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400575 }
576 }
577 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500578 case Shader::PARAMETER_LABEL: break;
579 case Shader::PARAMETER_PREDICATE: p0 = d; break;
580 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400581 default:
582 ASSERT(false);
583 }
584 }
585 }
586
John Bauman19bac1e2014-05-06 15:23:49 -0400587 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400588 {
589 Nucleus::setInsertBlock(returnBlock);
590 }
591 }
592
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500593 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400594 {
John Bauman19bac1e2014-05-06 15:23:49 -0400595 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400596 {
Nicolas Capensec0936c2016-05-18 12:32:02 -0400597 for(int i = 0; i < MAX_VERTEX_OUTPUTS; i++)
John Bauman89401822014-05-06 15:04:28 -0400598 {
Alexis Hetu02ad0aa2016-08-02 11:18:14 -0400599 unsigned char usage = shader->getOutput(i, 0).usage;
John Bauman89401822014-05-06 15:04:28 -0400600
601 switch(usage)
602 {
603 case 0xFF:
604 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400605 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500606 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400607 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400608 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500609 o[i].x = v[i].x;
610 o[i].y = v[i].y;
611 o[i].z = v[i].z;
612 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400613 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400614 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500615 o[i].x = v[i].x;
616 o[i].y = v[i].y;
617 o[i].z = v[i].z;
618 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400619 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400620 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500621 o[i].x = v[i].x;
622 o[i].y = v[i].y;
623 o[i].z = v[i].z;
624 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400625 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400626 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500627 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400628 break;
629 default:
630 ASSERT(false);
631 }
632 }
633 }
634 else
635 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500636 o[Pos].x = v[PositionT].x;
637 o[Pos].y = v[PositionT].y;
638 o[Pos].z = v[PositionT].z;
639 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400640
641 for(int i = 0; i < 2; i++)
642 {
Nicolas Capens995ddea2016-05-17 11:48:56 -0400643 o[C0 + i].x = v[Color0 + i].x;
644 o[C0 + i].y = v[Color0 + i].y;
645 o[C0 + i].z = v[Color0 + i].z;
646 o[C0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400647 }
648
649 for(int i = 0; i < 8; i++)
650 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500651 o[T0 + i].x = v[TexCoord0 + i].x;
652 o[T0 + i].y = v[TexCoord0 + i].y;
653 o[T0 + i].z = v[TexCoord0 + i].z;
654 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400655 }
656
Nicolas Capens7551ac62016-01-20 17:11:53 -0500657 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400658 }
659 }
660
Nicolas Capensc2534f42016-04-04 11:13:24 -0400661 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400662 {
John Bauman19bac1e2014-05-06 15:23:49 -0400663 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500664 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400665
John Bauman89401822014-05-06 15:04:28 -0400666 switch(src.type)
667 {
John Bauman19bac1e2014-05-06 15:23:49 -0400668 case Shader::PARAMETER_TEMP:
669 if(src.rel.type == Shader::PARAMETER_VOID)
670 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500671 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400672 }
673 else
674 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400675 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400676 }
677 break;
678 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500679 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400680 break;
681 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400682 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400683 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500684 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400685 }
686 else
687 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400688 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400689 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400690 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500691 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400692 case Shader::PARAMETER_FLOAT4LITERAL:
693 reg.x = Float4(src.value[0]);
694 reg.y = Float4(src.value[1]);
695 reg.z = Float4(src.value[2]);
696 reg.w = Float4(src.value[3]);
697 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500698 case Shader::PARAMETER_ADDR: reg = a0; break;
699 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
700 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
701 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
702 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400703 case Shader::PARAMETER_SAMPLER:
704 if(src.rel.type == Shader::PARAMETER_VOID)
705 {
706 reg.x = As<Float4>(Int4(i));
707 }
708 else if(src.rel.type == Shader::PARAMETER_TEMP)
709 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500710 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400711 }
712 return reg;
713 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400714 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400715 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500716 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400717 }
718 else
719 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400720 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400721 }
722 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400723 case Shader::PARAMETER_MISCTYPE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500724 reg.x = As<Float>(Int(instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400725 return reg;
John Bauman89401822014-05-06 15:04:28 -0400726 default:
727 ASSERT(false);
728 }
729
John Bauman66b8ab22014-05-06 15:57:45 -0400730 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
731 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
732 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
733 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400734
John Bauman66b8ab22014-05-06 15:57:45 -0400735 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400736
737 switch(src.modifier)
738 {
John Bauman19bac1e2014-05-06 15:23:49 -0400739 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400740 mod.x = x;
741 mod.y = y;
742 mod.z = z;
743 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400744 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400745 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400746 mod.x = -x;
747 mod.y = -y;
748 mod.z = -z;
749 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400750 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400751 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400752 mod.x = Abs(x);
753 mod.y = Abs(y);
754 mod.z = Abs(z);
755 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400756 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400757 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400758 mod.x = -Abs(x);
759 mod.y = -Abs(y);
760 mod.z = -Abs(z);
761 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400762 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400763 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400764 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
765 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
766 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
767 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400768 break;
769 default:
770 ASSERT(false);
771 }
772
773 return mod;
774 }
775
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400776 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
777 {
778 if(bufferIndex == -1)
779 {
780 return data + OFFSET(DrawData, vs.c[index]);
781 }
782 else
783 {
784 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
785 }
786 }
787
788 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
789 {
790 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
791 }
792
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500793 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400794 {
John Bauman19bac1e2014-05-06 15:23:49 -0400795 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500796 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400797
798 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
799 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400800 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400801
802 c.x = c.x.xxxx;
803 c.y = c.y.yyyy;
804 c.z = c.z.zzzz;
805 c.w = c.w.wwww;
806
Nicolas Capenseafdb222015-05-15 15:24:08 -0400807 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400808 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500809 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400810 {
811 const Shader::Instruction &instruction = *shader->getInstruction(j);
812
813 if(instruction.opcode == Shader::OPCODE_DEF)
814 {
815 if(instruction.dst.index == i)
816 {
817 c.x = Float4(instruction.src[0].value[0]);
818 c.y = Float4(instruction.src[0].value[1]);
819 c.z = Float4(instruction.src[0].value[2]);
820 c.w = Float4(instruction.src[0].value[3]);
821
822 break;
823 }
824 }
825 }
826 }
827 }
828 else if(src.rel.type == Shader::PARAMETER_LOOP)
829 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500830 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400831
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400832 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400833
834 c.x = c.x.xxxx;
835 c.y = c.y.yyyy;
836 c.z = c.z.zzzz;
837 c.w = c.w.wwww;
838 }
839 else
840 {
841 if(src.rel.deterministic)
842 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400843 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400844
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400845 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500846
John Bauman19bac1e2014-05-06 15:23:49 -0400847 c.x = c.x.xxxx;
848 c.y = c.y.yyyy;
849 c.z = c.z.zzzz;
850 c.w = c.w.wwww;
851 }
852 else
853 {
854 int component = src.rel.swizzle & 0x03;
855 Float4 a;
856
857 switch(src.rel.type)
858 {
Nicolas Capens32980ac2016-08-15 15:33:58 -0400859 case Shader::PARAMETER_ADDR: a = a0[component]; break;
860 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
861 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
862 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
863 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
864 case Shader::PARAMETER_MISCTYPE: a = As<Float4>(Int4(instanceID)); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400865 default: ASSERT(false);
866 }
867
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400868 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400869
Alexis Hetu028f41b2016-01-13 14:40:47 -0500870 index = Min(As<UInt4>(index), UInt4(VERTEX_UNIFORM_VECTORS)); // Clamp to constant register range, c[VERTEX_UNIFORM_VECTORS] = {0, 0, 0, 0}
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500871
John Bauman19bac1e2014-05-06 15:23:49 -0400872 Int index0 = Extract(index, 0);
873 Int index1 = Extract(index, 1);
874 Int index2 = Extract(index, 2);
875 Int index3 = Extract(index, 3);
876
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400877 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
878 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
879 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
880 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400881
882 transpose4x4(c.x, c.y, c.z, c.w);
883 }
884 }
885
886 return c;
887 }
888
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400889 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400890 {
891 ASSERT(var.rel.deterministic);
892
893 if(var.rel.type == Shader::PARAMETER_TEMP)
894 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500895 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400896 }
897 else if(var.rel.type == Shader::PARAMETER_INPUT)
898 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500899 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400900 }
901 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
902 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500903 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400904 }
905 else if(var.rel.type == Shader::PARAMETER_CONST)
906 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500907 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400908 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500909 else if(var.rel.type == Shader::PARAMETER_LOOP)
910 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500911 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500912 }
John Bauman19bac1e2014-05-06 15:23:49 -0400913 else ASSERT(false);
914
915 return 0;
916 }
917
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500918 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400919 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500920 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500921
John Baumand4ae8632014-05-06 16:18:33 -0400922 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400923 {
John Baumand4ae8632014-05-06 16:18:33 -0400924 if(shader->containsBreakInstruction() && instruction->analysisBreak)
925 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500926 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400927 }
John Bauman19bac1e2014-05-06 15:23:49 -0400928
John Baumand4ae8632014-05-06 16:18:33 -0400929 if(shader->containsContinueInstruction() && instruction->analysisContinue)
930 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500931 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400932 }
John Bauman19bac1e2014-05-06 15:23:49 -0400933
John Baumand4ae8632014-05-06 16:18:33 -0400934 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
935 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500936 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400937 }
John Bauman19bac1e2014-05-06 15:23:49 -0400938 }
939
940 return enable;
941 }
942
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500943 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400944 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400945 Vector4f row0 = fetchRegister(src1, 0);
946 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400947
948 dst.x = dot3(src0, row0);
949 dst.y = dot3(src0, row1);
950 }
951
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500952 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400953 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400954 Vector4f row0 = fetchRegister(src1, 0);
955 Vector4f row1 = fetchRegister(src1, 1);
956 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400957
958 dst.x = dot3(src0, row0);
959 dst.y = dot3(src0, row1);
960 dst.z = dot3(src0, row2);
961 }
962
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500963 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400964 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400965 Vector4f row0 = fetchRegister(src1, 0);
966 Vector4f row1 = fetchRegister(src1, 1);
967 Vector4f row2 = fetchRegister(src1, 2);
968 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400969
970 dst.x = dot3(src0, row0);
971 dst.y = dot3(src0, row1);
972 dst.z = dot3(src0, row2);
973 dst.w = dot3(src0, row3);
974 }
975
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500976 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400977 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400978 Vector4f row0 = fetchRegister(src1, 0);
979 Vector4f row1 = fetchRegister(src1, 1);
980 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400981
982 dst.x = dot4(src0, row0);
983 dst.y = dot4(src0, row1);
984 dst.z = dot4(src0, row2);
985 }
986
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500987 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400988 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400989 Vector4f row0 = fetchRegister(src1, 0);
990 Vector4f row1 = fetchRegister(src1, 1);
991 Vector4f row2 = fetchRegister(src1, 2);
992 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400993
994 dst.x = dot4(src0, row0);
995 dst.y = dot4(src0, row1);
996 dst.z = dot4(src0, row2);
997 dst.w = dot4(src0, row3);
998 }
999
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001000 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -04001001 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001002 BasicBlock *deadBlock = Nucleus::createBasicBlock();
1003 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001004
1005 if(breakDepth == 0)
1006 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001007 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001008 Nucleus::createBr(endBlock);
1009 }
1010 else
1011 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001012 enableBreak = enableBreak & ~enableStack[enableIndex];
1013 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001014
Nicolas Capens7551ac62016-01-20 17:11:53 -05001015 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001016 branch(allBreak, endBlock, deadBlock);
1017 }
1018
1019 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001020 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001021 }
1022
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001023 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001024 {
1025 Int4 condition;
1026
1027 switch(control)
1028 {
John Bauman19bac1e2014-05-06 15:23:49 -04001029 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1030 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1031 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1032 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1033 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1034 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001035 default:
1036 ASSERT(false);
1037 }
1038
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001039 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001040 }
1041
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001042 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001043 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001044 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001045
John Bauman19bac1e2014-05-06 15:23:49 -04001046 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001047 {
1048 condition = ~condition;
1049 }
1050
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001051 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001052 }
1053
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001054 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001055 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001056 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001057
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001058 BasicBlock *continueBlock = Nucleus::createBasicBlock();
1059 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001060
Nicolas Capens7551ac62016-01-20 17:11:53 -05001061 enableBreak = enableBreak & ~condition;
1062 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001063
Nicolas Capens7551ac62016-01-20 17:11:53 -05001064 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001065 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001066
John Bauman89401822014-05-06 15:04:28 -04001067 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001068 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001069 }
1070
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001071 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001072 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001073 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001074 }
1075
1076 void VertexProgram::TEST()
1077 {
1078 whileTest = true;
1079 }
1080
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001081 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001082 {
1083 if(!labelBlock[labelIndex])
1084 {
1085 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1086 }
1087
John Bauman19bac1e2014-05-06 15:23:49 -04001088 if(callRetBlock[labelIndex].size() > 1)
1089 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001090 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001091 }
John Bauman89401822014-05-06 15:04:28 -04001092
Nicolas Capens7551ac62016-01-20 17:11:53 -05001093 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001094
1095 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001096 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1097
Nicolas Capens7551ac62016-01-20 17:11:53 -05001098 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001099 }
1100
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001101 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001102 {
John Bauman19bac1e2014-05-06 15:23:49 -04001103 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001104 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001105 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001106 }
John Bauman19bac1e2014-05-06 15:23:49 -04001107 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001108 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001109 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001110 }
1111 else ASSERT(false);
1112 }
1113
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001114 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001115 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001116 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001117
John Bauman19bac1e2014-05-06 15:23:49 -04001118 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001119 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001120 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001121 }
1122
1123 if(!labelBlock[labelIndex])
1124 {
1125 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1126 }
1127
John Bauman19bac1e2014-05-06 15:23:49 -04001128 if(callRetBlock[labelIndex].size() > 1)
1129 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001130 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001131 }
John Bauman89401822014-05-06 15:04:28 -04001132
Nicolas Capens7551ac62016-01-20 17:11:53 -05001133 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001134
John Bauman19bac1e2014-05-06 15:23:49 -04001135 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1136 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1137
Nicolas Capens7551ac62016-01-20 17:11:53 -05001138 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001139 }
1140
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001141 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001142 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001143 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001144
John Bauman19bac1e2014-05-06 15:23:49 -04001145 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001146 {
1147 condition = ~condition;
1148 }
1149
Nicolas Capens7551ac62016-01-20 17:11:53 -05001150 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001151
1152 if(!labelBlock[labelIndex])
1153 {
1154 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1155 }
1156
John Bauman19bac1e2014-05-06 15:23:49 -04001157 if(callRetBlock[labelIndex].size() > 1)
1158 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001159 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001160 }
John Bauman89401822014-05-06 15:04:28 -04001161
Nicolas Capens7551ac62016-01-20 17:11:53 -05001162 enableIndex++;
1163 enableStack[enableIndex] = condition;
1164 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001165
John Bauman19bac1e2014-05-06 15:23:49 -04001166 Bool notAllFalse = SignMask(condition) != 0;
1167 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1168 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001169
Nicolas Capens7551ac62016-01-20 17:11:53 -05001170 enableIndex--;
1171 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001172 }
1173
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001174 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001175 {
1176 ifDepth--;
1177
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001178 BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1179 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001180
1181 if(isConditionalIf[ifDepth])
1182 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001183 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001184 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001185
1186 branch(notAllFalse, falseBlock, endBlock);
1187
Nicolas Capens7551ac62016-01-20 17:11:53 -05001188 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001189 }
1190 else
1191 {
1192 Nucleus::createBr(endBlock);
1193 Nucleus::setInsertBlock(falseBlock);
1194 }
1195
1196 ifFalseBlock[ifDepth] = endBlock;
1197
1198 ifDepth++;
1199 }
1200
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001201 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001202 {
1203 ifDepth--;
1204
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001205 BasicBlock *endBlock = ifFalseBlock[ifDepth];
John Bauman89401822014-05-06 15:04:28 -04001206
1207 Nucleus::createBr(endBlock);
1208 Nucleus::setInsertBlock(endBlock);
1209
1210 if(isConditionalIf[ifDepth])
1211 {
1212 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001213 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001214 }
1215 }
1216
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001217 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001218 {
1219 loopRepDepth--;
1220
Nicolas Capens7551ac62016-01-20 17:11:53 -05001221 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001222
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001223 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1224 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman89401822014-05-06 15:04:28 -04001225
1226 Nucleus::createBr(testBlock);
1227 Nucleus::setInsertBlock(endBlock);
1228
Nicolas Capens7551ac62016-01-20 17:11:53 -05001229 loopDepth--;
1230 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001231 }
1232
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001233 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001234 {
1235 loopRepDepth--;
1236
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001237 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1238 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001239
1240 Nucleus::createBr(testBlock);
1241 Nucleus::setInsertBlock(endBlock);
1242
Nicolas Capens7551ac62016-01-20 17:11:53 -05001243 loopDepth--;
1244 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001245 }
1246
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001247 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001248 {
1249 loopRepDepth--;
1250
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001251 BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1252 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
John Bauman19bac1e2014-05-06 15:23:49 -04001253
1254 Nucleus::createBr(testBlock);
1255 Nucleus::setInsertBlock(endBlock);
1256
Nicolas Capens7551ac62016-01-20 17:11:53 -05001257 enableIndex--;
1258 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001259 whileTest = false;
1260 }
1261
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001262 void VertexProgram::ENDSWITCH()
1263 {
1264 loopRepDepth--;
1265
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001266 BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
Nicolas Capensec0936c2016-05-18 12:32:02 -04001267
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001268 Nucleus::createBr(loopRepEndBlock[loopRepDepth]);
1269 Nucleus::setInsertBlock(endBlock);
1270
1271 enableIndex--;
1272 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
1273 }
1274
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001275 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001276 {
John Bauman19bac1e2014-05-06 15:23:49 -04001277 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001278 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001279 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001280 }
John Bauman19bac1e2014-05-06 15:23:49 -04001281 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001282 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001283 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001284 }
John Bauman19bac1e2014-05-06 15:23:49 -04001285 else
1286 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001287 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001288 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001289 }
John Bauman89401822014-05-06 15:04:28 -04001290 }
1291
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001292 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001293 {
1294 ASSERT(ifDepth < 24 + 4);
1295
Nicolas Capens7551ac62016-01-20 17:11:53 -05001296 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001297
John Bauman19bac1e2014-05-06 15:23:49 -04001298 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001299 {
John Bauman19bac1e2014-05-06 15:23:49 -04001300 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001301 }
1302
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001303 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1304 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001305
1306 branch(condition, trueBlock, falseBlock);
1307
1308 isConditionalIf[ifDepth] = false;
1309 ifFalseBlock[ifDepth] = falseBlock;
1310
1311 ifDepth++;
1312 }
1313
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001314 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001315 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001316 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001317
John Bauman19bac1e2014-05-06 15:23:49 -04001318 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001319 {
1320 condition = ~condition;
1321 }
1322
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001323 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001324 }
1325
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001326 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001327 {
1328 Int4 condition;
1329
1330 switch(control)
1331 {
John Bauman19bac1e2014-05-06 15:23:49 -04001332 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1333 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1334 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1335 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1336 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1337 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001338 default:
1339 ASSERT(false);
1340 }
1341
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001342 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001343 }
1344
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001345 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001346 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001347 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001348
Nicolas Capens7551ac62016-01-20 17:11:53 -05001349 enableIndex++;
1350 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001351
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001352 BasicBlock *trueBlock = Nucleus::createBasicBlock();
1353 BasicBlock *falseBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001354
John Bauman19bac1e2014-05-06 15:23:49 -04001355 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001356
1357 branch(notAllFalse, trueBlock, falseBlock);
1358
1359 isConditionalIf[ifDepth] = true;
1360 ifFalseBlock[ifDepth] = falseBlock;
1361
1362 ifDepth++;
1363 breakDepth++;
1364 }
1365
1366 void VertexProgram::LABEL(int labelIndex)
1367 {
John Bauman19bac1e2014-05-06 15:23:49 -04001368 if(!labelBlock[labelIndex])
1369 {
1370 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1371 }
1372
John Bauman89401822014-05-06 15:04:28 -04001373 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001374 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001375 }
1376
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001377 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001378 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001379 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001380
Nicolas Capens7551ac62016-01-20 17:11:53 -05001381 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1382 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1383 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001384
1385 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001386 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001387 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001388 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001389 }
1390
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001391 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1392 BasicBlock *testBlock = Nucleus::createBasicBlock();
1393 BasicBlock *endBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001394
1395 loopRepTestBlock[loopRepDepth] = testBlock;
1396 loopRepEndBlock[loopRepDepth] = endBlock;
1397
1398 // FIXME: jump(testBlock)
1399 Nucleus::createBr(testBlock);
1400 Nucleus::setInsertBlock(testBlock);
1401
Nicolas Capens7551ac62016-01-20 17:11:53 -05001402 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001403 Nucleus::setInsertBlock(loopBlock);
1404
Nicolas Capens7551ac62016-01-20 17:11:53 -05001405 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001406
John Bauman89401822014-05-06 15:04:28 -04001407 loopRepDepth++;
1408 breakDepth = 0;
1409 }
1410
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001411 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001412 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001413 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001414
Nicolas Capens7551ac62016-01-20 17:11:53 -05001415 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1416 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001417
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: --
John Bauman89401822014-05-06 15:04:28 -04001433
1434 loopRepDepth++;
1435 breakDepth = 0;
1436 }
1437
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001438 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001439 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001440 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001441
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001442 BasicBlock *loopBlock = Nucleus::createBasicBlock();
1443 BasicBlock *testBlock = Nucleus::createBasicBlock();
1444 BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001445
John Bauman19bac1e2014-05-06 15:23:49 -04001446 loopRepTestBlock[loopRepDepth] = testBlock;
1447 loopRepEndBlock[loopRepDepth] = endBlock;
1448
Nicolas Capens7551ac62016-01-20 17:11:53 -05001449 Int4 restoreBreak = enableBreak;
1450 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001451
1452 // FIXME: jump(testBlock)
1453 Nucleus::createBr(testBlock);
1454 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001455 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001456
Nicolas Capensc2534f42016-04-04 11:13:24 -04001457 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001458 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001459 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001460 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001461 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001462
1463 Bool notAllFalse = SignMask(condition) != 0;
1464 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001465
John Bauman19bac1e2014-05-06 15:23:49 -04001466 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001467 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001468
John Bauman19bac1e2014-05-06 15:23:49 -04001469 Nucleus::setInsertBlock(loopBlock);
1470
1471 loopRepDepth++;
1472 breakDepth = 0;
1473 }
1474
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001475 void VertexProgram::SWITCH()
1476 {
1477 enableIndex++;
1478 enableStack[enableIndex] = Int4(0xFFFFFFFF);
1479
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001480 BasicBlock *endBlock = Nucleus::createBasicBlock();
Alexis Hetu9aa83a92016-05-02 17:34:46 -04001481
1482 loopRepTestBlock[loopRepDepth] = nullptr;
1483 loopRepEndBlock[loopRepDepth] = endBlock;
1484
1485 loopRepDepth++;
1486 breakDepth = 0;
1487 }
1488
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001489 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001490 {
John Bauman19bac1e2014-05-06 15:23:49 -04001491 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001492 {
1493 returnBlock = Nucleus::createBasicBlock();
1494 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001495 }
1496 else
1497 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001498 BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001499
John Bauman19bac1e2014-05-06 15:23:49 -04001500 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001501 {
John Bauman19bac1e2014-05-06 15:23:49 -04001502 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001503 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001504
Nicolas Capens19336542016-09-26 10:32:29 -04001505 Value *value = index.loadValue();
1506 Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
John Bauman19bac1e2014-05-06 15:23:49 -04001507
1508 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1509 {
1510 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1511 }
1512 }
1513 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1514 {
1515 Nucleus::createBr(callRetBlock[currentLabel][0]);
1516 }
1517 else // Function isn't called
1518 {
1519 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001520 }
1521
1522 Nucleus::setInsertBlock(unreachableBlock);
1523 Nucleus::createUnreachable();
1524 }
1525 }
1526
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001527 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001528 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001529 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001530
John Bauman19bac1e2014-05-06 15:23:49 -04001531 // FIXME: Return from function if all instances left
1532 // FIXME: Use enableLeave in other control-flow constructs
1533 }
John Bauman89401822014-05-06 15:04:28 -04001534
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001535 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001536 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001537 sampleTexture(dst, src1, src0, a0, a0, src0, Lod);
John Bauman89401822014-05-06 15:04:28 -04001538 }
John Bauman19bac1e2014-05-06 15:23:49 -04001539
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001540 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001541 {
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001542 src0.w = Float(0.0f);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001543 sampleTexture(dst, src1, src0, a0, a0, src0, Lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001544 }
1545
Meng-Lin Wu2337a192016-06-01 13:54:07 -04001546 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001547 {
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001548 src0.w = Float(0.0f);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001549 sampleTexture(dst, src1, src0, a0, a0, src2, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001550 }
1551
Meng-Lin Wu2fce5822016-06-07 16:15:12 -04001552 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001553 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001554 sampleTexture(dst, src1, src0, a0, a0, offset, {Lod, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001555 }
1556
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001557 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001558 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001559 sampleTexture(dst, src1, src0, src0, src0, src0, Fetch);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001560 }
1561
Meng-Lin Wu9d62c482016-06-14 11:11:25 -04001562 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001563 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001564 sampleTexture(dst, src1, src0, src0, src0, offset, {Fetch, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001565 }
1566
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001567 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001568 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001569 sampleTexture(dst, src1, src0, src2, src3, src0, Grad);
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001570 }
1571
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001572 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001573 {
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001574 sampleTexture(dst, src1, src0, src2, src3, offset, {Grad, Offset});
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001575 }
1576
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001577 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001578 {
Alexis Hetu95ac1872016-06-06 13:26:52 -04001579 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
1580 sampler[src1.index]->textureSize(texture, dst, lod);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001581 }
1582
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001583 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Vector4f &uvwq, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
John Bauman19bac1e2014-05-06 15:23:49 -04001584 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001585 Vector4f tmp;
1586
John Bauman19bac1e2014-05-06 15:23:49 -04001587 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1588 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001589 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + s.index * sizeof(Texture);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001590 sampler[s.index]->sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001591 }
1592 else
1593 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001594 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001595
Nicolas Capensc2534f42016-04-04 11:13:24 -04001596 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001597 {
1598 if(shader->usesSampler(i))
1599 {
1600 If(index == i)
1601 {
Meng-Lin Wu234c9a92016-05-25 15:37:43 -04001602 Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + i * sizeof(Texture);
Nicolas Capensa3c16e42016-06-15 16:45:53 -04001603 sampler[i]->sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
John Bauman19bac1e2014-05-06 15:23:49 -04001604 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1605 }
1606 }
1607 }
1608 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001609
1610 c.x = tmp[(s.swizzle >> 0) & 0x3];
1611 c.y = tmp[(s.swizzle >> 2) & 0x3];
1612 c.z = tmp[(s.swizzle >> 4) & 0x3];
1613 c.w = tmp[(s.swizzle >> 6) & 0x3];
John Bauman19bac1e2014-05-06 15:23:49 -04001614 }
John Bauman89401822014-05-06 15:04:28 -04001615}