Skip to content

Commit

Permalink
host-visible buffer for DX12
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Jul 1, 2024
1 parent 5c2a37d commit 7f54aa8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 60 deletions.
17 changes: 13 additions & 4 deletions Engine/gapi/directx12/dxallocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "guid.h"

#include <Tempest/Pixmap>
#include <iostream>

using namespace Tempest;
using namespace Tempest::Detail;
Expand Down Expand Up @@ -59,10 +58,16 @@ DxAllocator::Provider::DeviceMemory DxAllocator::Provider::alloc(size_t size, ui
heapDesc.Flags |= D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS;
}
else if(bufferHeap==BufferHeap::Upload) {
heapDesc.Properties.Type = D3D12_HEAP_TYPE_UPLOAD;
// heapDesc.Properties.Type = D3D12_HEAP_TYPE_UPLOAD;
heapDesc.Properties.Type = D3D12_HEAP_TYPE_CUSTOM;
heapDesc.Properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE;
heapDesc.Properties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}
else if(bufferHeap==BufferHeap::Readback) {
heapDesc.Properties.Type = D3D12_HEAP_TYPE_READBACK;
// heapDesc.Properties.Type = D3D12_HEAP_TYPE_READBACK;
heapDesc.Properties.Type = D3D12_HEAP_TYPE_CUSTOM;
heapDesc.Properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_WRITE_BACK;
heapDesc.Properties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
}

