Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrGLSLShaderBuilder.h
Go to the documentation of this file.
1/*
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
8#ifndef GrGLSLShaderBuilder_DEFINED
9#define GrGLSLShaderBuilder_DEFINED
10
11#include "include/core/SkSpan.h"
19#include "src/sksl/ir/SkSLStatement.h" // IWYU pragma: keep
20
21#include <cstdarg>
22#include <cstddef>
23#include <cstdint>
24#include <string>
25
28enum class SkBlendMode;
29enum class SkSLType : char;
30
31/**
32 base class for all shaders builders
33*/
35public:
38
39 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
40
41 /** Appends a 2D texture sample with projection if necessary. The vec length and swizzle
42 order of the result depends on the GrProcessor::TextureSampler associated with the
43 SamplerHandle.
44 */
45 void appendTextureLookup(SkString* out, SamplerHandle, const char* coordName) const;
46
47 /** Version of above that appends the result to the shader code instead.*/
49 const char* coordName,
50 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
51
52 /** Does the work of appendTextureLookup and blends the result by dst, treating the texture
53 lookup as the src input to the blend. The dst is assumed to be half4 and the result is
54 always a half4. If dst is nullptr we use half4(1) as the blend dst. */
55 void appendTextureLookupAndBlend(const char* dst,
58 const char* coordName,
59 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
60
61 /** Appends a load of an input attachment into the shader code. */
63
64 /** Adds a helper function to facilitate color gamut transformation, and produces code that
65 returns the srcColor transformed into a new gamut (via multiplication by the xform from
66 colorXformHelper). Premultiplied sources are also handled correctly (colorXformHelper
67 determines if the source is premultipled or not). */
68 void appendColorGamutXform(SkString* out, const char* srcColor,
69 GrGLSLColorSpaceXformHelper* colorXformHelper);
70
71 /** Version of above that appends the result to the shader code instead. */
72 void appendColorGamutXform(const char* srcColor, GrGLSLColorSpaceXformHelper* colorXformHelper);
73
74 /**
75 * Adds a constant declaration to the top of the shader.
76 */
77 void defineConstant(const char* type, const char* name, const char* value) {
78 this->definitions().appendf("const %s %s = %s;\n", type, name, value);
79 }
80
81 void defineConstant(const char* name, int value) {
82 this->definitions().appendf("const int %s = %i;\n", name, value);
83 }
84
85 void defineConstant(const char* name, float value) {
86 this->definitions().appendf("const float %s = %f;\n", name, value);
87 }
88
89 void defineConstantf(const char* type, const char* name, const char* fmt, ...)
90 SK_PRINTF_LIKE(4, 5) {
91 this->definitions().appendf("const %s %s = ", type, name);
92 va_list args;
94 this->definitions().appendVAList(fmt, args);
96 this->definitions().append(";\n");
97 }
98
99 void definitionAppend(const char* str) { this->definitions().append(str); }
100
101 void declareGlobal(const GrShaderVar&);
102
103 // Generates a unique variable name for holding the result of a temporary expression when it's
104 // not reasonable to just add a new block for scoping. Does not declare anything.
105 SkString newTmpVarName(const char* suffix) {
106 int tmpIdx = fTmpVariableCounter++;
107 return SkStringPrintf("_tmp_%d_%s", tmpIdx, suffix);
108 }
109
110 /**
111 * Called by GrGLSLProcessors to add code to one of the shaders.
112 */
113 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
114 va_list args;
116 this->code().appendVAList(format, args);
118 }
119
120 void codeAppend(const char* str) { this->code().append(str); }
121
122 void codeAppend(const char* str, size_t length) { this->code().append(str, length); }
123
124 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
125 va_list args;
127 this->code().prependVAList(format, args);
129 }
130
131 /**
132 * Appends a variable declaration to one of the shaders
133 */
134 void declAppend(const GrShaderVar& var);
135
136 /**
137 * Generates a mangled name for a helper function in the fragment shader. Will give consistent
138 * results if called more than once.
139 */
140 SkString getMangledFunctionName(const char* baseName);
141
142 /** Emits a prototype for a helper function outside of main() in the fragment shader. */
143 void emitFunctionPrototype(SkSLType returnType,
144 const char* mangledName,
146
147 void emitFunctionPrototype(const char* declaration);
148
149 /** Emits a helper function outside of main() in the fragment shader. */
150 void emitFunction(SkSLType returnType,
151 const char* mangledName,
153 const char* body);
154
155 void emitFunction(const char* declaration, const char* body);
156
157 /**
158 * Combines the various parts of the shader to create a single finalized shader string.
159 */
160 void finalize(uint32_t visibility);
161
162 /**
163 * Get parent builder for adding uniforms.
164 */
166
167 /**
168 * Helper for begining and ending a block in the shader code.
169 */
171 public:
172 ShaderBlock(GrGLSLShaderBuilder* builder) : fBuilder(builder) {
173 SkASSERT(builder);
174 fBuilder->codeAppend("{");
175 }
176
178 fBuilder->codeAppend("}");
179 }
180 private:
181 GrGLSLShaderBuilder* fBuilder;
182 };
183
184protected:
186 void appendDecls(const VarArray& vars, SkString* out) const;
187
188 void appendFunctionDecl(SkSLType returnType,
189 const char* mangledName,
191
192 /**
193 * Features that should only be enabled internally by the builders.
194 */
204
205 /*
206 * A general function which enables an extension in a shader if the feature bit is not present
207 *
208 * @return true if the feature bit was not yet present, false otherwise.
209 */
210 bool addFeature(uint32_t featureBit, const char* extensionName);
211
217
218 /*
219 * A low level function to build default layout qualifiers.
220 *
221 * e.g. layout(param1, param2, ...) out;
222 *
223 * GLSL allows default layout qualifiers for in, out, and uniform.
224 */
225 void addLayoutQualifier(const char* param, InterfaceQualifier);
226
228
229 void nextStage() {
231 fCodeIndex++;
232 }
233
234 void deleteStage() {
236 fCodeIndex--;
237 }
238
249
250 virtual void onFinalize() = 0;
251
252 enum {
263
264 kPrealloc = kCode + 6, // 6 == Reasonable upper bound on number of processor stages
265 };
266
268 std::string fCompilerString;
273 // Hangs onto Declarations so we don't destroy them prior to the variables that refer to them.
275
282
283 // Counter for generating unique scratch variable names in a shader.
285
287 friend class GrGLProgramBuilder;
290 friend class GrGLSLVaryingHandler; // to access noperspective interpolation feature.
291 friend class GrGLPathProgramBuilder; // to access fInputs.
294};
295#endif
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SK_PRINTF_LIKE(A, B)
SkBlendMode
Definition SkBlendMode.h:38
SkSLType
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
ShaderBlock(GrGLSLShaderBuilder *builder)
virtual void onFinalize()=0
void emitFunction(SkSLType returnType, const char *mangledName, SkSpan< const GrShaderVar > args, const char *body)
void appendDecls(const VarArray &vars, SkString *out) const
void addLayoutQualifier(const char *param, InterfaceQualifier)
void defineConstant(const char *type, const char *name, const char *value)
SkTBlockList< GrShaderVar > VarArray
void defineConstantf(const char *type, const char *name, const char *fmt,...) SK_PRINTF_LIKE(4
GrGLSLUniformHandler::SamplerHandle SamplerHandle
void codePrependf(const char format[],...) SK_PRINTF_LIKE(2
void declareGlobal(const GrShaderVar &)
void defineConstant(const char *name, float value)
friend class GrGLPathProgramBuilder
this code().appendVAList(format
void declAppend(const GrShaderVar &var)
void definitionAppend(const char *str)
friend class GrDawnProgramBuilder
void codeAppend(const char *str, size_t length)
bool addFeature(uint32_t featureBit, const char *extensionName)
this code().prependVAList(format
void va_start(args, format)
void finalize(uint32_t visibility)
void codeAppend(const char *str)
void appendColorGamutXform(SkString *out, const char *srcColor, GrGLSLColorSpaceXformHelper *colorXformHelper)
this definitions().appendVAList(fmt
skia_private::STArray< kPrealloc, SkString > fShaderStrings
void appendTextureLookup(SkString *out, SamplerHandle, const char *coordName) const
SkString getMangledFunctionName(const char *baseName)
void codeAppendf(const char format[],...) SK_PRINTF_LIKE(2
SkString newTmpVarName(const char *suffix)
void appendInputLoad(SamplerHandle)
GrGLSLProgramBuilder * fProgramBuilder
this definitions().append("
void appendFunctionDecl(SkSLType returnType, const char *mangledName, SkSpan< const GrShaderVar > args)
SkSL::StatementArray fDeclarations
skia_private::STArray< 1, SkString > fLayoutParams[kLastInterfaceQualifier+1]
void appendTextureLookupAndBlend(const char *dst, SkBlendMode, SamplerHandle, const char *coordName, GrGLSLColorSpaceXformHelper *colorXformHelper=nullptr)
GrGLSLProgramBuilder * getProgramBuilder()
void defineConstant(const char *name, int value)
void emitFunctionPrototype(SkSLType returnType, const char *mangledName, SkSpan< const GrShaderVar > args)
uint8_t value
uint32_t uint32_t * format
const char * name
Definition fuchsia.cc:50
size_t length
static SkString fmt(SkColor4f c)
Definition p3.cpp:43