gfx/skia/trunk/src/gpu/gl/GrGLSL.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2011 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #ifndef GrGLSL_DEFINED
michael@0 9 #define GrGLSL_DEFINED
michael@0 10
michael@0 11 #include "gl/GrGLInterface.h"
michael@0 12 #include "GrColor.h"
michael@0 13 #include "GrTypesPriv.h"
michael@0 14 #include "SkString.h"
michael@0 15
michael@0 16 class GrGLContextInfo;
michael@0 17 class GrGLShaderVar;
michael@0 18
michael@0 19 // Limited set of GLSL versions we build shaders for. Caller should round
michael@0 20 // down the GLSL version to one of these enums.
michael@0 21 enum GrGLSLGeneration {
michael@0 22 /**
michael@0 23 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
michael@0 24 */
michael@0 25 k110_GrGLSLGeneration,
michael@0 26 /**
michael@0 27 * Desktop GLSL 1.30
michael@0 28 */
michael@0 29 k130_GrGLSLGeneration,
michael@0 30 /**
michael@0 31 * Desktop GLSL 1.40
michael@0 32 */
michael@0 33 k140_GrGLSLGeneration,
michael@0 34 /**
michael@0 35 * Desktop GLSL 1.50
michael@0 36 */
michael@0 37 k150_GrGLSLGeneration,
michael@0 38 };
michael@0 39
michael@0 40 /**
michael@0 41 * Gets the most recent GLSL Generation compatible with the OpenGL context.
michael@0 42 */
michael@0 43 GrGLSLGeneration GrGetGLSLGeneration(const GrGLInterface* gl);
michael@0 44
michael@0 45 /**
michael@0 46 * Returns a string to include at the beginning of a shader to declare the GLSL
michael@0 47 * version.
michael@0 48 */
michael@0 49 const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
michael@0 50
michael@0 51 /**
michael@0 52 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
michael@0 53 */
michael@0 54 static inline const char* GrGLSLTypeString(GrSLType t) {
michael@0 55 switch (t) {
michael@0 56 case kVoid_GrSLType:
michael@0 57 return "void";
michael@0 58 case kFloat_GrSLType:
michael@0 59 return "float";
michael@0 60 case kVec2f_GrSLType:
michael@0 61 return "vec2";
michael@0 62 case kVec3f_GrSLType:
michael@0 63 return "vec3";
michael@0 64 case kVec4f_GrSLType:
michael@0 65 return "vec4";
michael@0 66 case kMat33f_GrSLType:
michael@0 67 return "mat3";
michael@0 68 case kMat44f_GrSLType:
michael@0 69 return "mat4";
michael@0 70 case kSampler2D_GrSLType:
michael@0 71 return "sampler2D";
michael@0 72 default:
michael@0 73 GrCrash("Unknown shader var type.");
michael@0 74 return ""; // suppress warning
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 /** A generic base-class representing a GLSL expression.
michael@0 79 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
michael@0 80 * folding with help of 1 and 0.
michael@0 81 *
michael@0 82 * Clients should not use this class, rather the specific instantiations defined
michael@0 83 * later, for example GrGLSLExpr4.
michael@0 84 */
michael@0 85 template <typename Self>
michael@0 86 class GrGLSLExpr {
michael@0 87 public:
michael@0 88 bool isOnes() const { return kOnes_ExprType == fType; }
michael@0 89 bool isZeros() const { return kZeros_ExprType == fType; }
michael@0 90
michael@0 91 const char* c_str() const {
michael@0 92 if (kZeros_ExprType == fType) {
michael@0 93 return Self::ZerosStr();
michael@0 94 } else if (kOnes_ExprType == fType) {
michael@0 95 return Self::OnesStr();
michael@0 96 }
michael@0 97 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
michael@0 98 return fExpr.c_str();
michael@0 99 }
michael@0 100
michael@0 101 protected:
michael@0 102 /** Constructs an invalid expression.
michael@0 103 * Useful only as a return value from functions that never actually return
michael@0 104 * this and instances that will be assigned to later. */
michael@0 105 GrGLSLExpr()
michael@0 106 : fType(kFullExpr_ExprType) {
michael@0 107 // The only constructor that is allowed to build an empty expression.
michael@0 108 SkASSERT(!this->isValid());
michael@0 109 }
michael@0 110
michael@0 111 /** Constructs an expression with all components as value v */
michael@0 112 explicit GrGLSLExpr(int v) {
michael@0 113 if (v == 0) {
michael@0 114 fType = kZeros_ExprType;
michael@0 115 } else if (v == 1) {
michael@0 116 fType = kOnes_ExprType;
michael@0 117 } else {
michael@0 118 fType = kFullExpr_ExprType;
michael@0 119 fExpr.appendf(Self::CastIntStr(), v);
michael@0 120 }
michael@0 121 }
michael@0 122
michael@0 123 /** Constructs an expression from a string.
michael@0 124 * Argument expr is a simple expression or a parenthesized expression. */
michael@0 125 // TODO: make explicit once effects input Exprs.
michael@0 126 GrGLSLExpr(const char expr[]) {
michael@0 127 if (NULL == expr) { // TODO: remove this once effects input Exprs.
michael@0 128 fType = kOnes_ExprType;
michael@0 129 } else {
michael@0 130 fType = kFullExpr_ExprType;
michael@0 131 fExpr = expr;
michael@0 132 }
michael@0 133 SkASSERT(this->isValid());
michael@0 134 }
michael@0 135
michael@0 136 /** Constructs an expression from a string.
michael@0 137 * Argument expr is a simple expression or a parenthesized expression. */
michael@0 138 // TODO: make explicit once effects input Exprs.
michael@0 139 GrGLSLExpr(const SkString& expr) {
michael@0 140 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
michael@0 141 fType = kOnes_ExprType;
michael@0 142 } else {
michael@0 143 fType = kFullExpr_ExprType;
michael@0 144 fExpr = expr;
michael@0 145 }
michael@0 146 SkASSERT(this->isValid());
michael@0 147 }
michael@0 148
michael@0 149 /** Constructs an expression from a string with one substitution. */
michael@0 150 GrGLSLExpr(const char format[], const char in0[])
michael@0 151 : fType(kFullExpr_ExprType) {
michael@0 152 fExpr.appendf(format, in0);
michael@0 153 }
michael@0 154
michael@0 155 /** Constructs an expression from a string with two substitutions. */
michael@0 156 GrGLSLExpr(const char format[], const char in0[], const char in1[])
michael@0 157 : fType(kFullExpr_ExprType) {
michael@0 158 fExpr.appendf(format, in0, in1);
michael@0 159 }
michael@0 160
michael@0 161 bool isValid() const {
michael@0 162 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
michael@0 163 }
michael@0 164
michael@0 165 /** Returns expression casted to another type.
michael@0 166 * Generic implementation that is called for non-trivial cases of casts. */
michael@0 167 template <typename T>
michael@0 168 static Self VectorCastImpl(const T& other);
michael@0 169
michael@0 170 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
michael@0 171 * The multiplication will be component-wise or multiply each component by a scalar.
michael@0 172 *
michael@0 173 * The returned expression will compute the value of:
michael@0 174 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
michael@0 175 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
michael@0 176 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
michael@0 177 */
michael@0 178 template <typename T0, typename T1>
michael@0 179 static Self Mul(T0 in0, T1 in1);
michael@0 180
michael@0 181 /** Returns a GLSL addition: component-wise or add a scalar to each component.
michael@0 182 * Return value computes:
michael@0 183 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
michael@0 184 */
michael@0 185 template <typename T0, typename T1>
michael@0 186 static Self Add(T0 in0, T1 in1);
michael@0 187
michael@0 188 /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
michael@0 189 * Return value computes
michael@0 190 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
michael@0 191 */
michael@0 192 template <typename T0, typename T1>
michael@0 193 static Self Sub(T0 in0, T1 in1);
michael@0 194
michael@0 195 /** Returns expression that accesses component(s) of the expression.
michael@0 196 * format should be the form "%s.x" where 'x' is the component(s) to access.
michael@0 197 * Caller is responsible for making sure the amount of components in the
michael@0 198 * format string is equal to dim(T).
michael@0 199 */
michael@0 200 template <typename T>
michael@0 201 T extractComponents(const char format[]) const;
michael@0 202
michael@0 203 private:
michael@0 204 enum ExprType {
michael@0 205 kZeros_ExprType,
michael@0 206 kOnes_ExprType,
michael@0 207 kFullExpr_ExprType,
michael@0 208 };
michael@0 209 ExprType fType;
michael@0 210 SkString fExpr;
michael@0 211 };
michael@0 212
michael@0 213 class GrGLSLExpr1;
michael@0 214 class GrGLSLExpr4;
michael@0 215
michael@0 216 /** Class representing a float GLSL expression. */
michael@0 217 class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
michael@0 218 public:
michael@0 219 GrGLSLExpr1()
michael@0 220 : INHERITED() {
michael@0 221 }
michael@0 222 explicit GrGLSLExpr1(int v)
michael@0 223 : INHERITED(v) {
michael@0 224 }
michael@0 225 GrGLSLExpr1(const char* expr)
michael@0 226 : INHERITED(expr) {
michael@0 227 }
michael@0 228 GrGLSLExpr1(const SkString& expr)
michael@0 229 : INHERITED(expr) {
michael@0 230 }
michael@0 231
michael@0 232 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
michael@0 233
michael@0 234 private:
michael@0 235 GrGLSLExpr1(const char format[], const char in0[])
michael@0 236 : INHERITED(format, in0) {
michael@0 237 }
michael@0 238 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
michael@0 239 : INHERITED(format, in0, in1) {
michael@0 240 }
michael@0 241
michael@0 242 static const char* ZerosStr();
michael@0 243 static const char* OnesStr();
michael@0 244 static const char* CastStr();
michael@0 245 static const char* CastIntStr();
michael@0 246
michael@0 247 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
michael@0 248 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
michael@0 249 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
michael@0 250
michael@0 251 friend class GrGLSLExpr<GrGLSLExpr1>;
michael@0 252 friend class GrGLSLExpr<GrGLSLExpr4>;
michael@0 253
michael@0 254 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
michael@0 255 };
michael@0 256
michael@0 257 /** Class representing a float vector (vec4) GLSL expression. */
michael@0 258 class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
michael@0 259 public:
michael@0 260 GrGLSLExpr4()
michael@0 261 : INHERITED() {
michael@0 262 }
michael@0 263 explicit GrGLSLExpr4(int v)
michael@0 264 : INHERITED(v) {
michael@0 265 }
michael@0 266 GrGLSLExpr4(const char* expr)
michael@0 267 : INHERITED(expr) {
michael@0 268 }
michael@0 269 GrGLSLExpr4(const SkString& expr)
michael@0 270 : INHERITED(expr) {
michael@0 271 }
michael@0 272
michael@0 273 typedef GrGLSLExpr1 AExpr;
michael@0 274 AExpr a() const;
michael@0 275
michael@0 276 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
michael@0 277 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
michael@0 278 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
michael@0 279
michael@0 280 private:
michael@0 281 GrGLSLExpr4(const char format[], const char in0[])
michael@0 282 : INHERITED(format, in0) {
michael@0 283 }
michael@0 284 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
michael@0 285 : INHERITED(format, in0, in1) {
michael@0 286 }
michael@0 287
michael@0 288 static const char* ZerosStr();
michael@0 289 static const char* OnesStr();
michael@0 290 static const char* CastStr();
michael@0 291 static const char* CastIntStr();
michael@0 292
michael@0 293 // The vector-by-scalar and scalar-by-vector binary operations.
michael@0 294 friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
michael@0 295 friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
michael@0 296 friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
michael@0 297 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
michael@0 298 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
michael@0 299 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
michael@0 300
michael@0 301 // The vector-by-vector, i.e. component-wise, binary operations.
michael@0 302 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
michael@0 303 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
michael@0 304 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
michael@0 305
michael@0 306 friend class GrGLSLExpr<GrGLSLExpr4>;
michael@0 307
michael@0 308 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
michael@0 309 };
michael@0 310
michael@0 311 /**
michael@0 312 * Does an inplace mul, *=, of vec4VarName by mulFactor.
michael@0 313 * A semicolon and newline are added after the assignment.
michael@0 314 */
michael@0 315 void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt,
michael@0 316 const char* vec4VarName, const GrGLSLExpr4& mulFactor);
michael@0 317
michael@0 318 #include "GrGLSL_impl.h"
michael@0 319
michael@0 320 #endif

mercurial