CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/libretro/LibretroVulkanContext.cpp
Views: 1401
1
2
#include "Common/GPU/Vulkan/VulkanLoader.h"
3
#include "Common/GPU/Vulkan/VulkanContext.h"
4
#include "Common/GPU/Vulkan/VulkanDebug.h"
5
#include "Common/Log.h"
6
#include "Core/Config.h"
7
#include "Core/ConfigValues.h"
8
#include "Core/System.h"
9
#include "GPU/GPUInterface.h"
10
#include "Common/Data/Text/Parsers.h"
11
12
#include "libretro/LibretroVulkanContext.h"
13
#include "libretro/libretro_vulkan.h"
14
#include <GPU/Vulkan/VulkanRenderManager.h>
15
16
static VulkanContext *vk;
17
18
void vk_libretro_init(VkInstance instance, VkPhysicalDevice gpu, VkSurfaceKHR surface, PFN_vkGetInstanceProcAddr get_instance_proc_addr, const char **required_device_extensions, unsigned num_required_device_extensions, const char **required_device_layers, unsigned num_required_device_layers, const VkPhysicalDeviceFeatures *required_features);
19
void vk_libretro_shutdown();
20
void vk_libretro_set_hwrender_interface(retro_hw_render_interface *hw_render_interface);
21
void vk_libretro_wait_for_presentation();
22
23
LibretroVulkanContext::LibretroVulkanContext()
24
: LibretroHWRenderContext(RETRO_HW_CONTEXT_VULKAN, VK_MAKE_VERSION(1, 0, 18)) {}
25
26
void LibretroVulkanContext::SwapBuffers() {
27
vk_libretro_wait_for_presentation();
28
LibretroHWRenderContext::SwapBuffers();
29
}
30
31
static bool create_device(retro_vulkan_context *context, VkInstance instance, VkPhysicalDevice gpu, VkSurfaceKHR surface, PFN_vkGetInstanceProcAddr get_instance_proc_addr, const char **required_device_extensions, unsigned num_required_device_extensions, const char **required_device_layers, unsigned num_required_device_layers, const VkPhysicalDeviceFeatures *required_features) {
32
init_glslang();
33
34
vk = new VulkanContext();
35
36
vk_libretro_init(instance, gpu, surface, get_instance_proc_addr, required_device_extensions, num_required_device_extensions, required_device_layers, num_required_device_layers, required_features);
37
38
// TODO: Here we'll inject the instance and all of the stuff into the VulkanContext.
39
40
vk->CreateInstance({});
41
42
int physical_device = 0;
43
while (gpu && vk->GetPhysicalDevice(physical_device) != gpu) {
44
physical_device++;
45
}
46
47
if (!gpu) {
48
physical_device = vk->GetBestPhysicalDevice();
49
}
50
51
vk->CreateDevice(physical_device);
52
#ifdef _WIN32
53
vk->InitSurface(WINDOWSYSTEM_WIN32, nullptr, nullptr);
54
#elif defined(__ANDROID__)
55
vk->InitSurface(WINDOWSYSTEM_ANDROID, nullptr, nullptr);
56
#elif defined(VK_USE_PLATFORM_METAL_EXT)
57
vk->InitSurface(WINDOWSYSTEM_METAL_EXT, nullptr, nullptr);
58
#elif defined(VK_USE_PLATFORM_XLIB_KHR)
59
vk->InitSurface(WINDOWSYSTEM_XLIB, nullptr, nullptr);
60
#elif defined(VK_USE_PLATFORM_XCB_KHR)
61
vk->InitSurface(WINDOWSYSTEM_XCB, nullptr, nullptr);
62
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
63
vk->InitSurface(WINDOWSYSTEM_WAYLAND, nullptr, nullptr);
64
#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
65
vk->InitSurface(WINDOWSYSTEM_DISPLAY, nullptr, nullptr);
66
#endif
67
68
context->gpu = vk->GetPhysicalDevice(physical_device);
69
context->device = vk->GetDevice();
70
context->queue = vk->GetGraphicsQueue();
71
context->queue_family_index = vk->GetGraphicsQueueFamilyIndex();
72
context->presentation_queue = context->queue;
73
context->presentation_queue_family_index = context->queue_family_index;
74
#ifdef _DEBUG
75
fflush(stdout);
76
#endif
77
return true;
78
}
79
80
static const VkApplicationInfo *GetApplicationInfo(void) {
81
static VkApplicationInfo app_info{ VK_STRUCTURE_TYPE_APPLICATION_INFO };
82
app_info.pApplicationName = "PPSSPP";
83
app_info.applicationVersion = Version(PPSSPP_GIT_VERSION).ToInteger();
84
app_info.pEngineName = "PPSSPP";
85
app_info.engineVersion = 2;
86
app_info.apiVersion = VK_API_VERSION_1_0;
87
return &app_info;
88
}
89
90
bool LibretroVulkanContext::Init() {
91
if (!LibretroHWRenderContext::Init(true)) {
92
return false;
93
}
94
95
static const struct retro_hw_render_context_negotiation_interface_vulkan iface = {
96
RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN,
97
RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN_VERSION,
98
GetApplicationInfo,
99
create_device, // Callback above.
100
nullptr,
101
};
102
Libretro::environ_cb(RETRO_ENVIRONMENT_SET_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE, (void *)&iface);
103
104
g_Config.iGPUBackend = (int)GPUBackend::VULKAN;
105
return true;
106
}
107
108
void LibretroVulkanContext::ContextReset() {
109
retro_hw_render_interface *vulkan;
110
if (!Libretro::environ_cb(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, (void **)&vulkan) || !vulkan) {
111
ERROR_LOG(Log::G3D, "Failed to get HW rendering interface!\n");
112
return;
113
}
114
if (vulkan->interface_version != RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION) {
115
ERROR_LOG(Log::G3D, "HW render interface mismatch, expected %u, got %u!\n", RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION, vulkan->interface_version);
116
return;
117
}
118
vk_libretro_set_hwrender_interface(vulkan);
119
120
LibretroHWRenderContext::ContextReset();
121
}
122
123
void LibretroVulkanContext::ContextDestroy() {
124
INFO_LOG(Log::G3D, "LibretroVulkanContext::ContextDestroy()");
125
vk->WaitUntilQueueIdle();
126
LibretroHWRenderContext::ContextDestroy();
127
}
128
129
void LibretroVulkanContext::CreateDrawContext() {
130
vk->ReinitSurface();
131
132
if (!vk->InitSwapchain()) {
133
return;
134
}
135
136
bool useMultiThreading = g_Config.bRenderMultiThreading;
137
if (g_Config.iInflightFrames == 1) {
138
useMultiThreading = false;
139
}
140
draw_ = Draw::T3DCreateVulkanContext(vk, useMultiThreading);
141
((VulkanRenderManager*)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER))->SetInflightFrames(g_Config.iInflightFrames);
142
SetGPUBackend(GPUBackend::VULKAN);
143
}
144
145
void LibretroVulkanContext::Shutdown() {
146
if (!vk) {
147
return;
148
}
149
150
if (draw_)
151
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, vk->GetBackbufferWidth(), vk->GetBackbufferHeight());
152
153
LibretroHWRenderContext::Shutdown();
154
155
vk->WaitUntilQueueIdle();
156
157
vk->DestroySwapchain();
158
vk->DestroySurface();
159
vk->DestroyDevice();
160
vk->DestroyInstance();
161
162
delete vk;
163
vk = nullptr;
164
165
finalize_glslang();
166
vk_libretro_shutdown();
167
}
168
169
void *LibretroVulkanContext::GetAPIContext() { return vk; }
170
171