|
3 | 3 | #import "DisplayManager.h" |
4 | 4 | #import "iOSCoreAudio.h" |
5 | 5 |
|
6 | | -#include "Common/Log.h" |
| 6 | +#include "ios/iOSVulkanContext.h" |
7 | 7 |
|
8 | | -#include "Common/GPU/Vulkan/VulkanLoader.h" |
9 | | -#include "Common/GPU/Vulkan/VulkanContext.h" |
10 | | -#include "Common/GPU/Vulkan/VulkanRenderManager.h" |
11 | | -#include "Common/GPU/thin3d.h" |
12 | | -#include "Common/GPU/thin3d_create.h" |
13 | | -#include "Common/Data/Text/Parsers.h" |
| 8 | +#include "Common/Log.h" |
14 | 9 | #include "Common/System/Display.h" |
15 | 10 | #include "Common/System/System.h" |
16 | 11 | #include "Common/System/OSD.h" |
17 | 12 | #include "Common/System/NativeApp.h" |
18 | 13 | #include "Common/System/Request.h" |
19 | | -#include "Common/GraphicsContext.h" |
20 | 14 | #include "Common/Thread/ThreadUtil.h" |
21 | 15 | #include "Common/TimeUtil.h" |
22 | 16 |
|
|
26 | 20 |
|
27 | 21 | #include "GPU/Vulkan/VulkanUtil.h" |
28 | 22 |
|
29 | | -// ViewController lifecycle: |
30 | | -// https://www.progressconcepts.com/blog/ios-appdelegate-viewcontroller-method-order/ |
31 | | - |
32 | | -enum class GraphicsContextState { |
33 | | - PENDING, |
34 | | - INITIALIZED, |
35 | | - FAILED_INIT, |
36 | | - SHUTDOWN, |
37 | | -}; |
38 | | - |
39 | | -class IOSVulkanContext : public GraphicsContext { |
40 | | -public: |
41 | | - IOSVulkanContext() {} |
42 | | - ~IOSVulkanContext() { |
43 | | - delete g_Vulkan; |
44 | | - g_Vulkan = nullptr; |
45 | | - } |
46 | | - |
47 | | - bool InitAPI(); |
48 | | - |
49 | | - bool InitFromRenderThread(CAMetalLayer *layer, int desiredBackbufferSizeX, int desiredBackbufferSizeY); |
50 | | - void ShutdownFromRenderThread() override; // Inverses InitFromRenderThread. |
51 | | - |
52 | | - void Shutdown() override; |
53 | | - void Resize() override {} |
54 | | - |
55 | | - void *GetAPIContext() override { return g_Vulkan; } |
56 | | - Draw::DrawContext *GetDrawContext() override { return draw_; } |
57 | | - |
58 | | -private: |
59 | | - VulkanContext *g_Vulkan = nullptr; |
60 | | - Draw::DrawContext *draw_ = nullptr; |
61 | | - GraphicsContextState state_ = GraphicsContextState::PENDING; |
62 | | -}; |
63 | | - |
64 | | -bool IOSVulkanContext::InitFromRenderThread(CAMetalLayer *layer, int desiredBackbufferSizeX, int desiredBackbufferSizeY) { |
65 | | - INFO_LOG(Log::G3D, "IOSVulkanContext::InitFromRenderThread: desiredwidth=%d desiredheight=%d", desiredBackbufferSizeX, desiredBackbufferSizeY); |
66 | | - if (!g_Vulkan) { |
67 | | - ERROR_LOG(Log::G3D, "IOSVulkanContext::InitFromRenderThread: No Vulkan context"); |
68 | | - return false; |
69 | | - } |
70 | | - |
71 | | - VkResult res = g_Vulkan->InitSurface(WINDOWSYSTEM_METAL_EXT, (__bridge void *)layer, nullptr); |
72 | | - if (res != VK_SUCCESS) { |
73 | | - ERROR_LOG(Log::G3D, "g_Vulkan->InitSurface failed: '%s'", VulkanResultToString(res)); |
74 | | - return false; |
75 | | - } |
76 | | - |
77 | | - bool useMultiThreading = g_Config.bRenderMultiThreading; |
78 | | - if (g_Config.iInflightFrames == 1) { |
79 | | - useMultiThreading = false; |
80 | | - } |
81 | | - |
82 | | - draw_ = Draw::T3DCreateVulkanContext(g_Vulkan, useMultiThreading); |
83 | | - |
84 | | - VkPresentModeKHR presentMode = ConfigPresentModeToVulkan(draw_); |
85 | | - |
86 | | - // This MUST run on the main thread. We're taking our chances with a dispatch_sync here. |
87 | | - g_Vulkan->InitSwapchain(presentMode); |
88 | | - |
89 | | - if (false) { |
90 | | - delete draw_; |
91 | | - ERROR_LOG(Log::G3D, "InitSwapchain failed"); |
92 | | - g_Vulkan->DestroySwapchain(); |
93 | | - g_Vulkan->DestroySurface(); |
94 | | - g_Vulkan->DestroyDevice(); |
95 | | - g_Vulkan->DestroyInstance(); |
96 | | - return false; |
97 | | - } |
98 | | - |
99 | | - SetGPUBackend(GPUBackend::VULKAN); |
100 | | - bool shaderSuccess = draw_->CreatePresets(); // Doesn't fail, we ship the compiler. |
101 | | - _assert_msg_(shaderSuccess, "Failed to compile preset shaders"); |
102 | | - draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight()); |
103 | | - |
104 | | - VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER); |
105 | | - renderManager->SetInflightFrames(g_Config.iInflightFrames); |
106 | | - return true; |
107 | | -} |
108 | | - |
109 | | -void IOSVulkanContext::ShutdownFromRenderThread() { |
110 | | - INFO_LOG(Log::G3D, "IOSVulkanContext::Shutdown"); |
111 | | - draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight()); |
112 | | - delete draw_; |
113 | | - draw_ = nullptr; |
114 | | - g_Vulkan->WaitUntilQueueIdle(); |
115 | | - g_Vulkan->PerformPendingDeletes(); |
116 | | - g_Vulkan->DestroySwapchain(); |
117 | | - g_Vulkan->DestroySurface(); |
118 | | - INFO_LOG(Log::G3D, "Done with ShutdownFromRenderThread"); |
119 | | -} |
120 | | - |
121 | | -void IOSVulkanContext::Shutdown() { |
122 | | - INFO_LOG(Log::G3D, "Calling NativeShutdownGraphics"); |
123 | | - g_Vulkan->DestroyDevice(); |
124 | | - g_Vulkan->DestroyInstance(); |
125 | | - // We keep the g_Vulkan context around to avoid invalidating a ton of pointers around the app. |
126 | | - finalize_glslang(); |
127 | | - INFO_LOG(Log::G3D, "IOSVulkanContext::Shutdown completed"); |
128 | | -} |
129 | | - |
130 | | -bool IOSVulkanContext::InitAPI() { |
131 | | - INFO_LOG(Log::G3D, "IOSVulkanContext::Init"); |
132 | | - init_glslang(); |
133 | | - |
134 | | - g_LogOptions.breakOnError = true; |
135 | | - g_LogOptions.breakOnWarning = true; |
136 | | - g_LogOptions.msgBoxOnError = false; |
137 | | - |
138 | | - INFO_LOG(Log::G3D, "Creating Vulkan context"); |
139 | | - Version gitVer(PPSSPP_GIT_VERSION); |
140 | | - |
141 | | - std::string errorStr; |
142 | | - if (!VulkanLoad(&errorStr)) { |
143 | | - ERROR_LOG(Log::G3D, "Failed to load Vulkan driver library: %s", errorStr.c_str()); |
144 | | - state_ = GraphicsContextState::FAILED_INIT; |
145 | | - return false; |
146 | | - } |
147 | | - |
148 | | - if (!g_Vulkan) { |
149 | | - // TODO: Assert if g_Vulkan already exists here? |
150 | | - g_Vulkan = new VulkanContext(); |
151 | | - } |
152 | | - |
153 | | - VulkanContext::CreateInfo info{}; |
154 | | - InitVulkanCreateInfoFromConfig(&info); |
155 | | - if (!g_Vulkan->CreateInstanceAndDevice(info)) { |
156 | | - delete g_Vulkan; |
157 | | - g_Vulkan = nullptr; |
158 | | - state_ = GraphicsContextState::FAILED_INIT; |
159 | | - return false; |
160 | | - } |
161 | | - |
162 | | - g_Vulkan->SetCbGetDrawSize([]() { |
163 | | - return VkExtent2D {(uint32_t)g_display.pixel_xres, (uint32_t)g_display.pixel_yres}; |
164 | | - }); |
165 | | - |
166 | | - INFO_LOG(Log::G3D, "Vulkan device created!"); |
167 | | - state_ = GraphicsContextState::INITIALIZED; |
168 | | - return true; |
169 | | -} |
170 | | - |
171 | | - |
172 | 23 | #pragma mark - |
173 | 24 | #pragma mark PPSSPPViewControllerMetal |
174 | 25 |
|
|
0 commit comments