blob: d79b1b3e85075f30e04909aa69855350dfb90607 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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
Greg Daniel164a9f02016-02-22 09:56:40 -050011#include "GrAllocator.h"
Brian Salomon99938a82016-11-21 13:41:08 -050012#include "GrShaderVar.h"
13#include "glsl/GrGLSLUniformHandler.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014
Greg Daniel164a9f02016-02-22 09:56:40 -050015class GrVkUniformHandler : public GrGLSLUniformHandler {
16public:
jvanverth633b3562016-03-23 11:01:22 -070017 static const int kUniformsPerBlock = 8;
18
Greg Daniel164a9f02016-02-22 09:56:40 -050019 enum {
Brian Salomonf7232642018-09-19 08:58:08 -040020 /**
21 * Binding a descriptor set invalidates all higher index descriptor sets. We must bind
22 * in the order of this enumeration. Samplers are after Uniforms because GrOps can specify
23 * GP textures as dynamic state, meaning they get rebound for each GrMesh in a draw while
24 * uniforms are bound once before all the draws.
25 */
egdanielb4aa3622016-04-06 13:47:08 -070026 kUniformBufferDescSet = 0,
27 kSamplerDescSet = 1,
Greg Daniel164a9f02016-02-22 09:56:40 -050028 };
29 enum {
Greg Daniel18f96022017-05-04 15:09:03 -040030 kGeometryBinding = 0,
Greg Daniel164a9f02016-02-22 09:56:40 -050031 kFragBinding = 1,
32 };
33
34 // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler
35 struct UniformInfo {
Brian Salomon99938a82016-11-21 13:41:08 -050036 GrShaderVar fVariable;
Greg Daniel164a9f02016-02-22 09:56:40 -050037 uint32_t fVisibility;
Greg Daniel164a9f02016-02-22 09:56:40 -050038 uint32_t fUBOffset;
39 };
40 typedef GrTAllocator<UniformInfo> UniformInfoArray;
41
Brian Salomon99938a82016-11-21 13:41:08 -050042 const GrShaderVar& getUniformVariable(UniformHandle u) const override {
Greg Daniel164a9f02016-02-22 09:56:40 -050043 return fUniforms[u.toIndex()].fVariable;
44 }
45
46 const char* getUniformCStr(UniformHandle u) const override {
47 return this->getUniformVariable(u).c_str();
48 }
49
50private:
51 explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
52 : INHERITED(program)
53 , fUniforms(kUniformsPerBlock)
Brian Salomon101b8442016-11-18 11:58:54 -050054 , fSamplers(kUniformsPerBlock)
Greg Daniel18f96022017-05-04 15:09:03 -040055 , fCurrentGeometryUBOOffset(0)
Greg Daniel31ec1442017-05-08 10:30:59 -040056 , fCurrentFragmentUBOOffset(0) {
Greg Daniel164a9f02016-02-22 09:56:40 -050057 }
58
59 UniformHandle internalAddUniformArray(uint32_t visibility,
60 GrSLType type,
61 GrSLPrecision precision,
62 const char* name,
63 bool mangleName,
64 int arrayCount,
65 const char** outName) override;
66
Brian Salomon101b8442016-11-18 11:58:54 -050067 SamplerHandle addSampler(uint32_t visibility,
68 GrSwizzle swizzle,
Brian Salomon60dd8c72018-07-30 10:24:13 -040069 GrTextureType type,
Brian Salomon101b8442016-11-18 11:58:54 -050070 GrSLPrecision precision,
71 const char* name) override;
egdaniel09aa1fc2016-04-20 07:09:46 -070072
Brian Salomon101b8442016-11-18 11:58:54 -050073 int numSamplers() const { return fSamplers.count(); }
Brian Salomon99938a82016-11-21 13:41:08 -050074 const GrShaderVar& samplerVariable(SamplerHandle handle) const override {
Brian Salomon101b8442016-11-18 11:58:54 -050075 return fSamplers[handle.toIndex()].fVariable;
76 }
77 GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
78 return fSamplerSwizzles[handle.toIndex()];
79 }
80 uint32_t samplerVisibility(SamplerHandle handle) const {
81 return fSamplers[handle.toIndex()].fVisibility;
egdaniel09aa1fc2016-04-20 07:09:46 -070082 }
83
Greg Daniel164a9f02016-02-22 09:56:40 -050084 void appendUniformDecls(GrShaderFlags, SkString*) const override;
85
Greg Daniel18f96022017-05-04 15:09:03 -040086 bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; }
Greg Daniel164a9f02016-02-22 09:56:40 -050087 bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; }
88
89
90 const UniformInfo& getUniformInfo(UniformHandle u) const {
91 return fUniforms[u.toIndex()];
92 }
93
94
Brian Salomon101b8442016-11-18 11:58:54 -050095 UniformInfoArray fUniforms;
96 UniformInfoArray fSamplers;
97 SkTArray<GrSwizzle> fSamplerSwizzles;
egdaniel09aa1fc2016-04-20 07:09:46 -070098
Greg Daniel18f96022017-05-04 15:09:03 -040099 uint32_t fCurrentGeometryUBOOffset;
Brian Salomon101b8442016-11-18 11:58:54 -0500100 uint32_t fCurrentFragmentUBOOffset;
Greg Daniel164a9f02016-02-22 09:56:40 -0500101
egdaniel22281c12016-03-23 13:49:40 -0700102 friend class GrVkPipelineStateBuilder;
egdaniel707bbd62016-07-26 07:19:47 -0700103 friend class GrVkDescriptorSetManager;
Greg Daniel164a9f02016-02-22 09:56:40 -0500104
105 typedef GrGLSLUniformHandler INHERITED;
106};
107
jvanverth633b3562016-03-23 11:01:22 -0700108#endif