Skip to content

Commit 6c84cfd

Browse files
committed
Move the iOS vulkan context to its own file
1 parent da782cb commit 6c84cfd

File tree

4 files changed

+164
-151
lines changed

4 files changed

+164
-151
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,10 @@ elseif(IOS AND NOT LIBRETRO)
13561356
ios/ViewControllerMetal.h
13571357
ios/iOSCoreAudio.mm
13581358
ios/iOSCoreAudio.h
1359+
ios/iOSVulkanContext.h
1360+
ios/iOSVulkanContext.mm
1361+
#ios/iOSOpenGLContext.h
1362+
#ios/iOSOpenGLContext.cpp
13591363
ios/IAPManager.mm
13601364
ios/IAPManager.h
13611365
ios/CameraHelper.mm

ios/ViewControllerMetal.mm

Lines changed: 2 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@
33
#import "DisplayManager.h"
44
#import "iOSCoreAudio.h"
55

6-
#include "Common/Log.h"
6+
#include "ios/iOSVulkanContext.h"
77

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"
149
#include "Common/System/Display.h"
1510
#include "Common/System/System.h"
1611
#include "Common/System/OSD.h"
1712
#include "Common/System/NativeApp.h"
1813
#include "Common/System/Request.h"
19-
#include "Common/GraphicsContext.h"
2014
#include "Common/Thread/ThreadUtil.h"
2115
#include "Common/TimeUtil.h"
2216

@@ -26,149 +20,6 @@
2620

2721
#include "GPU/Vulkan/VulkanUtil.h"
2822

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-
17223
#pragma mark -
17324
#pragma mark PPSSPPViewControllerMetal
17425

ios/iOSVulkanContext.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include "Common/GPU/Vulkan/VulkanLoader.h"
4+
#include "Common/GPU/Vulkan/VulkanContext.h"
5+
#include "Common/GPU/Vulkan/VulkanRenderManager.h"
6+
#include "Common/GraphicsContext.h"
7+
#include "Common/GPU/thin3d.h"
8+
#include "Common/GPU/thin3d_create.h"
9+
10+
enum class GraphicsContextState {
11+
PENDING,
12+
INITIALIZED,
13+
FAILED_INIT,
14+
SHUTDOWN,
15+
};
16+
17+
class IOSVulkanContext : public GraphicsContext {
18+
public:
19+
IOSVulkanContext() {}
20+
~IOSVulkanContext() {
21+
delete g_Vulkan;
22+
g_Vulkan = nullptr;
23+
}
24+
25+
bool InitAPI();
26+
27+
bool InitFromRenderThread(CAMetalLayer *layer, int desiredBackbufferSizeX, int desiredBackbufferSizeY);
28+
void ShutdownFromRenderThread() override; // Inverses InitFromRenderThread.
29+
30+
void Shutdown() override;
31+
void Resize() override {}
32+
33+
void *GetAPIContext() override { return g_Vulkan; }
34+
Draw::DrawContext *GetDrawContext() override { return draw_; }
35+
36+
private:
37+
VulkanContext *g_Vulkan = nullptr;
38+
Draw::DrawContext *draw_ = nullptr;
39+
GraphicsContextState state_ = GraphicsContextState::PENDING;
40+
};

