Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/kyty/graphics/host_gpu/graphicContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct GraphicContext {
vk::Device device = nullptr;
VmaAllocator allocator = nullptr;
bool memory_budget_ext_enabled = false;
bool partial_buffers_enabled = false;
bool host_import_enabled = false;
uint64_t host_import_alignment = 0;
bool compute_subgroup_size_control_enabled = false;
Expand Down
5 changes: 3 additions & 2 deletions core/kyty/graphics/host_gpu/renderer/cache/bufferCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ void BufferCache::ChangeRegister(BufferId id) {
for (uint64_t i = 0; i < size_pages; ++i) {
addresses.push_back(buffer.BufferDeviceAddress() + (i << CACHING_PAGEBITS));
}
m_bda_pagetable_buffer.Commit(table_offset, addresses.size() * sizeof(vk::DeviceAddress));
WriteDataBuffer(m_bda_pagetable_buffer, table_offset,
addresses.data(), addresses.size() * sizeof(vk::DeviceAddress));
} else {
Expand Down Expand Up @@ -360,8 +361,7 @@ BufferCache::BufferCache(GraphicContext& graphics, CommandScheduler& scheduler,
: m_graphics(graphics), m_scheduler(scheduler), m_resources(resources),
m_fault_manager(graphics, scheduler, *this),
m_gds_buffer(graphics, scheduler, MemoryUsage::Stream, 0, AllFlags, GdsBufferSize),
m_bda_pagetable_buffer(graphics, scheduler, MemoryUsage::DeviceLocal, 0, AllFlags,
BDA_PAGETABLE_SIZE),
m_bda_pagetable_buffer(graphics, scheduler, Buffer::SparseResident {}, AllFlags, BDA_PAGETABLE_SIZE),
m_memory_tracker(page_manager),
m_staging_buffer(graphics, scheduler, MemoryUsage::Upload, 512 * MiB),
m_stream_buffer(graphics, scheduler, MemoryUsage::Stream, 64 * MiB),
Expand All @@ -379,6 +379,7 @@ BufferCache::BufferCache(GraphicContext& graphics, CommandScheduler& scheduler,
for (auto* buffer: {&m_bda_pagetable_buffer, m_fault_manager.GetFaultBuffer()}) {
buffer->Fill(0, buffer->Size(), 0);
}
m_bda_pagetable_buffer.Commit(NATIVE_RT_CONTEXT_PAGE * sizeof(vk::DeviceAddress), sizeof(vk::DeviceAddress));
const auto null_id =
m_slot_buffers.insert(m_graphics, m_scheduler, MemoryUsage::DeviceLocal, 0, AllFlags, 16);
EXIT_IF(null_id != NULL_BUFFER_ID);
Expand Down
1 change: 1 addition & 0 deletions core/kyty/graphics/host_gpu/renderer/cache/bufferCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class BufferCache {
friend struct BufferCacheTestAccess;
friend struct BufferCacheGcTestAccess;
friend struct IndirectDispatchTestAccess;
friend struct BufferCacheBdaTestAccess;
friend class TextureCache;

[[nodiscard]] std::pair<Buffer*, uint64_t> ObtainSynchronizedBuffer(
Expand Down
161 changes: 161 additions & 0 deletions core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "graphics/host_gpu/graphicContext.h"
#include "graphics/host_gpu/renderer/commandScheduler.h"

#include <array>
#include <cinttypes>
#include <cstring>
#include <numeric>
#include <vk_mem_alloc.h>
Expand Down Expand Up @@ -63,7 +65,157 @@ Buffer::Buffer(GraphicContext& graphics, CommandScheduler& scheduler, MemoryUsag
uint64_t cpu_address, vk::BufferUsageFlags flags, uint64_t size)
: m_graphics(&graphics), m_scheduler(&scheduler), m_usage(usage), m_cpu_address(cpu_address),
m_size(size) {
Create(flags);
}

Buffer::Buffer(GraphicContext& graphics, CommandScheduler& scheduler, SparseResident,
vk::BufferUsageFlags flags, uint64_t size)
: m_graphics(&graphics), m_scheduler(&scheduler), m_usage(MemoryUsage::DeviceLocal), m_size(size) {
if (!graphics.partial_buffers_enabled || (flags & vk::BufferUsageFlagBits::eShaderDeviceAddress)) {
Create(flags);
return;
}
vk::BufferCreateInfo buffer_info {};
buffer_info.flags = vk::BufferCreateFlagBits::eSparseBinding | vk::BufferCreateFlagBits::eSparseResidency;
buffer_info.size = size;
buffer_info.usage = flags;
const std::array<uint32_t, 2> families {graphics.queue_family, graphics.readback_family};
if (graphics.readback_family != static_cast<uint32_t>(-1)) {
buffer_info.sharingMode = vk::SharingMode::eConcurrent;
buffer_info.queueFamilyIndexCount = 2;
buffer_info.pQueueFamilyIndices = families.data();
}
if (graphics.device.createBuffer(&buffer_info, nullptr, &m_buffer) != vk::Result::eSuccess) {
m_buffer = nullptr;
Create(flags);
return;
}
const auto requirements = graphics.device.getBufferMemoryRequirements(m_buffer);
m_sparse_block = requirements.alignment;
m_sparse_types = requirements.memoryTypeBits;
m_sparse_size = requirements.size;
m_sparse_blocks.assign((m_sparse_size + m_sparse_block - 1) / m_sparse_block, nullptr);
#if defined(ANYPS5_KYTY)
m_device_local = true;
#endif
}

void Buffer::Commit(uint64_t offset, uint64_t size) {
if (m_sparse_block == 0 || size == 0) {
return;
}
EXIT_IF(offset > m_size || size > m_size - offset);
auto& graphics = *m_graphics;
const auto first = offset / m_sparse_block;
const auto last = (offset + size - 1) / m_sparse_block;
std::vector<uint64_t> blocks;
for (auto block = first; block <= last; block++) {
if (m_sparse_blocks[block] == nullptr) {
blocks.push_back(block);
}
}
if (blocks.empty()) {
return;
}
vk::BufferCreateInfo zero_info {};
zero_info.size = m_sparse_block;
zero_info.usage = vk::BufferUsageFlagBits::eTransferDst;
std::vector<vk::Buffer> zero_buffers(blocks.size(), nullptr);
std::vector<vk::SparseMemoryBind> binds;
binds.reserve(blocks.size());
for (size_t i = 0; i < blocks.size(); i++) {
const auto block = blocks[i];
EXIT_IF(graphics.device.createBuffer(&zero_info, nullptr, &zero_buffers[i]) != vk::Result::eSuccess);
const auto zero_requirements = graphics.device.getBufferMemoryRequirements(zero_buffers[i]);
VkMemoryRequirements memory {};
memory.size = std::max(m_sparse_block, zero_requirements.size);
memory.alignment = std::max(m_sparse_block, zero_requirements.alignment);
memory.memoryTypeBits = m_sparse_types & zero_requirements.memoryTypeBits;
VmaAllocationCreateInfo alloc_info {};
alloc_info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
VmaAllocationInfo allocated {};
const auto result =
vmaAllocateMemory(graphics.allocator, &memory, &alloc_info, &m_sparse_blocks[block], &allocated);
if (result != VK_SUCCESS) {
std::array<GraphicContext::HeapUsage, VK_MAX_MEMORY_HEAPS> heaps {};
const auto heap_count = graphics.GetHeapUsage(heaps);
for (uint32_t heap = 0; heap < heap_count; heap++) {
LOGF("Buffer: heap %u%s: %" PRIu64 " MiB used of a %" PRIu64 " MiB budget\n", heap,
heaps[heap].device_local ? " (device local)" : "", heaps[heap].usage >> 20u,
heaps[heap].budget >> 20u);
}
EXIT("Buffer: could not allocate sparse block %" PRIu64 " of %" PRIu64 " bytes: %s\n", block,
m_sparse_block, vk::to_string(static_cast<vk::Result>(result)).c_str());
}
EXIT_IF(graphics.device.bindBufferMemory(zero_buffers[i], allocated.deviceMemory, allocated.offset) !=
vk::Result::eSuccess);
vk::SparseMemoryBind bind {};
bind.resourceOffset = block * m_sparse_block;
bind.size = std::min(m_sparse_block, m_sparse_size - block * m_sparse_block);
bind.memory = allocated.deviceMemory;
bind.memoryOffset = allocated.offset;
binds.push_back(bind);
}
vk::CommandPoolCreateInfo pool_info {};
pool_info.flags = vk::CommandPoolCreateFlagBits::eTransient;
pool_info.queueFamilyIndex = graphics.queue_family;
vk::CommandPool pool = nullptr;
EXIT_IF(graphics.device.createCommandPool(&pool_info, nullptr, &pool) != vk::Result::eSuccess);
vk::CommandBufferAllocateInfo allocate {};
allocate.commandPool = pool;
allocate.level = vk::CommandBufferLevel::ePrimary;
allocate.commandBufferCount = 1;
vk::CommandBuffer command = nullptr;
EXIT_IF(graphics.device.allocateCommandBuffers(&allocate, &command) != vk::Result::eSuccess);
vk::CommandBufferBeginInfo begin_info {};
begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
EXIT_IF(command.begin(&begin_info) != vk::Result::eSuccess);
for (const auto zero_buffer: zero_buffers) {
command.fillBuffer(zero_buffer, 0, VK_WHOLE_SIZE, 0);
}
EXIT_IF(command.end() != vk::Result::eSuccess);
vk::SemaphoreCreateInfo semaphore_info {};
vk::Semaphore zeroed = nullptr;
EXIT_IF(graphics.device.createSemaphore(&semaphore_info, nullptr, &zeroed) != vk::Result::eSuccess);
vk::FenceCreateInfo fence_info {};
vk::Fence fence = nullptr;
EXIT_IF(graphics.device.createFence(&fence_info, nullptr, &fence) != vk::Result::eSuccess);
vk::SubmitInfo submit {};
submit.commandBufferCount = 1;
submit.pCommandBuffers = &command;
submit.signalSemaphoreCount = 1;
submit.pSignalSemaphores = &zeroed;
vk::SparseBufferMemoryBindInfo buffer_bind {m_buffer, static_cast<uint32_t>(binds.size()), binds.data()};
vk::BindSparseInfo bind_info {};
bind_info.waitSemaphoreCount = 1;
bind_info.pWaitSemaphores = &zeroed;
bind_info.bufferBindCount = 1;
bind_info.pBufferBinds = &buffer_bind;
{
Common::LockGuard lock {graphics.queue_mutex};
EXIT_IF(graphics.queue.submit(1, &submit, nullptr) != vk::Result::eSuccess);
const auto result = graphics.queue.bindSparse(1, &bind_info, fence);
if (result != vk::Result::eSuccess) {
EXIT("Buffer: sparse bind of %zu blocks at 0x%" PRIx64 " failed: %s (block %" PRIu64
", memory size %" PRIu64 ", buffer size %" PRIu64 ")\n",
binds.size(), binds.front().resourceOffset, vk::to_string(result).c_str(), m_sparse_block,
m_sparse_size, m_size);
}
}
EXIT_IF(graphics.device.waitForFences(1, &fence, VK_TRUE, UINT64_MAX) != vk::Result::eSuccess);
graphics.device.destroyFence(fence, nullptr);
graphics.device.destroySemaphore(zeroed, nullptr);
graphics.device.destroyCommandPool(pool, nullptr);
for (const auto zero_buffer: zero_buffers) {
graphics.device.destroyBuffer(zero_buffer, nullptr);
}
}

void Buffer::Create(vk::BufferUsageFlags flags) {
KYTY_PROFILER_FUNCTION();
auto& graphics = *m_graphics;
const auto usage = m_usage;
const auto size = m_size;
EXIT_IF(graphics.allocator == nullptr || size == 0);

vk::BufferCreateInfo buffer_info {};
Expand Down Expand Up @@ -138,6 +290,15 @@ Buffer::~Buffer() {
if (m_buffer == nullptr) {
return;
}
if (m_sparse_block != 0) {
m_graphics->device.destroyBuffer(m_buffer);
for (auto* block: m_sparse_blocks) {
if (block != nullptr) {
vmaFreeMemory(m_graphics->allocator, block);
}
}
return;
}
if (m_in_place) {
m_graphics->device.destroyBuffer(m_buffer);
} else {
Expand Down
10 changes: 10 additions & 0 deletions core/kyty/graphics/host_gpu/renderer/cache/streamBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class Buffer {
uint64_t cpu_address, vk::BufferUsageFlags flags, uint64_t size);
Buffer(GraphicContext& graphics, CommandScheduler& scheduler, uint64_t cpu_address, uint64_t size,
vk::Buffer imported, vk::DeviceAddress device_address);
struct SparseResident {};
Buffer(GraphicContext& graphics, CommandScheduler& scheduler, SparseResident sparse, vk::BufferUsageFlags flags,
uint64_t size);
~Buffer();
KYTY_CLASS_NO_COPY(Buffer);

Expand All @@ -55,6 +58,7 @@ class Buffer {
#endif
[[nodiscard]] MemoryUsage Usage() const noexcept { return m_usage; }
[[nodiscard]] bool InPlace() const noexcept { return m_in_place; }
[[nodiscard]] bool IsSparseResident() const noexcept { return m_sparse_block != 0; }
[[nodiscard]] uint64_t CpuAddress() const noexcept { return m_cpu_address; }
[[nodiscard]] vk::DeviceAddress BufferDeviceAddress() const noexcept {
EXIT_IF(m_device_address == 0);
Expand All @@ -74,6 +78,7 @@ class Buffer {
void CopyFrom(CommandBuffer& command, const Buffer& source, uint64_t source_offset,
uint64_t destination_offset, uint64_t size);
void Fill(uint64_t offset, uint64_t size, uint32_t value);
void Commit(uint64_t offset, uint64_t size);

bool is_deleted = false;
int stream_score = 0;
Expand All @@ -86,6 +91,7 @@ class Buffer {
[[nodiscard]] CommandScheduler& Scheduler() const noexcept { return *m_scheduler; }

private:
void Create(vk::BufferUsageFlags flags);
[[nodiscard]] vk::BufferMemoryBarrier Barrier(uint64_t offset, uint64_t size,
vk::AccessFlags source,
vk::AccessFlags destination) const;
Expand All @@ -104,6 +110,10 @@ class Buffer {
bool m_device_local = false;
#endif
std::span<uint8_t> m_mapped;
uint64_t m_sparse_block = 0;
uint32_t m_sparse_types = 0;
uint64_t m_sparse_size = 0;
std::vector<VmaAllocation> m_sparse_blocks;
};

class StreamBuffer final: public Buffer {
Expand Down
11 changes: 11 additions & 0 deletions core/kyty/graphics/presentation/window/vulkanWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,17 @@ static vk::Device VulkanCreateDevice(GraphicContext& graphics,
graphics.indirect_draws_enabled = device_features.multiDrawIndirect == VK_TRUE &&
device_features.drawIndirectFirstInstance == VK_TRUE &&
features12.drawIndirectCount == VK_TRUE;
{
const auto families = physical_device.getQueueFamilyProperties();
const auto sparse = physical_device.getProperties().sparseProperties;
graphics.partial_buffers_enabled =
supported2.features.sparseBinding == VK_TRUE && supported2.features.sparseResidencyBuffer == VK_TRUE &&
sparse.residencyNonResidentStrict == VK_TRUE && queue_family < families.size() &&
static_cast<bool>(families[queue_family].queueFlags & vk::QueueFlagBits::eSparseBinding);
device_features.sparseBinding = graphics.partial_buffers_enabled ? VK_TRUE : VK_FALSE;
device_features.sparseResidencyBuffer = graphics.partial_buffers_enabled ? VK_TRUE : VK_FALSE;
LOGF("Vulkan partially resident buffers: %s\n", graphics.partial_buffers_enabled ? "true" : "false");
}
device_features.shaderInt64 = VK_TRUE;
device_features.shaderFloat64 =
supported2.features.shaderFloat64 && float_controls.shaderSignedZeroInfNanPreserveFloat64;
Expand Down
Loading
Loading