1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/gl/GrGLSL.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,320 @@ 1.4 +/* 1.5 + * Copyright 2011 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_DEFINED 1.12 +#define GrGLSL_DEFINED 1.13 + 1.14 +#include "gl/GrGLInterface.h" 1.15 +#include "GrColor.h" 1.16 +#include "GrTypesPriv.h" 1.17 +#include "SkString.h" 1.18 + 1.19 +class GrGLContextInfo; 1.20 +class GrGLShaderVar; 1.21 + 1.22 +// Limited set of GLSL versions we build shaders for. Caller should round 1.23 +// down the GLSL version to one of these enums. 1.24 +enum GrGLSLGeneration { 1.25 + /** 1.26 + * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) 1.27 + */ 1.28 + k110_GrGLSLGeneration, 1.29 + /** 1.30 + * Desktop GLSL 1.30 1.31 + */ 1.32 + k130_GrGLSLGeneration, 1.33 + /** 1.34 + * Desktop GLSL 1.40 1.35 + */ 1.36 + k140_GrGLSLGeneration, 1.37 + /** 1.38 + * Desktop GLSL 1.50 1.39 + */ 1.40 + k150_GrGLSLGeneration, 1.41 +}; 1.42 + 1.43 +/** 1.44 + * Gets the most recent GLSL Generation compatible with the OpenGL context. 1.45 + */ 1.46 +GrGLSLGeneration GrGetGLSLGeneration(const GrGLInterface* gl); 1.47 + 1.48 +/** 1.49 + * Returns a string to include at the beginning of a shader to declare the GLSL 1.50 + * version. 1.51 + */ 1.52 +const char* GrGetGLSLVersionDecl(const GrGLContextInfo&); 1.53 + 1.54 +/** 1.55 + * Converts a GrSLType to a string containing the name of the equivalent GLSL type. 1.56 + */ 1.57 +static inline const char* GrGLSLTypeString(GrSLType t) { 1.58 + switch (t) { 1.59 + case kVoid_GrSLType: 1.60 + return "void"; 1.61 + case kFloat_GrSLType: 1.62 + return "float"; 1.63 + case kVec2f_GrSLType: 1.64 + return "vec2"; 1.65 + case kVec3f_GrSLType: 1.66 + return "vec3"; 1.67 + case kVec4f_GrSLType: 1.68 + return "vec4"; 1.69 + case kMat33f_GrSLType: 1.70 + return "mat3"; 1.71 + case kMat44f_GrSLType: 1.72 + return "mat4"; 1.73 + case kSampler2D_GrSLType: 1.74 + return "sampler2D"; 1.75 + default: 1.76 + GrCrash("Unknown shader var type."); 1.77 + return ""; // suppress warning 1.78 + } 1.79 +} 1.80 + 1.81 +/** A generic base-class representing a GLSL expression. 1.82 + * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant 1.83 + * folding with help of 1 and 0. 1.84 + * 1.85 + * Clients should not use this class, rather the specific instantiations defined 1.86 + * later, for example GrGLSLExpr4. 1.87 + */ 1.88 +template <typename Self> 1.89 +class GrGLSLExpr { 1.90 +public: 1.91 + bool isOnes() const { return kOnes_ExprType == fType; } 1.92 + bool isZeros() const { return kZeros_ExprType == fType; } 1.93 + 1.94 + const char* c_str() const { 1.95 + if (kZeros_ExprType == fType) { 1.96 + return Self::ZerosStr(); 1.97 + } else if (kOnes_ExprType == fType) { 1.98 + return Self::OnesStr(); 1.99 + } 1.100 + SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. 1.101 + return fExpr.c_str(); 1.102 + } 1.103 + 1.104 +protected: 1.105 + /** Constructs an invalid expression. 1.106 + * Useful only as a return value from functions that never actually return 1.107 + * this and instances that will be assigned to later. */ 1.108 + GrGLSLExpr() 1.109 + : fType(kFullExpr_ExprType) { 1.110 + // The only constructor that is allowed to build an empty expression. 1.111 + SkASSERT(!this->isValid()); 1.112 + } 1.113 + 1.114 + /** Constructs an expression with all components as value v */ 1.115 + explicit GrGLSLExpr(int v) { 1.116 + if (v == 0) { 1.117 + fType = kZeros_ExprType; 1.118 + } else if (v == 1) { 1.119 + fType = kOnes_ExprType; 1.120 + } else { 1.121 + fType = kFullExpr_ExprType; 1.122 + fExpr.appendf(Self::CastIntStr(), v); 1.123 + } 1.124 + } 1.125 + 1.126 + /** Constructs an expression from a string. 1.127 + * Argument expr is a simple expression or a parenthesized expression. */ 1.128 + // TODO: make explicit once effects input Exprs. 1.129 + GrGLSLExpr(const char expr[]) { 1.130 + if (NULL == expr) { // TODO: remove this once effects input Exprs. 1.131 + fType = kOnes_ExprType; 1.132 + } else { 1.133 + fType = kFullExpr_ExprType; 1.134 + fExpr = expr; 1.135 + } 1.136 + SkASSERT(this->isValid()); 1.137 + } 1.138 + 1.139 + /** Constructs an expression from a string. 1.140 + * Argument expr is a simple expression or a parenthesized expression. */ 1.141 + // TODO: make explicit once effects input Exprs. 1.142 + GrGLSLExpr(const SkString& expr) { 1.143 + if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. 1.144 + fType = kOnes_ExprType; 1.145 + } else { 1.146 + fType = kFullExpr_ExprType; 1.147 + fExpr = expr; 1.148 + } 1.149 + SkASSERT(this->isValid()); 1.150 + } 1.151 + 1.152 + /** Constructs an expression from a string with one substitution. */ 1.153 + GrGLSLExpr(const char format[], const char in0[]) 1.154 + : fType(kFullExpr_ExprType) { 1.155 + fExpr.appendf(format, in0); 1.156 + } 1.157 + 1.158 + /** Constructs an expression from a string with two substitutions. */ 1.159 + GrGLSLExpr(const char format[], const char in0[], const char in1[]) 1.160 + : fType(kFullExpr_ExprType) { 1.161 + fExpr.appendf(format, in0, in1); 1.162 + } 1.163 + 1.164 + bool isValid() const { 1.165 + return kFullExpr_ExprType != fType || !fExpr.isEmpty(); 1.166 + } 1.167 + 1.168 + /** Returns expression casted to another type. 1.169 + * Generic implementation that is called for non-trivial cases of casts. */ 1.170 + template <typename T> 1.171 + static Self VectorCastImpl(const T& other); 1.172 + 1.173 + /** Returns a GLSL multiplication: component-wise or component-by-scalar. 1.174 + * The multiplication will be component-wise or multiply each component by a scalar. 1.175 + * 1.176 + * The returned expression will compute the value of: 1.177 + * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise) 1.178 + * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar) 1.179 + * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector) 1.180 + */ 1.181 + template <typename T0, typename T1> 1.182 + static Self Mul(T0 in0, T1 in1); 1.183 + 1.184 + /** Returns a GLSL addition: component-wise or add a scalar to each component. 1.185 + * Return value computes: 1.186 + * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...). 1.187 + */ 1.188 + template <typename T0, typename T1> 1.189 + static Self Add(T0 in0, T1 in1); 1.190 + 1.191 + /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar. 1.192 + * Return value computes 1.193 + * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...). 1.194 + */ 1.195 + template <typename T0, typename T1> 1.196 + static Self Sub(T0 in0, T1 in1); 1.197 + 1.198 + /** Returns expression that accesses component(s) of the expression. 1.199 + * format should be the form "%s.x" where 'x' is the component(s) to access. 1.200 + * Caller is responsible for making sure the amount of components in the 1.201 + * format string is equal to dim(T). 1.202 + */ 1.203 + template <typename T> 1.204 + T extractComponents(const char format[]) const; 1.205 + 1.206 +private: 1.207 + enum ExprType { 1.208 + kZeros_ExprType, 1.209 + kOnes_ExprType, 1.210 + kFullExpr_ExprType, 1.211 + }; 1.212 + ExprType fType; 1.213 + SkString fExpr; 1.214 +}; 1.215 + 1.216 +class GrGLSLExpr1; 1.217 +class GrGLSLExpr4; 1.218 + 1.219 +/** Class representing a float GLSL expression. */ 1.220 +class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> { 1.221 +public: 1.222 + GrGLSLExpr1() 1.223 + : INHERITED() { 1.224 + } 1.225 + explicit GrGLSLExpr1(int v) 1.226 + : INHERITED(v) { 1.227 + } 1.228 + GrGLSLExpr1(const char* expr) 1.229 + : INHERITED(expr) { 1.230 + } 1.231 + GrGLSLExpr1(const SkString& expr) 1.232 + : INHERITED(expr) { 1.233 + } 1.234 + 1.235 + static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr); 1.236 + 1.237 +private: 1.238 + GrGLSLExpr1(const char format[], const char in0[]) 1.239 + : INHERITED(format, in0) { 1.240 + } 1.241 + GrGLSLExpr1(const char format[], const char in0[], const char in1[]) 1.242 + : INHERITED(format, in0, in1) { 1.243 + } 1.244 + 1.245 + static const char* ZerosStr(); 1.246 + static const char* OnesStr(); 1.247 + static const char* CastStr(); 1.248 + static const char* CastIntStr(); 1.249 + 1.250 + friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); 1.251 + friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); 1.252 + friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); 1.253 + 1.254 + friend class GrGLSLExpr<GrGLSLExpr1>; 1.255 + friend class GrGLSLExpr<GrGLSLExpr4>; 1.256 + 1.257 + typedef GrGLSLExpr<GrGLSLExpr1> INHERITED; 1.258 +}; 1.259 + 1.260 +/** Class representing a float vector (vec4) GLSL expression. */ 1.261 +class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> { 1.262 +public: 1.263 + GrGLSLExpr4() 1.264 + : INHERITED() { 1.265 + } 1.266 + explicit GrGLSLExpr4(int v) 1.267 + : INHERITED(v) { 1.268 + } 1.269 + GrGLSLExpr4(const char* expr) 1.270 + : INHERITED(expr) { 1.271 + } 1.272 + GrGLSLExpr4(const SkString& expr) 1.273 + : INHERITED(expr) { 1.274 + } 1.275 + 1.276 + typedef GrGLSLExpr1 AExpr; 1.277 + AExpr a() const; 1.278 + 1.279 + /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */ 1.280 + static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr); 1.281 + static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr); 1.282 + 1.283 +private: 1.284 + GrGLSLExpr4(const char format[], const char in0[]) 1.285 + : INHERITED(format, in0) { 1.286 + } 1.287 + GrGLSLExpr4(const char format[], const char in0[], const char in1[]) 1.288 + : INHERITED(format, in0, in1) { 1.289 + } 1.290 + 1.291 + static const char* ZerosStr(); 1.292 + static const char* OnesStr(); 1.293 + static const char* CastStr(); 1.294 + static const char* CastIntStr(); 1.295 + 1.296 + // The vector-by-scalar and scalar-by-vector binary operations. 1.297 + friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); 1.298 + friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); 1.299 + friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); 1.300 + friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); 1.301 + friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); 1.302 + friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); 1.303 + 1.304 + // The vector-by-vector, i.e. component-wise, binary operations. 1.305 + friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); 1.306 + friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); 1.307 + friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); 1.308 + 1.309 + friend class GrGLSLExpr<GrGLSLExpr4>; 1.310 + 1.311 + typedef GrGLSLExpr<GrGLSLExpr4> INHERITED; 1.312 +}; 1.313 + 1.314 +/** 1.315 + * Does an inplace mul, *=, of vec4VarName by mulFactor. 1.316 + * A semicolon and newline are added after the assignment. 1.317 + */ 1.318 +void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt, 1.319 + const char* vec4VarName, const GrGLSLExpr4& mulFactor); 1.320 + 1.321 +#include "GrGLSL_impl.h" 1.322 + 1.323 +#endif