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 "include/gpu/vk/GrVkTypes.h"
12 #include "src/gpu/GrAllocator.h"
13 #include "src/gpu/GrSamplerState.h"
14 #include "src/gpu/GrShaderVar.h"
15 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
16 #include "src/gpu/vk/GrVkSampler.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         kUniformBinding = 0
34     };
35 
36     struct UniformInfo {
37         GrShaderVar             fVariable;
38         uint32_t                fVisibility;
39         // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler
40         uint32_t                fUBOffset;
41         // fImmutableSampler is used for sampling an image with a ycbcr conversion.
42         const GrVkSampler*      fImmutableSampler = nullptr;
43     };
44     typedef GrTAllocator<UniformInfo> UniformInfoArray;
45 
46     ~GrVkUniformHandler() override;
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     /**
57      * Returns the offset that the RTHeight synthetic uniform should use if it needs to be created.
58      */
59     uint32_t getRTHeightOffset() const;
60 
61 private:
GrVkUniformHandler(GrGLSLProgramBuilder * program)62     explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
63         : INHERITED(program)
64         , fUniforms(kUniformsPerBlock)
65         , fSamplers(kUniformsPerBlock)
66         , fCurrentUBOOffset(0) {
67     }
68 
69     UniformHandle internalAddUniformArray(uint32_t visibility,
70                                           GrSLType type,
71                                           const char* name,
72                                           bool mangleName,
73                                           int arrayCount,
74                                           const char** outName) override;
75 
updateUniformVisibility(UniformHandle u,uint32_t visibility)76     void updateUniformVisibility(UniformHandle u, uint32_t visibility) override {
77         fUniforms[u.toIndex()].fVisibility |= visibility;
78     }
79 
80     SamplerHandle addSampler(const GrTextureProxy*,
81                              const GrSamplerState&,
82                              const GrSwizzle&,
83                              const char* name,
84                              const GrShaderCaps*) override;
85 
numSamplers()86     int numSamplers() const { return fSamplers.count(); }
samplerVariable(SamplerHandle handle)87     const char* samplerVariable(SamplerHandle handle) const override {
88         return fSamplers[handle.toIndex()].fVariable.c_str();
89     }
samplerSwizzle(SamplerHandle handle)90     GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
91         return fSamplerSwizzles[handle.toIndex()];
92     }
samplerVisibility(SamplerHandle handle)93     uint32_t samplerVisibility(SamplerHandle handle) const {
94         return fSamplers[handle.toIndex()].fVisibility;
95     }
96 
immutableSampler(UniformHandle u)97     const GrVkSampler* immutableSampler(UniformHandle u) const {
98         return fSamplers[u.toIndex()].fImmutableSampler;
99     }
100 
101     void appendUniformDecls(GrShaderFlags, SkString*) const override;
102 
getUniformInfo(UniformHandle u)103     const UniformInfo& getUniformInfo(UniformHandle u) const {
104         return fUniforms[u.toIndex()];
105     }
106 
107 
108     UniformInfoArray    fUniforms;
109     UniformInfoArray    fSamplers;
110     SkTArray<GrSwizzle> fSamplerSwizzles;
111 
112     uint32_t            fCurrentUBOOffset;
113 
114     friend class GrVkPipelineStateBuilder;
115     friend class GrVkDescriptorSetManager;
116 
117     typedef GrGLSLUniformHandler INHERITED;
118 };
119 
120 #endif
121