blob: 855011f37beec27d214a1895cfd14ad74acd751d [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;
egdaniel09aa1fc2016-04-20 07:09:46 -070029
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,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040038 GrSLType coordType = kHalf2_GrSLType) const;
joshualitt30ba4362014-08-21 20:18:45 -070039
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,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040043 GrSLType coordType = kHalf2_GrSLType,
brianosman77320db2016-09-07 08:09:10 -070044 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
Ethan Nicholasf7b88202017-09-18 14:10:39 -040048 always a half4. modulation and the swizzle specified by SamplerHandle must both be
49 half4 or half. If modulation is "" or nullptr it this function acts as though
egdaniel7dc4bd02015-10-29 07:57:01 -070050 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,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040054 GrSLType coordType = kHalf2_GrSLType,
brianosman77320db2016-09-07 08:09:10 -070055 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
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 Dalton60283612018-02-14 13:38:14 -0700167 kLastGLSLPrivateFeature = kNoPerspectiveInterpolation_GLSLPrivateFeature
cdaltonc08f1962016-02-12 12:14:06 -0800168 };
169
joshualitt30ba4362014-08-21 20:18:45 -0700170 /*
joshualitt30ba4362014-08-21 20:18:45 -0700171 * A general function which enables an extension in a shader if the feature bit is not present
cdalton33ad7012016-02-22 07:55:44 -0800172 *
173 * @return true if the feature bit was not yet present, false otherwise.
joshualitt30ba4362014-08-21 20:18:45 -0700174 */
cdalton33ad7012016-02-22 07:55:44 -0800175 bool addFeature(uint32_t featureBit, const char* extensionName);
joshualitt30ba4362014-08-21 20:18:45 -0700176
cdaltone4017d82015-05-06 11:48:56 -0700177 enum InterfaceQualifier {
csmartdalton276cc412016-11-21 11:55:00 -0700178 kIn_InterfaceQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700179 kOut_InterfaceQualifier,
180 kLastInterfaceQualifier = kOut_InterfaceQualifier
181 };
182
183 /*
184 * A low level function to build default layout qualifiers.
185 *
186 * e.g. layout(param1, param2, ...) out;
187 *
188 * GLSL allows default layout qualifiers for in, out, and uniform.
189 */
190 void addLayoutQualifier(const char* param, InterfaceQualifier);
191
192 void compileAndAppendLayoutQualifiers();
193
joshualitt43466a12015-02-13 17:18:27 -0800194 void nextStage() {
195 fShaderStrings.push_back();
196 fCompilerStrings.push_back(this->code().c_str());
197 fCompilerStringLengths.push_back((int)this->code().size());
198 fCodeIndex++;
199 }
joshualittb8a82f22015-02-13 16:31:46 -0800200
joshualitt43466a12015-02-13 17:18:27 -0800201 SkString& versionDecl() { return fShaderStrings[kVersionDecl]; }
202 SkString& extensions() { return fShaderStrings[kExtensions]; }
cdaltond36f2c42016-02-11 14:10:38 -0800203 SkString& definitions() { return fShaderStrings[kDefinitions]; }
joshualitt43466a12015-02-13 17:18:27 -0800204 SkString& precisionQualifier() { return fShaderStrings[kPrecisionQualifier]; }
cdaltone4017d82015-05-06 11:48:56 -0700205 SkString& layoutQualifiers() { return fShaderStrings[kLayoutQualifiers]; }
joshualitt43466a12015-02-13 17:18:27 -0800206 SkString& uniforms() { return fShaderStrings[kUniforms]; }
207 SkString& inputs() { return fShaderStrings[kInputs]; }
208 SkString& outputs() { return fShaderStrings[kOutputs]; }
209 SkString& functions() { return fShaderStrings[kFunctions]; }
210 SkString& main() { return fShaderStrings[kMain]; }
211 SkString& code() { return fShaderStrings[fCodeIndex]; }
egdaniel574a4c12015-11-02 06:22:44 -0800212
213 virtual void onFinalize() = 0;
joshualitt43466a12015-02-13 17:18:27 -0800214
215 enum {
216 kVersionDecl,
217 kExtensions,
cdaltond36f2c42016-02-11 14:10:38 -0800218 kDefinitions,
joshualitt43466a12015-02-13 17:18:27 -0800219 kPrecisionQualifier,
cdaltone4017d82015-05-06 11:48:56 -0700220 kLayoutQualifiers,
joshualitt43466a12015-02-13 17:18:27 -0800221 kUniforms,
222 kInputs,
223 kOutputs,
224 kFunctions,
225 kMain,
226 kCode,
227 };
228
egdaniel8dcdedc2015-11-11 06:27:20 -0800229 GrGLSLProgramBuilder* fProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800230 SkSTArray<kCode, const char*, true> fCompilerStrings;
231 SkSTArray<kCode, int, true> fCompilerStringLengths;
232 SkSTArray<kCode, SkString> fShaderStrings;
joshualitt30ba4362014-08-21 20:18:45 -0700233 SkString fCode;
234 SkString fFunctions;
235 SkString fExtensions;
236
237 VarArray fInputs;
238 VarArray fOutputs;
239 uint32_t fFeaturesAddedMask;
cdaltone4017d82015-05-06 11:48:56 -0700240 SkSTArray<1, SkString> fLayoutParams[kLastInterfaceQualifier + 1];
joshualitt43466a12015-02-13 17:18:27 -0800241 int fCodeIndex;
242 bool fFinalized;
243
Chris Dalton4c239342018-04-05 18:43:40 -0600244 friend class GrCCCoverageProcessor; // to access code().
egdanielfa896322016-01-13 12:19:30 -0800245 friend class GrGLSLProgramBuilder;
joshualitt43466a12015-02-13 17:18:27 -0800246 friend class GrGLProgramBuilder;
cdaltonc08f1962016-02-12 12:14:06 -0800247 friend class GrGLSLVaryingHandler; // to access noperspective interpolation feature.
kkinnunen7aedda52015-06-29 23:01:28 -0700248 friend class GrGLPathProgramBuilder; // to access fInputs.
egdaniel22281c12016-03-23 13:49:40 -0700249 friend class GrVkPipelineStateBuilder;
Timothy Liang7ac582e2018-08-06 09:47:23 -0400250 friend class GrMtlPipelineStateBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700251};
joshualitt30ba4362014-08-21 20:18:45 -0700252#endif