• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // 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
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // 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.
14 
15 #ifndef sw_ShaderCore_hpp
16 #define sw_ShaderCore_hpp
17 
18 #include "Shader.hpp"
19 #include "Reactor/Reactor.hpp"
20 #include "Vulkan/VkDebug.hpp"
21 
22 namespace sw
23 {
24 	using namespace rr;
25 
26 	class Vector4s
27 	{
28 	public:
29 		Vector4s();
30 		Vector4s(unsigned short x, unsigned short y, unsigned short z, unsigned short w);
31 		Vector4s(const Vector4s &rhs);
32 
33 		Short4 &operator[](int i);
34 		Vector4s &operator=(const Vector4s &rhs);
35 
36 		Short4 x;
37 		Short4 y;
38 		Short4 z;
39 		Short4 w;
40 	};
41 
42 	class Vector4f
43 	{
44 	public:
45 		Vector4f();
46 		Vector4f(float x, float y, float z, float w);
47 		Vector4f(const Vector4f &rhs);
48 
49 		Float4 &operator[](int i);
50 		Vector4f &operator=(const Vector4f &rhs);
51 
52 		Float4 x;
53 		Float4 y;
54 		Float4 z;
55 		Float4 w;
56 	};
57 
58 	Float4 exponential2(RValue<Float4> x, bool pp = false);
59 	Float4 logarithm2(RValue<Float4> x, bool abs, bool pp = false);
60 	Float4 exponential(RValue<Float4> x, bool pp = false);
61 	Float4 logarithm(RValue<Float4> x, bool abs, bool pp = false);
62 	Float4 power(RValue<Float4> x, RValue<Float4> y, bool pp = false);
63 	Float4 reciprocal(RValue<Float4> x, bool pp = false, bool finite = false, bool exactAtPow2 = false);
64 	Float4 reciprocalSquareRoot(RValue<Float4> x, bool abs, bool pp = false);
65 	Float4 modulo(RValue<Float4> x, RValue<Float4> y);
66 	Float4 sine_pi(RValue<Float4> x, bool pp = false);     // limited to [-pi, pi] range
67 	Float4 cosine_pi(RValue<Float4> x, bool pp = false);   // limited to [-pi, pi] range
68 	Float4 sine(RValue<Float4> x, bool pp = false);
69 	Float4 cosine(RValue<Float4> x, bool pp = false);
70 	Float4 tangent(RValue<Float4> x, bool pp = false);
71 	Float4 arccos(RValue<Float4> x, bool pp = false);
72 	Float4 arcsin(RValue<Float4> x, bool pp = false);
73 	Float4 arctan(RValue<Float4> x, bool pp = false);
74 	Float4 arctan(RValue<Float4> y, RValue<Float4> x, bool pp = false);
75 	Float4 sineh(RValue<Float4> x, bool pp = false);
76 	Float4 cosineh(RValue<Float4> x, bool pp = false);
77 	Float4 tangenth(RValue<Float4> x, bool pp = false);
78 	Float4 arccosh(RValue<Float4> x, bool pp = false);  // Limited to x >= 1
79 	Float4 arcsinh(RValue<Float4> x, bool pp = false);
80 	Float4 arctanh(RValue<Float4> x, bool pp = false);  // Limited to ]-1, 1[ range
81 
82 	Float4 dot2(const Vector4f &v0, const Vector4f &v1);
83 	Float4 dot3(const Vector4f &v0, const Vector4f &v1);
84 	Float4 dot4(const Vector4f &v0, const Vector4f &v1);
85 
86 	void transpose4x4(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3);
87 	void transpose4x3(Short4 &row0, Short4 &row1, Short4 &row2, Short4 &row3);
88 	void transpose4x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3);
89 	void transpose4x3(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3);
90 	void transpose4x2(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3);
91 	void transpose4x1(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3);
92 	void transpose2x4(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3);
93 	void transpose4xN(Float4 &row0, Float4 &row1, Float4 &row2, Float4 &row3, int N);
94 
95 	class Register
96 	{
97 	public:
Register(const Reference<Float4> & x,const Reference<Float4> & y,const Reference<Float4> & z,const Reference<Float4> & w)98 		Register(const Reference<Float4> &x, const Reference<Float4> &y, const Reference<Float4> &z, const Reference<Float4> &w) : x(x), y(y), z(z), w(w)
99 		{
100 		}
101 
operator [](int i)102 		Reference<Float4> &operator[](int i)
103 		{
104 			switch(i)
105 			{
106 			default:
107 			case 0: return x;
108 			case 1: return y;
109 			case 2: return z;
110 			case 3: return w;
111 			}
112 		}
113 
operator =(const Register & rhs)114 		Register &operator=(const Register &rhs)
115 		{
116 			x = rhs.x;
117 			y = rhs.y;
118 			z = rhs.z;
119 			w = rhs.w;
120 
121 			return *this;
122 		}
123 
operator =(const Vector4f & rhs)124 		Register &operator=(const Vector4f &rhs)
125 		{
126 			x = rhs.x;
127 			y = rhs.y;
128 			z = rhs.z;
129 			w = rhs.w;
130 
131 			return *this;
132 		}
133 
operator Vector4f()134 		operator Vector4f()
135 		{
136 			Vector4f v;
137 
138 			v.x = x;
139 			v.y = y;
140 			v.z = z;
141 			v.w = w;
142 
143 			return v;
144 		}
145 
146 		Reference<Float4> x;
147 		Reference<Float4> y;
148 		Reference<Float4> z;
149 		Reference<Float4> w;
150 	};
151 
152 	class RegisterFile
153 	{
154 	public:
RegisterFile(int size,bool indirectAddressable)155 		RegisterFile(int size, bool indirectAddressable) : size(size), indirectAddressable(indirectAddressable)
156 		{
157 			if(indirectAddressable)
158 			{
159 				x = new Array<Float4>(size);
160 				y = new Array<Float4>(size);
161 				z = new Array<Float4>(size);
162 				w = new Array<Float4>(size);
163 			}
164 			else
165 			{
166 				x = new Array<Float4>[size];
167 				y = new Array<Float4>[size];
168 				z = new Array<Float4>[size];
169 				w = new Array<Float4>[size];
170 			}
171 		}
172 
~RegisterFile()173 		~RegisterFile()
174 		{
175 			if(indirectAddressable)
176 			{
177 				delete x;
178 				delete y;
179 				delete z;
180 				delete w;
181 			}
182 			else
183 			{
184 				delete[] x;
185 				delete[] y;
186 				delete[] z;
187 				delete[] w;
188 			}
189 		}
190 
operator [](int i)191 		Register operator[](int i)
192 		{
193 			if(indirectAddressable)
194 			{
195 				return Register(x[0][i], y[0][i], z[0][i], w[0][i]);
196 			}
197 			else
198 			{
199 				return Register(x[i][0], y[i][0], z[i][0], w[i][0]);
200 			}
201 		}
202 
operator [](RValue<Int> i)203 		Register operator[](RValue<Int> i)
204 		{
205 			ASSERT(indirectAddressable);
206 
207 			return Register(x[0][i], y[0][i], z[0][i], w[0][i]);
208 		}
209 
210 		const Vector4f operator[](RValue<Int4> i);   // Gather operation (read only).
211 
212 		void scatter_x(Int4 i, RValue<Float4> r);
213 		void scatter_y(Int4 i, RValue<Float4> r);
214 		void scatter_z(Int4 i, RValue<Float4> r);
215 		void scatter_w(Int4 i, RValue<Float4> r);
216 
217 	protected:
218 		const int size;
219 		const bool indirectAddressable;
220 		Array<Float4> *x;
221 		Array<Float4> *y;
222 		Array<Float4> *z;
223 		Array<Float4> *w;
224 	};
225 
226 	template<int S, bool I = false>
227 	class RegisterArray : public RegisterFile
228 	{
229 	public:
RegisterArray(bool indirectAddressable=I)230 		RegisterArray(bool indirectAddressable = I) : RegisterFile(S, indirectAddressable)
231 		{
232 		}
233 	};
234 
235 	class ShaderCore
236 	{
237 		typedef Shader::Control Control;
238 
239 	public:
240 		void mov(Vector4f &dst, const Vector4f &src, bool integerDestination = false);
241 		void neg(Vector4f &dst, const Vector4f &src);
242 		void ineg(Vector4f &dst, const Vector4f &src);
243 		void f2b(Vector4f &dst, const Vector4f &src);
244 		void b2f(Vector4f &dst, const Vector4f &src);
245 		void f2i(Vector4f &dst, const Vector4f &src);
246 		void i2f(Vector4f &dst, const Vector4f &src);
247 		void f2u(Vector4f &dst, const Vector4f &src);
248 		void u2f(Vector4f &dst, const Vector4f &src);
249 		void i2b(Vector4f &dst, const Vector4f &src);
250 		void b2i(Vector4f &dst, const Vector4f &src);
251 		void add(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
252 		void iadd(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
253 		void sub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
254 		void isub(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
255 		void mad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
256 		void imad(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
257 		void mul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
258 		void imul(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
259 		void rcpx(Vector4f &dst, const Vector4f &src, bool pp = false);
260 		void div(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
261 		void idiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
262 		void udiv(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
263 		void mod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
264 		void imod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
265 		void umod(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
266 		void shl(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
267 		void ishr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
268 		void ushr(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
269 		void rsqx(Vector4f &dst, const Vector4f &src, bool pp = false);
270 		void sqrt(Vector4f &dst, const Vector4f &src, bool pp = false);
271 		void rsq(Vector4f &dst, const Vector4f &src, bool pp = false);
272 		void len2(Float4 &dst, const Vector4f &src, bool pp = false);
273 		void len3(Float4 &dst, const Vector4f &src, bool pp = false);
274 		void len4(Float4 &dst, const Vector4f &src, bool pp = false);
275 		void dist1(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp = false);
276 		void dist2(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp = false);
277 		void dist3(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp = false);
278 		void dist4(Float4 &dst, const Vector4f &src0, const Vector4f &src1, bool pp = false);
279 		void dp1(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
280 		void dp2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
281 		void dp2add(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
282 		void dp3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
283 		void dp4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
284 		void det2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
285 		void det3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
286 		void det4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2, const Vector4f &src3);
287 		void min(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
288 		void imin(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
289 		void umin(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
290 		void max(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
291 		void imax(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
292 		void umax(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
293 		void slt(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
294 		void step(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
295 		void exp2x(Vector4f &dst, const Vector4f &src, bool pp = false);
296 		void exp2(Vector4f &dst, const Vector4f &src, bool pp = false);
297 		void exp(Vector4f &dst, const Vector4f &src, bool pp = false);
298 		void log2x(Vector4f &dst, const Vector4f &src, bool pp = false);
299 		void log2(Vector4f &dst, const Vector4f &src, bool pp = false);
300 		void log(Vector4f &dst, const Vector4f &src, bool pp = false);
301 		void lit(Vector4f &dst, const Vector4f &src);
302 		void att(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
303 		void lrp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
304 		void isinf(Vector4f &dst, const Vector4f &src);
305 		void isnan(Vector4f &dst, const Vector4f &src);
306 		void smooth(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
307 		void packHalf2x16(Vector4f &dst, const Vector4f &src);
308 		void unpackHalf2x16(Vector4f &dst, const Vector4f &src);
309 		void packSnorm2x16(Vector4f &dst, const Vector4f &src);
310 		void packUnorm2x16(Vector4f &dst, const Vector4f &src);
311 		void unpackSnorm2x16(Vector4f &dst, const Vector4f &src);
312 		void unpackUnorm2x16(Vector4f &dst, const Vector4f &src);
313 		void frc(Vector4f &dst, const Vector4f &src);
314 		void trunc(Vector4f &dst, const Vector4f &src);
315 		void floor(Vector4f &dst, const Vector4f &src);
316 		void round(Vector4f &dst, const Vector4f &src);
317 		void roundEven(Vector4f &dst, const Vector4f &src);
318 		void ceil(Vector4f &dst, const Vector4f &src);
319 		void powx(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp = false);
320 		void pow(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp = false);
321 		void crs(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
322 		void forward1(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
323 		void forward2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
324 		void forward3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
325 		void forward4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
326 		void reflect1(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
327 		void reflect2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
328 		void reflect3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
329 		void reflect4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
330 		void refract1(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Float4 &src2);
331 		void refract2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Float4 &src2);
332 		void refract3(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Float4 &src2);
333 		void refract4(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Float4 &src2);
334 		void sgn(Vector4f &dst, const Vector4f &src);
335 		void isgn(Vector4f &dst, const Vector4f &src);
336 		void abs(Vector4f &dst, const Vector4f &src);
337 		void iabs(Vector4f &dst, const Vector4f &src);
338 		void nrm2(Vector4f &dst, const Vector4f &src, bool pp = false);
339 		void nrm3(Vector4f &dst, const Vector4f &src, bool pp = false);
340 		void nrm4(Vector4f &dst, const Vector4f &src, bool pp = false);
341 		void sincos(Vector4f &dst, const Vector4f &src, bool pp = false);
342 		void cos(Vector4f &dst, const Vector4f &src, bool pp = false);
343 		void sin(Vector4f &dst, const Vector4f &src, bool pp = false);
344 		void tan(Vector4f &dst, const Vector4f &src, bool pp = false);
345 		void acos(Vector4f &dst, const Vector4f &src, bool pp = false);
346 		void asin(Vector4f &dst, const Vector4f &src, bool pp = false);
347 		void atan(Vector4f &dst, const Vector4f &src, bool pp = false);
348 		void atan2(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, bool pp = false);
349 		void cosh(Vector4f &dst, const Vector4f &src, bool pp = false);
350 		void sinh(Vector4f &dst, const Vector4f &src, bool pp = false);
351 		void tanh(Vector4f &dst, const Vector4f &src, bool pp = false);
352 		void acosh(Vector4f &dst, const Vector4f &src, bool pp = false);
353 		void asinh(Vector4f &dst, const Vector4f &src, bool pp = false);
354 		void atanh(Vector4f &dst, const Vector4f &src, bool pp = false);
355 		void expp(Vector4f &dst, const Vector4f &src, unsigned short shaderModel);
356 		void logp(Vector4f &dst, const Vector4f &src, unsigned short shaderModel);
357 		void cmp0(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
358 		void cmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control);
359 		void icmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control);
360 		void ucmp(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, Control control);
361 		void select(Vector4f &dst, const Vector4f &src0, const Vector4f &src1, const Vector4f &src2);
362 		void extract(Float4 &dst, const Vector4f &src0, const Float4 &src1);
363 		void insert(Vector4f &dst, const Vector4f &src, const Float4 &element, const Float4 &index);
364 		void all(Float4 &dst, const Vector4f &src);
365 		void any(Float4 &dst, const Vector4f &src);
366 		void bitwise_not(Vector4f &dst, const Vector4f &src);
367 		void bitwise_or(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
368 		void bitwise_xor(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
369 		void bitwise_and(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
370 		void equal(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
371 		void notEqual(Vector4f &dst, const Vector4f &src0, const Vector4f &src1);
372 
373 	private:
374 		void sgn(Float4 &dst, const Float4 &src);
375 		void isgn(Float4 &dst, const Float4 &src);
376 		void cmp0(Float4 &dst, const Float4 &src0, const Float4 &src1, const Float4 &src2);
377 		void cmp0i(Float4 &dst, const Float4 &src0, const Float4 &src1, const Float4 &src2);
378 		void select(Float4 &dst, RValue<Int4> src0, const Float4 &src1, const Float4 &src2);
379 		void floatToHalfBits(Float4& dst, const Float4& floatBits, bool storeInUpperBits);
380 		void halfToFloatBits(Float4& dst, const Float4& halfBits);
381 	};
382 }
383 
384 #endif   // sw_ShaderCore_hpp
385