1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/gl/GrGLSL_impl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,175 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrGLSL_impl_DEFINED 1.12 +#define GrGLSL_impl_DEFINED 1.13 + 1.14 +template<typename Self> 1.15 +template<typename T> 1.16 +inline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) { 1.17 + if (expr.isZeros()) { 1.18 + return Self(0); 1.19 + } 1.20 + if (expr.isOnes()) { 1.21 + return Self(1); 1.22 + } 1.23 + return Self(Self::CastStr(), expr.c_str()); 1.24 +} 1.25 + 1.26 +template<typename Self> 1.27 +template<typename T0, typename T1> 1.28 +inline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) { 1.29 + if (in0.isZeros() || in1.isZeros()) { 1.30 + return Self(0); 1.31 + } 1.32 + if (in0.isOnes()) { 1.33 + return Self::VectorCast(in1); 1.34 + } 1.35 + if (in1.isOnes()) { 1.36 + return Self::VectorCast(in0); 1.37 + } 1.38 + return Self("(%s * %s)", in0.c_str(), in1.c_str()); 1.39 +} 1.40 + 1.41 +template<typename Self> 1.42 +template<typename T0, typename T1> 1.43 +inline Self GrGLSLExpr<Self>::Add(T0 in0, T1 in1) { 1.44 + if (in1.isZeros()) { 1.45 + return Self::VectorCast(in0); 1.46 + } 1.47 + if (in0.isZeros()) { 1.48 + return Self::VectorCast(in1); 1.49 + } 1.50 + if (in0.isOnes() && in1.isOnes()) { 1.51 + return Self(2); 1.52 + } 1.53 + return Self("(%s + %s)", in0.c_str(), in1.c_str()); 1.54 +} 1.55 + 1.56 +template<typename Self> 1.57 +template<typename T0, typename T1> 1.58 +inline Self GrGLSLExpr<Self>::Sub(T0 in0, T1 in1) { 1.59 + if (in1.isZeros()) { 1.60 + return Self::VectorCast(in0); 1.61 + } 1.62 + if (in1.isOnes()) { 1.63 + if (in0.isOnes()) { 1.64 + return Self(0); 1.65 + } 1.66 + } 1.67 + 1.68 + return Self("(%s - %s)", in0.c_str(), in1.c_str()); 1.69 +} 1.70 + 1.71 +template <typename Self> 1.72 +template <typename T> 1.73 +T GrGLSLExpr<Self>::extractComponents(const char format[]) const { 1.74 + if (this->isZeros()) { 1.75 + return T(0); 1.76 + } 1.77 + if (this->isOnes()) { 1.78 + return T(1); 1.79 + } 1.80 + return T(format, this->c_str()); 1.81 +} 1.82 + 1.83 +inline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) { 1.84 + return expr; 1.85 +} 1.86 + 1.87 +inline const char* GrGLSLExpr1::ZerosStr() { 1.88 + return "0"; 1.89 +} 1.90 + 1.91 +inline const char* GrGLSLExpr1::OnesStr() { 1.92 + return "1.0"; 1.93 +} 1.94 + 1.95 +// GrGLSLExpr1::CastStr() is unimplemented because using them is likely an 1.96 +// error. This is now caught compile-time. 1.97 + 1.98 +inline const char* GrGLSLExpr1::CastIntStr() { 1.99 + return "%d"; 1.100 +} 1.101 + 1.102 +inline GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { 1.103 + return GrGLSLExpr1::Mul(in0, in1); 1.104 +} 1.105 + 1.106 +inline GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { 1.107 + return GrGLSLExpr1::Add(in0, in1); 1.108 +} 1.109 + 1.110 +inline GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { 1.111 + return GrGLSLExpr1::Sub(in0, in1); 1.112 +} 1.113 + 1.114 +inline const char* GrGLSLExpr4::ZerosStr() { 1.115 + return "vec4(0)"; 1.116 +} 1.117 + 1.118 +inline const char* GrGLSLExpr4::OnesStr() { 1.119 + return "vec4(1)"; 1.120 +} 1.121 + 1.122 +inline const char* GrGLSLExpr4::CastStr() { 1.123 + return "vec4(%s)"; 1.124 +} 1.125 + 1.126 +inline const char* GrGLSLExpr4::CastIntStr() { 1.127 + return "vec4(%d)"; 1.128 +} 1.129 + 1.130 +inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) { 1.131 + return INHERITED::VectorCastImpl(expr); 1.132 +} 1.133 + 1.134 +inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) { 1.135 + return expr; 1.136 +} 1.137 + 1.138 +inline GrGLSLExpr4::AExpr GrGLSLExpr4::a() const { 1.139 + return this->extractComponents<GrGLSLExpr4::AExpr>("%s.a"); 1.140 +} 1.141 + 1.142 +inline GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { 1.143 + return GrGLSLExpr4::Mul(in0, in1); 1.144 +} 1.145 + 1.146 +inline GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { 1.147 + return GrGLSLExpr4::Add(in0, in1); 1.148 +} 1.149 + 1.150 +inline GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { 1.151 + return GrGLSLExpr4::Sub(in0, in1); 1.152 +} 1.153 + 1.154 +inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { 1.155 + return GrGLSLExpr4::Mul(in0, in1); 1.156 +} 1.157 + 1.158 +inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { 1.159 + return GrGLSLExpr4::Add(in0, in1); 1.160 +} 1.161 + 1.162 +inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { 1.163 + return GrGLSLExpr4::Sub(in0, in1); 1.164 +} 1.165 + 1.166 +inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { 1.167 + return GrGLSLExpr4::Mul(in0, in1); 1.168 +} 1.169 + 1.170 +inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { 1.171 + return GrGLSLExpr4::Add(in0, in1); 1.172 +} 1.173 + 1.174 +inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { 1.175 + return GrGLSLExpr4::Sub(in0, in1); 1.176 +} 1.177 + 1.178 +#endif