1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef GrGLSL_impl_DEFINED
9 #define GrGLSL_impl_DEFINED
10
11 template<typename Self>
12 template<typename T>
VectorCastImpl(const T & expr)13 inline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) {
14 if (expr.isZeros()) {
15 return Self(0);
16 }
17 if (expr.isOnes()) {
18 return Self(1);
19 }
20 return Self(Self::CastStr(), expr.c_str());
21 }
22
23 template<typename Self>
24 template<typename T0, typename T1>
Mul(T0 in0,T1 in1)25 inline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) {
26 if (in0.isZeros() || in1.isZeros()) {
27 return Self(0);
28 }
29 if (in0.isOnes()) {
30 return Self::VectorCast(in1);
31 }
32 if (in1.isOnes()) {
33 return Self::VectorCast(in0);
34 }
35 return Self("(%s * %s)", in0.c_str(), in1.c_str());
36 }
37
38 template<typename Self>
39 template<typename T0, typename T1>
Add(T0 in0,T1 in1)40 inline Self GrGLSLExpr<Self>::Add(T0 in0, T1 in1) {
41 if (in1.isZeros()) {
42 return Self::VectorCast(in0);
43 }
44 if (in0.isZeros()) {
45 return Self::VectorCast(in1);
46 }
47 if (in0.isOnes() && in1.isOnes()) {
48 return Self(2);
49 }
50 return Self("(%s + %s)", in0.c_str(), in1.c_str());
51 }
52
53 template<typename Self>
54 template<typename T0, typename T1>
Sub(T0 in0,T1 in1)55 inline Self GrGLSLExpr<Self>::Sub(T0 in0, T1 in1) {
56 if (in1.isZeros()) {
57 return Self::VectorCast(in0);
58 }
59 if (in1.isOnes()) {
60 if (in0.isOnes()) {
61 return Self(0);
62 }
63 }
64
65 return Self("(%s - %s)", in0.c_str(), in1.c_str());
66 }
67
68 template <typename Self>
69 template <typename T>
extractComponents(const char format[])70 T GrGLSLExpr<Self>::extractComponents(const char format[]) const {
71 if (this->isZeros()) {
72 return T(0);
73 }
74 if (this->isOnes()) {
75 return T(1);
76 }
77 return T(format, this->c_str());
78 }
79
VectorCast(const GrGLSLExpr1 & expr)80 inline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) {
81 return expr;
82 }
83
ZerosStr()84 inline const char* GrGLSLExpr1::ZerosStr() {
85 return "0";
86 }
87
OnesStr()88 inline const char* GrGLSLExpr1::OnesStr() {
89 return "1.0";
90 }
91
92 // GrGLSLExpr1::CastStr() is unimplemented because using them is likely an
93 // error. This is now caught compile-time.
94
CastIntStr()95 inline const char* GrGLSLExpr1::CastIntStr() {
96 return "%d";
97 }
98
99 inline GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
100 return GrGLSLExpr1::Mul(in0, in1);
101 }
102
103 inline GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
104 return GrGLSLExpr1::Add(in0, in1);
105 }
106
107 inline GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
108 return GrGLSLExpr1::Sub(in0, in1);
109 }
110
ZerosStr()111 inline const char* GrGLSLExpr4::ZerosStr() {
112 return "vec4(0)";
113 }
114
OnesStr()115 inline const char* GrGLSLExpr4::OnesStr() {
116 return "vec4(1)";
117 }
118
CastStr()119 inline const char* GrGLSLExpr4::CastStr() {
120 return "vec4(%s)";
121 }
122
CastIntStr()123 inline const char* GrGLSLExpr4::CastIntStr() {
124 return "vec4(%d)";
125 }
126
VectorCast(const GrGLSLExpr1 & expr)127 inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) {
128 return INHERITED::VectorCastImpl(expr);
129 }
130
VectorCast(const GrGLSLExpr4 & expr)131 inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) {
132 return expr;
133 }
134
a()135 inline GrGLSLExpr4::AExpr GrGLSLExpr4::a() const {
136 return this->extractComponents<GrGLSLExpr4::AExpr>("%s.a");
137 }
138
139 inline GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
140 return GrGLSLExpr4::Mul(in0, in1);
141 }
142
143 inline GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
144 return GrGLSLExpr4::Add(in0, in1);
145 }
146
147 inline GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
148 return GrGLSLExpr4::Sub(in0, in1);
149 }
150
151 inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
152 return GrGLSLExpr4::Mul(in0, in1);
153 }
154
155 inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
156 return GrGLSLExpr4::Add(in0, in1);
157 }
158
159 inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
160 return GrGLSLExpr4::Sub(in0, in1);
161 }
162
163 inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
164 return GrGLSLExpr4::Mul(in0, in1);
165 }
166
167 inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
168 return GrGLSLExpr4::Add(in0, in1);
169 }
170
171 inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
172 return GrGLSLExpr4::Sub(in0, in1);
173 }
174
175 #endif
176