• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrGLSLUniformHandler_DEFINED
9 #define GrGLSLUniformHandler_DEFINED
10 
11 #include "src/gpu/GrShaderVar.h"
12 #include "src/gpu/GrSwizzle.h"
13 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
14 
15 // variable names beginning with this prefix will not be mangled
16 #define GR_NO_MANGLE_PREFIX "sk_"
17 
18 class GrGLSLProgramBuilder;
19 class GrGLSLShaderBuilder;
20 class GrSamplerState;
21 class GrSurfaceProxy;
22 
23 // Handles for program uniforms (other than per-effect uniforms)
24 struct GrGLSLBuiltinUniformHandles {
25     GrGLSLProgramDataManager::UniformHandle fRTAdjustmentUni;
26     // Render target flip uniform (used for dFdy, sk_Clockwise, and sk_FragCoord)
27     GrGLSLProgramDataManager::UniformHandle fRTFlipUni;
28     // Destination texture origin and scale, used when dest-texture readback is enabled.
29     GrGLSLProgramDataManager::UniformHandle fDstTextureCoordsUni;
30 };
31 
32 class GrGLSLUniformHandler {
33 public:
34     struct UniformInfo {
35         GrShaderVar                fVariable;
36         uint32_t                   fVisibility;
37         const GrFragmentProcessor* fOwner;
38         SkString                   fRawName;
39     };
40 
~GrGLSLUniformHandler()41     virtual ~GrGLSLUniformHandler() {}
42 
43     using UniformHandle = GrGLSLProgramDataManager::UniformHandle;
44 
45     GR_DEFINE_RESOURCE_HANDLE_CLASS(SamplerHandle);
46 
47     /** Add a uniform variable to the current program, that has visibility in one or more shaders.
48         visibility is a bitfield of GrShaderFlag values indicating from which shaders the uniform
49         should be accessible. At least one bit must be set. Geometry shader uniforms are not
50         supported at this time. The actual uniform name will be mangled. If outName is not nullptr
51         then it will refer to the final uniform name after return. Use the addUniformArray variant
52         to add an array of uniforms. */
53     UniformHandle addUniform(const GrFragmentProcessor* owner,
54                              uint32_t visibility,
55                              GrSLType type,
56                              const char* name,
57                              const char** outName = nullptr) {
58         SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
59         return this->addUniformArray(owner, visibility, type, name, 0, outName);
60     }
61 
62     UniformHandle addUniformArray(const GrFragmentProcessor* owner,
63                                   uint32_t visibility,
64                                   GrSLType type,
65                                   const char* name,
66                                   int arrayCount,
67                                   const char** outName = nullptr) {
68         SkASSERT(!GrSLTypeIsCombinedSamplerType(type));
69         bool mangle = strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX));
70         return this->internalAddUniformArray(owner, visibility, type, name, mangle, arrayCount,
71                                              outName);
72     }
73 
74     virtual const GrShaderVar& getUniformVariable(UniformHandle u) const = 0;
75 
76     /**
77      * Shortcut for getUniformVariable(u).c_str()
78      */
79     virtual const char* getUniformCStr(UniformHandle u) const = 0;
80 
81     virtual int numUniforms() const = 0;
82 
83     virtual UniformInfo& uniform(int idx) = 0;
84     virtual const UniformInfo& uniform(int idx) const = 0;
85 
86     // Looks up a uniform that was added by 'owner' with the given 'rawName' (pre-mangling).
87     // If there is no such uniform, a variable with type kVoid is returned.
88     GrShaderVar getUniformMapping(const GrFragmentProcessor& owner, SkString rawName) const;
89 
90     // Like getUniformMapping(), but if the uniform is found it also marks it as accessible in
91     // the vertex shader.
92     GrShaderVar liftUniformToVertexShader(const GrFragmentProcessor& owner, SkString rawName);
93 
94 protected:
GrGLSLUniformHandler(GrGLSLProgramBuilder * program)95     explicit GrGLSLUniformHandler(GrGLSLProgramBuilder* program) : fProgramBuilder(program) {}
96 
97     // This is not owned by the class
98     GrGLSLProgramBuilder* fProgramBuilder;
99 
100 private:
101     virtual const char * samplerVariable(SamplerHandle) const = 0;
102     virtual GrSwizzle samplerSwizzle(SamplerHandle) const = 0;
103 
inputSamplerVariable(SamplerHandle)104     virtual const char* inputSamplerVariable(SamplerHandle) const {
105         SkDEBUGFAIL("Trying to get input sampler from unsupported backend");
106         return nullptr;
107     }
inputSamplerSwizzle(SamplerHandle)108     virtual GrSwizzle inputSamplerSwizzle(SamplerHandle) const {
109         SkDEBUGFAIL("Trying to get input sampler swizzle from unsupported backend");
110         return {};
111     }
112 
113     virtual SamplerHandle addSampler(const GrBackendFormat&, GrSamplerState, const GrSwizzle&,
114                                      const char* name, const GrShaderCaps*) = 0;
115 
addInputSampler(const GrSwizzle & swizzle,const char * name)116     virtual SamplerHandle addInputSampler(const GrSwizzle& swizzle, const char* name) {
117         SkDEBUGFAIL("Trying to add input sampler to unsupported backend");
118         return {};
119     }
120 
121     virtual UniformHandle internalAddUniformArray(const GrFragmentProcessor* owner,
122                                                   uint32_t visibility,
123                                                   GrSLType type,
124                                                   const char* name,
125                                                   bool mangleName,
126                                                   int arrayCount,
127                                                   const char** outName) = 0;
128 
129     virtual void appendUniformDecls(GrShaderFlags visibility, SkString*) const = 0;
130 
131     friend class GrGLSLProgramBuilder;
132 };
133 
134 #endif
135