diff --git a/src/graphics/host_gpu/graphicContext.h b/src/graphics/host_gpu/graphicContext.h index 21be1c7f7..d852c613d 100644 --- a/src/graphics/host_gpu/graphicContext.h +++ b/src/graphics/host_gpu/graphicContext.h @@ -14,7 +14,6 @@ namespace Libs::Graphics { -struct VulkanBuffer; struct VulkanImage; struct VulkanMemory; @@ -118,11 +117,9 @@ private: }; struct VulkanMemory { - vk::MemoryRequirements requirements = {}; vk::MemoryPropertyFlags property = {}; vk::MemoryPropertyFlags preferred_property = {}; VmaAllocation allocation = nullptr; - uint32_t type = 0; }; struct VulkanImageState { @@ -138,7 +135,6 @@ struct VulkanImage { vk::Format format = vk::Format::eUndefined; vk::ImageType image_type = vk::ImageType::e2D; vk::Extent3D extent = {1, 1, 1}; - uint32_t guest_pitch = 0; uint32_t layers = 1; uint32_t mip_levels = 1; uint32_t samples = 1; @@ -150,12 +146,7 @@ struct VulkanImage { Graphics::VulkanMemory memory; }; -struct VulkanBuffer { - vk::Buffer buffer = nullptr; - VulkanMemory memory; - vk::BufferUsageFlags usage = {}; - uint64_t buffer_size = 0; -}; + } // namespace Libs::Graphics diff --git a/src/graphics/host_gpu/renderer/cache/streamBuffer.cpp b/src/graphics/host_gpu/renderer/cache/streamBuffer.cpp index 73b11d293..c6872f661 100644 --- a/src/graphics/host_gpu/renderer/cache/streamBuffer.cpp +++ b/src/graphics/host_gpu/renderer/cache/streamBuffer.cpp @@ -4,7 +4,6 @@ #include "common/profiler.h" #include "graphics/host_gpu/graphicContext.h" #include "graphics/host_gpu/renderer/commandScheduler.h" -#include "graphics/host_gpu/vma.h" #include #include @@ -64,7 +63,7 @@ constexpr size_t WATCHES_RESERVE_CHUNK = 0x1000; Buffer::Buffer(GraphicContext& graphics, CommandScheduler& scheduler, MemoryUsage usage, 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_buffer(std::make_unique()) { + m_size(size) { KYTY_PROFILER_FUNCTION(); EXIT_IF(graphics.allocator == nullptr || size == 0); @@ -87,53 +86,35 @@ Buffer::Buffer(GraphicContext& graphics, CommandScheduler& scheduler, MemoryUsag VkBuffer native_buffer = VK_NULL_HANDLE; const auto result = static_cast(vmaCreateBuffer( graphics.allocator, static_cast(buffer_info), &allocation_info, - &native_buffer, &m_buffer->memory.allocation, &allocation_result)); + &native_buffer, &m_allocation, &allocation_result)); if (result != vk::Result::eSuccess) { graphics.LogMemoryBudget(); } EXIT_NOT_IMPLEMENTED(result != vk::Result::eSuccess); - m_buffer->buffer = native_buffer; - m_buffer->usage = flags; - m_buffer->buffer_size = size; - m_buffer->memory.type = allocation_result.memoryType; - graphics.device.getBufferMemoryRequirements(m_buffer->buffer, &m_buffer->memory.requirements); - if (static_cast(flags & vk::BufferUsageFlagBits::eShaderDeviceAddress)) { + m_buffer = native_buffer; + if (with_bda) { vk::BufferDeviceAddressInfo address_info {}; - address_info.buffer = m_buffer->buffer; + address_info.buffer = m_buffer; m_device_address = graphics.device.getBufferAddress(address_info); EXIT_IF(m_device_address == 0); } VkMemoryPropertyFlags properties = 0; - vmaGetAllocationMemoryProperties(graphics.allocator, m_buffer->memory.allocation, &properties); - m_buffer->memory.property = vk::MemoryPropertyFlags(properties); + vmaGetAllocationMemoryProperties(graphics.allocator, m_allocation, &properties); + m_coherent = (properties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0; if (allocation_result.pMappedData != nullptr) { m_mapped = {static_cast(allocation_result.pMappedData), static_cast(size)}; } - VulkanTrackAllocation(m_buffer->memory); } Buffer::~Buffer() { - if (m_buffer->buffer != nullptr) { - VulkanUntrackAllocation(m_buffer->memory); - vmaDestroyBuffer(m_graphics->allocator, m_buffer->buffer, m_buffer->memory.allocation); + if (m_buffer != nullptr) { + vmaDestroyBuffer(m_graphics->allocator, m_buffer, m_allocation); } } -vk::Buffer Buffer::Handle() const noexcept { - return m_buffer->buffer; -} - -uint64_t Buffer::Size() const noexcept { - return m_buffer->buffer_size; -} - -bool Buffer::IsCoherent() const noexcept { - return bool(m_buffer->memory.property & vk::MemoryPropertyFlagBits::eHostCoherent); -} - vk::DeviceAddress Buffer::BufferDeviceAddress() const noexcept { EXIT_IF(m_device_address == 0); return m_device_address; @@ -153,7 +134,7 @@ void Buffer::Flush(uint64_t offset, uint64_t size) { EXIT_IF(m_mapped.empty() || offset > Size() || size > Size() - offset); if (!IsCoherent() && size != 0) { const auto result = - vmaFlushAllocation(m_graphics->allocator, m_buffer->memory.allocation, offset, size); + vmaFlushAllocation(m_graphics->allocator, m_allocation, offset, size); EXIT_NOT_IMPLEMENTED(static_cast(result) != vk::Result::eSuccess); } } @@ -162,7 +143,7 @@ void Buffer::Invalidate(uint64_t offset, uint64_t size) { EXIT_IF(m_usage != MemoryUsage::Download || offset > Size() || size > Size() - offset); if (!IsCoherent() && size != 0) { const auto result = - vmaInvalidateAllocation(m_graphics->allocator, m_buffer->memory.allocation, offset, size); + vmaInvalidateAllocation(m_graphics->allocator, m_allocation, offset, size); EXIT_NOT_IMPLEMENTED(static_cast(result) != vk::Result::eSuccess); } } diff --git a/src/graphics/host_gpu/renderer/cache/streamBuffer.h b/src/graphics/host_gpu/renderer/cache/streamBuffer.h index 0334e2de2..781704731 100644 --- a/src/graphics/host_gpu/renderer/cache/streamBuffer.h +++ b/src/graphics/host_gpu/renderer/cache/streamBuffer.h @@ -6,19 +6,19 @@ #include "graphics/host_gpu/vulkanCommon.h" #include -#include #include #include #include #include +VK_DEFINE_HANDLE(VmaAllocation) + namespace Libs::Graphics { class CommandBuffer; class CommandScheduler; struct StreamBufferTestAccess; struct GraphicContext; -struct VulkanBuffer; enum class MemoryUsage : uint8_t { DeviceLocal, @@ -42,10 +42,10 @@ public: ~Buffer(); KYTY_CLASS_NO_COPY(Buffer); - [[nodiscard]] vk::Buffer Handle() const noexcept; - [[nodiscard]] uint64_t Size() const noexcept; + [[nodiscard]] vk::Buffer Handle() const noexcept { return m_buffer; } + [[nodiscard]] uint64_t Size() const noexcept { return m_size; } [[nodiscard]] std::span Mapped() const noexcept { return m_mapped; } - [[nodiscard]] bool IsCoherent() const noexcept; + [[nodiscard]] bool IsCoherent() const noexcept { return m_coherent; } [[nodiscard]] MemoryUsage Usage() const noexcept { return m_usage; } [[nodiscard]] uint64_t CpuAddress() const noexcept { return m_cpu_address; } [[nodiscard]] vk::DeviceAddress BufferDeviceAddress() const noexcept; @@ -88,7 +88,10 @@ private: MemoryUsage m_usage = MemoryUsage::DeviceLocal; uint64_t m_cpu_address = 0; vk::DeviceAddress m_device_address = 0; - std::unique_ptr m_buffer; + vk::Buffer m_buffer = nullptr; + VmaAllocation m_allocation = nullptr; + uint64_t m_size; + bool m_coherent = false; std::span m_mapped; }; diff --git a/src/graphics/host_gpu/renderer/context.cpp b/src/graphics/host_gpu/renderer/context.cpp index 9c271b7c4..545cff9b0 100644 --- a/src/graphics/host_gpu/renderer/context.cpp +++ b/src/graphics/host_gpu/renderer/context.cpp @@ -9,7 +9,6 @@ #include "graphics/host_gpu/renderer/image/imageView.h" #include "graphics/host_gpu/renderer/render.h" #include "graphics/host_gpu/renderer/renderContext.h" -#include "graphics/host_gpu/vma.h" #include "graphics/host_gpu/vulkanCommon.h" #include diff --git a/src/graphics/host_gpu/renderer/image/image.cpp b/src/graphics/host_gpu/renderer/image/image.cpp index c3d947123..ade7f35ff 100644 --- a/src/graphics/host_gpu/renderer/image/image.cpp +++ b/src/graphics/host_gpu/renderer/image/image.cpp @@ -659,7 +659,6 @@ Image::Image(GraphicContext& graphics, CommandScheduler& scheduler, const ImageI backing.format = info.pixel_format; backing.image_type = HostImageType(info.type); backing.extent = info.extent; - backing.guest_pitch = info.pitch; backing.layers = info.IsVolume() ? 1u : info.resources.layers; backing.mip_levels = info.resources.levels; backing.samples = info.samples; diff --git a/src/graphics/host_gpu/renderer/pipeline/descriptors.cpp b/src/graphics/host_gpu/renderer/pipeline/descriptors.cpp index 4b72e7725..ebebcd09a 100644 --- a/src/graphics/host_gpu/renderer/pipeline/descriptors.cpp +++ b/src/graphics/host_gpu/renderer/pipeline/descriptors.cpp @@ -20,7 +20,6 @@ #include "graphics/host_gpu/renderer/pipeline/shaderResourceBarrier.h" #include "graphics/host_gpu/renderer/render.h" #include "graphics/host_gpu/renderer/renderContext.h" -#include "graphics/host_gpu/vma.h" #include "graphics/host_gpu/vulkanCommon.h" #include "graphics/shader/recompiler/ir/ShaderIR.h" #include "graphics/shader/recompiler/ir/passes/BindingLayout.h" diff --git a/src/graphics/host_gpu/vma.cpp b/src/graphics/host_gpu/vma.cpp index 70035bca6..2ea71ed50 100644 --- a/src/graphics/host_gpu/vma.cpp +++ b/src/graphics/host_gpu/vma.cpp @@ -18,37 +18,12 @@ #include "common/logging/log.h" #include "common/profiler.h" #include "graphics/host_gpu/graphicContext.h" -#include "graphics/host_gpu/vma.h" #include -#include #include namespace Libs::Graphics { -namespace { - -struct MemoryStats { - std::atomic_uint64_t allocated[VK_MAX_MEMORY_TYPES] {}; - std::atomic_uint64_t count[VK_MAX_MEMORY_TYPES] {}; -}; - -MemoryStats g_memory_stats; - -} // namespace - -void VulkanTrackAllocation(const VulkanMemory& memory) { - g_memory_stats.allocated[memory.type] += memory.requirements.size; - g_memory_stats.count[memory.type]++; -} - -void VulkanUntrackAllocation(const VulkanMemory& memory) { - EXIT_IF(g_memory_stats.allocated[memory.type] < memory.requirements.size); - EXIT_IF(g_memory_stats.count[memory.type] == 0); - g_memory_stats.allocated[memory.type] -= memory.requirements.size; - g_memory_stats.count[memory.type]--; -} - bool GraphicContext::CreateAllocator() { KYTY_PROFILER_FUNCTION(); EXIT_IF(instance == nullptr || physical_device == nullptr || device == nullptr || @@ -163,20 +138,16 @@ bool GraphicContext::CreateImage(const vk::ImageCreateInfo& image_info, VulkanIm alloc_info.preferredFlags = static_cast(memory.preferred_property); - VmaAllocationInfo allocation_result {}; vk::Image::CType native_image = VK_NULL_HANDLE; const auto result = static_cast( vmaCreateImage(allocator, static_cast(image_info), - &alloc_info, &native_image, &memory.allocation, &allocation_result)); + &alloc_info, &native_image, &memory.allocation, nullptr)); image.image = native_image; if (result != vk::Result::eSuccess) { LogMemoryBudget(); return false; } - device.getImageMemoryRequirements(image.image, &memory.requirements); - memory.type = allocation_result.memoryType; - VulkanTrackAllocation(memory); return true; } @@ -185,7 +156,6 @@ void GraphicContext::DeleteImage(VulkanImage& image) { EXIT_IF(allocator == nullptr || image.image == nullptr || image.memory.allocation == nullptr); auto& memory = image.memory; - VulkanUntrackAllocation(memory); vmaDestroyImage(allocator, image.image, memory.allocation); image.image = nullptr; memory.allocation = nullptr; diff --git a/src/graphics/host_gpu/vma.h b/src/graphics/host_gpu/vma.h deleted file mode 100644 index f24822284..000000000 --- a/src/graphics/host_gpu/vma.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef EMULATOR_SRC_GRAPHICS_HOST_GPU_VMA_H_ -#define EMULATOR_SRC_GRAPHICS_HOST_GPU_VMA_H_ - -#include "common/common.h" -#include "graphics/host_gpu/graphicContext.h" - -namespace Libs::Graphics { - -void VulkanTrackAllocation(const VulkanMemory& memory); -void VulkanUntrackAllocation(const VulkanMemory& memory); - -} // namespace Libs::Graphics - -#endif /* EMULATOR_SRC_GRAPHICS_HOST_GPU_VMA_H_ */ diff --git a/src/graphics/presentation/window/swapchain.cpp b/src/graphics/presentation/window/swapchain.cpp index 80feb306c..e4d5fc5b4 100644 --- a/src/graphics/presentation/window/swapchain.cpp +++ b/src/graphics/presentation/window/swapchain.cpp @@ -7,7 +7,6 @@ #include "graphics/host_gpu/graphicContext.h" #include "graphics/host_gpu/renderer/render.h" #include "graphics/host_gpu/renderer/renderContext.h" -#include "graphics/host_gpu/vma.h" #include "graphics/host_gpu/vulkanCommon.h" #include "graphics/presentation/presenter.h" #include "graphics/presentation/systemOverlay.h" diff --git a/src/graphics/presentation/window/vulkanWindow.cpp b/src/graphics/presentation/window/vulkanWindow.cpp index 713bc2aea..799495e9d 100644 --- a/src/graphics/presentation/window/vulkanWindow.cpp +++ b/src/graphics/presentation/window/vulkanWindow.cpp @@ -27,7 +27,6 @@ #include "graphics/host_gpu/graphicContext.h" #include "graphics/host_gpu/renderer/render.h" #include "graphics/host_gpu/renderer/renderContext.h" -#include "graphics/host_gpu/vma.h" #include "graphics/host_gpu/vulkanCommon.h" #include "graphics/presentation/presenter.h" #include "graphics/presentation/systemOverlay.h" diff --git a/src/graphics/presentation/window/window.cpp b/src/graphics/presentation/window/window.cpp index d74257d99..36d094053 100644 --- a/src/graphics/presentation/window/window.cpp +++ b/src/graphics/presentation/window/window.cpp @@ -30,7 +30,6 @@ #include "graphics/host_gpu/graphicContext.h" #include "graphics/host_gpu/renderer/render.h" #include "graphics/host_gpu/renderer/renderContext.h" -#include "graphics/host_gpu/vma.h" #include "graphics/host_gpu/vulkanCommon.h" #include "graphics/presentation/renderDoc.h" #include "graphics/presentation/systemOverlay.h" diff --git a/tests/shaderCfgTests.cpp b/tests/shaderCfgTests.cpp index f4901b0fb..5cce53ec7 100644 --- a/tests/shaderCfgTests.cpp +++ b/tests/shaderCfgTests.cpp @@ -1155,9 +1155,8 @@ void TestNativeShaderResourceDependencies() { Check(HasShaderBufferWrites(runtime), "graphics/compute write predicate lost a maximum-size strided buffer"); - VulkanBuffer buffer; - buffer.buffer = reinterpret_cast(uintptr_t{1}); - const auto gds_barrier = MakeGdsDependency(buffer.buffer); + const vk::Buffer buffer{reinterpret_cast(uintptr_t{1})}; + const auto gds_barrier = MakeGdsDependency(buffer); Check((gds_barrier.srcAccessMask & vk::AccessFlagBits::eHostWrite) && (gds_barrier.srcAccessMask & vk::AccessFlagBits::eTransferWrite) && (gds_barrier.srcAccessMask & vk::AccessFlagBits::eShaderWrite) &&