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,
41 * Gets the most recent GLSL Generation compatible with the OpenGL context.
43 bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation);
46 * Returns a string to include at the beginning of a shader to declare the GLSL
49 const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
52 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
54 static inline const char* GrGLSLTypeString(GrSLType t) {
66 case kMat33f_GrSLType:
68 case kMat44f_GrSLType:
70 case kSampler2D_GrSLType:
73 SkFAIL("Unknown shader var type.");
74 return ""; // suppress warning
78 /** A generic base-class representing a GLSL expression.
79 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
80 * folding with help of 1 and 0.
82 * Clients should not use this class, rather the specific instantiations defined
83 * later, for example GrGLSLExpr4.
85 template <typename Self>
88 bool isOnes() const { return kOnes_ExprType == fType; }
89 bool isZeros() const { return kZeros_ExprType == fType; }
91 const char* c_str() const {
92 if (kZeros_ExprType == fType) {
93 return Self::ZerosStr();
94 } else if (kOnes_ExprType == fType) {
95 return Self::OnesStr();
97 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
102 /** Constructs an invalid expression.
103 * Useful only as a return value from functions that never actually return
104 * this and instances that will be assigned to later. */
106 : fType(kFullExpr_ExprType) {
107 // The only constructor that is allowed to build an empty expression.
108 SkASSERT(!this->isValid());
111 /** Constructs an expression with all components as value v */
112 explicit GrGLSLExpr(int v) {
114 fType = kZeros_ExprType;
116 fType = kOnes_ExprType;
118 fType = kFullExpr_ExprType;
119 fExpr.appendf(Self::CastIntStr(), v);
123 /** Constructs an expression from a string.
124 * Argument expr is a simple expression or a parenthesized expression. */
125 // TODO: make explicit once effects input Exprs.
126 GrGLSLExpr(const char expr[]) {
127 if (NULL == expr) { // TODO: remove this once effects input Exprs.
128 fType = kOnes_ExprType;
130 fType = kFullExpr_ExprType;
133 SkASSERT(this->isValid());
136 /** Constructs an expression from a string.
137 * Argument expr is a simple expression or a parenthesized expression. */
138 // TODO: make explicit once effects input Exprs.
139 GrGLSLExpr(const SkString& expr) {
140 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
141 fType = kOnes_ExprType;
143 fType = kFullExpr_ExprType;
146 SkASSERT(this->isValid());
149 /** Constructs an expression from a string with one substitution. */
150 GrGLSLExpr(const char format[], const char in0[])
151 : fType(kFullExpr_ExprType) {
152 fExpr.appendf(format, in0);
155 /** Constructs an expression from a string with two substitutions. */
156 GrGLSLExpr(const char format[], const char in0[], const char in1[])
157 : fType(kFullExpr_ExprType) {
158 fExpr.appendf(format, in0, in1);
161 bool isValid() const {
162 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
165 /** Returns expression casted to another type.
166 * Generic implementation that is called for non-trivial cases of casts. */
167 template <typename T>
168 static Self VectorCastImpl(const T& other);
170 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
171 * The multiplication will be component-wise or multiply each component by a scalar.
173 * The returned expression will compute the value of:
174 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
175 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
176 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
178 template <typename T0, typename T1>
179 static Self Mul(T0 in0, T1 in1);
181 /** Returns a GLSL addition: component-wise or add a scalar to each component.
182 * Return value computes:
183 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
185 template <typename T0, typename T1>
186 static Self Add(T0 in0, T1 in1);
188 /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
189 * Return value computes
190 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
192 template <typename T0, typename T1>
193 static Self Sub(T0 in0, T1 in1);
195 /** Returns expression that accesses component(s) of the expression.
196 * format should be the form "%s.x" where 'x' is the component(s) to access.
197 * Caller is responsible for making sure the amount of components in the
198 * format string is equal to dim(T).
200 template <typename T>
201 T extractComponents(const char format[]) const;
216 /** Class representing a float GLSL expression. */
217 class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
222 explicit GrGLSLExpr1(int v)
225 GrGLSLExpr1(const char* expr)
228 GrGLSLExpr1(const SkString& expr)
232 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
235 GrGLSLExpr1(const char format[], const char in0[])
236 : INHERITED(format, in0) {
238 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
239 : INHERITED(format, in0, in1) {
242 static const char* ZerosStr();
243 static const char* OnesStr();
244 static const char* CastStr();
245 static const char* CastIntStr();
247 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
248 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
249 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
251 friend class GrGLSLExpr<GrGLSLExpr1>;
252 friend class GrGLSLExpr<GrGLSLExpr4>;
254 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
257 /** Class representing a float vector (vec4) GLSL expression. */
258 class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
263 explicit GrGLSLExpr4(int v)
266 GrGLSLExpr4(const char* expr)
269 GrGLSLExpr4(const SkString& expr)
273 typedef GrGLSLExpr1 AExpr;
276 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
277 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
278 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
281 GrGLSLExpr4(const char format[], const char in0[])
282 : INHERITED(format, in0) {
284 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
285 : INHERITED(format, in0, in1) {
288 static const char* ZerosStr();
289 static const char* OnesStr();
290 static const char* CastStr();
291 static const char* CastIntStr();
293 // The vector-by-scalar and scalar-by-vector binary operations.
294 friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
295 friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
296 friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
297 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
298 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
299 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
301 // The vector-by-vector, i.e. component-wise, binary operations.
302 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
303 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
304 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
306 friend class GrGLSLExpr<GrGLSLExpr4>;
308 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
312 * Does an inplace mul, *=, of vec4VarName by mulFactor.
313 * A semicolon and newline are added after the assignment.
315 void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt,
316 const char* vec4VarName, const GrGLSLExpr4& mulFactor);
318 #include "GrGLSL_impl.h"