2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLShaderBuilder_DEFINED
9 #define GrGLShaderBuilder_DEFINED
11 #include "gl/GrGLProgramDesc.h"
12 #include "gl/GrGLProgramEffects.h"
13 #include "gl/GrGLSL.h"
14 #include "gl/GrGLProgramDataManager.h"
15 #include "GrBackendProcessorFactory.h"
17 #include "GrProcessor.h"
22 class GrGLContextInfo;
23 class GrProcessorStage;
24 class GrGLProgramDesc;
25 class GrGLProgramBuilder;
26 class GrGLFullProgramBuilder;
29 base class for all shaders builders
31 class GrGLShaderBuilder {
33 typedef GrGLProcessor::TransformedCoordsArray TransformedCoordsArray;
34 typedef GrGLProcessor::TextureSampler TextureSampler;
35 GrGLShaderBuilder(GrGLProgramBuilder* program);
37 void addInput(GrGLShaderVar i) { fInputs.push_back(i); }
38 void addOutput(GrGLShaderVar i) { fOutputs.push_back(i); }
41 * We put texture lookups in the base class because it is TECHNICALLY possible to do texture
42 * lookups in any kind of shader. However, for the time being using these calls on non-fragment
43 * shaders will result in a shader compilation error as texture sampler uniforms are only
44 * visible to the fragment shader. It would not be hard to change this behavior, if someone
45 * actually wants to do texture lookups in a non-fragment shader
47 * TODO if append texture lookup is used on a non-fragment shader, sampler uniforms should be
48 * made visible to that shaders
50 /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or
51 Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle
52 order of the result depends on the GrTextureAccess associated with the TextureSampler. */
53 void appendTextureLookup(SkString* out,
54 const TextureSampler&,
55 const char* coordName,
56 GrSLType coordType = kVec2f_GrSLType) const;
58 /** Version of above that appends the result to the fragment shader code instead.*/
59 void appendTextureLookup(const TextureSampler&,
60 const char* coordName,
61 GrSLType coordType = kVec2f_GrSLType);
64 /** Does the work of appendTextureLookup and modulates the result by modulation. The result is
65 always a vec4. modulation and the swizzle specified by TextureSampler must both be vec4 or
66 float. If modulation is "" or NULL it this function acts as though appendTextureLookup were
68 void appendTextureLookupAndModulate(const char* modulation,
69 const TextureSampler&,
70 const char* coordName,
71 GrSLType coordType = kVec2f_GrSLType);
73 /** If texture swizzling is available using tex parameters then it is preferred over mangling
74 the generated shader code. This potentially allows greater reuse of cached shaders. */
75 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps);
78 * Called by GrGLProcessors to add code to one of the shaders.
80 void codeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
82 va_start(args, format);
83 fCode.appendVAList(format, args);
87 void codeAppend(const char* str) { fCode.append(str); }
89 void codePrependf(const char format[], ...) SK_PRINTF_LIKE(2, 3) {
91 va_start(args, format);
92 fCode.prependVAList(format, args);
97 * Appends a variable declaration to one of the shaders
99 void declAppend(const GrGLShaderVar& var);
101 /** Emits a helper function outside of main() in the fragment shader. */
102 void emitFunction(GrSLType returnType,
105 const GrGLShaderVar* args,
110 * Get parent builder for adding uniforms
112 GrGLProgramBuilder* getProgramBuilder() { return fProgramBuilder; }
115 * Helper for begining and ending a block in the fragment code.
119 ShaderBlock(GrGLShaderBuilder* builder) : fBuilder(builder) {
121 fBuilder->codeAppend("{");
125 fBuilder->codeAppend("}");
128 GrGLShaderBuilder* fBuilder;
133 * this super low level function is just for use internally to builders
135 void appendTextureLookup(const char* samplerName,
136 const char* coordName,
137 uint32_t configComponentMask,
138 const char* swizzle);
141 * A general function which enables an extension in a shader if the feature bit is not present
143 void addFeature(uint32_t featureBit, const char* extensionName);
145 typedef GrTAllocator<GrGLShaderVar> VarArray;
147 GrGLProgramBuilder* fProgramBuilder;
151 SkString fExtensions;
155 uint32_t fFeaturesAddedMask;
160 * Full Shader builder is the base class for shaders which are only accessible through full program
161 * builder, ie vertex, geometry, and later TCU / TES. Using this base class, they can access the
162 * full program builder functionality through the full program pointer
164 class GrGLFullShaderBuilder : public GrGLShaderBuilder {
166 GrGLFullShaderBuilder(GrGLFullProgramBuilder* program);
168 GrGLFullProgramBuilder* fullProgramBuilder() { return fFullProgramBuilder; }
170 GrGLFullProgramBuilder* fFullProgramBuilder;
172 typedef GrGLShaderBuilder INHERITED;