ID3D12Heap* ret = nullptr;
Expand Down Expand Up @@ -125,7 +130,8 @@ DxBuffer DxAllocator::alloc(const void* mem, size_t size, MemUsage usage, Buffer

D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_COMMON;
if(bufFlg==BufferHeap::Upload) {
state = D3D12_RESOURCE_STATE_GENERIC_READ;
// state = D3D12_RESOURCE_STATE_GENERIC_READ;
state = D3D12_RESOURCE_STATE_COMMON;
}
else if(bufFlg==BufferHeap::Readback) {
state = D3D12_RESOURCE_STATE_COPY_DEST;
Expand All @@ -139,6 +145,9 @@ DxBuffer DxAllocator::alloc(const void* mem, size_t size, MemUsage usage, Buffer
resDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
state |= D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE;
}
if(MemUsage::Indirect==(usage&MemUsage::Indirect)) {
resDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}
if(MemUsage::ScratchBuffer==(usage&MemUsage::ScratchBuffer)) {
resDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
Expand Down
32 changes: 3 additions & 29 deletions Engine/gapi/directx12/dxbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "dxbuffer.h"
#include "dxdevice.h"
#include <cassert>

using namespace Tempest;
using namespace Tempest::Detail;
Expand Down Expand Up @@ -40,7 +39,7 @@ void DxBuffer::update(const void* data, size_t off, size_t size) {
ID3D12Resource& ret = *impl;
ret.GetHeapProperties(&prop,nullptr);

if(prop.Type==D3D12_HEAP_TYPE_UPLOAD) {
if(prop.Type==D3D12_HEAP_TYPE_UPLOAD || prop.Type==D3D12_HEAP_TYPE_CUSTOM) {
dx.dataMgr().waitFor(this); // write-after-write case
updateByMapped(*this,data,off,size);
return;
Expand All @@ -58,7 +57,7 @@ void DxBuffer::fill(uint32_t data, size_t off, size_t size) {
ID3D12Resource& ret = *impl;
ret.GetHeapProperties(&prop,nullptr);

if(prop.Type==D3D12_HEAP_TYPE_UPLOAD) {
if(prop.Type==D3D12_HEAP_TYPE_UPLOAD || prop.Type==D3D12_HEAP_TYPE_CUSTOM) {
dx.dataMgr().waitFor(this); // write-after-write case
fillByMapped(*this,data,off,size);
return;
Expand All @@ -76,7 +75,7 @@ void DxBuffer::read(void* data, size_t off, size_t size) {
ID3D12Resource& ret = *impl;
ret.GetHeapProperties(&prop,nullptr);

if(prop.Type==D3D12_HEAP_TYPE_READBACK) {
if(prop.Type==D3D12_HEAP_TYPE_READBACK || prop.Type==D3D12_HEAP_TYPE_CUSTOM) {
dx.dataMgr().waitFor(this); // write-after-write case
readFromMapped(*this,data,off,size);
return;
Expand Down Expand Up @@ -199,29 +198,4 @@ void DxBuffer::readFromMapped(DxBuffer& stage, void* data, size_t off, size_t si
stage.impl->Unmap(0,nullptr);
}


DxBufferWithStaging::DxBufferWithStaging(DxBuffer&& base, BufferHeap stagingHeap)
:DxBuffer(std::move(base)) {
if(stagingHeap==BufferHeap::Readback) {
auto stage = dev->dataMgr().allocStagingMemory(nullptr,sizeInBytes,MemUsage::TransferDst,BufferHeap::Readback);
stagingR = Detail::DSharedPtr<DxBuffer*>(new Detail::DxBuffer(std::move(stage)));
}
if(stagingHeap==BufferHeap::Upload) {
auto stage = dev->dataMgr().allocStagingMemory(nullptr,sizeInBytes,MemUsage::TransferSrc,BufferHeap::Upload);
stagingU = Detail::DSharedPtr<DxBuffer*>(new Detail::DxBuffer(std::move(stage)));
}
}

void DxBufferWithStaging::update(const void* data, size_t off, size_t size) {
if(stagingU.handler==nullptr)
return DxBuffer::update(data,off,size);
updateByStaging(stagingU.handler,data,off,off,size);
}

void DxBufferWithStaging::read(void* data, size_t off, size_t size) {
if(stagingR.handler==nullptr)
return DxBuffer::read(data,off,size);
readFromStaging(*stagingR.handler,data,off,size);
}

#endif
12 changes: 0 additions & 12 deletions Engine/gapi/directx12/dxbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ class DxBuffer : public AbstractGraphicsApi::Buffer {
void readFromMapped (DxBuffer& stage, void* data, size_t off, size_t size);
};

class DxBufferWithStaging : public DxBuffer {
public:
DxBufferWithStaging(DxBuffer&& base, BufferHeap stagingHeap);

void update(const void* data, size_t off, size_t size) override;
void read ( void* data, size_t off, size_t size) override;

private:
Detail::DSharedPtr<DxBuffer*> stagingU = {};
Detail::DSharedPtr<DxBuffer*> stagingR = {};
};

}
}

15 changes: 0 additions & 15 deletions Engine/gapi/directx12api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,28 +193,13 @@ AbstractGraphicsApi::Fence* DirectX12Api::createFence(AbstractGraphicsApi::Devic
AbstractGraphicsApi::PBuffer DirectX12Api::createBuffer(AbstractGraphicsApi::Device* d, const void* mem, size_t size,
MemUsage usage, BufferHeap flg) {
DxDevice& dx = *reinterpret_cast<DxDevice*>(d);

const BufferHeap flgOrig = flg;
if(MemUsage::StorageBuffer==(usage&MemUsage::StorageBuffer))
flg = BufferHeap::Device;

usage = usage|MemUsage::TransferSrc|MemUsage::TransferDst;

if(flg==BufferHeap::Upload) {
DxBuffer buf=dx.allocator.alloc(mem,size,usage,BufferHeap::Upload);
return PBuffer(new DxBuffer(std::move(buf)));
}

if(flg!=flgOrig && flgOrig!=BufferHeap::Device) {
DxBuffer base = dx.allocator.alloc(nullptr,size,usage,flg);
Detail::DSharedPtr<DxBuffer*> buf(new DxBufferWithStaging(std::move(base),flgOrig));
if(mem!=nullptr)
buf.handler->update(mem,0,size);
else if((usage&MemUsage::Initialized)==MemUsage::Initialized)
buf.handler->fill(0x0,0,size);
return PBuffer(buf.handler);
}

DxBuffer base = dx.allocator.alloc(nullptr,size,usage,flg);
Detail::DSharedPtr<DxBuffer*> buf(new Detail::DxBuffer(std::move(base)));
if(mem==nullptr && (usage&MemUsage::Initialized)==MemUsage::Initialized) {
Expand Down

0 comments on commit 7f54aa8

Please sign in to comment.