1018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com/*
2018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com * Copyright 2013 Google Inc.
3018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com *
4018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com * Use of this source code is governed by a BSD-style license that can be
5018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com * found in the LICENSE file.
6018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com */
7018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com
8018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com#ifndef GrGLSL_impl_DEFINED
9018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com#define GrGLSL_impl_DEFINED
10018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com
11a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename Self>
12a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename T>
13a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) {
14a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (expr.isZeros()) {
15a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self(0);
16a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
17a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (expr.isOnes()) {
18a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self(1);
19a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
20a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return Self(Self::CastStr(), expr.c_str());
21824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
22824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
23a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename Self>
24a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename T0, typename T1>
25a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) {
26a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in0.isZeros() || in1.isZeros()) {
27a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self(0);
28a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
29a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in0.isOnes()) {
30a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self::VectorCast(in1);
31a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
32a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in1.isOnes()) {
33a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self::VectorCast(in0);
34a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
35a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return Self("(%s * %s)", in0.c_str(), in1.c_str());
36824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
37824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
38a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename Self>
39a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename T0, typename T1>
40a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline Self GrGLSLExpr<Self>::Add(T0 in0, T1 in1) {
41a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in1.isZeros()) {
42a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self::VectorCast(in0);
43a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
44a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in0.isZeros()) {
45a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self::VectorCast(in1);
46a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
47a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in0.isOnes() && in1.isOnes()) {
48a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self(2);
49a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
50a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return Self("(%s + %s)", in0.c_str(), in1.c_str());
51824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
52824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
53a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename Self>
54a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate<typename T0, typename T1>
55a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline Self GrGLSLExpr<Self>::Sub(T0 in0, T1 in1) {
56a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in1.isZeros()) {
57a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return Self::VectorCast(in0);
58a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
59a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (in1.isOnes()) {
60a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        if (in0.isOnes()) {
61a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org            return Self(0);
62a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        }
63a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
64a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
65a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return Self("(%s - %s)", in0.c_str(), in1.c_str());
66824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
67a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
68a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate <typename Self>
69a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgtemplate <typename T>
70a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orgT GrGLSLExpr<Self>::extractComponents(const char format[]) const {
71a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (this->isZeros()) {
72a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return T(0);
73a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
74a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    if (this->isOnes()) {
75a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org        return T(1);
76a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    }
77a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return T(format, this->c_str());
78a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
79a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
80a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) {
81a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return expr;
82824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
83824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
84a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline const char* GrGLSLExpr1::ZerosStr() {
85824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org    return "0";
86824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
87824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
88a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline const char* GrGLSLExpr1::OnesStr() {
89a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return "1.0";
90824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
91824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
92a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org// GrGLSLExpr1::CastStr() is unimplemented because using them is likely an
93a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org// error. This is now caught compile-time.
94824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
95a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline const char* GrGLSLExpr1::CastIntStr() {
96824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org    return "%d";
97824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
98824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
99a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
100a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr1::Mul(in0, in1);
101a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
102a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
103a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
104a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr1::Add(in0, in1);
105a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
106a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
107a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) {
108a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr1::Sub(in0, in1);
109824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
110824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
111a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline const char* GrGLSLExpr4::ZerosStr() {
112a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return "vec4(0)";
113a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
114a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
115a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline const char* GrGLSLExpr4::OnesStr() {
116a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return "vec4(1)";
117a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
118a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
119a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline const char* GrGLSLExpr4::CastStr() {
120a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return "vec4(%s)";
121a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
122a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
123a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline const char* GrGLSLExpr4::CastIntStr() {
124a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return "vec4(%d)";
125a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
126a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
127a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) {
128a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return INHERITED::VectorCastImpl(expr);
129a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
130a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
131a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) {
132824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org    return expr;
133824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
134018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com
135a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4::AExpr GrGLSLExpr4::a() const {
136a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return this->extractComponents<GrGLSLExpr4::AExpr>("%s.a");
137018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com}
138018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com
139a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
140a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Mul(in0, in1);
141018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com}
142018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com
143a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
144a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Add(in0, in1);
145824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
146824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
147a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) {
148a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Sub(in0, in1);
149a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
150824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
151a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
152a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Mul(in0, in1);
153824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
154824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
155a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
156a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Add(in0, in1);
157824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org}
158824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org
159a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) {
160a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Sub(in0, in1);
161a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
162a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
163a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
164a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Mul(in0, in1);
165a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
166a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
167a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
168a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Add(in0, in1);
169a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org}
170a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org
171a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.orginline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) {
172a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org    return GrGLSLExpr4::Sub(in0, in1);
173018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com}
174018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com
175018f179efb2413431bdb1a9e6701eb44ef36b792bsalomon@google.com#endif
176