Diligent Engine  v.2.4.g
BufferD3D12Impl.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 
34 #include "BufferBase.hpp"
35 #include "BufferViewD3D12Impl.hpp" // Required by BufferBase
36 #include "D3D12ResourceBase.hpp"
37 #include "D3D12DynamicHeap.hpp"
38 #include "DescriptorHeap.hpp"
39 
40 namespace Diligent
41 {
42 
44 class BufferD3D12Impl final : public BufferBase<EngineD3D12ImplTraits>, public D3D12ResourceBase
45 {
46 public:
48 
49  BufferD3D12Impl(IReferenceCounters* pRefCounters,
50  FixedBlockMemoryAllocator& BuffViewObjMemAllocator,
51  RenderDeviceD3D12Impl* pDeviceD3D12,
52  const BufferDesc& BuffDesc,
53  const BufferData* pBuffData = nullptr);
54 
55  BufferD3D12Impl(IReferenceCounters* pRefCounters,
56  FixedBlockMemoryAllocator& BuffViewObjMemAllocator,
57  RenderDeviceD3D12Impl* pDeviceD3D12,
58  const BufferDesc& BuffDesc,
59  RESOURCE_STATE InitialState,
60  ID3D12Resource* pd3d12Buffer);
62 
64 
65 #ifdef DILIGENT_DEVELOPMENT
66  void DvpVerifyDynamicAllocation(class DeviceContextD3D12Impl* pCtx) const;
67 #endif
68 
70  virtual ID3D12Resource* DILIGENT_CALL_TYPE GetD3D12Buffer(Uint64& DataStartByteOffset, IDeviceContext* pContext) override final;
71 
73  virtual void* DILIGENT_CALL_TYPE GetNativeHandle() override final
74  {
75  VERIFY(GetD3D12Resource() != nullptr, "The buffer is dynamic and has no pointer to D3D12 resource");
76  Uint64 DataStartByteOffset = 0;
77  auto* pd3d12Buffer = GetD3D12Buffer(DataStartByteOffset, 0);
78  VERIFY(DataStartByteOffset == 0, "0 offset expected");
79  return pd3d12Buffer;
80  }
81 
83  virtual void DILIGENT_CALL_TYPE SetD3D12ResourceState(D3D12_RESOURCE_STATES state) override final;
84 
86  virtual D3D12_RESOURCE_STATES DILIGENT_CALL_TYPE GetD3D12ResourceState() const override final;
87 
88  __forceinline D3D12_GPU_VIRTUAL_ADDRESS GetGPUAddress(Uint32 ContextId, class DeviceContextD3D12Impl* pCtx)
89  {
90  if (m_Desc.Usage == USAGE_DYNAMIC)
91  {
92 #ifdef DILIGENT_DEVELOPMENT
93  DvpVerifyDynamicAllocation(pCtx);
94 #endif
95  return m_DynamicData[ContextId].GPUAddress;
96  }
97  else
98  {
99  return GetD3D12Resource()->GetGPUVirtualAddress();
100  }
101  }
102 
103  __forceinline D3D12_GPU_VIRTUAL_ADDRESS GetGPUAddress()
104  {
106  return GetD3D12Resource()->GetGPUVirtualAddress();
107  }
108 
109  D3D12_CPU_DESCRIPTOR_HANDLE GetCBVHandle() { return m_CBVDescriptorAllocation.GetCpuHandle(); }
110 
111 private:
112  virtual void CreateViewInternal(const struct BufferViewDesc& ViewDesc, IBufferView** ppView, bool bIsDefaultView) override;
113 
114  void CreateUAV(struct BufferViewDesc& UAVDesc, D3D12_CPU_DESCRIPTOR_HANDLE UAVDescriptor);
115  void CreateSRV(struct BufferViewDesc& SRVDesc, D3D12_CPU_DESCRIPTOR_HANDLE SRVDescriptor);
116  void CreateCBV(D3D12_CPU_DESCRIPTOR_HANDLE CBVDescriptor);
117 
118  DescriptorHeapAllocation m_CBVDescriptorAllocation;
119 
120  // Align the struct size to the cache line size to avoid false sharing
121  static constexpr size_t CacheLineSize = 64;
122  struct alignas(CacheLineSize) CtxDynamicData : D3D12DynamicAllocation
123  {
124  CtxDynamicData& operator=(const D3D12DynamicAllocation& Allocation)
125  {
126  *static_cast<D3D12DynamicAllocation*>(this) = Allocation;
127  return *this;
128  }
129  Uint8 Padding[CacheLineSize - sizeof(D3D12DynamicAllocation)];
130  };
131  static_assert(sizeof(CtxDynamicData) == CacheLineSize, "Unexpected sizeof(CtxDynamicData)");
132 
133  friend class DeviceContextD3D12Impl;
134  // Array of dynamic allocations for every device context.
135  std::vector<CtxDynamicData, STDAllocatorRawMem<D3D12DynamicAllocation>> m_DynamicData;
136 };
137 
138 } // namespace Diligent
Diligent::BufferViewDesc
Buffer view description.
Definition: BufferView.h:88
D3D12ResourceBase.hpp
Diligent::DeviceObjectBase< EngineD3D12ImplTraits ::BufferInterface, EngineD3D12ImplTraits ::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
DescriptorHeap.hpp
Diligent::BufferD3D12Impl::GetGPUAddress
__forceinline D3D12_GPU_VIRTUAL_ADDRESS GetGPUAddress()
Definition: BufferD3D12Impl.hpp:103
Diligent::BufferD3D12Impl::SetD3D12ResourceState
virtual void SetD3D12ResourceState(D3D12_RESOURCE_STATES state) override final
Implementation of IBufferD3D12::SetD3D12ResourceState().
Definition: BufferD3D12Impl.cpp:415
Diligent::BufferD3D12Impl::GetD3D12ResourceState
virtual D3D12_RESOURCE_STATES GetD3D12ResourceState() const override final
Implementation of IBufferD3D12::GetD3D12ResourceState().
Definition: BufferD3D12Impl.cpp:420
Diligent::Uint64
uint64_t Uint64
64-bit unsigned integer
Definition: BasicTypes.h:50
BufferBase.hpp
BufferViewD3D12Impl.hpp
Diligent::DeviceObjectBase< EngineD3D12ImplTraits ::BufferInterface, EngineD3D12ImplTraits ::RenderDeviceImplType, BufferDesc >::m_Desc
BufferDesc m_Desc
Object description.
Definition: DeviceObjectBase.hpp:182
Diligent::BufferD3D12Impl::GetNativeHandle
virtual void * GetNativeHandle() override final
Implementation of IBuffer::GetNativeHandle().
Definition: BufferD3D12Impl.hpp:73
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
Diligent::BufferD3D12Impl
Buffer object implementation in Direct3D12 backend.
Definition: BufferD3D12Impl.hpp:44
Diligent::BufferD3D12Impl::BufferD3D12Impl
BufferD3D12Impl(IReferenceCounters *pRefCounters, FixedBlockMemoryAllocator &BuffViewObjMemAllocator, RenderDeviceD3D12Impl *pDeviceD3D12, const BufferDesc &BuffDesc, const BufferData *pBuffData=nullptr)
Definition: BufferD3D12Impl.cpp:44
Diligent::RenderDeviceD3D12Impl
Render device implementation in Direct3D12 backend.
Definition: RenderDeviceD3D12Impl.hpp:70
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::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::DescriptorHeapAllocation::GetCpuHandle
D3D12_CPU_DESCRIPTOR_HANDLE GetCpuHandle(Uint32 Offset=0) const
Definition: DescriptorHeap.hpp:162
Diligent::BufferDesc
Buffer description.
Definition: Buffer.h:74
EngineD3D12ImplTraits.hpp
Diligent::BufferD3D12Impl::~BufferD3D12Impl
~BufferD3D12Impl()
Definition: BufferD3D12Impl.cpp:306
Diligent::BufferD3D12Impl::GetCBVHandle
D3D12_CPU_DESCRIPTOR_HANDLE GetCBVHandle()
Definition: BufferD3D12Impl.hpp:109
Diligent::IDeviceContext
Device context interface.
Definition: DeviceContext.h:1460
Diligent::DeviceContextD3D12Impl
Device context implementation in Direct3D12 backend.
Definition: DeviceContextD3D12Impl.hpp:59
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
Diligent::BufferD3D12Impl::CreateViewInternal
virtual void CreateViewInternal(const struct BufferViewDesc &ViewDesc, IBufferView **ppView, bool bIsDefaultView) override
Definition: BufferD3D12Impl.cpp:312
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::FixedBlockMemoryAllocator
Memory allocator that allocates memory in a fixed-size chunks.
Definition: FixedBlockMemoryAllocator.hpp:56
Diligent::BufferDesc::Usage
USAGE Usage
Buffer usage, see Diligent::USAGE for details.
Definition: Buffer.h:88
Diligent::D3D12DynamicAllocation
Definition: D3D12DynamicHeap.hpp:40
Diligent::RESOURCE_STATE
RESOURCE_STATE
Resource usage state.
Definition: GraphicsTypes.h:2814
Diligent::IBufferView
Buffer view interface.
Definition: BufferView.h:155
D3D12DynamicHeap.hpp
Diligent::BufferData
Describes the buffer initial data.
Definition: Buffer.h:155
Diligent::DescriptorHeapAllocation
Definition: DescriptorHeap.hpp:67
Diligent::D3D12ResourceBase
Base implementation of a D3D12 resource.
Definition: D3D12ResourceBase.hpp:37
Diligent::D3D12ResourceBase::GetD3D12Resource
ID3D12Resource * GetD3D12Resource() const
Definition: D3D12ResourceBase.hpp:43
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::BufferD3D12Impl::GetD3D12Buffer
virtual ID3D12Resource * GetD3D12Buffer(Uint64 &DataStartByteOffset, IDeviceContext *pContext) override final
Implementation of IBufferD3D12::GetD3D12Buffer().
Definition: BufferD3D12Impl.cpp:382