blob: f2809709a93594941b9132454d3c34653a01e4b6 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrSamplerState.h"
12#include "include/gpu/vk/GrVkTypes.h"
13#include "src/gpu/GrAllocator.h"
14#include "src/gpu/GrShaderVar.h"
15#include "src/gpu/glsl/GrGLSLUniformHandler.h"
16#include "src/gpu/vk/GrVkSampler.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017
Greg Daniel164a9f02016-02-22 09:56:40 -050018class GrVkUniformHandler : public GrGLSLUniformHandler {
19public:
jvanverth633b3562016-03-23 11:01:22 -070020 static const int kUniformsPerBlock = 8;
21
Greg Daniel164a9f02016-02-22 09:56:40 -050022 enum {
Brian Salomonf7232642018-09-19 08:58:08 -040023 /**
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 */
egdanielb4aa3622016-04-06 13:47:08 -070029 kUniformBufferDescSet = 0,
30 kSamplerDescSet = 1,
Greg Daniel164a9f02016-02-22 09:56:40 -050031 };
32 enum {
Greg Daniel18f96022017-05-04 15:09:03 -040033 kGeometryBinding = 0,
Greg Daniel164a9f02016-02-22 09:56:40 -050034 kFragBinding = 1,
35 };
36
Greg Daniel164a9f02016-02-22 09:56:40 -050037 struct UniformInfo {
Greg Daniel9a51a862018-11-30 10:18:14 -050038 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;
Sergey Ulanov2739fd22019-08-11 22:46:33 -070042 // fImmutableSampler is used for sampling an image with a ycbcr conversion.
Greg Daniel9a51a862018-11-30 10:18:14 -050043 const GrVkSampler* fImmutableSampler = nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -050044 };
45 typedef GrTAllocator<UniformInfo> UniformInfoArray;
46
Sergey Ulanov2739fd22019-08-11 22:46:33 -070047 ~GrVkUniformHandler() override;
48
Brian Salomon99938a82016-11-21 13:41:08 -050049 const GrShaderVar& getUniformVariable(UniformHandle u) const override {
Greg Daniel164a9f02016-02-22 09:56:40 -050050 return fUniforms[u.toIndex()].fVariable;
51 }
52
53 const char* getUniformCStr(UniformHandle u) const override {
54 return this->getUniformVariable(u).c_str();
55 }
56
57private:
58 explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
59 : INHERITED(program)
60 , fUniforms(kUniformsPerBlock)
Brian Salomon101b8442016-11-18 11:58:54 -050061 , fSamplers(kUniformsPerBlock)
Greg Daniel18f96022017-05-04 15:09:03 -040062 , fCurrentGeometryUBOOffset(0)
Greg Daniel31ec1442017-05-08 10:30:59 -040063 , fCurrentFragmentUBOOffset(0) {
Greg Daniel164a9f02016-02-22 09:56:40 -050064 }
65
66 UniformHandle internalAddUniformArray(uint32_t visibility,
67 GrSLType type,
Greg Daniel164a9f02016-02-22 09:56:40 -050068 const char* name,
69 bool mangleName,
70 int arrayCount,
71 const char** outName) override;
72
Greg Daniel9a51a862018-11-30 10:18:14 -050073 SamplerHandle addSampler(const GrTexture* texture,
74 const GrSamplerState&,
Greg Daniel2c3398d2019-06-19 11:58:01 -040075 const GrSwizzle&,
Greg Daniel9a51a862018-11-30 10:18:14 -050076 const char* name,
77 const GrShaderCaps*) override;
egdaniel09aa1fc2016-04-20 07:09:46 -070078
Brian Salomon101b8442016-11-18 11:58:54 -050079 int numSamplers() const { return fSamplers.count(); }
Stephen Whited523a062019-06-19 13:12:46 -040080 const char* samplerVariable(SamplerHandle handle) const override {
81 return fSamplers[handle.toIndex()].fVariable.c_str();
Brian Salomon101b8442016-11-18 11:58:54 -050082 }
83 GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
84 return fSamplerSwizzles[handle.toIndex()];
85 }
86 uint32_t samplerVisibility(SamplerHandle handle) const {
87 return fSamplers[handle.toIndex()].fVisibility;
egdaniel09aa1fc2016-04-20 07:09:46 -070088 }
89
Greg Daniel9a51a862018-11-30 10:18:14 -050090 const GrVkSampler* immutableSampler(UniformHandle u) const {
91 return fSamplers[u.toIndex()].fImmutableSampler;
92 }
93
Greg Daniel164a9f02016-02-22 09:56:40 -050094 void appendUniformDecls(GrShaderFlags, SkString*) const override;
95
Greg Daniel18f96022017-05-04 15:09:03 -040096 bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; }
Greg Daniel164a9f02016-02-22 09:56:40 -050097 bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; }
98
99
100 const UniformInfo& getUniformInfo(UniformHandle u) const {
101 return fUniforms[u.toIndex()];
102 }
103
104
Brian Salomon101b8442016-11-18 11:58:54 -0500105 UniformInfoArray fUniforms;
106 UniformInfoArray fSamplers;
107 SkTArray<GrSwizzle> fSamplerSwizzles;
egdaniel09aa1fc2016-04-20 07:09:46 -0700108
Greg Daniel18f96022017-05-04 15:09:03 -0400109 uint32_t fCurrentGeometryUBOOffset;
Brian Salomon101b8442016-11-18 11:58:54 -0500110 uint32_t fCurrentFragmentUBOOffset;
Greg Daniel164a9f02016-02-22 09:56:40 -0500111
egdaniel22281c12016-03-23 13:49:40 -0700112 friend class GrVkPipelineStateBuilder;
egdaniel707bbd62016-07-26 07:19:47 -0700113 friend class GrVkDescriptorSetManager;
Greg Daniel164a9f02016-02-22 09:56:40 -0500114
115 typedef GrGLSLUniformHandler INHERITED;
116};
117
jvanverth633b3562016-03-23 11:01:22 -0700118#endif