blob: 2d6aaf08fa5c820dfe9047ffe63707fa269bbfc8 [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
Brian Salomonf9f45122016-11-29 11:59:17 -050028 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
29 using ImageStorageHandle = GrGLSLUniformHandler::ImageStorageHandle;
egdaniel09aa1fc2016-04-20 07:09:46 -070030
joshualitt30ba4362014-08-21 20:18:45 -070031 /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or
32 Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle
Brian Salomon101b8442016-11-18 11:58:54 -050033 order of the result depends on the GrProcessor::TextureSampler associated with the
34 SamplerHandle.
egdaniel7dc4bd02015-10-29 07:57:01 -070035 */
joshualitt30ba4362014-08-21 20:18:45 -070036 void appendTextureLookup(SkString* out,
egdaniel09aa1fc2016-04-20 07:09:46 -070037 SamplerHandle,
joshualitt30ba4362014-08-21 20:18:45 -070038 const char* coordName,
39 GrSLType coordType = kVec2f_GrSLType) const;
40
cdaltonf8a6ce82016-04-11 13:02:05 -070041 /** Version of above that appends the result to the shader code instead.*/
egdaniel09aa1fc2016-04-20 07:09:46 -070042 void appendTextureLookup(SamplerHandle,
joshualitt30ba4362014-08-21 20:18:45 -070043 const char* coordName,
brianosman77320db2016-09-07 08:09:10 -070044 GrSLType coordType = kVec2f_GrSLType,
45 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
joshualitt30ba4362014-08-21 20:18:45 -070046
47
48 /** Does the work of appendTextureLookup and modulates the result by modulation. The result is
Brian Salomon101b8442016-11-18 11:58:54 -050049 always a vec4. modulation and the swizzle specified by SamplerHandle must both be
egdaniel7dc4bd02015-10-29 07:57:01 -070050 vec4 or float. If modulation is "" or nullptr it this function acts as though
51 appendTextureLookup were called. */
joshualitt30ba4362014-08-21 20:18:45 -070052 void appendTextureLookupAndModulate(const char* modulation,
egdaniel09aa1fc2016-04-20 07:09:46 -070053 SamplerHandle,
joshualitt30ba4362014-08-21 20:18:45 -070054 const char* coordName,
brianosman77320db2016-09-07 08:09:10 -070055 GrSLType coordType = kVec2f_GrSLType,
56 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
57
58 /** Adds a helper function to facilitate color gamut transformation, and produces code that
59 returns the srcColor transformed into a new gamut (via multiplication by the xform from
60 colorXformHelper). Premultiplied sources are also handled correctly (colorXformHelper
61 determines if the source is premultipled or not). */
62 void appendColorGamutXform(SkString* out, const char* srcColor,
63 GrGLSLColorSpaceXformHelper* colorXformHelper);
64
65 /** Version of above that appends the result to the shader code instead. */
66 void appendColorGamutXform(const char* srcColor, GrGLSLColorSpaceXformHelper* colorXformHelper);
joshualitt30ba4362014-08-21 20:18:45 -070067
cdaltonf8a6ce82016-04-11 13:02:05 -070068 /** Fetches an unfiltered texel from a sampler at integer coordinates. coordExpr must match the
69 dimensionality of the sampler and must be within the sampler's range. coordExpr is emitted
70 exactly once, so expressions like "idx++" are acceptable. */
egdaniel09aa1fc2016-04-20 07:09:46 -070071 void appendTexelFetch(SkString* out, SamplerHandle, const char* coordExpr) const;
cdaltonf8a6ce82016-04-11 13:02:05 -070072
73 /** Version of above that appends the result to the shader code instead.*/
egdaniel09aa1fc2016-04-20 07:09:46 -070074 void appendTexelFetch(SamplerHandle, const char* coordExpr);
cdaltonf8a6ce82016-04-11 13:02:05 -070075
Brian Salomonf9f45122016-11-29 11:59:17 -050076 /** Creates a string of shader code that performs an image load. */
77 void appendImageStorageLoad(SkString* out, ImageStorageHandle, const char* coordExpr);
78 /** Version of above that appends the result to the shader code instead. */
79 void appendImageStorageLoad(ImageStorageHandle, const char* coordExpr);
80
joshualitt30ba4362014-08-21 20:18:45 -070081 /**
ethannicholas5961bc92016-10-12 06:39:56 -070082 * Adds a constant declaration to the top of the shader.
cdaltond36f2c42016-02-11 14:10:38 -080083 */
ethannicholas5961bc92016-10-12 06:39:56 -070084 void defineConstant(const char* type, const char* name, const char* value) {
85 this->definitions().appendf("const %s %s = %s;\n", type, name, value);
cdaltond36f2c42016-02-11 14:10:38 -080086 }
87
ethannicholas5961bc92016-10-12 06:39:56 -070088 void defineConstant(const char* name, int value) {
89 this->definitions().appendf("const int %s = %i;\n", name, value);
cdaltond36f2c42016-02-11 14:10:38 -080090 }
91
ethannicholas5961bc92016-10-12 06:39:56 -070092 void defineConstant(const char* name, float value) {
93 this->definitions().appendf("const float %s = %f;\n", name, value);
94 }
95
96 void defineConstantf(const char* type, const char* name, const char* fmt, ...) {
97 this->definitions().appendf("const %s %s = ", type, name);
cdaltond36f2c42016-02-11 14:10:38 -080098 va_list args;
ethannicholas5961bc92016-10-12 06:39:56 -070099 va_start(args, fmt);
100 this->definitions().appendVAList(fmt, args);
cdaltond36f2c42016-02-11 14:10:38 -0800101 va_end(args);
ethannicholas5961bc92016-10-12 06:39:56 -0700102 this->definitions().append(";\n");
cdaltond36f2c42016-02-11 14:10:38 -0800103 }
104
csmartdalton75864b02017-02-09 09:47:21 -0500105 void declareGlobal(const GrShaderVar&);
106
cdaltond36f2c42016-02-11 14:10:38 -0800107 /**
egdaniel57d3b032015-11-13 11:57:27 -0800108 * Called by GrGLSLProcessors to add code to one of the shaders.
joshualitt30ba4362014-08-21 20:18:45 -0700109 */
110 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
111 va_list args;
112 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800113 this->code().appendVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700114 va_end(args);
115 }
116
joshualitt43466a12015-02-13 17:18:27 -0800117 void codeAppend(const char* str) { this->code().append(str); }
joshualitt30ba4362014-08-21 20:18:45 -0700118
119 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
120 va_list args;
121 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800122 this->code().prependVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700123 va_end(args);
124 }
125
egdanielb2f94d12014-08-29 10:08:36 -0700126 /**
127 * Appends a variable declaration to one of the shaders
128 */
Brian Salomon99938a82016-11-21 13:41:08 -0500129 void declAppend(const GrShaderVar& var);
egdanielb2f94d12014-08-29 10:08:36 -0700130
joshualitt30ba4362014-08-21 20:18:45 -0700131 /** Emits a helper function outside of main() in the fragment shader. */
132 void emitFunction(GrSLType returnType,
133 const char* name,
134 int argCnt,
Brian Salomon99938a82016-11-21 13:41:08 -0500135 const GrShaderVar* args,
joshualitt30ba4362014-08-21 20:18:45 -0700136 const char* body,
137 SkString* outName);
138
139 /*
egdaniel574a4c12015-11-02 06:22:44 -0800140 * Combines the various parts of the shader to create a single finalized shader string.
141 */
142 void finalize(uint32_t visibility);
143
144 /*
joshualitt30ba4362014-08-21 20:18:45 -0700145 * Get parent builder for adding uniforms
146 */
egdaniel8dcdedc2015-11-11 06:27:20 -0800147 GrGLSLProgramBuilder* getProgramBuilder() { return fProgramBuilder; }
joshualitt30ba4362014-08-21 20:18:45 -0700148
149 /**
joshualitt47bb3822014-10-07 16:43:25 -0700150 * Helper for begining and ending a block in the shader code.
joshualitt30ba4362014-08-21 20:18:45 -0700151 */
152 class ShaderBlock {
153 public:
egdaniel2d721d32015-11-11 13:06:05 -0800154 ShaderBlock(GrGLSLShaderBuilder* builder) : fBuilder(builder) {
bsalomon49f085d2014-09-05 13:34:00 -0700155 SkASSERT(builder);
joshualitt30ba4362014-08-21 20:18:45 -0700156 fBuilder->codeAppend("{");
157 }
158
159 ~ShaderBlock() {
160 fBuilder->codeAppend("}");
161 }
162 private:
egdaniel2d721d32015-11-11 13:06:05 -0800163 GrGLSLShaderBuilder* fBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700164 };
joshualitt47bb3822014-10-07 16:43:25 -0700165
joshualitt30ba4362014-08-21 20:18:45 -0700166protected:
Brian Salomon99938a82016-11-21 13:41:08 -0500167 typedef GrTAllocator<GrShaderVar> VarArray;
joshualitt47bb3822014-10-07 16:43:25 -0700168 void appendDecls(const VarArray& vars, SkString* out) const;
joshualitt30ba4362014-08-21 20:18:45 -0700169
cdaltonc08f1962016-02-12 12:14:06 -0800170 /**
171 * Features that should only be enabled internally by the builders.
172 */
173 enum GLSLPrivateFeature {
174 kFragCoordConventions_GLSLPrivateFeature,
175 kBlendEquationAdvanced_GLSLPrivateFeature,
176 kBlendFuncExtended_GLSLPrivateFeature,
177 kExternalTexture_GLSLPrivateFeature,
cdalton74b8d322016-04-11 14:47:28 -0700178 kTexelBuffer_GLSLPrivateFeature,
cdaltonc08f1962016-02-12 12:14:06 -0800179 kFramebufferFetch_GLSLPrivateFeature,
180 kNoPerspectiveInterpolation_GLSLPrivateFeature,
cdalton33ad7012016-02-22 07:55:44 -0800181 kSampleVariables_GLSLPrivateFeature,
182 kSampleMaskOverrideCoverage_GLSLPrivateFeature,
183 kLastGLSLPrivateFeature = kSampleMaskOverrideCoverage_GLSLPrivateFeature
cdaltonc08f1962016-02-12 12:14:06 -0800184 };
185
joshualitt30ba4362014-08-21 20:18:45 -0700186 /*
joshualitt30ba4362014-08-21 20:18:45 -0700187 * A general function which enables an extension in a shader if the feature bit is not present
cdalton33ad7012016-02-22 07:55:44 -0800188 *
189 * @return true if the feature bit was not yet present, false otherwise.
joshualitt30ba4362014-08-21 20:18:45 -0700190 */
cdalton33ad7012016-02-22 07:55:44 -0800191 bool addFeature(uint32_t featureBit, const char* extensionName);
joshualitt30ba4362014-08-21 20:18:45 -0700192
cdaltone4017d82015-05-06 11:48:56 -0700193 enum InterfaceQualifier {
csmartdalton276cc412016-11-21 11:55:00 -0700194 kIn_InterfaceQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700195 kOut_InterfaceQualifier,
196 kLastInterfaceQualifier = kOut_InterfaceQualifier
197 };
198
199 /*
200 * A low level function to build default layout qualifiers.
201 *
202 * e.g. layout(param1, param2, ...) out;
203 *
204 * GLSL allows default layout qualifiers for in, out, and uniform.
205 */
206 void addLayoutQualifier(const char* param, InterfaceQualifier);
207
208 void compileAndAppendLayoutQualifiers();
209
joshualitt43466a12015-02-13 17:18:27 -0800210 void nextStage() {
211 fShaderStrings.push_back();
212 fCompilerStrings.push_back(this->code().c_str());
213 fCompilerStringLengths.push_back((int)this->code().size());
214 fCodeIndex++;
215 }
joshualittb8a82f22015-02-13 16:31:46 -0800216
joshualitt43466a12015-02-13 17:18:27 -0800217 SkString& versionDecl() { return fShaderStrings[kVersionDecl]; }
218 SkString& extensions() { return fShaderStrings[kExtensions]; }
cdaltond36f2c42016-02-11 14:10:38 -0800219 SkString& definitions() { return fShaderStrings[kDefinitions]; }
joshualitt43466a12015-02-13 17:18:27 -0800220 SkString& precisionQualifier() { return fShaderStrings[kPrecisionQualifier]; }
cdaltone4017d82015-05-06 11:48:56 -0700221 SkString& layoutQualifiers() { return fShaderStrings[kLayoutQualifiers]; }
joshualitt43466a12015-02-13 17:18:27 -0800222 SkString& uniforms() { return fShaderStrings[kUniforms]; }
223 SkString& inputs() { return fShaderStrings[kInputs]; }
224 SkString& outputs() { return fShaderStrings[kOutputs]; }
225 SkString& functions() { return fShaderStrings[kFunctions]; }
226 SkString& main() { return fShaderStrings[kMain]; }
227 SkString& code() { return fShaderStrings[fCodeIndex]; }
egdaniel574a4c12015-11-02 06:22:44 -0800228
229 virtual void onFinalize() = 0;
joshualitt43466a12015-02-13 17:18:27 -0800230
231 enum {
232 kVersionDecl,
233 kExtensions,
cdaltond36f2c42016-02-11 14:10:38 -0800234 kDefinitions,
joshualitt43466a12015-02-13 17:18:27 -0800235 kPrecisionQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700236 kLayoutQualifiers,
joshualitt43466a12015-02-13 17:18:27 -0800237 kUniforms,
238 kInputs,
239 kOutputs,
240 kFunctions,
241 kMain,
242 kCode,
243 };
244
egdaniel8dcdedc2015-11-11 06:27:20 -0800245 GrGLSLProgramBuilder* fProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800246 SkSTArray<kCode, const char*, true> fCompilerStrings;
247 SkSTArray<kCode, int, true> fCompilerStringLengths;
248 SkSTArray<kCode, SkString> fShaderStrings;
joshualitt30ba4362014-08-21 20:18:45 -0700249 SkString fCode;
250 SkString fFunctions;
251 SkString fExtensions;
252
253 VarArray fInputs;
254 VarArray fOutputs;
255 uint32_t fFeaturesAddedMask;
cdaltone4017d82015-05-06 11:48:56 -0700256 SkSTArray<1, SkString> fLayoutParams[kLastInterfaceQualifier + 1];
joshualitt43466a12015-02-13 17:18:27 -0800257 int fCodeIndex;
258 bool fFinalized;
259
egdanielfa896322016-01-13 12:19:30 -0800260 friend class GrGLSLProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800261 friend class GrGLProgramBuilder;
cdaltonc08f1962016-02-12 12:14:06 -0800262 friend class GrGLSLVaryingHandler; // to access noperspective interpolation feature.
kkinnunen7aedda52015-06-29 23:01:28 -0700263 friend class GrGLPathProgramBuilder; // to access fInputs.
egdaniel22281c12016-03-23 13:49:40 -0700264 friend class GrVkPipelineStateBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700265};
joshualitt30ba4362014-08-21 20:18:45 -0700266#endif