diff --git a/src/Core/PS4/HLE/Graphics/graphics_ctx.h b/src/Core/PS4/HLE/Graphics/graphics_ctx.h index 051949b5..4496b5ea 100644 --- a/src/Core/PS4/HLE/Graphics/graphics_ctx.h +++ b/src/Core/PS4/HLE/Graphics/graphics_ctx.h @@ -17,7 +17,7 @@ struct VulkanCommandPool { }; struct VulkanQueueInfo { - std::mutex* mutex = nullptr; + std::unique_ptr mutex{}; u32 family = static_cast(-1); u32 index = static_cast(-1); VkQueue vk_queue = nullptr; @@ -69,4 +69,4 @@ struct VideoOutVulkanImage : public VulkanImage { VideoOutVulkanImage() : VulkanImage(VulkanImageType::VideoOut) {} }; -} // namespace HLE::Libs::Graphics \ No newline at end of file +} // namespace HLE::Libs::Graphics diff --git a/src/emulator.cpp b/src/emulator.cpp index b489b39a..44e69683 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -109,41 +109,37 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { window_ctx->is_window_hidden = false; } - window_ctx->swapchain->current_index = static_cast(-1); + window_ctx->swapchain.current_index = static_cast(-1); - auto result = vkAcquireNextImageKHR(window_ctx->m_graphic_ctx.m_device, window_ctx->swapchain->swapchain, UINT64_MAX, nullptr, - window_ctx->swapchain->present_complete_fence, &window_ctx->swapchain->current_index); + auto result = vkAcquireNextImageKHR(window_ctx->m_graphic_ctx.m_device, window_ctx->swapchain.swapchain, UINT64_MAX, nullptr, + window_ctx->swapchain.present_complete_fence, &window_ctx->swapchain.current_index); if (result != VK_SUCCESS) { fmt::print("Can't aquireNextImage\n"); std::exit(0); } - if (window_ctx->swapchain->current_index == static_cast(-1)) { + if (window_ctx->swapchain.current_index == static_cast(-1)) { fmt::print("Unsupported:swapchain current index is -1\n"); std::exit(0); } do { - result = vkWaitForFences(window_ctx->m_graphic_ctx.m_device, 1, &window_ctx->swapchain->present_complete_fence, VK_TRUE, 100000000); + result = vkWaitForFences(window_ctx->m_graphic_ctx.m_device, 1, &window_ctx->swapchain.present_complete_fence, VK_TRUE, 100000000); } while (result == VK_TIMEOUT); if (result != VK_SUCCESS) { fmt::print("vkWaitForFences is not success\n"); std::exit(0); } - vkResetFences(window_ctx->m_graphic_ctx.m_device, 1, &window_ctx->swapchain->present_complete_fence); + vkResetFences(window_ctx->m_graphic_ctx.m_device, 1, &window_ctx->swapchain.present_complete_fence); - auto* blt_src_image = image; - auto* blt_dst_image = window_ctx->swapchain; + auto blt_src_image = image; + auto blt_dst_image = window_ctx->swapchain; if (blt_src_image == nullptr) { fmt::print("blt_src_image is null\n"); std::exit(0); } - if (blt_dst_image == nullptr) { - fmt::print("blt_dst_image is null\n"); - std::exit(0); - } GPU::CommandBuffer buffer(10); @@ -151,7 +147,7 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { buffer.begin(); - Graphics::Vulkan::vulkanBlitImage(&buffer, blt_src_image, blt_dst_image); + Graphics::Vulkan::vulkanBlitImage(&buffer, blt_src_image, &blt_dst_image); VkImageMemoryBarrier pre_present_barrier{}; pre_present_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -167,7 +163,7 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { pre_present_barrier.subresourceRange.levelCount = 1; pre_present_barrier.subresourceRange.baseArrayLayer = 0; pre_present_barrier.subresourceRange.layerCount = 1; - pre_present_barrier.image = window_ctx->swapchain->swapchain_images[window_ctx->swapchain->current_index]; + pre_present_barrier.image = window_ctx->swapchain.swapchain_images[window_ctx->swapchain.current_index]; vkCmdPipelineBarrier(vk_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &pre_present_barrier); @@ -178,8 +174,8 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { present.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; present.pNext = nullptr; present.swapchainCount = 1; - present.pSwapchains = &window_ctx->swapchain->swapchain; - present.pImageIndices = &window_ctx->swapchain->current_index; + present.pSwapchains = &window_ctx->swapchain.swapchain; + present.pImageIndices = &window_ctx->swapchain.current_index; present.pWaitSemaphores = &buffer.getPool()->semaphores[buffer.getIndex()]; present.waitSemaphoreCount = 1; present.pResults = nullptr; @@ -223,4 +219,4 @@ void keyboardEvent(SDL_Event* event) { } } -} // namespace Emu \ No newline at end of file +} // namespace Emu diff --git a/src/emulator.h b/src/emulator.h index 7842faae..0e9f938d 100644 --- a/src/emulator.h +++ b/src/emulator.h @@ -49,8 +49,8 @@ struct VulkanSwapchain { VkSwapchainKHR swapchain = nullptr; VkFormat swapchain_format = VK_FORMAT_UNDEFINED; VkExtent2D swapchain_extent = {}; - VkImage* swapchain_images = nullptr; - VkImageView* swapchain_image_views = nullptr; + std::vector swapchain_images; + std::vector swapchain_image_views; u32 swapchain_images_count = 0; VkSemaphore present_complete_semaphore = nullptr; VkFence present_complete_fence = nullptr; @@ -65,8 +65,8 @@ struct WindowCtx { SDL_Window* m_window = nullptr; bool is_window_hidden = true; VkSurfaceKHR m_surface = nullptr; - VulkanSurfaceCapabilities* m_surface_capabilities = nullptr; - VulkanSwapchain* swapchain = nullptr; + VulkanSurfaceCapabilities m_surface_capabilities; + VulkanSwapchain swapchain; }; struct EmuPrivate { @@ -78,10 +78,11 @@ struct EmuPrivate { u32 m_screen_width = {0}; u32 m_screen_height = {0}; }; + void emuInit(u32 width, u32 height); void emuRun(); void checkAndWaitForGraphicsInit(); HLE::Libs::Graphics::GraphicCtx* getGraphicCtx(); void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image); void keyboardEvent(SDL_Event* event); -} // namespace Emulator \ No newline at end of file +} // namespace Emulator diff --git a/src/vulkan_util.cpp b/src/vulkan_util.cpp index 39f5305b..5821944d 100644 --- a/src/vulkan_util.cpp +++ b/src/vulkan_util.cpp @@ -53,10 +53,9 @@ void Graphics::Vulkan::vulkanCreate(Emu::WindowCtx* ctx) { std::vector device_extensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME, VK_EXT_COLOR_WRITE_ENABLE_EXTENSION_NAME, "VK_KHR_maintenance1"}; - ctx->m_surface_capabilities = new Emu::VulkanSurfaceCapabilities{}; Emu::VulkanQueues queues; - vulkanFindCompatiblePhysicalDevice(ctx->m_graphic_ctx.m_instance, ctx->m_surface, device_extensions, ctx->m_surface_capabilities, + vulkanFindCompatiblePhysicalDevice(ctx->m_graphic_ctx.m_instance, ctx->m_surface, device_extensions, &ctx->m_surface_capabilities, &ctx->m_graphic_ctx.m_physical_device, &queues); if (ctx->m_graphic_ctx.m_physical_device == nullptr) { @@ -79,18 +78,17 @@ void Graphics::Vulkan::vulkanCreate(Emu::WindowCtx* ctx) { ctx->swapchain = vulkanCreateSwapchain(&ctx->m_graphic_ctx, 2); } -Emu::VulkanSwapchain* Graphics::Vulkan::vulkanCreateSwapchain(HLE::Libs::Graphics::GraphicCtx* ctx, u32 image_count) { - auto* window_ctx = singleton::instance(); - auto* s = new Emu::VulkanSwapchain; +Emu::VulkanSwapchain Graphics::Vulkan::vulkanCreateSwapchain(HLE::Libs::Graphics::GraphicCtx* ctx, u32 image_count) { + auto window_ctx = singleton::instance(); + const auto& capabilities = window_ctx->m_surface_capabilities.capabilities; + Emu::VulkanSwapchain s{}; VkExtent2D extent{}; - extent.width = clamp(ctx->screen_width, window_ctx->m_surface_capabilities->capabilities.minImageExtent.width, - window_ctx->m_surface_capabilities->capabilities.maxImageExtent.width); - extent.height = clamp(ctx->screen_height, window_ctx->m_surface_capabilities->capabilities.minImageExtent.height, - window_ctx->m_surface_capabilities->capabilities.maxImageExtent.height); - - image_count = clamp(image_count, window_ctx->m_surface_capabilities->capabilities.minImageCount, - window_ctx->m_surface_capabilities->capabilities.maxImageCount); + extent.width = std::clamp(ctx->screen_width, capabilities.minImageExtent.width, + capabilities.maxImageExtent.width); + extent.height = std::clamp(ctx->screen_height, capabilities.minImageExtent.height, + capabilities.maxImageExtent.height); + image_count = std::clamp(image_count, capabilities.minImageCount, capabilities.maxImageCount); VkSwapchainCreateInfoKHR create_info{}; create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; @@ -99,15 +97,15 @@ Emu::VulkanSwapchain* Graphics::Vulkan::vulkanCreateSwapchain(HLE::Libs::Graphic create_info.surface = window_ctx->m_surface; create_info.minImageCount = image_count; - if (window_ctx->m_surface_capabilities->is_format_unorm_bgra32) { + if (window_ctx->m_surface_capabilities.is_format_unorm_bgra32) { create_info.imageFormat = VK_FORMAT_B8G8R8A8_UNORM; create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; - } else if (window_ctx->m_surface_capabilities->is_format_srgb_bgra32) { + } else if (window_ctx->m_surface_capabilities.is_format_srgb_bgra32) { create_info.imageFormat = VK_FORMAT_B8G8R8A8_SRGB; create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; } else { - create_info.imageFormat = window_ctx->m_surface_capabilities->formats.at(0).format; - create_info.imageColorSpace = window_ctx->m_surface_capabilities->formats.at(0).colorSpace; + create_info.imageFormat = window_ctx->m_surface_capabilities.formats.at(0).format; + create_info.imageColorSpace = window_ctx->m_surface_capabilities.formats.at(0).colorSpace; } create_info.imageExtent = extent; @@ -116,31 +114,31 @@ Emu::VulkanSwapchain* Graphics::Vulkan::vulkanCreateSwapchain(HLE::Libs::Graphic create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; create_info.queueFamilyIndexCount = 0; create_info.pQueueFamilyIndices = nullptr; - create_info.preTransform = window_ctx->m_surface_capabilities->capabilities.currentTransform; + create_info.preTransform = window_ctx->m_surface_capabilities.capabilities.currentTransform; create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR; create_info.clipped = VK_TRUE; create_info.oldSwapchain = nullptr; - s->swapchain_format = create_info.imageFormat; - s->swapchain_extent = extent; + s.swapchain_format = create_info.imageFormat; + s.swapchain_extent = extent; - vkCreateSwapchainKHR(ctx->m_device, &create_info, nullptr, &s->swapchain); + vkCreateSwapchainKHR(ctx->m_device, &create_info, nullptr, &s.swapchain); - vkGetSwapchainImagesKHR(ctx->m_device, s->swapchain, &s->swapchain_images_count, nullptr); + vkGetSwapchainImagesKHR(ctx->m_device, s.swapchain, &s.swapchain_images_count, nullptr); - s->swapchain_images = new VkImage[s->swapchain_images_count]; - vkGetSwapchainImagesKHR(ctx->m_device, s->swapchain, &s->swapchain_images_count, s->swapchain_images); + s.swapchain_images.resize(s.swapchain_images_count); + vkGetSwapchainImagesKHR(ctx->m_device, s.swapchain, &s.swapchain_images_count, s.swapchain_images.data()); - s->swapchain_image_views = new VkImageView[s->swapchain_images_count]; - for (uint32_t i = 0; i < s->swapchain_images_count; i++) { + s.swapchain_image_views.resize(s.swapchain_images_count); + for (uint32_t i = 0; i < s.swapchain_images_count; i++) { VkImageViewCreateInfo create_info{}; create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; create_info.pNext = nullptr; create_info.flags = 0; - create_info.image = (s->swapchain_images)[i]; + create_info.image = s.swapchain_images[i]; create_info.viewType = VK_IMAGE_VIEW_TYPE_2D; - create_info.format = s->swapchain_format; + create_info.format = s.swapchain_format; create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; @@ -151,28 +149,28 @@ Emu::VulkanSwapchain* Graphics::Vulkan::vulkanCreateSwapchain(HLE::Libs::Graphic create_info.subresourceRange.layerCount = 1; create_info.subresourceRange.levelCount = 1; - vkCreateImageView(ctx->m_device, &create_info, nullptr, &((s->swapchain_image_views)[i])); + vkCreateImageView(ctx->m_device, &create_info, nullptr, &s.swapchain_image_views[i]); } - if (s->swapchain == nullptr) { + if (s.swapchain == nullptr) { LOG_CRITICAL_IF(log_file_vulkanutil, "Could not create swapchain\n"); std::exit(0); } - s->current_index = static_cast(-1); + s.current_index = static_cast(-1); VkSemaphoreCreateInfo present_complete_info{}; present_complete_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; present_complete_info.pNext = nullptr; present_complete_info.flags = 0; - auto result = vkCreateSemaphore(ctx->m_device, &present_complete_info, nullptr, &s->present_complete_semaphore); + auto result = vkCreateSemaphore(ctx->m_device, &present_complete_info, nullptr, &s.present_complete_semaphore); VkFenceCreateInfo fence_info{}; fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; fence_info.pNext = nullptr; fence_info.flags = 0; - result = vkCreateFence(ctx->m_device, &fence_info, nullptr, &s->present_complete_fence); + result = vkCreateFence(ctx->m_device, &fence_info, nullptr, &s.present_complete_fence); if (result != VK_SUCCESS) { LOG_CRITICAL_IF(log_file_vulkanutil, "Can't create vulkan fence\n"); std::exit(0); @@ -187,7 +185,7 @@ void Graphics::Vulkan::vulkanCreateQueues(HLE::Libs::Graphics::GraphicCtx* ctx, ctx->queues[id].index = info.index; vkGetDeviceQueue(ctx->m_device, ctx->queues[id].family, ctx->queues[id].index, &ctx->queues[id].vk_queue); if (with_mutex) { - ctx->queues[id].mutex = new std::mutex; + ctx->queues[id].mutex = std::make_unique(); } }; diff --git a/src/vulkan_util.h b/src/vulkan_util.h index 6230e76b..bd1fdda8 100644 --- a/src/vulkan_util.h +++ b/src/vulkan_util.h @@ -19,13 +19,6 @@ constexpr int VULKAN_QUEUE_GFX = 8; constexpr int VULKAN_QUEUE_UTIL = 9; constexpr int VULKAN_QUEUE_PRESENT = 10; -template -const T& clamp(const T& x, const T& min, const T& max) { - if (x < min) return min; - if (x > max) return max; - return x; -} - void vulkanCreate(Emu::WindowCtx* ctx); void vulkanGetInstanceExtensions(Emu::VulkanExt* ext); void vulkanFindCompatiblePhysicalDevice(VkInstance instance, VkSurfaceKHR surface, const std::vector& device_extensions, @@ -36,7 +29,7 @@ VkDevice vulkanCreateDevice(VkPhysicalDevice physical_device, VkSurfaceKHR surfa Emu::VulkanQueues vulkanFindQueues(VkPhysicalDevice device, VkSurfaceKHR surface); void vulkanGetSurfaceCapabilities(VkPhysicalDevice physical_device, VkSurfaceKHR surface, Emu::VulkanSurfaceCapabilities* surfaceCap); void vulkanCreateQueues(HLE::Libs::Graphics::GraphicCtx* ctx, const Emu::VulkanQueues& queues); -Emu::VulkanSwapchain* vulkanCreateSwapchain(HLE::Libs::Graphics::GraphicCtx* ctx, u32 image_count); +Emu::VulkanSwapchain vulkanCreateSwapchain(HLE::Libs::Graphics::GraphicCtx* ctx, u32 image_count); void vulkanBlitImage(GPU::CommandBuffer* buffer, HLE::Libs::Graphics::VulkanImage* src_image, Emu::VulkanSwapchain* dst_swapchain); void vulkanFillImage(HLE::Libs::Graphics::GraphicCtx* ctx, HLE::Libs::Graphics::VulkanImage* dst_image, const void* src_data, u64 size, u32 src_pitch, u64 dst_layout); @@ -44,4 +37,4 @@ void vulkanBufferToImage(GPU::CommandBuffer* buffer, HLE::Libs::Graphics::Vulkan HLE::Libs::Graphics::VulkanImage* dst_image, u64 dst_layout); void vulkanCreateBuffer(HLE::Libs::Graphics::GraphicCtx* ctx, u64 size, HLE::Libs::Graphics::VulkanBuffer* buffer); void vulkanDeleteBuffer(HLE::Libs::Graphics::GraphicCtx* ctx, HLE::Libs::Graphics::VulkanBuffer* buffer); -}; // namespace Graphics::Vulkan \ No newline at end of file +}; // namespace Graphics::Vulkan