2 * Copyright 2015 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 GrGLSLProgramBuilder_DEFINED
9 #define GrGLSLProgramBuilder_DEFINED
11 #include "GrGeometryProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "glsl/GrGLSLGeometryShaderBuilder.h"
15 #include "glsl/GrGLSLProgramDataManager.h"
16 #include "glsl/GrGLSLVertexShaderBuilder.h"
19 class GrGLSLShaderVar;
20 class GrGLSLVaryingHandler;
22 // Enough precision to represent 1 / 2048 accurately in printf
23 #define GR_SIGNIFICANT_POW2_DECIMAL_DIG 11
25 class GrGLSLUniformBuilder {
27 enum ShaderVisibility {
28 kVertex_Visibility = 1 << kVertex_GrShaderType,
29 kGeometry_Visibility = 1 << kGeometry_GrShaderType,
30 kFragment_Visibility = 1 << kFragment_GrShaderType,
33 virtual ~GrGLSLUniformBuilder() {}
35 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
37 /** Add a uniform variable to the current program, that has visibility in one or more shaders.
38 visibility is a bitfield of ShaderVisibility values indicating from which shaders the
39 uniform should be accessible. At least one bit must be set. Geometry shader uniforms are not
40 supported at this time. The actual uniform name will be mangled. If outName is not nullptr
41 then it will refer to the final uniform name after return. Use the addUniformArray variant
42 to add an array of uniforms. */
43 UniformHandle addUniform(uint32_t visibility,
45 GrSLPrecision precision,
47 const char** outName = nullptr) {
48 return this->addUniformArray(visibility, type, precision, name, 0, outName);
51 UniformHandle addUniformArray(uint32_t visibility,
53 GrSLPrecision precision,
56 const char** outName = nullptr) {
57 return this->internalAddUniformArray(visibility, type, precision, name, true, arrayCount,
61 virtual const GrGLSLShaderVar& getUniformVariable(UniformHandle u) const = 0;
64 * Shortcut for getUniformVariable(u).c_str()
66 virtual const char* getUniformCStr(UniformHandle u) const = 0;
69 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
72 virtual UniformHandle internalAddUniformArray(
75 GrSLPrecision precision,
79 const char** outName) = 0;
82 /* a specialization of the above for GPs. Lets the user add uniforms, varyings, and VS / FS code */
83 class GrGLSLGPBuilder : public virtual GrGLSLUniformBuilder {
86 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
91 /* a specializations for FPs. Lets the user add uniforms and FS code */
92 class GrGLSLFPBuilder : public virtual GrGLSLUniformBuilder {
95 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
99 /* a specializations for XPs. Lets the user add uniforms and FS code */
100 class GrGLSLXPBuilder : public virtual GrGLSLUniformBuilder {
103 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
107 class GrGLSLProgramBuilder : public GrGLSLGPBuilder,
108 public GrGLSLFPBuilder,
109 public GrGLSLXPBuilder {
111 typedef GrGpu::DrawArgs DrawArgs;
113 virtual const GrGLSLCaps* glslCaps() const = 0;
115 // Handles for program uniforms (other than per-effect uniforms)
116 struct BuiltinUniformHandles {
117 UniformHandle fRTAdjustmentUni;
119 // We use the render target height to provide a y-down frag coord when specifying
120 // origin_upper_left is not supported.
121 UniformHandle fRTHeightUni;
125 explicit GrGLSLProgramBuilder(const DrawArgs& args);
127 const GrPrimitiveProcessor& primitiveProcessor() const { return *fArgs.fPrimitiveProcessor; }
128 const GrPipeline& pipeline() const { return *fArgs.fPipeline; }
129 const GrProgramDesc& desc() const { return *fArgs.fDesc; }
130 const GrProgramDesc::KeyHeader& header() const { return fArgs.fDesc->header(); }
132 void appendUniformDecls(ShaderVisibility, SkString*) const;
134 // Used to add a uniform for frag position without mangling the name of the uniform inside of a
136 UniformHandle addFragPosUniform(uint32_t visibility,
138 GrSLPrecision precision,
140 const char** outName) {
141 return this->internalAddUniformArray(visibility, type, precision, name, false, 0, outName);
144 const char* rtAdjustment() const { return "rtAdjustment"; }
146 // Generates a name for a variable. The generated string will be name prefixed by the prefix
147 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
148 // explicitly asked not to.
149 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
151 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
153 // number of each input/output type in a single allocation block, used by many builders
154 static const int kVarsPerBlock;
156 GrGLSLVertexBuilder fVS;
157 GrGLSLGeometryBuilder fGS;
158 GrGLSLFragmentShaderBuilder fFS;
162 BuiltinUniformHandles fUniformHandles;
164 const DrawArgs& fArgs;
167 virtual void onAppendUniformDecls(ShaderVisibility visibility, SkString* out) const = 0;
169 friend class GrGLSLShaderBuilder;
170 friend class GrGLSLVertexBuilder;
171 friend class GrGLSLFragmentShaderBuilder;
172 friend class GrGLSLGeometryBuilder;
173 friend class GrGLSLVaryingHandler;