81037084e9805968b0dbb2cb88413bbca32c4ed2
[platform/upstream/libSkiaSharp.git] / src / gpu / glsl / GrGLSLProgramBuilder.h
1 /*
2  * Copyright 2015 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 GrGLSLProgramBuilder_DEFINED
9 #define GrGLSLProgramBuilder_DEFINED
10
11 #include "GrGeometryProcessor.h"
12 #include "GrGpu.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 #include "glsl/GrGLSLGeometryShaderBuilder.h"
15 #include "glsl/GrGLSLProgramDataManager.h"
16 #include "glsl/GrGLSLVertexShaderBuilder.h"
17
18 class GrGLSLCaps;
19 class GrGLSLShaderVar;
20 class GrGLSLVaryingHandler;
21
22 // Enough precision to represent 1 / 2048 accurately in printf
23 #define GR_SIGNIFICANT_POW2_DECIMAL_DIG 11
24
25 class GrGLSLUniformBuilder {
26 public:
27     enum ShaderVisibility {
28         kVertex_Visibility   = 1 << kVertex_GrShaderType,
29         kGeometry_Visibility = 1 << kGeometry_GrShaderType,
30         kFragment_Visibility = 1 << kFragment_GrShaderType,
31     };
32
33     virtual ~GrGLSLUniformBuilder() {}
34
35     typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
36
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,
44                              GrSLType type,
45                              GrSLPrecision precision,
46                              const char* name,
47                              const char** outName = nullptr) {
48         return this->addUniformArray(visibility, type, precision, name, 0, outName);
49     }
50
51     UniformHandle addUniformArray(uint32_t visibility,
52                                   GrSLType type,
53                                   GrSLPrecision precision,
54                                   const char* name,
55                                   int arrayCount,
56                                   const char** outName = nullptr) {
57         return this->internalAddUniformArray(visibility, type, precision, name, true, arrayCount,
58                                              outName);
59     }
60
61     virtual const GrGLSLShaderVar& getUniformVariable(UniformHandle u) const = 0;
62
63     /**
64      * Shortcut for getUniformVariable(u).c_str()
65      */
66     virtual const char* getUniformCStr(UniformHandle u) const = 0;
67
68     /*
69      * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
70      */
71 protected:
72     virtual UniformHandle internalAddUniformArray(
73         uint32_t visibility,
74         GrSLType type,
75         GrSLPrecision precision,
76         const char* name,
77         bool mangleName,
78         int arrayCount,
79         const char** outName) = 0;
80 };
81
82 /* a specialization of the above for GPs.  Lets the user add uniforms, varyings, and VS / FS code */
83 class GrGLSLGPBuilder : public virtual GrGLSLUniformBuilder {
84 public:
85     /*
86      * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
87      */
88 };
89
90
91 /* a specializations for FPs. Lets the user add uniforms and FS code */
92 class GrGLSLFPBuilder : public virtual GrGLSLUniformBuilder {
93 public:
94     /*
95      * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
96      */
97 };
98
99 /* a specializations for XPs. Lets the user add uniforms and FS code */
100 class GrGLSLXPBuilder : public virtual GrGLSLUniformBuilder {
101 public:
102     /*
103      * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
104      */
105 };
106
107 class GrGLSLProgramBuilder : public GrGLSLGPBuilder,
108                              public GrGLSLFPBuilder,
109                              public GrGLSLXPBuilder {
110 public:
111     typedef GrGpu::DrawArgs DrawArgs;
112
113     virtual const GrGLSLCaps* glslCaps() const = 0;
114
115     // Handles for program uniforms (other than per-effect uniforms)
116     struct BuiltinUniformHandles {
117         UniformHandle       fRTAdjustmentUni;
118
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;
122     };
123
124 protected:
125     explicit GrGLSLProgramBuilder(const DrawArgs& args);
126
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(); }
131
132     void appendUniformDecls(ShaderVisibility, SkString*) const;
133
134     // Used to add a uniform for frag position without mangling the name of the uniform inside of a
135     // stage.
136     UniformHandle addFragPosUniform(uint32_t visibility,
137                                     GrSLType type,
138                                     GrSLPrecision precision,
139                                     const char* name,
140                                     const char** outName) {
141         return this->internalAddUniformArray(visibility, type, precision, name, false, 0, outName);
142     }
143
144     const char* rtAdjustment() const { return "rtAdjustment"; }
145
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);
150
151     virtual GrGLSLVaryingHandler* varyingHandler() = 0;
152
153     // number of each input/output type in a single allocation block, used by many builders
154     static const int kVarsPerBlock;
155
156     GrGLSLVertexBuilder         fVS;
157     GrGLSLGeometryBuilder       fGS;
158     GrGLSLFragmentShaderBuilder fFS;
159
160     int fStageIndex;
161
162     BuiltinUniformHandles fUniformHandles;
163
164     const DrawArgs& fArgs;
165
166 private:
167     virtual void onAppendUniformDecls(ShaderVisibility visibility, SkString* out) const = 0;
168
169     friend class GrGLSLShaderBuilder;
170     friend class GrGLSLVertexBuilder;
171     friend class GrGLSLFragmentShaderBuilder;
172     friend class GrGLSLGeometryBuilder;
173     friend class GrGLSLVaryingHandler;
174 };
175
176 #endif