blob: de6a3e46488a71a23f6a405871c3096cba54b15a [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/vk/GrVkTypes.h"
12#include "src/gpu/GrAllocator.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040013#include "src/gpu/GrSamplerState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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 {
Ethan Nicholas0be34802019-08-15 12:36:58 -040033 kUniformBinding = 0
Greg Daniel164a9f02016-02-22 09:56:40 -050034 };
35
Greg Daniel164a9f02016-02-22 09:56:40 -050036 struct UniformInfo {
Greg Daniel9a51a862018-11-30 10:18:14 -050037 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;
Sergey Ulanov2739fd22019-08-11 22:46:33 -070041 // fImmutableSampler is used for sampling an image with a ycbcr conversion.
Greg Daniel9a51a862018-11-30 10:18:14 -050042 const GrVkSampler* fImmutableSampler = nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -050043 };
44 typedef GrTAllocator<UniformInfo> UniformInfoArray;
45
Sergey Ulanov2739fd22019-08-11 22:46:33 -070046 ~GrVkUniformHandler() override;
47
Brian Salomon99938a82016-11-21 13:41:08 -050048 const GrShaderVar& getUniformVariable(UniformHandle u) const override {
Greg Daniel164a9f02016-02-22 09:56:40 -050049 return fUniforms[u.toIndex()].fVariable;
50 }
51
52 const char* getUniformCStr(UniformHandle u) const override {
53 return this->getUniformVariable(u).c_str();
54 }
55
Ethan Nicholas0be34802019-08-15 12:36:58 -040056 /**
57 * Returns the offset that the RTHeight synthetic uniform should use if it needs to be created.
58 */
59 uint32_t getRTHeightOffset() const;
60
Greg Daniel164a9f02016-02-22 09:56:40 -050061private:
62 explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
63 : INHERITED(program)
64 , fUniforms(kUniformsPerBlock)
Brian Salomon101b8442016-11-18 11:58:54 -050065 , fSamplers(kUniformsPerBlock)
Ethan Nicholas0be34802019-08-15 12:36:58 -040066 , fCurrentUBOOffset(0) {
Greg Daniel164a9f02016-02-22 09:56:40 -050067 }
68
69 UniformHandle internalAddUniformArray(uint32_t visibility,
70 GrSLType type,
Greg Daniel164a9f02016-02-22 09:56:40 -050071 const char* name,
72 bool mangleName,
73 int arrayCount,
74 const char** outName) override;
75
Ethan Nicholasd4efe682019-08-29 16:10:13 -040076 void updateUniformVisibility(UniformHandle u, uint32_t visibility) override {
77 fUniforms[u.toIndex()].fVisibility |= visibility;
78 }
79
Greg Daniel9a51a862018-11-30 10:18:14 -050080 SamplerHandle addSampler(const GrTexture* texture,
81 const GrSamplerState&,
Greg Daniel2c3398d2019-06-19 11:58:01 -040082 const GrSwizzle&,
Greg Daniel9a51a862018-11-30 10:18:14 -050083 const char* name,
84 const GrShaderCaps*) override;
egdaniel09aa1fc2016-04-20 07:09:46 -070085
Brian Salomon101b8442016-11-18 11:58:54 -050086 int numSamplers() const { return fSamplers.count(); }
Stephen Whited523a062019-06-19 13:12:46 -040087 const char* samplerVariable(SamplerHandle handle) const override {
88 return fSamplers[handle.toIndex()].fVariable.c_str();
Brian Salomon101b8442016-11-18 11:58:54 -050089 }
90 GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
91 return fSamplerSwizzles[handle.toIndex()];
92 }
93 uint32_t samplerVisibility(SamplerHandle handle) const {
94 return fSamplers[handle.toIndex()].fVisibility;
egdaniel09aa1fc2016-04-20 07:09:46 -070095 }
96
Greg Daniel9a51a862018-11-30 10:18:14 -050097 const GrVkSampler* immutableSampler(UniformHandle u) const {
98 return fSamplers[u.toIndex()].fImmutableSampler;
99 }
100
Greg Daniel164a9f02016-02-22 09:56:40 -0500101 void appendUniformDecls(GrShaderFlags, SkString*) const override;
102
Greg Daniel164a9f02016-02-22 09:56:40 -0500103 const UniformInfo& getUniformInfo(UniformHandle u) const {
104 return fUniforms[u.toIndex()];
105 }
106
107
Brian Salomon101b8442016-11-18 11:58:54 -0500108 UniformInfoArray fUniforms;
109 UniformInfoArray fSamplers;
110 SkTArray<GrSwizzle> fSamplerSwizzles;
egdaniel09aa1fc2016-04-20 07:09:46 -0700111
Ethan Nicholas0be34802019-08-15 12:36:58 -0400112 uint32_t fCurrentUBOOffset;
Greg Daniel164a9f02016-02-22 09:56:40 -0500113
egdaniel22281c12016-03-23 13:49:40 -0700114 friend class GrVkPipelineStateBuilder;
egdaniel707bbd62016-07-26 07:19:47 -0700115 friend class GrVkDescriptorSetManager;
Greg Daniel164a9f02016-02-22 09:56:40 -0500116
117 typedef GrGLSLUniformHandler INHERITED;
118};
119
jvanverth633b3562016-03-23 11:01:22 -0700120#endif