blob: a54bd3b3d742a7e76951fa242bd576a17efce64e [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
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000011#include "gl/GrGLInterface.h"
bsalomon@google.com018f1792013-04-18 19:36:09 +000012#include "GrColor.h"
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000013#include "GrTypesPriv.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000014
commit-bot@chromium.org06f05982013-08-30 15:52:36 +000015class GrGLContextInfo;
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000016class GrGLShaderVar;
bsalomon@google.com4af0af62012-08-29 12:59:57 +000017class SkString;
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000018
tomhudson@google.com086e5352011-12-08 14:44:10 +000019// Limited set of GLSL versions we build shaders for. Caller should round
20// down the GLSL version to one of these enums.
21enum GrGLSLGeneration {
22 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000023 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
tomhudson@google.com086e5352011-12-08 14:44:10 +000024 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000025 k110_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000026 /**
27 * Desktop GLSL 1.30
28 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000029 k130_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000030 /**
bsalomon@google.com281c7262012-10-23 14:31:30 +000031 * Desktop GLSL 1.40
32 */
33 k140_GrGLSLGeneration,
34 /**
35 * Desktop GLSL 1.50
tomhudson@google.com086e5352011-12-08 14:44:10 +000036 */
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000037 k150_GrGLSLGeneration,
tomhudson@google.com086e5352011-12-08 14:44:10 +000038};
39
bsalomon@google.com4af0af62012-08-29 12:59:57 +000040enum GrSLConstantVec {
41 kZeros_GrSLConstantVec,
42 kOnes_GrSLConstantVec,
43 kNone_GrSLConstantVec,
44};
45
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000046namespace {
humper@google.com05af1af2013-01-07 16:47:43 +000047static inline int GrSLTypeToVecLength(GrSLType type) {
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000048 static const int kVecLengths[] = {
49 0, // kVoid_GrSLType
50 1, // kFloat_GrSLType
51 2, // kVec2f_GrSLType
52 3, // kVec3f_GrSLType
53 4, // kVec4f_GrSLType
54 1, // kMat33f_GrSLType
55 1, // kMat44f_GrSLType
56 1, // kSampler2D_GrSLType
57 };
commit-bot@chromium.org5d7ca952013-04-22 20:26:44 +000058 GR_STATIC_ASSERT(kGrSLTypeCount == GR_ARRAY_COUNT(kVecLengths));
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000059 return kVecLengths[type];
60}
bsalomon@google.com4af0af62012-08-29 12:59:57 +000061
humper@google.com05af1af2013-01-07 16:47:43 +000062static inline const char* GrGLSLOnesVecf(int count) {
bsalomon@google.com4af0af62012-08-29 12:59:57 +000063 static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
64 "vec3(1,1,1)", "vec4(1,1,1,1)"};
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000065 SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC));
bsalomon@google.com4af0af62012-08-29 12:59:57 +000066 return kONESVEC[count];
67}
68
humper@google.com05af1af2013-01-07 16:47:43 +000069static inline const char* GrGLSLZerosVecf(int count) {
bsalomon@google.com4af0af62012-08-29 12:59:57 +000070 static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
71 "vec3(0,0,0)", "vec4(0,0,0,0)"};
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000072 SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC));
bsalomon@google.com4af0af62012-08-29 12:59:57 +000073 return kZEROSVEC[count];
74}
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000075}
76
tomhudson@google.com168e6342012-04-18 17:49:20 +000077/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000078 * Gets the most recent GLSL Generation compatible with the OpenGL context.
79 */
80GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
81 const GrGLInterface* gl);
82
83/**
bsalomon@google.com281c7262012-10-23 14:31:30 +000084 * Returns a string to include at the beginning of a shader to declare the GLSL
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000085 * version.
86 */
commit-bot@chromium.org06f05982013-08-30 15:52:36 +000087const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000088
89/**
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000090 * Depending on the GLSL version being emitted there may be an assumed output
91 * variable from the fragment shader for the color. Otherwise, the shader must
92 * declare an output variable for the color. If this function returns true:
93 * * Parameter var's name will be set to nameIfDeclared
94 * * The variable must be declared in the fragment shader
rmistry@google.comfbfcd562012-08-23 18:09:54 +000095 * * The variable has to be bound as the color output
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000096 * (using glBindFragDataLocation)
97 * If the function returns false:
98 * * Parameter var's name will be set to the GLSL built-in color output name.
99 * * Do not declare the variable in the shader.
100 * * Do not use glBindFragDataLocation to bind the variable
101 * In either case var is initialized to represent the color output in the
102 * shader.
103 */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000104bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000105 const char* nameIfDeclared,
106 GrGLShaderVar* var);
bsalomon@google.com018f1792013-04-18 19:36:09 +0000107/**
108 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
109 */
110static const char* GrGLSLTypeString(GrSLType t) {
111 switch (t) {
112 case kVoid_GrSLType:
113 return "void";
114 case kFloat_GrSLType:
115 return "float";
116 case kVec2f_GrSLType:
117 return "vec2";
118 case kVec3f_GrSLType:
119 return "vec3";
120 case kVec4f_GrSLType:
121 return "vec4";
122 case kMat33f_GrSLType:
123 return "mat3";
124 case kMat44f_GrSLType:
125 return "mat4";
126 case kSampler2D_GrSLType:
127 return "sampler2D";
128 default:
129 GrCrash("Unknown shader var type.");
130 return ""; // suppress warning
131 }
132}
tomhudson@google.com086e5352011-12-08 14:44:10 +0000133
bsalomon@google.com018f1792013-04-18 19:36:09 +0000134/** Return the type enum for a vector of floats of length n (1..4),
135 e.g. 1 -> "float", 2 -> "vec2", ... */
136static inline const char* GrGLSLFloatVectorTypeString(int n) {
137 return GrGLSLTypeString(GrSLFloatVectorType(n));
138}
tomhudson@google.com168e6342012-04-18 17:49:20 +0000139
140/** Return the GLSL swizzle operator for a homogenous component of a vector
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000141 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000142const char* GrGLSLVectorHomogCoord(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000143const char* GrGLSLVectorHomogCoord(GrSLType type);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000144
145/** Return the GLSL swizzle operator for a nonhomogenous components of a vector
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000146 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */
tomhudson@google.com168e6342012-04-18 17:49:20 +0000147const char* GrGLSLVectorNonhomogCoords(int count);
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000148const char* GrGLSLVectorNonhomogCoords(GrSLType type);
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000149
150/**
bsalomon@google.com018f1792013-04-18 19:36:09 +0000151 * Produces a string that is the result of modulating two inputs. The inputs must be vecN or
152 * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
153 * Either can be NULL or "" in which case the default params control whether a vector of ones or
154 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
155 * function determines that the result is a zeros or ones vec then any expression represented by
156 * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
157 * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
158 * by passing true for omitIfConstVec.
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000159 */
bsalomon@google.com018f1792013-04-18 19:36:09 +0000160template <int N>
161GrSLConstantVec GrGLSLModulatef(SkString* outAppend,
162 const char* in0,
163 const char* in1,
164 GrSLConstantVec default0 = kOnes_GrSLConstantVec,
165 GrSLConstantVec default1 = kOnes_GrSLConstantVec,
166 bool omitIfConstVec = false);
167
168/**
169 * Produces a string that is the result of adding two inputs. The inputs must be vecN or
170 * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
171 * Either can be NULL or "" in which case the default params control whether a vector of ones or
172 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
173 * function determines that the result is a zeros or ones vec then any expression represented by
174 * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
175 * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
176 * by passing true for omitIfConstVec.
177 */
178template <int N>
179GrSLConstantVec GrGLSLAddf(SkString* outAppend,
180 const char* in0,
181 const char* in1,
182 GrSLConstantVec default0 = kZeros_GrSLConstantVec,
183 GrSLConstantVec default1 = kZeros_GrSLConstantVec,
184 bool omitIfConstVec = false);
185
186/**
187 * Produces a string that is the result of subtracting two inputs. The inputs must be vecN or
188 * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
189 * Either can be NULL or "" in which case the default params control whether a vector of ones or
190 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
191 * function determines that the result is a zeros or ones vec then any expression represented by
192 * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
193 * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
194 * by passing true for omitIfConstVec.
195 */
196template <int N>
197GrSLConstantVec GrGLSLSubtractf(SkString* outAppend,
198 const char* in0,
199 const char* in1,
200 GrSLConstantVec default0 = kZeros_GrSLConstantVec,
201 GrSLConstantVec default1 = kZeros_GrSLConstantVec,
202 bool omitIfConstVec = false);
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000203
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000204/**
205 * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then
bsalomon@google.com281c7262012-10-23 14:31:30 +0000206 * mulFactor may be either "" or NULL. In this case either nothing will be appended (kOnes) or an
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000207 * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs.
208 * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert
bsalomon@google.comd698f772012-10-25 13:22:00 +0000209 * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the return value is
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000210 * kZeros, otherwise kNone.
211 */
212GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
213 int tabCnt,
214 const char* vec4VarName,
215 const char* mulFactor,
216 GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec);
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000217
218/**
bsalomon@google.com018f1792013-04-18 19:36:09 +0000219 * Given an expression that evaluates to a GLSL vec4, extract a component. If expr is NULL or ""
220 * the value of defaultExpr is used. It is an error to pass an empty expr and have set defaultExpr
221 * to kNone. The return value indicates whether the value is known to be 0 or 1. If omitIfConst is
222 * set then nothing is appended when the return is not kNone.
223 */
224GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend,
225 const char* expr,
226 GrColorComponentFlags component,
227 GrSLConstantVec defaultExpr = kNone_GrSLConstantVec,
228 bool omitIfConst = false);
229
230#include "GrGLSL_impl.h"
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000231
tomhudson@google.com086e5352011-12-08 14:44:10 +0000232#endif