2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "gl/GrGLInterface.h"
13 #include "GrTypesPriv.h"
16 class GrGLContextInfo;
19 // Limited set of GLSL versions we build shaders for. Caller should round
20 // down the GLSL version to one of these enums.
21 enum GrGLSLGeneration {
23 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
25 k110_GrGLSLGeneration,
29 k130_GrGLSLGeneration,
33 k140_GrGLSLGeneration,
37 k150_GrGLSLGeneration,
39 * Desktop GLSL 3.30, and ES GLSL 3.00
41 k330_GrGLSLGeneration,
43 * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular
45 k310es_GrGLSLGeneration,
49 * Gets the most recent GLSL Generation compatible with the OpenGL context.
51 bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation);
54 * Returns a string to include at the beginning of a shader to declare the GLSL
57 const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
60 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
62 static inline const char* GrGLSLTypeString(GrSLType t) {
74 case kMat33f_GrSLType:
76 case kMat44f_GrSLType:
78 case kSampler2D_GrSLType:
81 SkFAIL("Unknown shader var type.");
82 return ""; // suppress warning
86 /** A generic base-class representing a GLSL expression.
87 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
88 * folding with help of 1 and 0.
90 * Clients should not use this class, rather the specific instantiations defined
91 * later, for example GrGLSLExpr4.
93 template <typename Self>
96 bool isOnes() const { return kOnes_ExprType == fType; }
97 bool isZeros() const { return kZeros_ExprType == fType; }
99 const char* c_str() const {
100 if (kZeros_ExprType == fType) {
101 return Self::ZerosStr();
102 } else if (kOnes_ExprType == fType) {
103 return Self::OnesStr();
105 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
106 return fExpr.c_str();
110 /** Constructs an invalid expression.
111 * Useful only as a return value from functions that never actually return
112 * this and instances that will be assigned to later. */
114 : fType(kFullExpr_ExprType) {
115 // The only constructor that is allowed to build an empty expression.
116 SkASSERT(!this->isValid());
119 /** Constructs an expression with all components as value v */
120 explicit GrGLSLExpr(int v) {
122 fType = kZeros_ExprType;
124 fType = kOnes_ExprType;
126 fType = kFullExpr_ExprType;
127 fExpr.appendf(Self::CastIntStr(), v);
131 /** Constructs an expression from a string.
132 * Argument expr is a simple expression or a parenthesized expression. */
133 // TODO: make explicit once effects input Exprs.
134 GrGLSLExpr(const char expr[]) {
135 if (NULL == expr) { // TODO: remove this once effects input Exprs.
136 fType = kOnes_ExprType;
138 fType = kFullExpr_ExprType;
141 SkASSERT(this->isValid());
144 /** Constructs an expression from a string.
145 * Argument expr is a simple expression or a parenthesized expression. */
146 // TODO: make explicit once effects input Exprs.
147 GrGLSLExpr(const SkString& expr) {
148 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
149 fType = kOnes_ExprType;
151 fType = kFullExpr_ExprType;
154 SkASSERT(this->isValid());
157 /** Constructs an expression from a string with one substitution. */
158 GrGLSLExpr(const char format[], const char in0[])
159 : fType(kFullExpr_ExprType) {
160 fExpr.appendf(format, in0);
163 /** Constructs an expression from a string with two substitutions. */
164 GrGLSLExpr(const char format[], const char in0[], const char in1[])
165 : fType(kFullExpr_ExprType) {
166 fExpr.appendf(format, in0, in1);
169 bool isValid() const {
170 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
173 /** Returns expression casted to another type.
174 * Generic implementation that is called for non-trivial cases of casts. */
175 template <typename T>
176 static Self VectorCastImpl(const T& other);
178 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
179 * The multiplication will be component-wise or multiply each component by a scalar.
181 * The returned expression will compute the value of:
182 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
183 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
184 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
186 template <typename T0, typename T1>
187 static Self Mul(T0 in0, T1 in1);
189 /** Returns a GLSL addition: component-wise or add a scalar to each component.
190 * Return value computes:
191 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
193 template <typename T0, typename T1>
194 static Self Add(T0 in0, T1 in1);
196 /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
197 * Return value computes
198 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
200 template <typename T0, typename T1>
201 static Self Sub(T0 in0, T1 in1);
203 /** Returns expression that accesses component(s) of the expression.
204 * format should be the form "%s.x" where 'x' is the component(s) to access.
205 * Caller is responsible for making sure the amount of components in the
206 * format string is equal to dim(T).
208 template <typename T>
209 T extractComponents(const char format[]) const;
224 /** Class representing a float GLSL expression. */
225 class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
230 explicit GrGLSLExpr1(int v)
233 GrGLSLExpr1(const char* expr)
236 GrGLSLExpr1(const SkString& expr)
240 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
243 GrGLSLExpr1(const char format[], const char in0[])
244 : INHERITED(format, in0) {
246 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
247 : INHERITED(format, in0, in1) {
250 static const char* ZerosStr();
251 static const char* OnesStr();
252 static const char* CastStr();
253 static const char* CastIntStr();
255 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
256 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
257 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
259 friend class GrGLSLExpr<GrGLSLExpr1>;
260 friend class GrGLSLExpr<GrGLSLExpr4>;
262 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
265 /** Class representing a float vector (vec4) GLSL expression. */
266 class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
271 explicit GrGLSLExpr4(int v)
274 GrGLSLExpr4(const char* expr)
277 GrGLSLExpr4(const SkString& expr)
281 typedef GrGLSLExpr1 AExpr;
284 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
285 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
286 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
289 GrGLSLExpr4(const char format[], const char in0[])
290 : INHERITED(format, in0) {
292 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
293 : INHERITED(format, in0, in1) {
296 static const char* ZerosStr();
297 static const char* OnesStr();
298 static const char* CastStr();
299 static const char* CastIntStr();
301 // The vector-by-scalar and scalar-by-vector binary operations.
302 friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
303 friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
304 friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
305 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
306 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
307 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
309 // The vector-by-vector, i.e. component-wise, binary operations.
310 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
311 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
312 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
314 friend class GrGLSLExpr<GrGLSLExpr4>;
316 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
320 * Does an inplace mul, *=, of vec4VarName by mulFactor.
321 * A semicolon is added after the assignment.
323 void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor);
325 #include "GrGLSL_impl.h"