blob: 417f6d78d2257ddb0f69912e01fc8950831cdcc8 [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
egdanielcb7ba1e2015-10-26 08:38:25 -070014class GrGLSLCaps;
15
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,
63 const GrGLSLCaps& glslCaps,
64 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";
77 default:
78 SkFAIL("Unexpected precision type.");
79 return "";
80 }
81}
82
83/**
bsalomon@google.com018f1792013-04-18 19:36:09 +000084 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
85 */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +000086static inline const char* GrGLSLTypeString(GrSLType t) {
bsalomon@google.com018f1792013-04-18 19:36:09 +000087 switch (t) {
88 case kVoid_GrSLType:
89 return "void";
90 case kFloat_GrSLType:
91 return "float";
92 case kVec2f_GrSLType:
93 return "vec2";
94 case kVec3f_GrSLType:
95 return "vec3";
96 case kVec4f_GrSLType:
97 return "vec4";
cdalton8d988b32016-03-07 15:39:09 -080098 case kMat22f_GrSLType:
99 return "mat2";
bsalomon@google.com018f1792013-04-18 19:36:09 +0000100 case kMat33f_GrSLType:
101 return "mat3";
102 case kMat44f_GrSLType:
103 return "mat4";
egdaniel990dbc82016-07-13 14:09:30 -0700104 case kTexture2DSampler_GrSLType:
bsalomon@google.com018f1792013-04-18 19:36:09 +0000105 return "sampler2D";
Brian Salomona8f00022016-11-16 12:55:57 -0500106 case kITexture2DSampler_GrSLType:
Brian Salomonbf7b6202016-11-11 16:08:03 -0500107 return "isampler2D";
egdaniel990dbc82016-07-13 14:09:30 -0700108 case kTextureExternalSampler_GrSLType:
bsalomon7ea33f52015-11-22 14:51:00 -0800109 return "samplerExternalOES";
egdaniel990dbc82016-07-13 14:09:30 -0700110 case kTexture2DRectSampler_GrSLType:
bsalomone5286e02016-01-14 09:24:09 -0800111 return "sampler2DRect";
csmartdalton22458032016-11-16 11:28:16 -0700112 case kBufferSampler_GrSLType:
cdalton74b8d322016-04-11 14:47:28 -0700113 return "samplerBuffer";
ethannicholas22793252016-01-30 09:59:10 -0800114 case kBool_GrSLType:
115 return "bool";
116 case kInt_GrSLType:
117 return "int";
cdalton793dc262016-02-08 10:11:47 -0800118 case kUint_GrSLType:
119 return "uint";
egdaniel990dbc82016-07-13 14:09:30 -0700120 case kTexture2D_GrSLType:
121 return "texture2D";
122 case kSampler_GrSLType:
123 return "sampler";
Brian Salomonf9f45122016-11-29 11:59:17 -0500124 case kImageStorage2D_GrSLType:
125 return "image2D";
126 case kIImageStorage2D_GrSLType:
127 return "iimage2D";
bsalomon@google.com018f1792013-04-18 19:36:09 +0000128 }
Brian Salomonbf7b6202016-11-11 16:08:03 -0500129 SkFAIL("Unknown shader var type.");
130 return ""; // suppress warning
bsalomon@google.com018f1792013-04-18 19:36:09 +0000131}
tomhudson@google.com086e5352011-12-08 14:44:10 +0000132
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000133/** A generic base-class representing a GLSL expression.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000134 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
135 * folding with help of 1 and 0.
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000136 *
137 * Clients should not use this class, rather the specific instantiations defined
138 * later, for example GrGLSLExpr4.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000139 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000140template <typename Self>
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000141class GrGLSLExpr {
142public:
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000143 bool isOnes() const { return kOnes_ExprType == fType; }
144 bool isZeros() const { return kZeros_ExprType == fType; }
145
146 const char* c_str() const {
147 if (kZeros_ExprType == fType) {
148 return Self::ZerosStr();
149 } else if (kOnes_ExprType == fType) {
150 return Self::OnesStr();
151 }
152 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
153 return fExpr.c_str();
154 }
155
joshualitt4973d9d2014-11-08 09:24:25 -0800156 bool isValid() const {
157 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
158 }
159
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000160protected:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000161 /** Constructs an invalid expression.
162 * Useful only as a return value from functions that never actually return
163 * this and instances that will be assigned to later. */
164 GrGLSLExpr()
165 : fType(kFullExpr_ExprType) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000166 // The only constructor that is allowed to build an empty expression.
167 SkASSERT(!this->isValid());
168 }
169
170 /** Constructs an expression with all components as value v */
171 explicit GrGLSLExpr(int v) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000172 if (v == 0) {
173 fType = kZeros_ExprType;
174 } else if (v == 1) {
175 fType = kOnes_ExprType;
176 } else {
177 fType = kFullExpr_ExprType;
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000178 fExpr.appendf(Self::CastIntStr(), v);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000179 }
180 }
181
182 /** Constructs an expression from a string.
183 * Argument expr is a simple expression or a parenthesized expression. */
184 // TODO: make explicit once effects input Exprs.
185 GrGLSLExpr(const char expr[]) {
halcanary96fcdcc2015-08-27 07:41:13 -0700186 if (nullptr == expr) { // TODO: remove this once effects input Exprs.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000187 fType = kOnes_ExprType;
188 } else {
189 fType = kFullExpr_ExprType;
190 fExpr = expr;
191 }
192 SkASSERT(this->isValid());
193 }
194
195 /** Constructs an expression from a string.
196 * Argument expr is a simple expression or a parenthesized expression. */
197 // TODO: make explicit once effects input Exprs.
198 GrGLSLExpr(const SkString& expr) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000199 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
200 fType = kOnes_ExprType;
201 } else {
202 fType = kFullExpr_ExprType;
203 fExpr = expr;
204 }
205 SkASSERT(this->isValid());
206 }
207
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000208 /** Constructs an expression from a string with one substitution. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000209 GrGLSLExpr(const char format[], const char in0[])
210 : fType(kFullExpr_ExprType) {
211 fExpr.appendf(format, in0);
212 }
213
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000214 /** Constructs an expression from a string with two substitutions. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000215 GrGLSLExpr(const char format[], const char in0[], const char in1[])
216 : fType(kFullExpr_ExprType) {
217 fExpr.appendf(format, in0, in1);
218 }
219
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000220 /** Returns expression casted to another type.
221 * Generic implementation that is called for non-trivial cases of casts. */
222 template <typename T>
223 static Self VectorCastImpl(const T& other);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000224
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000225 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
226 * The multiplication will be component-wise or multiply each component by a scalar.
227 *
228 * The returned expression will compute the value of:
229 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
230 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
231 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000232 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000233 template <typename T0, typename T1>
234 static Self Mul(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000235
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000236 /** Returns a GLSL addition: component-wise or add a scalar to each component.
237 * Return value computes:
238 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000239 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000240 template <typename T0, typename T1>
241 static Self Add(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000242
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000243 /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
244 * Return value computes
245 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000246 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000247 template <typename T0, typename T1>
248 static Self Sub(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000249
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000250 /** Returns expression that accesses component(s) of the expression.
251 * format should be the form "%s.x" where 'x' is the component(s) to access.
252 * Caller is responsible for making sure the amount of components in the
253 * format string is equal to dim(T).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000254 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000255 template <typename T>
256 T extractComponents(const char format[]) const;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000257
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000258private:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000259 enum ExprType {
260 kZeros_ExprType,
261 kOnes_ExprType,
262 kFullExpr_ExprType,
263 };
264 ExprType fType;
265 SkString fExpr;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000266};
267
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000268class GrGLSLExpr1;
269class GrGLSLExpr4;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000270
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000271/** Class representing a float GLSL expression. */
272class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
273public:
274 GrGLSLExpr1()
275 : INHERITED() {
276 }
277 explicit GrGLSLExpr1(int v)
278 : INHERITED(v) {
279 }
280 GrGLSLExpr1(const char* expr)
281 : INHERITED(expr) {
282 }
283 GrGLSLExpr1(const SkString& expr)
284 : INHERITED(expr) {
285 }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000286
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000287 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000288
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000289private:
290 GrGLSLExpr1(const char format[], const char in0[])
291 : INHERITED(format, in0) {
292 }
293 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
294 : INHERITED(format, in0, in1) {
295 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000296
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000297 static const char* ZerosStr();
298 static const char* OnesStr();
299 static const char* CastStr();
300 static const char* CastIntStr();
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000301
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000302 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
303 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
304 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000305
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000306 friend class GrGLSLExpr<GrGLSLExpr1>;
307 friend class GrGLSLExpr<GrGLSLExpr4>;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000308
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000309 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
310};
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000311
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000312/** Class representing a float vector (vec4) GLSL expression. */
313class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
314public:
315 GrGLSLExpr4()
316 : INHERITED() {
317 }
318 explicit GrGLSLExpr4(int v)
319 : INHERITED(v) {
320 }
321 GrGLSLExpr4(const char* expr)
322 : INHERITED(expr) {
323 }
324 GrGLSLExpr4(const SkString& expr)
325 : INHERITED(expr) {
326 }
327
328 typedef GrGLSLExpr1 AExpr;
329 AExpr a() const;
330
331 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
332 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
333 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
334
335private:
336 GrGLSLExpr4(const char format[], const char in0[])
337 : INHERITED(format, in0) {
338 }
339 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
340 : INHERITED(format, in0, in1) {
341 }
342
343 static const char* ZerosStr();
344 static const char* OnesStr();
345 static const char* CastStr();
346 static const char* CastIntStr();
347
348 // The vector-by-scalar and scalar-by-vector binary operations.
349 friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
350 friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
351 friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
352 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
353 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
354 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
355
356 // The vector-by-vector, i.e. component-wise, binary operations.
357 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
358 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
359 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
360
361 friend class GrGLSLExpr<GrGLSLExpr4>;
362
363 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
364};
bsalomon@google.com018f1792013-04-18 19:36:09 +0000365
366/**
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000367 * Does an inplace mul, *=, of vec4VarName by mulFactor.
egdaniel089f8de2014-10-09 10:34:58 -0700368 * A semicolon is added after the assignment.
bsalomon@google.com018f1792013-04-18 19:36:09 +0000369 */
egdaniel089f8de2014-10-09 10:34:58 -0700370void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000371
372#include "GrGLSL_impl.h"
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000373
tomhudson@google.com086e5352011-12-08 14:44:10 +0000374#endif