blob: 73b17d23f92d8c22c3ccec29d4828c55d86a6ef9 [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"
Greg Daniel9a51a862018-11-30 10:18:14 -050012#include "GrSamplerState.h"
Brian Salomon99938a82016-11-21 13:41:08 -050013#include "GrShaderVar.h"
Greg Daniel9a51a862018-11-30 10:18:14 -050014#include "GrVkSampler.h"
Brian Salomon99938a82016-11-21 13:41:08 -050015#include "glsl/GrGLSLUniformHandler.h"
Greg Daniel9a51a862018-11-30 10:18:14 -050016#include "vk/GrVkTypes.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;
42 // The SamplerState, maxMipLevel, and ycbcrInfo are only valid if the GrSLType is a sampler
43 // and that sampler is used for sampling an external image with a ycbcr conversion.
44 const GrVkSampler* fImmutableSampler = nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -050045 };
46 typedef GrTAllocator<UniformInfo> UniformInfoArray;
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
56private:
57 explicit GrVkUniformHandler(GrGLSLProgramBuilder* program)
58 : INHERITED(program)
59 , fUniforms(kUniformsPerBlock)
Brian Salomon101b8442016-11-18 11:58:54 -050060 , fSamplers(kUniformsPerBlock)
Greg Daniel18f96022017-05-04 15:09:03 -040061 , fCurrentGeometryUBOOffset(0)
Greg Daniel31ec1442017-05-08 10:30:59 -040062 , fCurrentFragmentUBOOffset(0) {
Greg Daniel164a9f02016-02-22 09:56:40 -050063 }
64
65 UniformHandle internalAddUniformArray(uint32_t visibility,
66 GrSLType type,
67 GrSLPrecision precision,
68 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&,
75 const char* name,
76 const GrShaderCaps*) override;
egdaniel09aa1fc2016-04-20 07:09:46 -070077
Brian Salomon101b8442016-11-18 11:58:54 -050078 int numSamplers() const { return fSamplers.count(); }
Brian Salomon99938a82016-11-21 13:41:08 -050079 const GrShaderVar& samplerVariable(SamplerHandle handle) const override {
Brian Salomon101b8442016-11-18 11:58:54 -050080 return fSamplers[handle.toIndex()].fVariable;
81 }
82 GrSwizzle samplerSwizzle(SamplerHandle handle) const override {
83 return fSamplerSwizzles[handle.toIndex()];
84 }
85 uint32_t samplerVisibility(SamplerHandle handle) const {
86 return fSamplers[handle.toIndex()].fVisibility;
egdaniel09aa1fc2016-04-20 07:09:46 -070087 }
88
Greg Daniel9a51a862018-11-30 10:18:14 -050089 const GrVkSampler* immutableSampler(UniformHandle u) const {
90 return fSamplers[u.toIndex()].fImmutableSampler;
91 }
92
Greg Daniel164a9f02016-02-22 09:56:40 -050093 void appendUniformDecls(GrShaderFlags, SkString*) const override;
94
Greg Daniel18f96022017-05-04 15:09:03 -040095 bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; }
Greg Daniel164a9f02016-02-22 09:56:40 -050096 bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; }
97
98
99 const UniformInfo& getUniformInfo(UniformHandle u) const {
100 return fUniforms[u.toIndex()];
101 }
102
103
Brian Salomon101b8442016-11-18 11:58:54 -0500104 UniformInfoArray fUniforms;
105 UniformInfoArray fSamplers;
106 SkTArray<GrSwizzle> fSamplerSwizzles;
egdaniel09aa1fc2016-04-20 07:09:46 -0700107
Greg Daniel18f96022017-05-04 15:09:03 -0400108 uint32_t fCurrentGeometryUBOOffset;
Brian Salomon101b8442016-11-18 11:58:54 -0500109 uint32_t fCurrentFragmentUBOOffset;
Greg Daniel164a9f02016-02-22 09:56:40 -0500110
egdaniel22281c12016-03-23 13:49:40 -0700111 friend class GrVkPipelineStateBuilder;
egdaniel707bbd62016-07-26 07:19:47 -0700112 friend class GrVkDescriptorSetManager;
Greg Daniel164a9f02016-02-22 09:56:40 -0500113
114 typedef GrGLSLUniformHandler INHERITED;
115};
116
jvanverth633b3562016-03-23 11:01:22 -0700117#endif