blob: 8c5da51bde097386f73da9a8df1f0053755459fe [file] [log] [blame]
tomhudson@google.com086e5352011-12-08 14:44:10 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrGLSL_DEFINED
9#define GrGLSL_DEFINED
10
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000011#include "gl/GrGLInterface.h"
bsalomon@google.com018f1792013-04-18 19:36:09 +000012#include "GrColor.h"
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000013#include "GrTypesPriv.h"
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000014#include "SkString.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000015
commit-bot@chromium.org06f05982013-08-30 15:52:36 +000016class GrGLContextInfo;
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000017class GrGLShaderVar;
18
tomhudson@google.com086e5352011-12-08 14:44:10 +000019// Limited set of GLSL versions we build shaders for. Caller should round
20// down the GLSL version to one of these enums.
21enum GrGLSLGeneration {
22 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000023 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
tomhudson@google.com086e5352011-12-08 14:44:10 +000024 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000025 k110_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000026 /**
27 * Desktop GLSL 1.30
28 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000029 k130_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000030 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000031 * Desktop GLSL 1.40
32 */
33 k140_GrGLSLGeneration,
34 /**
35 * Desktop GLSL 1.50
tomhudson@google.com086e5352011-12-08 14:44:10 +000036 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000037 k150_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000038};
39
tomhudson@google.com168e6342012-04-18 17:49:20 +000040/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000041 * Gets the most recent GLSL Generation compatible with the OpenGL context.
42 */
43GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
44 const GrGLInterface* gl);
45
46/**
bsalomon@google.com281c7262012-10-23 14:31:30 +000047 * Returns a string to include at the beginning of a shader to declare the GLSL
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000048 * version.
49 */
commit-bot@chromium.org06f05982013-08-30 15:52:36 +000050const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000051
52/**
bsalomon@google.com018f1792013-04-18 19:36:09 +000053 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
54 */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000055static inline const char* GrGLSLTypeString(GrSLType t) {
bsalomon@google.com018f1792013-04-18 19:36:09 +000056 switch (t) {
57 case kVoid_GrSLType:
58 return "void";
59 case kFloat_GrSLType:
60 return "float";
61 case kVec2f_GrSLType:
62 return "vec2";
63 case kVec3f_GrSLType:
64 return "vec3";
65 case kVec4f_GrSLType:
66 return "vec4";
67 case kMat33f_GrSLType:
68 return "mat3";
69 case kMat44f_GrSLType:
70 return "mat4";
71 case kSampler2D_GrSLType:
72 return "sampler2D";
73 default:
74 GrCrash("Unknown shader var type.");
75 return ""; // suppress warning
76 }
77}
tomhudson@google.com086e5352011-12-08 14:44:10 +000078
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000079/** A generic base-class representing a GLSL expression.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000080 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
81 * folding with help of 1 and 0.
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000082 *
83 * Clients should not use this class, rather the specific instantiations defined
84 * later, for example GrGLSLExpr4.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000085 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000086template <typename Self>
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000087class GrGLSLExpr {
88public:
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +000089 bool isOnes() const { return kOnes_ExprType == fType; }
90 bool isZeros() const { return kZeros_ExprType == fType; }
91
92 const char* c_str() const {
93 if (kZeros_ExprType == fType) {
94 return Self::ZerosStr();
95 } else if (kOnes_ExprType == fType) {
96 return Self::OnesStr();
97 }
98 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
99 return fExpr.c_str();
100 }
101
102protected:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000103 /** Constructs an invalid expression.
104 * Useful only as a return value from functions that never actually return
105 * this and instances that will be assigned to later. */
106 GrGLSLExpr()
107 : fType(kFullExpr_ExprType) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000108 // The only constructor that is allowed to build an empty expression.
109 SkASSERT(!this->isValid());
110 }
111
112 /** Constructs an expression with all components as value v */
113 explicit GrGLSLExpr(int v) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000114 if (v == 0) {
115 fType = kZeros_ExprType;
116 } else if (v == 1) {
117 fType = kOnes_ExprType;
118 } else {
119 fType = kFullExpr_ExprType;
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000120 fExpr.appendf(Self::CastIntStr(), v);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000121 }
122 }
123
124 /** Constructs an expression from a string.
125 * Argument expr is a simple expression or a parenthesized expression. */
126 // TODO: make explicit once effects input Exprs.
127 GrGLSLExpr(const char expr[]) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000128 if (NULL == expr) { // TODO: remove this once effects input Exprs.
129 fType = kOnes_ExprType;
130 } else {
131 fType = kFullExpr_ExprType;
132 fExpr = expr;
133 }
134 SkASSERT(this->isValid());
135 }
136
137 /** Constructs an expression from a string.
138 * Argument expr is a simple expression or a parenthesized expression. */
139 // TODO: make explicit once effects input Exprs.
140 GrGLSLExpr(const SkString& expr) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000141 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
142 fType = kOnes_ExprType;
143 } else {
144 fType = kFullExpr_ExprType;
145 fExpr = expr;
146 }
147 SkASSERT(this->isValid());
148 }
149
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000150 /** Constructs an expression from a string with one substitution. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000151 GrGLSLExpr(const char format[], const char in0[])
152 : fType(kFullExpr_ExprType) {
153 fExpr.appendf(format, in0);
154 }
155
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000156 /** Constructs an expression from a string with two substitutions. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000157 GrGLSLExpr(const char format[], const char in0[], const char in1[])
158 : fType(kFullExpr_ExprType) {
159 fExpr.appendf(format, in0, in1);
160 }
161
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000162 bool isValid() const {
163 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
164 }
165
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000166 /** Returns expression casted to another type.
167 * Generic implementation that is called for non-trivial cases of casts. */
168 template <typename T>
169 static Self VectorCastImpl(const T& other);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000170
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000171 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
172 * The multiplication will be component-wise or multiply each component by a scalar.
173 *
174 * The returned expression will compute the value of:
175 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
176 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
177 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000178 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000179 template <typename T0, typename T1>
180 static Self Mul(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000181
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000182 /** Returns a GLSL addition: component-wise or add a scalar to each component.
183 * Return value computes:
184 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000185 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000186 template <typename T0, typename T1>
187 static Self Add(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000188
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000189 /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
190 * Return value computes
191 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000192 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000193 template <typename T0, typename T1>
194 static Self Sub(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000195
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000196 /** Returns expression that accesses component(s) of the expression.
197 * format should be the form "%s.x" where 'x' is the component(s) to access.
198 * Caller is responsible for making sure the amount of components in the
199 * format string is equal to dim(T).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000200 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000201 template <typename T>
202 T extractComponents(const char format[]) const;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000203
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000204private:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000205 enum ExprType {
206 kZeros_ExprType,
207 kOnes_ExprType,
208 kFullExpr_ExprType,
209 };
210 ExprType fType;
211 SkString fExpr;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000212};
213
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000214class GrGLSLExpr1;
215class GrGLSLExpr4;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000216
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000217/** Class representing a float GLSL expression. */
218class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
219public:
220 GrGLSLExpr1()
221 : INHERITED() {
222 }
223 explicit GrGLSLExpr1(int v)
224 : INHERITED(v) {
225 }
226 GrGLSLExpr1(const char* expr)
227 : INHERITED(expr) {
228 }
229 GrGLSLExpr1(const SkString& expr)
230 : INHERITED(expr) {
231 }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000232
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000233 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000234
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000235private:
236 GrGLSLExpr1(const char format[], const char in0[])
237 : INHERITED(format, in0) {
238 }
239 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
240 : INHERITED(format, in0, in1) {
241 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000242
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000243 static const char* ZerosStr();
244 static const char* OnesStr();
245 static const char* CastStr();
246 static const char* CastIntStr();
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000247
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000248 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
249 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
250 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000251
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000252 friend class GrGLSLExpr<GrGLSLExpr1>;
253 friend class GrGLSLExpr<GrGLSLExpr4>;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000254
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000255 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
256};
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000257
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000258/** Class representing a float vector (vec4) GLSL expression. */
259class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
260public:
261 GrGLSLExpr4()
262 : INHERITED() {
263 }
264 explicit GrGLSLExpr4(int v)
265 : INHERITED(v) {
266 }
267 GrGLSLExpr4(const char* expr)
268 : INHERITED(expr) {
269 }
270 GrGLSLExpr4(const SkString& expr)
271 : INHERITED(expr) {
272 }
273
274 typedef GrGLSLExpr1 AExpr;
275 AExpr a() const;
276
277 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
278 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
279 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
280
281private:
282 GrGLSLExpr4(const char format[], const char in0[])
283 : INHERITED(format, in0) {
284 }
285 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
286 : INHERITED(format, in0, in1) {
287 }
288
289 static const char* ZerosStr();
290 static const char* OnesStr();
291 static const char* CastStr();
292 static const char* CastIntStr();
293
294 // The vector-by-scalar and scalar-by-vector binary operations.
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 GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
298 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
299 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
300 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
301
302 // The vector-by-vector, i.e. component-wise, binary operations.
303 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
304 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
305 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
306
307 friend class GrGLSLExpr<GrGLSLExpr4>;
308
309 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
310};
bsalomon@google.com018f1792013-04-18 19:36:09 +0000311
312/**
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000313 * Does an inplace mul, *=, of vec4VarName by mulFactor.
314 * A semicolon and newline are added after the assignment.
bsalomon@google.com018f1792013-04-18 19:36:09 +0000315 */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000316void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000317 const char* vec4VarName, const GrGLSLExpr4& mulFactor);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000318
319#include "GrGLSL_impl.h"
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000320
tomhudson@google.com086e5352011-12-08 14:44:10 +0000321#endif