blob: 6be69d4a2bac603504bfcfbb98a8c8cf03ff4bc9 [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
2 * Copyright 2014 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
egdaniel2d721d32015-11-11 13:06:05 -08008#ifndef GrGLSLShaderBuilder_DEFINED
9#define GrGLSLShaderBuilder_DEFINED
joshualitt30ba4362014-08-21 20:18:45 -070010
egdaniel574a4c12015-11-02 06:22:44 -080011#include "GrAllocator.h"
Brian Salomon99938a82016-11-21 13:41:08 -050012#include "GrShaderVar.h"
egdaniel09aa1fc2016-04-20 07:09:46 -070013#include "glsl/GrGLSLUniformHandler.h"
egdaniel574a4c12015-11-02 06:22:44 -080014#include "SkTDArray.h"
joshualitt30ba4362014-08-21 20:18:45 -070015
16#include <stdarg.h>
17
brianosman77320db2016-09-07 08:09:10 -070018class GrGLSLColorSpaceXformHelper;
19
joshualitt30ba4362014-08-21 20:18:45 -070020/**
21 base class for all shaders builders
22*/
egdaniel2d721d32015-11-11 13:06:05 -080023class GrGLSLShaderBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070024public:
egdaniel2d721d32015-11-11 13:06:05 -080025 GrGLSLShaderBuilder(GrGLSLProgramBuilder* program);
26 virtual ~GrGLSLShaderBuilder() {}
joshualitt30ba4362014-08-21 20:18:45 -070027
egdaniel09aa1fc2016-04-20 07:09:46 -070028 typedef GrGLSLUniformHandler::SamplerHandle SamplerHandle;
29
joshualitt30ba4362014-08-21 20:18:45 -070030 /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or
31 Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle
Brian Salomon101b8442016-11-18 11:58:54 -050032 order of the result depends on the GrProcessor::TextureSampler associated with the
33 SamplerHandle.
egdaniel7dc4bd02015-10-29 07:57:01 -070034 */
joshualitt30ba4362014-08-21 20:18:45 -070035 void appendTextureLookup(SkString* out,
egdaniel09aa1fc2016-04-20 07:09:46 -070036 SamplerHandle,
joshualitt30ba4362014-08-21 20:18:45 -070037 const char* coordName,
38 GrSLType coordType = kVec2f_GrSLType) const;
39
cdaltonf8a6ce82016-04-11 13:02:05 -070040 /** Version of above that appends the result to the shader code instead.*/
egdaniel09aa1fc2016-04-20 07:09:46 -070041 void appendTextureLookup(SamplerHandle,
joshualitt30ba4362014-08-21 20:18:45 -070042 const char* coordName,
brianosman77320db2016-09-07 08:09:10 -070043 GrSLType coordType = kVec2f_GrSLType,
44 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
joshualitt30ba4362014-08-21 20:18:45 -070045
46
47 /** Does the work of appendTextureLookup and modulates the result by modulation. The result is
Brian Salomon101b8442016-11-18 11:58:54 -050048 always a vec4. modulation and the swizzle specified by SamplerHandle must both be
egdaniel7dc4bd02015-10-29 07:57:01 -070049 vec4 or float. If modulation is "" or nullptr it this function acts as though
50 appendTextureLookup were called. */
joshualitt30ba4362014-08-21 20:18:45 -070051 void appendTextureLookupAndModulate(const char* modulation,
egdaniel09aa1fc2016-04-20 07:09:46 -070052 SamplerHandle,
joshualitt30ba4362014-08-21 20:18:45 -070053 const char* coordName,
brianosman77320db2016-09-07 08:09:10 -070054 GrSLType coordType = kVec2f_GrSLType,
55 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
56
57 /** Adds a helper function to facilitate color gamut transformation, and produces code that
58 returns the srcColor transformed into a new gamut (via multiplication by the xform from
59 colorXformHelper). Premultiplied sources are also handled correctly (colorXformHelper
60 determines if the source is premultipled or not). */
61 void appendColorGamutXform(SkString* out, const char* srcColor,
62 GrGLSLColorSpaceXformHelper* colorXformHelper);
63
64 /** Version of above that appends the result to the shader code instead. */
65 void appendColorGamutXform(const char* srcColor, GrGLSLColorSpaceXformHelper* colorXformHelper);
joshualitt30ba4362014-08-21 20:18:45 -070066
cdaltonf8a6ce82016-04-11 13:02:05 -070067 /** Fetches an unfiltered texel from a sampler at integer coordinates. coordExpr must match the
68 dimensionality of the sampler and must be within the sampler's range. coordExpr is emitted
69 exactly once, so expressions like "idx++" are acceptable. */
egdaniel09aa1fc2016-04-20 07:09:46 -070070 void appendTexelFetch(SkString* out, SamplerHandle, const char* coordExpr) const;
cdaltonf8a6ce82016-04-11 13:02:05 -070071
72 /** Version of above that appends the result to the shader code instead.*/
egdaniel09aa1fc2016-04-20 07:09:46 -070073 void appendTexelFetch(SamplerHandle, const char* coordExpr);
cdaltonf8a6ce82016-04-11 13:02:05 -070074
joshualitt30ba4362014-08-21 20:18:45 -070075 /**
ethannicholas5961bc92016-10-12 06:39:56 -070076 * Adds a constant declaration to the top of the shader.
cdaltond36f2c42016-02-11 14:10:38 -080077 */
ethannicholas5961bc92016-10-12 06:39:56 -070078 void defineConstant(const char* type, const char* name, const char* value) {
79 this->definitions().appendf("const %s %s = %s;\n", type, name, value);
cdaltond36f2c42016-02-11 14:10:38 -080080 }
81
ethannicholas5961bc92016-10-12 06:39:56 -070082 void defineConstant(const char* name, int value) {
83 this->definitions().appendf("const int %s = %i;\n", name, value);
cdaltond36f2c42016-02-11 14:10:38 -080084 }
85
ethannicholas5961bc92016-10-12 06:39:56 -070086 void defineConstant(const char* name, float value) {
87 this->definitions().appendf("const float %s = %f;\n", name, value);
88 }
89
90 void defineConstantf(const char* type, const char* name, const char* fmt, ...) {
91 this->definitions().appendf("const %s %s = ", type, name);
cdaltond36f2c42016-02-11 14:10:38 -080092 va_list args;
ethannicholas5961bc92016-10-12 06:39:56 -070093 va_start(args, fmt);
94 this->definitions().appendVAList(fmt, args);
cdaltond36f2c42016-02-11 14:10:38 -080095 va_end(args);
ethannicholas5961bc92016-10-12 06:39:56 -070096 this->definitions().append(";\n");
cdaltond36f2c42016-02-11 14:10:38 -080097 }
98
99 /**
egdaniel57d3b032015-11-13 11:57:27 -0800100 * Called by GrGLSLProcessors to add code to one of the shaders.
joshualitt30ba4362014-08-21 20:18:45 -0700101 */
102 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
103 va_list args;
104 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800105 this->code().appendVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700106 va_end(args);
107 }
108
joshualitt43466a12015-02-13 17:18:27 -0800109 void codeAppend(const char* str) { this->code().append(str); }
joshualitt30ba4362014-08-21 20:18:45 -0700110
111 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
112 va_list args;
113 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800114 this->code().prependVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700115 va_end(args);
116 }
117
egdanielb2f94d12014-08-29 10:08:36 -0700118 /**
119 * Appends a variable declaration to one of the shaders
120 */
Brian Salomon99938a82016-11-21 13:41:08 -0500121 void declAppend(const GrShaderVar& var);
egdanielb2f94d12014-08-29 10:08:36 -0700122
cdalton1f50acf2016-04-11 11:30:50 -0700123 /**
124 * Appends a precision qualifier followed by a space, if relevant for the GLSL version.
125 */
126 void appendPrecisionModifier(GrSLPrecision);
127
joshualitt30ba4362014-08-21 20:18:45 -0700128 /** Emits a helper function outside of main() in the fragment shader. */
129 void emitFunction(GrSLType returnType,
130 const char* name,
131 int argCnt,
Brian Salomon99938a82016-11-21 13:41:08 -0500132 const GrShaderVar* args,
joshualitt30ba4362014-08-21 20:18:45 -0700133 const char* body,
134 SkString* outName);
135
136 /*
egdaniel574a4c12015-11-02 06:22:44 -0800137 * Combines the various parts of the shader to create a single finalized shader string.
138 */
139 void finalize(uint32_t visibility);
140
141 /*
joshualitt30ba4362014-08-21 20:18:45 -0700142 * Get parent builder for adding uniforms
143 */
egdaniel8dcdedc2015-11-11 06:27:20 -0800144 GrGLSLProgramBuilder* getProgramBuilder() { return fProgramBuilder; }
joshualitt30ba4362014-08-21 20:18:45 -0700145
146 /**
joshualitt47bb3822014-10-07 16:43:25 -0700147 * Helper for begining and ending a block in the shader code.
joshualitt30ba4362014-08-21 20:18:45 -0700148 */
149 class ShaderBlock {
150 public:
egdaniel2d721d32015-11-11 13:06:05 -0800151 ShaderBlock(GrGLSLShaderBuilder* builder) : fBuilder(builder) {
bsalomon49f085d2014-09-05 13:34:00 -0700152 SkASSERT(builder);
joshualitt30ba4362014-08-21 20:18:45 -0700153 fBuilder->codeAppend("{");
154 }
155
156 ~ShaderBlock() {
157 fBuilder->codeAppend("}");
158 }
159 private:
egdaniel2d721d32015-11-11 13:06:05 -0800160 GrGLSLShaderBuilder* fBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700161 };
joshualitt47bb3822014-10-07 16:43:25 -0700162
joshualitt30ba4362014-08-21 20:18:45 -0700163protected:
Brian Salomon99938a82016-11-21 13:41:08 -0500164 typedef GrTAllocator<GrShaderVar> VarArray;
joshualitt47bb3822014-10-07 16:43:25 -0700165 void appendDecls(const VarArray& vars, SkString* out) const;
joshualitt30ba4362014-08-21 20:18:45 -0700166
cdaltonc08f1962016-02-12 12:14:06 -0800167 /**
168 * Features that should only be enabled internally by the builders.
169 */
170 enum GLSLPrivateFeature {
171 kFragCoordConventions_GLSLPrivateFeature,
172 kBlendEquationAdvanced_GLSLPrivateFeature,
173 kBlendFuncExtended_GLSLPrivateFeature,
174 kExternalTexture_GLSLPrivateFeature,
cdalton74b8d322016-04-11 14:47:28 -0700175 kTexelBuffer_GLSLPrivateFeature,
cdaltonc08f1962016-02-12 12:14:06 -0800176 kFramebufferFetch_GLSLPrivateFeature,
177 kNoPerspectiveInterpolation_GLSLPrivateFeature,
cdalton33ad7012016-02-22 07:55:44 -0800178 kSampleVariables_GLSLPrivateFeature,
179 kSampleMaskOverrideCoverage_GLSLPrivateFeature,
180 kLastGLSLPrivateFeature = kSampleMaskOverrideCoverage_GLSLPrivateFeature
cdaltonc08f1962016-02-12 12:14:06 -0800181 };
182
joshualitt30ba4362014-08-21 20:18:45 -0700183 /*
joshualitt30ba4362014-08-21 20:18:45 -0700184 * A general function which enables an extension in a shader if the feature bit is not present
cdalton33ad7012016-02-22 07:55:44 -0800185 *
186 * @return true if the feature bit was not yet present, false otherwise.
joshualitt30ba4362014-08-21 20:18:45 -0700187 */
cdalton33ad7012016-02-22 07:55:44 -0800188 bool addFeature(uint32_t featureBit, const char* extensionName);
joshualitt30ba4362014-08-21 20:18:45 -0700189
cdaltone4017d82015-05-06 11:48:56 -0700190 enum InterfaceQualifier {
csmartdalton276cc412016-11-21 11:55:00 -0700191 kIn_InterfaceQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700192 kOut_InterfaceQualifier,
193 kLastInterfaceQualifier = kOut_InterfaceQualifier
194 };
195
196 /*
197 * A low level function to build default layout qualifiers.
198 *
199 * e.g. layout(param1, param2, ...) out;
200 *
201 * GLSL allows default layout qualifiers for in, out, and uniform.
202 */
203 void addLayoutQualifier(const char* param, InterfaceQualifier);
204
205 void compileAndAppendLayoutQualifiers();
206
joshualitt43466a12015-02-13 17:18:27 -0800207 void nextStage() {
208 fShaderStrings.push_back();
209 fCompilerStrings.push_back(this->code().c_str());
210 fCompilerStringLengths.push_back((int)this->code().size());
211 fCodeIndex++;
212 }
joshualittb8a82f22015-02-13 16:31:46 -0800213
joshualitt43466a12015-02-13 17:18:27 -0800214 SkString& versionDecl() { return fShaderStrings[kVersionDecl]; }
215 SkString& extensions() { return fShaderStrings[kExtensions]; }
cdaltond36f2c42016-02-11 14:10:38 -0800216 SkString& definitions() { return fShaderStrings[kDefinitions]; }
joshualitt43466a12015-02-13 17:18:27 -0800217 SkString& precisionQualifier() { return fShaderStrings[kPrecisionQualifier]; }
cdaltone4017d82015-05-06 11:48:56 -0700218 SkString& layoutQualifiers() { return fShaderStrings[kLayoutQualifiers]; }
joshualitt43466a12015-02-13 17:18:27 -0800219 SkString& uniforms() { return fShaderStrings[kUniforms]; }
220 SkString& inputs() { return fShaderStrings[kInputs]; }
221 SkString& outputs() { return fShaderStrings[kOutputs]; }
222 SkString& functions() { return fShaderStrings[kFunctions]; }
223 SkString& main() { return fShaderStrings[kMain]; }
224 SkString& code() { return fShaderStrings[fCodeIndex]; }
egdaniel574a4c12015-11-02 06:22:44 -0800225
226 virtual void onFinalize() = 0;
joshualitt43466a12015-02-13 17:18:27 -0800227
228 enum {
229 kVersionDecl,
230 kExtensions,
cdaltond36f2c42016-02-11 14:10:38 -0800231 kDefinitions,
joshualitt43466a12015-02-13 17:18:27 -0800232 kPrecisionQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700233 kLayoutQualifiers,
joshualitt43466a12015-02-13 17:18:27 -0800234 kUniforms,
235 kInputs,
236 kOutputs,
237 kFunctions,
238 kMain,
239 kCode,
240 };
241
egdaniel8dcdedc2015-11-11 06:27:20 -0800242 GrGLSLProgramBuilder* fProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800243 SkSTArray<kCode, const char*, true> fCompilerStrings;
244 SkSTArray<kCode, int, true> fCompilerStringLengths;
245 SkSTArray<kCode, SkString> fShaderStrings;
joshualitt30ba4362014-08-21 20:18:45 -0700246 SkString fCode;
247 SkString fFunctions;
248 SkString fExtensions;
249
250 VarArray fInputs;
251 VarArray fOutputs;
252 uint32_t fFeaturesAddedMask;
cdaltone4017d82015-05-06 11:48:56 -0700253 SkSTArray<1, SkString> fLayoutParams[kLastInterfaceQualifier + 1];
joshualitt43466a12015-02-13 17:18:27 -0800254 int fCodeIndex;
255 bool fFinalized;
256
egdanielfa896322016-01-13 12:19:30 -0800257 friend class GrGLSLProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800258 friend class GrGLProgramBuilder;
cdaltonc08f1962016-02-12 12:14:06 -0800259 friend class GrGLSLVaryingHandler; // to access noperspective interpolation feature.
kkinnunen7aedda52015-06-29 23:01:28 -0700260 friend class GrGLPathProgramBuilder; // to access fInputs.
egdaniel22281c12016-03-23 13:49:40 -0700261 friend class GrVkPipelineStateBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700262};
joshualitt30ba4362014-08-21 20:18:45 -0700263#endif