Line data Source code
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>
13 0 : inline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) {
14 0 : if (expr.isZeros()) {
15 0 : return Self(0);
16 : }
17 0 : if (expr.isOnes()) {
18 0 : return Self(1);
19 : }
20 0 : return Self(Self::CastStr(), expr.c_str());
21 : }
22 :
23 : template<typename Self>
24 : template<typename T0, typename T1>
25 0 : inline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) {
26 0 : if (in0.isZeros() || in1.isZeros()) {
27 0 : return Self(0);
28 : }
29 0 : if (in0.isOnes()) {
30 0 : return Self::VectorCast(in1);
31 : }
32 0 : if (in1.isOnes()) {
33 0 : return Self::VectorCast(in0);
34 : }
35 0 : return Self("(%s * %s)", in0.c_str(), in1.c_str());
36 : }
37 :
38 : template<typename Self>
39 : template<typename T0, typename T1>
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>
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>
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 :
80 : inline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) {
81 : return expr;
82 : }
83 :
84 0 : inline const char* GrGLSLExpr1::ZerosStr() {
85 0 : return "0";
86 : }
87 :
88 0 : inline const char* GrGLSLExpr1::OnesStr() {
89 0 : 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 :
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 :
111 0 : inline const char* GrGLSLExpr4::ZerosStr() {
112 0 : return "vec4(0)";
113 : }
114 :
115 0 : inline const char* GrGLSLExpr4::OnesStr() {
116 0 : return "vec4(1)";
117 : }
118 :
119 0 : inline const char* GrGLSLExpr4::CastStr() {
120 0 : return "vec4(%s)";
121 : }
122 :
123 0 : inline const char* GrGLSLExpr4::CastIntStr() {
124 0 : return "vec4(%d)";
125 : }
126 :
127 0 : inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) {
128 0 : return INHERITED::VectorCastImpl(expr);
129 : }
130 :
131 0 : inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) {
132 0 : return expr;
133 : }
134 :
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 0 : inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
152 0 : 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 0 : inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
164 0 : 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
|