blob: bdd69cc76b37fc47d9747a1427d5043162ba36cf [file] [log] [blame]
bsalomon@google.com018f1792013-04-18 19:36:09 +00001/*
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
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000011template<typename Self>
12template<typename T>
13inline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000014 if (expr.isZeros()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000015 return Self(0);
bsalomon@google.com018f1792013-04-18 19:36:09 +000016 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000017 if (expr.isOnes()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000018 return Self(1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000019 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000020 return Self(Self::CastStr(), expr.c_str());
bsalomon@google.com018f1792013-04-18 19:36:09 +000021}
22
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000023template<typename Self>
24template<typename T0, typename T1>
25inline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000026 if (in0.isZeros() || in1.isZeros()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000027 return Self(0);
bsalomon@google.com018f1792013-04-18 19:36:09 +000028 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000029 if (in0.isOnes()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000030 return Self::VectorCast(in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000031 }
32 if (in1.isOnes()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000033 return Self::VectorCast(in0);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000034 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000035 return Self("(%s * %s)", in0.c_str(), in1.c_str());
bsalomon@google.com018f1792013-04-18 19:36:09 +000036}
37
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000038template<typename Self>
39template<typename T0, typename T1>
40inline Self GrGLSLExpr<Self>::Add(T0 in0, T1 in1) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000041 if (in1.isZeros()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000042 return Self::VectorCast(in0);
bsalomon@google.com018f1792013-04-18 19:36:09 +000043 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000044 if (in0.isZeros()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000045 return Self::VectorCast(in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000046 }
47 if (in0.isOnes() && in1.isOnes()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000048 return Self(2);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000049 }
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000050 return Self("(%s + %s)", in0.c_str(), in1.c_str());
bsalomon@google.com018f1792013-04-18 19:36:09 +000051}
52
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000053template<typename Self>
54template<typename T0, typename T1>
55inline Self GrGLSLExpr<Self>::Sub(T0 in0, T1 in1) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000056 if (in1.isZeros()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000057 return Self::VectorCast(in0);
bsalomon@google.com018f1792013-04-18 19:36:09 +000058 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000059 if (in1.isOnes()) {
60 if (in0.isOnes()) {
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000061 return Self(0);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000062 }
63 }
64
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000065 return Self("(%s - %s)", in0.c_str(), in1.c_str());
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000066}
67
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000068template <typename Self>
69template <typename T>
70T 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());
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000078}
79
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000080inline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) {
81 return expr;
82}
83
84inline const char* GrGLSLExpr1::ZerosStr() {
85 return "0";
86}
87
88inline 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
95inline const char* GrGLSLExpr1::CastIntStr() {
96 return "%d";
97}
98
99inline GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
100 return GrGLSLExpr1::Mul(in0, in1);
101}
102
103inline GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
104 return GrGLSLExpr1::Add(in0, in1);
105}
106
107inline GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
108 return GrGLSLExpr1::Sub(in0, in1);
109}
110
111inline const char* GrGLSLExpr4::ZerosStr() {
112 return "vec4(0)";
113}
114
115inline const char* GrGLSLExpr4::OnesStr() {
116 return "vec4(1)";
117}
118
119inline const char* GrGLSLExpr4::CastStr() {
120 return "vec4(%s)";
121}
122
123inline const char* GrGLSLExpr4::CastIntStr() {
124 return "vec4(%d)";
125}
126
127inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) {
128 return INHERITED::VectorCastImpl(expr);
129}
130
131inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) {
132 return expr;
133}
134
135inline GrGLSLExpr4::AExpr GrGLSLExpr4::a() const {
136 return this->extractComponents<GrGLSLExpr4::AExpr>("%s.a");
137}
138
139inline GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
140 return GrGLSLExpr4::Mul(in0, in1);
141}
142
143inline GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
144 return GrGLSLExpr4::Add(in0, in1);
145}
146
147inline GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
148 return GrGLSLExpr4::Sub(in0, in1);
149}
150
151inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
152 return GrGLSLExpr4::Mul(in0, in1);
153}
154
155inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
156 return GrGLSLExpr4::Add(in0, in1);
157}
158
159inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
160 return GrGLSLExpr4::Sub(in0, in1);
161}
162
163inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
164 return GrGLSLExpr4::Mul(in0, in1);
165}
166
167inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
168 return GrGLSLExpr4::Add(in0, in1);
169}
170
171inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
172 return GrGLSLExpr4::Sub(in0, in1);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000173}
174
bsalomon@google.com018f1792013-04-18 19:36:09 +0000175#endif