blob: e4e165b284b0052cce552f4db2a87a952dff0ca2 [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 /**
rmistry63a9f842014-10-17 06:07:08 -070044 * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular
45 */
46 k310es_GrGLSLGeneration,
cdalton33ad7012016-02-22 07:55:44 -080047 /**
48 * ES GLSL 3.20
49 */
50 k320es_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000051};
52
bsalomonbc50e5c2015-06-04 08:49:34 -070053bool GrGLSLSupportsNamedFragmentShaderOutputs(GrGLSLGeneration);
54
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000055/**
bsalomon6df86402015-06-01 10:41:49 -070056 * Gets the name of the function that should be used to sample a 2D texture. Coord type is used
57 * to indicate whether the texture is sampled using projective textured (kVec3f) or not (kVec2f).
58 */
bsalomone5286e02016-01-14 09:24:09 -080059inline const char* GrGLSLTexture2DFunctionName(GrSLType coordType, GrSLType samplerType,
60 GrGLSLGeneration glslGen) {
egdaniel990dbc82016-07-13 14:09:30 -070061 SkASSERT(GrSLTypeIs2DCombinedSamplerType(samplerType));
bsalomone5286e02016-01-14 09:24:09 -080062 SkASSERT(kVec2f_GrSLType == coordType || kVec3f_GrSLType == coordType);
63 // GL_TEXTURE_RECTANGLE_ARB is written against OpenGL 2.0/GLSL 1.10. At that time there were
64 // separate texture*() functions. In OpenGL 3.0/GLSL 1.30 the different texture*() functions
65 // were deprecated in favor or the unified texture() function. RECTANGLE textures became
66 // standard in OpenGL 3.2/GLSL 1.50 and use texture(). It isn't completely clear what function
67 // should be used for RECTANGLE textures in GLSL versions >= 1.30 && < 1.50. We're going with
68 // using texture().
69 if (glslGen >= k130_GrGLSLGeneration) {
70 return (kVec2f_GrSLType == coordType) ? "texture" : "textureProj";
71 }
bsalomon6df86402015-06-01 10:41:49 -070072 if (kVec2f_GrSLType == coordType) {
egdaniel990dbc82016-07-13 14:09:30 -070073 return (samplerType == kTexture2DRectSampler_GrSLType) ? "texture2DRect" : "texture2D";
bsalomon6df86402015-06-01 10:41:49 -070074 } else {
egdaniel990dbc82016-07-13 14:09:30 -070075 return (samplerType == kTexture2DRectSampler_GrSLType) ? "texture2DRectProj"
76 : "texture2DProj";
bsalomon6df86402015-06-01 10:41:49 -070077 }
78}
79
80/**
egdanielcb7ba1e2015-10-26 08:38:25 -070081 * Adds a line of GLSL code to declare the default precision for float types.
82 */
83void GrGLSLAppendDefaultFloatPrecisionDeclaration(GrSLPrecision,
84 const GrGLSLCaps& glslCaps,
85 SkString* out);
86
87/**
cdalton1f50acf2016-04-11 11:30:50 -070088 * Converts a GrSLPrecision to its corresponding GLSL precision qualifier.
89 */
90static inline const char* GrGLSLPrecisionString(GrSLPrecision p) {
91 switch (p) {
92 case kLow_GrSLPrecision:
93 return "lowp";
94 case kMedium_GrSLPrecision:
95 return "mediump";
96 case kHigh_GrSLPrecision:
97 return "highp";
98 default:
99 SkFAIL("Unexpected precision type.");
100 return "";
101 }
102}
103
104/**
bsalomon@google.com018f1792013-04-18 19:36:09 +0000105 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
106 */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000107static inline const char* GrGLSLTypeString(GrSLType t) {
bsalomon@google.com018f1792013-04-18 19:36:09 +0000108 switch (t) {
109 case kVoid_GrSLType:
110 return "void";
111 case kFloat_GrSLType:
112 return "float";
113 case kVec2f_GrSLType:
114 return "vec2";
115 case kVec3f_GrSLType:
116 return "vec3";
117 case kVec4f_GrSLType:
118 return "vec4";
cdalton8d988b32016-03-07 15:39:09 -0800119 case kMat22f_GrSLType:
120 return "mat2";
bsalomon@google.com018f1792013-04-18 19:36:09 +0000121 case kMat33f_GrSLType:
122 return "mat3";
123 case kMat44f_GrSLType:
124 return "mat4";
egdaniel990dbc82016-07-13 14:09:30 -0700125 case kTexture2DSampler_GrSLType:
bsalomon@google.com018f1792013-04-18 19:36:09 +0000126 return "sampler2D";
egdaniel990dbc82016-07-13 14:09:30 -0700127 case kTextureExternalSampler_GrSLType:
bsalomon7ea33f52015-11-22 14:51:00 -0800128 return "samplerExternalOES";
egdaniel990dbc82016-07-13 14:09:30 -0700129 case kTexture2DRectSampler_GrSLType:
bsalomone5286e02016-01-14 09:24:09 -0800130 return "sampler2DRect";
egdaniel990dbc82016-07-13 14:09:30 -0700131 case kTextureBufferSampler_GrSLType:
cdalton74b8d322016-04-11 14:47:28 -0700132 return "samplerBuffer";
ethannicholas22793252016-01-30 09:59:10 -0800133 case kBool_GrSLType:
134 return "bool";
135 case kInt_GrSLType:
136 return "int";
cdalton793dc262016-02-08 10:11:47 -0800137 case kUint_GrSLType:
138 return "uint";
egdaniel990dbc82016-07-13 14:09:30 -0700139 case kTexture2D_GrSLType:
140 return "texture2D";
141 case kSampler_GrSLType:
142 return "sampler";
bsalomon@google.com018f1792013-04-18 19:36:09 +0000143 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000144 SkFAIL("Unknown shader var type.");
bsalomon@google.com018f1792013-04-18 19:36:09 +0000145 return ""; // suppress warning
146 }
147}
tomhudson@google.com086e5352011-12-08 14:44:10 +0000148
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000149/** A generic base-class representing a GLSL expression.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000150 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
151 * folding with help of 1 and 0.
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000152 *
153 * Clients should not use this class, rather the specific instantiations defined
154 * later, for example GrGLSLExpr4.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000155 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000156template <typename Self>
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000157class GrGLSLExpr {
158public:
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000159 bool isOnes() const { return kOnes_ExprType == fType; }
160 bool isZeros() const { return kZeros_ExprType == fType; }
161
162 const char* c_str() const {
163 if (kZeros_ExprType == fType) {
164 return Self::ZerosStr();
165 } else if (kOnes_ExprType == fType) {
166 return Self::OnesStr();
167 }
168 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
169 return fExpr.c_str();
170 }
171
joshualitt4973d9d2014-11-08 09:24:25 -0800172 bool isValid() const {
173 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
174 }
175
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000176protected:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000177 /** Constructs an invalid expression.
178 * Useful only as a return value from functions that never actually return
179 * this and instances that will be assigned to later. */
180 GrGLSLExpr()
181 : fType(kFullExpr_ExprType) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000182 // The only constructor that is allowed to build an empty expression.
183 SkASSERT(!this->isValid());
184 }
185
186 /** Constructs an expression with all components as value v */
187 explicit GrGLSLExpr(int v) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000188 if (v == 0) {
189 fType = kZeros_ExprType;
190 } else if (v == 1) {
191 fType = kOnes_ExprType;
192 } else {
193 fType = kFullExpr_ExprType;
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000194 fExpr.appendf(Self::CastIntStr(), v);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000195 }
196 }
197
198 /** Constructs an expression from a string.
199 * Argument expr is a simple expression or a parenthesized expression. */
200 // TODO: make explicit once effects input Exprs.
201 GrGLSLExpr(const char expr[]) {
halcanary96fcdcc2015-08-27 07:41:13 -0700202 if (nullptr == expr) { // TODO: remove this once effects input Exprs.
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000203 fType = kOnes_ExprType;
204 } else {
205 fType = kFullExpr_ExprType;
206 fExpr = expr;
207 }
208 SkASSERT(this->isValid());
209 }
210
211 /** Constructs an expression from a string.
212 * Argument expr is a simple expression or a parenthesized expression. */
213 // TODO: make explicit once effects input Exprs.
214 GrGLSLExpr(const SkString& expr) {
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000215 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
216 fType = kOnes_ExprType;
217 } else {
218 fType = kFullExpr_ExprType;
219 fExpr = expr;
220 }
221 SkASSERT(this->isValid());
222 }
223
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000224 /** Constructs an expression from a string with one substitution. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000225 GrGLSLExpr(const char format[], const char in0[])
226 : fType(kFullExpr_ExprType) {
227 fExpr.appendf(format, in0);
228 }
229
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000230 /** Constructs an expression from a string with two substitutions. */
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000231 GrGLSLExpr(const char format[], const char in0[], const char in1[])
232 : fType(kFullExpr_ExprType) {
233 fExpr.appendf(format, in0, in1);
234 }
235
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000236 /** Returns expression casted to another type.
237 * Generic implementation that is called for non-trivial cases of casts. */
238 template <typename T>
239 static Self VectorCastImpl(const T& other);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000240
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000241 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
242 * The multiplication will be component-wise or multiply each component by a scalar.
243 *
244 * The returned expression will compute the value of:
245 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
246 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
247 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000248 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000249 template <typename T0, typename T1>
250 static Self Mul(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000251
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000252 /** Returns a GLSL addition: component-wise or add a scalar to each component.
253 * Return value computes:
254 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000255 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000256 template <typename T0, typename T1>
257 static Self Add(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000258
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000259 /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
260 * Return value computes
261 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000262 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000263 template <typename T0, typename T1>
264 static Self Sub(T0 in0, T1 in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000265
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000266 /** Returns expression that accesses component(s) of the expression.
267 * format should be the form "%s.x" where 'x' is the component(s) to access.
268 * Caller is responsible for making sure the amount of components in the
269 * format string is equal to dim(T).
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000270 */
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000271 template <typename T>
272 T extractComponents(const char format[]) const;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000273
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000274private:
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000275 enum ExprType {
276 kZeros_ExprType,
277 kOnes_ExprType,
278 kFullExpr_ExprType,
279 };
280 ExprType fType;
281 SkString fExpr;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000282};
283
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000284class GrGLSLExpr1;
285class GrGLSLExpr4;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000286
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000287/** Class representing a float GLSL expression. */
288class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
289public:
290 GrGLSLExpr1()
291 : INHERITED() {
292 }
293 explicit GrGLSLExpr1(int v)
294 : INHERITED(v) {
295 }
296 GrGLSLExpr1(const char* expr)
297 : INHERITED(expr) {
298 }
299 GrGLSLExpr1(const SkString& expr)
300 : INHERITED(expr) {
301 }
tomhudson@google.com168e6342012-04-18 17:49:20 +0000302
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000303 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000304
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000305private:
306 GrGLSLExpr1(const char format[], const char in0[])
307 : INHERITED(format, in0) {
308 }
309 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
310 : INHERITED(format, in0, in1) {
311 }
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000312
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000313 static const char* ZerosStr();
314 static const char* OnesStr();
315 static const char* CastStr();
316 static const char* CastIntStr();
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000317
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000318 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
319 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
320 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000321
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000322 friend class GrGLSLExpr<GrGLSLExpr1>;
323 friend class GrGLSLExpr<GrGLSLExpr4>;
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000324
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000325 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
326};
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000327
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000328/** Class representing a float vector (vec4) GLSL expression. */
329class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
330public:
331 GrGLSLExpr4()
332 : INHERITED() {
333 }
334 explicit GrGLSLExpr4(int v)
335 : INHERITED(v) {
336 }
337 GrGLSLExpr4(const char* expr)
338 : INHERITED(expr) {
339 }
340 GrGLSLExpr4(const SkString& expr)
341 : INHERITED(expr) {
342 }
343
344 typedef GrGLSLExpr1 AExpr;
345 AExpr a() const;
346
347 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
348 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
349 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
350
351private:
352 GrGLSLExpr4(const char format[], const char in0[])
353 : INHERITED(format, in0) {
354 }
355 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
356 : INHERITED(format, in0, in1) {
357 }
358
359 static const char* ZerosStr();
360 static const char* OnesStr();
361 static const char* CastStr();
362 static const char* CastIntStr();
363
364 // The vector-by-scalar and scalar-by-vector binary operations.
365 friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
366 friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
367 friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
368 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
369 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
370 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
371
372 // The vector-by-vector, i.e. component-wise, binary operations.
373 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
374 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
375 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
376
377 friend class GrGLSLExpr<GrGLSLExpr4>;
378
379 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
380};
bsalomon@google.com018f1792013-04-18 19:36:09 +0000381
382/**
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000383 * Does an inplace mul, *=, of vec4VarName by mulFactor.
egdaniel089f8de2014-10-09 10:34:58 -0700384 * A semicolon is added after the assignment.
bsalomon@google.com018f1792013-04-18 19:36:09 +0000385 */
egdaniel089f8de2014-10-09 10:34:58 -0700386void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000387
388#include "GrGLSL_impl.h"
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000389
tomhudson@google.com086e5352011-12-08 14:44:10 +0000390#endif