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