bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 11 | template<typename Self> |
| 12 | template<typename T> |
| 13 | inline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 14 | if (expr.isZeros()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 15 | return Self(0); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 16 | } |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 17 | if (expr.isOnes()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 18 | return Self(1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 19 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 20 | return Self(Self::CastStr(), expr.c_str()); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 21 | } |
| 22 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 23 | template<typename Self> |
| 24 | template<typename T0, typename T1> |
| 25 | inline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 26 | if (in0.isZeros() || in1.isZeros()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 27 | return Self(0); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 28 | } |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 29 | if (in0.isOnes()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 30 | return Self::VectorCast(in1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 31 | } |
| 32 | if (in1.isOnes()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 33 | return Self::VectorCast(in0); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 34 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 35 | return Self("(%s * %s)", in0.c_str(), in1.c_str()); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 36 | } |
| 37 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 38 | template<typename Self> |
| 39 | template<typename T0, typename T1> |
| 40 | inline Self GrGLSLExpr<Self>::Add(T0 in0, T1 in1) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 41 | if (in1.isZeros()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 42 | return Self::VectorCast(in0); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 43 | } |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 44 | if (in0.isZeros()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 45 | return Self::VectorCast(in1); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 46 | } |
| 47 | if (in0.isOnes() && in1.isOnes()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 48 | return Self(2); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 49 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 50 | return Self("(%s + %s)", in0.c_str(), in1.c_str()); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 51 | } |
| 52 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 53 | template<typename Self> |
| 54 | template<typename T0, typename T1> |
| 55 | inline Self GrGLSLExpr<Self>::Sub(T0 in0, T1 in1) { |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 56 | if (in1.isZeros()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 57 | return Self::VectorCast(in0); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 58 | } |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 59 | if (in1.isOnes()) { |
| 60 | if (in0.isOnes()) { |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 61 | return Self(0); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 65 | return Self("(%s - %s)", in0.c_str(), in1.c_str()); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 66 | } |
| 67 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 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()); |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 78 | } |
| 79 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 80 | inline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) { |
| 81 | return expr; |
| 82 | } |
| 83 | |
| 84 | inline const char* GrGLSLExpr1::ZerosStr() { |
| 85 | return "0"; |
| 86 | } |
| 87 | |
| 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 | |
| 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 | inline const char* GrGLSLExpr4::ZerosStr() { |
| 112 | return "vec4(0)"; |
| 113 | } |
| 114 | |
| 115 | inline const char* GrGLSLExpr4::OnesStr() { |
| 116 | return "vec4(1)"; |
| 117 | } |
| 118 | |
| 119 | inline const char* GrGLSLExpr4::CastStr() { |
| 120 | return "vec4(%s)"; |
| 121 | } |
| 122 | |
| 123 | inline const char* GrGLSLExpr4::CastIntStr() { |
| 124 | return "vec4(%d)"; |
| 125 | } |
| 126 | |
| 127 | inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) { |
| 128 | return INHERITED::VectorCastImpl(expr); |
| 129 | } |
| 130 | |
| 131 | inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) { |
| 132 | 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 | 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); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 173 | } |
| 174 | |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 175 | #endif |