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 "GrShaderVar.h"
13 #include "glsl/GrGLSLUniformHandler.h"
14 
15 class GrVkUniformHandler : public GrGLSLUniformHandler {
16 public:
17     static const int kUniformsPerBlock = 8;
18 
19     enum {
20         kUniformBufferDescSet = 0,
21         kSamplerDescSet = 1,
22     };
23     enum {
24         kVertexBinding = 0,
25         kFragBinding = 1,
26     };
27 
28     // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler
29     struct UniformInfo {
30         GrShaderVar fVariable;
31         uint32_t        fVisibility;
32         uint32_t        fUBOffset;
33     };
34     typedef GrTAllocator<UniformInfo> UniformInfoArray;
35 
getUniformVariable(UniformHandle u)36     const GrShaderVar& getUniformVariable(UniformHandle u) const override {
37         return fUniforms[u.toIndex()].fVariable;
38     }
39 
getUniformCStr(UniformHandle u)40     const char* getUniformCStr(UniformHandle u) const override {
41         return this->getUniformVariable(u).c_str();
42     }
43 
44 private:
GrVkUniformHandler(GrGLSLProgramBuilder * program)45     explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
46         : INHERITED(program)
47         , fUniforms(kUniformsPerBlock)
48         , fSamplers(kUniformsPerBlock)
49         , fCurrentVertexUBOOffset(0)
50         , fCurrentFragmentUBOOffset(0)
51         , fCurrentSamplerBinding(0) {
52     }
53 
54     UniformHandle internalAddUniformArray(uint32_t visibility,
55                                           GrSLType type,
56                                           GrSLPrecision precision,
57                                           const char* name,
58                                           bool mangleName,
59                                           int arrayCount,
60                                           const char** outName) override;
61 
62     SamplerHandle addSampler(uint32_t visibility,
63                              GrSwizzle swizzle,
64                              GrSLType type,
65                              GrSLPrecision precision,
66                              const char* name) override;
67 
numSamplers()68     int numSamplers() const { return fSamplers.count(); }
samplerVariable(SamplerHandle handle)69     const GrShaderVar& samplerVariable(SamplerHandle handle) const override {
70         return fSamplers[handle.toIndex()].fVariable;
71     }
samplerSwizzle(SamplerHandle handle)72     GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
73         return fSamplerSwizzles[handle.toIndex()];
74     }
samplerVisibility(SamplerHandle handle)75     uint32_t samplerVisibility(SamplerHandle handle) const {
76         return fSamplers[handle.toIndex()].fVisibility;
77     }
78 
addImageStorage(uint32_t visibility,GrSLType,GrImageStorageFormat,GrSLMemoryModel,GrSLRestrict,GrIOType,const char * name)79     ImageStorageHandle addImageStorage(uint32_t visibility, GrSLType,  GrImageStorageFormat,
80                                        GrSLMemoryModel, GrSLRestrict, GrIOType,
81                                        const char* name) override {
82         SkFAIL("Image storages not implemented for Vulkan.");
83         return 0;
84     }
85 
imageStorageVariable(ImageStorageHandle handle)86     const GrShaderVar& imageStorageVariable(ImageStorageHandle handle) const override {
87         SkFAIL("Image storages not implemented for Vulkan.");
88         static const GrShaderVar* gVar = nullptr;
89         return *gVar;
90     }
91 
92     void appendUniformDecls(GrShaderFlags, SkString*) const override;
93 
hasVertexUniforms()94     bool hasVertexUniforms() const { return fCurrentVertexUBOOffset > 0; }
hasFragmentUniforms()95     bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; }
96 
97 
getUniformInfo(UniformHandle u)98     const UniformInfo& getUniformInfo(UniformHandle u) const {
99         return fUniforms[u.toIndex()];
100     }
101 
102 
103     UniformInfoArray    fUniforms;
104     UniformInfoArray    fSamplers;
105     SkTArray<GrSwizzle> fSamplerSwizzles;
106 
107     uint32_t            fCurrentVertexUBOOffset;
108     uint32_t            fCurrentFragmentUBOOffset;
109     uint32_t            fCurrentSamplerBinding;
110 
111     friend class GrVkPipelineStateBuilder;
112     friend class GrVkDescriptorSetManager;
113 
114     typedef GrGLSLUniformHandler INHERITED;
115 };
116 
117 #endif
118