blob: 5be2070f99f99b283161520744e1619a699b13e6 [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
53 if(shader->instanceIdDeclared)
54 {
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;
299 case Shader::OPCODE_IF: IF(src0); break;
300 case Shader::OPCODE_IFC: IFC(s0, s1, control); break;
301 case Shader::OPCODE_LABEL: LABEL(dst.index); break;
302 case Shader::OPCODE_LOOP: LOOP(src1); break;
303 case Shader::OPCODE_REP: REP(src0); break;
304 case Shader::OPCODE_WHILE: WHILE(src0); break;
305 case Shader::OPCODE_RET: RET(); break;
306 case Shader::OPCODE_LEAVE: LEAVE(); break;
307 case Shader::OPCODE_CMP: cmp(d, s0, s1, control); break;
308 case Shader::OPCODE_ICMP: icmp(d, s0, s1, control); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400309 case Shader::OPCODE_UCMP: ucmp(d, s0, s1, control); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500310 case Shader::OPCODE_SELECT: select(d, s0, s1, s2); break;
311 case Shader::OPCODE_EXTRACT: extract(d.x, s0, s1.x); break;
312 case Shader::OPCODE_INSERT: insert(d, s0, s1.x, s2.x); break;
313 case Shader::OPCODE_ALL: all(d.x, s0); break;
314 case Shader::OPCODE_ANY: any(d.x, s0); break;
315 case Shader::OPCODE_NOT: not(d, s0); break;
Alexis Hetu8d78cf72015-08-28 14:24:45 -0400316 case Shader::OPCODE_OR: or(d, s0, s1); break;
317 case Shader::OPCODE_XOR: xor(d, s0, s1); break;
318 case Shader::OPCODE_AND: and(d, s0, s1); break;
319 case Shader::OPCODE_EQ: equal(d, s0, s1); break;
320 case Shader::OPCODE_NE: notEqual(d, s0, s1); break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500321 case Shader::OPCODE_TEXLDL: TEXLDL(d, s0, src1); break;
322 case Shader::OPCODE_TEX: TEX(d, s0, src1); break;
323 case Shader::OPCODE_TEXOFFSET: TEXOFFSET(d, s0, src1, s2, s3); break;
324 case Shader::OPCODE_TEXLDLOFFSET: TEXLDL(d, s0, src1, s2); break;
325 case Shader::OPCODE_TEXELFETCH: TEXELFETCH(d, s0, src1, s2); break;
326 case Shader::OPCODE_TEXELFETCHOFFSET: TEXELFETCH(d, s0, src1, s2, s3); break;
327 case Shader::OPCODE_TEXGRAD: TEXGRAD(d, s0, src1, s2, s3); break;
328 case Shader::OPCODE_TEXGRADOFFSET: TEXGRAD(d, s0, src1, s2, s3, s4); break;
329 case Shader::OPCODE_TEXSIZE: TEXSIZE(d, s0.x, src1); break;
330 case Shader::OPCODE_END: break;
John Bauman89401822014-05-06 15:04:28 -0400331 default:
332 ASSERT(false);
333 }
334
John Bauman19bac1e2014-05-06 15:23:49 -0400335 if(dst.type != Shader::PARAMETER_VOID && dst.type != Shader::PARAMETER_LABEL && opcode != Shader::OPCODE_NOP)
John Bauman89401822014-05-06 15:04:28 -0400336 {
John Bauman19bac1e2014-05-06 15:23:49 -0400337 if(dst.integer)
John Bauman89401822014-05-06 15:04:28 -0400338 {
John Bauman19bac1e2014-05-06 15:23:49 -0400339 switch(opcode)
340 {
341 case Shader::OPCODE_DIV:
342 if(dst.x) d.x = Trunc(d.x);
343 if(dst.y) d.y = Trunc(d.y);
344 if(dst.z) d.z = Trunc(d.z);
345 if(dst.w) d.w = Trunc(d.w);
346 break;
347 default:
348 break; // No truncation to integer required when arguments are integer
349 }
John Bauman89401822014-05-06 15:04:28 -0400350 }
351
John Bauman19bac1e2014-05-06 15:23:49 -0400352 if(dst.saturate)
John Bauman89401822014-05-06 15:04:28 -0400353 {
John Bauman19bac1e2014-05-06 15:23:49 -0400354 if(dst.x) d.x = Max(d.x, Float4(0.0f));
355 if(dst.y) d.y = Max(d.y, Float4(0.0f));
356 if(dst.z) d.z = Max(d.z, Float4(0.0f));
357 if(dst.w) d.w = Max(d.w, Float4(0.0f));
John Bauman89401822014-05-06 15:04:28 -0400358
John Bauman19bac1e2014-05-06 15:23:49 -0400359 if(dst.x) d.x = Min(d.x, Float4(1.0f));
360 if(dst.y) d.y = Min(d.y, Float4(1.0f));
361 if(dst.z) d.z = Min(d.z, Float4(1.0f));
362 if(dst.w) d.w = Min(d.w, Float4(1.0f));
363 }
364
Nicolas Capensc6e8ab12014-05-06 23:31:07 -0400365 if(instruction->isPredicated())
John Bauman19bac1e2014-05-06 15:23:49 -0400366 {
367 Vector4f pDst; // FIXME: Rename
368
369 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400370 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500371 case Shader::PARAMETER_VOID: break;
John Bauman19bac1e2014-05-06 15:23:49 -0400372 case Shader::PARAMETER_TEMP:
373 if(dst.rel.type == Shader::PARAMETER_VOID)
374 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500375 if(dst.x) pDst.x = r[dst.index].x;
376 if(dst.y) pDst.y = r[dst.index].y;
377 if(dst.z) pDst.z = r[dst.index].z;
378 if(dst.w) pDst.w = r[dst.index].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400379 }
380 else
381 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500382 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400383
Nicolas Capens7551ac62016-01-20 17:11:53 -0500384 if(dst.x) pDst.x = r[dst.index + a].x;
385 if(dst.y) pDst.y = r[dst.index + a].y;
386 if(dst.z) pDst.z = r[dst.index + a].z;
387 if(dst.w) pDst.w = r[dst.index + a].w;
John Bauman19bac1e2014-05-06 15:23:49 -0400388 }
389 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500390 case Shader::PARAMETER_ADDR: pDst = a0; break;
John Bauman19bac1e2014-05-06 15:23:49 -0400391 case Shader::PARAMETER_RASTOUT:
392 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400393 {
394 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500395 if(dst.x) pDst.x = o[Pos].x;
396 if(dst.y) pDst.y = o[Pos].y;
397 if(dst.z) pDst.z = o[Pos].z;
398 if(dst.w) pDst.w = o[Pos].w;
John Bauman89401822014-05-06 15:04:28 -0400399 break;
400 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500401 pDst.x = o[Fog].x;
John Bauman89401822014-05-06 15:04:28 -0400402 break;
403 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500404 pDst.x = o[Pts].y;
John Bauman89401822014-05-06 15:04:28 -0400405 break;
406 default:
407 ASSERT(false);
408 }
409 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400410 case Shader::PARAMETER_ATTROUT:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500411 if(dst.x) pDst.x = o[D0 + dst.index].x;
412 if(dst.y) pDst.y = o[D0 + dst.index].y;
413 if(dst.z) pDst.z = o[D0 + dst.index].z;
414 if(dst.w) pDst.w = o[D0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400415 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400416 case Shader::PARAMETER_TEXCRDOUT:
417 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400418 if(version < 0x0300)
419 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500420 if(dst.x) pDst.x = o[T0 + dst.index].x;
421 if(dst.y) pDst.y = o[T0 + dst.index].y;
422 if(dst.z) pDst.z = o[T0 + dst.index].z;
423 if(dst.w) pDst.w = o[T0 + dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400424 }
425 else
426 {
John Bauman19bac1e2014-05-06 15:23:49 -0400427 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400428 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500429 if(dst.x) pDst.x = o[dst.index].x;
430 if(dst.y) pDst.y = o[dst.index].y;
431 if(dst.z) pDst.z = o[dst.index].z;
432 if(dst.w) pDst.w = o[dst.index].w;
John Bauman89401822014-05-06 15:04:28 -0400433 }
John Bauman19bac1e2014-05-06 15:23:49 -0400434 else
435 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500436 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400437
Nicolas Capens7551ac62016-01-20 17:11:53 -0500438 if(dst.x) pDst.x = o[dst.index + a].x;
439 if(dst.y) pDst.y = o[dst.index + a].y;
440 if(dst.z) pDst.z = o[dst.index + a].z;
441 if(dst.w) pDst.w = o[dst.index + a].w;
John Bauman89401822014-05-06 15:04:28 -0400442 }
443 }
444 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500445 case Shader::PARAMETER_LABEL: break;
446 case Shader::PARAMETER_PREDICATE: pDst = p0; break;
447 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400448 default:
449 ASSERT(false);
450 }
451
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500452 Int4 enable = enableMask(instruction);
John Bauman89401822014-05-06 15:04:28 -0400453
454 Int4 xEnable = enable;
455 Int4 yEnable = enable;
456 Int4 zEnable = enable;
457 Int4 wEnable = enable;
458
459 if(predicate)
460 {
John Bauman19bac1e2014-05-06 15:23:49 -0400461 unsigned char pSwizzle = instruction->predicateSwizzle;
John Bauman89401822014-05-06 15:04:28 -0400462
Nicolas Capens7551ac62016-01-20 17:11:53 -0500463 Float4 xPredicate = p0[(pSwizzle >> 0) & 0x03];
464 Float4 yPredicate = p0[(pSwizzle >> 2) & 0x03];
465 Float4 zPredicate = p0[(pSwizzle >> 4) & 0x03];
466 Float4 wPredicate = p0[(pSwizzle >> 6) & 0x03];
John Bauman89401822014-05-06 15:04:28 -0400467
John Bauman19bac1e2014-05-06 15:23:49 -0400468 if(!instruction->predicateNot)
John Bauman89401822014-05-06 15:04:28 -0400469 {
John Bauman19bac1e2014-05-06 15:23:49 -0400470 if(dst.x) xEnable = xEnable & As<Int4>(xPredicate);
471 if(dst.y) yEnable = yEnable & As<Int4>(yPredicate);
472 if(dst.z) zEnable = zEnable & As<Int4>(zPredicate);
473 if(dst.w) wEnable = wEnable & As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400474 }
475 else
476 {
John Bauman19bac1e2014-05-06 15:23:49 -0400477 if(dst.x) xEnable = xEnable & ~As<Int4>(xPredicate);
478 if(dst.y) yEnable = yEnable & ~As<Int4>(yPredicate);
479 if(dst.z) zEnable = zEnable & ~As<Int4>(zPredicate);
480 if(dst.w) wEnable = wEnable & ~As<Int4>(wPredicate);
John Bauman89401822014-05-06 15:04:28 -0400481 }
482 }
483
John Bauman19bac1e2014-05-06 15:23:49 -0400484 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) & xEnable);
485 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) & yEnable);
486 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) & zEnable);
487 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) & wEnable);
John Bauman89401822014-05-06 15:04:28 -0400488
John Bauman19bac1e2014-05-06 15:23:49 -0400489 if(dst.x) d.x = As<Float4>(As<Int4>(d.x) | (As<Int4>(pDst.x) & ~xEnable));
490 if(dst.y) d.y = As<Float4>(As<Int4>(d.y) | (As<Int4>(pDst.y) & ~yEnable));
491 if(dst.z) d.z = As<Float4>(As<Int4>(d.z) | (As<Int4>(pDst.z) & ~zEnable));
492 if(dst.w) d.w = As<Float4>(As<Int4>(d.w) | (As<Int4>(pDst.w) & ~wEnable));
John Bauman89401822014-05-06 15:04:28 -0400493 }
494
John Bauman19bac1e2014-05-06 15:23:49 -0400495 switch(dst.type)
John Bauman89401822014-05-06 15:04:28 -0400496 {
John Bauman19bac1e2014-05-06 15:23:49 -0400497 case Shader::PARAMETER_VOID:
John Bauman89401822014-05-06 15:04:28 -0400498 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400499 case Shader::PARAMETER_TEMP:
500 if(dst.rel.type == Shader::PARAMETER_VOID)
501 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500502 if(dst.x) r[dst.index].x = d.x;
503 if(dst.y) r[dst.index].y = d.y;
504 if(dst.z) r[dst.index].z = d.z;
505 if(dst.w) r[dst.index].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400506 }
507 else
508 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500509 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400510
Nicolas Capens7551ac62016-01-20 17:11:53 -0500511 if(dst.x) r[dst.index + a].x = d.x;
512 if(dst.y) r[dst.index + a].y = d.y;
513 if(dst.z) r[dst.index + a].z = d.z;
514 if(dst.w) r[dst.index + a].w = d.w;
John Bauman19bac1e2014-05-06 15:23:49 -0400515 }
John Bauman89401822014-05-06 15:04:28 -0400516 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400517 case Shader::PARAMETER_ADDR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500518 if(dst.x) a0.x = d.x;
519 if(dst.y) a0.y = d.y;
520 if(dst.z) a0.z = d.z;
521 if(dst.w) a0.w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400522 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400523 case Shader::PARAMETER_RASTOUT:
524 switch(dst.index)
John Bauman89401822014-05-06 15:04:28 -0400525 {
526 case 0:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500527 if(dst.x) o[Pos].x = d.x;
528 if(dst.y) o[Pos].y = d.y;
529 if(dst.z) o[Pos].z = d.z;
530 if(dst.w) o[Pos].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400531 break;
532 case 1:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500533 o[Fog].x = d.x;
John Bauman89401822014-05-06 15:04:28 -0400534 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500535 case 2:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500536 o[Pts].y = d.x;
John Bauman89401822014-05-06 15:04:28 -0400537 break;
538 default: ASSERT(false);
539 }
540 break;
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500541 case Shader::PARAMETER_ATTROUT:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500542 if(dst.x) o[D0 + dst.index].x = d.x;
543 if(dst.y) o[D0 + dst.index].y = d.y;
544 if(dst.z) o[D0 + dst.index].z = d.z;
545 if(dst.w) o[D0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400546 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400547 case Shader::PARAMETER_TEXCRDOUT:
548 // case Shader::PARAMETER_OUTPUT:
John Bauman89401822014-05-06 15:04:28 -0400549 if(version < 0x0300)
550 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500551 if(dst.x) o[T0 + dst.index].x = d.x;
552 if(dst.y) o[T0 + dst.index].y = d.y;
553 if(dst.z) o[T0 + dst.index].z = d.z;
554 if(dst.w) o[T0 + dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400555 }
556 else
557 {
John Bauman19bac1e2014-05-06 15:23:49 -0400558 if(dst.rel.type == Shader::PARAMETER_VOID) // Not relative
John Bauman89401822014-05-06 15:04:28 -0400559 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500560 if(dst.x) o[dst.index].x = d.x;
561 if(dst.y) o[dst.index].y = d.y;
562 if(dst.z) o[dst.index].z = d.z;
563 if(dst.w) o[dst.index].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400564 }
John Bauman19bac1e2014-05-06 15:23:49 -0400565 else
566 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500567 Int a = relativeAddress(dst);
John Bauman19bac1e2014-05-06 15:23:49 -0400568
Nicolas Capens7551ac62016-01-20 17:11:53 -0500569 if(dst.x) o[dst.index + a].x = d.x;
570 if(dst.y) o[dst.index + a].y = d.y;
571 if(dst.z) o[dst.index + a].z = d.z;
572 if(dst.w) o[dst.index + a].w = d.w;
John Bauman89401822014-05-06 15:04:28 -0400573 }
574 }
575 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500576 case Shader::PARAMETER_LABEL: break;
577 case Shader::PARAMETER_PREDICATE: p0 = d; break;
578 case Shader::PARAMETER_INPUT: break;
John Bauman89401822014-05-06 15:04:28 -0400579 default:
580 ASSERT(false);
581 }
582 }
583 }
584
John Bauman19bac1e2014-05-06 15:23:49 -0400585 if(currentLabel != -1)
John Bauman89401822014-05-06 15:04:28 -0400586 {
587 Nucleus::setInsertBlock(returnBlock);
588 }
589 }
590
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500591 void VertexProgram::passThrough()
John Bauman89401822014-05-06 15:04:28 -0400592 {
John Bauman19bac1e2014-05-06 15:23:49 -0400593 if(shader)
John Bauman89401822014-05-06 15:04:28 -0400594 {
595 for(int i = 0; i < 12; i++)
596 {
John Bauman19bac1e2014-05-06 15:23:49 -0400597 unsigned char usage = shader->output[i][0].usage;
John Bauman89401822014-05-06 15:04:28 -0400598
599 switch(usage)
600 {
601 case 0xFF:
602 continue;
John Bauman19bac1e2014-05-06 15:23:49 -0400603 case Shader::USAGE_PSIZE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500604 o[i].y = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400605 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400606 case Shader::USAGE_TEXCOORD:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500607 o[i].x = v[i].x;
608 o[i].y = v[i].y;
609 o[i].z = v[i].z;
610 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400611 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400612 case Shader::USAGE_POSITION:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500613 o[i].x = v[i].x;
614 o[i].y = v[i].y;
615 o[i].z = v[i].z;
616 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400617 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400618 case Shader::USAGE_COLOR:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500619 o[i].x = v[i].x;
620 o[i].y = v[i].y;
621 o[i].z = v[i].z;
622 o[i].w = v[i].w;
John Bauman89401822014-05-06 15:04:28 -0400623 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400624 case Shader::USAGE_FOG:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500625 o[i].x = v[i].x;
John Bauman89401822014-05-06 15:04:28 -0400626 break;
627 default:
628 ASSERT(false);
629 }
630 }
631 }
632 else
633 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500634 o[Pos].x = v[PositionT].x;
635 o[Pos].y = v[PositionT].y;
636 o[Pos].z = v[PositionT].z;
637 o[Pos].w = v[PositionT].w;
John Bauman89401822014-05-06 15:04:28 -0400638
639 for(int i = 0; i < 2; i++)
640 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500641 o[D0 + i].x = v[Color0 + i].x;
642 o[D0 + i].y = v[Color0 + i].y;
643 o[D0 + i].z = v[Color0 + i].z;
644 o[D0 + i].w = v[Color0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400645 }
646
647 for(int i = 0; i < 8; i++)
648 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500649 o[T0 + i].x = v[TexCoord0 + i].x;
650 o[T0 + i].y = v[TexCoord0 + i].y;
651 o[T0 + i].z = v[TexCoord0 + i].z;
652 o[T0 + i].w = v[TexCoord0 + i].w;
John Bauman89401822014-05-06 15:04:28 -0400653 }
654
Nicolas Capens7551ac62016-01-20 17:11:53 -0500655 o[Pts].y = v[PointSize].x;
John Bauman89401822014-05-06 15:04:28 -0400656 }
657 }
658
Nicolas Capensc2534f42016-04-04 11:13:24 -0400659 Vector4f VertexProgram::fetchRegister(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400660 {
John Bauman19bac1e2014-05-06 15:23:49 -0400661 Vector4f reg;
Nicolas Capens5d961882016-01-01 23:18:14 -0500662 unsigned int i = src.index + offset;
John Bauman89401822014-05-06 15:04:28 -0400663
John Bauman89401822014-05-06 15:04:28 -0400664 switch(src.type)
665 {
John Bauman19bac1e2014-05-06 15:23:49 -0400666 case Shader::PARAMETER_TEMP:
667 if(src.rel.type == Shader::PARAMETER_VOID)
668 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500669 reg = r[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400670 }
671 else
672 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400673 reg = r[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400674 }
675 break;
676 case Shader::PARAMETER_CONST:
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500677 reg = readConstant(src, offset);
John Bauman19bac1e2014-05-06 15:23:49 -0400678 break;
679 case Shader::PARAMETER_INPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400680 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400681 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500682 reg = v[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400683 }
684 else
685 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400686 reg = v[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400687 }
Nicolas Capens0bac2852016-05-07 06:09:58 -0400688 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500689 case Shader::PARAMETER_VOID: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400690 case Shader::PARAMETER_FLOAT4LITERAL:
691 reg.x = Float4(src.value[0]);
692 reg.y = Float4(src.value[1]);
693 reg.z = Float4(src.value[2]);
694 reg.w = Float4(src.value[3]);
695 break;
Nicolas Capens7551ac62016-01-20 17:11:53 -0500696 case Shader::PARAMETER_ADDR: reg = a0; break;
697 case Shader::PARAMETER_CONSTBOOL: return r[0]; // Dummy
698 case Shader::PARAMETER_CONSTINT: return r[0]; // Dummy
699 case Shader::PARAMETER_LOOP: return r[0]; // Dummy
700 case Shader::PARAMETER_PREDICATE: return r[0]; // Dummy
John Bauman19bac1e2014-05-06 15:23:49 -0400701 case Shader::PARAMETER_SAMPLER:
702 if(src.rel.type == Shader::PARAMETER_VOID)
703 {
704 reg.x = As<Float4>(Int4(i));
705 }
706 else if(src.rel.type == Shader::PARAMETER_TEMP)
707 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500708 reg.x = As<Float4>(Int4(i) + As<Int4>(r[src.rel.index].x));
John Bauman19bac1e2014-05-06 15:23:49 -0400709 }
710 return reg;
711 case Shader::PARAMETER_OUTPUT:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400712 if(src.rel.type == Shader::PARAMETER_VOID)
John Bauman19bac1e2014-05-06 15:23:49 -0400713 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500714 reg = o[i];
John Bauman19bac1e2014-05-06 15:23:49 -0400715 }
716 else
717 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400718 reg = o[i + relativeAddress(src, src.bufferIndex)];
John Bauman19bac1e2014-05-06 15:23:49 -0400719 }
720 break;
Alexis Hetudd8df682015-06-05 17:08:39 -0400721 case Shader::PARAMETER_MISCTYPE:
Nicolas Capens7551ac62016-01-20 17:11:53 -0500722 reg.x = As<Float>(Int(instanceID));
Alexis Hetudd8df682015-06-05 17:08:39 -0400723 return reg;
John Bauman89401822014-05-06 15:04:28 -0400724 default:
725 ASSERT(false);
726 }
727
John Bauman66b8ab22014-05-06 15:57:45 -0400728 const Float4 &x = reg[(src.swizzle >> 0) & 0x3];
729 const Float4 &y = reg[(src.swizzle >> 2) & 0x3];
730 const Float4 &z = reg[(src.swizzle >> 4) & 0x3];
731 const Float4 &w = reg[(src.swizzle >> 6) & 0x3];
John Bauman89401822014-05-06 15:04:28 -0400732
John Bauman66b8ab22014-05-06 15:57:45 -0400733 Vector4f mod;
John Bauman89401822014-05-06 15:04:28 -0400734
735 switch(src.modifier)
736 {
John Bauman19bac1e2014-05-06 15:23:49 -0400737 case Shader::MODIFIER_NONE:
John Bauman66b8ab22014-05-06 15:57:45 -0400738 mod.x = x;
739 mod.y = y;
740 mod.z = z;
741 mod.w = w;
John Bauman89401822014-05-06 15:04:28 -0400742 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400743 case Shader::MODIFIER_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400744 mod.x = -x;
745 mod.y = -y;
746 mod.z = -z;
747 mod.w = -w;
John Bauman89401822014-05-06 15:04:28 -0400748 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400749 case Shader::MODIFIER_ABS:
John Bauman66b8ab22014-05-06 15:57:45 -0400750 mod.x = Abs(x);
751 mod.y = Abs(y);
752 mod.z = Abs(z);
753 mod.w = Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400754 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400755 case Shader::MODIFIER_ABS_NEGATE:
John Bauman66b8ab22014-05-06 15:57:45 -0400756 mod.x = -Abs(x);
757 mod.y = -Abs(y);
758 mod.z = -Abs(z);
759 mod.w = -Abs(w);
John Bauman89401822014-05-06 15:04:28 -0400760 break;
John Bauman19bac1e2014-05-06 15:23:49 -0400761 case Shader::MODIFIER_NOT:
John Bauman66b8ab22014-05-06 15:57:45 -0400762 mod.x = As<Float4>(As<Int4>(x) ^ Int4(0xFFFFFFFF));
763 mod.y = As<Float4>(As<Int4>(y) ^ Int4(0xFFFFFFFF));
764 mod.z = As<Float4>(As<Int4>(z) ^ Int4(0xFFFFFFFF));
765 mod.w = As<Float4>(As<Int4>(w) ^ Int4(0xFFFFFFFF));
John Bauman89401822014-05-06 15:04:28 -0400766 break;
767 default:
768 ASSERT(false);
769 }
770
771 return mod;
772 }
773
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400774 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index)
775 {
776 if(bufferIndex == -1)
777 {
778 return data + OFFSET(DrawData, vs.c[index]);
779 }
780 else
781 {
782 return *Pointer<Pointer<Byte>>(data + OFFSET(DrawData, vs.u[bufferIndex])) + index;
783 }
784 }
785
786 RValue<Pointer<Byte>> VertexProgram::uniformAddress(int bufferIndex, unsigned int index, Int& offset)
787 {
788 return uniformAddress(bufferIndex, index) + offset * sizeof(float4);
789 }
790
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500791 Vector4f VertexProgram::readConstant(const Src &src, unsigned int offset)
John Bauman89401822014-05-06 15:04:28 -0400792 {
John Bauman19bac1e2014-05-06 15:23:49 -0400793 Vector4f c;
Nicolas Capens5d961882016-01-01 23:18:14 -0500794 unsigned int i = src.index + offset;
John Bauman19bac1e2014-05-06 15:23:49 -0400795
796 if(src.rel.type == Shader::PARAMETER_VOID) // Not relative
797 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400798 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i));
John Bauman19bac1e2014-05-06 15:23:49 -0400799
800 c.x = c.x.xxxx;
801 c.y = c.y.yyyy;
802 c.z = c.z.zzzz;
803 c.w = c.w.wwww;
804
Nicolas Capenseafdb222015-05-15 15:24:08 -0400805 if(shader->containsDefineInstruction()) // Constant may be known at compile time
John Bauman19bac1e2014-05-06 15:23:49 -0400806 {
Alexis Hetu903e0252014-11-25 14:25:32 -0500807 for(size_t j = 0; j < shader->getLength(); j++)
John Bauman19bac1e2014-05-06 15:23:49 -0400808 {
809 const Shader::Instruction &instruction = *shader->getInstruction(j);
810
811 if(instruction.opcode == Shader::OPCODE_DEF)
812 {
813 if(instruction.dst.index == i)
814 {
815 c.x = Float4(instruction.src[0].value[0]);
816 c.y = Float4(instruction.src[0].value[1]);
817 c.z = Float4(instruction.src[0].value[2]);
818 c.w = Float4(instruction.src[0].value[3]);
819
820 break;
821 }
822 }
823 }
824 }
825 }
826 else if(src.rel.type == Shader::PARAMETER_LOOP)
827 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500828 Int loopCounter = aL[loopDepth];
John Bauman19bac1e2014-05-06 15:23:49 -0400829
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400830 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, loopCounter));
John Bauman19bac1e2014-05-06 15:23:49 -0400831
832 c.x = c.x.xxxx;
833 c.y = c.y.yyyy;
834 c.z = c.z.zzzz;
835 c.w = c.w.wwww;
836 }
837 else
838 {
839 if(src.rel.deterministic)
840 {
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400841 Int a = relativeAddress(src, src.bufferIndex);
Nicolas Capensc2534f42016-04-04 11:13:24 -0400842
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400843 c.x = c.y = c.z = c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, i, a));
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500844
John Bauman19bac1e2014-05-06 15:23:49 -0400845 c.x = c.x.xxxx;
846 c.y = c.y.yyyy;
847 c.z = c.z.zzzz;
848 c.w = c.w.wwww;
849 }
850 else
851 {
852 int component = src.rel.swizzle & 0x03;
853 Float4 a;
854
855 switch(src.rel.type)
856 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500857 case Shader::PARAMETER_ADDR: a = a0[component]; break;
858 case Shader::PARAMETER_TEMP: a = r[src.rel.index][component]; break;
859 case Shader::PARAMETER_INPUT: a = v[src.rel.index][component]; break;
860 case Shader::PARAMETER_OUTPUT: a = o[src.rel.index][component]; break;
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400861 case Shader::PARAMETER_CONST: a = *Pointer<Float>(uniformAddress(src.bufferIndex, src.rel.index) + component * sizeof(float)); break;
John Bauman19bac1e2014-05-06 15:23:49 -0400862 default: ASSERT(false);
863 }
864
Alexis Hetu02a2bb82015-08-20 14:10:33 -0400865 Int4 index = Int4(i) + As<Int4>(a) * Int4(src.rel.scale);
John Bauman19bac1e2014-05-06 15:23:49 -0400866
Alexis Hetu028f41b2016-01-13 14:40:47 -0500867 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 -0500868
John Bauman19bac1e2014-05-06 15:23:49 -0400869 Int index0 = Extract(index, 0);
870 Int index1 = Extract(index, 1);
871 Int index2 = Extract(index, 2);
872 Int index3 = Extract(index, 3);
873
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400874 c.x = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index0), 16);
875 c.y = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index1), 16);
876 c.z = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index2), 16);
877 c.w = *Pointer<Float4>(uniformAddress(src.bufferIndex, 0, index3), 16);
John Bauman19bac1e2014-05-06 15:23:49 -0400878
879 transpose4x4(c.x, c.y, c.z, c.w);
880 }
881 }
882
883 return c;
884 }
885
Alexis Hetu2c2a7b22015-10-27 16:12:11 -0400886 Int VertexProgram::relativeAddress(const Shader::Parameter &var, int bufferIndex)
John Bauman19bac1e2014-05-06 15:23:49 -0400887 {
888 ASSERT(var.rel.deterministic);
889
890 if(var.rel.type == Shader::PARAMETER_TEMP)
891 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500892 return As<Int>(Extract(r[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400893 }
894 else if(var.rel.type == Shader::PARAMETER_INPUT)
895 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500896 return As<Int>(Extract(v[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400897 }
898 else if(var.rel.type == Shader::PARAMETER_OUTPUT)
899 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500900 return As<Int>(Extract(o[var.rel.index].x, 0)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400901 }
902 else if(var.rel.type == Shader::PARAMETER_CONST)
903 {
Alexis Hetu48be7352016-02-10 14:32:34 -0500904 return *Pointer<Int>(uniformAddress(bufferIndex, var.rel.index)) * var.rel.scale;
John Bauman19bac1e2014-05-06 15:23:49 -0400905 }
Nicolas Capens907700d2016-01-20 17:09:28 -0500906 else if(var.rel.type == Shader::PARAMETER_LOOP)
907 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500908 return aL[loopDepth];
Nicolas Capens907700d2016-01-20 17:09:28 -0500909 }
John Bauman19bac1e2014-05-06 15:23:49 -0400910 else ASSERT(false);
911
912 return 0;
913 }
914
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500915 Int4 VertexProgram::enableMask(const Shader::Instruction *instruction)
John Bauman19bac1e2014-05-06 15:23:49 -0400916 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500917 Int4 enable = instruction->analysisBranch ? Int4(enableStack[enableIndex]) : Int4(0xFFFFFFFF);
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500918
John Baumand4ae8632014-05-06 16:18:33 -0400919 if(!whileTest)
John Bauman19bac1e2014-05-06 15:23:49 -0400920 {
John Baumand4ae8632014-05-06 16:18:33 -0400921 if(shader->containsBreakInstruction() && instruction->analysisBreak)
922 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500923 enable &= enableBreak;
John Baumand4ae8632014-05-06 16:18:33 -0400924 }
John Bauman19bac1e2014-05-06 15:23:49 -0400925
John Baumand4ae8632014-05-06 16:18:33 -0400926 if(shader->containsContinueInstruction() && instruction->analysisContinue)
927 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500928 enable &= enableContinue;
John Baumand4ae8632014-05-06 16:18:33 -0400929 }
John Bauman19bac1e2014-05-06 15:23:49 -0400930
John Baumand4ae8632014-05-06 16:18:33 -0400931 if(shader->containsLeaveInstruction() && instruction->analysisLeave)
932 {
Nicolas Capens7551ac62016-01-20 17:11:53 -0500933 enable &= enableLeave;
John Baumand4ae8632014-05-06 16:18:33 -0400934 }
John Bauman19bac1e2014-05-06 15:23:49 -0400935 }
936
937 return enable;
938 }
939
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500940 void VertexProgram::M3X2(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -0400941 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400942 Vector4f row0 = fetchRegister(src1, 0);
943 Vector4f row1 = fetchRegister(src1, 1);
John Bauman89401822014-05-06 15:04:28 -0400944
945 dst.x = dot3(src0, row0);
946 dst.y = dot3(src0, row1);
947 }
948
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500949 void VertexProgram::M3X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400950 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400951 Vector4f row0 = fetchRegister(src1, 0);
952 Vector4f row1 = fetchRegister(src1, 1);
953 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400954
955 dst.x = dot3(src0, row0);
956 dst.y = dot3(src0, row1);
957 dst.z = dot3(src0, row2);
958 }
959
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500960 void VertexProgram::M3X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400961 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400962 Vector4f row0 = fetchRegister(src1, 0);
963 Vector4f row1 = fetchRegister(src1, 1);
964 Vector4f row2 = fetchRegister(src1, 2);
965 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400966
967 dst.x = dot3(src0, row0);
968 dst.y = dot3(src0, row1);
969 dst.z = dot3(src0, row2);
970 dst.w = dot3(src0, row3);
971 }
972
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500973 void VertexProgram::M4X3(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400974 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400975 Vector4f row0 = fetchRegister(src1, 0);
976 Vector4f row1 = fetchRegister(src1, 1);
977 Vector4f row2 = fetchRegister(src1, 2);
John Bauman89401822014-05-06 15:04:28 -0400978
979 dst.x = dot4(src0, row0);
980 dst.y = dot4(src0, row1);
981 dst.z = dot4(src0, row2);
982 }
983
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500984 void VertexProgram::M4X4(Vector4f &dst, Vector4f &src0, Src &src1)
John Bauman89401822014-05-06 15:04:28 -0400985 {
Nicolas Capensc2534f42016-04-04 11:13:24 -0400986 Vector4f row0 = fetchRegister(src1, 0);
987 Vector4f row1 = fetchRegister(src1, 1);
988 Vector4f row2 = fetchRegister(src1, 2);
989 Vector4f row3 = fetchRegister(src1, 3);
John Bauman89401822014-05-06 15:04:28 -0400990
991 dst.x = dot4(src0, row0);
992 dst.y = dot4(src0, row1);
993 dst.z = dot4(src0, row2);
994 dst.w = dot4(src0, row3);
995 }
996
Nicolas Capensb4fb3672016-01-15 17:02:41 -0500997 void VertexProgram::BREAK()
John Bauman89401822014-05-06 15:04:28 -0400998 {
999 llvm::BasicBlock *deadBlock = Nucleus::createBasicBlock();
1000 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1001
1002 if(breakDepth == 0)
1003 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001004 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001005 Nucleus::createBr(endBlock);
1006 }
1007 else
1008 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001009 enableBreak = enableBreak & ~enableStack[enableIndex];
1010 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001011
Nicolas Capens7551ac62016-01-20 17:11:53 -05001012 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001013 branch(allBreak, endBlock, deadBlock);
1014 }
1015
1016 Nucleus::setInsertBlock(deadBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001017 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001018 }
1019
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001020 void VertexProgram::BREAKC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001021 {
1022 Int4 condition;
1023
1024 switch(control)
1025 {
John Bauman19bac1e2014-05-06 15:23:49 -04001026 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1027 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1028 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1029 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1030 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1031 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001032 default:
1033 ASSERT(false);
1034 }
1035
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001036 BREAK(condition);
John Bauman89401822014-05-06 15:04:28 -04001037 }
1038
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001039 void VertexProgram::BREAKP(const Src &predicateRegister) // FIXME: Factor out parts common with BREAKC
John Bauman89401822014-05-06 15:04:28 -04001040 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001041 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001042
John Bauman19bac1e2014-05-06 15:23:49 -04001043 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001044 {
1045 condition = ~condition;
1046 }
1047
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001048 BREAK(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001049 }
1050
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001051 void VertexProgram::BREAK(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001052 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001053 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001054
1055 llvm::BasicBlock *continueBlock = Nucleus::createBasicBlock();
1056 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth - 1];
1057
Nicolas Capens7551ac62016-01-20 17:11:53 -05001058 enableBreak = enableBreak & ~condition;
1059 Bool allBreak = SignMask(enableBreak) == 0x0;
John Bauman89401822014-05-06 15:04:28 -04001060
Nicolas Capens7551ac62016-01-20 17:11:53 -05001061 enableIndex = enableIndex - breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001062 branch(allBreak, endBlock, continueBlock);
John Bauman19bac1e2014-05-06 15:23:49 -04001063
John Bauman89401822014-05-06 15:04:28 -04001064 Nucleus::setInsertBlock(continueBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001065 enableIndex = enableIndex + breakDepth;
John Bauman89401822014-05-06 15:04:28 -04001066 }
1067
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001068 void VertexProgram::CONTINUE()
John Bauman19bac1e2014-05-06 15:23:49 -04001069 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001070 enableContinue = enableContinue & ~enableStack[enableIndex];
John Bauman19bac1e2014-05-06 15:23:49 -04001071 }
1072
1073 void VertexProgram::TEST()
1074 {
1075 whileTest = true;
1076 }
1077
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001078 void VertexProgram::CALL(int labelIndex, int callSiteIndex)
John Bauman89401822014-05-06 15:04:28 -04001079 {
1080 if(!labelBlock[labelIndex])
1081 {
1082 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1083 }
1084
John Bauman19bac1e2014-05-06 15:23:49 -04001085 if(callRetBlock[labelIndex].size() > 1)
1086 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001087 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001088 }
John Bauman89401822014-05-06 15:04:28 -04001089
Nicolas Capens7551ac62016-01-20 17:11:53 -05001090 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001091
1092 Nucleus::createBr(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001093 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1094
Nicolas Capens7551ac62016-01-20 17:11:53 -05001095 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001096 }
1097
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001098 void VertexProgram::CALLNZ(int labelIndex, int callSiteIndex, const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001099 {
John Bauman19bac1e2014-05-06 15:23:49 -04001100 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001101 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001102 CALLNZb(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001103 }
John Bauman19bac1e2014-05-06 15:23:49 -04001104 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001105 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001106 CALLNZp(labelIndex, callSiteIndex, src);
John Bauman89401822014-05-06 15:04:28 -04001107 }
1108 else ASSERT(false);
1109 }
1110
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001111 void VertexProgram::CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001112 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001113 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001114
John Bauman19bac1e2014-05-06 15:23:49 -04001115 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001116 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001117 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001118 }
1119
1120 if(!labelBlock[labelIndex])
1121 {
1122 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1123 }
1124
John Bauman19bac1e2014-05-06 15:23:49 -04001125 if(callRetBlock[labelIndex].size() > 1)
1126 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001127 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001128 }
John Bauman89401822014-05-06 15:04:28 -04001129
Nicolas Capens7551ac62016-01-20 17:11:53 -05001130 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001131
John Bauman19bac1e2014-05-06 15:23:49 -04001132 branch(condition, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1133 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
1134
Nicolas Capens7551ac62016-01-20 17:11:53 -05001135 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001136 }
1137
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001138 void VertexProgram::CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001139 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001140 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001141
John Bauman19bac1e2014-05-06 15:23:49 -04001142 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001143 {
1144 condition = ~condition;
1145 }
1146
Nicolas Capens7551ac62016-01-20 17:11:53 -05001147 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001148
1149 if(!labelBlock[labelIndex])
1150 {
1151 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1152 }
1153
John Bauman19bac1e2014-05-06 15:23:49 -04001154 if(callRetBlock[labelIndex].size() > 1)
1155 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001156 callStack[stackIndex++] = UInt(callSiteIndex);
John Bauman19bac1e2014-05-06 15:23:49 -04001157 }
John Bauman89401822014-05-06 15:04:28 -04001158
Nicolas Capens7551ac62016-01-20 17:11:53 -05001159 enableIndex++;
1160 enableStack[enableIndex] = condition;
1161 Int4 restoreLeave = enableLeave;
John Bauman89401822014-05-06 15:04:28 -04001162
John Bauman19bac1e2014-05-06 15:23:49 -04001163 Bool notAllFalse = SignMask(condition) != 0;
1164 branch(notAllFalse, labelBlock[labelIndex], callRetBlock[labelIndex][callSiteIndex]);
1165 Nucleus::setInsertBlock(callRetBlock[labelIndex][callSiteIndex]);
John Bauman89401822014-05-06 15:04:28 -04001166
Nicolas Capens7551ac62016-01-20 17:11:53 -05001167 enableIndex--;
1168 enableLeave = restoreLeave;
John Bauman89401822014-05-06 15:04:28 -04001169 }
1170
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001171 void VertexProgram::ELSE()
John Bauman89401822014-05-06 15:04:28 -04001172 {
1173 ifDepth--;
1174
1175 llvm::BasicBlock *falseBlock = ifFalseBlock[ifDepth];
1176 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1177
1178 if(isConditionalIf[ifDepth])
1179 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001180 Int4 condition = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman19bac1e2014-05-06 15:23:49 -04001181 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001182
1183 branch(notAllFalse, falseBlock, endBlock);
1184
Nicolas Capens7551ac62016-01-20 17:11:53 -05001185 enableStack[enableIndex] = ~enableStack[enableIndex] & enableStack[enableIndex - 1];
John Bauman89401822014-05-06 15:04:28 -04001186 }
1187 else
1188 {
1189 Nucleus::createBr(endBlock);
1190 Nucleus::setInsertBlock(falseBlock);
1191 }
1192
1193 ifFalseBlock[ifDepth] = endBlock;
1194
1195 ifDepth++;
1196 }
1197
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001198 void VertexProgram::ENDIF()
John Bauman89401822014-05-06 15:04:28 -04001199 {
1200 ifDepth--;
1201
1202 llvm::BasicBlock *endBlock = ifFalseBlock[ifDepth];
1203
1204 Nucleus::createBr(endBlock);
1205 Nucleus::setInsertBlock(endBlock);
1206
1207 if(isConditionalIf[ifDepth])
1208 {
1209 breakDepth--;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001210 enableIndex--;
John Bauman89401822014-05-06 15:04:28 -04001211 }
1212 }
1213
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001214 void VertexProgram::ENDLOOP()
John Bauman89401822014-05-06 15:04:28 -04001215 {
1216 loopRepDepth--;
1217
Nicolas Capens7551ac62016-01-20 17:11:53 -05001218 aL[loopDepth] = aL[loopDepth] + increment[loopDepth]; // FIXME: +=
John Bauman89401822014-05-06 15:04:28 -04001219
1220 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1221 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1222
1223 Nucleus::createBr(testBlock);
1224 Nucleus::setInsertBlock(endBlock);
1225
Nicolas Capens7551ac62016-01-20 17:11:53 -05001226 loopDepth--;
1227 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman89401822014-05-06 15:04:28 -04001228 }
1229
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001230 void VertexProgram::ENDREP()
John Bauman19bac1e2014-05-06 15:23:49 -04001231 {
1232 loopRepDepth--;
1233
1234 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1235 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1236
1237 Nucleus::createBr(testBlock);
1238 Nucleus::setInsertBlock(endBlock);
1239
Nicolas Capens7551ac62016-01-20 17:11:53 -05001240 loopDepth--;
1241 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001242 }
1243
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001244 void VertexProgram::ENDWHILE()
John Bauman19bac1e2014-05-06 15:23:49 -04001245 {
1246 loopRepDepth--;
1247
1248 llvm::BasicBlock *testBlock = loopRepTestBlock[loopRepDepth];
1249 llvm::BasicBlock *endBlock = loopRepEndBlock[loopRepDepth];
1250
1251 Nucleus::createBr(testBlock);
1252 Nucleus::setInsertBlock(endBlock);
1253
Nicolas Capens7551ac62016-01-20 17:11:53 -05001254 enableIndex--;
1255 enableBreak = Int4(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
John Bauman19bac1e2014-05-06 15:23:49 -04001256 whileTest = false;
1257 }
1258
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001259 void VertexProgram::IF(const Src &src)
John Bauman89401822014-05-06 15:04:28 -04001260 {
John Bauman19bac1e2014-05-06 15:23:49 -04001261 if(src.type == Shader::PARAMETER_CONSTBOOL)
John Bauman89401822014-05-06 15:04:28 -04001262 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001263 IFb(src);
John Bauman89401822014-05-06 15:04:28 -04001264 }
John Bauman19bac1e2014-05-06 15:23:49 -04001265 else if(src.type == Shader::PARAMETER_PREDICATE)
John Bauman89401822014-05-06 15:04:28 -04001266 {
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001267 IFp(src);
John Bauman89401822014-05-06 15:04:28 -04001268 }
John Bauman19bac1e2014-05-06 15:23:49 -04001269 else
1270 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001271 Int4 condition = As<Int4>(fetchRegister(src).x);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001272 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001273 }
John Bauman89401822014-05-06 15:04:28 -04001274 }
1275
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001276 void VertexProgram::IFb(const Src &boolRegister)
John Bauman89401822014-05-06 15:04:28 -04001277 {
1278 ASSERT(ifDepth < 24 + 4);
1279
Nicolas Capens7551ac62016-01-20 17:11:53 -05001280 Bool condition = (*Pointer<Byte>(data + OFFSET(DrawData,vs.b[boolRegister.index])) != Byte(0)); // FIXME
John Bauman89401822014-05-06 15:04:28 -04001281
John Bauman19bac1e2014-05-06 15:23:49 -04001282 if(boolRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001283 {
John Bauman19bac1e2014-05-06 15:23:49 -04001284 condition = !condition;
John Bauman89401822014-05-06 15:04:28 -04001285 }
1286
1287 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1288 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1289
1290 branch(condition, trueBlock, falseBlock);
1291
1292 isConditionalIf[ifDepth] = false;
1293 ifFalseBlock[ifDepth] = falseBlock;
1294
1295 ifDepth++;
1296 }
1297
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001298 void VertexProgram::IFp(const Src &predicateRegister)
John Bauman89401822014-05-06 15:04:28 -04001299 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001300 Int4 condition = As<Int4>(p0[predicateRegister.swizzle & 0x3]);
John Bauman89401822014-05-06 15:04:28 -04001301
John Bauman19bac1e2014-05-06 15:23:49 -04001302 if(predicateRegister.modifier == Shader::MODIFIER_NOT)
John Bauman89401822014-05-06 15:04:28 -04001303 {
1304 condition = ~condition;
1305 }
1306
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001307 IF(condition);
John Bauman89401822014-05-06 15:04:28 -04001308 }
1309
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001310 void VertexProgram::IFC(Vector4f &src0, Vector4f &src1, Control control)
John Bauman89401822014-05-06 15:04:28 -04001311 {
1312 Int4 condition;
1313
1314 switch(control)
1315 {
John Bauman19bac1e2014-05-06 15:23:49 -04001316 case Shader::CONTROL_GT: condition = CmpNLE(src0.x, src1.x); break;
1317 case Shader::CONTROL_EQ: condition = CmpEQ(src0.x, src1.x); break;
1318 case Shader::CONTROL_GE: condition = CmpNLT(src0.x, src1.x); break;
1319 case Shader::CONTROL_LT: condition = CmpLT(src0.x, src1.x); break;
1320 case Shader::CONTROL_NE: condition = CmpNEQ(src0.x, src1.x); break;
1321 case Shader::CONTROL_LE: condition = CmpLE(src0.x, src1.x); break;
John Bauman89401822014-05-06 15:04:28 -04001322 default:
1323 ASSERT(false);
1324 }
1325
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001326 IF(condition);
John Bauman19bac1e2014-05-06 15:23:49 -04001327 }
1328
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001329 void VertexProgram::IF(Int4 &condition)
John Bauman19bac1e2014-05-06 15:23:49 -04001330 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001331 condition &= enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001332
Nicolas Capens7551ac62016-01-20 17:11:53 -05001333 enableIndex++;
1334 enableStack[enableIndex] = condition;
John Bauman89401822014-05-06 15:04:28 -04001335
1336 llvm::BasicBlock *trueBlock = Nucleus::createBasicBlock();
1337 llvm::BasicBlock *falseBlock = Nucleus::createBasicBlock();
1338
John Bauman19bac1e2014-05-06 15:23:49 -04001339 Bool notAllFalse = SignMask(condition) != 0;
John Bauman89401822014-05-06 15:04:28 -04001340
1341 branch(notAllFalse, trueBlock, falseBlock);
1342
1343 isConditionalIf[ifDepth] = true;
1344 ifFalseBlock[ifDepth] = falseBlock;
1345
1346 ifDepth++;
1347 breakDepth++;
1348 }
1349
1350 void VertexProgram::LABEL(int labelIndex)
1351 {
John Bauman19bac1e2014-05-06 15:23:49 -04001352 if(!labelBlock[labelIndex])
1353 {
1354 labelBlock[labelIndex] = Nucleus::createBasicBlock();
1355 }
1356
John Bauman89401822014-05-06 15:04:28 -04001357 Nucleus::setInsertBlock(labelBlock[labelIndex]);
John Bauman19bac1e2014-05-06 15:23:49 -04001358 currentLabel = labelIndex;
John Bauman89401822014-05-06 15:04:28 -04001359 }
1360
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001361 void VertexProgram::LOOP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001362 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001363 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001364
Nicolas Capens7551ac62016-01-20 17:11:53 -05001365 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1366 aL[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][1]));
1367 increment[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][2]));
John Bauman89401822014-05-06 15:04:28 -04001368
1369 // FIXME: Compiles to two instructions?
Nicolas Capens7551ac62016-01-20 17:11:53 -05001370 If(increment[loopDepth] == 0)
John Bauman89401822014-05-06 15:04:28 -04001371 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001372 increment[loopDepth] = 1;
John Bauman89401822014-05-06 15:04:28 -04001373 }
1374
1375 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1376 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1377 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1378
1379 loopRepTestBlock[loopRepDepth] = testBlock;
1380 loopRepEndBlock[loopRepDepth] = endBlock;
1381
1382 // FIXME: jump(testBlock)
1383 Nucleus::createBr(testBlock);
1384 Nucleus::setInsertBlock(testBlock);
1385
Nicolas Capens7551ac62016-01-20 17:11:53 -05001386 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001387 Nucleus::setInsertBlock(loopBlock);
1388
Nicolas Capens7551ac62016-01-20 17:11:53 -05001389 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001390
John Bauman89401822014-05-06 15:04:28 -04001391 loopRepDepth++;
1392 breakDepth = 0;
1393 }
1394
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001395 void VertexProgram::REP(const Src &integerRegister)
John Bauman89401822014-05-06 15:04:28 -04001396 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001397 loopDepth++;
John Bauman89401822014-05-06 15:04:28 -04001398
Nicolas Capens7551ac62016-01-20 17:11:53 -05001399 iteration[loopDepth] = *Pointer<Int>(data + OFFSET(DrawData,vs.i[integerRegister.index][0]));
1400 aL[loopDepth] = aL[loopDepth - 1];
John Bauman89401822014-05-06 15:04:28 -04001401
1402 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1403 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1404 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
1405
1406 loopRepTestBlock[loopRepDepth] = testBlock;
1407 loopRepEndBlock[loopRepDepth] = endBlock;
1408
1409 // FIXME: jump(testBlock)
1410 Nucleus::createBr(testBlock);
1411 Nucleus::setInsertBlock(testBlock);
1412
Nicolas Capens7551ac62016-01-20 17:11:53 -05001413 branch(iteration[loopDepth] > 0, loopBlock, endBlock);
John Bauman89401822014-05-06 15:04:28 -04001414 Nucleus::setInsertBlock(loopBlock);
1415
Nicolas Capens7551ac62016-01-20 17:11:53 -05001416 iteration[loopDepth] = iteration[loopDepth] - 1; // FIXME: --
John Bauman89401822014-05-06 15:04:28 -04001417
1418 loopRepDepth++;
1419 breakDepth = 0;
1420 }
1421
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001422 void VertexProgram::WHILE(const Src &temporaryRegister)
John Bauman19bac1e2014-05-06 15:23:49 -04001423 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001424 enableIndex++;
John Bauman19bac1e2014-05-06 15:23:49 -04001425
1426 llvm::BasicBlock *loopBlock = Nucleus::createBasicBlock();
1427 llvm::BasicBlock *testBlock = Nucleus::createBasicBlock();
1428 llvm::BasicBlock *endBlock = Nucleus::createBasicBlock();
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001429
John Bauman19bac1e2014-05-06 15:23:49 -04001430 loopRepTestBlock[loopRepDepth] = testBlock;
1431 loopRepEndBlock[loopRepDepth] = endBlock;
1432
Nicolas Capens7551ac62016-01-20 17:11:53 -05001433 Int4 restoreBreak = enableBreak;
1434 Int4 restoreContinue = enableContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001435
1436 // FIXME: jump(testBlock)
1437 Nucleus::createBr(testBlock);
1438 Nucleus::setInsertBlock(testBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001439 enableContinue = restoreContinue;
John Bauman19bac1e2014-05-06 15:23:49 -04001440
Nicolas Capensc2534f42016-04-04 11:13:24 -04001441 const Vector4f &src = fetchRegister(temporaryRegister);
John Bauman19bac1e2014-05-06 15:23:49 -04001442 Int4 condition = As<Int4>(src.x);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001443 condition &= enableStack[enableIndex - 1];
Nicolas Capens2ff29482016-04-28 15:28:02 -04001444 if(shader->containsLeaveInstruction()) condition &= enableLeave;
Nicolas Capens7551ac62016-01-20 17:11:53 -05001445 enableStack[enableIndex] = condition;
John Bauman19bac1e2014-05-06 15:23:49 -04001446
1447 Bool notAllFalse = SignMask(condition) != 0;
1448 branch(notAllFalse, loopBlock, endBlock);
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001449
John Bauman19bac1e2014-05-06 15:23:49 -04001450 Nucleus::setInsertBlock(endBlock);
Nicolas Capens7551ac62016-01-20 17:11:53 -05001451 enableBreak = restoreBreak;
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001452
John Bauman19bac1e2014-05-06 15:23:49 -04001453 Nucleus::setInsertBlock(loopBlock);
1454
1455 loopRepDepth++;
1456 breakDepth = 0;
1457 }
1458
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001459 void VertexProgram::RET()
John Bauman89401822014-05-06 15:04:28 -04001460 {
John Bauman19bac1e2014-05-06 15:23:49 -04001461 if(currentLabel == -1)
John Bauman89401822014-05-06 15:04:28 -04001462 {
1463 returnBlock = Nucleus::createBasicBlock();
1464 Nucleus::createBr(returnBlock);
John Bauman89401822014-05-06 15:04:28 -04001465 }
1466 else
1467 {
John Bauman89401822014-05-06 15:04:28 -04001468 llvm::BasicBlock *unreachableBlock = Nucleus::createBasicBlock();
John Bauman89401822014-05-06 15:04:28 -04001469
John Bauman19bac1e2014-05-06 15:23:49 -04001470 if(callRetBlock[currentLabel].size() > 1) // Pop the return destination from the call stack
John Bauman89401822014-05-06 15:04:28 -04001471 {
John Bauman19bac1e2014-05-06 15:23:49 -04001472 // FIXME: Encapsulate
Nicolas Capens7551ac62016-01-20 17:11:53 -05001473 UInt index = callStack[--stackIndex];
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001474
John Bauman66b8ab22014-05-06 15:57:45 -04001475 llvm::Value *value = index.loadValue();
John Bauman19bac1e2014-05-06 15:23:49 -04001476 llvm::Value *switchInst = Nucleus::createSwitch(value, unreachableBlock, (int)callRetBlock[currentLabel].size());
1477
1478 for(unsigned int i = 0; i < callRetBlock[currentLabel].size(); i++)
1479 {
1480 Nucleus::addSwitchCase(switchInst, i, callRetBlock[currentLabel][i]);
1481 }
1482 }
1483 else if(callRetBlock[currentLabel].size() == 1) // Jump directly to the unique return destination
1484 {
1485 Nucleus::createBr(callRetBlock[currentLabel][0]);
1486 }
1487 else // Function isn't called
1488 {
1489 Nucleus::createBr(unreachableBlock);
John Bauman89401822014-05-06 15:04:28 -04001490 }
1491
1492 Nucleus::setInsertBlock(unreachableBlock);
1493 Nucleus::createUnreachable();
1494 }
1495 }
1496
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001497 void VertexProgram::LEAVE()
John Bauman89401822014-05-06 15:04:28 -04001498 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001499 enableLeave = enableLeave & ~enableStack[enableIndex];
John Bauman89401822014-05-06 15:04:28 -04001500
John Bauman19bac1e2014-05-06 15:23:49 -04001501 // FIXME: Return from function if all instances left
1502 // FIXME: Use enableLeave in other control-flow constructs
1503 }
John Bauman89401822014-05-06 15:04:28 -04001504
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001505 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001506 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001507 sampleTexture(dst, src1, src0.x, src0.y, src0.z, src0.w, Lod);
John Bauman89401822014-05-06 15:04:28 -04001508 }
John Bauman19bac1e2014-05-06 15:23:49 -04001509
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001510 void VertexProgram::TEX(Vector4f &dst, Vector4f &src0, const Src &src1)
John Bauman19bac1e2014-05-06 15:23:49 -04001511 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001512 Float4 lod0 = Float4(0.0f);
1513 sampleTexture(dst, src1, src0.x, src0.y, src0.z, lod0, Lod);
John Bauman19bac1e2014-05-06 15:23:49 -04001514 }
1515
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001516 void VertexProgram::TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001517 {
1518 UNIMPLEMENTED();
1519 }
1520
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001521 void VertexProgram::TEXLDL(Vector4f &dst, Vector4f &src, const Src&, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001522 {
1523 UNIMPLEMENTED();
1524 }
1525
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001526 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001527 {
1528 UNIMPLEMENTED();
1529 }
1530
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001531 void VertexProgram::TEXELFETCH(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001532 {
1533 UNIMPLEMENTED();
1534 }
1535
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001536 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001537 {
1538 UNIMPLEMENTED();
1539 }
1540
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001541 void VertexProgram::TEXGRAD(Vector4f &dst, Vector4f &src0, const Src& src1, Vector4f &src2, Vector4f &src3, Vector4f &offset)
Alexis Hetu25d47fc2015-10-22 13:58:32 -04001542 {
1543 UNIMPLEMENTED();
1544 }
1545
Nicolas Capensb4fb3672016-01-15 17:02:41 -05001546 void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001547 {
Nicolas Capens7551ac62016-01-20 17:11:53 -05001548 Pointer<Byte> textureMipmap = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture) + OFFSET(Texture, mipmap);
Alexis Hetu9bcb31d2015-07-22 17:03:26 -04001549 for(int i = 0; i < 4; ++i)
1550 {
1551 Pointer<Byte> mipmap = textureMipmap + (As<Int>(Extract(lod, i)) + Int(1)) * sizeof(Mipmap);
1552 dst.x = Insert(dst.x, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, width)))), i);
1553 dst.y = Insert(dst.y, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, height)))), i);
1554 dst.z = Insert(dst.z, As<Float>(Int(*Pointer<Short>(mipmap + OFFSET(Mipmap, depth)))), i);
1555 }
1556 }
1557
Nicolas Capensc2534f42016-04-04 11:13:24 -04001558 void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Float4 &u, Float4 &v, Float4 &w, Float4 &q, SamplerMethod method)
John Bauman19bac1e2014-05-06 15:23:49 -04001559 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001560 Vector4f tmp;
1561
John Bauman19bac1e2014-05-06 15:23:49 -04001562 if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
1563 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001564 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[TEXTURE_IMAGE_UNITS]) + s.index * sizeof(Texture);
1565 sampler[s.index]->sampleTexture(texture, tmp, u, v, w, q, a0, a0, method);
John Bauman19bac1e2014-05-06 15:23:49 -04001566 }
1567 else
1568 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001569 Int index = As<Int>(Float(fetchRegister(s).x.x));
John Bauman19bac1e2014-05-06 15:23:49 -04001570
Nicolas Capensc2534f42016-04-04 11:13:24 -04001571 for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
John Bauman19bac1e2014-05-06 15:23:49 -04001572 {
1573 if(shader->usesSampler(i))
1574 {
1575 If(index == i)
1576 {
Nicolas Capensc2534f42016-04-04 11:13:24 -04001577 Pointer<Byte> texture = data + OFFSET(DrawData,mipmap[TEXTURE_IMAGE_UNITS]) + i * sizeof(Texture);
1578 sampler[i]->sampleTexture(texture, tmp, u, v, w, q, a0, a0, method);
John Bauman19bac1e2014-05-06 15:23:49 -04001579 // FIXME: When the sampler states are the same, we could use one sampler and just index the texture
1580 }
1581 }
1582 }
1583 }
Nicolas Capensc2534f42016-04-04 11:13:24 -04001584
1585 c.x = tmp[(s.swizzle >> 0) & 0x3];
1586 c.y = tmp[(s.swizzle >> 2) & 0x3];
1587 c.z = tmp[(s.swizzle >> 4) & 0x3];
1588 c.w = tmp[(s.swizzle >> 6) & 0x3];
John Bauman19bac1e2014-05-06 15:23:49 -04001589 }
John Bauman89401822014-05-06 15:04:28 -04001590}