[D3D12] Unify UploadBufferPool page size (2 MB), add alignment parameter

This commit is contained in:
Triang3l
2020-09-15 22:13:53 +03:00
parent dfbe36a8aa
commit 2cebd3cabe
9 changed files with 111 additions and 79 deletions

View File

@@ -287,8 +287,7 @@ bool D3D12ImmediateDrawer::Initialize() {
device->CreateSampler(&sampler_desc, sampler_handle);
// Create pools for draws.
vertex_buffer_pool_ =
std::make_unique<UploadBufferPool>(provider, 2 * 1024 * 1024);
vertex_buffer_pool_ = std::make_unique<UploadBufferPool>(provider);
texture_descriptor_pool_ = std::make_unique<DescriptorHeapPool>(
device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 2048);
texture_descriptor_pool_heap_index_ = DescriptorHeapPool::kHeapIndexInvalid;
@@ -506,8 +505,8 @@ void D3D12ImmediateDrawer::BeginDrawBatch(const ImmediateDrawBatch& batch) {
vertex_buffer_view.SizeInBytes =
batch.vertex_count * uint32_t(sizeof(ImmediateVertex));
void* vertex_buffer_mapping = vertex_buffer_pool_->Request(
current_fence_value, vertex_buffer_view.SizeInBytes, nullptr, nullptr,
&vertex_buffer_view.BufferLocation);
current_fence_value, vertex_buffer_view.SizeInBytes, sizeof(uint32_t),
nullptr, nullptr, &vertex_buffer_view.BufferLocation);
if (vertex_buffer_mapping == nullptr) {
XELOGE("Failed to get a buffer for {} vertices in the immediate drawer",
batch.vertex_count);
@@ -524,8 +523,7 @@ void D3D12ImmediateDrawer::BeginDrawBatch(const ImmediateDrawBatch& batch) {
index_buffer_view.SizeInBytes = batch.index_count * sizeof(uint16_t);
index_buffer_view.Format = DXGI_FORMAT_R16_UINT;
void* index_buffer_mapping = vertex_buffer_pool_->Request(
current_fence_value,
xe::align(index_buffer_view.SizeInBytes, UINT(sizeof(uint32_t))),
current_fence_value, index_buffer_view.SizeInBytes, sizeof(uint16_t),
nullptr, nullptr, &index_buffer_view.BufferLocation);
if (index_buffer_mapping == nullptr) {
XELOGE("Failed to get a buffer for {} indices in the immediate drawer",

View File

@@ -13,14 +13,20 @@
#include "xenia/base/assert.h"
#include "xenia/base/logging.h"
#include "xenia/base/math.h"
#include "xenia/ui/d3d12/d3d12_util.h"
namespace xe {
namespace ui {
namespace d3d12 {
// Align to D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT not to waste any space if
// it's smaller (the size of the heap backing the buffer will be aligned to
// D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT anyway).
UploadBufferPool::UploadBufferPool(D3D12Provider& provider, uint32_t page_size)
: provider_(provider), page_size_(page_size) {}
: provider_(provider),
page_size_(xe::align(
page_size, uint32_t(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT))) {}
UploadBufferPool::~UploadBufferPool() { ClearCache(); }
@@ -68,9 +74,13 @@ void UploadBufferPool::ClearCache() {
}
uint8_t* UploadBufferPool::Request(uint64_t submission_index, uint32_t size,
uint32_t alignment,
ID3D12Resource** buffer_out,
uint32_t* offset_out,
D3D12_GPU_VIRTUAL_ADDRESS* gpu_address_out) {
assert_not_zero(alignment);
assert_true(xe::is_pow2(alignment));
size = xe::align(size, alignment);
assert_true(size <= page_size_);
if (size > page_size_) {
return nullptr;
@@ -79,7 +89,8 @@ uint8_t* UploadBufferPool::Request(uint64_t submission_index, uint32_t size,
submission_index >= writable_first_->last_submission_index);
assert_true(!submitted_last_ ||
submission_index >= submitted_last_->last_submission_index);
if (page_size_ - current_page_used_ < size || !writable_first_) {
uint32_t current_page_used_aligned = xe::align(current_page_used_, alignment);
if (current_page_used_aligned + size > page_size_ || !writable_first_) {
// Start a new page if can't fit all the bytes or don't have an open page.
if (writable_first_) {
// Close the page that was current.
@@ -128,33 +139,39 @@ uint8_t* UploadBufferPool::Request(uint64_t submission_index, uint32_t size,
writable_last_ = writable_first_;
}
current_page_used_ = 0;
current_page_used_aligned = 0;
}
writable_first_->last_submission_index = submission_index;
if (buffer_out) {
*buffer_out = writable_first_->buffer;
}
if (offset_out) {
*offset_out = current_page_used_;
*offset_out = current_page_used_aligned;
}
if (gpu_address_out) {
*gpu_address_out = writable_first_->gpu_address + current_page_used_;
*gpu_address_out = writable_first_->gpu_address + current_page_used_aligned;
}
uint8_t* mapping =
reinterpret_cast<uint8_t*>(writable_first_->mapping) + current_page_used_;
current_page_used_ += size;
uint8_t* mapping = reinterpret_cast<uint8_t*>(writable_first_->mapping) +
current_page_used_aligned;
current_page_used_ = current_page_used_aligned + size;
return mapping;
}
uint8_t* UploadBufferPool::RequestPartial(
uint64_t submission_index, uint32_t size, ID3D12Resource** buffer_out,
uint32_t* offset_out, uint32_t* size_out,
uint64_t submission_index, uint32_t size, uint32_t alignment,
ID3D12Resource** buffer_out, uint32_t* offset_out, uint32_t* size_out,
D3D12_GPU_VIRTUAL_ADDRESS* gpu_address_out) {
assert_not_zero(alignment);
assert_true(xe::is_pow2(alignment));
size = xe::align(size, alignment);
size = std::min(size, page_size_);
if (current_page_used_ < page_size_) {
size = std::min(size, page_size_ - current_page_used_);
uint32_t current_page_used_aligned = xe::align(current_page_used_, alignment);
if (current_page_used_aligned + alignment <= page_size_) {
size = std::min(
size, (page_size_ - current_page_used_aligned) & ~(alignment - 1));
}
uint8_t* mapping =
Request(submission_index, size, buffer_out, offset_out, gpu_address_out);
uint8_t* mapping = Request(submission_index, size, alignment, buffer_out,
offset_out, gpu_address_out);
if (!mapping) {
return nullptr;
}

View File

@@ -23,7 +23,12 @@ namespace d3d12 {
class UploadBufferPool {
public:
UploadBufferPool(D3D12Provider& provider, uint32_t page_size);
// Taken from the Direct3D 12 MiniEngine sample (LinearAllocator
// kCpuAllocatorPageSize). Large enough for most cases.
static constexpr uint32_t kDefaultPageSize = 2 * 1024 * 1024;
UploadBufferPool(D3D12Provider& provider,
uint32_t page_size = kDefaultPageSize);
~UploadBufferPool();
void Reclaim(uint64_t completed_submission_index);
@@ -31,13 +36,13 @@ class UploadBufferPool {
// Request to write data in a single piece, creating a new page if the current
// one doesn't have enough free space.
uint8_t* Request(uint64_t submission_index, uint32_t size,
uint8_t* Request(uint64_t submission_index, uint32_t size, uint32_t alignment,
ID3D12Resource** buffer_out, uint32_t* offset_out,
D3D12_GPU_VIRTUAL_ADDRESS* gpu_address_out);
// Request to write data in multiple parts, filling the buffer entirely.
uint8_t* RequestPartial(uint64_t submission_index, uint32_t size,
ID3D12Resource** buffer_out, uint32_t* offset_out,
uint32_t* size_out,
uint32_t alignment, ID3D12Resource** buffer_out,
uint32_t* offset_out, uint32_t* size_out,
D3D12_GPU_VIRTUAL_ADDRESS* gpu_address_out);
private: