Diligent Engine API Reference
BufferD3D12Impl.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
28 
29 #include "BufferD3D12.h"
30 #include "RenderDeviceD3D12.h"
31 #include "BufferBase.h"
32 #include "BufferViewD3D12Impl.h"
33 #include "D3D12ResourceBase.h"
34 #include "DynamicUploadHeap.h"
35 
36 namespace Diligent
37 {
38 
39 class FixedBlockMemoryAllocator;
40 
42 class BufferD3D12Impl : public BufferBase<IBufferD3D12, BufferViewD3D12Impl, FixedBlockMemoryAllocator>, public D3D12ResourceBase
43 {
44 public:
46  BufferD3D12Impl(IReferenceCounters *pRefCounters,
47  FixedBlockMemoryAllocator &BuffViewObjMemAllocator,
48  class RenderDeviceD3D12Impl *pDeviceD3D12,
49  const BufferDesc& BuffDesc,
50  const BufferData &BuffData = BufferData());
51  BufferD3D12Impl(IReferenceCounters *pRefCounters,
52  FixedBlockMemoryAllocator &BuffViewObjMemAllocator,
53  class RenderDeviceD3D12Impl *pDeviceD3D12,
54  const BufferDesc& BuffDesc,
55  ID3D12Resource *pd3d12Buffer);
56  ~BufferD3D12Impl();
57 
58  virtual void QueryInterface( const Diligent::INTERFACE_ID &IID, IObject **ppInterface )override;
59 
60  virtual void UpdateData( IDeviceContext *pContext, Uint32 Offset, Uint32 Size, const PVoid pData )override;
61  virtual void CopyData( IDeviceContext *pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size )override;
62  virtual void Map( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData )override;
63  virtual void Unmap( IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags )override;
64 
65 #ifdef _DEBUG
66  void DbgVerifyDynamicAllocation(Uint32 ContextId);
67 #endif
68 
69  virtual ID3D12Resource *GetD3D12Buffer(size_t &DataStartByteOffset, Uint32 ContextId)override final
70  {
71  auto *pd3d12Resource = GetD3D12Resource();
72  if(pd3d12Resource != nullptr)
73  {
74  VERIFY(m_Desc.Usage != USAGE_DYNAMIC || (m_Desc.BindFlags | (BIND_SHADER_RESOURCE|BIND_UNORDERED_ACCESS)) != 0, "Expected non-dynamic buffer or a buffer with SRV or UAV bind flags");
75  DataStartByteOffset = 0;
76  return pd3d12Resource;
77  }
78  else
79  {
80  VERIFY(m_Desc.Usage == USAGE_DYNAMIC, "Dynamic buffer is expected");
81 
82 #ifdef _DEBUG
83  DbgVerifyDynamicAllocation(ContextId);
84 #endif
85  DataStartByteOffset = m_DynamicData[ContextId].Offset;
86  return m_DynamicData[ContextId].pBuffer;
87  }
88  }
89 
90  virtual void* GetNativeHandle()override final
91  {
92  VERIFY(GetD3D12Resource() != nullptr, "The buffer is dynamic and has no pointer to D3D12 resource");
93  size_t DataStartByteOffset = 0;
94  auto *pd3d12Buffer = GetD3D12Buffer(DataStartByteOffset, 0);
95  VERIFY(DataStartByteOffset == 0, "0 offset expected");
96  return pd3d12Buffer;
97  }
98 
99  virtual void SetD3D12ResourceState(D3D12_RESOURCE_STATES state)override final{ SetState(state); }
100 
101  D3D12_GPU_VIRTUAL_ADDRESS GetGPUAddress(Uint32 ContextId)
102  {
103  if(m_Desc.Usage == USAGE_DYNAMIC)
104  {
105 #ifdef _DEBUG
106  DbgVerifyDynamicAllocation(ContextId);
107 #endif
108  return m_DynamicData[ContextId].GPUAddress;
109  }
110  else
111  {
112  return GetD3D12Resource()->GetGPUVirtualAddress();
113  }
114  }
115 
116  D3D12_CPU_DESCRIPTOR_HANDLE GetCBVHandle(){return m_CBVDescriptorAllocation.GetCpuHandle();}
117 
118 private:
119  virtual void CreateViewInternal( const struct BufferViewDesc &ViewDesc, IBufferView **ppView, bool bIsDefaultView )override;
120 
121  void CreateUAV( struct BufferViewDesc &UAVDesc, D3D12_CPU_DESCRIPTOR_HANDLE UAVDescriptor );
122  void CreateSRV( struct BufferViewDesc &SRVDesc, D3D12_CPU_DESCRIPTOR_HANDLE SRVDescriptor );
123  void CreateCBV( D3D12_CPU_DESCRIPTOR_HANDLE CBVDescriptor );
124  DescriptorHeapAllocation m_CBVDescriptorAllocation;
125 
126 #ifdef _DEBUG
127  std::vector< std::pair<MAP_TYPE, Uint32>, STDAllocatorRawMem<std::pair<MAP_TYPE, Uint32>> > m_DbgMapType;
128 #endif
129 
130  friend class DeviceContextD3D12Impl;
131  // Array of dynamic allocations for every device context
132  std::vector<DynamicAllocation, STDAllocatorRawMem<DynamicAllocation> > m_DynamicData;
133 };
134 
135 }
Describes the buffer initial data.
Definition: Buffer.h:176
virtual ID3D12Resource * GetD3D12Buffer(size_t &DataStartByteOffset, Uint32 ContextId) override final
Returns a pointer to the ID3D12Resource interface of the internal Direct3D12 object.
Definition: BufferD3D12Impl.h:69
Implementation of the Diligent::IBufferD3D12 interface.
Definition: BufferD3D12Impl.h:42
A buffer or a texture can be bound as a shader resource.
Definition: GraphicsTypes.h:68
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Base implementation of a D3D12 resource.
Definition: D3D12ResourceBase.h:33
virtual void QueryInterface(const Diligent::INTERFACE_ID &IID, IObject **ppInterface) override
Queries the specific interface, see IObject::QueryInterface() for details.
USAGE Usage
Buffer usage, see Diligent::USAGE for details.
Definition: Buffer.h:71
virtual void UpdateData(IDeviceContext *pContext, Uint32 Offset, Uint32 Size, const PVoid pData) override
Base implementation of IBuffer::UpdateData(); validates input parameters.
Definition: BufferD3D12Impl.cpp:272
Template class implementing base functionality for a buffer object.
Definition: BufferBase.h:46
Buffer interface.
Definition: Buffer.h:200
Device context interface.
Definition: DeviceContext.h:443
virtual void * GetNativeHandle() override final
Returns native buffer handle specific to the underlying graphics API.
Definition: BufferD3D12Impl.h:90
Buffer description.
Definition: Buffer.h:57
Memory allocator that allocates memory in a fixed-size chunks.
Definition: FixedBlockMemoryAllocator.h:50
virtual void Map(IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags, PVoid &pMappedData) override
Base implementation of IBuffer::Map(); validates input parameters.
Definition: BufferD3D12Impl.cpp:289
BufferDesc m_Desc
Object description.
Definition: DeviceObjectBase.h:138
virtual void CopyData(IDeviceContext *pContext, IBuffer *pSrcBuffer, Uint32 SrcOffset, Uint32 DstOffset, Uint32 Size) override
Base implementation of IBuffer::CopyData(); validates input parameters.
Definition: BufferD3D12Impl.cpp:282
A buffer or a texture can be bound as an unordered access view.
Definition: GraphicsTypes.h:73
Uint32 BindFlags
Buffer bind flags, see Diligent::BIND_FLAGS for details.
Definition: Buffer.h:68
virtual void SetD3D12ResourceState(D3D12_RESOURCE_STATES state) override final
Sets the buffer usage state.
Definition: BufferD3D12Impl.h:99
virtual void Unmap(IDeviceContext *pContext, MAP_TYPE MapType, Uint32 MapFlags) override
Base implementation of IBuffer::Unmap()
Definition: BufferD3D12Impl.cpp:344
MAP_TYPE
Resource mapping type.
Definition: GraphicsTypes.h:125
A resource that can be read by the GPU and written at least once per frame by the CPU...
Definition: GraphicsTypes.h:99
Implementation of the Diligent::IRenderDeviceD3D12 interface.
Definition: RenderDeviceD3D12Impl.h:43