diff options
author | 2016-11-29 13:43:05 -0500 | |
---|---|---|
committer | 2016-11-29 19:32:31 +0000 | |
commit | 94efbf51f5a88d9e8aa961d3fbe38c5e335d6108 (patch) | |
tree | 2723f6ec5bfc291d4a52adf961165e3e74a098ca /include/private | |
parent | e11b614795bbe707d131ad599ead5be452e7c06f (diff) |
Merge GrGLSLCaps into GrShaderCaps
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=5121
Change-Id: If8d13638f80f42161cbc766a2666c5789e5772c8
Reviewed-on: https://skia-review.googlesource.com/5121
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'include/private')
-rw-r--r-- | include/private/GrGLSL.h | 374 | ||||
-rw-r--r-- | include/private/GrGLSL_impl.h | 175 | ||||
-rw-r--r-- | include/private/GrSwizzle.h | 115 |
3 files changed, 664 insertions, 0 deletions
diff --git a/include/private/GrGLSL.h b/include/private/GrGLSL.h new file mode 100644 index 0000000000..6d9e403cfe --- /dev/null +++ b/include/private/GrGLSL.h @@ -0,0 +1,374 @@ +/* + * Copyright 2011 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef GrGLSL_DEFINED +#define GrGLSL_DEFINED + +#include "GrTypesPriv.h" +#include "SkString.h" + +class GrShaderCaps; + +// Limited set of GLSL versions we build shaders for. Caller should round +// down the GLSL version to one of these enums. +enum GrGLSLGeneration { + /** + * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) + */ + k110_GrGLSLGeneration, + /** + * Desktop GLSL 1.30 + */ + k130_GrGLSLGeneration, + /** + * Desktop GLSL 1.40 + */ + k140_GrGLSLGeneration, + /** + * Desktop GLSL 1.50 + */ + k150_GrGLSLGeneration, + /** + * Desktop GLSL 3.30, and ES GLSL 3.00 + */ + k330_GrGLSLGeneration, + /** + * Desktop GLSL 4.00 + */ + k400_GrGLSLGeneration, + /** + * Desktop GLSL 4.20 + */ + k420_GrGLSLGeneration, + /** + * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular + */ + k310es_GrGLSLGeneration, + /** + * ES GLSL 3.20 + */ + k320es_GrGLSLGeneration, +}; + +bool GrGLSLSupportsNamedFragmentShaderOutputs(GrGLSLGeneration); + +/** + * Adds a line of GLSL code to declare the default precision for float types. + */ +void GrGLSLAppendDefaultFloatPrecisionDeclaration(GrSLPrecision, + const GrShaderCaps&, + SkString* out); + +/** + * Converts a GrSLPrecision to its corresponding GLSL precision qualifier. + */ +static inline const char* GrGLSLPrecisionString(GrSLPrecision p) { + switch (p) { + case kLow_GrSLPrecision: + return "lowp"; + case kMedium_GrSLPrecision: + return "mediump"; + case kHigh_GrSLPrecision: + return "highp"; + default: + SkFAIL("Unexpected precision type."); + return ""; + } +} + +/** + * Converts a GrSLType to a string containing the name of the equivalent GLSL type. + */ +static inline const char* GrGLSLTypeString(GrSLType t) { + switch (t) { + case kVoid_GrSLType: + return "void"; + case kFloat_GrSLType: + return "float"; + case kVec2f_GrSLType: + return "vec2"; + case kVec3f_GrSLType: + return "vec3"; + case kVec4f_GrSLType: + return "vec4"; + case kMat22f_GrSLType: + return "mat2"; + case kMat33f_GrSLType: + return "mat3"; + case kMat44f_GrSLType: + return "mat4"; + case kTexture2DSampler_GrSLType: + return "sampler2D"; + case kITexture2DSampler_GrSLType: + return "isampler2D"; + case kTextureExternalSampler_GrSLType: + return "samplerExternalOES"; + case kTexture2DRectSampler_GrSLType: + return "sampler2DRect"; + case kBufferSampler_GrSLType: + return "samplerBuffer"; + case kBool_GrSLType: + return "bool"; + case kInt_GrSLType: + return "int"; + case kUint_GrSLType: + return "uint"; + case kTexture2D_GrSLType: + return "texture2D"; + case kSampler_GrSLType: + return "sampler"; + case kImageStorage2D_GrSLType: + return "image2D"; + case kIImageStorage2D_GrSLType: + return "iimage2D"; + } + SkFAIL("Unknown shader var type."); + return ""; // suppress warning +} + +/** A generic base-class representing a GLSL expression. + * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant + * folding with help of 1 and 0. + * + * Clients should not use this class, rather the specific instantiations defined + * later, for example GrGLSLExpr4. + */ +template <typename Self> +class GrGLSLExpr { +public: + bool isOnes() const { return kOnes_ExprType == fType; } + bool isZeros() const { return kZeros_ExprType == fType; } + + const char* c_str() const { + if (kZeros_ExprType == fType) { + return Self::ZerosStr(); + } else if (kOnes_ExprType == fType) { + return Self::OnesStr(); + } + SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. + return fExpr.c_str(); + } + + bool isValid() const { + return kFullExpr_ExprType != fType || !fExpr.isEmpty(); + } + +protected: + /** Constructs an invalid expression. + * Useful only as a return value from functions that never actually return + * this and instances that will be assigned to later. */ + GrGLSLExpr() + : fType(kFullExpr_ExprType) { + // The only constructor that is allowed to build an empty expression. + SkASSERT(!this->isValid()); + } + + /** Constructs an expression with all components as value v */ + explicit GrGLSLExpr(int v) { + if (v == 0) { + fType = kZeros_ExprType; + } else if (v == 1) { + fType = kOnes_ExprType; + } else { + fType = kFullExpr_ExprType; + fExpr.appendf(Self::CastIntStr(), v); + } + } + + /** Constructs an expression from a string. + * Argument expr is a simple expression or a parenthesized expression. */ + // TODO: make explicit once effects input Exprs. + GrGLSLExpr(const char expr[]) { + if (nullptr == expr) { // TODO: remove this once effects input Exprs. + fType = kOnes_ExprType; + } else { + fType = kFullExpr_ExprType; + fExpr = expr; + } + SkASSERT(this->isValid()); + } + + /** Constructs an expression from a string. + * Argument expr is a simple expression or a parenthesized expression. */ + // TODO: make explicit once effects input Exprs. + GrGLSLExpr(const SkString& expr) { + if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. + fType = kOnes_ExprType; + } else { + fType = kFullExpr_ExprType; + fExpr = expr; + } + SkASSERT(this->isValid()); + } + + /** Constructs an expression from a string with one substitution. */ + GrGLSLExpr(const char format[], const char in0[]) + : fType(kFullExpr_ExprType) { + fExpr.appendf(format, in0); + } + + /** Constructs an expression from a string with two substitutions. */ + GrGLSLExpr(const char format[], const char in0[], const char in1[]) + : fType(kFullExpr_ExprType) { + fExpr.appendf(format, in0, in1); + } + + /** Returns expression casted to another type. + * Generic implementation that is called for non-trivial cases of casts. */ + template <typename T> + static Self VectorCastImpl(const T& other); + + /** Returns a GLSL multiplication: component-wise or component-by-scalar. + * The multiplication will be component-wise or multiply each component by a scalar. + * + * The returned expression will compute the value of: + * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise) + * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar) + * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector) + */ + template <typename T0, typename T1> + static Self Mul(T0 in0, T1 in1); + + /** Returns a GLSL addition: component-wise or add a scalar to each component. + * Return value computes: + * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...). + */ + template <typename T0, typename T1> + static Self Add(T0 in0, T1 in1); + + /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar. + * Return value computes + * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...). + */ + template <typename T0, typename T1> + static Self Sub(T0 in0, T1 in1); + + /** Returns expression that accesses component(s) of the expression. + * format should be the form "%s.x" where 'x' is the component(s) to access. + * Caller is responsible for making sure the amount of components in the + * format string is equal to dim(T). + */ + template <typename T> + T extractComponents(const char format[]) const; + +private: + enum ExprType { + kZeros_ExprType, + kOnes_ExprType, + kFullExpr_ExprType, + }; + ExprType fType; + SkString fExpr; +}; + +class GrGLSLExpr1; +class GrGLSLExpr4; + +/** Class representing a float GLSL expression. */ +class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> { +public: + GrGLSLExpr1() + : INHERITED() { + } + explicit GrGLSLExpr1(int v) + : INHERITED(v) { + } + GrGLSLExpr1(const char* expr) + : INHERITED(expr) { + } + GrGLSLExpr1(const SkString& expr) + : INHERITED(expr) { + } + + static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr); + +private: + GrGLSLExpr1(const char format[], const char in0[]) + : INHERITED(format, in0) { + } + GrGLSLExpr1(const char format[], const char in0[], const char in1[]) + : INHERITED(format, in0, in1) { + } + + static const char* ZerosStr(); + static const char* OnesStr(); + static const char* CastStr(); + static const char* CastIntStr(); + + friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); + friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); + friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); + + friend class GrGLSLExpr<GrGLSLExpr1>; + friend class GrGLSLExpr<GrGLSLExpr4>; + + typedef GrGLSLExpr<GrGLSLExpr1> INHERITED; +}; + +/** Class representing a float vector (vec4) GLSL expression. */ +class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> { +public: + GrGLSLExpr4() + : INHERITED() { + } + explicit GrGLSLExpr4(int v) + : INHERITED(v) { + } + GrGLSLExpr4(const char* expr) + : INHERITED(expr) { + } + GrGLSLExpr4(const SkString& expr) + : INHERITED(expr) { + } + + typedef GrGLSLExpr1 AExpr; + AExpr a() const; + + /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */ + static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr); + static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr); + +private: + GrGLSLExpr4(const char format[], const char in0[]) + : INHERITED(format, in0) { + } + GrGLSLExpr4(const char format[], const char in0[], const char in1[]) + : INHERITED(format, in0, in1) { + } + + static const char* ZerosStr(); + static const char* OnesStr(); + static const char* CastStr(); + static const char* CastIntStr(); + + // The vector-by-scalar and scalar-by-vector binary operations. + friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); + friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); + friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); + friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); + friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); + friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); + + // The vector-by-vector, i.e. component-wise, binary operations. + friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); + friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); + friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); + + friend class GrGLSLExpr<GrGLSLExpr4>; + + typedef GrGLSLExpr<GrGLSLExpr4> INHERITED; +}; + +/** + * Does an inplace mul, *=, of vec4VarName by mulFactor. + * A semicolon is added after the assignment. + */ +void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor); + +#include "GrGLSL_impl.h" + +#endif diff --git a/include/private/GrGLSL_impl.h b/include/private/GrGLSL_impl.h new file mode 100644 index 0000000000..bdd69cc76b --- /dev/null +++ b/include/private/GrGLSL_impl.h @@ -0,0 +1,175 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef GrGLSL_impl_DEFINED +#define GrGLSL_impl_DEFINED + +template<typename Self> +template<typename T> +inline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) { + if (expr.isZeros()) { + return Self(0); + } + if (expr.isOnes()) { + return Self(1); + } + return Self(Self::CastStr(), expr.c_str()); +} + +template<typename Self> +template<typename T0, typename T1> +inline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) { + if (in0.isZeros() || in1.isZeros()) { + return Self(0); + } + if (in0.isOnes()) { + return Self::VectorCast(in1); + } + if (in1.isOnes()) { + return Self::VectorCast(in0); + } + return Self("(%s * %s)", in0.c_str(), in1.c_str()); +} + +template<typename Self> +template<typename T0, typename T1> +inline Self GrGLSLExpr<Self>::Add(T0 in0, T1 in1) { + if (in1.isZeros()) { + return Self::VectorCast(in0); + } + if (in0.isZeros()) { + return Self::VectorCast(in1); + } + if (in0.isOnes() && in1.isOnes()) { + return Self(2); + } + return Self("(%s + %s)", in0.c_str(), in1.c_str()); +} + +template<typename Self> +template<typename T0, typename T1> +inline Self GrGLSLExpr<Self>::Sub(T0 in0, T1 in1) { + if (in1.isZeros()) { + return Self::VectorCast(in0); + } + if (in1.isOnes()) { + if (in0.isOnes()) { + return Self(0); + } + } + + return Self("(%s - %s)", in0.c_str(), in1.c_str()); +} + +template <typename Self> +template <typename T> +T GrGLSLExpr<Self>::extractComponents(const char format[]) const { + if (this->isZeros()) { + return T(0); + } + if (this->isOnes()) { + return T(1); + } + return T(format, this->c_str()); +} + +inline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) { + return expr; +} + +inline const char* GrGLSLExpr1::ZerosStr() { + return "0"; +} + +inline const char* GrGLSLExpr1::OnesStr() { + return "1.0"; +} + +// GrGLSLExpr1::CastStr() is unimplemented because using them is likely an +// error. This is now caught compile-time. + +inline const char* GrGLSLExpr1::CastIntStr() { + return "%d"; +} + +inline GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { + return GrGLSLExpr1::Mul(in0, in1); +} + +inline GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { + return GrGLSLExpr1::Add(in0, in1); +} + +inline GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { + return GrGLSLExpr1::Sub(in0, in1); +} + +inline const char* GrGLSLExpr4::ZerosStr() { + return "vec4(0)"; +} + +inline const char* GrGLSLExpr4::OnesStr() { + return "vec4(1)"; +} + +inline const char* GrGLSLExpr4::CastStr() { + return "vec4(%s)"; +} + +inline const char* GrGLSLExpr4::CastIntStr() { + return "vec4(%d)"; +} + +inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) { + return INHERITED::VectorCastImpl(expr); +} + +inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) { + return expr; +} + +inline GrGLSLExpr4::AExpr GrGLSLExpr4::a() const { + return this->extractComponents<GrGLSLExpr4::AExpr>("%s.a"); +} + +inline GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { + return GrGLSLExpr4::Mul(in0, in1); +} + +inline GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { + return GrGLSLExpr4::Add(in0, in1); +} + +inline GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { + return GrGLSLExpr4::Sub(in0, in1); +} + +inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { + return GrGLSLExpr4::Mul(in0, in1); +} + +inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { + return GrGLSLExpr4::Add(in0, in1); +} + +inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { + return GrGLSLExpr4::Sub(in0, in1); +} + +inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { + return GrGLSLExpr4::Mul(in0, in1); +} + +inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { + return GrGLSLExpr4::Add(in0, in1); +} + +inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { + return GrGLSLExpr4::Sub(in0, in1); +} + +#endif diff --git a/include/private/GrSwizzle.h b/include/private/GrSwizzle.h new file mode 100644 index 0000000000..5fa39dd987 --- /dev/null +++ b/include/private/GrSwizzle.h @@ -0,0 +1,115 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef GrSwizzle_DEFINED +#define GrSwizzle_DEFINED + +#include "GrColor.h" +#include "SkRandom.h" + +/** Represents a rgba swizzle. It can be converted either into a string or a eight bit int. + Currently there is no way to specify an arbitrary swizzle, just some static swizzles and an + assignment operator. That could be relaxed. */ +class GrSwizzle { +private: + char fSwiz[5]; + uint8_t fKey; + + static constexpr int CToI(char c) { + return ('r' == c) ? (GrColor_SHIFT_R / 8) : + ('g' == c) ? (GrColor_SHIFT_G / 8) : + ('b' == c) ? (GrColor_SHIFT_B / 8) : + ('a' == c) ? (GrColor_SHIFT_A / 8) : -1; + } + + static constexpr char IToC(int idx) { + return (8 * idx) == GrColor_SHIFT_R ? 'r' : + (8 * idx) == GrColor_SHIFT_G ? 'g' : + (8 * idx) == GrColor_SHIFT_B ? 'b' : + (8 * idx) == GrColor_SHIFT_A ? 'a' : 'x'; + } + + constexpr GrSwizzle(const char c[4]) + : fSwiz{c[0], c[1], c[2], c[3], 0} + , fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 2) | (CToI(c[2]) << 4) | (CToI(c[3]) << 6)) {} + + GR_STATIC_ASSERT(sizeof(char[4]) == sizeof(uint32_t)); + uint32_t* asUIntPtr() { return SkTCast<uint32_t*>(fSwiz); } + uint32_t asUInt() const { return *SkTCast<const uint32_t*>(fSwiz); } + +public: + GrSwizzle() { *this = RGBA(); } + + GrSwizzle(const GrSwizzle& that) { *this = that; } + + GrSwizzle& operator=(const GrSwizzle& that) { + memcpy(this, &that, sizeof(GrSwizzle)); + return *this; + } + + /** Recreates a GrSwizzle from the output of asKey() */ + void setFromKey(uint8_t key) { + fKey = key; + for (int i = 0; i < 4; ++i) { + fSwiz[i] = IToC(key & 3); + key >>= 2; + } + SkASSERT(fSwiz[4] == 0); + } + + bool operator==(const GrSwizzle& that) const { return this->asUInt() == that.asUInt(); } + + bool operator!=(const GrSwizzle& that) const { return !(*this == that); } + + /** Compact representation of the swizzle suitable for a key. */ + uint8_t asKey() const { return fKey; } + + /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a'. */ + const char* c_str() const { return fSwiz; } + + /** Applies this swizzle to the input color and returns the swizzled color. */ + GrColor applyTo(GrColor color) const { + int idx; + uint32_t key = fKey; + // Index of the input color that should be mapped to output r. + idx = (key & 3); + uint32_t outR = (color >> idx * 8) & 0xFF; + key >>= 2; + idx = (key & 3); + uint32_t outG = (color >> idx * 8) & 0xFF; + key >>= 2; + idx = (key & 3); + uint32_t outB = (color >> idx * 8) & 0xFF; + key >>= 2; + idx = (key & 3); + uint32_t outA = (color >> idx * 8) & 0xFF; + return GrColorPackRGBA(outR, outG, outB, outA); + } + + static GrSwizzle RGBA() { return GrSwizzle("rgba"); } + static GrSwizzle AAAA() { return GrSwizzle("aaaa"); } + static GrSwizzle RRRR() { return GrSwizzle("rrrr"); } + static GrSwizzle BGRA() { return GrSwizzle("bgra"); } + + static GrSwizzle CreateRandom(SkRandom* random) { + switch (random->nextU() % 4) { + case 0: + return RGBA(); + case 1: + return BGRA(); + case 2: + return RRRR(); + case 3: + return AAAA(); + default: + SkFAIL("Mod is broken?!?"); + return RGBA(); + } + } +}; + +#endif |