michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrGLSL_DEFINED michael@0: #define GrGLSL_DEFINED michael@0: michael@0: #include "gl/GrGLInterface.h" michael@0: #include "GrColor.h" michael@0: #include "GrTypesPriv.h" michael@0: #include "SkString.h" michael@0: michael@0: class GrGLContextInfo; michael@0: class GrGLShaderVar; michael@0: michael@0: // Limited set of GLSL versions we build shaders for. Caller should round michael@0: // down the GLSL version to one of these enums. michael@0: enum GrGLSLGeneration { michael@0: /** michael@0: * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) michael@0: */ michael@0: k110_GrGLSLGeneration, michael@0: /** michael@0: * Desktop GLSL 1.30 michael@0: */ michael@0: k130_GrGLSLGeneration, michael@0: /** michael@0: * Desktop GLSL 1.40 michael@0: */ michael@0: k140_GrGLSLGeneration, michael@0: /** michael@0: * Desktop GLSL 1.50 michael@0: */ michael@0: k150_GrGLSLGeneration, michael@0: }; michael@0: michael@0: /** michael@0: * Gets the most recent GLSL Generation compatible with the OpenGL context. michael@0: */ michael@0: GrGLSLGeneration GrGetGLSLGeneration(const GrGLInterface* gl); michael@0: michael@0: /** michael@0: * Returns a string to include at the beginning of a shader to declare the GLSL michael@0: * version. michael@0: */ michael@0: const char* GrGetGLSLVersionDecl(const GrGLContextInfo&); michael@0: michael@0: /** michael@0: * Converts a GrSLType to a string containing the name of the equivalent GLSL type. michael@0: */ michael@0: static inline const char* GrGLSLTypeString(GrSLType t) { michael@0: switch (t) { michael@0: case kVoid_GrSLType: michael@0: return "void"; michael@0: case kFloat_GrSLType: michael@0: return "float"; michael@0: case kVec2f_GrSLType: michael@0: return "vec2"; michael@0: case kVec3f_GrSLType: michael@0: return "vec3"; michael@0: case kVec4f_GrSLType: michael@0: return "vec4"; michael@0: case kMat33f_GrSLType: michael@0: return "mat3"; michael@0: case kMat44f_GrSLType: michael@0: return "mat4"; michael@0: case kSampler2D_GrSLType: michael@0: return "sampler2D"; michael@0: default: michael@0: GrCrash("Unknown shader var type."); michael@0: return ""; // suppress warning michael@0: } michael@0: } michael@0: michael@0: /** A generic base-class representing a GLSL expression. michael@0: * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant michael@0: * folding with help of 1 and 0. michael@0: * michael@0: * Clients should not use this class, rather the specific instantiations defined michael@0: * later, for example GrGLSLExpr4. michael@0: */ michael@0: template michael@0: class GrGLSLExpr { michael@0: public: michael@0: bool isOnes() const { return kOnes_ExprType == fType; } michael@0: bool isZeros() const { return kZeros_ExprType == fType; } michael@0: michael@0: const char* c_str() const { michael@0: if (kZeros_ExprType == fType) { michael@0: return Self::ZerosStr(); michael@0: } else if (kOnes_ExprType == fType) { michael@0: return Self::OnesStr(); michael@0: } michael@0: SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. michael@0: return fExpr.c_str(); michael@0: } michael@0: michael@0: protected: michael@0: /** Constructs an invalid expression. michael@0: * Useful only as a return value from functions that never actually return michael@0: * this and instances that will be assigned to later. */ michael@0: GrGLSLExpr() michael@0: : fType(kFullExpr_ExprType) { michael@0: // The only constructor that is allowed to build an empty expression. michael@0: SkASSERT(!this->isValid()); michael@0: } michael@0: michael@0: /** Constructs an expression with all components as value v */ michael@0: explicit GrGLSLExpr(int v) { michael@0: if (v == 0) { michael@0: fType = kZeros_ExprType; michael@0: } else if (v == 1) { michael@0: fType = kOnes_ExprType; michael@0: } else { michael@0: fType = kFullExpr_ExprType; michael@0: fExpr.appendf(Self::CastIntStr(), v); michael@0: } michael@0: } michael@0: michael@0: /** Constructs an expression from a string. michael@0: * Argument expr is a simple expression or a parenthesized expression. */ michael@0: // TODO: make explicit once effects input Exprs. michael@0: GrGLSLExpr(const char expr[]) { michael@0: if (NULL == expr) { // TODO: remove this once effects input Exprs. michael@0: fType = kOnes_ExprType; michael@0: } else { michael@0: fType = kFullExpr_ExprType; michael@0: fExpr = expr; michael@0: } michael@0: SkASSERT(this->isValid()); michael@0: } michael@0: michael@0: /** Constructs an expression from a string. michael@0: * Argument expr is a simple expression or a parenthesized expression. */ michael@0: // TODO: make explicit once effects input Exprs. michael@0: GrGLSLExpr(const SkString& expr) { michael@0: if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. michael@0: fType = kOnes_ExprType; michael@0: } else { michael@0: fType = kFullExpr_ExprType; michael@0: fExpr = expr; michael@0: } michael@0: SkASSERT(this->isValid()); michael@0: } michael@0: michael@0: /** Constructs an expression from a string with one substitution. */ michael@0: GrGLSLExpr(const char format[], const char in0[]) michael@0: : fType(kFullExpr_ExprType) { michael@0: fExpr.appendf(format, in0); michael@0: } michael@0: michael@0: /** Constructs an expression from a string with two substitutions. */ michael@0: GrGLSLExpr(const char format[], const char in0[], const char in1[]) michael@0: : fType(kFullExpr_ExprType) { michael@0: fExpr.appendf(format, in0, in1); michael@0: } michael@0: michael@0: bool isValid() const { michael@0: return kFullExpr_ExprType != fType || !fExpr.isEmpty(); michael@0: } michael@0: michael@0: /** Returns expression casted to another type. michael@0: * Generic implementation that is called for non-trivial cases of casts. */ michael@0: template michael@0: static Self VectorCastImpl(const T& other); michael@0: michael@0: /** Returns a GLSL multiplication: component-wise or component-by-scalar. michael@0: * The multiplication will be component-wise or multiply each component by a scalar. michael@0: * michael@0: * The returned expression will compute the value of: michael@0: * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise) michael@0: * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar) michael@0: * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector) michael@0: */ michael@0: template michael@0: static Self Mul(T0 in0, T1 in1); michael@0: michael@0: /** Returns a GLSL addition: component-wise or add a scalar to each component. michael@0: * Return value computes: michael@0: * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...). michael@0: */ michael@0: template michael@0: static Self Add(T0 in0, T1 in1); michael@0: michael@0: /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar. michael@0: * Return value computes michael@0: * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...). michael@0: */ michael@0: template michael@0: static Self Sub(T0 in0, T1 in1); michael@0: michael@0: /** Returns expression that accesses component(s) of the expression. michael@0: * format should be the form "%s.x" where 'x' is the component(s) to access. michael@0: * Caller is responsible for making sure the amount of components in the michael@0: * format string is equal to dim(T). michael@0: */ michael@0: template michael@0: T extractComponents(const char format[]) const; michael@0: michael@0: private: michael@0: enum ExprType { michael@0: kZeros_ExprType, michael@0: kOnes_ExprType, michael@0: kFullExpr_ExprType, michael@0: }; michael@0: ExprType fType; michael@0: SkString fExpr; michael@0: }; michael@0: michael@0: class GrGLSLExpr1; michael@0: class GrGLSLExpr4; michael@0: michael@0: /** Class representing a float GLSL expression. */ michael@0: class GrGLSLExpr1 : public GrGLSLExpr { michael@0: public: michael@0: GrGLSLExpr1() michael@0: : INHERITED() { michael@0: } michael@0: explicit GrGLSLExpr1(int v) michael@0: : INHERITED(v) { michael@0: } michael@0: GrGLSLExpr1(const char* expr) michael@0: : INHERITED(expr) { michael@0: } michael@0: GrGLSLExpr1(const SkString& expr) michael@0: : INHERITED(expr) { michael@0: } michael@0: michael@0: static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr); michael@0: michael@0: private: michael@0: GrGLSLExpr1(const char format[], const char in0[]) michael@0: : INHERITED(format, in0) { michael@0: } michael@0: GrGLSLExpr1(const char format[], const char in0[], const char in1[]) michael@0: : INHERITED(format, in0, in1) { michael@0: } michael@0: michael@0: static const char* ZerosStr(); michael@0: static const char* OnesStr(); michael@0: static const char* CastStr(); michael@0: static const char* CastIntStr(); michael@0: michael@0: friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); michael@0: friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); michael@0: friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); michael@0: michael@0: friend class GrGLSLExpr; michael@0: friend class GrGLSLExpr; michael@0: michael@0: typedef GrGLSLExpr INHERITED; michael@0: }; michael@0: michael@0: /** Class representing a float vector (vec4) GLSL expression. */ michael@0: class GrGLSLExpr4 : public GrGLSLExpr { michael@0: public: michael@0: GrGLSLExpr4() michael@0: : INHERITED() { michael@0: } michael@0: explicit GrGLSLExpr4(int v) michael@0: : INHERITED(v) { michael@0: } michael@0: GrGLSLExpr4(const char* expr) michael@0: : INHERITED(expr) { michael@0: } michael@0: GrGLSLExpr4(const SkString& expr) michael@0: : INHERITED(expr) { michael@0: } michael@0: michael@0: typedef GrGLSLExpr1 AExpr; michael@0: AExpr a() const; michael@0: michael@0: /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */ michael@0: static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr); michael@0: static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr); michael@0: michael@0: private: michael@0: GrGLSLExpr4(const char format[], const char in0[]) michael@0: : INHERITED(format, in0) { michael@0: } michael@0: GrGLSLExpr4(const char format[], const char in0[], const char in1[]) michael@0: : INHERITED(format, in0, in1) { michael@0: } michael@0: michael@0: static const char* ZerosStr(); michael@0: static const char* OnesStr(); michael@0: static const char* CastStr(); michael@0: static const char* CastIntStr(); michael@0: michael@0: // The vector-by-scalar and scalar-by-vector binary operations. michael@0: friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); michael@0: friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); michael@0: friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); michael@0: friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); michael@0: friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); michael@0: friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); michael@0: michael@0: // The vector-by-vector, i.e. component-wise, binary operations. michael@0: friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); michael@0: friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); michael@0: friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); michael@0: michael@0: friend class GrGLSLExpr; michael@0: michael@0: typedef GrGLSLExpr INHERITED; michael@0: }; michael@0: michael@0: /** michael@0: * Does an inplace mul, *=, of vec4VarName by mulFactor. michael@0: * A semicolon and newline are added after the assignment. michael@0: */ michael@0: void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt, michael@0: const char* vec4VarName, const GrGLSLExpr4& mulFactor); michael@0: michael@0: #include "GrGLSL_impl.h" michael@0: michael@0: #endif