blob: 983d50b92671ced5ac47f00218ade527d8383fb8 [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
105 /**
egdaniel57d3b032015-11-13 11:57:27 -0800106 * Called by GrGLSLProcessors to add code to one of the shaders.
joshualitt30ba4362014-08-21 20:18:45 -0700107 */
108 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
109 va_list args;
110 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800111 this->code().appendVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700112 va_end(args);
113 }
114
joshualitt43466a12015-02-13 17:18:27 -0800115 void codeAppend(const char* str) { this->code().append(str); }
joshualitt30ba4362014-08-21 20:18:45 -0700116
117 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
118 va_list args;
119 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800120 this->code().prependVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700121 va_end(args);
122 }
123
egdanielb2f94d12014-08-29 10:08:36 -0700124 /**
125 * Appends a variable declaration to one of the shaders
126 */
Brian Salomon99938a82016-11-21 13:41:08 -0500127 void declAppend(const GrShaderVar& var);
egdanielb2f94d12014-08-29 10:08:36 -0700128
joshualitt30ba4362014-08-21 20:18:45 -0700129 /** Emits a helper function outside of main() in the fragment shader. */
130 void emitFunction(GrSLType returnType,
131 const char* name,
132 int argCnt,
Brian Salomon99938a82016-11-21 13:41:08 -0500133 const GrShaderVar* args,
joshualitt30ba4362014-08-21 20:18:45 -0700134 const char* body,
135 SkString* outName);
136
137 /*
egdaniel574a4c12015-11-02 06:22:44 -0800138 * Combines the various parts of the shader to create a single finalized shader string.
139 */
140 void finalize(uint32_t visibility);
141
142 /*
joshualitt30ba4362014-08-21 20:18:45 -0700143 * Get parent builder for adding uniforms
144 */
egdaniel8dcdedc2015-11-11 06:27:20 -0800145 GrGLSLProgramBuilder* getProgramBuilder() { return fProgramBuilder; }
joshualitt30ba4362014-08-21 20:18:45 -0700146
147 /**
joshualitt47bb3822014-10-07 16:43:25 -0700148 * Helper for begining and ending a block in the shader code.
joshualitt30ba4362014-08-21 20:18:45 -0700149 */
150 class ShaderBlock {
151 public:
egdaniel2d721d32015-11-11 13:06:05 -0800152 ShaderBlock(GrGLSLShaderBuilder* builder) : fBuilder(builder) {
bsalomon49f085d2014-09-05 13:34:00 -0700153 SkASSERT(builder);
joshualitt30ba4362014-08-21 20:18:45 -0700154 fBuilder->codeAppend("{");
155 }
156
157 ~ShaderBlock() {
158 fBuilder->codeAppend("}");
159 }
160 private:
egdaniel2d721d32015-11-11 13:06:05 -0800161 GrGLSLShaderBuilder* fBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700162 };
joshualitt47bb3822014-10-07 16:43:25 -0700163
joshualitt30ba4362014-08-21 20:18:45 -0700164protected:
Brian Salomon99938a82016-11-21 13:41:08 -0500165 typedef GrTAllocator<GrShaderVar> VarArray;
joshualitt47bb3822014-10-07 16:43:25 -0700166 void appendDecls(const VarArray& vars, SkString* out) const;
joshualitt30ba4362014-08-21 20:18:45 -0700167
cdaltonc08f1962016-02-12 12:14:06 -0800168 /**
169 * Features that should only be enabled internally by the builders.
170 */
171 enum GLSLPrivateFeature {
172 kFragCoordConventions_GLSLPrivateFeature,
173 kBlendEquationAdvanced_GLSLPrivateFeature,
174 kBlendFuncExtended_GLSLPrivateFeature,
175 kExternalTexture_GLSLPrivateFeature,
cdalton74b8d322016-04-11 14:47:28 -0700176 kTexelBuffer_GLSLPrivateFeature,
cdaltonc08f1962016-02-12 12:14:06 -0800177 kFramebufferFetch_GLSLPrivateFeature,
178 kNoPerspectiveInterpolation_GLSLPrivateFeature,
cdalton33ad7012016-02-22 07:55:44 -0800179 kSampleVariables_GLSLPrivateFeature,
180 kSampleMaskOverrideCoverage_GLSLPrivateFeature,
181 kLastGLSLPrivateFeature = kSampleMaskOverrideCoverage_GLSLPrivateFeature
cdaltonc08f1962016-02-12 12:14:06 -0800182 };
183
joshualitt30ba4362014-08-21 20:18:45 -0700184 /*
joshualitt30ba4362014-08-21 20:18:45 -0700185 * A general function which enables an extension in a shader if the feature bit is not present
cdalton33ad7012016-02-22 07:55:44 -0800186 *
187 * @return true if the feature bit was not yet present, false otherwise.
joshualitt30ba4362014-08-21 20:18:45 -0700188 */
cdalton33ad7012016-02-22 07:55:44 -0800189 bool addFeature(uint32_t featureBit, const char* extensionName);
joshualitt30ba4362014-08-21 20:18:45 -0700190
cdaltone4017d82015-05-06 11:48:56 -0700191 enum InterfaceQualifier {
csmartdalton276cc412016-11-21 11:55:00 -0700192 kIn_InterfaceQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700193 kOut_InterfaceQualifier,
194 kLastInterfaceQualifier = kOut_InterfaceQualifier
195 };
196
197 /*
198 * A low level function to build default layout qualifiers.
199 *
200 * e.g. layout(param1, param2, ...) out;
201 *
202 * GLSL allows default layout qualifiers for in, out, and uniform.
203 */
204 void addLayoutQualifier(const char* param, InterfaceQualifier);
205
206 void compileAndAppendLayoutQualifiers();
207
joshualitt43466a12015-02-13 17:18:27 -0800208 void nextStage() {
209 fShaderStrings.push_back();
210 fCompilerStrings.push_back(this->code().c_str());
211 fCompilerStringLengths.push_back((int)this->code().size());
212 fCodeIndex++;
213 }
joshualittb8a82f22015-02-13 16:31:46 -0800214
joshualitt43466a12015-02-13 17:18:27 -0800215 SkString& versionDecl() { return fShaderStrings[kVersionDecl]; }
216 SkString& extensions() { return fShaderStrings[kExtensions]; }
cdaltond36f2c42016-02-11 14:10:38 -0800217 SkString& definitions() { return fShaderStrings[kDefinitions]; }
joshualitt43466a12015-02-13 17:18:27 -0800218 SkString& precisionQualifier() { return fShaderStrings[kPrecisionQualifier]; }
cdaltone4017d82015-05-06 11:48:56 -0700219 SkString& layoutQualifiers() { return fShaderStrings[kLayoutQualifiers]; }
joshualitt43466a12015-02-13 17:18:27 -0800220 SkString& uniforms() { return fShaderStrings[kUniforms]; }
221 SkString& inputs() { return fShaderStrings[kInputs]; }
222 SkString& outputs() { return fShaderStrings[kOutputs]; }
223 SkString& functions() { return fShaderStrings[kFunctions]; }
224 SkString& main() { return fShaderStrings[kMain]; }
225 SkString& code() { return fShaderStrings[fCodeIndex]; }
egdaniel574a4c12015-11-02 06:22:44 -0800226
227 virtual void onFinalize() = 0;
joshualitt43466a12015-02-13 17:18:27 -0800228
229 enum {
230 kVersionDecl,
231 kExtensions,
cdaltond36f2c42016-02-11 14:10:38 -0800232 kDefinitions,
joshualitt43466a12015-02-13 17:18:27 -0800233 kPrecisionQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700234 kLayoutQualifiers,
joshualitt43466a12015-02-13 17:18:27 -0800235 kUniforms,
236 kInputs,
237 kOutputs,
238 kFunctions,
239 kMain,
240 kCode,
241 };
242
egdaniel8dcdedc2015-11-11 06:27:20 -0800243 GrGLSLProgramBuilder* fProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800244 SkSTArray<kCode, const char*, true> fCompilerStrings;
245 SkSTArray<kCode, int, true> fCompilerStringLengths;
246 SkSTArray<kCode, SkString> fShaderStrings;
joshualitt30ba4362014-08-21 20:18:45 -0700247 SkString fCode;
248 SkString fFunctions;
249 SkString fExtensions;
250
251 VarArray fInputs;
252 VarArray fOutputs;
253 uint32_t fFeaturesAddedMask;
cdaltone4017d82015-05-06 11:48:56 -0700254 SkSTArray<1, SkString> fLayoutParams[kLastInterfaceQualifier + 1];
joshualitt43466a12015-02-13 17:18:27 -0800255 int fCodeIndex;
256 bool fFinalized;
257
egdanielfa896322016-01-13 12:19:30 -0800258 friend class GrGLSLProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800259 friend class GrGLProgramBuilder;
cdaltonc08f1962016-02-12 12:14:06 -0800260 friend class GrGLSLVaryingHandler; // to access noperspective interpolation feature.
kkinnunen7aedda52015-06-29 23:01:28 -0700261 friend class GrGLPathProgramBuilder; // to access fInputs.
egdaniel22281c12016-03-23 13:49:40 -0700262 friend class GrVkPipelineStateBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700263};
joshualitt30ba4362014-08-21 20:18:45 -0700264#endif