Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: src/gpu/gl/GrGLSL.h

Issue 1202293002: Move GLSL-specific routines/classes to separate glsl directory (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rename GrGLGLSL.{cpp,h} Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/gl/GrGLProgramDataManager.h ('k') | src/gpu/gl/GrGLSL.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "gl/GrGLInterface.h"
12 #include "GrColor.h"
13 #include "GrTypesPriv.h"
14 #include "SkString.h"
15
16 class GrGLContextInfo;
17 class GrGLShaderVar;
18
19 // Limited set of GLSL versions we build shaders for. Caller should round
20 // down the GLSL version to one of these enums.
21 enum GrGLSLGeneration {
22 /**
23 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
24 */
25 k110_GrGLSLGeneration,
26 /**
27 * Desktop GLSL 1.30
28 */
29 k130_GrGLSLGeneration,
30 /**
31 * Desktop GLSL 1.40
32 */
33 k140_GrGLSLGeneration,
34 /**
35 * Desktop GLSL 1.50
36 */
37 k150_GrGLSLGeneration,
38 /**
39 * Desktop GLSL 3.30, and ES GLSL 3.00
40 */
41 k330_GrGLSLGeneration,
42 /**
43 * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular
44 */
45 k310es_GrGLSLGeneration,
46 };
47
48 /**
49 * Gets the most recent GLSL Generation compatible with the OpenGL context.
50 */
51 bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation);
52
53 bool GrGLSLSupportsNamedFragmentShaderOutputs(GrGLSLGeneration);
54
55 /**
56 * Returns a string to include at the beginning of a shader to declare the GLSL
57 * version.
58 */
59 const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
60
61 /**
62 * Adds a line of GLSL code to declare the default precision for float types.
63 */
64 void GrGLSLAppendDefaultFloatPrecisionDeclaration(GrSLPrecision, GrGLStandard, S kString* out);
65
66 /**
67 * Gets the name of the function that should be used to sample a 2D texture. Coo rd type is used
68 * to indicate whether the texture is sampled using projective textured (kVec3f) or not (kVec2f).
69 */
70 inline const char* GrGLSLTexture2DFunctionName(GrSLType coordType, GrGLSLGenerat ion glslGen) {
71 if (kVec2f_GrSLType == coordType) {
72 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D";
73 } else {
74 SkASSERT(kVec3f_GrSLType == coordType);
75 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj ";
76 }
77 }
78
79 /**
80 * Converts a GrSLType to a string containing the name of the equivalent GLSL ty pe.
81 */
82 static inline const char* GrGLSLTypeString(GrSLType t) {
83 switch (t) {
84 case kVoid_GrSLType:
85 return "void";
86 case kFloat_GrSLType:
87 return "float";
88 case kVec2f_GrSLType:
89 return "vec2";
90 case kVec3f_GrSLType:
91 return "vec3";
92 case kVec4f_GrSLType:
93 return "vec4";
94 case kMat33f_GrSLType:
95 return "mat3";
96 case kMat44f_GrSLType:
97 return "mat4";
98 case kSampler2D_GrSLType:
99 return "sampler2D";
100 default:
101 SkFAIL("Unknown shader var type.");
102 return ""; // suppress warning
103 }
104 }
105
106 /** A generic base-class representing a GLSL expression.
107 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does s imple constant
108 * folding with help of 1 and 0.
109 *
110 * Clients should not use this class, rather the specific instantiations defined
111 * later, for example GrGLSLExpr4.
112 */
113 template <typename Self>
114 class GrGLSLExpr {
115 public:
116 bool isOnes() const { return kOnes_ExprType == fType; }
117 bool isZeros() const { return kZeros_ExprType == fType; }
118
119 const char* c_str() const {
120 if (kZeros_ExprType == fType) {
121 return Self::ZerosStr();
122 } else if (kOnes_ExprType == fType) {
123 return Self::OnesStr();
124 }
125 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
126 return fExpr.c_str();
127 }
128
129 bool isValid() const {
130 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
131 }
132
133 protected:
134 /** Constructs an invalid expression.
135 * Useful only as a return value from functions that never actually return
136 * this and instances that will be assigned to later. */
137 GrGLSLExpr()
138 : fType(kFullExpr_ExprType) {
139 // The only constructor that is allowed to build an empty expression.
140 SkASSERT(!this->isValid());
141 }
142
143 /** Constructs an expression with all components as value v */
144 explicit GrGLSLExpr(int v) {
145 if (v == 0) {
146 fType = kZeros_ExprType;
147 } else if (v == 1) {
148 fType = kOnes_ExprType;
149 } else {
150 fType = kFullExpr_ExprType;
151 fExpr.appendf(Self::CastIntStr(), v);
152 }
153 }
154
155 /** Constructs an expression from a string.
156 * Argument expr is a simple expression or a parenthesized expression. */
157 // TODO: make explicit once effects input Exprs.
158 GrGLSLExpr(const char expr[]) {
159 if (NULL == expr) { // TODO: remove this once effects input Exprs.
160 fType = kOnes_ExprType;
161 } else {
162 fType = kFullExpr_ExprType;
163 fExpr = expr;
164 }
165 SkASSERT(this->isValid());
166 }
167
168 /** Constructs an expression from a string.
169 * Argument expr is a simple expression or a parenthesized expression. */
170 // TODO: make explicit once effects input Exprs.
171 GrGLSLExpr(const SkString& expr) {
172 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
173 fType = kOnes_ExprType;
174 } else {
175 fType = kFullExpr_ExprType;
176 fExpr = expr;
177 }
178 SkASSERT(this->isValid());
179 }
180
181 /** Constructs an expression from a string with one substitution. */
182 GrGLSLExpr(const char format[], const char in0[])
183 : fType(kFullExpr_ExprType) {
184 fExpr.appendf(format, in0);
185 }
186
187 /** Constructs an expression from a string with two substitutions. */
188 GrGLSLExpr(const char format[], const char in0[], const char in1[])
189 : fType(kFullExpr_ExprType) {
190 fExpr.appendf(format, in0, in1);
191 }
192
193 /** Returns expression casted to another type.
194 * Generic implementation that is called for non-trivial cases of casts. */
195 template <typename T>
196 static Self VectorCastImpl(const T& other);
197
198 /** Returns a GLSL multiplication: component-wise or component-by-scalar.
199 * The multiplication will be component-wise or multiply each component by a scalar.
200 *
201 * The returned expression will compute the value of:
202 * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
203 * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
204 * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
205 */
206 template <typename T0, typename T1>
207 static Self Mul(T0 in0, T1 in1);
208
209 /** Returns a GLSL addition: component-wise or add a scalar to each componen t.
210 * Return value computes:
211 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
212 */
213 template <typename T0, typename T1>
214 static Self Add(T0 in0, T1 in1);
215
216 /** Returns a GLSL subtraction: component-wise or subtract compoments by a s calar.
217 * Return value computes
218 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
219 */
220 template <typename T0, typename T1>
221 static Self Sub(T0 in0, T1 in1);
222
223 /** Returns expression that accesses component(s) of the expression.
224 * format should be the form "%s.x" where 'x' is the component(s) to access.
225 * Caller is responsible for making sure the amount of components in the
226 * format string is equal to dim(T).
227 */
228 template <typename T>
229 T extractComponents(const char format[]) const;
230
231 private:
232 enum ExprType {
233 kZeros_ExprType,
234 kOnes_ExprType,
235 kFullExpr_ExprType,
236 };
237 ExprType fType;
238 SkString fExpr;
239 };
240
241 class GrGLSLExpr1;
242 class GrGLSLExpr4;
243
244 /** Class representing a float GLSL expression. */
245 class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
246 public:
247 GrGLSLExpr1()
248 : INHERITED() {
249 }
250 explicit GrGLSLExpr1(int v)
251 : INHERITED(v) {
252 }
253 GrGLSLExpr1(const char* expr)
254 : INHERITED(expr) {
255 }
256 GrGLSLExpr1(const SkString& expr)
257 : INHERITED(expr) {
258 }
259
260 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
261
262 private:
263 GrGLSLExpr1(const char format[], const char in0[])
264 : INHERITED(format, in0) {
265 }
266 GrGLSLExpr1(const char format[], const char in0[], const char in1[])
267 : INHERITED(format, in0, in1) {
268 }
269
270 static const char* ZerosStr();
271 static const char* OnesStr();
272 static const char* CastStr();
273 static const char* CastIntStr();
274
275 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
276 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
277 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
278
279 friend class GrGLSLExpr<GrGLSLExpr1>;
280 friend class GrGLSLExpr<GrGLSLExpr4>;
281
282 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
283 };
284
285 /** Class representing a float vector (vec4) GLSL expression. */
286 class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
287 public:
288 GrGLSLExpr4()
289 : INHERITED() {
290 }
291 explicit GrGLSLExpr4(int v)
292 : INHERITED(v) {
293 }
294 GrGLSLExpr4(const char* expr)
295 : INHERITED(expr) {
296 }
297 GrGLSLExpr4(const SkString& expr)
298 : INHERITED(expr) {
299 }
300
301 typedef GrGLSLExpr1 AExpr;
302 AExpr a() const;
303
304 /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, fl oatv, floatv) */
305 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
306 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
307
308 private:
309 GrGLSLExpr4(const char format[], const char in0[])
310 : INHERITED(format, in0) {
311 }
312 GrGLSLExpr4(const char format[], const char in0[], const char in1[])
313 : INHERITED(format, in0, in1) {
314 }
315
316 static const char* ZerosStr();
317 static const char* OnesStr();
318 static const char* CastStr();
319 static const char* CastIntStr();
320
321 // The vector-by-scalar and scalar-by-vector binary operations.
322 friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
323 friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
324 friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
325 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
326 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
327 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
328
329 // The vector-by-vector, i.e. component-wise, binary operations.
330 friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
331 friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
332 friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
333
334 friend class GrGLSLExpr<GrGLSLExpr4>;
335
336 typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
337 };
338
339 /**
340 * Does an inplace mul, *=, of vec4VarName by mulFactor.
341 * A semicolon is added after the assignment.
342 */
343 void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSL Expr4& mulFactor);
344
345 #include "GrGLSL_impl.h"
346
347 #endif
OLDNEW
« no previous file with comments | « src/gpu/gl/GrGLProgramDataManager.h ('k') | src/gpu/gl/GrGLSL.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698