ios/iOSVulkanContext.mm

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "ppsspp_config.h"
2+
#import "iOSVulkanContext.h"
3+
#include "GPU/Vulkan/VulkanUtil.h"
4+
#include "Common/Data/Text/Parsers.h"
5+
#include "Core/Config.h"
6+
#include "Core/ConfigValues.h"
7+
#include "Core/System.h"
8+
9+
// ViewController lifecycle:
10+
// https://www.progressconcepts.com/blog/ios-appdelegate-viewcontroller-method-order/
11+
12+
bool IOSVulkanContext::InitFromRenderThread(CAMetalLayer *layer, int desiredBackbufferSizeX, int desiredBackbufferSizeY) {
13+
INFO_LOG(Log::G3D, "IOSVulkanContext::InitFromRenderThread: desiredwidth=%d desiredheight=%d", desiredBackbufferSizeX, desiredBackbufferSizeY);
14+
if (!g_Vulkan) {
15+
ERROR_LOG(Log::G3D, "IOSVulkanContext::InitFromRenderThread: No Vulkan context");
16+
return false;
17+
}
18+
19+
VkResult res = g_Vulkan->InitSurface(WINDOWSYSTEM_METAL_EXT, (__bridge void *)layer, nullptr);
20+
if (res != VK_SUCCESS) {
21+
ERROR_LOG(Log::G3D, "g_Vulkan->InitSurface failed: '%s'", VulkanResultToString(res));
22+
return false;
23+
}
24+
25+
bool useMultiThreading = g_Config.bRenderMultiThreading;
26+
if (g_Config.iInflightFrames == 1) {
27+
useMultiThreading = false;
28+
}
29+
30+
draw_ = Draw::T3DCreateVulkanContext(g_Vulkan, useMultiThreading);
31+
32+
VkPresentModeKHR presentMode = ConfigPresentModeToVulkan(draw_);
33+
34+
// This MUST run on the main thread. We're taking our chances with a dispatch_sync here.
35+
g_Vulkan->InitSwapchain(presentMode);
36+
37+
if (false) {
38+
delete draw_;
39+
ERROR_LOG(Log::G3D, "InitSwapchain failed");
40+
g_Vulkan->DestroySwapchain();
41+
g_Vulkan->DestroySurface();
42+
g_Vulkan->DestroyDevice();
43+
g_Vulkan->DestroyInstance();
44+
return false;
45+
}
46+
47+
SetGPUBackend(GPUBackend::VULKAN);
48+
bool shaderSuccess = draw_->CreatePresets(); // Doesn't fail, we ship the compiler.
49+
_assert_msg_(shaderSuccess, "Failed to compile preset shaders");
50+
draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
51+
52+
VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
53+
renderManager->SetInflightFrames(g_Config.iInflightFrames);
54+
return true;
55+
}
56+
57+
void IOSVulkanContext::ShutdownFromRenderThread() {
58+
INFO_LOG(Log::G3D, "IOSVulkanContext::Shutdown");
59+
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
60+
delete draw_;
61+
draw_ = nullptr;
62+
g_Vulkan->WaitUntilQueueIdle();
63+
g_Vulkan->PerformPendingDeletes();
64+
g_Vulkan->DestroySwapchain();
65+
g_Vulkan->DestroySurface();
66+
INFO_LOG(Log::G3D, "Done with ShutdownFromRenderThread");
67+
}
68+
69+
void IOSVulkanContext::Shutdown() {
70+
INFO_LOG(Log::G3D, "Calling NativeShutdownGraphics");
71+
g_Vulkan->DestroyDevice();
72+
g_Vulkan->DestroyInstance();
73+
// We keep the g_Vulkan context around to avoid invalidating a ton of pointers around the app.
74+
finalize_glslang();
75+
INFO_LOG(Log::G3D, "IOSVulkanContext::Shutdown completed");
76+
}
77+
78+
bool IOSVulkanContext::InitAPI() {
79+
INFO_LOG(Log::G3D, "IOSVulkanContext::Init");
80+
init_glslang();
81+
82+
g_LogOptions.breakOnError = true;
83+
g_LogOptions.breakOnWarning = true;
84+
g_LogOptions.msgBoxOnError = false;
85+
86+
INFO_LOG(Log::G3D, "Creating Vulkan context");
87+
Version gitVer(PPSSPP_GIT_VERSION);
88+
89+
std::string errorStr;
90+
if (!VulkanLoad(&errorStr)) {
91+
ERROR_LOG(Log::G3D, "Failed to load Vulkan driver library: %s", errorStr.c_str());
92+
state_ = GraphicsContextState::FAILED_INIT;
93+
return false;
94+
}
95+
96+
if (!g_Vulkan) {
97+
// TODO: Assert if g_Vulkan already exists here?
98+
g_Vulkan = new VulkanContext();
99+
}
100+
101+
VulkanContext::CreateInfo info{};
102+
InitVulkanCreateInfoFromConfig(&info);
103+
if (!g_Vulkan->CreateInstanceAndDevice(info)) {
104+
delete g_Vulkan;
105+
g_Vulkan = nullptr;
106+
state_ = GraphicsContextState::FAILED_INIT;
107+
return false;
108+
}
109+
110+
g_Vulkan->SetCbGetDrawSize([]() {
111+
return VkExtent2D {(uint32_t)g_display.pixel_xres, (uint32_t)g_display.pixel_yres};
112+
});
113+
114+
INFO_LOG(Log::G3D, "Vulkan device created!");
115+
state_ = GraphicsContextState::INITIALIZED;
116+
return true;
117+
}
118+

0 commit comments

Comments
 (0)