blob: 84592180592e2ac19e48de021103c83b74ef447e [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;
Greg Danielbc5d4d72017-05-05 10:28:42 -040029 using TexelBufferHandle = GrGLSLUniformHandler::TexelBufferHandle;
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,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040039 GrSLType coordType = kHalf2_GrSLType) const;
joshualitt30ba4362014-08-21 20:18:45 -070040
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,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040044 GrSLType coordType = kHalf2_GrSLType,
brianosman77320db2016-09-07 08:09:10 -070045 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
Ethan Nicholasf7b88202017-09-18 14:10:39 -040049 always a half4. modulation and the swizzle specified by SamplerHandle must both be
50 half4 or half. If modulation is "" or nullptr it this function acts as though
egdaniel7dc4bd02015-10-29 07:57:01 -070051 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,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040055 GrSLType coordType = kHalf2_GrSLType,
brianosman77320db2016-09-07 08:09:10 -070056 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. */
Greg Danielbc5d4d72017-05-05 10:28:42 -040071 void appendTexelFetch(SkString* out, TexelBufferHandle, const char* coordExpr) const;
cdaltonf8a6ce82016-04-11 13:02:05 -070072
73 /** Version of above that appends the result to the shader code instead.*/
Greg Danielbc5d4d72017-05-05 10:28:42 -040074 void appendTexelFetch(TexelBufferHandle, const char* coordExpr);
cdaltonf8a6ce82016-04-11 13:02:05 -070075
joshualitt30ba4362014-08-21 20:18:45 -070076 /**
ethannicholas5961bc92016-10-12 06:39:56 -070077 * Adds a constant declaration to the top of the shader.
cdaltond36f2c42016-02-11 14:10:38 -080078 */
ethannicholas5961bc92016-10-12 06:39:56 -070079 void defineConstant(const char* type, const char* name, const char* value) {
80 this->definitions().appendf("const %s %s = %s;\n", type, name, value);
cdaltond36f2c42016-02-11 14:10:38 -080081 }
82
ethannicholas5961bc92016-10-12 06:39:56 -070083 void defineConstant(const char* name, int value) {
84 this->definitions().appendf("const int %s = %i;\n", name, value);
cdaltond36f2c42016-02-11 14:10:38 -080085 }
86
ethannicholas5961bc92016-10-12 06:39:56 -070087 void defineConstant(const char* name, float value) {
88 this->definitions().appendf("const float %s = %f;\n", name, value);
89 }
90
91 void defineConstantf(const char* type, const char* name, const char* fmt, ...) {
92 this->definitions().appendf("const %s %s = ", type, name);
cdaltond36f2c42016-02-11 14:10:38 -080093 va_list args;
ethannicholas5961bc92016-10-12 06:39:56 -070094 va_start(args, fmt);
95 this->definitions().appendVAList(fmt, args);
cdaltond36f2c42016-02-11 14:10:38 -080096 va_end(args);
ethannicholas5961bc92016-10-12 06:39:56 -070097 this->definitions().append(";\n");
cdaltond36f2c42016-02-11 14:10:38 -080098 }
99
csmartdalton75864b02017-02-09 09:47:21 -0500100 void declareGlobal(const GrShaderVar&);
101
cdaltond36f2c42016-02-11 14:10:38 -0800102 /**
egdaniel57d3b032015-11-13 11:57:27 -0800103 * Called by GrGLSLProcessors to add code to one of the shaders.
joshualitt30ba4362014-08-21 20:18:45 -0700104 */
105 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
106 va_list args;
107 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800108 this->code().appendVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700109 va_end(args);
110 }
111
joshualitt43466a12015-02-13 17:18:27 -0800112 void codeAppend(const char* str) { this->code().append(str); }
joshualitt30ba4362014-08-21 20:18:45 -0700113
114 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
115 va_list args;
116 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800117 this->code().prependVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700118 va_end(args);
119 }
120
egdanielb2f94d12014-08-29 10:08:36 -0700121 /**
122 * Appends a variable declaration to one of the shaders
123 */
Brian Salomon99938a82016-11-21 13:41:08 -0500124 void declAppend(const GrShaderVar& var);
egdanielb2f94d12014-08-29 10:08:36 -0700125
joshualitt30ba4362014-08-21 20:18:45 -0700126 /** Emits a helper function outside of main() in the fragment shader. */
127 void emitFunction(GrSLType returnType,
128 const char* name,
129 int argCnt,
Brian Salomon99938a82016-11-21 13:41:08 -0500130 const GrShaderVar* args,
joshualitt30ba4362014-08-21 20:18:45 -0700131 const char* body,
132 SkString* outName);
133
134 /*
egdaniel574a4c12015-11-02 06:22:44 -0800135 * Combines the various parts of the shader to create a single finalized shader string.
136 */
137 void finalize(uint32_t visibility);
138
139 /*
joshualitt30ba4362014-08-21 20:18:45 -0700140 * Get parent builder for adding uniforms
141 */
egdaniel8dcdedc2015-11-11 06:27:20 -0800142 GrGLSLProgramBuilder* getProgramBuilder() { return fProgramBuilder; }
joshualitt30ba4362014-08-21 20:18:45 -0700143
144 /**
joshualitt47bb3822014-10-07 16:43:25 -0700145 * Helper for begining and ending a block in the shader code.
joshualitt30ba4362014-08-21 20:18:45 -0700146 */
147 class ShaderBlock {
148 public:
egdaniel2d721d32015-11-11 13:06:05 -0800149 ShaderBlock(GrGLSLShaderBuilder* builder) : fBuilder(builder) {
bsalomon49f085d2014-09-05 13:34:00 -0700150 SkASSERT(builder);
joshualitt30ba4362014-08-21 20:18:45 -0700151 fBuilder->codeAppend("{");
152 }
153
154 ~ShaderBlock() {
155 fBuilder->codeAppend("}");
156 }
157 private:
egdaniel2d721d32015-11-11 13:06:05 -0800158 GrGLSLShaderBuilder* fBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700159 };
joshualitt47bb3822014-10-07 16:43:25 -0700160
joshualitt30ba4362014-08-21 20:18:45 -0700161protected:
Brian Salomon99938a82016-11-21 13:41:08 -0500162 typedef GrTAllocator<GrShaderVar> VarArray;
joshualitt47bb3822014-10-07 16:43:25 -0700163 void appendDecls(const VarArray& vars, SkString* out) const;
joshualitt30ba4362014-08-21 20:18:45 -0700164
cdaltonc08f1962016-02-12 12:14:06 -0800165 /**
166 * Features that should only be enabled internally by the builders.
167 */
168 enum GLSLPrivateFeature {
169 kFragCoordConventions_GLSLPrivateFeature,
170 kBlendEquationAdvanced_GLSLPrivateFeature,
171 kBlendFuncExtended_GLSLPrivateFeature,
172 kExternalTexture_GLSLPrivateFeature,
cdalton74b8d322016-04-11 14:47:28 -0700173 kTexelBuffer_GLSLPrivateFeature,
cdaltonc08f1962016-02-12 12:14:06 -0800174 kFramebufferFetch_GLSLPrivateFeature,
175 kNoPerspectiveInterpolation_GLSLPrivateFeature,
Chris Dalton60283612018-02-14 13:38:14 -0700176 kLastGLSLPrivateFeature = kNoPerspectiveInterpolation_GLSLPrivateFeature
cdaltonc08f1962016-02-12 12:14:06 -0800177 };
178
joshualitt30ba4362014-08-21 20:18:45 -0700179 /*
joshualitt30ba4362014-08-21 20:18:45 -0700180 * A general function which enables an extension in a shader if the feature bit is not present
cdalton33ad7012016-02-22 07:55:44 -0800181 *
182 * @return true if the feature bit was not yet present, false otherwise.
joshualitt30ba4362014-08-21 20:18:45 -0700183 */
cdalton33ad7012016-02-22 07:55:44 -0800184 bool addFeature(uint32_t featureBit, const char* extensionName);
joshualitt30ba4362014-08-21 20:18:45 -0700185
cdaltone4017d82015-05-06 11:48:56 -0700186 enum InterfaceQualifier {
csmartdalton276cc412016-11-21 11:55:00 -0700187 kIn_InterfaceQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700188 kOut_InterfaceQualifier,
189 kLastInterfaceQualifier = kOut_InterfaceQualifier
190 };
191
192 /*
193 * A low level function to build default layout qualifiers.
194 *
195 * e.g. layout(param1, param2, ...) out;
196 *
197 * GLSL allows default layout qualifiers for in, out, and uniform.
198 */
199 void addLayoutQualifier(const char* param, InterfaceQualifier);
200
201 void compileAndAppendLayoutQualifiers();
202
joshualitt43466a12015-02-13 17:18:27 -0800203 void nextStage() {
204 fShaderStrings.push_back();
205 fCompilerStrings.push_back(this->code().c_str());
206 fCompilerStringLengths.push_back((int)this->code().size());
207 fCodeIndex++;
208 }
joshualittb8a82f22015-02-13 16:31:46 -0800209
joshualitt43466a12015-02-13 17:18:27 -0800210 SkString& versionDecl() { return fShaderStrings[kVersionDecl]; }
211 SkString& extensions() { return fShaderStrings[kExtensions]; }
cdaltond36f2c42016-02-11 14:10:38 -0800212 SkString& definitions() { return fShaderStrings[kDefinitions]; }
joshualitt43466a12015-02-13 17:18:27 -0800213 SkString& precisionQualifier() { return fShaderStrings[kPrecisionQualifier]; }
cdaltone4017d82015-05-06 11:48:56 -0700214 SkString& layoutQualifiers() { return fShaderStrings[kLayoutQualifiers]; }
joshualitt43466a12015-02-13 17:18:27 -0800215 SkString& uniforms() { return fShaderStrings[kUniforms]; }
216 SkString& inputs() { return fShaderStrings[kInputs]; }
217 SkString& outputs() { return fShaderStrings[kOutputs]; }
218 SkString& functions() { return fShaderStrings[kFunctions]; }
219 SkString& main() { return fShaderStrings[kMain]; }
220 SkString& code() { return fShaderStrings[fCodeIndex]; }
egdaniel574a4c12015-11-02 06:22:44 -0800221
222 virtual void onFinalize() = 0;
joshualitt43466a12015-02-13 17:18:27 -0800223
224 enum {
225 kVersionDecl,
226 kExtensions,
cdaltond36f2c42016-02-11 14:10:38 -0800227 kDefinitions,
joshualitt43466a12015-02-13 17:18:27 -0800228 kPrecisionQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700229 kLayoutQualifiers,
joshualitt43466a12015-02-13 17:18:27 -0800230 kUniforms,
231 kInputs,
232 kOutputs,
233 kFunctions,
234 kMain,
235 kCode,
236 };
237
egdaniel8dcdedc2015-11-11 06:27:20 -0800238 GrGLSLProgramBuilder* fProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800239 SkSTArray<kCode, const char*, true> fCompilerStrings;
240 SkSTArray<kCode, int, true> fCompilerStringLengths;
241 SkSTArray<kCode, SkString> fShaderStrings;
joshualitt30ba4362014-08-21 20:18:45 -0700242 SkString fCode;
243 SkString fFunctions;
244 SkString fExtensions;
245
246 VarArray fInputs;
247 VarArray fOutputs;
248 uint32_t fFeaturesAddedMask;
cdaltone4017d82015-05-06 11:48:56 -0700249 SkSTArray<1, SkString> fLayoutParams[kLastInterfaceQualifier + 1];
joshualitt43466a12015-02-13 17:18:27 -0800250 int fCodeIndex;
251 bool fFinalized;
252
egdanielfa896322016-01-13 12:19:30 -0800253 friend class GrGLSLProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800254 friend class GrGLProgramBuilder;
cdaltonc08f1962016-02-12 12:14:06 -0800255 friend class GrGLSLVaryingHandler; // to access noperspective interpolation feature.
kkinnunen7aedda52015-06-29 23:01:28 -0700256 friend class GrGLPathProgramBuilder; // to access fInputs.
egdaniel22281c12016-03-23 13:49:40 -0700257 friend class GrVkPipelineStateBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700258};
joshualitt30ba4362014-08-21 20:18:45 -0700259#endif