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/libretro_vulkan.h
Views: 1401
1
/* Copyright (C) 2010-2016 The RetroArch team
2
*
3
* ---------------------------------------------------------------------------------------------
4
* The following license statement only applies to this libretro API header (libretro_vulkan.h)
5
* ---------------------------------------------------------------------------------------------
6
*
7
* Permission is hereby granted, free of charge,
8
* to any person obtaining a copy of this software and associated documentation files (the
9
* "Software"),
10
* to deal in the Software without restriction, including without limitation the rights to
11
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12
* and to permit persons to whom the Software is furnished to do so, subject to the following
13
* conditions:
14
*
15
* The above copyright notice and this permission notice shall be included in all copies or
16
* substantial portions of the Software.
17
*
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
* LIABILITY,
23
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
*/
26
27
#ifndef LIBRETRO_VULKAN_H__
28
#define LIBRETRO_VULKAN_H__
29
30
#include "ext/vulkan/vulkan.h"
31
#include "libretro.h"
32
#include "LibretroGraphicsContext.h"
33
34
#define RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION 5
35
#define RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN_VERSION 1
36
37
struct retro_vulkan_image
38
{
39
VkImageView image_view;
40
VkImageLayout image_layout;
41
VkImageViewCreateInfo create_info;
42
};
43
44
typedef void (*retro_vulkan_set_image_t)(void* handle, const struct retro_vulkan_image* image,
45
uint32_t num_semaphores, const VkSemaphore* semaphores,
46
uint32_t src_queue_family);
47
48
typedef uint32_t (*retro_vulkan_get_sync_index_t)(void* handle);
49
typedef uint32_t (*retro_vulkan_get_sync_index_mask_t)(void* handle);
50
typedef void (*retro_vulkan_set_command_buffers_t)(void* handle, uint32_t num_cmd,
51
const VkCommandBuffer* cmd);
52
typedef void (*retro_vulkan_wait_sync_index_t)(void* handle);
53
typedef void (*retro_vulkan_lock_queue_t)(void* handle);
54
typedef void (*retro_vulkan_unlock_queue_t)(void* handle);
55
typedef void (*retro_vulkan_set_signal_semaphore_t)(void* handle, VkSemaphore semaphore);
56
57
typedef const VkApplicationInfo* (*retro_vulkan_get_application_info_t)(void);
58
59
struct retro_vulkan_context
60
{
61
VkPhysicalDevice gpu;
62
VkDevice device;
63
VkQueue queue;
64
uint32_t queue_family_index;
65
VkQueue presentation_queue;
66
uint32_t presentation_queue_family_index;
67
};
68
69
typedef bool (*retro_vulkan_create_device_t)(
70
struct retro_vulkan_context* context, VkInstance instance, VkPhysicalDevice gpu,
71
VkSurfaceKHR surface, PFN_vkGetInstanceProcAddr get_instance_proc_addr,
72
const char** required_device_extensions, unsigned num_required_device_extensions,
73
const char** required_device_layers, unsigned num_required_device_layers,
74
const VkPhysicalDeviceFeatures* required_features);
75
76
typedef void (*retro_vulkan_destroy_device_t)(void);
77
78
/* Note on thread safety:
79
* The Vulkan API is heavily designed around multi-threading, and
80
* the libretro interface for it should also be threading friendly.
81
* A core should be able to build command buffers and submit
82
* command buffers to the GPU from any thread.
83
*/
84
85
struct retro_hw_render_context_negotiation_interface_vulkan
86
{
87
/* Must be set to RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN. */
88
enum retro_hw_render_context_negotiation_interface_type interface_type;
89
/* Must be set to RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN_VERSION. */
90
unsigned interface_version;
91
92
/* If non-NULL, returns a VkApplicationInfo struct that the frontend can use instead of
93
* its "default" application info.
94
*/
95
retro_vulkan_get_application_info_t get_application_info;
96
97
/* If non-NULL, the libretro core will choose one or more physical devices,
98
* create one or more logical devices and create one or more queues.
99
* The core must prepare a designated PhysicalDevice, Device, Queue and queue family index
100
* which the frontend will use for its internal operation.
101
*
102
* If gpu is not VK_NULL_HANDLE, the physical device provided to the frontend must be this
103
* PhysicalDevice.
104
* The core is still free to use other physical devices.
105
*
106
* The frontend will request certain extensions and layers for a device which is created.
107
* The core must ensure that the queue and queue_family_index support GRAPHICS and COMPUTE.
108
*
109
* If surface is not VK_NULL_HANDLE, the core must consider presentation when creating the queues.
110
* If presentation to "surface" is supported on the queue, presentation_queue must be equal to
111
* queue.
112
* If not, a second queue must be provided in presentation_queue and presentation_queue_index.
113
* If surface is not VK_NULL_HANDLE, the instance from frontend will have been created with
114
* supported for
115
* VK_KHR_surface extension.
116
*
117
* The core is free to set its own queue priorities.
118
* Device provided to frontend is owned by the frontend, but any additional device resources must
119
* be freed by core
120
* in destroy_device callback.
121
*
122
* If this function returns true, a PhysicalDevice, Device and Queues are initialized.
123
* If false, none of the above have been initialized and the frontend will attempt
124
* to fallback to "default" device creation, as if this function was never called.
125
*/
126
retro_vulkan_create_device_t create_device;
127
128
/* If non-NULL, this callback is called similar to context_destroy for HW_RENDER_INTERFACE.
129
* However, it will be called even if context_reset was not called.
130
* This can happen if the context never succeeds in being created.
131
* destroy_device will always be called before the VkInstance
132
* of the frontend is destroyed if create_device was called successfully so that the core has a
133
* chance of
134
* tearing down its own device resources.
135
*
136
* Only auxillary resources should be freed here, i.e. resources which are not part of
137
* retro_vulkan_context.
138
*/
139
retro_vulkan_destroy_device_t destroy_device;
140
};
141
142
struct retro_hw_render_interface_vulkan
143
{
144
/* Must be set to RETRO_HW_RENDER_INTERFACE_VULKAN. */
145
enum retro_hw_render_interface_type interface_type;
146
/* Must be set to RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION. */
147
unsigned interface_version;
148
149
/* Opaque handle to the Vulkan backend in the frontend
150
* which must be passed along to all function pointers
151
* in this interface.
152
*
153
* The rationale for including a handle here (which libretro v1
154
* doesn't currently do in general) is:
155
*
156
* - Vulkan cores should be able to be freely threaded without lots of fuzz.
157
* This would break frontends which currently rely on TLS
158
* to deal with multiple cores loaded at the same time.
159
* - Fixing this in general is TODO for an eventual libretro v2.
160
*/
161
void* handle;
162
163
/* The Vulkan instance the context is using. */
164
VkInstance instance;
165
/* The physical device used. */
166
VkPhysicalDevice gpu;
167
/* The logical device used. */
168
VkDevice device;
169
170
/* Allows a core to fetch all its needed symbols without having to link
171
* against the loader itself. */
172
PFN_vkGetDeviceProcAddr get_device_proc_addr;
173
PFN_vkGetInstanceProcAddr get_instance_proc_addr;
174
175
/* The queue the core must use to submit data.
176
* This queue and index must remain constant throughout the lifetime
177
* of the context.
178
*
179
* This queue will be the queue that supports graphics and compute
180
* if the device supports compute.
181
*/
182
VkQueue queue;
183
unsigned queue_index;
184
185
/* Before calling retro_video_refresh_t with RETRO_HW_FRAME_BUFFER_VALID,
186
* set which image to use for this frame.
187
*
188
* If num_semaphores is non-zero, the frontend will wait for the
189
* semaphores provided to be signaled before using the results further
190
* in the pipeline.
191
*
192
* Semaphores provided by a single call to set_image will only be
193
* waited for once (waiting for a semaphore resets it).
194
* E.g. set_image, video_refresh, and then another
195
* video_refresh without set_image,
196
* but same image will only wait for semaphores once.
197
*
198
* For this reason, ownership transfer will only occur if semaphores
199
* are waited on for a particular frame in the frontend.
200
*
201
* Using semaphores is optional for synchronization purposes,
202
* but if not using
203
* semaphores, an image memory barrier in vkCmdPipelineBarrier
204
* should be used in the graphics_queue.
205
* Example:
206
*
207
* vkCmdPipelineBarrier(cmd,
208
* srcStageMask = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
209
* dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
210
* image_memory_barrier = {
211
* srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
212
* dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
213
* });
214
*
215
* The use of pipeline barriers instead of semaphores is encouraged
216
* as it is simpler and more fine-grained. A layout transition
217
* must generally happen anyways which requires a
218
* pipeline barrier.
219
*
220
* The image passed to set_image must have imageUsage flags set to at least
221
* VK_IMAGE_USAGE_TRANSFER_SRC_BIT and VK_IMAGE_USAGE_SAMPLED_BIT.
222
* The core will naturally want to use flags such as
223
* VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT and/or
224
* VK_IMAGE_USAGE_TRANSFER_DST_BIT depending
225
* on how the final image is created.
226
*
227
* The image must also have been created with MUTABLE_FORMAT bit set if
228
* 8-bit formats are used, so that the frontend can reinterpret sRGB
229
* formats as it sees fit.
230
*
231
* Images passed to set_image should be created with TILING_OPTIMAL.
232
* The image layout should be transitioned to either
233
* VK_IMAGE_LAYOUT_GENERIC or VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL.
234
* The actual image layout used must be set in image_layout.
235
*
236
* The image must be a 2D texture which may or not be layered
237
* and/or mipmapped.
238
*
239
* The image must be suitable for linear sampling.
240
* While the image_view is typically the only field used,
241
* the frontend may want to reinterpret the texture as sRGB vs.
242
* non-sRGB for example so the VkImageViewCreateInfo used to
243
* create the image view must also be passed in.
244
*
245
* The data in the pointer to the image struct will not be copied
246
* as the pNext field in create_info cannot be reliably deep-copied.
247
* The image pointer passed to set_image must be valid until
248
* retro_video_refresh_t has returned.
249
*
250
* If frame duping is used when passing NULL to retro_video_refresh_t,
251
* the frontend is free to either use the latest image passed to
252
* set_image or reuse the older pointer passed to set_image the
253
* frame RETRO_HW_FRAME_BUFFER_VALID was last used.
254
*
255
* Essentially, the lifetime of the pointer passed to
256
* set_image should be extended if frame duping is used
257
* so that the frontend can reuse the older pointer.
258
*
259
* The image itself however, must not be touched by the core until
260
* wait_sync_index has been completed later. The frontend may perform
261
* layout transitions on the image, so even read-only access is not defined.
262
* The exception to read-only rule is if GENERAL layout is used for the image.
263
* In this case, the frontend is not allowed to perform any layout transitions,
264
* so concurrent reads from core and frontend are allowed.
265
*
266
* If frame duping is used, or if set_command_buffers is used,
267
* the frontend will not wait for any semaphores.
268
*
269
* The src_queue_family is used to specify which queue family
270
* the image is currently owned by. If using multiple queue families
271
* (e.g. async compute), the frontend will need to acquire ownership of the
272
* image before rendering with it and release the image afterwards.
273
*
274
* If src_queue_family is equal to the queue family (queue_index),
275
* no ownership transfer will occur.
276
* Similarly, if src_queue_family is VK_QUEUE_FAMILY_IGNORED,
277
* no ownership transfer will occur.
278
*
279
* The frontend will always release ownership back to src_queue_family.
280
* Waiting for frontend to complete with wait_sync_index() ensures that
281
* the frontend has released ownership back to the application.
282
* Note that in Vulkan, transfering ownership is a two-part process.
283
*
284
* Example frame:
285
* - core releases ownership from src_queue_index to queue_index with VkImageMemoryBarrier.
286
* - core calls set_image with src_queue_index.
287
* - Frontend will acquire the image with src_queue_index -> queue_index as well, completing the
288
* ownership transfer.
289
* - Frontend renders the frame.
290
* - Frontend releases ownership with queue_index -> src_queue_index.
291
* - Next time image is used, core must acquire ownership from queue_index ...
292
*
293
* Since the frontend releases ownership, we cannot necessarily dupe the frame because
294
* the core needs to make the roundtrip of ownership transfer.
295
*/
296
retro_vulkan_set_image_t set_image;
297
298
/* Get the current sync index for this frame which is obtained in
299
* frontend by calling e.g. vkAcquireNextImageKHR before calling
300
* retro_run().
301
*
302
* This index will correspond to which swapchain buffer is currently
303
* the active one.
304
*
305
* Knowing this index is very useful for maintaining safe asynchronous CPU
306
* and GPU operation without stalling.
307
*
308
* The common pattern for synchronization is to receive fences when
309
* submitting command buffers to Vulkan (vkQueueSubmit) and add this fence
310
* to a list of fences for frame number get_sync_index().
311
*
312
* Next time we receive the same get_sync_index(), we can wait for the
313
* fences from before, which will usually return immediately as the
314
* frontend will generally also avoid letting the GPU run ahead too much.
315
*
316
* After the fence has signaled, we know that the GPU has completed all
317
* GPU work related to work submitted in the frame we last saw get_sync_index().
318
*
319
* This means we can safely reuse or free resources allocated in this frame.
320
*
321
* In theory, even if we wait for the fences correctly, it is not technically
322
* safe to write to the image we earlier passed to the frontend since we're
323
* not waiting for the frontend GPU jobs to complete.
324
*
325
* The frontend will guarantee that the appropriate pipeline barrier
326
* in graphics_queue has been used such that
327
* VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT cannot
328
* start until the frontend is done with the image.
329
*/
330
retro_vulkan_get_sync_index_t get_sync_index;
331
332
/* Returns a bitmask of how many swapchain images we currently have
333
* in the frontend.
334
*
335
* If bit #N is set in the return value, get_sync_index can return N.
336
* Knowing this value is useful for preallocating per-frame management
337
* structures ahead of time.
338
*
339
* While this value will typically remain constant throughout the
340
* applications lifecycle, it may for example change if the frontend
341
* suddently changes fullscreen state and/or latency.
342
*
343
* If this value ever changes, it is safe to assume that the device
344
* is completely idle and all synchronization objects can be deleted
345
* right away as desired.
346
*/
347
retro_vulkan_get_sync_index_mask_t get_sync_index_mask;
348
349
/* Instead of submitting the command buffer to the queue first, the core
350
* can pass along its command buffer to the frontend, and the frontend
351
* will submit the command buffer together with the frontends command buffers.
352
*
353
* This has the advantage that the overhead of vkQueueSubmit can be
354
* amortized into a single call. For this mode, semaphores in set_image
355
* will be ignored, so vkCmdPipelineBarrier must be used to synchronize
356
* the core and frontend.
357
*
358
* The command buffers in set_command_buffers are only executed once,
359
* even if frame duping is used.
360
*
361
* If frame duping is used, set_image should be used for the frames
362
* which should be duped instead.
363
*
364
* Command buffers passed to the frontend with set_command_buffers
365
* must not actually be submitted to the GPU until retro_video_refresh_t
366
* is called.
367
*
368
* The frontend must submit the command buffer before submitting any
369
* other command buffers provided by set_command_buffers. */
370
retro_vulkan_set_command_buffers_t set_command_buffers;
371
372
/* Waits on CPU for device activity for the current sync index to complete.
373
* This is useful since the core will not have a relevant fence to sync with
374
* when the frontend is submitting the command buffers. */
375
retro_vulkan_wait_sync_index_t wait_sync_index;
376
377
/* If the core submits command buffers itself to any of the queues provided
378
* in this interface, the core must lock and unlock the frontend from
379
* racing on the VkQueue.
380
*
381
* Queue submission can happen on any thread.
382
* Even if queue submission happens on the same thread as retro_run(),
383
* the lock/unlock functions must still be called.
384
*
385
* NOTE: Queue submissions are heavy-weight. */
386
retro_vulkan_lock_queue_t lock_queue;
387
retro_vulkan_unlock_queue_t unlock_queue;
388
389
/* Sets a semaphore which is signaled when the image in set_image can safely be reused.
390
* The semaphore is consumed next call to retro_video_refresh_t.
391
* The semaphore will be signalled even for duped frames.
392
* The semaphore will be signalled only once, so set_signal_semaphore should be called every
393
* frame.
394
* The semaphore may be VK_NULL_HANDLE, which disables semaphore signalling for next call to
395
* retro_video_refresh_t.
396
*
397
* This is mostly useful to support use cases where you're rendering to a single image that
398
* is recycled in a ping-pong fashion with the frontend to save memory (but potentially less
399
* throughput).
400
*/
401
retro_vulkan_set_signal_semaphore_t set_signal_semaphore;
402
};
403
404
#endif
405
406