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 "Reactor/Print.hpp"
19#include "Reactor/Reactor.hpp"
20#include "Vulkan/VkDebug.hpp"
21
22namespace 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 pp = false);
60 Float4 exponential(RValue<Float4> x, bool pp = false);
61 Float4 logarithm(RValue<Float4> x, 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 UInt4 halfToFloatBits(UInt4 halfBits);
96}
97
98#ifdef ENABLE_RR_PRINT
99namespace rr {
100 template <> struct PrintValue::Ty<sw::Vector4f>
101 {
102 static std::string fmt(const sw::Vector4f& v)
103 {
104 return "[x: " + PrintValue::fmt(v.x) + ","
105 " y: " + PrintValue::fmt(v.y) + ","
106 " z: " + PrintValue::fmt(v.z) + ","
107 " w: " + PrintValue::fmt(v.w) + "]";
108 }
109
110 static std::vector<rr::Value*> val(const sw::Vector4f& v)
111 {
112 return PrintValue::vals(v.x, v.y, v.z, v.w);
113 }
114 };
115 template <> struct PrintValue::Ty<sw::Vector4s>
116 {
117 static std::string fmt(const sw::Vector4s& v)
118 {
119 return "[x: " + PrintValue::fmt(v.x) + ","
120 " y: " + PrintValue::fmt(v.y) + ","
121 " z: " + PrintValue::fmt(v.z) + ","
122 " w: " + PrintValue::fmt(v.w) + "]";
123 }
124
125 static std::vector<rr::Value*> val(const sw::Vector4s& v)
126 {
127 return PrintValue::vals(v.x, v.y, v.z, v.w);
128 }
129 };
130}
131#endif // ENABLE_RR_PRINT
132
133#endif // sw_ShaderCore_hpp
134