blob: 937a8719d1604a5defef64058bf57b70b585cd76 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkTDArray.h"
12#include "src/gpu/GrAllocator.h"
13#include "src/gpu/GrShaderVar.h"
14#include "src/gpu/glsl/GrGLSLUniformHandler.h"
15#include "src/sksl/SkSLString.h"
joshualitt30ba4362014-08-21 20:18:45 -070016
17#include <stdarg.h>
18
brianosman77320db2016-09-07 08:09:10 -070019class GrGLSLColorSpaceXformHelper;
20
joshualitt30ba4362014-08-21 20:18:45 -070021/**
22 base class for all shaders builders
23*/
egdaniel2d721d32015-11-11 13:06:05 -080024class GrGLSLShaderBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070025public:
egdaniel2d721d32015-11-11 13:06:05 -080026 GrGLSLShaderBuilder(GrGLSLProgramBuilder* program);
27 virtual ~GrGLSLShaderBuilder() {}
joshualitt30ba4362014-08-21 20:18:45 -070028
Brian Salomonf9f45122016-11-29 11:59:17 -050029 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
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
Brian Salomon87e9ddb2019-12-19 14:50:22 -050047 /** Does the work of appendTextureLookup and blends the result by dst, treating the texture
48 lookup a the src input to the blend. The dst is assumed to be half4 and the result is always
49 a half4. If dst is nullptr we use half4(1) as the blend dst. */
50 void appendTextureLookupAndBlend(const char* dst,
51 SkBlendMode,
52 SamplerHandle,
53 const char* coordName,
54 GrSLType coordType = kHalf2_GrSLType,
55 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
brianosman77320db2016-09-07 08:09:10 -070056
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
joshualitt30ba4362014-08-21 20:18:45 -070067 /**
ethannicholas5961bc92016-10-12 06:39:56 -070068 * Adds a constant declaration to the top of the shader.
cdaltond36f2c42016-02-11 14:10:38 -080069 */
ethannicholas5961bc92016-10-12 06:39:56 -070070 void defineConstant(const char* type, const char* name, const char* value) {
71 this->definitions().appendf("const %s %s = %s;\n", type, name, value);
cdaltond36f2c42016-02-11 14:10:38 -080072 }
73
ethannicholas5961bc92016-10-12 06:39:56 -070074 void defineConstant(const char* name, int value) {
75 this->definitions().appendf("const int %s = %i;\n", name, value);
cdaltond36f2c42016-02-11 14:10:38 -080076 }
77
ethannicholas5961bc92016-10-12 06:39:56 -070078 void defineConstant(const char* name, float value) {
79 this->definitions().appendf("const float %s = %f;\n", name, value);
80 }
81
82 void defineConstantf(const char* type, const char* name, const char* fmt, ...) {
83 this->definitions().appendf("const %s %s = ", type, name);
cdaltond36f2c42016-02-11 14:10:38 -080084 va_list args;
ethannicholas5961bc92016-10-12 06:39:56 -070085 va_start(args, fmt);
86 this->definitions().appendVAList(fmt, args);
cdaltond36f2c42016-02-11 14:10:38 -080087 va_end(args);
ethannicholas5961bc92016-10-12 06:39:56 -070088 this->definitions().append(";\n");
cdaltond36f2c42016-02-11 14:10:38 -080089 }
90
csmartdalton75864b02017-02-09 09:47:21 -050091 void declareGlobal(const GrShaderVar&);
92
cdaltond36f2c42016-02-11 14:10:38 -080093 /**
egdaniel57d3b032015-11-13 11:57:27 -080094 * Called by GrGLSLProcessors to add code to one of the shaders.
joshualitt30ba4362014-08-21 20:18:45 -070095 */
96 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
97 va_list args;
98 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -080099 this->code().appendVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700100 va_end(args);
101 }
102
joshualitt43466a12015-02-13 17:18:27 -0800103 void codeAppend(const char* str) { this->code().append(str); }
joshualitt30ba4362014-08-21 20:18:45 -0700104
Ethan Nicholas00543112018-07-31 09:44:36 -0400105 void codeAppend(const char* str, size_t length) { this->code().append(str, length); }
106
joshualitt30ba4362014-08-21 20:18:45 -0700107 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
108 va_list args;
109 va_start(args, format);
joshualitt43466a12015-02-13 17:18:27 -0800110 this->code().prependVAList(format, args);
joshualitt30ba4362014-08-21 20:18:45 -0700111 va_end(args);
112 }
113
egdanielb2f94d12014-08-29 10:08:36 -0700114 /**
115 * Appends a variable declaration to one of the shaders
116 */
Brian Salomon99938a82016-11-21 13:41:08 -0500117 void declAppend(const GrShaderVar& var);
egdanielb2f94d12014-08-29 10:08:36 -0700118
joshualitt30ba4362014-08-21 20:18:45 -0700119 /** Emits a helper function outside of main() in the fragment shader. */
120 void emitFunction(GrSLType returnType,
121 const char* name,
122 int argCnt,
Brian Salomon99938a82016-11-21 13:41:08 -0500123 const GrShaderVar* args,
joshualitt30ba4362014-08-21 20:18:45 -0700124 const char* body,
125 SkString* outName);
126
127 /*
egdaniel574a4c12015-11-02 06:22:44 -0800128 * Combines the various parts of the shader to create a single finalized shader string.
129 */
130 void finalize(uint32_t visibility);
131
132 /*
joshualitt30ba4362014-08-21 20:18:45 -0700133 * Get parent builder for adding uniforms
134 */
egdaniel8dcdedc2015-11-11 06:27:20 -0800135 GrGLSLProgramBuilder* getProgramBuilder() { return fProgramBuilder; }
joshualitt30ba4362014-08-21 20:18:45 -0700136
137 /**
joshualitt47bb3822014-10-07 16:43:25 -0700138 * Helper for begining and ending a block in the shader code.
joshualitt30ba4362014-08-21 20:18:45 -0700139 */
140 class ShaderBlock {
141 public:
egdaniel2d721d32015-11-11 13:06:05 -0800142 ShaderBlock(GrGLSLShaderBuilder* builder) : fBuilder(builder) {
bsalomon49f085d2014-09-05 13:34:00 -0700143 SkASSERT(builder);
joshualitt30ba4362014-08-21 20:18:45 -0700144 fBuilder->codeAppend("{");
145 }
146
147 ~ShaderBlock() {
148 fBuilder->codeAppend("}");
149 }
150 private:
egdaniel2d721d32015-11-11 13:06:05 -0800151 GrGLSLShaderBuilder* fBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700152 };
joshualitt47bb3822014-10-07 16:43:25 -0700153
joshualitt30ba4362014-08-21 20:18:45 -0700154protected:
Brian Salomon99938a82016-11-21 13:41:08 -0500155 typedef GrTAllocator<GrShaderVar> VarArray;
joshualitt47bb3822014-10-07 16:43:25 -0700156 void appendDecls(const VarArray& vars, SkString* out) const;
joshualitt30ba4362014-08-21 20:18:45 -0700157
cdaltonc08f1962016-02-12 12:14:06 -0800158 /**
159 * Features that should only be enabled internally by the builders.
160 */
161 enum GLSLPrivateFeature {
162 kFragCoordConventions_GLSLPrivateFeature,
163 kBlendEquationAdvanced_GLSLPrivateFeature,
164 kBlendFuncExtended_GLSLPrivateFeature,
cdaltonc08f1962016-02-12 12:14:06 -0800165 kFramebufferFetch_GLSLPrivateFeature,
166 kNoPerspectiveInterpolation_GLSLPrivateFeature,
Chris Daltond31b5e72019-02-26 18:02:16 -0700167 kSampleVariables_GLSLPrivateFeature,
168 kLastGLSLPrivateFeature = kSampleVariables_GLSLPrivateFeature
cdaltonc08f1962016-02-12 12:14:06 -0800169 };
170
joshualitt30ba4362014-08-21 20:18:45 -0700171 /*
joshualitt30ba4362014-08-21 20:18:45 -0700172 * A general function which enables an extension in a shader if the feature bit is not present
cdalton33ad7012016-02-22 07:55:44 -0800173 *
174 * @return true if the feature bit was not yet present, false otherwise.
joshualitt30ba4362014-08-21 20:18:45 -0700175 */
cdalton33ad7012016-02-22 07:55:44 -0800176 bool addFeature(uint32_t featureBit, const char* extensionName);
joshualitt30ba4362014-08-21 20:18:45 -0700177
cdaltone4017d82015-05-06 11:48:56 -0700178 enum InterfaceQualifier {
csmartdalton276cc412016-11-21 11:55:00 -0700179 kIn_InterfaceQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700180 kOut_InterfaceQualifier,
181 kLastInterfaceQualifier = kOut_InterfaceQualifier
182 };
183
184 /*
185 * A low level function to build default layout qualifiers.
186 *
187 * e.g. layout(param1, param2, ...) out;
188 *
189 * GLSL allows default layout qualifiers for in, out, and uniform.
190 */
191 void addLayoutQualifier(const char* param, InterfaceQualifier);
192
193 void compileAndAppendLayoutQualifiers();
194
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000195 void nextStage() {
196 fShaderStrings.push_back();
197 fCodeIndex++;
198 }
199
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400200 void deleteStage() {
201 fShaderStrings.pop_back();
202 fCodeIndex--;
203 }
204
joshualitt43466a12015-02-13 17:18:27 -0800205 SkString& extensions() { return fShaderStrings[kExtensions]; }
cdaltond36f2c42016-02-11 14:10:38 -0800206 SkString& definitions() { return fShaderStrings[kDefinitions]; }
joshualitt43466a12015-02-13 17:18:27 -0800207 SkString& precisionQualifier() { return fShaderStrings[kPrecisionQualifier]; }
cdaltone4017d82015-05-06 11:48:56 -0700208 SkString& layoutQualifiers() { return fShaderStrings[kLayoutQualifiers]; }
joshualitt43466a12015-02-13 17:18:27 -0800209 SkString& uniforms() { return fShaderStrings[kUniforms]; }
210 SkString& inputs() { return fShaderStrings[kInputs]; }
211 SkString& outputs() { return fShaderStrings[kOutputs]; }
212 SkString& functions() { return fShaderStrings[kFunctions]; }
213 SkString& main() { return fShaderStrings[kMain]; }
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000214 SkString& code() { return fShaderStrings[fCodeIndex]; }
egdaniel574a4c12015-11-02 06:22:44 -0800215
216 virtual void onFinalize() = 0;
joshualitt43466a12015-02-13 17:18:27 -0800217
218 enum {
joshualitt43466a12015-02-13 17:18:27 -0800219 kExtensions,
cdaltond36f2c42016-02-11 14:10:38 -0800220 kDefinitions,
joshualitt43466a12015-02-13 17:18:27 -0800221 kPrecisionQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700222 kLayoutQualifiers,
joshualitt43466a12015-02-13 17:18:27 -0800223 kUniforms,
224 kInputs,
225 kOutputs,
226 kFunctions,
227 kMain,
228 kCode,
Brian Osman389b4b22019-03-18 16:45:25 -0400229
230 kPrealloc = kCode + 6, // 6 == Reasonable upper bound on number of processor stages
joshualitt43466a12015-02-13 17:18:27 -0800231 };
232
egdaniel8dcdedc2015-11-11 06:27:20 -0800233 GrGLSLProgramBuilder* fProgramBuilder;
Brian Osman6c431d52019-04-15 16:31:54 -0400234 SkSL::String fCompilerString;
Brian Osman389b4b22019-03-18 16:45:25 -0400235 SkSTArray<kPrealloc, SkString> fShaderStrings;
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000236 SkString fCode;
237 SkString fFunctions;
238 SkString fExtensions;
joshualitt30ba4362014-08-21 20:18:45 -0700239
240 VarArray fInputs;
241 VarArray fOutputs;
242 uint32_t fFeaturesAddedMask;
cdaltone4017d82015-05-06 11:48:56 -0700243 SkSTArray<1, SkString> fLayoutParams[kLastInterfaceQualifier + 1];
joshualitt43466a12015-02-13 17:18:27 -0800244 int fCodeIndex;
245 bool fFinalized;
246
Chris Dalton4c239342018-04-05 18:43:40 -0600247 friend class GrCCCoverageProcessor; // to access code().
egdanielfa896322016-01-13 12:19:30 -0800248 friend class GrGLSLProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800249 friend class GrGLProgramBuilder;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400250 friend class GrDawnProgramBuilder;
cdaltonc08f1962016-02-12 12:14:06 -0800251 friend class GrGLSLVaryingHandler; // to access noperspective interpolation feature.
kkinnunen7aedda52015-06-29 23:01:28 -0700252 friend class GrGLPathProgramBuilder; // to access fInputs.
egdaniel22281c12016-03-23 13:49:40 -0700253 friend class GrVkPipelineStateBuilder;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400254 friend class GrMtlPipelineStateBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700255};
joshualitt30ba4362014-08-21 20:18:45 -0700256#endif