1 /*
2 * Copyright 2016 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 GrVkUniformHandler_DEFINED
9 #define GrVkUniformHandler_DEFINED
10 
11 #include "GrAllocator.h"
12 #include "GrSamplerState.h"
13 #include "GrShaderVar.h"
14 #include "GrVkSampler.h"
15 #include "glsl/GrGLSLUniformHandler.h"
16 #include "vk/GrVkTypes.h"
17 
18 class GrVkUniformHandler : public GrGLSLUniformHandler {
19 public:
20     static const int kUniformsPerBlock = 8;
21 
22     enum {
23         /**
24          * Binding a descriptor set invalidates all higher index descriptor sets. We must bind
25          * in the order of this enumeration. Samplers are after Uniforms because GrOps can specify
26          * GP textures as dynamic state, meaning they get rebound for each GrMesh in a draw while
27          * uniforms are bound once before all the draws.
28          */
29         kUniformBufferDescSet = 0,
30         kSamplerDescSet = 1,
31     };
32     enum {
33         kGeometryBinding = 0,
34         kFragBinding = 1,
35     };
36 
37     struct UniformInfo {
38         GrShaderVar             fVariable;
39         uint32_t                fVisibility;
40         // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler
41         uint32_t                fUBOffset;
42         // The SamplerState, maxMipLevel, and ycbcrInfo are only valid if the GrSLType is a sampler
43         // and that sampler is used for sampling an external image with a ycbcr conversion.
44         const GrVkSampler*      fImmutableSampler = nullptr;
45     };
46     typedef GrTAllocator<UniformInfo> UniformInfoArray;
47 
getUniformVariable(UniformHandle u)48     const GrShaderVar& getUniformVariable(UniformHandle u) const override {
49         return fUniforms[u.toIndex()].fVariable;
50     }
51 
getUniformCStr(UniformHandle u)52     const char* getUniformCStr(UniformHandle u) const override {
53         return this->getUniformVariable(u).c_str();
54     }
55 
56 private:
GrVkUniformHandler(GrGLSLProgramBuilder * program)57     explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
58         : INHERITED(program)
59         , fUniforms(kUniformsPerBlock)
60         , fSamplers(kUniformsPerBlock)
61         , fCurrentGeometryUBOOffset(0)
62         , fCurrentFragmentUBOOffset(0) {
63     }
64 
65     UniformHandle internalAddUniformArray(uint32_t visibility,
66                                           GrSLType type,
67                                           GrSLPrecision precision,
68                                           const char* name,
69                                           bool mangleName,
70                                           int arrayCount,
71                                           const char** outName) override;
72 
73     SamplerHandle addSampler(const GrTexture* texture,
74                              const GrSamplerState&,
75                              const char* name,
76                              const GrShaderCaps*) override;
77 
numSamplers()78     int numSamplers() const { return fSamplers.count(); }
samplerVariable(SamplerHandle handle)79     const GrShaderVar& samplerVariable(SamplerHandle handle) const override {
80         return fSamplers[handle.toIndex()].fVariable;
81     }
samplerSwizzle(SamplerHandle handle)82     GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
83         return fSamplerSwizzles[handle.toIndex()];
84     }
samplerVisibility(SamplerHandle handle)85     uint32_t samplerVisibility(SamplerHandle handle) const {
86         return fSamplers[handle.toIndex()].fVisibility;
87     }
88 
immutableSampler(UniformHandle u)89     const GrVkSampler* immutableSampler(UniformHandle u) const {
90         return fSamplers[u.toIndex()].fImmutableSampler;
91     }
92 
93     void appendUniformDecls(GrShaderFlags, SkString*) const override;
94 
hasGeometryUniforms()95     bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; }
hasFragmentUniforms()96     bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; }
97 
98 
getUniformInfo(UniformHandle u)99     const UniformInfo& getUniformInfo(UniformHandle u) const {
100         return fUniforms[u.toIndex()];
101     }
102 
103 
104     UniformInfoArray    fUniforms;
105     UniformInfoArray    fSamplers;
106     SkTArray<GrSwizzle> fSamplerSwizzles;
107 
108     uint32_t            fCurrentGeometryUBOOffset;
109     uint32_t            fCurrentFragmentUBOOffset;
110 
111     friend class GrVkPipelineStateBuilder;
112     friend class GrVkDescriptorSetManager;
113 
114     typedef GrGLSLUniformHandler INHERITED;
115 };
116 
117 #endif
118