blob: 66f7be84d1435c16e34e94772c4529c38b7946bd [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
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000011#include "GrTypesPriv.h"
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000012#include "SkString.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000013
Brian Salomon94efbf52016-11-29 13:43:05 -050014class GrShaderCaps;
egdanielcb7ba1e2015-10-26 08:38:25 -070015
tomhudson@google.com086e5352011-12-08 14:44:10 +000016// Limited set of GLSL versions we build shaders for. Caller should round
17// down the GLSL version to one of these enums.
18enum GrGLSLGeneration {
19 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000020 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
tomhudson@google.com086e5352011-12-08 14:44:10 +000021 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000022 k110_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000023 /**
24 * Desktop GLSL 1.30
25 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000026 k130_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000027 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000028 * Desktop GLSL 1.40
29 */
30 k140_GrGLSLGeneration,
31 /**
32 * Desktop GLSL 1.50
tomhudson@google.com086e5352011-12-08 14:44:10 +000033 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000034 k150_GrGLSLGeneration,
rmistry63a9f842014-10-17 06:07:08 -070035 /**
36 * Desktop GLSL 3.30, and ES GLSL 3.00
37 */
38 k330_GrGLSLGeneration,
39 /**
cdalton33ad7012016-02-22 07:55:44 -080040 * Desktop GLSL 4.00
41 */
42 k400_GrGLSLGeneration,
43 /**
Brian Salomond327e8c2016-11-15 13:26:08 -050044 * Desktop GLSL 4.20
45 */
46 k420_GrGLSLGeneration,
47 /**
rmistry63a9f842014-10-17 06:07:08 -070048 * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular
49 */
50 k310es_GrGLSLGeneration,
cdalton33ad7012016-02-22 07:55:44 -080051 /**
52 * ES GLSL 3.20
53 */
54 k320es_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000055};
56
bsalomonbc50e5c2015-06-04 08:49:34 -070057bool GrGLSLSupportsNamedFragmentShaderOutputs(GrGLSLGeneration);
58
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000059/**
egdanielcb7ba1e2015-10-26 08:38:25 -070060 * Adds a line of GLSL code to declare the default precision for float types.
61 */
62void GrGLSLAppendDefaultFloatPrecisionDeclaration(GrSLPrecision,
Brian Salomon94efbf52016-11-29 13:43:05 -050063 const GrShaderCaps&,
egdanielcb7ba1e2015-10-26 08:38:25 -070064 SkString* out);
65
66/**
cdalton1f50acf2016-04-11 11:30:50 -070067 * Converts a GrSLPrecision to its corresponding GLSL precision qualifier.
68 */
69static inline const char* GrGLSLPrecisionString(GrSLPrecision p) {
70 switch (p) {
71 case kLow_GrSLPrecision:
72 return "lowp";
73 case kMedium_GrSLPrecision:
74 return "mediump";
75 case kHigh_GrSLPrecision:
76 return "highp";
Brian Osman33aa2c72017-04-05 09:26:15 -040077 case kDefault_GrSLPrecision:
78 return "";
cdalton1f50acf2016-04-11 11:30:50 -070079 default:
80 SkFAIL("Unexpected precision type.");
81 return "";
82 }
83}
84
85/**
bsalomon@google.com018f1792013-04-18 19:36:09 +000086 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
87 */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000088static inline const char* GrGLSLTypeString(GrSLType t) {
bsalomon@google.com018f1792013-04-18 19:36:09 +000089 switch (t) {
90 case kVoid_GrSLType:
91 return "void";
92 case kFloat_GrSLType:
93 return "float";
94 case kVec2f_GrSLType:
95 return "vec2";
96 case kVec3f_GrSLType:
97 return "vec3";
98 case kVec4f_GrSLType:
99 return "vec4";
csmartdaltonb37cb232017-02-08 14:56:27 -0500100 case kVec2i_GrSLType:
101 return "ivec2";
102 case kVec3i_GrSLType:
103 return "ivec3";
104 case kVec4i_GrSLType:
105 return "ivec4";
cdalton8d988b32016-03-07 15:39:09 -0800106 case kMat22f_GrSLType:
107 return "mat2";
bsalomon@google.com018f1792013-04-18 19:36:09 +0000108 case kMat33f_GrSLType:
109 return "mat3";
110 case kMat44f_GrSLType:
111 return "mat4";
egdaniel990dbc82016-07-13 14:09:30 -0700112 case kTexture2DSampler_GrSLType:
bsalomon@google.com018f1792013-04-18 19:36:09 +0000113 return "sampler2D";
Brian Salomona8f00022016-11-16 12:55:57 -0500114 case kITexture2DSampler_GrSLType:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500115 return "isampler2D";
egdaniel990dbc82016-07-13 14:09:30 -0700116 case kTextureExternalSampler_GrSLType:
bsalomon7ea33f52015-11-22 14:51:00 -0800117 return "samplerExternalOES";
egdaniel990dbc82016-07-13 14:09:30 -0700118 case kTexture2DRectSampler_GrSLType:
bsalomone5286e02016-01-14 09:24:09 -0800119 return "sampler2DRect";
csmartdalton22458032016-11-16 11:28:16 -0700120 case kBufferSampler_GrSLType:
cdalton74b8d322016-04-11 14:47:28 -0700121 return "samplerBuffer";
ethannicholas22793252016-01-30 09:59:10 -0800122 case kBool_GrSLType:
123 return "bool";
124 case kInt_GrSLType:
125 return "int";
cdalton793dc262016-02-08 10:11:47 -0800126 case kUint_GrSLType:
127 return "uint";
egdaniel990dbc82016-07-13 14:09:30 -0700128 case kTexture2D_GrSLType:
129 return "texture2D";
130 case kSampler_GrSLType:
131 return "sampler";
Brian Salomonf9f45122016-11-29 11:59:17 -0500132 case kImageStorage2D_GrSLType:
133 return "image2D";
134 case kIImageStorage2D_GrSLType:
135 return "iimage2D";
bsalomon@google.com018f1792013-04-18 19:36:09 +0000136 }
Brian Salomonbf7b6202016-11-11 16:08:03 -0500137 SkFAIL("Unknown shader var type.");
138 return ""; // suppress warning
bsalomon@google.com018f1792013-04-18 19:36:09 +0000139}
tomhudson@google.com086e5352011-12-08 14:44:10 +0000140
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000141/** A generic base-class representing a GLSL expression.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000142 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
143 * folding with help of 1 and 0.
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000144 *
145 * Clients should not use this class, rather the specific instantiations defined
146 * later, for example GrGLSLExpr4.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000147 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000148template <typename Self>
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000149class GrGLSLExpr {
150public:
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000151 bool isOnes() const { return kOnes_ExprType == fType; }
152 bool isZeros() const { return kZeros_ExprType == fType; }
153
154 const char* c_str() const {
155 if (kZeros_ExprType == fType) {
156 return Self::ZerosStr();
157 } else if (kOnes_ExprType == fType) {
158 return Self::OnesStr();
159 }
160 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
161 return fExpr.c_str();
162 }
163
joshualitt4973d9d2014-11-08 09:24:25 -0800164 bool isValid() const {
165 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
166 }
167
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000168protected:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000169 /** Constructs an invalid expression.
170 * Useful only as a return value from functions that never actually return
171 * this and instances that will be assigned to later. */
172 GrGLSLExpr()
173 : fType(kFullExpr_ExprType) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000174 // The only constructor that is allowed to build an empty expression.
175 SkASSERT(!this->isValid());
176 }
177
178 /** Constructs an expression with all components as value v */
179 explicit GrGLSLExpr(int v) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000180 if (v == 0) {
181 fType = kZeros_ExprType;
182 } else if (v == 1) {
183 fType = kOnes_ExprType;
184 } else {
185 fType = kFullExpr_ExprType;
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000186 fExpr.appendf(Self::CastIntStr(), v);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000187 }
188 }
189
190 /** Constructs an expression from a string.
191 * Argument expr is a simple expression or a parenthesized expression. */
192 // TODO: make explicit once effects input Exprs.
193 GrGLSLExpr(const char expr[]) {
halcanary96fcdcc2015-08-27 07:41:13 -0700194 if (nullptr == expr) { // TODO: remove this once effects input Exprs.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000195 fType = kOnes_ExprType;
196 } else {
197 fType = kFullExpr_ExprType;
198 fExpr = expr;
199 }
200 SkASSERT(this->isValid());
201 }
202
203 /** Constructs an expression from a string.
204 * Argument expr is a simple expression or a parenthesized expression. */
205 // TODO: make explicit once effects input Exprs.
206 GrGLSLExpr(const SkString& expr) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000207 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
208 fType = kOnes_ExprType;
209 } else {
210 fType = kFullExpr_ExprType;
211 fExpr = expr;
212 }
213 SkASSERT(this->isValid());
214 }
215
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000216 /** Constructs an expression from a string with one substitution. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000217 GrGLSLExpr(const char format[], const char in0[])
218 : fType(kFullExpr_ExprType) {
219 fExpr.appendf(format, in0);
220 }
221
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000222 /** Constructs an expression from a string with two substitutions. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000223 GrGLSLExpr(const char format[], const char in0[], const char in1[])
224 : fType(kFullExpr_ExprType) {
225 fExpr.appendf(format, in0, in1);
226 }
227
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000228 /** Returns expression casted to another type.
229 * Generic implementation that is called for non-trivial cases of casts. */
230 template <typename T>
231 static Self VectorCastImpl(const T& other);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000232
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000233 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
234 * The multiplication will be component-wise or multiply each component by a scalar.
235 *
236 * The returned expression will compute the value of:
237 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
238 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
239 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000240 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000241 template <typename T0, typename T1>
242 static Self Mul(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000243
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000244 /** Returns a GLSL addition: component-wise or add a scalar to each component.
245 * Return value computes:
246 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000247 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000248 template <typename T0, typename T1>
249 static Self Add(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000250
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000251 /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
252 * Return value computes
253 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000254 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000255 template <typename T0, typename T1>
256 static Self Sub(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000257
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000258 /** Returns expression that accesses component(s) of the expression.
259 * format should be the form "%s.x" where 'x' is the component(s) to access.
260 * Caller is responsible for making sure the amount of components in the
261 * format string is equal to dim(T).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000262 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000263 template <typename T>
264 T extractComponents(const char format[]) const;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000265
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000266private:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000267 enum ExprType {
268 kZeros_ExprType,
269 kOnes_ExprType,
270 kFullExpr_ExprType,
271 };
272 ExprType fType;
273 SkString fExpr;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000274};
275
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000276class GrGLSLExpr1;
277class GrGLSLExpr4;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000278
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000279/** Class representing a float GLSL expression. */
280class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
281public:
282 GrGLSLExpr1()
283 : INHERITED() {
284 }
285 explicit GrGLSLExpr1(int v)
286 : INHERITED(v) {
287 }
288 GrGLSLExpr1(const char* expr)
289 : INHERITED(expr) {
290 }
291 GrGLSLExpr1(const SkString& expr)
292 : INHERITED(expr) {
293 }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000294
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000295 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000296
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000297private:
298 GrGLSLExpr1(const char format[], const char in0[])
299 : INHERITED(format, in0) {
300 }
301 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
302 : INHERITED(format, in0, in1) {
303 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000304
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000305 static const char* ZerosStr();
306 static const char* OnesStr();
307 static const char* CastStr();
308 static const char* CastIntStr();
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000309
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000310 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
311 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
312 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000313
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000314 friend class GrGLSLExpr<GrGLSLExpr1>;
315 friend class GrGLSLExpr<GrGLSLExpr4>;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000316
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000317 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
318};
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000319
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000320/** Class representing a float vector (vec4) GLSL expression. */
321class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
322public:
323 GrGLSLExpr4()
324 : INHERITED() {
325 }
326 explicit GrGLSLExpr4(int v)
327 : INHERITED(v) {
328 }
329 GrGLSLExpr4(const char* expr)
330 : INHERITED(expr) {
331 }
332 GrGLSLExpr4(const SkString& expr)
333 : INHERITED(expr) {
334 }
335
336 typedef GrGLSLExpr1 AExpr;
337 AExpr a() const;
338
339 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
340 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
341 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
342
343private:
344 GrGLSLExpr4(const char format[], const char in0[])
345 : INHERITED(format, in0) {
346 }
347 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
348 : INHERITED(format, in0, in1) {
349 }
350
351 static const char* ZerosStr();
352 static const char* OnesStr();
353 static const char* CastStr();
354 static const char* CastIntStr();
355
356 // The vector-by-scalar and scalar-by-vector binary operations.
357 friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
358 friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
359 friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
360 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
361 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
362 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
363
364 // The vector-by-vector, i.e. component-wise, binary operations.
365 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
366 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
367 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
368
369 friend class GrGLSLExpr<GrGLSLExpr4>;
370
371 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
372};
bsalomon@google.com018f1792013-04-18 19:36:09 +0000373
374/**
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000375 * Does an inplace mul, *=, of vec4VarName by mulFactor.
egdaniel089f8de2014-10-09 10:34:58 -0700376 * A semicolon is added after the assignment.
bsalomon@google.com018f1792013-04-18 19:36:09 +0000377 */
egdaniel089f8de2014-10-09 10:34:58 -0700378void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000379
380#include "GrGLSL_impl.h"
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000381
tomhudson@google.com086e5352011-12-08 14:44:10 +0000382#endif