Diligent Engine  v.2.4.g
BufferVkImpl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include "EngineVkImplTraits.hpp"
34 #include "BufferBase.hpp"
35 #include "BufferViewVkImpl.hpp" // Required by BufferBase
36 
37 #include "VulkanDynamicHeap.hpp"
40 #include "STDAllocator.hpp"
41 
42 namespace Diligent
43 {
44 
46 class BufferVkImpl final : public BufferBase<EngineVkImplTraits>
47 {
48 public:
50 
51  BufferVkImpl(IReferenceCounters* pRefCounters,
52  FixedBlockMemoryAllocator& BuffViewObjMemAllocator,
53  RenderDeviceVkImpl* pDeviceVk,
54  const BufferDesc& BuffDesc,
55  const BufferData* pBuffData = nullptr);
56 
57  BufferVkImpl(IReferenceCounters* pRefCounters,
58  FixedBlockMemoryAllocator& BuffViewObjMemAllocator,
59  class RenderDeviceVkImpl* pDeviceVk,
60  const BufferDesc& BuffDesc,
61  RESOURCE_STATE InitialState,
62  VkBuffer vkBuffer);
63  ~BufferVkImpl();
64 
66 
67 #ifdef DILIGENT_DEVELOPMENT
68  void DvpVerifyDynamicAllocation(DeviceContextVkImpl* pCtx) const;
69 #endif
70 
72  {
73  if (m_VulkanBuffer != VK_NULL_HANDLE)
74  {
75  return 0;
76  }
77  else
78  {
79  VERIFY(m_Desc.Usage == USAGE_DYNAMIC, "Dynamic buffer is expected");
80  VERIFY_EXPR(!m_DynamicData.empty());
81 #ifdef DILIGENT_DEVELOPMENT
82  DvpVerifyDynamicAllocation(pCtx);
83 #endif
84  auto& DynAlloc = m_DynamicData[CtxId];
85  return static_cast<Uint32>(DynAlloc.AlignedOffset);
86  }
87  }
88 
90  virtual VkBuffer DILIGENT_CALL_TYPE GetVkBuffer() const override final;
91 
93  virtual void* DILIGENT_CALL_TYPE GetNativeHandle() override final
94  {
95  auto vkBuffer = GetVkBuffer();
96  return reinterpret_cast<void*>(vkBuffer);
97  }
98 
100  virtual void DILIGENT_CALL_TYPE SetAccessFlags(VkAccessFlags AccessFlags) override final;
101 
103  virtual VkAccessFlags DILIGENT_CALL_TYPE GetAccessFlags() const override final;
104 
106  virtual VkDeviceAddress DILIGENT_CALL_TYPE GetVkDeviceAddress() const override final;
107 
108  bool CheckAccessFlags(VkAccessFlags AccessFlags) const
109  {
110  return (GetAccessFlags() & AccessFlags) == AccessFlags;
111  }
112 
114  {
116  return reinterpret_cast<Uint8*>(m_MemoryAllocation.Page->GetCPUMemory()) + m_BufferMemoryAlignedOffset;
117  }
118 
119 private:
120  friend class DeviceContextVkImpl;
121 
122  virtual void CreateViewInternal(const struct BufferViewDesc& ViewDesc, IBufferView** ppView, bool bIsDefaultView) override;
123 
124  VulkanUtilities::BufferViewWrapper CreateView(struct BufferViewDesc& ViewDesc);
125 
126  Uint32 m_DynamicOffsetAlignment = 0;
127  VkDeviceSize m_BufferMemoryAlignedOffset = 0;
128 
129  // TODO (assiduous): move dynamic allocations to device context.
130  static constexpr size_t CacheLineSize = 64;
131  struct alignas(CacheLineSize) CtxDynamicData : VulkanDynamicAllocation
132  {
133  CtxDynamicData() noexcept {}
134  CtxDynamicData(CtxDynamicData&&) = default;
135 
136  CtxDynamicData& operator=(VulkanDynamicAllocation&& Allocation)
137  {
138  *static_cast<VulkanDynamicAllocation*>(this) = std::move(Allocation);
139  return *this;
140  }
141 
142  Uint8 Padding[CacheLineSize - sizeof(VulkanDynamicAllocation)];
143  };
144  static_assert(sizeof(CtxDynamicData) == CacheLineSize, "Unexpected sizeof(CtxDynamicData)");
145  std::vector<CtxDynamicData, STDAllocatorRawMem<CtxDynamicData>> m_DynamicData;
146 
147  VulkanUtilities::BufferWrapper m_VulkanBuffer;
148  VulkanUtilities::VulkanMemoryAllocation m_MemoryAllocation;
149 };
150 
151 } // namespace Diligent
Diligent::BufferViewDesc
Buffer view description.
Definition: BufferView.h:88
Diligent::USAGE_STAGING
@ USAGE_STAGING
A resource that facilitates transferring data between GPU and CPU. D3D11 Counterpart: D3D11_USAGE_S...
Definition: GraphicsTypes.h:167
Diligent::VulkanDynamicAllocation
Definition: VulkanDynamicHeap.hpp:50
Diligent::DeviceObjectBase< EngineVkImplTraits ::BufferInterface, EngineVkImplTraits ::RenderDeviceImplType, BufferDesc >::operator=
DeviceObjectBase & operator=(const DeviceObjectBase &)=delete
Diligent::IReferenceCounters
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
Diligent::RenderDeviceVkImpl
Render device implementation in Vulkan backend.
Definition: RenderDeviceVkImpl.hpp:58
VulkanUtilities::BufferViewWrapper
DEFINE_VULKAN_OBJECT_WRAPPER(BufferView) BufferViewWrapper
Definition: VulkanLogicalDevice.hpp:70
Diligent::BufferVkImpl::GetVkBuffer
virtual VkBuffer GetVkBuffer() const override final
Implementation of IBufferVk::GetVkBuffer().
Definition: BufferVkImpl.cpp:476
STDAllocator.hpp
Diligent::BufferVkImpl::GetDynamicOffset
Uint32 GetDynamicOffset(Uint32 CtxId, DeviceContextVkImpl *pCtx) const
Definition: BufferVkImpl.hpp:71
Diligent::BufferVkImpl::CreateViewInternal
virtual void CreateViewInternal(const struct BufferViewDesc &ViewDesc, IBufferView **ppView, bool bIsDefaultView) override
Definition: BufferVkImpl.cpp:416
BufferBase.hpp
Diligent::USAGE_UNIFIED
@ USAGE_UNIFIED
A resource residing in a unified memory (e.g. memory shared between CPU and GPU), that can be read an...
Definition: GraphicsTypes.h:179
Diligent::BufferVkImpl::~BufferVkImpl
~BufferVkImpl()
Definition: BufferVkImpl.cpp:407
Diligent::BufferVkImpl::GetVkDeviceAddress
virtual VkDeviceAddress GetVkDeviceAddress() const override final
Implementation of IBufferVk::GetVkDeviceAddress().
Definition: BufferVkImpl.cpp:497
Diligent::DeviceObjectBase< EngineVkImplTraits ::BufferInterface, EngineVkImplTraits ::RenderDeviceImplType, BufferDesc >::m_Desc
BufferDesc m_Desc
Object description.
Definition: DeviceObjectBase.hpp:182
VulkanDynamicHeap.hpp
Diligent::USAGE_DYNAMIC
@ USAGE_DYNAMIC
A resource that can be read by the GPU and written at least once per frame by the CPU....
Definition: GraphicsTypes.h:161
Diligent::BufferBase
Template class implementing base functionality of the buffer object.
Definition: BufferBase.hpp:59
VulkanUtilities::BufferWrapper
DEFINE_VULKAN_OBJECT_WRAPPER(Buffer) BufferWrapper
Definition: VulkanLogicalDevice.hpp:69
Diligent::BufferVkImpl::BufferVkImpl
BufferVkImpl(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &BuffViewObjMemAllocator, RenderDeviceVkImpl *pDeviceVk, const BufferDesc &BuffDesc, const BufferData *pBuffData=nullptr)
Definition: BufferVkImpl.cpp:43
IMPLEMENT_QUERY_INTERFACE_IN_PLACE
#define IMPLEMENT_QUERY_INTERFACE_IN_PLACE(InterfaceID, ParentClassName)
Definition: ObjectBase.hpp:59
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::BufferVkImpl::GetNativeHandle
virtual void * GetNativeHandle() override final
Implementation of IBuffer::GetNativeHandle() in Vulkan backend.
Definition: BufferVkImpl.hpp:93
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::DeviceContextVkImpl
Device context implementation in Vulkan backend.
Definition: DeviceContextVkImpl.hpp:67
Diligent::BufferDesc
Buffer description.
Definition: Buffer.h:74
Diligent::BufferVkImpl::GetCPUAddress
void * GetCPUAddress()
Definition: BufferVkImpl.hpp:113
Diligent::BufferVkImpl::SetAccessFlags
virtual void SetAccessFlags(VkAccessFlags AccessFlags) override final
Implementation of IBufferVk::SetAccessFlags().
Definition: BufferVkImpl.cpp:487
Diligent::BufferVkImpl
Buffer object implementation in Vulkan backend.
Definition: BufferVkImpl.hpp:46
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
BufferViewVkImpl.hpp
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
VulkanUtilities::VulkanMemoryAllocation::Page
VulkanMemoryPage * Page
Definition: VulkanMemoryManager.hpp:90
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
VulkanObjectWrappers.hpp
Diligent::FixedBlockMemoryAllocator
Memory allocator that allocates memory in a fixed-size chunks.
Definition: FixedBlockMemoryAllocator.hpp:56
EngineVkImplTraits.hpp
Diligent::BufferDesc::Usage
USAGE Usage
Buffer usage, see Diligent::USAGE for details.
Definition: Buffer.h:88
Diligent::RESOURCE_STATE
RESOURCE_STATE
Resource usage state.
Definition: GraphicsTypes.h:2814
Diligent::IBufferView
Buffer view interface.
Definition: BufferView.h:155
VulkanUtilities::VulkanMemoryPage::GetCPUMemory
void * GetCPUMemory() const
Definition: VulkanMemoryManager.hpp:129
Diligent::BufferData
Describes the buffer initial data.
Definition: Buffer.h:155
Diligent::BufferVkImpl::CheckAccessFlags
bool CheckAccessFlags(VkAccessFlags AccessFlags) const
Definition: BufferVkImpl.hpp:108
VulkanUtilities::VulkanMemoryAllocation
Definition: VulkanMemoryManager.hpp:48
VulkanMemoryManager.hpp
Diligent::BufferVkImpl::GetAccessFlags
virtual VkAccessFlags GetAccessFlags() const override final
Implementation of IBufferVk::GetAccessFlags().
Definition: BufferVkImpl.cpp:492
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37