From 627c0d1b61a7d8b218d0aa589442bf5a336e8555 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Mon, 25 Sep 2023 15:08:36 +0300 Subject: [PATCH] more gpu memory work --- CMakeLists.txt | 2 +- src/Core/PS4/GPU/gpu_memory.cpp | 42 +++++++++++++++++-- src/Core/PS4/GPU/gpu_memory.h | 20 +++++++-- src/Core/PS4/HLE/Graphics/graphics_render.cpp | 9 ++++ src/Core/PS4/HLE/Graphics/graphics_render.h | 24 +++++++++++ src/Core/PS4/HLE/Graphics/video_out.cpp | 3 +- src/Core/PS4/HLE/Kernel/memory_management.cpp | 2 +- 7 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 src/Core/PS4/HLE/Graphics/graphics_render.cpp create mode 100644 src/Core/PS4/HLE/Graphics/graphics_render.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d720ec92..5d751971 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ add_executable(shadps4 src/Core/PS4/HLE/Kernel/event_queues.h src/Core/PS4/HLE/Kernel/cpu_management.cpp src/Core/PS4/HLE/Kernel/cpu_management.h - "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Lib/Timer.cpp" "src/Lib/Timer.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h" "src/Core/PS4/HLE/ErrorCodes.h" "src/debug.h" "src/Core/PS4/HLE/Kernel/memory_management.cpp" "src/Core/PS4/HLE/Kernel/memory_management.h" "src/Core/PS4/GPU/gpu_memory.cpp" "src/Core/PS4/GPU/gpu_memory.h" "src/emulator.cpp" "src/emulator.h" "src/Core/PS4/HLE/Kernel/Objects/event_queue.h" "src/Core/PS4/HLE/Kernel/Objects/event_queue.cpp" "src/Core/PS4/HLE/Graphics/Objects/video_out_ctx.cpp" "src/Core/PS4/HLE/Graphics/Objects/video_out_ctx.h" "src/Core/PS4/HLE/Graphics/graphics_ctx.h" "src/vulkan_util.cpp" "src/vulkan_util.h" "src/Core/PS4/GPU/video_out_buffer.cpp" "src/Core/PS4/GPU/video_out_buffer.h") + "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h" "src/Core/PS4/Util/aerolib.h" "src/Core/PS4/Loader/SymbolsResolver.h" "src/Core/PS4/Loader/SymbolsResolver.cpp" "src/Core/PS4/HLE/Libs.cpp" "src/Core/PS4/HLE/Libs.h" "src/Core/PS4/HLE/LibC.cpp" "src/Core/PS4/HLE/LibC.h" "src/Lib/Timer.cpp" "src/Lib/Timer.h" "src/Core/PS4/HLE/LibKernel.cpp" "src/Core/PS4/HLE/LibKernel.h" "src/Core/PS4/HLE/LibSceGnmDriver.cpp" "src/Core/PS4/HLE/LibSceGnmDriver.h" "src/Core/PS4/HLE/Kernel/ThreadManagement.cpp" "src/Core/PS4/HLE/Kernel/ThreadManagement.h" "src/Core/PS4/HLE/ErrorCodes.h" "src/debug.h" "src/Core/PS4/HLE/Kernel/memory_management.cpp" "src/Core/PS4/HLE/Kernel/memory_management.h" "src/Core/PS4/GPU/gpu_memory.cpp" "src/Core/PS4/GPU/gpu_memory.h" "src/emulator.cpp" "src/emulator.h" "src/Core/PS4/HLE/Kernel/Objects/event_queue.h" "src/Core/PS4/HLE/Kernel/Objects/event_queue.cpp" "src/Core/PS4/HLE/Graphics/Objects/video_out_ctx.cpp" "src/Core/PS4/HLE/Graphics/Objects/video_out_ctx.h" "src/Core/PS4/HLE/Graphics/graphics_ctx.h" "src/vulkan_util.cpp" "src/vulkan_util.h" "src/Core/PS4/GPU/video_out_buffer.cpp" "src/Core/PS4/GPU/video_out_buffer.h" "src/Core/PS4/HLE/Graphics/graphics_render.cpp" "src/Core/PS4/HLE/Graphics/graphics_render.h") find_package(OpenGL REQUIRED) target_link_libraries(shadps4 PUBLIC fmt mincore spdlog IMGUI SDL3-shared ${OPENGL_LIBRARY} vulkan-1 spirv-tools-opt spirv-tools) diff --git a/src/Core/PS4/GPU/gpu_memory.cpp b/src/Core/PS4/GPU/gpu_memory.cpp index 6b51f022..25b7f8c6 100644 --- a/src/Core/PS4/GPU/gpu_memory.cpp +++ b/src/Core/PS4/GPU/gpu_memory.cpp @@ -1,8 +1,42 @@ #include "gpu_memory.h" -namespace GPU { -void MemorySetAllocArea(u64 virtual_addr, u64 size) {} -void* memoryCreateObj(u64 submit_id, HLE::Libs::Graphics::GraphicCtx* ctx, void* todo, u64 virtual_addr, u64 size, const GPUObject& info) { +#include "Util/Singleton.h" + +void GPU::memorySetAllocArea(u64 virtual_addr, u64 size) { + auto* gpumemory = Singleton::Instance(); + + Lib::LockMutexGuard lock(gpumemory->m_mutex); + + MemoryHeap h; + h.allocated_virtual_addr = virtual_addr; + h.allocated_size = size; + + gpumemory->m_heaps.push_back(h); +} +void* GPU::memoryCreateObj(u64 submit_id, HLE::Libs::Graphics::GraphicCtx* ctx, void* todo, u64 virtual_addr, u64 size, const GPUObject& info) { + auto* gpumemory = Singleton::Instance(); + + Lib::LockMutexGuard lock(gpumemory->m_mutex); + + int heap_id = gpumemory->getHeapId(virtual_addr, size); + + if (heap_id < 0) + { + return nullptr; + } return nullptr; } -} // namespace GPU \ No newline at end of file + + +int GPU::GPUMemory::getHeapId(u64 virtual_addr, u64 size) { + int index = 0; + for (const auto& heap : m_heaps) { + if ((virtual_addr >= heap.allocated_virtual_addr && virtual_addr < heap.allocated_virtual_addr + heap.allocated_size) || + ((virtual_addr + size - 1) >= heap.allocated_virtual_addr && + (virtual_addr + size - 1) < heap.allocated_virtual_addr + heap.allocated_size)) { + return index; + } + index++; + } + return -1; +} diff --git a/src/Core/PS4/GPU/gpu_memory.h b/src/Core/PS4/GPU/gpu_memory.h index 043c075d..0a655b62 100644 --- a/src/Core/PS4/GPU/gpu_memory.h +++ b/src/Core/PS4/GPU/gpu_memory.h @@ -1,20 +1,34 @@ #pragma once -#include #include +#include +#include namespace GPU { enum class MemoryMode : u32 { NoAccess = 0, Read = 1, Write = 2, ReadWrite = 3 }; enum class MemoryObjectType : u64 { InvalidObj, VideoOutBufferObj }; -void MemorySetAllocArea(u64 virtual_addr, u64 size); +struct MemoryHeap { + u64 allocated_virtual_addr = 0; + u64 allocated_size = 0; +}; + +class GPUMemory { + public: + GPUMemory() {} + virtual ~GPUMemory() {} + int getHeapId(u64 vaddr, u64 size); + Lib::Mutex m_mutex; + std::vector m_heaps; +}; class GPUObject { public: GPUObject() = default; virtual ~GPUObject() = default; }; -void* memoryCreateObj(u64 submit_id, HLE::Libs::Graphics::GraphicCtx* ctx, /*CommandBuffer* buffer*/void* todo, u64 virtual_addr, u64 size, +void memorySetAllocArea(u64 virtual_addr, u64 size); +void* memoryCreateObj(u64 submit_id, HLE::Libs::Graphics::GraphicCtx* ctx, /*CommandBuffer* buffer*/ void* todo, u64 virtual_addr, u64 size, const GPUObject& info); } // namespace GPU \ No newline at end of file diff --git a/src/Core/PS4/HLE/Graphics/graphics_render.cpp b/src/Core/PS4/HLE/Graphics/graphics_render.cpp new file mode 100644 index 00000000..43d9e8ab --- /dev/null +++ b/src/Core/PS4/HLE/Graphics/graphics_render.cpp @@ -0,0 +1,9 @@ +#include "graphics_render.h" +#include "Util/Singleton.h" +#include "emulator.h" + +void GPU::renderCreateCtx() { + auto* render_ctx = Singleton::Instance(); + + render_ctx->setGraphicCtx(Emulator::getGraphicCtx()); +} diff --git a/src/Core/PS4/HLE/Graphics/graphics_render.h b/src/Core/PS4/HLE/Graphics/graphics_render.h new file mode 100644 index 00000000..9b019711 --- /dev/null +++ b/src/Core/PS4/HLE/Graphics/graphics_render.h @@ -0,0 +1,24 @@ +#pragma once +#include "graphics_ctx.h" + +namespace GPU { + +class Framebuffer { + public: + Framebuffer() {} + virtual ~Framebuffer() {} +}; +class RenderCtx { + public: + RenderCtx() : m_framebuffer(new Framebuffer) {} + + virtual ~RenderCtx() {} + void setGraphicCtx(HLE::Libs::Graphics::GraphicCtx* ctx) { m_graphic_ctx = ctx; } + private: + Framebuffer* m_framebuffer = nullptr; + HLE::Libs::Graphics::GraphicCtx* m_graphic_ctx = nullptr; +}; + + +void renderCreateCtx(); +}; // namespace GPU \ No newline at end of file diff --git a/src/Core/PS4/HLE/Graphics/video_out.cpp b/src/Core/PS4/HLE/Graphics/video_out.cpp index 52b1e034..7db6b895 100644 --- a/src/Core/PS4/HLE/Graphics/video_out.cpp +++ b/src/Core/PS4/HLE/Graphics/video_out.cpp @@ -16,6 +16,7 @@ #include "Util/Singleton.h" #include "emulator.h" #include +#include "graphics_render.h" namespace HLE::Libs::Graphics::VideoOut { @@ -163,7 +164,7 @@ s32 PS4_SYSV_ABI sceVideoOutRegisterBuffers(s32 handle, s32 startIndex, void* co int registration_index = ctx->buffers_registration_index++; Emulator::checkAndWaitForGraphicsInit(); - // TODO Graphics::RenderCreateCtx(); + GPU::renderCreateCtx(); // try to calculate buffer size u64 buffer_size = 1280 * 720 * 4; // TODO hardcoded value should be redone diff --git a/src/Core/PS4/HLE/Kernel/memory_management.cpp b/src/Core/PS4/HLE/Kernel/memory_management.cpp index 4897ad38..220e81be 100644 --- a/src/Core/PS4/HLE/Kernel/memory_management.cpp +++ b/src/Core/PS4/HLE/Kernel/memory_management.cpp @@ -120,7 +120,7 @@ int PS4_SYSV_ABI sceKernelMapDirectMemory(void** addr, u64 len, int prot, int fl } if (gpu_mode != GPU::MemoryMode::NoAccess) { - GPU::MemorySetAllocArea(out_addr, len); + GPU::memorySetAllocArea(out_addr, len); } return SCE_OK; }