Line data Source code
1 : /*
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 :
11 : #include "GrTypesPriv.h"
12 : #include "SkString.h"
13 :
14 : class GrShaderCaps;
15 :
16 : // Limited set of GLSL versions we build shaders for. Caller should round
17 : // down the GLSL version to one of these enums.
18 : enum GrGLSLGeneration {
19 : /**
20 : * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
21 : */
22 : k110_GrGLSLGeneration,
23 : /**
24 : * Desktop GLSL 1.30
25 : */
26 : k130_GrGLSLGeneration,
27 : /**
28 : * Desktop GLSL 1.40
29 : */
30 : k140_GrGLSLGeneration,
31 : /**
32 : * Desktop GLSL 1.50
33 : */
34 : k150_GrGLSLGeneration,
35 : /**
36 : * Desktop GLSL 3.30, and ES GLSL 3.00
37 : */
38 : k330_GrGLSLGeneration,
39 : /**
40 : * Desktop GLSL 4.00
41 : */
42 : k400_GrGLSLGeneration,
43 : /**
44 : * Desktop GLSL 4.20
45 : */
46 : k420_GrGLSLGeneration,
47 : /**
48 : * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular
49 : */
50 : k310es_GrGLSLGeneration,
51 : /**
52 : * ES GLSL 3.20
53 : */
54 : k320es_GrGLSLGeneration,
55 : };
56 :
57 : bool GrGLSLSupportsNamedFragmentShaderOutputs(GrGLSLGeneration);
58 :
59 : /**
60 : * Adds a line of GLSL code to declare the default precision for float types.
61 : */
62 : void GrGLSLAppendDefaultFloatPrecisionDeclaration(GrSLPrecision,
63 : const GrShaderCaps&,
64 : SkString* out);
65 :
66 : /**
67 : * Converts a GrSLPrecision to its corresponding GLSL precision qualifier.
68 : */
69 0 : static inline const char* GrGLSLPrecisionString(GrSLPrecision p) {
70 0 : switch (p) {
71 : case kLow_GrSLPrecision:
72 0 : return "lowp";
73 : case kMedium_GrSLPrecision:
74 0 : return "mediump";
75 : case kHigh_GrSLPrecision:
76 0 : return "highp";
77 : case kDefault_GrSLPrecision:
78 0 : return "";
79 : default:
80 0 : SkFAIL("Unexpected precision type.");
81 0 : return "";
82 : }
83 : }
84 :
85 : /**
86 : * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
87 : */
88 0 : static inline const char* GrGLSLTypeString(GrSLType t) {
89 0 : switch (t) {
90 : case kVoid_GrSLType:
91 0 : return "void";
92 : case kFloat_GrSLType:
93 0 : return "float";
94 : case kVec2f_GrSLType:
95 0 : return "vec2";
96 : case kVec3f_GrSLType:
97 0 : return "vec3";
98 : case kVec4f_GrSLType:
99 0 : return "vec4";
100 : case kVec2i_GrSLType:
101 0 : return "ivec2";
102 : case kVec3i_GrSLType:
103 0 : return "ivec3";
104 : case kVec4i_GrSLType:
105 0 : return "ivec4";
106 : case kMat22f_GrSLType:
107 0 : return "mat2";
108 : case kMat33f_GrSLType:
109 0 : return "mat3";
110 : case kMat44f_GrSLType:
111 0 : return "mat4";
112 : case kTexture2DSampler_GrSLType:
113 0 : return "sampler2D";
114 : case kITexture2DSampler_GrSLType:
115 0 : return "isampler2D";
116 : case kTextureExternalSampler_GrSLType:
117 0 : return "samplerExternalOES";
118 : case kTexture2DRectSampler_GrSLType:
119 0 : return "sampler2DRect";
120 : case kBufferSampler_GrSLType:
121 0 : return "samplerBuffer";
122 : case kBool_GrSLType:
123 0 : return "bool";
124 : case kInt_GrSLType:
125 0 : return "int";
126 : case kUint_GrSLType:
127 0 : return "uint";
128 : case kTexture2D_GrSLType:
129 0 : return "texture2D";
130 : case kSampler_GrSLType:
131 0 : return "sampler";
132 : case kImageStorage2D_GrSLType:
133 0 : return "image2D";
134 : case kIImageStorage2D_GrSLType:
135 0 : return "iimage2D";
136 : }
137 0 : SkFAIL("Unknown shader var type.");
138 0 : return ""; // suppress warning
139 : }
140 :
141 : /** A generic base-class representing a GLSL expression.
142 : * 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.
144 : *
145 : * Clients should not use this class, rather the specific instantiations defined
146 : * later, for example GrGLSLExpr4.
147 : */
148 : template <typename Self>
149 0 : class GrGLSLExpr {
150 : public:
151 0 : bool isOnes() const { return kOnes_ExprType == fType; }
152 0 : bool isZeros() const { return kZeros_ExprType == fType; }
153 :
154 0 : const char* c_str() const {
155 0 : if (kZeros_ExprType == fType) {
156 0 : return Self::ZerosStr();
157 0 : } else if (kOnes_ExprType == fType) {
158 0 : return Self::OnesStr();
159 : }
160 0 : SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
161 0 : return fExpr.c_str();
162 : }
163 :
164 0 : bool isValid() const {
165 0 : return kFullExpr_ExprType != fType || !fExpr.isEmpty();
166 : }
167 :
168 : protected:
169 : /** 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 0 : GrGLSLExpr()
173 0 : : fType(kFullExpr_ExprType) {
174 : // The only constructor that is allowed to build an empty expression.
175 0 : SkASSERT(!this->isValid());
176 0 : }
177 :
178 : /** Constructs an expression with all components as value v */
179 0 : explicit GrGLSLExpr(int v) {
180 0 : if (v == 0) {
181 0 : fType = kZeros_ExprType;
182 0 : } else if (v == 1) {
183 0 : fType = kOnes_ExprType;
184 : } else {
185 0 : fType = kFullExpr_ExprType;
186 0 : fExpr.appendf(Self::CastIntStr(), v);
187 : }
188 0 : }
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 0 : GrGLSLExpr(const char expr[]) {
194 0 : if (nullptr == expr) { // TODO: remove this once effects input Exprs.
195 0 : fType = kOnes_ExprType;
196 : } else {
197 0 : fType = kFullExpr_ExprType;
198 0 : fExpr = expr;
199 : }
200 0 : SkASSERT(this->isValid());
201 0 : }
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 0 : GrGLSLExpr(const SkString& expr) {
207 0 : if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
208 0 : fType = kOnes_ExprType;
209 : } else {
210 0 : fType = kFullExpr_ExprType;
211 0 : fExpr = expr;
212 : }
213 0 : SkASSERT(this->isValid());
214 0 : }
215 :
216 : /** Constructs an expression from a string with one substitution. */
217 0 : GrGLSLExpr(const char format[], const char in0[])
218 0 : : fType(kFullExpr_ExprType) {
219 0 : fExpr.appendf(format, in0);
220 0 : }
221 :
222 : /** Constructs an expression from a string with two substitutions. */
223 0 : GrGLSLExpr(const char format[], const char in0[], const char in1[])
224 0 : : fType(kFullExpr_ExprType) {
225 0 : fExpr.appendf(format, in0, in1);
226 0 : }
227 :
228 : /** 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);
232 :
233 : /** 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)
240 : */
241 : template <typename T0, typename T1>
242 : static Self Mul(T0 in0, T1 in1);
243 :
244 : /** 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, ...).
247 : */
248 : template <typename T0, typename T1>
249 : static Self Add(T0 in0, T1 in1);
250 :
251 : /** 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, ...).
254 : */
255 : template <typename T0, typename T1>
256 : static Self Sub(T0 in0, T1 in1);
257 :
258 : /** 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).
262 : */
263 : template <typename T>
264 : T extractComponents(const char format[]) const;
265 :
266 : private:
267 : enum ExprType {
268 : kZeros_ExprType,
269 : kOnes_ExprType,
270 : kFullExpr_ExprType,
271 : };
272 : ExprType fType;
273 : SkString fExpr;
274 : };
275 :
276 : class GrGLSLExpr1;
277 : class GrGLSLExpr4;
278 :
279 : /** Class representing a float GLSL expression. */
280 0 : class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
281 : public:
282 : GrGLSLExpr1()
283 : : INHERITED() {
284 : }
285 : explicit GrGLSLExpr1(int v)
286 : : INHERITED(v) {
287 : }
288 0 : GrGLSLExpr1(const char* expr)
289 0 : : INHERITED(expr) {
290 0 : }
291 : GrGLSLExpr1(const SkString& expr)
292 : : INHERITED(expr) {
293 : }
294 :
295 : static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
296 :
297 : private:
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 : }
304 :
305 : static const char* ZerosStr();
306 : static const char* OnesStr();
307 : static const char* CastStr();
308 : static const char* CastIntStr();
309 :
310 : 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);
313 :
314 : friend class GrGLSLExpr<GrGLSLExpr1>;
315 : friend class GrGLSLExpr<GrGLSLExpr4>;
316 :
317 : typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
318 : };
319 :
320 : /** Class representing a float vector (vec4) GLSL expression. */
321 0 : class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
322 : public:
323 0 : GrGLSLExpr4()
324 0 : : INHERITED() {
325 0 : }
326 0 : explicit GrGLSLExpr4(int v)
327 0 : : INHERITED(v) {
328 0 : }
329 0 : GrGLSLExpr4(const char* expr)
330 0 : : INHERITED(expr) {
331 0 : }
332 0 : GrGLSLExpr4(const SkString& expr)
333 0 : : INHERITED(expr) {
334 0 : }
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 :
343 : private:
344 0 : GrGLSLExpr4(const char format[], const char in0[])
345 0 : : INHERITED(format, in0) {
346 0 : }
347 0 : GrGLSLExpr4(const char format[], const char in0[], const char in1[])
348 0 : : INHERITED(format, in0, in1) {
349 0 : }
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 : };
373 :
374 : /**
375 : * Does an inplace mul, *=, of vec4VarName by mulFactor.
376 : * A semicolon is added after the assignment.
377 : */
378 : void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor);
379 :
380 : #include "GrGLSL_impl.h"
381 :
382 : #endif
|