mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
Mali bridge (#517, by ASEbrahim) on the non-Adreno test branch
Squash of Droid-Deck/DroidDeck#517 (ASEbrahim/DroidDeck mali-bridge @ 6b823341): the Vulkan
bridge (tools/vkbridge) that runs the guest's Vulkan on the phone's own driver, with BC1-BC7,
clip/cull distance, vertex-divisor and depth-clip emulation and dma-heap memory sharing for Mali.
All of the bridge is ASEbrahim's work; squashed only to carry it on this test branch.
Combined with #570 on this branch, a device draws through exactly one path:
- Adreno: Turnip, unchanged (checked first; the bridge setting is ignored there).
- PowerVR / Xclipse with the system HAL and a render node: the system-Vulkan path (#570).
- Anything else with the Setup toggle on (Mali): the bridge.
This commit is contained in:
1 parent
cf06f89c11
commit
ee0fb08268
125 files changed
+119982
-12
No files matched your search
@@ -0,0 +1,234 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Command buffers: vkCmd* calls are encoded into the command buffer's own stream (no round trip
|
||||
* each); vkEndCommandBuffer sends the stream in one message and the server replays it.
|
||||
*
|
||||
* Stream entry: u32 command id, u32 payload bytes, payload (8-aligned), padded to 8.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_client.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static __thread vkb_cmdbuf *tls_encoding;
|
||||
|
||||
int vkb_cb_begin_is_secondary(const VkCommandBufferBeginInfo *s)
|
||||
{
|
||||
(void)s;
|
||||
return tls_encoding && tls_encoding->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY;
|
||||
}
|
||||
|
||||
vkb_enc *vkb_cmd_record_begin(VkCommandBuffer commandBuffer, uint32_t cmd)
|
||||
{
|
||||
vkb_cmdbuf *cb = vkb_cb(commandBuffer);
|
||||
if (!cb->recording) {
|
||||
VKB_ONCE(VKB_LOG_WARN, "%s on a command buffer that is not recording (ignored; logged once)", vkb_cmd_names[cmd]);
|
||||
return NULL;
|
||||
}
|
||||
vkb_enc *e = &cb->stream;
|
||||
/* entries start 8-aligned */
|
||||
while (e->len & 7) vkb_enc_u8(e, 0);
|
||||
vkb_enc_u32(e, cmd);
|
||||
vkb_enc_u32(e, 0);
|
||||
cb->entry = e->len;
|
||||
return e;
|
||||
}
|
||||
|
||||
void vkb_cmd_record_end(VkCommandBuffer commandBuffer)
|
||||
{
|
||||
vkb_cmdbuf *cb = vkb_cb(commandBuffer);
|
||||
vkb_enc *e = &cb->stream;
|
||||
if (e->oom) {
|
||||
cb->broken = 1;
|
||||
return;
|
||||
}
|
||||
if (e->nfds) {
|
||||
VKB_ERR("internal: a recorded command carries a file descriptor");
|
||||
cb->broken = 1;
|
||||
e->nfds = 0;
|
||||
}
|
||||
uint32_t n = (uint32_t)(e->len - cb->entry);
|
||||
memcpy(e->buf + cb->entry - 4, &n, 4);
|
||||
}
|
||||
|
||||
static void stream_reset(vkb_cmdbuf *cb)
|
||||
{
|
||||
if (cb->stream.cap > (8u << 20)) {
|
||||
vkb_enc_free(&cb->stream);
|
||||
vkb_enc_init(&cb->stream);
|
||||
} else {
|
||||
vkb_enc_reset(&cb->stream);
|
||||
}
|
||||
cb->recording = 0;
|
||||
cb->broken = 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ pools */
|
||||
|
||||
static vkb_cmdpool *pool_get(vkb_device *dev, VkCommandPool pool, int create)
|
||||
{
|
||||
vkb_cmdpool *p = vkb_map_get(&dev->pools, (uint64_t)pool);
|
||||
if (!p && create) {
|
||||
p = calloc(1, sizeof(*p));
|
||||
vkb_map_put(&dev->pools, (uint64_t)pool, p);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void cmdbuf_free(vkb_cmdbuf *cb)
|
||||
{
|
||||
vkb_enc_free(&cb->stream);
|
||||
free(cb);
|
||||
}
|
||||
|
||||
void vkb_cmdpool_free(vkb_cmdpool *p)
|
||||
{
|
||||
vkb_cmdbuf *cb = p->head;
|
||||
while (cb) {
|
||||
vkb_cmdbuf *n = cb->pool_next;
|
||||
cmdbuf_free(cb);
|
||||
cb = n;
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo,
|
||||
VkCommandBuffer *pCommandBuffers)
|
||||
{
|
||||
VkResult r = vkb_wire_vkAllocateCommandBuffers(device, pAllocateInfo, pCommandBuffers);
|
||||
if (r != VK_SUCCESS) {
|
||||
for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) pCommandBuffers[i] = VK_NULL_HANDLE;
|
||||
return r;
|
||||
}
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
vkb_cmdpool *p = pool_get(dev, pAllocateInfo->commandPool, 1);
|
||||
for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) {
|
||||
vkb_cmdbuf *cb = vkb_cb(pCommandBuffers[i]);
|
||||
if (!cb) continue;
|
||||
cb->pool = pAllocateInfo->commandPool;
|
||||
cb->level = pAllocateInfo->level;
|
||||
cb->pool_next = p->head;
|
||||
cb->pool_prev = NULL;
|
||||
if (p->head) p->head->pool_prev = cb;
|
||||
p->head = cb;
|
||||
}
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
|
||||
const VkCommandBuffer *pCommandBuffers)
|
||||
{
|
||||
vkb_wire_vkFreeCommandBuffers(device, commandPool, commandBufferCount, pCommandBuffers);
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
vkb_cmdpool *p = pool_get(dev, commandPool, 0);
|
||||
for (uint32_t i = 0; i < commandBufferCount; i++) {
|
||||
vkb_cmdbuf *cb = vkb_cb(pCommandBuffers[i]);
|
||||
if (!cb) continue;
|
||||
if (p) {
|
||||
if (cb->pool_prev) cb->pool_prev->pool_next = cb->pool_next;
|
||||
else if (p->head == cb) p->head = cb->pool_next;
|
||||
if (cb->pool_next) cb->pool_next->pool_prev = cb->pool_prev;
|
||||
}
|
||||
cmdbuf_free(cb);
|
||||
}
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
if (!commandPool) return;
|
||||
vkb_wire_vkDestroyCommandPool(device, commandPool, pAllocator);
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
vkb_cmdpool *p = vkb_map_remove(&dev->pools, (uint64_t)commandPool);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
if (p) vkb_cmdpool_free(p);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags)
|
||||
{
|
||||
VkResult r = vkb_wire_vkResetCommandPool(device, commandPool, flags);
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
vkb_cmdpool *p = pool_get(dev, commandPool, 0);
|
||||
for (vkb_cmdbuf *cb = p ? p->head : NULL; cb; cb = cb->pool_next) stream_reset(cb);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ recording */
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo)
|
||||
{
|
||||
vkb_cmdbuf *cb = vkb_cb(commandBuffer);
|
||||
stream_reset(cb);
|
||||
cb->recording = 1;
|
||||
vkb_enc *e = vkb_cmd_record_begin(commandBuffer, VKB_CMD_vkBeginCommandBuffer);
|
||||
vkb_enc_u64(e, cb->obj.remote);
|
||||
tls_encoding = cb;
|
||||
vkb_enc_u8(e, pBeginInfo != NULL);
|
||||
if (pBeginInfo) vkb_enc_VkCommandBufferBeginInfo(e, pBeginInfo);
|
||||
tls_encoding = NULL;
|
||||
vkb_cmd_record_end(commandBuffer);
|
||||
return cb->broken ? VK_ERROR_OUT_OF_HOST_MEMORY : VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEndCommandBuffer(VkCommandBuffer commandBuffer)
|
||||
{
|
||||
vkb_cmdbuf *cb = vkb_cb(commandBuffer);
|
||||
if (!cb->recording) return VK_SUCCESS;
|
||||
cb->recording = 0;
|
||||
if (cb->broken || cb->stream.oom) {
|
||||
VKB_ERR("command buffer could not be recorded (out of memory)");
|
||||
stream_reset(cb);
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkEndCommandBuffer, cb->obj.table);
|
||||
vkb_enc_u64(&c.e, cb->obj.remote);
|
||||
vkb_enc_blob(&c.e, cb->stream.buf ? cb->stream.buf : (const void *)"", cb->stream.len);
|
||||
VkResult r = VK_ERROR_DEVICE_LOST;
|
||||
if (vkb_async_results()) {
|
||||
/* Replayed on the server while the app goes on; ordered before its submit. */
|
||||
r = vkb_call_exec_async(&c) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST;
|
||||
} else if (vkb_call_exec(&c)) {
|
||||
r = (VkResult)vkb_dec_u32(&c.d);
|
||||
vkb_call_end(&c);
|
||||
}
|
||||
stream_reset(cb);
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetCommandBuffer(VkCommandBuffer commandBuffer, VkCommandBufferResetFlags flags)
|
||||
{
|
||||
stream_reset(vkb_cb(commandBuffer));
|
||||
return vkb_wire_vkResetCommandBuffer(commandBuffer, flags);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
|
||||
const VkCommandBuffer *pCommandBuffers)
|
||||
{
|
||||
vkb_wire_vkCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
|
||||
}
|
||||
|
||||
/* Debug labels never reach the driver. */
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkCmdBeginDebugUtilsLabelEXT(VkCommandBuffer commandBuffer, const VkDebugUtilsLabelEXT *pLabelInfo)
|
||||
{
|
||||
(void)commandBuffer;
|
||||
(void)pLabelInfo;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkCmdEndDebugUtilsLabelEXT(VkCommandBuffer commandBuffer)
|
||||
{
|
||||
(void)commandBuffer;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkCmdInsertDebugUtilsLabelEXT(VkCommandBuffer commandBuffer, const VkDebugUtilsLabelEXT *pLabelInfo)
|
||||
{
|
||||
(void)commandBuffer;
|
||||
(void)pLabelInfo;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Minimal Xlib declarations for the bridge ICD: it never links libX11, it only needs the types
|
||||
* vulkan_xlib.h names and looks up XGetXCBConnection at run time. */
|
||||
#ifndef VKB_COMPAT_XLIB_H
|
||||
#define VKB_COMPAT_XLIB_H
|
||||
typedef struct _XDisplay Display;
|
||||
typedef unsigned long XID;
|
||||
typedef XID Window;
|
||||
typedef unsigned long VisualID;
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Minimal xcb declarations for the bridge ICD: libxcb is opened at run time (dlopen), so only the
|
||||
* types and the few request/reply layouts the software present path uses are declared here. */
|
||||
#ifndef VKB_COMPAT_XCB_H
|
||||
#define VKB_COMPAT_XCB_H
|
||||
#include <stdint.h>
|
||||
typedef struct xcb_connection_t xcb_connection_t;
|
||||
typedef uint32_t xcb_window_t;
|
||||
typedef uint32_t xcb_visualid_t;
|
||||
typedef uint32_t xcb_drawable_t;
|
||||
typedef uint32_t xcb_gcontext_t;
|
||||
typedef uint32_t xcb_pixmap_t;
|
||||
typedef struct { unsigned int sequence; } xcb_void_cookie_t;
|
||||
typedef struct { unsigned int sequence; } xcb_get_geometry_cookie_t;
|
||||
typedef struct {
|
||||
uint8_t response_type;
|
||||
uint8_t depth;
|
||||
uint16_t sequence;
|
||||
uint32_t length;
|
||||
xcb_window_t root;
|
||||
int16_t x, y;
|
||||
uint16_t width, height, border_width;
|
||||
uint8_t pad0[2];
|
||||
} xcb_get_geometry_reply_t;
|
||||
typedef struct {
|
||||
uint8_t response_type;
|
||||
uint8_t error_code;
|
||||
uint16_t sequence;
|
||||
uint32_t resource_id;
|
||||
uint16_t minor_code;
|
||||
uint8_t major_code;
|
||||
uint8_t pad0;
|
||||
uint32_t pad[5];
|
||||
uint32_t full_sequence;
|
||||
} xcb_generic_error_t;
|
||||
#define XCB_IMAGE_FORMAT_Z_PIXMAP 2
|
||||
#endif
|
||||
@@ -0,0 +1,672 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Client transport: one Unix-socket connection per thread (the server mirrors it with one thread),
|
||||
* so calls from different threads run in parallel exactly as they would on a local driver.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_client.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/un.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* ------------------------------------------------------------------ logging */
|
||||
|
||||
static int log_level_v = -1;
|
||||
static FILE *log_file;
|
||||
static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
int vkb_log_level(void)
|
||||
{
|
||||
if (log_level_v < 0) {
|
||||
const char *d = getenv("VKBRIDGE_DEBUG");
|
||||
log_level_v = d && *d && strcmp(d, "0") ? VKB_LOG_DEBUG : VKB_LOG_WARN;
|
||||
const char *v = getenv("VKBRIDGE_LOG_LEVEL");
|
||||
if (v) log_level_v = atoi(v);
|
||||
const char *f = getenv("VKBRIDGE_CLIENT_LOG");
|
||||
if (f && *f) log_file = fopen(f, "ae");
|
||||
}
|
||||
return log_level_v;
|
||||
}
|
||||
|
||||
void vkb_log(int level, const char *fmt, ...)
|
||||
{
|
||||
if (level > vkb_log_level()) return;
|
||||
char msg[2048];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(msg, sizeof(msg), fmt, ap);
|
||||
va_end(ap);
|
||||
static const char *const tags[] = {"error", "warning", "info", "debug"};
|
||||
char comm[32] = "?";
|
||||
FILE *cf = fopen("/proc/self/comm", "re");
|
||||
if (cf) {
|
||||
if (fgets(comm, sizeof(comm), cf)) comm[strcspn(comm, "\n")] = 0;
|
||||
fclose(cf);
|
||||
}
|
||||
pthread_mutex_lock(&log_lock);
|
||||
fprintf(stderr, "vkbridge %s [%s %d]: %s\n", tags[level & 3], comm, getpid(), msg);
|
||||
if (log_file) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
struct tm tm;
|
||||
localtime_r(&ts.tv_sec, &tm);
|
||||
fprintf(log_file, "%02d:%02d:%02d.%03ld vkbridge-client %s [%s %d]: %s\n", tm.tm_hour, tm.tm_min, tm.tm_sec,
|
||||
ts.tv_nsec / 1000000, tags[level & 3], comm, getpid(), msg);
|
||||
fflush(log_file);
|
||||
}
|
||||
pthread_mutex_unlock(&log_lock);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ connection */
|
||||
|
||||
typedef struct vkb_conn {
|
||||
int sock;
|
||||
int dead;
|
||||
uint32_t epoch;
|
||||
vkb_enc enc; /* reused request buffer */
|
||||
uint8_t *in;
|
||||
size_t incap;
|
||||
vkb_arena arena;
|
||||
int busy;
|
||||
} vkb_conn;
|
||||
|
||||
static uint64_t process_token;
|
||||
/* The last asynchronous request's sequence number in this process (see vkb_msg_hdr). */
|
||||
static uint64_t async_seq;
|
||||
static uint32_t fork_epoch;
|
||||
static pthread_key_t conn_key;
|
||||
static pthread_once_t conn_once = PTHREAD_ONCE_INIT;
|
||||
static __thread vkb_conn *tls_conn;
|
||||
|
||||
static void conn_free(vkb_conn *c)
|
||||
{
|
||||
if (!c) return;
|
||||
if (c->sock >= 0) close(c->sock);
|
||||
vkb_enc_free(&c->enc);
|
||||
free(c->in);
|
||||
vkb_arena_free(&c->arena);
|
||||
free(c);
|
||||
}
|
||||
|
||||
static void conn_thread_exit(void *p)
|
||||
{
|
||||
conn_free(p);
|
||||
}
|
||||
|
||||
/* Asynchronous requests of the whole process wait here and are written out in one go, ahead of
|
||||
* the next synchronous request any thread makes (or when the queue grows large). They carry their
|
||||
* sequence numbers, so whichever connection carries them, the server runs them in order. */
|
||||
static pthread_mutex_t aq_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static uint8_t *aq_buf;
|
||||
static size_t aq_len, aq_cap;
|
||||
#define AQ_FLUSH (256u << 10)
|
||||
|
||||
static int write_all(int sock, const void *p, size_t n)
|
||||
{
|
||||
const uint8_t *b = p;
|
||||
while (n) {
|
||||
ssize_t r = send(sock, b, n, MSG_NOSIGNAL);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
return -1;
|
||||
}
|
||||
b += r;
|
||||
n -= (size_t)r;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int aq_flush_locked(vkb_conn *conn)
|
||||
{
|
||||
if (!aq_len) return 0;
|
||||
int r = write_all(conn->sock, aq_buf, aq_len);
|
||||
aq_len = 0;
|
||||
if (aq_cap > (4u << 20)) {
|
||||
free(aq_buf);
|
||||
aq_buf = NULL;
|
||||
aq_cap = 0;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/* The flusher: asynchronous requests do not wait for the next synchronous one forever - another
|
||||
* thread may be blocked in the server on work they would start (a submit that signals what it is
|
||||
* waiting for). It sends what is queued ~100 us after it was queued, batching what follows. */
|
||||
static pthread_cond_t aq_cond = PTHREAD_COND_INITIALIZER;
|
||||
static int aq_flusher_started;
|
||||
static uint64_t aq_first_ns;
|
||||
|
||||
static uint64_t now_ns(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
|
||||
}
|
||||
|
||||
static vkb_conn *get_conn(void);
|
||||
|
||||
static void *aq_flusher(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
vkb_conn *conn = get_conn();
|
||||
if (!conn) return NULL;
|
||||
pthread_mutex_lock(&aq_lock);
|
||||
for (;;) {
|
||||
while (!aq_len) pthread_cond_wait(&aq_cond, &aq_lock);
|
||||
uint64_t due = aq_first_ns + 100000, now = now_ns();
|
||||
if (now < due && aq_len < AQ_FLUSH / 4) {
|
||||
pthread_mutex_unlock(&aq_lock);
|
||||
struct timespec ts = {0, (long)(due - now)};
|
||||
nanosleep(&ts, NULL);
|
||||
pthread_mutex_lock(&aq_lock);
|
||||
continue;
|
||||
}
|
||||
if (aq_flush_locked(conn) < 0) {
|
||||
conn->dead = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&aq_lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void aq_exit_flush(void)
|
||||
{
|
||||
vkb_conn *c = tls_conn;
|
||||
if (!c || c->dead) return;
|
||||
pthread_mutex_lock(&aq_lock);
|
||||
aq_flush_locked(c);
|
||||
pthread_mutex_unlock(&aq_lock);
|
||||
}
|
||||
|
||||
static void atfork_child(void)
|
||||
{
|
||||
aq_len = 0;
|
||||
aq_flusher_started = 0;
|
||||
pthread_cond_init(&aq_cond, NULL);
|
||||
pthread_mutex_init(&aq_lock, NULL);
|
||||
/* The child must not talk on the parent's sockets: new token, new connections. */
|
||||
fork_epoch++;
|
||||
process_token = 0;
|
||||
async_seq = 0;
|
||||
tls_conn = NULL; /* the old one is leaked deliberately: its socket belongs to the parent too */
|
||||
}
|
||||
|
||||
static void conn_init_once(void)
|
||||
{
|
||||
pthread_key_create(&conn_key, conn_thread_exit);
|
||||
pthread_atfork(NULL, NULL, atfork_child);
|
||||
atexit(aq_exit_flush);
|
||||
}
|
||||
|
||||
void vkb_conn_after_fork(void) { atfork_child(); }
|
||||
|
||||
static uint64_t make_token(void)
|
||||
{
|
||||
uint64_t t = 0;
|
||||
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
if (read(fd, &t, sizeof(t)) != sizeof(t)) t = 0;
|
||||
close(fd);
|
||||
}
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
t ^= ((uint64_t)getpid() << 32) ^ (uint64_t)ts.tv_nsec ^ (uint64_t)ts.tv_sec * 1000003u;
|
||||
return t ? t : 1;
|
||||
}
|
||||
|
||||
static const char *socket_path(void)
|
||||
{
|
||||
const char *p = getenv(VKB_DEFAULT_SOCKET_ENV);
|
||||
if (p && *p) return p;
|
||||
static char buf[256];
|
||||
const char *rd = getenv("XDG_RUNTIME_DIR");
|
||||
snprintf(buf, sizeof(buf), "%s/vkbridge.sock", rd && *rd ? rd : "/tmp");
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int do_connect(vkb_conn *c)
|
||||
{
|
||||
const char *path = socket_path();
|
||||
struct sockaddr_un a;
|
||||
memset(&a, 0, sizeof(a));
|
||||
a.sun_family = AF_UNIX;
|
||||
if (strlen(path) >= sizeof(a.sun_path)) {
|
||||
VKB_ERR("socket path too long: %s", path);
|
||||
return -1;
|
||||
}
|
||||
strcpy(a.sun_path, path);
|
||||
int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (s < 0) return -1;
|
||||
if (connect(s, (struct sockaddr *)&a, sizeof(a)) < 0) {
|
||||
VKB_ONCE(VKB_LOG_ERROR, "cannot reach the Vulkan bridge server at %s: %s", path, strerror(errno));
|
||||
close(s);
|
||||
return -1;
|
||||
}
|
||||
c->sock = s;
|
||||
/* Hello: protocol check and which process this thread belongs to. */
|
||||
if (!process_token) process_token = make_token();
|
||||
vkb_enc e;
|
||||
vkb_enc_init(&e);
|
||||
vkb_enc_u64(&e, VKB_PROTOCOL_HASH);
|
||||
vkb_enc_u64(&e, process_token);
|
||||
vkb_enc_u32(&e, (uint32_t)getpid());
|
||||
char comm[64] = "?";
|
||||
FILE *f = fopen("/proc/self/comm", "re");
|
||||
if (f) {
|
||||
if (fgets(comm, sizeof(comm), f)) comm[strcspn(comm, "\n")] = 0;
|
||||
fclose(f);
|
||||
}
|
||||
vkb_enc_str(&e, comm);
|
||||
vkb_msg_hdr h = {VKB_MAGIC, (uint32_t)e.len, VKB_CMD_vkbHello, 0, 0, 0, 0, 0};
|
||||
int ok = vkb_send_msg(s, &h, e.buf, NULL, 0) == 0;
|
||||
vkb_enc_free(&e);
|
||||
int fds[VKB_MAX_FDS], nfds = 0;
|
||||
vkb_msg_hdr rh;
|
||||
if (ok) ok = vkb_recv_msg(s, &rh, &c->in, &c->incap, fds, &nfds) == 0 && rh.cmd == VKB_ST_OK && rh.size >= 12;
|
||||
if (ok) {
|
||||
uint64_t sh;
|
||||
uint32_t accepted;
|
||||
memcpy(&sh, c->in, 8);
|
||||
memcpy(&accepted, c->in + 8, 4);
|
||||
if (!accepted) {
|
||||
VKB_ONCE(VKB_LOG_ERROR, "the bridge server speaks protocol %016llx, this driver %016llx; update DroidDeck's Linux side",
|
||||
(unsigned long long)sh, (unsigned long long)VKB_PROTOCOL_HASH);
|
||||
ok = 0;
|
||||
}
|
||||
}
|
||||
if (!ok) {
|
||||
close(s);
|
||||
c->sock = -1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static vkb_conn *get_conn(void)
|
||||
{
|
||||
pthread_once(&conn_once, conn_init_once);
|
||||
vkb_conn *c = tls_conn;
|
||||
if (c && c->epoch != fork_epoch) c = tls_conn = NULL;
|
||||
if (c) return c->dead ? NULL : c;
|
||||
c = calloc(1, sizeof(*c));
|
||||
if (!c) return NULL;
|
||||
c->sock = -1;
|
||||
c->epoch = fork_epoch;
|
||||
vkb_enc_init(&c->enc);
|
||||
if (do_connect(c) < 0) {
|
||||
c->dead = 1;
|
||||
}
|
||||
tls_conn = c;
|
||||
pthread_setspecific(conn_key, c);
|
||||
return c->dead ? NULL : c;
|
||||
}
|
||||
|
||||
int vkb_connected(void)
|
||||
{
|
||||
return get_conn() != NULL;
|
||||
}
|
||||
|
||||
void vkb_call_begin(vkb_call *c, uint32_t cmd, uint32_t table)
|
||||
{
|
||||
memset(c, 0, sizeof(*c));
|
||||
c->cmd = cmd;
|
||||
c->table = table;
|
||||
c->conn = get_conn();
|
||||
if (c->conn && !c->conn->busy) {
|
||||
c->conn->busy = 1;
|
||||
c->e = c->conn->enc;
|
||||
vkb_enc_reset(&c->e);
|
||||
} else {
|
||||
/* No connection, or a nested call: encode into a private buffer. */
|
||||
if (c->conn && c->conn->busy) VKB_ERR("internal: nested bridge call (%s)", vkb_cmd_names[cmd]);
|
||||
vkb_enc_init(&c->e);
|
||||
c->flags |= 0x80000000u;
|
||||
}
|
||||
}
|
||||
|
||||
static void release_enc(vkb_call *c)
|
||||
{
|
||||
if (c->flags & 0x80000000u) {
|
||||
vkb_enc_free(&c->e);
|
||||
} else if (c->conn) {
|
||||
c->conn->enc = c->e;
|
||||
c->conn->busy = 0;
|
||||
/* Don't keep a huge buffer around after one big message. */
|
||||
if (c->conn->enc.cap > (16u << 20)) {
|
||||
vkb_enc_free(&c->conn->enc);
|
||||
vkb_enc_init(&c->conn->enc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int trace_on(void)
|
||||
{
|
||||
static int v = -1;
|
||||
if (v < 0) v = getenv("VKBRIDGE_TRACE") && strcmp(getenv("VKBRIDGE_TRACE"), "0");
|
||||
return v;
|
||||
}
|
||||
|
||||
int vkb_call_exec(vkb_call *c)
|
||||
{
|
||||
vkb_conn *conn = c->conn;
|
||||
if (trace_on()) {
|
||||
/* VkResult-returning calls put it first in the reply; peeked after the round trip. */
|
||||
vkb_log(VKB_LOG_ERROR, "trace: %s", vkb_cmd_names[c->cmd]);
|
||||
}
|
||||
if (!conn || conn->dead || (c->flags & 0x80000000u)) {
|
||||
release_enc(c);
|
||||
c->conn = NULL;
|
||||
return 0;
|
||||
}
|
||||
if (c->e.oom) {
|
||||
VKB_ERR("%s: request could not be encoded", vkb_cmd_names[c->cmd]);
|
||||
release_enc(c);
|
||||
c->conn = NULL;
|
||||
return 0;
|
||||
}
|
||||
vkb_msg_hdr h = {VKB_MAGIC, (uint32_t)c->e.len, c->cmd, c->table, (uint32_t)c->e.nfds, c->flags & VKB_F_NOREPLY, 0, 0};
|
||||
vkb_stats_count(c->cmd, (c->flags & VKB_F_NOREPLY) != 0, c->e.len);
|
||||
pthread_mutex_lock(&aq_lock);
|
||||
if ((c->flags & VKB_F_NOREPLY) && !c->e.nfds) {
|
||||
/* Queued: header and payload appended under the lock that also orders the seqs. */
|
||||
h.seq = ++async_seq;
|
||||
h.barrier = h.seq - 1;
|
||||
size_t need = aq_len + sizeof(h) + c->e.len;
|
||||
if (need > aq_cap) {
|
||||
size_t nc = aq_cap ? aq_cap : 65536;
|
||||
while (nc < need) nc *= 2;
|
||||
uint8_t *nb = realloc(aq_buf, nc);
|
||||
if (!nb) {
|
||||
pthread_mutex_unlock(&aq_lock);
|
||||
release_enc(c);
|
||||
c->conn = NULL;
|
||||
return 0;
|
||||
}
|
||||
aq_buf = nb;
|
||||
aq_cap = nc;
|
||||
}
|
||||
if (!aq_len) aq_first_ns = now_ns();
|
||||
memcpy(aq_buf + aq_len, &h, sizeof(h));
|
||||
memcpy(aq_buf + aq_len + sizeof(h), c->e.buf, c->e.len);
|
||||
aq_len = need;
|
||||
if (!aq_flusher_started) {
|
||||
aq_flusher_started = 1;
|
||||
pthread_t t;
|
||||
pthread_attr_t a;
|
||||
pthread_attr_init(&a);
|
||||
pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
|
||||
if (pthread_create(&t, &a, aq_flusher, NULL) != 0) aq_flusher_started = 0;
|
||||
pthread_attr_destroy(&a);
|
||||
}
|
||||
pthread_cond_signal(&aq_cond);
|
||||
int r = aq_len >= AQ_FLUSH ? aq_flush_locked(conn) : 0;
|
||||
pthread_mutex_unlock(&aq_lock);
|
||||
release_enc(c);
|
||||
c->conn = NULL;
|
||||
if (r < 0) {
|
||||
VKB_ERR("lost the bridge server (%s)", strerror(errno));
|
||||
conn->dead = 1;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/* Everything queued goes first, on this connection. */
|
||||
int qr = aq_flush_locked(conn);
|
||||
if (c->flags & VKB_F_NOREPLY) {
|
||||
h.seq = ++async_seq;
|
||||
h.barrier = h.seq - 1;
|
||||
} else {
|
||||
h.barrier = async_seq;
|
||||
}
|
||||
pthread_mutex_unlock(&aq_lock);
|
||||
if (qr < 0 || vkb_send_msg(conn->sock, &h, c->e.buf, c->e.fds, c->e.nfds) < 0) {
|
||||
VKB_ERR("%s: lost the bridge server (%s)", vkb_cmd_names[c->cmd], strerror(errno));
|
||||
conn->dead = 1;
|
||||
release_enc(c);
|
||||
c->conn = NULL;
|
||||
return 0;
|
||||
}
|
||||
release_enc(c);
|
||||
if (c->flags & VKB_F_NOREPLY) {
|
||||
c->conn = NULL;
|
||||
return 1;
|
||||
}
|
||||
int fds[VKB_MAX_FDS], nfds = 0;
|
||||
vkb_msg_hdr rh;
|
||||
if (vkb_recv_msg(conn->sock, &rh, &conn->in, &conn->incap, fds, &nfds) < 0) {
|
||||
VKB_ERR("%s: lost the bridge server while waiting for the reply", vkb_cmd_names[c->cmd]);
|
||||
conn->dead = 1;
|
||||
c->conn = NULL;
|
||||
return 0;
|
||||
}
|
||||
if (rh.cmd != VKB_ST_OK) {
|
||||
static const char *const why[] = {"ok", "malformed request", "the driver lacks this command", "unknown object"};
|
||||
VKB_ERR("%s: the server refused the call (%s)", vkb_cmd_names[c->cmd], rh.cmd < 4 ? why[rh.cmd] : "?");
|
||||
for (int i = 0; i < nfds; i++) close(fds[i]);
|
||||
c->conn = NULL;
|
||||
return 0;
|
||||
}
|
||||
/* The decoder keeps fds in a per-call array. */
|
||||
static __thread int call_fds[VKB_MAX_FDS];
|
||||
memcpy(call_fds, fds, sizeof(int) * (size_t)nfds);
|
||||
vkb_dec_init(&c->d, conn->in, rh.size, &conn->arena, call_fds, nfds);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void vkb_call_end(vkb_call *c)
|
||||
{
|
||||
if (!c->conn) return;
|
||||
if (c->d.err) VKB_ERR("%s: malformed reply", vkb_cmd_names[c->cmd]);
|
||||
vkb_dec_close_untaken(&c->d);
|
||||
vkb_arena_reset(&c->conn->arena);
|
||||
c->conn = NULL;
|
||||
}
|
||||
|
||||
/* VkResult commands treated as asynchronous (ASYNC_RESULT in the generator): on unless
|
||||
* VKBRIDGE_SYNC is set. */
|
||||
int vkb_async_results(void)
|
||||
{
|
||||
static int v = -1;
|
||||
if (v < 0) v = getenv("VKBRIDGE_SYNC") == NULL;
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Asynchronous variant: no reply is awaited (void commands without outputs). */
|
||||
int vkb_call_exec_async(vkb_call *c)
|
||||
{
|
||||
if (getenv("VKBRIDGE_SYNC")) return vkb_call_exec(c) ? (vkb_call_end(c), 1) : 0;
|
||||
c->flags |= VKB_F_NOREPLY;
|
||||
return vkb_call_exec(c);
|
||||
}
|
||||
|
||||
/* VKBRIDGE_STATS=<seconds>: per-command counts of round trips and async sends, logged. */
|
||||
static uint32_t stat_sync[VKB_CMD_COUNT], stat_async[VKB_CMD_COUNT];
|
||||
static uint64_t stat_bytes;
|
||||
static uint64_t stat_last;
|
||||
void vkb_stats_count(uint32_t cmd, int async, size_t bytes)
|
||||
{
|
||||
static int period = -1;
|
||||
if (period < 0) {
|
||||
const char *v = getenv("VKBRIDGE_STATS");
|
||||
period = v ? atoi(v) : 0;
|
||||
}
|
||||
if (!period) return;
|
||||
__atomic_add_fetch(async ? &stat_async[cmd] : &stat_sync[cmd], 1, __ATOMIC_RELAXED);
|
||||
__atomic_add_fetch(&stat_bytes, bytes, __ATOMIC_RELAXED);
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
uint64_t now = (uint64_t)ts.tv_sec;
|
||||
uint64_t last = __atomic_load_n(&stat_last, __ATOMIC_RELAXED);
|
||||
if (!last) {
|
||||
__atomic_store_n(&stat_last, now, __ATOMIC_RELAXED);
|
||||
return;
|
||||
}
|
||||
if (now - last < (uint64_t)period || !__atomic_compare_exchange_n(&stat_last, &last, now, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
|
||||
return;
|
||||
uint32_t tot_s = 0, tot_a = 0;
|
||||
for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) {
|
||||
tot_s += stat_sync[i];
|
||||
tot_a += stat_async[i];
|
||||
}
|
||||
vkb_log(VKB_LOG_ERROR, "stats over %us: %u round trips, %u async, %llu KB sent", period, tot_s, tot_a,
|
||||
(unsigned long long)(stat_bytes >> 10));
|
||||
for (int k = 0; k < 8; k++) {
|
||||
uint32_t best = 0, bi = 0;
|
||||
for (uint32_t i = 0; i < VKB_CMD_COUNT; i++)
|
||||
if (stat_sync[i] + stat_async[i] > best) {
|
||||
best = stat_sync[i] + stat_async[i];
|
||||
bi = i;
|
||||
}
|
||||
if (!best) break;
|
||||
vkb_log(VKB_LOG_ERROR, " %-40s %6u sync %6u async", vkb_cmd_names[bi], stat_sync[bi], stat_async[bi]);
|
||||
stat_sync[bi] = stat_async[bi] = 0;
|
||||
}
|
||||
memset(stat_sync, 0, sizeof(stat_sync));
|
||||
memset(stat_async, 0, sizeof(stat_async));
|
||||
stat_bytes = 0;
|
||||
}
|
||||
|
||||
uint32_t vkb_table_of(const void *h)
|
||||
{
|
||||
return h ? ((const vkb_object *)h)->table : 0;
|
||||
}
|
||||
|
||||
uint64_t vkb_remote(const void *h)
|
||||
{
|
||||
return h ? ((const vkb_object *)h)->remote : 0;
|
||||
}
|
||||
|
||||
int vkb_memfd(const char *name, size_t size)
|
||||
{
|
||||
int fd = (int)syscall(SYS_memfd_create, name, 1u /* MFD_CLOEXEC */);
|
||||
if (fd < 0) {
|
||||
/* No memfd (old kernel or a filter): an unlinked file in the runtime dir. */
|
||||
char path[256];
|
||||
const char *rd = getenv("XDG_RUNTIME_DIR");
|
||||
snprintf(path, sizeof(path), "%s/vkbridge-XXXXXX", rd && *rd ? rd : "/tmp");
|
||||
fd = mkostemp(path, O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
unlink(path);
|
||||
}
|
||||
if (ftruncate(fd, (off_t)size) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ hash map */
|
||||
|
||||
void vkb_map_init(vkb_map *m)
|
||||
{
|
||||
memset(m, 0, sizeof(*m));
|
||||
pthread_mutex_init(&m->lock, NULL);
|
||||
}
|
||||
|
||||
void vkb_map_destroy(vkb_map *m)
|
||||
{
|
||||
free(m->keys);
|
||||
free(m->vals);
|
||||
pthread_mutex_destroy(&m->lock);
|
||||
memset(m, 0, sizeof(*m));
|
||||
}
|
||||
|
||||
static uint32_t hash64(uint64_t k)
|
||||
{
|
||||
k ^= k >> 33;
|
||||
k *= 0xff51afd7ed558ccdull;
|
||||
k ^= k >> 33;
|
||||
return (uint32_t)k;
|
||||
}
|
||||
|
||||
/* Keys are never 0 (VK_NULL_HANDLE); 0 marks an empty slot, ~0 a deleted one. */
|
||||
#define TOMB (~(uint64_t)0)
|
||||
|
||||
static void map_rehash(vkb_map *m, uint32_t ncap)
|
||||
{
|
||||
uint64_t *ok = m->keys;
|
||||
void **ov = m->vals;
|
||||
uint32_t oc = m->cap;
|
||||
m->keys = calloc(ncap, sizeof(uint64_t));
|
||||
m->vals = calloc(ncap, sizeof(void *));
|
||||
m->cap = ncap;
|
||||
m->count = 0;
|
||||
for (uint32_t i = 0; i < oc; i++) {
|
||||
if (ok[i] && ok[i] != TOMB) {
|
||||
uint32_t j = hash64(ok[i]) & (ncap - 1);
|
||||
while (m->keys[j]) j = (j + 1) & (ncap - 1);
|
||||
m->keys[j] = ok[i];
|
||||
m->vals[j] = ov[i];
|
||||
m->count++;
|
||||
}
|
||||
}
|
||||
free(ok);
|
||||
free(ov);
|
||||
}
|
||||
|
||||
void vkb_map_put(vkb_map *m, uint64_t k, void *v)
|
||||
{
|
||||
if (!k) return;
|
||||
pthread_mutex_lock(&m->lock);
|
||||
if (!m->cap || (m->count + 1) * 2 > m->cap) map_rehash(m, m->cap ? m->cap * 2 : 64);
|
||||
uint32_t j = hash64(k) & (m->cap - 1);
|
||||
int64_t tomb = -1;
|
||||
while (m->keys[j]) {
|
||||
if (m->keys[j] == k) {
|
||||
m->vals[j] = v;
|
||||
pthread_mutex_unlock(&m->lock);
|
||||
return;
|
||||
}
|
||||
if (m->keys[j] == TOMB && tomb < 0) tomb = j;
|
||||
j = (j + 1) & (m->cap - 1);
|
||||
}
|
||||
if (tomb >= 0) j = (uint32_t)tomb;
|
||||
m->keys[j] = k;
|
||||
m->vals[j] = v;
|
||||
m->count++;
|
||||
pthread_mutex_unlock(&m->lock);
|
||||
}
|
||||
|
||||
static int64_t map_find(vkb_map *m, uint64_t k)
|
||||
{
|
||||
if (!m->cap || !k) return -1;
|
||||
uint32_t j = hash64(k) & (m->cap - 1);
|
||||
for (uint32_t n = 0; n < m->cap && m->keys[j]; n++) {
|
||||
if (m->keys[j] == k) return j;
|
||||
j = (j + 1) & (m->cap - 1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *vkb_map_get(vkb_map *m, uint64_t k)
|
||||
{
|
||||
pthread_mutex_lock(&m->lock);
|
||||
int64_t j = map_find(m, k);
|
||||
void *v = j >= 0 ? m->vals[j] : NULL;
|
||||
pthread_mutex_unlock(&m->lock);
|
||||
return v;
|
||||
}
|
||||
|
||||
void *vkb_map_remove(vkb_map *m, uint64_t k)
|
||||
{
|
||||
pthread_mutex_lock(&m->lock);
|
||||
int64_t j = map_find(m, k);
|
||||
void *v = NULL;
|
||||
if (j >= 0) {
|
||||
v = m->vals[j];
|
||||
m->keys[j] = TOMB;
|
||||
m->vals[j] = NULL;
|
||||
/* count keeps tombstones, so the table rehashes before it fills with them */
|
||||
}
|
||||
pthread_mutex_unlock(&m->lock);
|
||||
return v;
|
||||
}
|
||||
@@ -0,0 +1,652 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Descriptor update templates and private data.
|
||||
*
|
||||
* A template's data is laid out by the app (offsets and strides into its own structures). The
|
||||
* server's copy of the template is created with packed entries instead, and every update packs
|
||||
* the app's data the same way before sending it, so the server passes the blob straight on.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_client.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
typedef struct vkb_template {
|
||||
uint32_t count;
|
||||
size_t size; /* packed bytes */
|
||||
VkDescriptorUpdateTemplateEntry *app; /* the app's entries */
|
||||
size_t *packed; /* packed offset per entry */
|
||||
size_t *elem; /* element size per entry (inline: 1) */
|
||||
} vkb_template;
|
||||
|
||||
static size_t elem_size(VkDescriptorType t)
|
||||
{
|
||||
switch (t) {
|
||||
case VK_DESCRIPTOR_TYPE_SAMPLER:
|
||||
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
|
||||
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
|
||||
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
|
||||
case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
|
||||
return sizeof(VkDescriptorImageInfo);
|
||||
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
|
||||
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
|
||||
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
|
||||
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
|
||||
return sizeof(VkDescriptorBufferInfo);
|
||||
case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
|
||||
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
|
||||
return sizeof(VkBufferView);
|
||||
case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
|
||||
return 1;
|
||||
default:
|
||||
return 8;
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorUpdateTemplate(VkDevice device, const VkDescriptorUpdateTemplateCreateInfo *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator,
|
||||
VkDescriptorUpdateTemplate *pTemplate)
|
||||
{
|
||||
uint32_t n = pCreateInfo->descriptorUpdateEntryCount;
|
||||
vkb_template *t = calloc(1, sizeof(*t));
|
||||
t->count = n;
|
||||
t->app = calloc(n ? n : 1, sizeof(*t->app));
|
||||
t->packed = calloc(n ? n : 1, sizeof(size_t));
|
||||
t->elem = calloc(n ? n : 1, sizeof(size_t));
|
||||
VkDescriptorUpdateTemplateEntry *packed = calloc(n ? n : 1, sizeof(*packed));
|
||||
size_t off = 0;
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
const VkDescriptorUpdateTemplateEntry *e = &pCreateInfo->pDescriptorUpdateEntries[i];
|
||||
t->app[i] = *e;
|
||||
size_t es = elem_size(e->descriptorType);
|
||||
t->elem[i] = es;
|
||||
t->packed[i] = off;
|
||||
packed[i] = *e;
|
||||
packed[i].offset = off;
|
||||
packed[i].stride = es;
|
||||
off += (es * e->descriptorCount + 7) & ~(size_t)7;
|
||||
}
|
||||
t->size = off;
|
||||
VkDescriptorUpdateTemplateCreateInfo ci = *pCreateInfo;
|
||||
ci.pDescriptorUpdateEntries = packed;
|
||||
VkResult r = vkb_wire_vkCreateDescriptorUpdateTemplate(device, &ci, pAllocator, pTemplate);
|
||||
free(packed);
|
||||
if (r != VK_SUCCESS) {
|
||||
free(t->app);
|
||||
free(t->packed);
|
||||
free(t->elem);
|
||||
free(t);
|
||||
return r;
|
||||
}
|
||||
vkb_map_put(&vkb_dev(device)->templates, (uint64_t)*pTemplate, t);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void template_free(vkb_template *t)
|
||||
{
|
||||
if (!t) return;
|
||||
free(t->app);
|
||||
free(t->packed);
|
||||
free(t->elem);
|
||||
free(t);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorUpdateTemplate(VkDevice device, VkDescriptorUpdateTemplate tmpl,
|
||||
const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
if (!tmpl) return;
|
||||
template_free(vkb_map_remove(&vkb_dev(device)->templates, (uint64_t)tmpl));
|
||||
vkb_wire_vkDestroyDescriptorUpdateTemplate(device, tmpl, pAllocator);
|
||||
}
|
||||
|
||||
static void pack(const vkb_template *t, const void *pData, uint8_t *out)
|
||||
{
|
||||
const uint8_t *src = pData;
|
||||
for (uint32_t i = 0; i < t->count; i++) {
|
||||
const VkDescriptorUpdateTemplateEntry *e = &t->app[i];
|
||||
uint8_t *dst = out + t->packed[i];
|
||||
if (e->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK) {
|
||||
memcpy(dst, src + e->offset, e->descriptorCount);
|
||||
continue;
|
||||
}
|
||||
size_t es = t->elem[i];
|
||||
for (uint32_t j = 0; j < e->descriptorCount; j++) memcpy(dst + j * es, src + e->offset + (size_t)j * e->stride, es);
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkUpdateDescriptorSetWithTemplate(VkDevice device, VkDescriptorSet set, VkDescriptorUpdateTemplate tmpl,
|
||||
const void *pData)
|
||||
{
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_template *t = vkb_map_get(&dev->templates, (uint64_t)tmpl);
|
||||
if (!t) {
|
||||
VKB_ERR("vkUpdateDescriptorSetWithTemplate with an unknown template");
|
||||
return;
|
||||
}
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkbDescUpdateRaw, dev->obj.table);
|
||||
vkb_enc_u64(&c.e, dev->obj.remote);
|
||||
vkb_enc_bytes(&c.e, &set, sizeof(set));
|
||||
vkb_enc_bytes(&c.e, &tmpl, sizeof(tmpl));
|
||||
/* Packed straight into the request. */
|
||||
vkb_enc_u8(&c.e, 1);
|
||||
vkb_enc_u64(&c.e, t->size);
|
||||
size_t at = vkb_enc_raw(&c.e, NULL, t->size);
|
||||
if (!c.e.oom) pack(t, pData, c.e.buf + at);
|
||||
vkb_call_exec_async(&c);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkCmdPushDescriptorSetWithTemplate(VkCommandBuffer commandBuffer, VkDescriptorUpdateTemplate tmpl,
|
||||
VkPipelineLayout layout, uint32_t set, const void *pData)
|
||||
{
|
||||
vkb_cmdbuf *cb = vkb_cb(commandBuffer);
|
||||
vkb_template *t = vkb_map_get(&cb->device->templates, (uint64_t)tmpl);
|
||||
if (!t) {
|
||||
VKB_ERR("vkCmdPushDescriptorSetWithTemplateKHR with an unknown template");
|
||||
return;
|
||||
}
|
||||
vkb_enc *e = vkb_cmd_record_begin(commandBuffer, VKB_CMD_vkbPushDescRaw);
|
||||
if (!e) return;
|
||||
vkb_enc_u64(e, cb->obj.remote);
|
||||
vkb_enc_bytes(e, &tmpl, sizeof(tmpl));
|
||||
vkb_enc_bytes(e, &layout, sizeof(layout));
|
||||
vkb_enc_u32(e, set);
|
||||
vkb_enc_u8(e, 1);
|
||||
vkb_enc_u64(e, t->size);
|
||||
size_t at = vkb_enc_raw(e, NULL, t->size);
|
||||
if (!e->oom) pack(t, pData, e->buf + at);
|
||||
vkb_cmd_record_end(commandBuffer);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ private data (client-local) */
|
||||
|
||||
typedef struct vkb_private_slot {
|
||||
vkb_map values;
|
||||
} vkb_private_slot;
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreatePrivateDataSlot(VkDevice device, const VkPrivateDataSlotCreateInfo *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkPrivateDataSlot *pSlot)
|
||||
{
|
||||
(void)device;
|
||||
(void)pCreateInfo;
|
||||
(void)pAllocator;
|
||||
vkb_private_slot *s = calloc(1, sizeof(*s));
|
||||
if (!s) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
vkb_map_init(&s->values);
|
||||
*pSlot = (VkPrivateDataSlot)(uintptr_t)s;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyPrivateDataSlot(VkDevice device, VkPrivateDataSlot slot, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
(void)device;
|
||||
(void)pAllocator;
|
||||
vkb_private_slot *s = (vkb_private_slot *)(uintptr_t)slot;
|
||||
if (!s) return;
|
||||
vkb_map_destroy(&s->values);
|
||||
free(s);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkSetPrivateData(VkDevice device, VkObjectType objectType, uint64_t objectHandle,
|
||||
VkPrivateDataSlot slot, uint64_t data)
|
||||
{
|
||||
(void)device;
|
||||
(void)objectType;
|
||||
vkb_private_slot *s = (vkb_private_slot *)(uintptr_t)slot;
|
||||
vkb_map_put(&s->values, objectHandle, (void *)(uintptr_t)data);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPrivateData(VkDevice device, VkObjectType objectType, uint64_t objectHandle,
|
||||
VkPrivateDataSlot slot, uint64_t *pData)
|
||||
{
|
||||
(void)device;
|
||||
(void)objectType;
|
||||
vkb_private_slot *s = (vkb_private_slot *)(uintptr_t)slot;
|
||||
*pData = (uint64_t)(uintptr_t)vkb_map_get(&s->values, objectHandle);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ debug utils (no-ops) */
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkSetDebugUtilsObjectNameEXT(VkDevice device, const VkDebugUtilsObjectNameInfoEXT *pNameInfo)
|
||||
{
|
||||
(void)device;
|
||||
(void)pNameInfo;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkSetDebugUtilsObjectTagEXT(VkDevice device, const VkDebugUtilsObjectTagInfoEXT *pTagInfo)
|
||||
{
|
||||
(void)device;
|
||||
(void)pTagInfo;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkQueueBeginDebugUtilsLabelEXT(VkQueue queue, const VkDebugUtilsLabelEXT *pLabelInfo)
|
||||
{
|
||||
(void)queue;
|
||||
(void)pLabelInfo;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkQueueEndDebugUtilsLabelEXT(VkQueue queue)
|
||||
{
|
||||
(void)queue;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkQueueInsertDebugUtilsLabelEXT(VkQueue queue, const VkDebugUtilsLabelEXT *pLabelInfo)
|
||||
{
|
||||
(void)queue;
|
||||
(void)pLabelInfo;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT *pMessenger)
|
||||
{
|
||||
(void)instance;
|
||||
(void)pCreateInfo;
|
||||
(void)pAllocator;
|
||||
static uint64_t next = 1;
|
||||
*pMessenger = (VkDebugUtilsMessengerEXT)(uintptr_t)__atomic_fetch_add(&next, 1, __ATOMIC_RELAXED);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger,
|
||||
const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
(void)instance;
|
||||
(void)messenger;
|
||||
(void)pAllocator;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkSubmitDebugUtilsMessageEXT(VkInstance instance, VkDebugUtilsMessageSeverityFlagBitsEXT severity,
|
||||
VkDebugUtilsMessageTypeFlagsEXT types,
|
||||
const VkDebugUtilsMessengerCallbackDataEXT *pData)
|
||||
{
|
||||
(void)instance;
|
||||
(void)severity;
|
||||
(void)types;
|
||||
(void)pData;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkCmdPushDescriptorSetWithTemplate2(VkCommandBuffer commandBuffer,
|
||||
const VkPushDescriptorSetWithTemplateInfo *pInfo)
|
||||
{
|
||||
vkb_ep_vkCmdPushDescriptorSetWithTemplate(commandBuffer, pInfo->descriptorUpdateTemplate, pInfo->layout, pInfo->set, pInfo->pData);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ host image copy (not offered) */
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCopyMemoryToImage(VkDevice device, const VkCopyMemoryToImageInfo *pInfo)
|
||||
{
|
||||
(void)device;
|
||||
(void)pInfo;
|
||||
return VK_ERROR_FEATURE_NOT_PRESENT;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCopyImageToMemory(VkDevice device, const VkCopyImageToMemoryInfo *pInfo)
|
||||
{
|
||||
(void)device;
|
||||
(void)pInfo;
|
||||
return VK_ERROR_FEATURE_NOT_PRESENT;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCopyImageToImage(VkDevice device, const VkCopyImageToImageInfo *pInfo)
|
||||
{
|
||||
(void)device;
|
||||
(void)pInfo;
|
||||
return VK_ERROR_FEATURE_NOT_PRESENT;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkTransitionImageLayout(VkDevice device, uint32_t transitionCount,
|
||||
const VkHostImageLayoutTransitionInfo *pTransitions)
|
||||
{
|
||||
(void)device;
|
||||
(void)transitionCount;
|
||||
(void)pTransitions;
|
||||
return VK_ERROR_FEATURE_NOT_PRESENT;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ descriptor set batching */
|
||||
|
||||
/*
|
||||
* DXVK allocates a descriptor set (or two) per draw; one round trip each was most of a frame's
|
||||
* time. Sets are allocated from the server in batches per (pool, layout) and handed out here. A
|
||||
* pool's reset or destruction drops what was cached for it (the reset freed those sets anyway).
|
||||
*
|
||||
* Where it can, the client keeps the pool's books: what each layout takes of every descriptor
|
||||
* type, and what the pool has left. A batch then asks for no more than fits, and a pool that is
|
||||
* full answers VK_ERROR_OUT_OF_POOL_MEMORY here, without a round trip (DXVK fills a pool every few
|
||||
* hundred draws). Pools and layouts the books cannot follow (sets freed one by one, inline uniform
|
||||
* blocks, mutable or variable-count descriptors) go by the driver's answers: a batch that does not
|
||||
* fit falls back to what the app asked for, so the app sees the out-of-pool error when it would.
|
||||
*/
|
||||
#define DS_BATCH 256
|
||||
#define DS_TYPES 12 /* VK_DESCRIPTOR_TYPE_SAMPLER..INPUT_ATTACHMENT, then ACCELERATION_STRUCTURE_KHR */
|
||||
|
||||
static int ds_type_index(VkDescriptorType t)
|
||||
{
|
||||
if ((uint32_t)t <= VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) return (int)t;
|
||||
if (t == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) return 11;
|
||||
return -1;
|
||||
}
|
||||
|
||||
typedef struct ds_cache {
|
||||
VkDescriptorPool pool;
|
||||
VkDescriptorSetLayout layout;
|
||||
VkDescriptorSet sets[DS_BATCH];
|
||||
uint32_t n;
|
||||
uint32_t batch; /* grows from 4 to the pool's max_batch while the pool keeps up */
|
||||
struct ds_cache *next;
|
||||
} ds_cache;
|
||||
|
||||
static pthread_mutex_t ds_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static ds_cache *ds_head;
|
||||
|
||||
/* Pools the app made, with the batch they allow: a pool sized for exactly what the app will
|
||||
* allocate (DXVK's sampler pool) must not lose capacity to prefetching. */
|
||||
typedef struct ds_pool {
|
||||
VkDescriptorPool pool;
|
||||
uint32_t max_batch;
|
||||
int books; /* capacity below is kept */
|
||||
uint32_t max_sets, sets_left;
|
||||
uint32_t size[DS_TYPES], left[DS_TYPES];
|
||||
struct ds_pool *next;
|
||||
} ds_pool;
|
||||
static ds_pool *ds_pools;
|
||||
|
||||
/* Layouts with what one set of them takes; known = 0 when the books cannot follow it. */
|
||||
typedef struct ds_layout {
|
||||
VkDescriptorSetLayout layout;
|
||||
int known;
|
||||
uint32_t count[DS_TYPES];
|
||||
struct ds_layout *next;
|
||||
} ds_layout;
|
||||
static ds_layout *ds_layouts;
|
||||
|
||||
static ds_pool *pool_find(VkDescriptorPool pool)
|
||||
{
|
||||
for (ds_pool *p = ds_pools; p; p = p->next)
|
||||
if (p->pool == pool) return p;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static ds_layout *layout_find(VkDescriptorSetLayout layout)
|
||||
{
|
||||
for (ds_layout *l = ds_layouts; l; l = l->next)
|
||||
if (l->layout == layout) return l;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo *ci,
|
||||
const VkAllocationCallbacks *pAllocator, VkDescriptorSetLayout *pLayout)
|
||||
{
|
||||
VkResult r = vkb_wire_vkCreateDescriptorSetLayout(device, ci, pAllocator, pLayout);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
ds_layout *l = calloc(1, sizeof(*l));
|
||||
if (!l) return r;
|
||||
l->layout = *pLayout;
|
||||
/* Binding flags only matter for variable counts; anything else in the chain is left alone. */
|
||||
l->known = 1;
|
||||
for (const VkBaseInStructure *n = ci->pNext; n; n = n->pNext) {
|
||||
if (n->sType != VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO) {
|
||||
l->known = 0;
|
||||
continue;
|
||||
}
|
||||
const VkDescriptorSetLayoutBindingFlagsCreateInfo *bf = (const void *)n;
|
||||
for (uint32_t i = 0; i < bf->bindingCount; i++)
|
||||
if (bf->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT) l->known = 0;
|
||||
}
|
||||
for (uint32_t i = 0; i < ci->bindingCount && l->known; i++) {
|
||||
int t = ds_type_index(ci->pBindings[i].descriptorType);
|
||||
if (t < 0) l->known = 0;
|
||||
else l->count[t] += ci->pBindings[i].descriptorCount;
|
||||
}
|
||||
pthread_mutex_lock(&ds_lock);
|
||||
l->next = ds_layouts;
|
||||
ds_layouts = l;
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout layout,
|
||||
const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
if (!layout) return;
|
||||
pthread_mutex_lock(&ds_lock);
|
||||
for (ds_layout **pp = &ds_layouts; *pp; pp = &(*pp)->next) {
|
||||
if ((*pp)->layout == layout) {
|
||||
ds_layout *l = *pp;
|
||||
*pp = l->next;
|
||||
free(l);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Sets cached for it stay allocated in their pools, but a new layout may get its handle. */
|
||||
for (ds_cache **pp = &ds_head; *pp;) {
|
||||
if ((*pp)->layout == layout) {
|
||||
ds_cache *c = *pp;
|
||||
*pp = c->next;
|
||||
free(c);
|
||||
} else {
|
||||
pp = &(*pp)->next;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
vkb_wire_vkDestroyDescriptorSetLayout(device, layout, pAllocator);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorPool(VkDevice device, const VkDescriptorPoolCreateInfo *ci,
|
||||
const VkAllocationCallbacks *pAllocator, VkDescriptorPool *pPool)
|
||||
{
|
||||
VkResult r = vkb_wire_vkCreateDescriptorPool(device, ci, pAllocator, pPool);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
ds_pool *p = calloc(1, sizeof(*p));
|
||||
if (!p) return r;
|
||||
p->pool = *pPool;
|
||||
p->max_batch = ci->maxSets >= 256 ? ci->maxSets / 8 : 1;
|
||||
if (p->max_batch > DS_BATCH) p->max_batch = DS_BATCH;
|
||||
p->books = !ci->pNext && !(ci->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
|
||||
p->max_sets = p->sets_left = ci->maxSets;
|
||||
for (uint32_t i = 0; i < ci->poolSizeCount && p->books; i++) {
|
||||
int t = ds_type_index(ci->pPoolSizes[i].type);
|
||||
if (t < 0) p->books = 0;
|
||||
else p->size[t] += ci->pPoolSizes[i].descriptorCount;
|
||||
}
|
||||
memcpy(p->left, p->size, sizeof(p->left));
|
||||
VKB_DBG("descriptor pool: maxSets %u, %u pool sizes, prefetch up to %u%s", ci->maxSets, ci->poolSizeCount,
|
||||
p->max_batch, p->books ? ", capacity tracked" : "");
|
||||
pthread_mutex_lock(&ds_lock);
|
||||
p->next = ds_pools;
|
||||
ds_pools = p;
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
static ds_cache *ds_find(VkDescriptorPool pool, VkDescriptorSetLayout layout, int create)
|
||||
{
|
||||
for (ds_cache *c = ds_head; c; c = c->next)
|
||||
if (c->pool == pool && c->layout == layout) return c;
|
||||
if (!create) return NULL;
|
||||
ds_cache *c = calloc(1, sizeof(*c));
|
||||
if (!c) return NULL;
|
||||
c->pool = pool;
|
||||
c->layout = layout;
|
||||
c->batch = 4;
|
||||
c->next = ds_head;
|
||||
ds_head = c;
|
||||
return c;
|
||||
}
|
||||
|
||||
/* A reset frees the pool's sets, cached ones included; destruction also forgets the pool. The
|
||||
* learned batch size survives a reset (pools are reset every frame). */
|
||||
static void ds_drop_pool(VkDescriptorPool pool, int destroy)
|
||||
{
|
||||
pthread_mutex_lock(&ds_lock);
|
||||
ds_cache **pp = &ds_head;
|
||||
while (*pp) {
|
||||
if ((*pp)->pool == pool && destroy) {
|
||||
ds_cache *c = *pp;
|
||||
*pp = c->next;
|
||||
free(c);
|
||||
} else {
|
||||
if ((*pp)->pool == pool) (*pp)->n = 0;
|
||||
pp = &(*pp)->next;
|
||||
}
|
||||
}
|
||||
ds_pool *p = pool_find(pool);
|
||||
if (p && !destroy) {
|
||||
p->sets_left = p->max_sets;
|
||||
memcpy(p->left, p->size, sizeof(p->left));
|
||||
}
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
}
|
||||
|
||||
/* How many sets of layout l still fit in pool p by the books; UINT32_MAX when not kept. */
|
||||
static uint32_t ds_fits(const ds_pool *p, const ds_layout *l)
|
||||
{
|
||||
if (!p->books || !l || !l->known) return UINT32_MAX;
|
||||
uint32_t n = p->sets_left;
|
||||
for (int t = 0; t < DS_TYPES; t++)
|
||||
if (l->count[t] && p->left[t] / l->count[t] < n) n = p->left[t] / l->count[t];
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Fills the cache for (pool, layout) with `want` sets; returns the driver's result. */
|
||||
static VkResult ds_refill(VkDevice device, ds_pool *p, const ds_layout *l, ds_cache *c, uint32_t want)
|
||||
{
|
||||
VkDescriptorSetLayout layouts[DS_BATCH];
|
||||
for (uint32_t i = 0; i < want; i++) layouts[i] = c->layout;
|
||||
VkDescriptorSetAllocateInfo ai = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, NULL, c->pool, want, layouts};
|
||||
VkResult r = vkb_wire_vkAllocateDescriptorSets(device, &ai, c->sets + c->n);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
c->n += want;
|
||||
if (ds_fits(p, l) != UINT32_MAX) {
|
||||
p->sets_left -= want;
|
||||
for (int t = 0; t < DS_TYPES; t++) p->left[t] -= want * l->count[t];
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the client names itself: with the pool's books kept and every layout known, whether the
|
||||
* allocation fits is known here, so the sets get names (VKB_DS_TAG | n) at once and the server
|
||||
* allocates the driver's sets without a reply (server_dsmap.c). All or nothing, as the spec has
|
||||
* it. VK_ERROR_UNKNOWN: not for this path (a layout the books cannot follow).
|
||||
*/
|
||||
#define VKB_DS_TAG (1ull << 63)
|
||||
#define DS_NAMED_MAX 1024
|
||||
|
||||
static VkResult ds_alloc_named(VkDevice device, ds_pool *p, const VkDescriptorSetAllocateInfo *ai, VkDescriptorSet *out)
|
||||
{
|
||||
static uint64_t next_name;
|
||||
uint32_t n = ai->descriptorSetCount;
|
||||
if (!n || n > DS_NAMED_MAX) return VK_ERROR_UNKNOWN;
|
||||
uint64_t need[DS_TYPES] = {0};
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
const ds_layout *l = layout_find(ai->pSetLayouts[i]);
|
||||
if (!l || !l->known) return VK_ERROR_UNKNOWN;
|
||||
for (int t = 0; t < DS_TYPES; t++) need[t] += l->count[t];
|
||||
}
|
||||
if (n > p->sets_left) return VK_ERROR_OUT_OF_POOL_MEMORY;
|
||||
for (int t = 0; t < DS_TYPES; t++)
|
||||
if (need[t] > p->left[t]) return VK_ERROR_OUT_OF_POOL_MEMORY;
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkbAllocDescSets, dev->obj.table);
|
||||
vkb_enc_u64(&c.e, dev->obj.remote);
|
||||
vkb_enc_bytes(&c.e, &ai->descriptorPool, sizeof(ai->descriptorPool));
|
||||
vkb_enc_u32(&c.e, n);
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
out[i] = (VkDescriptorSet)(VKB_DS_TAG | __atomic_add_fetch(&next_name, 1, __ATOMIC_RELAXED));
|
||||
vkb_enc_bytes(&c.e, &ai->pSetLayouts[i], sizeof(ai->pSetLayouts[i]));
|
||||
vkb_enc_u64(&c.e, (uint64_t)out[i]);
|
||||
}
|
||||
if (!vkb_call_exec_async(&c)) {
|
||||
for (uint32_t i = 0; i < n; i++) out[i] = VK_NULL_HANDLE;
|
||||
return VK_ERROR_DEVICE_LOST;
|
||||
}
|
||||
p->sets_left -= n;
|
||||
for (int t = 0; t < DS_TYPES; t++) p->left[t] -= (uint32_t)need[t];
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *ai, VkDescriptorSet *out)
|
||||
{
|
||||
/* Variable descriptor counts and the like are per allocation: those go straight through. */
|
||||
if (ai->pNext || getenv("VKBRIDGE_SYNC")) return vkb_wire_vkAllocateDescriptorSets(device, ai, out);
|
||||
pthread_mutex_lock(&ds_lock);
|
||||
ds_pool *p = pool_find(ai->descriptorPool);
|
||||
if (!p || p->max_batch <= 1) {
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
return vkb_wire_vkAllocateDescriptorSets(device, ai, out);
|
||||
}
|
||||
if (p->books) {
|
||||
VkResult nr = ds_alloc_named(device, p, ai, out);
|
||||
if (nr != VK_ERROR_UNKNOWN) {
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
return nr;
|
||||
}
|
||||
}
|
||||
VkResult r = VK_SUCCESS;
|
||||
uint32_t done = 0;
|
||||
for (; done < ai->descriptorSetCount; done++) {
|
||||
ds_cache *c = ds_find(ai->descriptorPool, ai->pSetLayouts[done], 1);
|
||||
if (!c) {
|
||||
r = VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
break;
|
||||
}
|
||||
if (c->batch > p->max_batch) c->batch = p->max_batch;
|
||||
if (!c->n) {
|
||||
const ds_layout *l = layout_find(c->layout);
|
||||
uint32_t fits = ds_fits(p, l), want = c->batch < fits ? c->batch : fits;
|
||||
if (!want) {
|
||||
r = VK_ERROR_OUT_OF_POOL_MEMORY;
|
||||
break;
|
||||
}
|
||||
r = ds_refill(device, p, l, c, want);
|
||||
if ((r == VK_ERROR_OUT_OF_POOL_MEMORY || r == VK_ERROR_FRAGMENTED_POOL) && want > 1) {
|
||||
/* The driver disagrees with the batch (or the books): just what was asked for. */
|
||||
c->batch = want / 2 > 4 ? want / 2 : 4;
|
||||
r = ds_refill(device, p, l, c, 1);
|
||||
} else if (r == VK_SUCCESS && c->batch < p->max_batch && want == c->batch) {
|
||||
c->batch = c->batch * 2 > p->max_batch ? p->max_batch : c->batch * 2;
|
||||
}
|
||||
if (r != VK_SUCCESS) break;
|
||||
}
|
||||
out[done] = c->sets[--c->n];
|
||||
}
|
||||
if (r != VK_SUCCESS) {
|
||||
/* None allocated, as the spec has it: what was taken goes back to the cache. */
|
||||
for (uint32_t i = 0; i < done; i++) {
|
||||
ds_cache *c = ds_find(ai->descriptorPool, ai->pSetLayouts[i], 0);
|
||||
if (c && c->n < DS_BATCH) c->sets[c->n++] = out[i];
|
||||
}
|
||||
for (uint32_t i = 0; i < ai->descriptorSetCount; i++) out[i] = VK_NULL_HANDLE;
|
||||
}
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetDescriptorPool(VkDevice device, VkDescriptorPool pool, VkDescriptorPoolResetFlags flags)
|
||||
{
|
||||
ds_drop_pool(pool, 0);
|
||||
return vkb_wire_vkResetDescriptorPool(device, pool, flags);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorPool(VkDevice device, VkDescriptorPool pool, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
if (!pool) return;
|
||||
ds_drop_pool(pool, 1);
|
||||
pthread_mutex_lock(&ds_lock);
|
||||
for (ds_pool **pp = &ds_pools; *pp; pp = &(*pp)->next) {
|
||||
if ((*pp)->pool == pool) {
|
||||
ds_pool *p = *pp;
|
||||
*pp = p->next;
|
||||
free(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&ds_lock);
|
||||
vkb_wire_vkDestroyDescriptorPool(device, pool, pAllocator);
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Client side of feature emulation: the app sees a feature when the GPU has it or the server
|
||||
* emulates it (vkb_server_pd_info.emu), and does not see one the GPU lacks (.missing).
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "emu.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
#include "vkb_emu_formats.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define EMU(pd, f) (((pd)->info.emu & (f)) != 0)
|
||||
#define MISSING(pd, f) (((pd)->info.missing & (f)) != 0 && !EMU(pd, f))
|
||||
|
||||
const VkExtensionProperties *vkb_emu_extensions(vkb_physdev *pd, uint32_t *n)
|
||||
{
|
||||
static __thread VkExtensionProperties list[8];
|
||||
uint32_t c = 0;
|
||||
if (EMU(pd, VKB_EMU_MAINT5)) list[c++] = (VkExtensionProperties){"VK_KHR_maintenance5", 1};
|
||||
if (EMU(pd, VKB_EMU_DIVISOR)) {
|
||||
list[c++] = (VkExtensionProperties){"VK_EXT_vertex_attribute_divisor", 3};
|
||||
list[c++] = (VkExtensionProperties){"VK_KHR_vertex_attribute_divisor", 1};
|
||||
}
|
||||
if (EMU(pd, VKB_EMU_DEPTH_CLIP)) list[c++] = (VkExtensionProperties){"VK_EXT_depth_clip_enable", 1};
|
||||
if (EMU(pd, VKB_EMU_ROBUSTNESS2)) list[c++] = (VkExtensionProperties){"VK_EXT_robustness2", 1};
|
||||
*n = c;
|
||||
return list;
|
||||
}
|
||||
|
||||
void vkb_emu_patch_limits(vkb_physdev *pd, VkPhysicalDeviceProperties *p)
|
||||
{
|
||||
(void)pd;
|
||||
(void)p;
|
||||
}
|
||||
|
||||
void vkb_emu_patch_properties_chain(vkb_physdev *pd, VkBaseOutStructure *b)
|
||||
{
|
||||
if (EMU(pd, VKB_EMU_ROBUSTNESS2) && b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_PROPERTIES_EXT) {
|
||||
VkPhysicalDeviceRobustness2PropertiesEXT *r = (void *)b;
|
||||
r->robustStorageBufferAccessSizeAlignment = 4;
|
||||
r->robustUniformBufferAccessSizeAlignment = 256;
|
||||
}
|
||||
if (EMU(pd, VKB_EMU_DIVISOR)) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT)
|
||||
((VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)b)->maxVertexAttribDivisor = UINT32_MAX;
|
||||
if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES) {
|
||||
VkPhysicalDeviceVertexAttributeDivisorProperties *p = (void *)b;
|
||||
p->maxVertexAttribDivisor = UINT32_MAX;
|
||||
p->supportsNonZeroFirstInstance = VK_TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void set_feature(vkb_physdev *pd, uint32_t flag, VkBool32 *f)
|
||||
{
|
||||
if (EMU(pd, flag)) *f = VK_TRUE;
|
||||
else if (MISSING(pd, flag)) *f = VK_FALSE;
|
||||
}
|
||||
|
||||
void vkb_emu_patch_features(vkb_physdev *pd, VkPhysicalDeviceFeatures *f)
|
||||
{
|
||||
set_feature(pd, VKB_EMU_BCN, &f->textureCompressionBC);
|
||||
set_feature(pd, VKB_EMU_CLIP_DISTANCE, &f->shaderClipDistance);
|
||||
set_feature(pd, VKB_EMU_CULL_DISTANCE, &f->shaderCullDistance);
|
||||
}
|
||||
|
||||
void vkb_emu_patch_features_chain(vkb_physdev *pd, VkBaseOutStructure *b)
|
||||
{
|
||||
switch ((int)b->sType) {
|
||||
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES:
|
||||
if (EMU(pd, VKB_EMU_MAINT5)) ((VkPhysicalDeviceMaintenance5Features *)b)->maintenance5 = VK_TRUE;
|
||||
break;
|
||||
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES: {
|
||||
VkPhysicalDeviceVertexAttributeDivisorFeatures *d = (void *)b;
|
||||
set_feature(pd, VKB_EMU_DIVISOR, &d->vertexAttributeInstanceRateDivisor);
|
||||
set_feature(pd, VKB_EMU_DIVISOR, &d->vertexAttributeInstanceRateZeroDivisor);
|
||||
break;
|
||||
}
|
||||
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_3_FEATURES_EXT:
|
||||
/* Emulated depth clip is pipeline state (clamp = not clip): neither may be dynamic. */
|
||||
if (MISSING(pd, VKB_EMU_DEPTH_CLIP) || EMU(pd, VKB_EMU_DEPTH_CLIP)) {
|
||||
VkPhysicalDeviceExtendedDynamicState3FeaturesEXT *d3 = (void *)b;
|
||||
d3->extendedDynamicState3DepthClipEnable = VK_FALSE;
|
||||
if (EMU(pd, VKB_EMU_DEPTH_CLIP)) d3->extendedDynamicState3DepthClampEnable = VK_FALSE;
|
||||
}
|
||||
break;
|
||||
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT:
|
||||
if (EMU(pd, VKB_EMU_ROBUSTNESS2)) {
|
||||
VkPhysicalDeviceRobustness2FeaturesEXT *r = (void *)b;
|
||||
r->robustBufferAccess2 = r->robustImageAccess2 = r->nullDescriptor = VK_TRUE;
|
||||
}
|
||||
break;
|
||||
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT:
|
||||
set_feature(pd, VKB_EMU_DEPTH_CLIP, &((VkPhysicalDeviceDepthClipEnableFeaturesEXT *)b)->depthClipEnable);
|
||||
break;
|
||||
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_4_FEATURES: {
|
||||
VkPhysicalDeviceVulkan14Features *v = (void *)b;
|
||||
v->hostImageCopy = VK_FALSE;
|
||||
set_feature(pd, VKB_EMU_DIVISOR, &v->vertexAttributeInstanceRateDivisor);
|
||||
set_feature(pd, VKB_EMU_DIVISOR, &v->vertexAttributeInstanceRateZeroDivisor);
|
||||
break;
|
||||
}
|
||||
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_IMAGE_COPY_FEATURES:
|
||||
/* Host image copy hands the driver host pointers: never offered across the bridge. */
|
||||
((VkPhysicalDeviceHostImageCopyFeatures *)b)->hostImageCopy = VK_FALSE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int vkb_emu_is_emulated_format(vkb_physdev *pd, VkFormat format)
|
||||
{
|
||||
return vkb_is_bc(format) && EMU(pd, VKB_EMU_BCN);
|
||||
}
|
||||
|
||||
int vkb_emu_format_properties(vkb_physdev *pd, VkFormat format, VkFormatProperties *fp, VkFormatProperties2 *fp2)
|
||||
{
|
||||
if (!vkb_is_bc(format) || !(pd->info.missing & VKB_EMU_BCN)) return 0;
|
||||
memset(fp, 0, sizeof(*fp));
|
||||
if (EMU(pd, VKB_EMU_BCN)) {
|
||||
/* What the decoded image can do, limited to what a compressed texture is used for. */
|
||||
VkFormatProperties real;
|
||||
vkb_wire_vkGetPhysicalDeviceFormatProperties((VkPhysicalDevice)pd, vkb_bc_view_format(format), &real);
|
||||
fp->optimalTilingFeatures = real.optimalTilingFeatures &
|
||||
(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
|
||||
VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
|
||||
VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT);
|
||||
}
|
||||
if (fp2) {
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)fp2->pNext; b; b = b->pNext) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3) {
|
||||
VkFormatProperties3 *p3 = (void *)b;
|
||||
p3->linearTilingFeatures = 0;
|
||||
p3->optimalTilingFeatures = fp->optimalTilingFeatures;
|
||||
p3->bufferFeatures = 0;
|
||||
} else if (b->sType == VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT) {
|
||||
((VkDrmFormatModifierPropertiesListEXT *)b)->drmFormatModifierCount = 0;
|
||||
} else if (b->sType == VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT) {
|
||||
((VkDrmFormatModifierPropertiesList2EXT *)b)->drmFormatModifierCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
VkFormat vkb_emu_real_format(vkb_physdev *pd, VkFormat format, VkImageUsageFlags *usage, VkImageCreateFlags *flags)
|
||||
{
|
||||
if (!vkb_emu_is_emulated_format(pd, format)) return format;
|
||||
/* As the server creates it (server_emu_bcn.c): decoded format, mutable, extended usage. */
|
||||
*flags &= ~(VkImageCreateFlags)VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT;
|
||||
*flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT;
|
||||
*usage |= VK_IMAGE_USAGE_STORAGE_BIT;
|
||||
return vkb_bc_view_format(format);
|
||||
}
|
||||
|
||||
/* Image creation is rewritten on the server (it knows the decode); nothing to do here. */
|
||||
void vkb_emu_image_create_info(vkb_device *dev, VkImageCreateInfo *ci) { (void)dev; (void)ci; }
|
||||
void vkb_emu_image_created(vkb_device *dev, const VkImageCreateInfo *ci, VkImage image) { (void)dev; (void)ci; (void)image; }
|
||||
void vkb_emu_image_destroyed(vkb_device *dev, VkImage image) { (void)dev; (void)image; }
|
||||
@@ -0,0 +1,24 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Client side of feature emulation: reporting what the server emulates (see server_emu.c).
|
||||
*/
|
||||
#ifndef VKB_CLIENT_EMU_H
|
||||
#define VKB_CLIENT_EMU_H
|
||||
|
||||
#include "vkb_client.h"
|
||||
|
||||
const VkExtensionProperties *vkb_emu_extensions(vkb_physdev *pd, uint32_t *n);
|
||||
void vkb_emu_patch_limits(vkb_physdev *pd, VkPhysicalDeviceProperties *p);
|
||||
void vkb_emu_patch_properties_chain(vkb_physdev *pd, VkBaseOutStructure *b);
|
||||
void vkb_emu_patch_features(vkb_physdev *pd, VkPhysicalDeviceFeatures *f);
|
||||
void vkb_emu_patch_features_chain(vkb_physdev *pd, VkBaseOutStructure *b);
|
||||
/* 1 = handled (format is emulated). fp2 may be NULL. */
|
||||
int vkb_emu_format_properties(vkb_physdev *pd, VkFormat format, VkFormatProperties *fp, VkFormatProperties2 *fp2);
|
||||
/* The format the server really uses for an emulated one (usage/flags adjusted); else format. */
|
||||
VkFormat vkb_emu_real_format(vkb_physdev *pd, VkFormat format, VkImageUsageFlags *usage, VkImageCreateFlags *flags);
|
||||
int vkb_emu_is_emulated_format(vkb_physdev *pd, VkFormat format);
|
||||
void vkb_emu_image_create_info(vkb_device *dev, VkImageCreateInfo *ci);
|
||||
void vkb_emu_image_created(vkb_device *dev, const VkImageCreateInfo *ci, VkImage image);
|
||||
void vkb_emu_image_destroyed(vkb_device *dev, VkImage image);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,593 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Loader interface, instances, physical devices, devices and queues.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_client.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
#include "wsi.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* ------------------------------------------------------------------ proc lookup */
|
||||
|
||||
static int proc_cmp(const void *a, const void *b)
|
||||
{
|
||||
return strcmp(((const vkb_proc_desc *)a)->name, ((const vkb_proc_desc *)b)->name);
|
||||
}
|
||||
|
||||
static vkb_proc_desc *sorted_procs;
|
||||
static pthread_once_t procs_once = PTHREAD_ONCE_INIT;
|
||||
|
||||
static void procs_init(void)
|
||||
{
|
||||
sorted_procs = malloc(vkb_client_proc_count * sizeof(vkb_proc_desc));
|
||||
memcpy(sorted_procs, vkb_client_procs, vkb_client_proc_count * sizeof(vkb_proc_desc));
|
||||
qsort(sorted_procs, vkb_client_proc_count, sizeof(vkb_proc_desc), proc_cmp);
|
||||
}
|
||||
|
||||
static const vkb_proc_desc *find_proc(const char *name)
|
||||
{
|
||||
pthread_once(&procs_once, procs_init);
|
||||
vkb_proc_desc key = {name, NULL, 0, NULL};
|
||||
return bsearch(&key, sorted_procs, vkb_client_proc_count, sizeof(vkb_proc_desc), proc_cmp);
|
||||
}
|
||||
|
||||
int vkb_device_ext_enabled(const vkb_device *d, const char *name)
|
||||
{
|
||||
for (uint32_t i = 0; i < d->enabled_ext_count; i++)
|
||||
if (!strcmp(d->enabled_exts[i], name)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
PFN_vkVoidFunction vkb_lookup_proc(const char *name, int level_max, vkb_device *dev, vkb_instance *inst)
|
||||
{
|
||||
(void)inst;
|
||||
if (!name) return NULL;
|
||||
const vkb_proc_desc *p = find_proc(name);
|
||||
if (!p) return NULL;
|
||||
if (level_max == VKB_LEVEL_DEVICE) {
|
||||
if (p->level != VKB_LEVEL_DEVICE) return NULL;
|
||||
if (dev && p->ext && !vkb_device_ext_enabled(dev, p->ext)) return NULL;
|
||||
/* Core commands newer than the device's Vulkan version do not exist for it. */
|
||||
if (dev && p->core && p->core > dev->api_version) return NULL;
|
||||
}
|
||||
return p->fn;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ loader entry points */
|
||||
|
||||
VKB_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pVersion)
|
||||
{
|
||||
if (*pVersion > 5) *pVersion = 5;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKB_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
|
||||
{
|
||||
if (!pName) return NULL;
|
||||
if (!instance) {
|
||||
const vkb_proc_desc *p = find_proc(pName);
|
||||
if (!p) return NULL;
|
||||
if (p->level == VKB_LEVEL_GLOBAL || !strcmp(pName, "vkGetInstanceProcAddr")) return p->fn;
|
||||
return NULL;
|
||||
}
|
||||
return vkb_lookup_proc(pName, VKB_LEVEL_DEVICE + 1, NULL, vkb_inst(instance));
|
||||
}
|
||||
|
||||
VKB_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetPhysicalDeviceProcAddr(VkInstance instance, const char *pName)
|
||||
{
|
||||
const vkb_proc_desc *p = find_proc(pName);
|
||||
if (!p || p->level != VKB_LEVEL_PHYSDEV) return NULL;
|
||||
(void)instance;
|
||||
return p->fn;
|
||||
}
|
||||
|
||||
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkb_ep_vkGetInstanceProcAddr(VkInstance instance, const char *pName)
|
||||
{
|
||||
return vk_icdGetInstanceProcAddr(instance, pName);
|
||||
}
|
||||
|
||||
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkb_ep_vkGetDeviceProcAddr(VkDevice device, const char *pName)
|
||||
{
|
||||
return vkb_lookup_proc(pName, VKB_LEVEL_DEVICE, vkb_dev(device), NULL);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ wrapping server objects */
|
||||
|
||||
void *vkb_wrap(VkObjectType type, const void *parent, uint64_t remote)
|
||||
{
|
||||
if (!remote) return NULL;
|
||||
switch (type) {
|
||||
case VK_OBJECT_TYPE_PHYSICAL_DEVICE: {
|
||||
vkb_instance *inst = (vkb_instance *)parent;
|
||||
pthread_mutex_lock(&inst->lock);
|
||||
vkb_physdev *pd = inst->pds;
|
||||
while (pd && pd->obj.remote != remote) pd = pd->list_next;
|
||||
if (!pd) {
|
||||
pd = calloc(1, sizeof(*pd));
|
||||
pd->obj.loader.loaderMagic = ICD_LOADER_MAGIC;
|
||||
pd->obj.type = type;
|
||||
pd->obj.remote = remote;
|
||||
pd->obj.table = inst->obj.table;
|
||||
pd->instance = inst;
|
||||
pthread_mutex_init(&pd->lock, NULL);
|
||||
pd->list_next = inst->pds;
|
||||
inst->pds = pd;
|
||||
}
|
||||
pthread_mutex_unlock(&inst->lock);
|
||||
return pd;
|
||||
}
|
||||
case VK_OBJECT_TYPE_QUEUE: {
|
||||
vkb_device *dev = (vkb_device *)parent;
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
vkb_queue *q = dev->queues;
|
||||
while (q && q->obj.remote != remote) q = q->next;
|
||||
if (!q) {
|
||||
q = calloc(1, sizeof(*q));
|
||||
q->obj.loader.loaderMagic = ICD_LOADER_MAGIC;
|
||||
q->obj.type = type;
|
||||
q->obj.remote = remote;
|
||||
q->obj.table = dev->obj.table;
|
||||
q->device = dev;
|
||||
q->next = dev->queues;
|
||||
dev->queues = q;
|
||||
}
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return q;
|
||||
}
|
||||
case VK_OBJECT_TYPE_COMMAND_BUFFER: {
|
||||
vkb_device *dev = (vkb_device *)parent;
|
||||
vkb_cmdbuf *cb = calloc(1, sizeof(*cb));
|
||||
cb->obj.loader.loaderMagic = ICD_LOADER_MAGIC;
|
||||
cb->obj.type = type;
|
||||
cb->obj.remote = remote;
|
||||
cb->obj.table = dev->obj.table;
|
||||
cb->device = dev;
|
||||
vkb_enc_init(&cb->stream);
|
||||
return cb;
|
||||
}
|
||||
default:
|
||||
VKB_ERR("internal: cannot wrap object type %d", type);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ instance */
|
||||
|
||||
static uint32_t server_instance_version(void)
|
||||
{
|
||||
static uint32_t v;
|
||||
if (!v) {
|
||||
uint32_t sv = VK_API_VERSION_1_0;
|
||||
if (vkb_wire_vkEnumerateInstanceVersion(&sv) != VK_SUCCESS) return VK_API_VERSION_1_0;
|
||||
extern uint32_t vkb_max_api(void);
|
||||
v = sv < vkb_max_api() ? sv : vkb_max_api();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEnumerateInstanceVersion(uint32_t *pApiVersion)
|
||||
{
|
||||
if (!vkb_connected()) {
|
||||
*pApiVersion = VK_API_VERSION_1_0;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
*pApiVersion = server_instance_version();
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static const char *const client_instance_exts[] = {
|
||||
"VK_KHR_surface", "VK_KHR_wayland_surface", "VK_KHR_xcb_surface", "VK_KHR_xlib_surface",
|
||||
"VK_EXT_headless_surface", "VK_KHR_get_surface_capabilities2", "VK_EXT_swapchain_colorspace",
|
||||
"VK_EXT_debug_utils",
|
||||
};
|
||||
|
||||
static int bridged_ext(const char *name, int device)
|
||||
{
|
||||
for (int i = 0; i < VKB_EXT_COUNT; i++)
|
||||
if (vkb_exts[i].device == device && !vkb_exts[i].client && !strcmp(vkb_exts[i].name, name)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static VkResult fill_props(const VkExtensionProperties *list, uint32_t n, uint32_t *pCount, VkExtensionProperties *out)
|
||||
{
|
||||
if (!out) {
|
||||
*pCount = n;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
uint32_t c = *pCount < n ? *pCount : n;
|
||||
memcpy(out, list, c * sizeof(*out));
|
||||
*pCount = c;
|
||||
return c < n ? VK_INCOMPLETE : VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEnumerateInstanceExtensionProperties(const char *pLayerName, uint32_t *pPropertyCount,
|
||||
VkExtensionProperties *pProperties)
|
||||
{
|
||||
if (pLayerName) return VK_ERROR_LAYER_NOT_PRESENT;
|
||||
static VkExtensionProperties *list;
|
||||
static uint32_t count;
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (!list) {
|
||||
if (!vkb_connected()) {
|
||||
/* No server: offer nothing, and vkCreateInstance will say the driver is incompatible. */
|
||||
pthread_mutex_unlock(&lock);
|
||||
*pPropertyCount = 0;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
uint32_t n = 0;
|
||||
vkb_wire_vkEnumerateInstanceExtensionProperties(NULL, &n, NULL);
|
||||
VkExtensionProperties *srv = calloc(n + 1, sizeof(*srv));
|
||||
vkb_wire_vkEnumerateInstanceExtensionProperties(NULL, &n, srv);
|
||||
size_t nc = sizeof(client_instance_exts) / sizeof(client_instance_exts[0]);
|
||||
list = calloc(n + nc, sizeof(*list));
|
||||
for (uint32_t i = 0; i < n; i++)
|
||||
if (bridged_ext(srv[i].extensionName, 0)) list[count++] = srv[i];
|
||||
for (size_t i = 0; i < nc; i++) {
|
||||
snprintf(list[count].extensionName, sizeof(list[count].extensionName), "%s", client_instance_exts[i]);
|
||||
list[count].specVersion = 1;
|
||||
if (!strcmp(client_instance_exts[i], "VK_KHR_surface")) list[count].specVersion = 25;
|
||||
if (!strcmp(client_instance_exts[i], "VK_KHR_wayland_surface")) list[count].specVersion = 6;
|
||||
if (!strcmp(client_instance_exts[i], "VK_KHR_xcb_surface")) list[count].specVersion = 6;
|
||||
if (!strcmp(client_instance_exts[i], "VK_KHR_xlib_surface")) list[count].specVersion = 6;
|
||||
if (!strcmp(client_instance_exts[i], "VK_EXT_swapchain_colorspace")) list[count].specVersion = 4;
|
||||
if (!strcmp(client_instance_exts[i], "VK_EXT_debug_utils")) list[count].specVersion = 2;
|
||||
count++;
|
||||
}
|
||||
free(srv);
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return fill_props(list, count, pPropertyCount, pProperties);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEnumerateInstanceLayerProperties(uint32_t *pPropertyCount, VkLayerProperties *pProperties)
|
||||
{
|
||||
(void)pProperties;
|
||||
*pPropertyCount = 0;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static int is_client_instance_ext(const char *n)
|
||||
{
|
||||
for (size_t i = 0; i < sizeof(client_instance_exts) / sizeof(client_instance_exts[0]); i++)
|
||||
if (!strcmp(client_instance_exts[i], n)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateInstance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
|
||||
VkInstance *pInstance)
|
||||
{
|
||||
(void)pAllocator;
|
||||
if (!vkb_connected()) return VK_ERROR_INCOMPATIBLE_DRIVER;
|
||||
vkb_instance *inst = calloc(1, sizeof(*inst));
|
||||
if (!inst) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
inst->obj.loader.loaderMagic = ICD_LOADER_MAGIC;
|
||||
inst->obj.type = VK_OBJECT_TYPE_INSTANCE;
|
||||
pthread_mutex_init(&inst->lock, NULL);
|
||||
|
||||
VkInstanceCreateInfo ci = *pCreateInfo;
|
||||
const char **exts = calloc(ci.enabledExtensionCount + 1, sizeof(char *));
|
||||
uint32_t n = 0;
|
||||
for (uint32_t i = 0; i < ci.enabledExtensionCount; i++) {
|
||||
const char *e = ci.ppEnabledExtensionNames[i];
|
||||
VKB_DBG("instance extension requested: %s", e);
|
||||
if (is_client_instance_ext(e)) {
|
||||
if (!strcmp(e, "VK_KHR_surface")) inst->ext_surface = 1;
|
||||
else if (!strcmp(e, "VK_KHR_wayland_surface")) inst->ext_wayland = 1;
|
||||
else if (!strcmp(e, "VK_KHR_xcb_surface")) inst->ext_xcb = 1;
|
||||
else if (!strcmp(e, "VK_KHR_xlib_surface")) inst->ext_xlib = 1;
|
||||
else if (!strcmp(e, "VK_EXT_headless_surface")) inst->ext_headless = 1;
|
||||
else if (!strcmp(e, "VK_KHR_get_surface_capabilities2")) inst->ext_surface_caps2 = 1;
|
||||
else if (!strcmp(e, "VK_EXT_debug_utils")) inst->ext_debug_utils = 1;
|
||||
continue;
|
||||
}
|
||||
exts[n++] = e;
|
||||
}
|
||||
ci.enabledExtensionCount = n;
|
||||
ci.ppEnabledExtensionNames = exts;
|
||||
ci.enabledLayerCount = 0;
|
||||
ci.ppEnabledLayerNames = NULL;
|
||||
/* The app's version goes through as asked: the device's effective version is the lower of it
|
||||
* and the GPU's, which is what a driver does (and what apps compute for themselves). */
|
||||
if (ci.pApplicationInfo) {
|
||||
inst->api_version = ci.pApplicationInfo->apiVersion ? ci.pApplicationInfo->apiVersion : VK_API_VERSION_1_0;
|
||||
} else {
|
||||
inst->api_version = VK_API_VERSION_1_0;
|
||||
}
|
||||
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkCreateInstance, 0);
|
||||
vkb_enc_u8(&c.e, 1);
|
||||
vkb_enc_VkInstanceCreateInfo(&c.e, &ci);
|
||||
vkb_enc_u8(&c.e, 1);
|
||||
free(exts);
|
||||
if (!vkb_call_exec(&c)) {
|
||||
free(inst);
|
||||
return VK_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
VkResult r = (VkResult)vkb_dec_u32(&c.d);
|
||||
inst->obj.remote = vkb_dec_u64(&c.d);
|
||||
inst->obj.table = vkb_dec_u32(&c.d);
|
||||
vkb_call_end(&c);
|
||||
if (r != VK_SUCCESS) {
|
||||
free(inst);
|
||||
return r;
|
||||
}
|
||||
VKB_INFO("instance created (Vulkan %u.%u)", VK_API_VERSION_MAJOR(inst->api_version), VK_API_VERSION_MINOR(inst->api_version));
|
||||
*pInstance = (VkInstance)inst;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
(void)pAllocator;
|
||||
if (!instance) return;
|
||||
vkb_instance *inst = vkb_inst(instance);
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkDestroyInstance, inst->obj.table);
|
||||
vkb_enc_u64(&c.e, inst->obj.remote);
|
||||
if (vkb_call_exec(&c)) vkb_call_end(&c);
|
||||
vkb_physdev *pd = inst->pds;
|
||||
while (pd) {
|
||||
vkb_physdev *n = pd->list_next;
|
||||
free(pd->exts);
|
||||
pthread_mutex_destroy(&pd->lock);
|
||||
free(pd);
|
||||
pd = n;
|
||||
}
|
||||
pthread_mutex_destroy(&inst->lock);
|
||||
free(inst);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ physical devices */
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEnumeratePhysicalDevices(VkInstance instance, uint32_t *pCount, VkPhysicalDevice *pDevices)
|
||||
{
|
||||
VkResult r = vkb_wire_vkEnumeratePhysicalDevices(instance, pCount, pDevices);
|
||||
if (pDevices && (r == VK_SUCCESS || r == VK_INCOMPLETE)) {
|
||||
for (uint32_t i = 0; i < *pCount; i++) vkb_physdev_ready(vkb_pd(pDevices[i]));
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEnumeratePhysicalDeviceGroups(VkInstance instance, uint32_t *pCount,
|
||||
VkPhysicalDeviceGroupProperties *pGroups)
|
||||
{
|
||||
/* One group per device: what a single-GPU driver reports. */
|
||||
uint32_t n = 0;
|
||||
VkResult r = vkb_ep_vkEnumeratePhysicalDevices(instance, &n, NULL);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
if (!pGroups) {
|
||||
*pCount = n;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
VkPhysicalDevice *pds = calloc(n ? n : 1, sizeof(*pds));
|
||||
r = vkb_ep_vkEnumeratePhysicalDevices(instance, &n, pds);
|
||||
uint32_t c = *pCount < n ? *pCount : n;
|
||||
for (uint32_t i = 0; i < c; i++) {
|
||||
pGroups[i].physicalDeviceCount = 1;
|
||||
memset(pGroups[i].physicalDevices, 0, sizeof(pGroups[i].physicalDevices));
|
||||
pGroups[i].physicalDevices[0] = pds[i];
|
||||
pGroups[i].subsetAllocation = VK_FALSE;
|
||||
}
|
||||
free(pds);
|
||||
*pCount = c;
|
||||
return c < n ? VK_INCOMPLETE : r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char *pLayerName,
|
||||
uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
|
||||
{
|
||||
if (pLayerName) return VK_ERROR_LAYER_NOT_PRESENT;
|
||||
vkb_physdev *pd = vkb_pd(physicalDevice);
|
||||
if (!vkb_physdev_ready(pd)) {
|
||||
*pPropertyCount = 0;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
return fill_props(pd->exts, pd->next, pPropertyCount, pProperties);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t *pPropertyCount,
|
||||
VkLayerProperties *pProperties)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)pProperties;
|
||||
*pPropertyCount = 0;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ device */
|
||||
|
||||
static const char *const client_device_exts[] = {
|
||||
"VK_KHR_swapchain", "VK_KHR_incremental_present", "VK_KHR_swapchain_mutable_format",
|
||||
};
|
||||
|
||||
int vkb_is_client_device_ext(const char *n)
|
||||
{
|
||||
for (size_t i = 0; i < sizeof(client_device_exts) / sizeof(client_device_exts[0]); i++)
|
||||
if (!strcmp(client_device_exts[i], n)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *const *vkb_client_device_exts(uint32_t *n)
|
||||
{
|
||||
*n = sizeof(client_device_exts) / sizeof(client_device_exts[0]);
|
||||
return client_device_exts;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkDevice *pDevice)
|
||||
{
|
||||
(void)pAllocator;
|
||||
vkb_physdev *pd = vkb_pd(physicalDevice);
|
||||
if (!vkb_physdev_ready(pd)) return VK_ERROR_INITIALIZATION_FAILED;
|
||||
vkb_device *dev = calloc(1, sizeof(*dev));
|
||||
if (!dev) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
dev->obj.loader.loaderMagic = ICD_LOADER_MAGIC;
|
||||
dev->obj.type = VK_OBJECT_TYPE_DEVICE;
|
||||
dev->pd = pd;
|
||||
dev->instance = pd->instance;
|
||||
pthread_mutex_init(&dev->lock, NULL);
|
||||
|
||||
VkDeviceCreateInfo ci = *pCreateInfo;
|
||||
const char **server_exts = calloc(ci.enabledExtensionCount + 1, sizeof(char *));
|
||||
dev->enabled_exts = calloc(ci.enabledExtensionCount + 1, sizeof(char *));
|
||||
uint32_t n = 0;
|
||||
for (uint32_t i = 0; i < ci.enabledExtensionCount; i++) {
|
||||
const char *e = ci.ppEnabledExtensionNames[i];
|
||||
VKB_DBG("device extension requested: %s", e);
|
||||
dev->enabled_exts[dev->enabled_ext_count++] = strdup(e);
|
||||
if (!strcmp(e, "VK_KHR_swapchain")) dev->ext_swapchain = 1;
|
||||
if (vkb_is_client_device_ext(e)) continue;
|
||||
server_exts[n++] = e;
|
||||
}
|
||||
ci.enabledExtensionCount = n;
|
||||
ci.ppEnabledExtensionNames = server_exts;
|
||||
ci.enabledLayerCount = 0;
|
||||
ci.ppEnabledLayerNames = NULL;
|
||||
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkCreateDevice, pd->obj.table);
|
||||
vkb_enc_u64(&c.e, pd->obj.remote);
|
||||
vkb_enc_u8(&c.e, 1);
|
||||
vkb_enc_VkDeviceCreateInfo(&c.e, &ci);
|
||||
vkb_enc_u8(&c.e, 1);
|
||||
free(server_exts);
|
||||
VkResult r;
|
||||
if (!vkb_call_exec(&c)) {
|
||||
r = VK_ERROR_INITIALIZATION_FAILED;
|
||||
} else {
|
||||
r = (VkResult)vkb_dec_u32(&c.d);
|
||||
dev->obj.remote = vkb_dec_u64(&c.d);
|
||||
dev->obj.table = vkb_dec_u32(&c.d);
|
||||
vkb_call_end(&c);
|
||||
}
|
||||
if (r != VK_SUCCESS) {
|
||||
for (uint32_t i = 0; i < dev->enabled_ext_count; i++) free((void *)dev->enabled_exts[i]);
|
||||
free(dev->enabled_exts);
|
||||
free(dev);
|
||||
return r;
|
||||
}
|
||||
{
|
||||
extern uint32_t vkb_max_api(void);
|
||||
uint32_t v = pd->props.apiVersion < vkb_max_api() ? pd->props.apiVersion : vkb_max_api();
|
||||
dev->api_version = v < pd->instance->api_version ? v : pd->instance->api_version;
|
||||
}
|
||||
vkb_map_init(&dev->memories);
|
||||
vkb_map_init(&dev->templates);
|
||||
vkb_map_init(&dev->pools);
|
||||
vkb_map_init(&dev->unshared);
|
||||
vkb_map_init(&dev->shared_images);
|
||||
vkb_memory_init_device(dev);
|
||||
VKB_INFO("device created on %s", pd->props.deviceName);
|
||||
*pDevice = (VkDevice)dev;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
(void)pAllocator;
|
||||
if (!device) return;
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkDestroyDevice, dev->obj.table);
|
||||
vkb_enc_u64(&c.e, dev->obj.remote);
|
||||
if (vkb_call_exec(&c)) vkb_call_end(&c);
|
||||
vkb_queue *q = dev->queues;
|
||||
while (q) {
|
||||
vkb_queue *n = q->next;
|
||||
free(q);
|
||||
q = n;
|
||||
}
|
||||
/* Mapped memory the app never freed. */
|
||||
for (uint32_t i = 0; i < dev->memories.cap; i++) {
|
||||
if (dev->memories.keys[i] && dev->memories.keys[i] != ~(uint64_t)0) {
|
||||
vkb_memory *m = dev->memories.vals[i];
|
||||
extern void vkb_memory_unmap_free(vkb_memory * m);
|
||||
vkb_memory_unmap_free(m);
|
||||
}
|
||||
}
|
||||
for (uint32_t i = 0; i < dev->pools.cap; i++) {
|
||||
if (dev->pools.keys[i] && dev->pools.keys[i] != ~(uint64_t)0) {
|
||||
extern void vkb_cmdpool_free(vkb_cmdpool * p);
|
||||
vkb_cmdpool_free(dev->pools.vals[i]);
|
||||
}
|
||||
}
|
||||
for (uint32_t i = 0; i < dev->templates.cap; i++)
|
||||
if (dev->templates.keys[i] && dev->templates.keys[i] != ~(uint64_t)0) free(dev->templates.vals[i]);
|
||||
vkb_map_destroy(&dev->memories);
|
||||
vkb_map_destroy(&dev->templates);
|
||||
vkb_map_destroy(&dev->pools);
|
||||
vkb_map_destroy(&dev->unshared);
|
||||
vkb_map_destroy(&dev->shared_images);
|
||||
for (uint32_t i = 0; i < dev->enabled_ext_count; i++) free((void *)dev->enabled_exts[i]);
|
||||
free(dev->enabled_exts);
|
||||
pthread_mutex_destroy(&dev->lock);
|
||||
free(dev);
|
||||
}
|
||||
|
||||
static void queue_set_info(VkQueue q, uint32_t family, uint32_t index)
|
||||
{
|
||||
if (!q) return;
|
||||
vkb_queue *vq = (vkb_queue *)q;
|
||||
vq->family = family;
|
||||
vq->index = index;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue *pQueue)
|
||||
{
|
||||
vkb_wire_vkGetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
|
||||
queue_set_info(*pQueue, queueFamilyIndex, queueIndex);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *pQueueInfo, VkQueue *pQueue)
|
||||
{
|
||||
vkb_wire_vkGetDeviceQueue2(device, pQueueInfo, pQueue);
|
||||
queue_set_info(*pQueue, pQueueInfo->queueFamilyIndex, pQueueInfo->queueIndex);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ queues and sync */
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence)
|
||||
{
|
||||
return vkb_wire_vkQueueSubmit(queue, submitCount, pSubmits, fence);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSubmitInfo2 *pSubmits, VkFence fence)
|
||||
{
|
||||
return vkb_wire_vkQueueSubmit2(queue, submitCount, pSubmits, fence);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueueWaitIdle(VkQueue queue)
|
||||
{
|
||||
return vkb_wire_vkQueueWaitIdle(queue);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkDeviceWaitIdle(VkDevice device)
|
||||
{
|
||||
return vkb_wire_vkDeviceWaitIdle(device);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkWaitForFences(VkDevice device, uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll,
|
||||
uint64_t timeout)
|
||||
{
|
||||
return vkb_wire_vkWaitForFences(device, fenceCount, pFences, waitAll, timeout);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetFenceStatus(VkDevice device, VkFence fence)
|
||||
{
|
||||
return vkb_wire_vkGetFenceStatus(device, fence);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkWaitSemaphores(VkDevice device, const VkSemaphoreWaitInfo *pWaitInfo, uint64_t timeout)
|
||||
{
|
||||
return vkb_wire_vkWaitSemaphores(device, pWaitInfo, timeout);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetSemaphoreCounterValue(VkDevice device, VkSemaphore semaphore, uint64_t *pValue)
|
||||
{
|
||||
return vkb_wire_vkGetSemaphoreCounterValue(device, semaphore, pValue);
|
||||
}
|
||||
@@ -0,0 +1,433 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Device memory on the client side: the memory-type translation of physdev.c applied to every
|
||||
* allocation and requirement, and mapping of shared allocations (server_memory.c explains how
|
||||
* they are shared).
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_client.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
#include "emu.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void vkb_memory_init_device(vkb_device *d)
|
||||
{
|
||||
(void)d;
|
||||
}
|
||||
|
||||
void vkb_memory_unmap_free(vkb_memory *m)
|
||||
{
|
||||
if (m->ptr) munmap(m->ptr, (size_t)m->size);
|
||||
free(m);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ resources */
|
||||
|
||||
/* Shallow copy of a pNext chain (known structures only), in one malloc'd block (*mem). */
|
||||
void *vkb_chain_clone(const void *pNext, void **mem)
|
||||
{
|
||||
size_t total = 0;
|
||||
for (const VkBaseInStructure *b = pNext; b; b = b->pNext) total += (vkb_struct_size(b->sType) + 15) & ~(size_t)15;
|
||||
uint8_t *block = calloc(1, total ? total : 16);
|
||||
*mem = block;
|
||||
VkBaseOutStructure *head = NULL, *prev = NULL;
|
||||
size_t off = 0;
|
||||
for (const VkBaseInStructure *b = pNext; b; b = b->pNext) {
|
||||
size_t sz = vkb_struct_size(b->sType);
|
||||
if (!sz) continue;
|
||||
VkBaseOutStructure *n = (VkBaseOutStructure *)(block + off);
|
||||
memcpy(n, b, sz);
|
||||
n->pNext = NULL;
|
||||
off += (sz + 15) & ~(size_t)15;
|
||||
if (prev) prev->pNext = n;
|
||||
else head = n;
|
||||
prev = n;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
static int can_share(vkb_device *dev)
|
||||
{
|
||||
return dev->pd->info.strategy != VKB_MEM_NONE && dev->pd->info.buffer_handle_types;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer)
|
||||
{
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
if (!can_share(dev) || (pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)) {
|
||||
VkResult r = vkb_wire_vkCreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
|
||||
if (r == VK_SUCCESS) vkb_map_put(&dev->unshared, (uint64_t)*pBuffer, (void *)1);
|
||||
return r;
|
||||
}
|
||||
/* Every buffer may be bound to shared memory: say so at creation, as the spec requires. */
|
||||
VkBufferCreateInfo ci = *pCreateInfo;
|
||||
VkExternalMemoryBufferCreateInfo ext = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO, ci.pNext,
|
||||
dev->pd->info.buffer_handle_types};
|
||||
int had = 0;
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)ci.pNext; b; b = b->pNext) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO) had = 1;
|
||||
}
|
||||
VkResult r;
|
||||
if (had) {
|
||||
/* The app has its own external handle types: ours are added to a copy of its chain. */
|
||||
void *mem = NULL;
|
||||
ci.pNext = vkb_chain_clone(ci.pNext, &mem);
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)ci.pNext; b; b = b->pNext)
|
||||
if (b->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO)
|
||||
((VkExternalMemoryBufferCreateInfo *)b)->handleTypes |= dev->pd->info.buffer_handle_types;
|
||||
r = vkb_wire_vkCreateBuffer(device, &ci, pAllocator, pBuffer);
|
||||
free(mem);
|
||||
if (r != VK_SUCCESS && r != VK_ERROR_OUT_OF_HOST_MEMORY && r != VK_ERROR_OUT_OF_DEVICE_MEMORY) {
|
||||
r = vkb_wire_vkCreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
|
||||
if (r == VK_SUCCESS) vkb_map_put(&dev->unshared, (uint64_t)*pBuffer, (void *)1);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
ci.pNext = &ext;
|
||||
r = vkb_wire_vkCreateBuffer(device, &ci, pAllocator, pBuffer);
|
||||
if (r != VK_SUCCESS && r != VK_ERROR_OUT_OF_HOST_MEMORY && r != VK_ERROR_OUT_OF_DEVICE_MEMORY) {
|
||||
VKB_ONCE(VKB_LOG_WARN, "a buffer cannot be created shareable (%d); it gets device-only memory (logged once)", r);
|
||||
r = vkb_wire_vkCreateBuffer(device, pCreateInfo, pAllocator, pBuffer);
|
||||
if (r == VK_SUCCESS) vkb_map_put(&dev->unshared, (uint64_t)*pBuffer, (void *)1);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
if (!buffer) return;
|
||||
vkb_map_remove(&vkb_dev(device)->unshared, (uint64_t)buffer);
|
||||
vkb_wire_vkDestroyBuffer(device, buffer, pAllocator);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkImage *pImage)
|
||||
{
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
VkImageCreateInfo ci = *pCreateInfo;
|
||||
vkb_emu_image_create_info(dev, &ci);
|
||||
/* Linear images may be mapped by the app: those get the shared handle type when allowed. */
|
||||
if (can_share(dev) && ci.tiling == VK_IMAGE_TILING_LINEAR && !(ci.flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)) {
|
||||
int had = 0;
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)ci.pNext; b; b = b->pNext)
|
||||
had |= b->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;
|
||||
if (!had) {
|
||||
VkExternalMemoryImageCreateInfo ext = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, ci.pNext,
|
||||
dev->pd->info.buffer_handle_types};
|
||||
VkImageCreateInfo c2 = ci;
|
||||
c2.pNext = &ext;
|
||||
VkResult r = vkb_wire_vkCreateImage(device, &c2, pAllocator, pImage);
|
||||
if (r == VK_SUCCESS) {
|
||||
vkb_map_put(&dev->shared_images, (uint64_t)*pImage, (void *)1);
|
||||
vkb_emu_image_created(dev, pCreateInfo, *pImage);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
VkResult r = vkb_wire_vkCreateImage(device, &ci, pAllocator, pImage);
|
||||
if (r == VK_SUCCESS) vkb_emu_image_created(dev, pCreateInfo, *pImage);
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
if (!image) return;
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_map_remove(&dev->shared_images, (uint64_t)image);
|
||||
vkb_emu_image_destroyed(dev, image);
|
||||
vkb_wire_vkDestroyImage(device, image, pAllocator);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ requirements */
|
||||
|
||||
static void fix_reqs(vkb_device *dev, VkMemoryRequirements *r, int allow_shared)
|
||||
{
|
||||
r->memoryTypeBits = vkb_mem_bits_to_client(dev->pd, r->memoryTypeBits, allow_shared);
|
||||
}
|
||||
|
||||
static int buffer_shareable(vkb_device *dev, VkBuffer b)
|
||||
{
|
||||
return can_share(dev) && !vkb_map_get(&dev->unshared, (uint64_t)b);
|
||||
}
|
||||
|
||||
static int image_shareable(vkb_device *dev, VkImage i)
|
||||
{
|
||||
return vkb_map_get(&dev->shared_images, (uint64_t)i) != NULL;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, VkMemoryRequirements *pReq)
|
||||
{
|
||||
vkb_wire_vkGetBufferMemoryRequirements(device, buffer, pReq);
|
||||
fix_reqs(vkb_dev(device), pReq, buffer_shareable(vkb_dev(device), buffer));
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetBufferMemoryRequirements2(VkDevice device, const VkBufferMemoryRequirementsInfo2 *pInfo,
|
||||
VkMemoryRequirements2 *pReq)
|
||||
{
|
||||
vkb_wire_vkGetBufferMemoryRequirements2(device, pInfo, pReq);
|
||||
fix_reqs(vkb_dev(device), &pReq->memoryRequirements, buffer_shareable(vkb_dev(device), pInfo->buffer));
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetImageMemoryRequirements(VkDevice device, VkImage image, VkMemoryRequirements *pReq)
|
||||
{
|
||||
vkb_wire_vkGetImageMemoryRequirements(device, image, pReq);
|
||||
fix_reqs(vkb_dev(device), pReq, image_shareable(vkb_dev(device), image));
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetImageMemoryRequirements2(VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
|
||||
VkMemoryRequirements2 *pReq)
|
||||
{
|
||||
vkb_wire_vkGetImageMemoryRequirements2(device, pInfo, pReq);
|
||||
fix_reqs(vkb_dev(device), &pReq->memoryRequirements, image_shareable(vkb_dev(device), pInfo->image));
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetDeviceBufferMemoryRequirements(VkDevice device, const VkDeviceBufferMemoryRequirements *pInfo,
|
||||
VkMemoryRequirements2 *pReq)
|
||||
{
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
if (!can_share(dev)) {
|
||||
vkb_wire_vkGetDeviceBufferMemoryRequirements(device, pInfo, pReq);
|
||||
fix_reqs(dev, &pReq->memoryRequirements, 0);
|
||||
return;
|
||||
}
|
||||
VkBufferCreateInfo ci = *pInfo->pCreateInfo;
|
||||
VkExternalMemoryBufferCreateInfo ext = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO, ci.pNext,
|
||||
dev->pd->info.buffer_handle_types};
|
||||
int had = 0;
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)ci.pNext; b; b = b->pNext)
|
||||
had |= b->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO;
|
||||
if (!had) ci.pNext = &ext;
|
||||
VkDeviceBufferMemoryRequirements info = *pInfo;
|
||||
info.pCreateInfo = &ci;
|
||||
vkb_wire_vkGetDeviceBufferMemoryRequirements(device, &info, pReq);
|
||||
fix_reqs(dev, &pReq->memoryRequirements, 1);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetDeviceImageMemoryRequirements(VkDevice device, const VkDeviceImageMemoryRequirements *pInfo,
|
||||
VkMemoryRequirements2 *pReq)
|
||||
{
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
VkImageCreateInfo ci = *pInfo->pCreateInfo;
|
||||
vkb_emu_image_create_info(dev, &ci);
|
||||
VkDeviceImageMemoryRequirements info = *pInfo;
|
||||
info.pCreateInfo = &ci;
|
||||
vkb_wire_vkGetDeviceImageMemoryRequirements(device, &info, pReq);
|
||||
fix_reqs(dev, &pReq->memoryRequirements, 0);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetImageSparseMemoryRequirements(VkDevice device, VkImage image, uint32_t *pCount,
|
||||
VkSparseImageMemoryRequirements *pReq)
|
||||
{
|
||||
vkb_wire_vkGetImageSparseMemoryRequirements(device, image, pCount, pReq);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetImageSparseMemoryRequirements2(VkDevice device, const VkImageSparseMemoryRequirementsInfo2 *pInfo,
|
||||
uint32_t *pCount, VkSparseImageMemoryRequirements2 *pReq)
|
||||
{
|
||||
vkb_wire_vkGetImageSparseMemoryRequirements2(device, pInfo, pCount, pReq);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetDeviceImageSparseMemoryRequirements(VkDevice device, const VkDeviceImageMemoryRequirements *pInfo,
|
||||
uint32_t *pCount, VkSparseImageMemoryRequirements2 *pReq)
|
||||
{
|
||||
vkb_wire_vkGetDeviceImageSparseMemoryRequirements(device, pInfo, pCount, pReq);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetMemoryFdPropertiesKHR(VkDevice device, VkExternalMemoryHandleTypeFlagBits handleType, int fd,
|
||||
VkMemoryFdPropertiesKHR *pProps)
|
||||
{
|
||||
VkResult r = vkb_wire_vkGetMemoryFdPropertiesKHR(device, handleType, fd, pProps);
|
||||
if (r == VK_SUCCESS) pProps->memoryTypeBits = vkb_mem_bits_to_client(vkb_dev(device)->pd, pProps->memoryTypeBits, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize offset)
|
||||
{
|
||||
return vkb_wire_vkBindBufferMemory(device, buffer, memory, offset);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBindBufferMemory2(VkDevice device, uint32_t n, const VkBindBufferMemoryInfo *p)
|
||||
{
|
||||
return vkb_wire_vkBindBufferMemory2(device, n, p);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory, VkDeviceSize offset)
|
||||
{
|
||||
return vkb_wire_vkBindImageMemory(device, image, memory, offset);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBindImageMemory2(VkDevice device, uint32_t n, const VkBindImageMemoryInfo *p)
|
||||
{
|
||||
return vkb_wire_vkBindImageMemory2(device, n, p);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetImageSubresourceLayout(VkDevice device, VkImage image, const VkImageSubresource *pSubresource,
|
||||
VkSubresourceLayout *pLayout)
|
||||
{
|
||||
vkb_wire_vkGetImageSubresourceLayout(device, image, pSubresource, pLayout);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ allocation and mapping */
|
||||
|
||||
static void close_import_fds(const void *pNext)
|
||||
{
|
||||
for (const VkBaseInStructure *b = pNext; b; b = b->pNext) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR) {
|
||||
int fd = ((const VkImportMemoryFdInfoKHR *)b)->fd;
|
||||
if (fd >= 0) close(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory)
|
||||
{
|
||||
(void)pAllocator;
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_physdev *pd = dev->pd;
|
||||
uint32_t ct = pAllocateInfo->memoryTypeIndex;
|
||||
if (ct >= pd->client_mem.memoryTypeCount) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
VkMemoryAllocateInfo ai = *pAllocateInfo;
|
||||
ai.memoryTypeIndex = pd->type_map[ct].server_type;
|
||||
int shared = pd->type_map[ct].shared;
|
||||
/* An app-imported dma-buf/opaque fd keeps its own memory; it is not one of our shared pages. */
|
||||
for (const VkBaseInStructure *b = ai.pNext; b; b = b->pNext)
|
||||
if (b->sType == VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR) shared = 0;
|
||||
uint32_t strategy = pd->info.strategy;
|
||||
int memfd = -1;
|
||||
void *ptr = NULL;
|
||||
if (shared && strategy == VKB_MEM_HOSTPTR) {
|
||||
VkDeviceSize a = pd->info.import_alignment ? pd->info.import_alignment : 4096;
|
||||
ai.allocationSize = (ai.allocationSize + a - 1) & ~(a - 1);
|
||||
memfd = vkb_memfd("vkbridge-memory", (size_t)ai.allocationSize);
|
||||
if (memfd < 0) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
ptr = mmap(NULL, (size_t)ai.allocationSize, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, 0);
|
||||
if (ptr == MAP_FAILED) {
|
||||
close(memfd);
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
}
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkAllocateMemory, dev->obj.table);
|
||||
vkb_enc_u64(&c.e, dev->obj.remote);
|
||||
vkb_enc_u8(&c.e, 1);
|
||||
vkb_enc_VkMemoryAllocateInfo(&c.e, &ai);
|
||||
vkb_enc_u32(&c.e, shared ? VKB_ALLOC_SHARED : VKB_ALLOC_PLAIN);
|
||||
vkb_enc_fd(&c.e, memfd);
|
||||
int ok = vkb_call_exec(&c);
|
||||
if (memfd >= 0) close(memfd);
|
||||
if (!ok) {
|
||||
if (ptr) munmap(ptr, (size_t)ai.allocationSize);
|
||||
return VK_ERROR_DEVICE_LOST;
|
||||
}
|
||||
VkResult r = (VkResult)vkb_dec_u32(&c.d);
|
||||
VkDeviceMemory mem = (VkDeviceMemory)vkb_dec_u64(&c.d);
|
||||
int fd = vkb_dec_fd(&c.d);
|
||||
uint64_t size = vkb_dec_u64(&c.d);
|
||||
vkb_call_end(&c);
|
||||
if (r != VK_SUCCESS) {
|
||||
if (ptr) munmap(ptr, (size_t)ai.allocationSize);
|
||||
if (fd >= 0) close(fd);
|
||||
return r;
|
||||
}
|
||||
/* The import succeeded: the fd the app handed over now belongs to the implementation, and the
|
||||
* server holds its own copy. */
|
||||
close_import_fds(pAllocateInfo->pNext);
|
||||
vkb_memory *m = calloc(1, sizeof(*m));
|
||||
m->client_type = ct;
|
||||
m->shared = shared;
|
||||
m->size = shared && strategy == VKB_MEM_HOSTPTR ? ai.allocationSize : size;
|
||||
if (shared && strategy != VKB_MEM_HOSTPTR) {
|
||||
if (fd < 0) {
|
||||
VKB_ERR("shared allocation came back without its dma-buf");
|
||||
} else {
|
||||
ptr = mmap(NULL, (size_t)m->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (ptr == MAP_FAILED) {
|
||||
VKB_ERR("cannot map shared memory (%llu bytes): %s", (unsigned long long)m->size, strerror(errno));
|
||||
ptr = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fd >= 0) close(fd);
|
||||
m->ptr = ptr;
|
||||
vkb_map_put(&dev->memories, (uint64_t)mem, m);
|
||||
*pMemory = mem;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
(void)pAllocator;
|
||||
if (!memory) return;
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_memory *m = vkb_map_remove(&dev->memories, (uint64_t)memory);
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkFreeMemory, dev->obj.table);
|
||||
vkb_enc_u64(&c.e, dev->obj.remote);
|
||||
vkb_enc_u64(&c.e, (uint64_t)memory);
|
||||
vkb_call_exec_async(&c);
|
||||
if (m) vkb_memory_unmap_free(m);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkMapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size,
|
||||
VkMemoryMapFlags flags, void **ppData)
|
||||
{
|
||||
(void)size;
|
||||
(void)flags;
|
||||
vkb_memory *m = vkb_map_get(&vkb_dev(device)->memories, (uint64_t)memory);
|
||||
if (!m || !m->ptr) {
|
||||
VKB_ONCE(VKB_LOG_WARN, "vkMapMemory on memory that is not host-visible through the bridge (logged once)");
|
||||
*ppData = NULL;
|
||||
return VK_ERROR_MEMORY_MAP_FAILED;
|
||||
}
|
||||
*ppData = (uint8_t *)m->ptr + offset;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkUnmapMemory(VkDevice device, VkDeviceMemory memory)
|
||||
{
|
||||
/* The mapping stays until vkFreeMemory: mapping again is free, and nothing else reuses it. */
|
||||
(void)device;
|
||||
(void)memory;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkMapMemory2(VkDevice device, const VkMemoryMapInfo *pInfo, void **ppData)
|
||||
{
|
||||
return vkb_ep_vkMapMemory(device, pInfo->memory, pInfo->offset, pInfo->size, pInfo->flags, ppData);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkUnmapMemory2(VkDevice device, const VkMemoryUnmapInfo *pInfo)
|
||||
{
|
||||
(void)device;
|
||||
(void)pInfo;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VkResult ranges_call(VkDevice device, uint32_t cmd, uint32_t n, const VkMappedMemoryRange *r)
|
||||
{
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, cmd, dev->obj.table);
|
||||
vkb_enc_u64(&c.e, dev->obj.remote);
|
||||
vkb_enc_u32(&c.e, n);
|
||||
for (uint32_t i = 0; i < n; i++) vkb_enc_VkMappedMemoryRange(&c.e, &r[i]);
|
||||
if (!vkb_call_exec(&c)) return VK_ERROR_DEVICE_LOST;
|
||||
VkResult res = (VkResult)vkb_dec_u32(&c.d);
|
||||
vkb_call_end(&c);
|
||||
return res;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkFlushMappedMemoryRanges(VkDevice device, uint32_t n, const VkMappedMemoryRange *r)
|
||||
{
|
||||
return ranges_call(device, VKB_CMD_vkFlushMappedMemoryRanges, n, r);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkInvalidateMappedMemoryRanges(VkDevice device, uint32_t n, const VkMappedMemoryRange *r)
|
||||
{
|
||||
return ranges_call(device, VKB_CMD_vkInvalidateMappedMemoryRanges, n, r);
|
||||
}
|
||||
@@ -0,0 +1,435 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Physical device queries: what the app sees is the server's GPU, with the bridge's memory
|
||||
* model, its extension list and the features the server emulates.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_client.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
#include "emu.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *const *vkb_client_device_exts(uint32_t *n);
|
||||
|
||||
/* ------------------------------------------------------------------ memory model */
|
||||
|
||||
/*
|
||||
* Images and GPU-only buffers live in memory the client never maps; host-visible memory must be
|
||||
* shared pages (server_memory.c), which only buffers (and the rare linear image) can use. So the
|
||||
* app gets a discrete-GPU-like view: device-local types without host access, and host-visible
|
||||
* types (shared) that only resources able to use them list in their memoryTypeBits.
|
||||
*/
|
||||
static void build_memory_model(vkb_physdev *pd)
|
||||
{
|
||||
const VkPhysicalDeviceMemoryProperties *s = &pd->server_mem;
|
||||
VkPhysicalDeviceMemoryProperties *c = &pd->client_mem;
|
||||
memset(c, 0, sizeof(*c));
|
||||
uint32_t share = pd->info.strategy != VKB_MEM_NONE ? pd->info.shareable_types : 0;
|
||||
int pure_dl = -1;
|
||||
for (uint32_t t = 0; t < s->memoryTypeCount; t++) {
|
||||
VkMemoryPropertyFlags f = s->memoryTypes[t].propertyFlags;
|
||||
if ((f & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) && !(f & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) &&
|
||||
!(f & VK_MEMORY_PROPERTY_PROTECTED_BIT)) {
|
||||
pure_dl = (int)t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int uma = pure_dl < 0;
|
||||
/* Heaps: the server's, plus (UMA) a host heap for shared types so they are not "device local". */
|
||||
for (uint32_t h = 0; h < s->memoryHeapCount; h++) {
|
||||
c->memoryHeaps[h] = s->memoryHeaps[h];
|
||||
pd->heap_map[h] = h;
|
||||
}
|
||||
c->memoryHeapCount = s->memoryHeapCount;
|
||||
uint32_t host_heap = UINT32_MAX;
|
||||
|
||||
struct cand {
|
||||
VkMemoryPropertyFlags flags;
|
||||
uint32_t heap, server;
|
||||
uint8_t shared;
|
||||
} cands[VK_MAX_MEMORY_TYPES * 2];
|
||||
uint32_t nc = 0;
|
||||
if (uma) {
|
||||
/* A device-local type with no host access, backed by the first device-local type. */
|
||||
for (uint32_t t = 0; t < s->memoryTypeCount; t++) {
|
||||
VkMemoryPropertyFlags f = s->memoryTypes[t].propertyFlags;
|
||||
if ((f & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) && !(f & VK_MEMORY_PROPERTY_PROTECTED_BIT)) {
|
||||
cands[nc++] = (struct cand){VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, s->memoryTypes[t].heapIndex, t, 0};
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (uint32_t t = 0; t < s->memoryTypeCount && nc < VK_MAX_MEMORY_TYPES; t++) {
|
||||
VkMemoryPropertyFlags f = s->memoryTypes[t].propertyFlags;
|
||||
uint32_t heap = s->memoryTypes[t].heapIndex;
|
||||
if (!(f & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) {
|
||||
cands[nc++] = (struct cand){f, heap, t, 0};
|
||||
continue;
|
||||
}
|
||||
if (!(share & (1u << t))) continue; /* host-visible but not shareable: not offered */
|
||||
VkMemoryPropertyFlags cf = f;
|
||||
if (!(pd->info.coherent_types & (1u << t))) cf &= ~VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||
else cf |= VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; /* coherent in practice (the self-test says so) */
|
||||
/* dma-heap pages come from the uncached heap: not what an app asking for cached reads wants. */
|
||||
if (pd->info.strategy == VKB_MEM_DMAHEAP) cf &= ~VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
|
||||
if (uma) {
|
||||
cf &= ~VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
||||
if (host_heap == UINT32_MAX && c->memoryHeapCount < VK_MAX_MEMORY_HEAPS) {
|
||||
host_heap = c->memoryHeapCount++;
|
||||
c->memoryHeaps[host_heap] = s->memoryHeaps[heap];
|
||||
c->memoryHeaps[host_heap].flags &= ~VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
|
||||
pd->heap_map[host_heap] = heap;
|
||||
}
|
||||
if (host_heap != UINT32_MAX) heap = host_heap;
|
||||
}
|
||||
/* Two client types with identical flags on one heap are pointless: keep the first. */
|
||||
int dup = 0;
|
||||
for (uint32_t i = 0; i < nc; i++) dup |= cands[i].flags == cf && cands[i].heap == heap;
|
||||
if (!dup) cands[nc++] = (struct cand){cf, heap, t, 1};
|
||||
}
|
||||
/* Order: fewer property bits first, which satisfies the spec's subset ordering. */
|
||||
for (uint32_t i = 1; i < nc; i++) {
|
||||
struct cand x = cands[i];
|
||||
uint32_t j = i;
|
||||
while (j > 0 && __builtin_popcount(cands[j - 1].flags) > __builtin_popcount(x.flags)) {
|
||||
cands[j] = cands[j - 1];
|
||||
j--;
|
||||
}
|
||||
cands[j] = x;
|
||||
}
|
||||
if (nc > VK_MAX_MEMORY_TYPES) nc = VK_MAX_MEMORY_TYPES;
|
||||
c->memoryTypeCount = nc;
|
||||
for (uint32_t i = 0; i < nc; i++) {
|
||||
c->memoryTypes[i].propertyFlags = cands[i].flags;
|
||||
c->memoryTypes[i].heapIndex = cands[i].heap;
|
||||
pd->type_map[i].server_type = cands[i].server;
|
||||
pd->type_map[i].shared = cands[i].shared;
|
||||
}
|
||||
for (uint32_t i = 0; i < nc; i++)
|
||||
VKB_DBG("memory type %u: flags 0x%x heap %u -> server type %u%s", i, c->memoryTypes[i].propertyFlags,
|
||||
c->memoryTypes[i].heapIndex, pd->type_map[i].server_type, pd->type_map[i].shared ? " (shared)" : "");
|
||||
}
|
||||
|
||||
uint32_t vkb_mem_bits_to_client(vkb_physdev *pd, uint32_t server_bits, int allow_shared)
|
||||
{
|
||||
uint32_t bits = 0;
|
||||
for (uint32_t i = 0; i < pd->client_mem.memoryTypeCount; i++) {
|
||||
if (!(server_bits & (1u << pd->type_map[i].server_type))) continue;
|
||||
if (pd->type_map[i].shared && !allow_shared) continue;
|
||||
bits |= 1u << i;
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ setup */
|
||||
|
||||
/* The render node the session presents for this GPU (VKBRIDGE_DRM_RENDER="major:minor"). */
|
||||
static int drm_node(int64_t *major, int64_t *minor)
|
||||
{
|
||||
const char *v = getenv("VKBRIDGE_DRM_RENDER");
|
||||
long long a, b;
|
||||
if (!v || sscanf(v, "%lld:%lld", &a, &b) != 2) return 0;
|
||||
if (major) *major = a;
|
||||
if (minor) *minor = b;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* VKBRIDGE_HIDE_EXTS=a,b,c: pretend the GPU lacks these (testing; imitating another driver). */
|
||||
static int hidden_ext(const char *name)
|
||||
{
|
||||
const char *h = getenv("VKBRIDGE_HIDE_EXTS");
|
||||
if (!h) return 0;
|
||||
size_t n = strlen(name);
|
||||
for (const char *p = h; (p = strstr(p, name)); p += n)
|
||||
if ((p == h || p[-1] == ',') && (p[n] == ',' || p[n] == 0)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bridged_device_ext_for(vkb_physdev *pd, const char *name)
|
||||
{
|
||||
/* Clip distance emulation rewrites the vertex and fragment shaders together, at pipeline
|
||||
* creation: pipeline libraries and shader identifiers would split them. */
|
||||
if ((pd->info.emu & VKB_EMU_CLIP_DISTANCE) &&
|
||||
(!strcmp(name, "VK_EXT_graphics_pipeline_library") || !strcmp(name, "VK_EXT_shader_module_identifier")))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int bridged_device_ext(const char *name)
|
||||
{
|
||||
if (hidden_ext(name)) return 0;
|
||||
/* The server GPU's DRM node means nothing on this side: the session's stand-in node is
|
||||
* reported instead (drm_node), or the extension is hidden. VKBRIDGE_PASS_DRM=1 passes the
|
||||
* server's through (host tests, where both sides share one machine). */
|
||||
if (!strcmp(name, "VK_EXT_physical_device_drm")) return getenv("VKBRIDGE_PASS_DRM") && !getenv("VKBRIDGE_DRM_RENDER");
|
||||
for (int i = 0; i < VKB_EXT_COUNT; i++)
|
||||
if (vkb_exts[i].device && !vkb_exts[i].client && !strcmp(vkb_exts[i].name, name)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vkb_physdev_ready(vkb_physdev *pd)
|
||||
{
|
||||
if (pd->ready) return pd->ready > 0;
|
||||
pthread_mutex_lock(&pd->lock);
|
||||
if (pd->ready) {
|
||||
pthread_mutex_unlock(&pd->lock);
|
||||
return pd->ready > 0;
|
||||
}
|
||||
vkb_call c;
|
||||
vkb_call_begin(&c, VKB_CMD_vkbQueryServer, pd->obj.table);
|
||||
vkb_enc_u64(&c.e, pd->obj.remote);
|
||||
if (!vkb_call_exec(&c)) {
|
||||
pd->ready = -1;
|
||||
pthread_mutex_unlock(&pd->lock);
|
||||
return 0;
|
||||
}
|
||||
vkb_dec_raw_into(&c.d, &pd->info, sizeof(pd->info));
|
||||
vkb_call_end(&c);
|
||||
vkb_wire_vkGetPhysicalDeviceProperties((VkPhysicalDevice)pd, &pd->props);
|
||||
vkb_wire_vkGetPhysicalDeviceMemoryProperties((VkPhysicalDevice)pd, &pd->server_mem);
|
||||
build_memory_model(pd);
|
||||
|
||||
uint32_t n = 0;
|
||||
vkb_wire_vkEnumerateDeviceExtensionProperties((VkPhysicalDevice)pd, NULL, &n, NULL);
|
||||
VkExtensionProperties *srv = calloc(n + 1, sizeof(*srv));
|
||||
vkb_wire_vkEnumerateDeviceExtensionProperties((VkPhysicalDevice)pd, NULL, &n, srv);
|
||||
uint32_t ncl = 0;
|
||||
const char *const *cl = vkb_client_device_exts(&ncl);
|
||||
uint32_t nemu = 0;
|
||||
const VkExtensionProperties *emu = vkb_emu_extensions(pd, &nemu);
|
||||
pd->exts = calloc(n + ncl + nemu + 2, sizeof(VkExtensionProperties));
|
||||
pd->next = 0;
|
||||
for (uint32_t i = 0; i < n; i++)
|
||||
if (bridged_device_ext(srv[i].extensionName) && bridged_device_ext_for(pd, srv[i].extensionName)) pd->exts[pd->next++] = srv[i];
|
||||
for (uint32_t i = 0; i < nemu; i++) {
|
||||
int dup = 0;
|
||||
for (uint32_t j = 0; j < pd->next; j++) dup |= !strcmp(pd->exts[j].extensionName, emu[i].extensionName);
|
||||
if (!dup) pd->exts[pd->next++] = emu[i];
|
||||
}
|
||||
if (drm_node(NULL, NULL)) {
|
||||
VkExtensionProperties *e = &pd->exts[pd->next++];
|
||||
snprintf(e->extensionName, sizeof(e->extensionName), "VK_EXT_physical_device_drm");
|
||||
e->specVersion = 1;
|
||||
}
|
||||
for (uint32_t i = 0; i < ncl; i++) {
|
||||
VkExtensionProperties *e = &pd->exts[pd->next++];
|
||||
snprintf(e->extensionName, sizeof(e->extensionName), "%s", cl[i]);
|
||||
e->specVersion = !strcmp(cl[i], "VK_KHR_swapchain") ? 70 : !strcmp(cl[i], "VK_KHR_incremental_present") ? 2 : 1;
|
||||
}
|
||||
free(srv);
|
||||
VKB_INFO("%s: %u extensions offered, memory sharing %s, emulation 0x%x", pd->props.deviceName, pd->next,
|
||||
vkb_mem_strategy_name(pd->info.strategy), pd->info.emu);
|
||||
pd->ready = 1;
|
||||
pthread_mutex_unlock(&pd->lock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ properties */
|
||||
|
||||
uint32_t vkb_max_api(void)
|
||||
{
|
||||
/* VKBRIDGE_MAX_API=1.3 imitates an older driver (testing). */
|
||||
const char *v = getenv("VKBRIDGE_MAX_API");
|
||||
unsigned ma, mi;
|
||||
if (v && sscanf(v, "%u.%u", &ma, &mi) == 2) return VK_MAKE_API_VERSION(0, ma, mi, 0xfff);
|
||||
return VKB_API_VERSION;
|
||||
}
|
||||
|
||||
static void patch_props(vkb_physdev *pd, VkPhysicalDeviceProperties *p)
|
||||
{
|
||||
if (p->apiVersion > vkb_max_api()) p->apiVersion = vkb_max_api();
|
||||
(void)pd;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties)
|
||||
{
|
||||
vkb_physdev *pd = vkb_pd(physicalDevice);
|
||||
if (vkb_physdev_ready(pd)) *pProperties = pd->props;
|
||||
else vkb_wire_vkGetPhysicalDeviceProperties(physicalDevice, pProperties);
|
||||
patch_props(pd, pProperties);
|
||||
vkb_emu_patch_limits(pd, pProperties);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties2 *pProperties)
|
||||
{
|
||||
vkb_physdev *pd = vkb_pd(physicalDevice);
|
||||
vkb_wire_vkGetPhysicalDeviceProperties2(physicalDevice, pProperties);
|
||||
patch_props(pd, &pProperties->properties);
|
||||
vkb_emu_patch_limits(pd, &pProperties->properties);
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)pProperties->pNext; b; b = b->pNext) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES) {
|
||||
VkPhysicalDeviceDriverProperties *d = (VkPhysicalDeviceDriverProperties *)b;
|
||||
size_t l = strlen(d->driverInfo);
|
||||
if (l + 12 < sizeof(d->driverInfo)) strcat(d->driverInfo, " (vkbridge)");
|
||||
}
|
||||
if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT) {
|
||||
VkPhysicalDeviceDrmPropertiesEXT *d = (VkPhysicalDeviceDrmPropertiesEXT *)b;
|
||||
int64_t ma, mi;
|
||||
if (drm_node(&ma, &mi)) {
|
||||
/* gamescope insists on a primary node; the stand-in serves as both, as with
|
||||
* KGSL on an Adreno. */
|
||||
d->hasPrimary = VK_TRUE;
|
||||
d->primaryMajor = ma;
|
||||
d->primaryMinor = mi;
|
||||
d->hasRender = VK_TRUE;
|
||||
d->renderMajor = ma;
|
||||
d->renderMinor = mi;
|
||||
}
|
||||
}
|
||||
vkb_emu_patch_properties_chain(pd, b);
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures *pFeatures)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceFeatures(physicalDevice, pFeatures);
|
||||
vkb_emu_patch_features(vkb_pd(physicalDevice), pFeatures);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice, VkPhysicalDeviceFeatures2 *pFeatures)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
|
||||
vkb_emu_patch_features(vkb_pd(physicalDevice), &pFeatures->features);
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)pFeatures->pNext; b; b = b->pNext)
|
||||
vkb_emu_patch_features_chain(vkb_pd(physicalDevice), b);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,
|
||||
VkPhysicalDeviceMemoryProperties *pMemoryProperties)
|
||||
{
|
||||
vkb_physdev *pd = vkb_pd(physicalDevice);
|
||||
if (!vkb_physdev_ready(pd)) {
|
||||
memset(pMemoryProperties, 0, sizeof(*pMemoryProperties));
|
||||
return;
|
||||
}
|
||||
*pMemoryProperties = pd->client_mem;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceMemoryProperties2(VkPhysicalDevice physicalDevice,
|
||||
VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
|
||||
{
|
||||
vkb_physdev *pd = vkb_pd(physicalDevice);
|
||||
VkPhysicalDeviceMemoryBudgetPropertiesEXT *budget = NULL;
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)pMemoryProperties->pNext; b; b = b->pNext)
|
||||
if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT) budget = (void *)b;
|
||||
vkb_wire_vkGetPhysicalDeviceMemoryProperties2(physicalDevice, pMemoryProperties);
|
||||
if (!vkb_physdev_ready(pd)) return;
|
||||
pMemoryProperties->memoryProperties = pd->client_mem;
|
||||
if (budget) {
|
||||
VkDeviceSize b[VK_MAX_MEMORY_HEAPS], u[VK_MAX_MEMORY_HEAPS];
|
||||
memcpy(b, budget->heapBudget, sizeof(b));
|
||||
memcpy(u, budget->heapUsage, sizeof(u));
|
||||
memset(budget->heapBudget, 0, sizeof(budget->heapBudget));
|
||||
memset(budget->heapUsage, 0, sizeof(budget->heapUsage));
|
||||
for (uint32_t h = 0; h < pd->client_mem.memoryHeapCount; h++) {
|
||||
budget->heapBudget[h] = b[pd->heap_map[h]];
|
||||
budget->heapUsage[h] = u[pd->heap_map[h]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice, uint32_t *pCount,
|
||||
VkQueueFamilyProperties *pProps)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pCount, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice, uint32_t *pCount,
|
||||
VkQueueFamilyProperties2 *pProps)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceQueueFamilyProperties2(physicalDevice, pCount, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
|
||||
VkFormatProperties *pFormatProperties)
|
||||
{
|
||||
if (vkb_emu_format_properties(vkb_pd(physicalDevice), format, pFormatProperties, NULL)) return;
|
||||
vkb_wire_vkGetPhysicalDeviceFormatProperties(physicalDevice, format, pFormatProperties);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice, VkFormat format,
|
||||
VkFormatProperties2 *pFormatProperties)
|
||||
{
|
||||
if (vkb_emu_format_properties(vkb_pd(physicalDevice), format, &pFormatProperties->formatProperties, pFormatProperties)) return;
|
||||
vkb_wire_vkGetPhysicalDeviceFormatProperties2(physicalDevice, format, pFormatProperties);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
|
||||
VkImageType type, VkImageTiling tiling,
|
||||
VkImageUsageFlags usage, VkImageCreateFlags flags,
|
||||
VkImageFormatProperties *pProps)
|
||||
{
|
||||
VkFormat real = vkb_emu_real_format(vkb_pd(physicalDevice), format, &usage, &flags);
|
||||
return vkb_wire_vkGetPhysicalDeviceImageFormatProperties(physicalDevice, real, type, tiling, usage, flags, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,
|
||||
const VkPhysicalDeviceImageFormatInfo2 *pInfo,
|
||||
VkImageFormatProperties2 *pProps)
|
||||
{
|
||||
VkPhysicalDeviceImageFormatInfo2 info = *pInfo;
|
||||
info.format = vkb_emu_real_format(vkb_pd(physicalDevice), pInfo->format, &info.usage, &info.flags);
|
||||
return vkb_wire_vkGetPhysicalDeviceImageFormatProperties2(physicalDevice, &info, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format,
|
||||
VkImageType type, VkSampleCountFlagBits samples,
|
||||
VkImageUsageFlags usage, VkImageTiling tiling,
|
||||
uint32_t *pCount, VkSparseImageFormatProperties *pProps)
|
||||
{
|
||||
if (vkb_emu_is_emulated_format(vkb_pd(physicalDevice), format)) {
|
||||
*pCount = 0;
|
||||
return;
|
||||
}
|
||||
vkb_wire_vkGetPhysicalDeviceSparseImageFormatProperties(physicalDevice, format, type, samples, usage, tiling, pCount, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,
|
||||
const VkPhysicalDeviceSparseImageFormatInfo2 *pInfo,
|
||||
uint32_t *pCount, VkSparseImageFormatProperties2 *pProps)
|
||||
{
|
||||
if (vkb_emu_is_emulated_format(vkb_pd(physicalDevice), pInfo->format)) {
|
||||
*pCount = 0;
|
||||
return;
|
||||
}
|
||||
vkb_wire_vkGetPhysicalDeviceSparseImageFormatProperties2(physicalDevice, pInfo, pCount, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceToolProperties(VkPhysicalDevice physicalDevice, uint32_t *pToolCount,
|
||||
VkPhysicalDeviceToolProperties *pToolProperties)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)pToolProperties;
|
||||
*pToolCount = 0;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,
|
||||
const VkPhysicalDeviceExternalBufferInfo *pInfo,
|
||||
VkExternalBufferProperties *pProps)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceExternalBufferProperties(physicalDevice, pInfo, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceExternalSemaphoreProperties(VkPhysicalDevice physicalDevice,
|
||||
const VkPhysicalDeviceExternalSemaphoreInfo *pInfo,
|
||||
VkExternalSemaphoreProperties *pProps)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceExternalSemaphoreProperties(physicalDevice, pInfo, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceExternalFenceProperties(VkPhysicalDevice physicalDevice,
|
||||
const VkPhysicalDeviceExternalFenceInfo *pInfo,
|
||||
VkExternalFenceProperties *pProps)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceExternalFenceProperties(physicalDevice, pInfo, pProps);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkGetPhysicalDeviceMultisamplePropertiesEXT(VkPhysicalDevice physicalDevice,
|
||||
VkSampleCountFlagBits samples,
|
||||
VkMultisamplePropertiesEXT *pProps)
|
||||
{
|
||||
vkb_wire_vkGetPhysicalDeviceMultisamplePropertiesEXT(physicalDevice, samples, pProps);
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* DroidDeck Vulkan bridge - client ICD (libvulkan_droidbridge.so, glibc).
|
||||
*
|
||||
* A Vulkan driver for the Vulkan loader inside the Linux session. It owns no GPU: every call is
|
||||
* encoded and executed by the server (app/src/main/cpp/vkbridge) on the device's real driver.
|
||||
* Command buffers are recorded locally and shipped at vkEndCommandBuffer; mapped memory is
|
||||
* shared pages (see server_memory.c); presentation (WSI) is implemented here.
|
||||
*/
|
||||
#ifndef VKB_CLIENT_H
|
||||
#define VKB_CLIENT_H
|
||||
|
||||
#define VK_USE_PLATFORM_WAYLAND_KHR 1
|
||||
#define VK_USE_PLATFORM_XCB_KHR 1
|
||||
#define VK_USE_PLATFORM_XLIB_KHR 1
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "vkb_wire.h"
|
||||
#include "vkb_proto.h"
|
||||
#include "vkb_log.h"
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <vulkan/vk_icd.h>
|
||||
|
||||
#define VKB_EXPORT __attribute__((visibility("default")))
|
||||
#define VKB_API_VERSION VK_MAKE_API_VERSION(0, 1, 4, VK_HEADER_VERSION)
|
||||
|
||||
/* ------------------------------------------------------------------ small hash map (u64 -> ptr) */
|
||||
typedef struct vkb_map {
|
||||
uint64_t *keys;
|
||||
void **vals;
|
||||
uint32_t cap, count;
|
||||
pthread_mutex_t lock;
|
||||
} vkb_map;
|
||||
void vkb_map_init(vkb_map *m);
|
||||
void vkb_map_destroy(vkb_map *m);
|
||||
void vkb_map_put(vkb_map *m, uint64_t k, void *v);
|
||||
void *vkb_map_get(vkb_map *m, uint64_t k);
|
||||
void *vkb_map_remove(vkb_map *m, uint64_t k);
|
||||
|
||||
/* ------------------------------------------------------------------ dispatchable objects */
|
||||
typedef struct vkb_object {
|
||||
VK_LOADER_DATA loader; /* must be first: the loader keeps its dispatch pointer here */
|
||||
VkObjectType type;
|
||||
uint64_t remote; /* the server's handle */
|
||||
uint32_t table; /* the server's dispatch table for calls on this object */
|
||||
} vkb_object;
|
||||
|
||||
typedef struct vkb_mem_type_map {
|
||||
uint32_t server_type;
|
||||
uint8_t shared;
|
||||
} vkb_mem_type_map;
|
||||
|
||||
struct vkb_instance;
|
||||
|
||||
typedef struct vkb_physdev {
|
||||
vkb_object obj;
|
||||
struct vkb_instance *instance;
|
||||
vkb_server_pd_info info;
|
||||
VkPhysicalDeviceProperties props;
|
||||
VkPhysicalDeviceMemoryProperties server_mem;
|
||||
VkPhysicalDeviceMemoryProperties client_mem; /* what the app sees */
|
||||
vkb_mem_type_map type_map[VK_MAX_MEMORY_TYPES];
|
||||
uint32_t heap_map[VK_MAX_MEMORY_HEAPS]; /* client heap -> server heap */
|
||||
VkExtensionProperties *exts; /* filtered device extensions */
|
||||
uint32_t next;
|
||||
int ready;
|
||||
pthread_mutex_t lock;
|
||||
struct vkb_physdev *list_next;
|
||||
} vkb_physdev;
|
||||
|
||||
typedef struct vkb_instance {
|
||||
vkb_object obj;
|
||||
uint32_t api_version;
|
||||
vkb_physdev *pds;
|
||||
pthread_mutex_t lock;
|
||||
uint8_t ext_surface, ext_wayland, ext_xcb, ext_xlib, ext_headless, ext_surface_caps2, ext_debug_utils;
|
||||
} vkb_instance;
|
||||
|
||||
struct vkb_queue;
|
||||
|
||||
typedef struct vkb_device {
|
||||
vkb_object obj;
|
||||
vkb_physdev *pd;
|
||||
vkb_instance *instance;
|
||||
struct vkb_queue *queues;
|
||||
pthread_mutex_t lock;
|
||||
vkb_map memories; /* VkDeviceMemory -> vkb_memory* */
|
||||
vkb_map templates; /* VkDescriptorUpdateTemplate -> vkb_template* */
|
||||
vkb_map pools; /* VkCommandPool -> vkb_cmdpool* */
|
||||
vkb_map unshared; /* VkBuffer/VkImage that cannot live in shared memory -> (void*)1 */
|
||||
vkb_map shared_images; /* VkImage that may live in shared memory -> (void*)1 */
|
||||
const char **enabled_exts;
|
||||
uint32_t enabled_ext_count;
|
||||
uint8_t ext_swapchain;
|
||||
uint32_t api_version;
|
||||
} vkb_device;
|
||||
|
||||
typedef struct vkb_queue {
|
||||
vkb_object obj;
|
||||
vkb_device *device;
|
||||
uint32_t family, index, flags;
|
||||
struct vkb_queue *next;
|
||||
} vkb_queue;
|
||||
|
||||
typedef struct vkb_cmdbuf {
|
||||
vkb_object obj;
|
||||
vkb_device *device;
|
||||
VkCommandPool pool;
|
||||
VkCommandBufferLevel level;
|
||||
vkb_enc stream;
|
||||
size_t entry; /* offset of the open entry's payload */
|
||||
int recording;
|
||||
int broken;
|
||||
struct vkb_cmdbuf *pool_next, *pool_prev;
|
||||
} vkb_cmdbuf;
|
||||
|
||||
typedef struct vkb_cmdpool {
|
||||
vkb_cmdbuf *head;
|
||||
} vkb_cmdpool;
|
||||
|
||||
typedef struct vkb_memory {
|
||||
void *ptr; /* client mapping (shared allocations) */
|
||||
VkDeviceSize size;
|
||||
uint32_t client_type;
|
||||
int shared;
|
||||
} vkb_memory;
|
||||
|
||||
static inline vkb_device *vkb_dev(VkDevice d) { return (vkb_device *)d; }
|
||||
static inline vkb_physdev *vkb_pd(VkPhysicalDevice p) { return (vkb_physdev *)p; }
|
||||
static inline vkb_instance *vkb_inst(VkInstance i) { return (vkb_instance *)i; }
|
||||
static inline vkb_cmdbuf *vkb_cb(VkCommandBuffer c) { return (vkb_cmdbuf *)c; }
|
||||
|
||||
/* ------------------------------------------------------------------ calls */
|
||||
typedef struct vkb_call {
|
||||
vkb_enc e;
|
||||
vkb_dec d;
|
||||
uint32_t cmd, table, flags;
|
||||
struct vkb_conn *conn;
|
||||
} vkb_call;
|
||||
|
||||
void vkb_call_begin(vkb_call *c, uint32_t cmd, uint32_t table);
|
||||
/* Sends the request and waits for the reply. 0 = failed (the call is already released). */
|
||||
int vkb_call_exec(vkb_call *c);
|
||||
void vkb_call_end(vkb_call *c);
|
||||
/* Sends without waiting for a reply (void commands without outputs); ordered by vkb_msg_hdr.seq. */
|
||||
int vkb_call_exec_async(vkb_call *c);
|
||||
int vkb_async_results(void);
|
||||
void vkb_stats_count(uint32_t cmd, int async, size_t bytes);
|
||||
uint32_t vkb_table_of(const void *dispatchable);
|
||||
int vkb_connected(void);
|
||||
/* Per-thread transport cleanup for fork children. */
|
||||
void vkb_conn_after_fork(void);
|
||||
|
||||
/* Command streams (cmdbuf.c). */
|
||||
vkb_enc *vkb_cmd_record_begin(VkCommandBuffer cb, uint32_t cmd);
|
||||
void vkb_cmd_record_end(VkCommandBuffer cb);
|
||||
|
||||
/* Dispatchable objects created by the server (generated stubs call this). */
|
||||
void *vkb_wrap(VkObjectType type, const void *parent, uint64_t remote);
|
||||
|
||||
/* physdev.c */
|
||||
int vkb_physdev_ready(vkb_physdev *pd);
|
||||
uint32_t vkb_mem_bits_to_client(vkb_physdev *pd, uint32_t server_bits, int allow_shared);
|
||||
|
||||
/* memory.c */
|
||||
void vkb_memory_init_device(vkb_device *d);
|
||||
|
||||
/* icd.c */
|
||||
PFN_vkVoidFunction vkb_lookup_proc(const char *name, int level_max, vkb_device *dev, vkb_instance *inst);
|
||||
int vkb_device_ext_enabled(const vkb_device *d, const char *name);
|
||||
|
||||
/* memory.c */
|
||||
void *vkb_chain_clone(const void *pNext, void **mem);
|
||||
int vkb_is_client_device_ext(const char *n);
|
||||
|
||||
/* misc */
|
||||
int vkb_memfd(const char *name, size_t size);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,801 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Surfaces and swapchains (backend-independent part).
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "wsi.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
const VkSurfaceFormatKHR vkb_wsi_formats[] = {
|
||||
{VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
|
||||
{VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
|
||||
{VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
|
||||
{VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
|
||||
};
|
||||
const uint32_t vkb_wsi_format_count = sizeof(vkb_wsi_formats) / sizeof(vkb_wsi_formats[0]);
|
||||
|
||||
#define FOURCC(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
|
||||
|
||||
uint32_t vkb_drm_format(VkFormat f)
|
||||
{
|
||||
switch (f) {
|
||||
case VK_FORMAT_B8G8R8A8_SRGB:
|
||||
case VK_FORMAT_B8G8R8A8_UNORM: return FOURCC('A', 'R', '2', '4');
|
||||
case VK_FORMAT_R8G8B8A8_SRGB:
|
||||
case VK_FORMAT_R8G8B8A8_UNORM: return FOURCC('A', 'B', '2', '4');
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static vkb_surface *surf(VkSurfaceKHR s) { return (vkb_surface *)(uintptr_t)s; }
|
||||
static vkb_swapchain *chain(VkSwapchainKHR s) { return (vkb_swapchain *)(uintptr_t)s; }
|
||||
|
||||
/* ------------------------------------------------------------------ surfaces */
|
||||
|
||||
static VkResult new_surface(enum vkb_surface_kind kind, VkSurfaceKHR *out, vkb_surface **ps)
|
||||
{
|
||||
vkb_surface *s = calloc(1, sizeof(*s));
|
||||
if (!s) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
s->kind = kind;
|
||||
*ps = s;
|
||||
*out = (VkSurfaceKHR)(uintptr_t)s;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateWaylandSurfaceKHR(VkInstance instance, const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
|
||||
{
|
||||
(void)instance;
|
||||
(void)pAllocator;
|
||||
if (!vkb_wl_load()) return VK_ERROR_INITIALIZATION_FAILED;
|
||||
vkb_surface *s;
|
||||
VkResult r = new_surface(VKB_SURF_WAYLAND, pSurface, &s);
|
||||
if (r == VK_SUCCESS) {
|
||||
s->wl_display = pCreateInfo->display;
|
||||
s->wl_surface = pCreateInfo->surface;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateXcbSurfaceKHR(VkInstance instance, const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
|
||||
{
|
||||
(void)instance;
|
||||
(void)pAllocator;
|
||||
if (!vkb_xcb_load()) return VK_ERROR_INITIALIZATION_FAILED;
|
||||
vkb_surface *s;
|
||||
VkResult r = new_surface(VKB_SURF_XCB, pSurface, &s);
|
||||
if (r == VK_SUCCESS) {
|
||||
s->xcb_conn = pCreateInfo->connection;
|
||||
s->xcb_window = pCreateInfo->window;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateXlibSurfaceKHR(VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
|
||||
{
|
||||
(void)instance;
|
||||
(void)pAllocator;
|
||||
if (!vkb_xcb_load()) return VK_ERROR_INITIALIZATION_FAILED;
|
||||
void *conn = vkb_xlib_to_xcb(pCreateInfo->dpy);
|
||||
if (!conn) return VK_ERROR_INITIALIZATION_FAILED;
|
||||
vkb_surface *s;
|
||||
VkResult r = new_surface(VKB_SURF_XCB, pSurface, &s);
|
||||
if (r == VK_SUCCESS) {
|
||||
s->xcb_conn = conn;
|
||||
s->xcb_window = (uint32_t)pCreateInfo->window;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateHeadlessSurfaceEXT(VkInstance instance, const VkHeadlessSurfaceCreateInfoEXT *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
|
||||
{
|
||||
(void)instance;
|
||||
(void)pCreateInfo;
|
||||
(void)pAllocator;
|
||||
vkb_surface *s;
|
||||
return new_surface(VKB_SURF_HEADLESS, pSurface, &s);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
(void)instance;
|
||||
(void)pAllocator;
|
||||
free(surf(surface));
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkBool32 VKAPI_CALL vkb_ep_vkGetPhysicalDeviceWaylandPresentationSupportKHR(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex,
|
||||
struct wl_display *display)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)queueFamilyIndex;
|
||||
(void)display;
|
||||
return vkb_wl_load() ? VK_TRUE : VK_FALSE;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkBool32 VKAPI_CALL vkb_ep_vkGetPhysicalDeviceXcbPresentationSupportKHR(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex,
|
||||
xcb_connection_t *connection, xcb_visualid_t visual_id)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)queueFamilyIndex;
|
||||
(void)connection;
|
||||
(void)visual_id;
|
||||
return vkb_xcb_load() ? VK_TRUE : VK_FALSE;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkBool32 VKAPI_CALL vkb_ep_vkGetPhysicalDeviceXlibPresentationSupportKHR(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex,
|
||||
Display *dpy, VisualID visualID)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)queueFamilyIndex;
|
||||
(void)dpy;
|
||||
(void)visualID;
|
||||
return vkb_xcb_load() ? VK_TRUE : VK_FALSE;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex,
|
||||
VkSurfaceKHR surface, VkBool32 *pSupported)
|
||||
{
|
||||
(void)surface;
|
||||
/* Presentation is a copy or a hand-over after the queue's work: any queue that can do
|
||||
* graphics or compute (or transfer) can present. */
|
||||
uint32_t n = 0;
|
||||
vkb_wire_vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &n, NULL);
|
||||
VkQueueFamilyProperties *qp = calloc(n ? n : 1, sizeof(*qp));
|
||||
vkb_wire_vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &n, qp);
|
||||
*pSupported = queueFamilyIndex < n &&
|
||||
(qp[queueFamilyIndex].queueFlags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT));
|
||||
free(qp);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VkResult surface_extent(vkb_surface *s, VkExtent2D *e)
|
||||
{
|
||||
switch (s->kind) {
|
||||
case VKB_SURF_WAYLAND: return vkb_wl_surface_extent(s, e);
|
||||
case VKB_SURF_XCB: return vkb_xcb_surface_extent(s, e);
|
||||
default:
|
||||
e->width = e->height = 0xFFFFFFFFu;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
static VkImageUsageFlags supported_usage(VkPhysicalDevice pd)
|
||||
{
|
||||
VkImageUsageFlags u = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
|
||||
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
|
||||
VkFormatProperties fp;
|
||||
vkb_wire_vkGetPhysicalDeviceFormatProperties(pd, VK_FORMAT_B8G8R8A8_UNORM, &fp);
|
||||
if (fp.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) u |= VK_IMAGE_USAGE_STORAGE_BIT;
|
||||
return u;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
|
||||
VkSurfaceCapabilitiesKHR *c)
|
||||
{
|
||||
vkb_surface *s = surf(surface);
|
||||
memset(c, 0, sizeof(*c));
|
||||
VkResult r = surface_extent(s, &c->currentExtent);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
c->minImageCount = s->kind == VKB_SURF_WAYLAND ? 3 : 2;
|
||||
c->maxImageCount = 8;
|
||||
c->minImageExtent = (VkExtent2D){1, 1};
|
||||
c->maxImageExtent = (VkExtent2D){16384, 16384};
|
||||
if (c->currentExtent.width != 0xFFFFFFFFu) {
|
||||
c->minImageExtent = c->maxImageExtent = c->currentExtent;
|
||||
if (!c->currentExtent.width || !c->currentExtent.height) c->minImageExtent = (VkExtent2D){0, 0};
|
||||
}
|
||||
c->maxImageArrayLayers = 1;
|
||||
c->supportedTransforms = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
c->currentTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
c->supportedCompositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
|
||||
if (s->kind == VKB_SURF_WAYLAND) c->supportedCompositeAlpha |= VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
|
||||
c->supportedUsageFlags = supported_usage(physicalDevice);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice physicalDevice,
|
||||
const VkPhysicalDeviceSurfaceInfo2KHR *pInfo,
|
||||
VkSurfaceCapabilities2KHR *pCaps)
|
||||
{
|
||||
VkResult r = vkb_ep_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, pInfo->surface, &pCaps->surfaceCapabilities);
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)pCaps->pNext; b; b = b->pNext) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR)
|
||||
((VkSurfaceProtectedCapabilitiesKHR *)b)->supportsProtected = VK_FALSE;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
|
||||
uint32_t *pCount, VkSurfaceFormatKHR *pFormats)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)surface;
|
||||
if (!pFormats) {
|
||||
*pCount = vkb_wsi_format_count;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
uint32_t n = *pCount < vkb_wsi_format_count ? *pCount : vkb_wsi_format_count;
|
||||
memcpy(pFormats, vkb_wsi_formats, n * sizeof(*pFormats));
|
||||
*pCount = n;
|
||||
return n < vkb_wsi_format_count ? VK_INCOMPLETE : VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
|
||||
const VkPhysicalDeviceSurfaceInfo2KHR *pInfo, uint32_t *pCount,
|
||||
VkSurfaceFormat2KHR *pFormats)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)pInfo;
|
||||
if (!pFormats) {
|
||||
*pCount = vkb_wsi_format_count;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
uint32_t n = *pCount < vkb_wsi_format_count ? *pCount : vkb_wsi_format_count;
|
||||
for (uint32_t i = 0; i < n; i++) pFormats[i].surfaceFormat = vkb_wsi_formats[i];
|
||||
*pCount = n;
|
||||
return n < vkb_wsi_format_count ? VK_INCOMPLETE : VK_SUCCESS;
|
||||
}
|
||||
|
||||
static const VkPresentModeKHR present_modes[] = {VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR};
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
|
||||
uint32_t *pCount, VkPresentModeKHR *pModes)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
(void)surface;
|
||||
uint32_t total = 3;
|
||||
if (!pModes) {
|
||||
*pCount = total;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
uint32_t n = *pCount < total ? *pCount : total;
|
||||
memcpy(pModes, present_modes, n * sizeof(*pModes));
|
||||
*pCount = n;
|
||||
return n < total ? VK_INCOMPLETE : VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
|
||||
uint32_t *pRectCount, VkRect2D *pRects)
|
||||
{
|
||||
(void)physicalDevice;
|
||||
if (!pRects) {
|
||||
*pRectCount = 1;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
if (*pRectCount < 1) return VK_INCOMPLETE;
|
||||
VkExtent2D e;
|
||||
surface_extent(surf(surface), &e);
|
||||
if (e.width == 0xFFFFFFFFu) e = (VkExtent2D){16384, 16384};
|
||||
pRects[0] = (VkRect2D){{0, 0}, e};
|
||||
*pRectCount = 1;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetDeviceGroupPresentCapabilitiesKHR(VkDevice device, VkDeviceGroupPresentCapabilitiesKHR *pCaps)
|
||||
{
|
||||
(void)device;
|
||||
memset(pCaps->presentMask, 0, sizeof(pCaps->presentMask));
|
||||
pCaps->presentMask[0] = 1;
|
||||
pCaps->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetDeviceGroupSurfacePresentModesKHR(VkDevice device, VkSurfaceKHR surface,
|
||||
VkDeviceGroupPresentModeFlagsKHR *pModes)
|
||||
{
|
||||
(void)device;
|
||||
(void)surface;
|
||||
*pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ swapchain images */
|
||||
|
||||
static uint32_t pick_type(vkb_device *dev, uint32_t bits, VkMemoryPropertyFlags want)
|
||||
{
|
||||
const VkPhysicalDeviceMemoryProperties *mp = &dev->pd->client_mem;
|
||||
for (uint32_t i = 0; i < mp->memoryTypeCount; i++)
|
||||
if ((bits & (1u << i)) && (mp->memoryTypes[i].propertyFlags & want) == want) return i;
|
||||
for (uint32_t i = 0; i < mp->memoryTypeCount; i++)
|
||||
if (bits & (1u << i)) return i;
|
||||
return UINT32_MAX;
|
||||
}
|
||||
|
||||
static int pd_has_ext(vkb_physdev *pd, const char *name)
|
||||
{
|
||||
for (uint32_t i = 0; i < pd->next; i++)
|
||||
if (!strcmp(pd->exts[i].extensionName, name)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* One swapchain image: exported as a linear dma-buf when `dmabuf`, else an ordinary image. */
|
||||
static VkResult create_image(vkb_swapchain *sc, vkb_swap_image *img, int dmabuf)
|
||||
{
|
||||
vkb_device *dev = sc->dev;
|
||||
VkDevice device = (VkDevice)dev;
|
||||
const VkSwapchainCreateInfoKHR *ci = &sc->info;
|
||||
VkImageCreateInfo ici = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
|
||||
ici.imageType = VK_IMAGE_TYPE_2D;
|
||||
ici.format = ci->imageFormat;
|
||||
ici.extent = (VkExtent3D){ci->imageExtent.width, ci->imageExtent.height, 1};
|
||||
ici.mipLevels = 1;
|
||||
ici.arrayLayers = ci->imageArrayLayers;
|
||||
ici.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
ici.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
ici.usage = ci->imageUsage | (sc->copy_present || !dmabuf ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT : 0);
|
||||
ici.sharingMode = ci->imageSharingMode;
|
||||
ici.queueFamilyIndexCount = ci->queueFamilyIndexCount;
|
||||
ici.pQueueFamilyIndices = ci->pQueueFamilyIndices;
|
||||
ici.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
if (ci->flags & VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR) ici.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
|
||||
VkImageFormatListCreateInfo fl = {VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO, NULL, sc->view_format_count, sc->view_formats};
|
||||
VkExternalMemoryImageCreateInfo ext = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, NULL,
|
||||
VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT};
|
||||
uint64_t linear = 0; /* DRM_FORMAT_MOD_LINEAR */
|
||||
VkImageDrmFormatModifierListCreateInfoEXT mods = {VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT, NULL, 1, &linear};
|
||||
const void *chain = NULL;
|
||||
if (sc->view_format_count) {
|
||||
fl.pNext = chain;
|
||||
chain = &fl;
|
||||
}
|
||||
int use_mods = 0;
|
||||
if (dmabuf) {
|
||||
ext.pNext = chain;
|
||||
chain = &ext;
|
||||
if (pd_has_ext(dev->pd, "VK_EXT_image_drm_format_modifier")) {
|
||||
mods.pNext = chain;
|
||||
chain = &mods;
|
||||
ici.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
|
||||
use_mods = 1;
|
||||
} else {
|
||||
ici.tiling = VK_IMAGE_TILING_LINEAR;
|
||||
}
|
||||
}
|
||||
ici.pNext = chain;
|
||||
VkResult r = vkb_wire_vkCreateImage(device, &ici, NULL, &img->image);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
VkMemoryRequirements req;
|
||||
vkb_ep_vkGetImageMemoryRequirements(device, img->image, &req);
|
||||
uint32_t type = pick_type(dev, req.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
if (type == UINT32_MAX) return VK_ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
VkMemoryDedicatedAllocateInfo ded = {VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, NULL, img->image, VK_NULL_HANDLE};
|
||||
VkExportMemoryAllocateInfo exp = {VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO, &ded, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT};
|
||||
VkMemoryAllocateInfo ai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, dmabuf ? (const void *)&exp : (const void *)&ded, req.size, type};
|
||||
r = vkb_ep_vkAllocateMemory(device, &ai, NULL, &img->memory);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
r = vkb_wire_vkBindImageMemory(device, img->image, img->memory, 0);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
img->dmabuf_fd = -1;
|
||||
if (dmabuf) {
|
||||
VkMemoryGetFdInfoKHR gi = {VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, NULL, img->memory, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT};
|
||||
r = vkb_wire_vkGetMemoryFdKHR(device, &gi, &img->dmabuf_fd);
|
||||
if (r != VK_SUCCESS || img->dmabuf_fd < 0) return r != VK_SUCCESS ? r : VK_ERROR_INITIALIZATION_FAILED;
|
||||
VkImageSubresource sub = {use_mods ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT : VK_IMAGE_ASPECT_COLOR_BIT, 0, 0};
|
||||
VkSubresourceLayout lay;
|
||||
vkb_wire_vkGetImageSubresourceLayout(device, img->image, &sub, &lay);
|
||||
img->stride = (uint32_t)lay.rowPitch;
|
||||
img->offset = (uint32_t)lay.offset;
|
||||
img->modifier = 0;
|
||||
if (use_mods) {
|
||||
VkImageDrmFormatModifierPropertiesEXT mp = {VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT};
|
||||
if (vkb_wire_vkGetImageDrmFormatModifierPropertiesEXT(device, img->image, &mp) == VK_SUCCESS) img->modifier = mp.drmFormatModifier;
|
||||
}
|
||||
}
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static void destroy_image(vkb_swapchain *sc, vkb_swap_image *img)
|
||||
{
|
||||
VkDevice device = (VkDevice)sc->dev;
|
||||
if (img->dmabuf_fd >= 0) close(img->dmabuf_fd);
|
||||
if (img->image) vkb_ep_vkDestroyImage(device, img->image, NULL);
|
||||
if (img->memory) vkb_ep_vkFreeMemory(device, img->memory, NULL);
|
||||
}
|
||||
|
||||
static VkResult create_staging(vkb_swapchain *sc)
|
||||
{
|
||||
vkb_device *dev = sc->dev;
|
||||
VkDevice device = (VkDevice)dev;
|
||||
VkDeviceSize size = (VkDeviceSize)sc->info.imageExtent.width * sc->info.imageExtent.height * 4;
|
||||
VkBufferCreateInfo bci = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, NULL, 0, size, VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||
VK_SHARING_MODE_EXCLUSIVE, 0, NULL};
|
||||
VkResult r = vkb_ep_vkCreateBuffer(device, &bci, NULL, &sc->staging);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
VkMemoryRequirements req;
|
||||
vkb_ep_vkGetBufferMemoryRequirements(device, sc->staging, &req);
|
||||
uint32_t type = pick_type(dev, req.memoryTypeBits,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
|
||||
if (type == UINT32_MAX || !(dev->pd->client_mem.memoryTypes[type].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
|
||||
type = pick_type(dev, req.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
|
||||
if (type == UINT32_MAX || !(dev->pd->client_mem.memoryTypes[type].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) {
|
||||
VKB_ERR("swapchain: no host-visible memory to read frames back into");
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
VkMemoryAllocateInfo ai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, NULL, req.size, type};
|
||||
r = vkb_ep_vkAllocateMemory(device, &ai, NULL, &sc->staging_mem);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
r = vkb_wire_vkBindBufferMemory(device, sc->staging, sc->staging_mem, 0);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
void *p = NULL;
|
||||
r = vkb_ep_vkMapMemory(device, sc->staging_mem, 0, VK_WHOLE_SIZE, 0, &p);
|
||||
sc->staging_ptr = p;
|
||||
sc->staging_size = size;
|
||||
return r;
|
||||
}
|
||||
|
||||
static VkResult ensure_copy_cbs(vkb_swapchain *sc, uint32_t family)
|
||||
{
|
||||
VkDevice device = (VkDevice)sc->dev;
|
||||
if (sc->pool && sc->pool_family == family) return VK_SUCCESS;
|
||||
if (sc->pool) {
|
||||
vkb_ep_vkDestroyCommandPool(device, sc->pool, NULL);
|
||||
sc->pool = VK_NULL_HANDLE;
|
||||
}
|
||||
VkCommandPoolCreateInfo pci = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, NULL, 0, family};
|
||||
VkResult r = vkb_wire_vkCreateCommandPool(device, &pci, NULL, &sc->pool);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
sc->pool_family = family;
|
||||
VkCommandBufferAllocateInfo cai = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, NULL, sc->pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, sc->count};
|
||||
r = vkb_ep_vkAllocateCommandBuffers(device, &cai, sc->copy_cbs);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
for (uint32_t i = 0; i < sc->count; i++) {
|
||||
VkCommandBuffer cb = sc->copy_cbs[i];
|
||||
VkCommandBufferBeginInfo bi = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, NULL, 0, NULL};
|
||||
vkb_ep_vkBeginCommandBuffer(cb, &bi);
|
||||
VkImageMemoryBarrier b = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, NULL, VK_ACCESS_MEMORY_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
|
||||
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_QUEUE_FAMILY_IGNORED,
|
||||
VK_QUEUE_FAMILY_IGNORED, sc->images[i].image, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
||||
vkb_wire_vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &b);
|
||||
VkBufferImageCopy region = {0, 0, 0, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, {0, 0, 0},
|
||||
{sc->info.imageExtent.width, sc->info.imageExtent.height, 1}};
|
||||
vkb_wire_vkCmdCopyImageToBuffer(cb, sc->images[i].image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, sc->staging, 1, ®ion);
|
||||
b.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
|
||||
b.dstAccessMask = 0;
|
||||
b.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
|
||||
b.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
VkMemoryBarrier hb = {VK_STRUCTURE_TYPE_MEMORY_BARRIER, NULL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT};
|
||||
vkb_wire_vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 1,
|
||||
&hb, 0, NULL, 1, &b);
|
||||
r = vkb_ep_vkEndCommandBuffer(cb);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
}
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
/* Waits (on the CPU) for the app's rendering into image `index`, reading it back when needed. */
|
||||
VkResult vkb_wsi_wait_rendering(vkb_swapchain *sc, VkQueue queue, uint32_t index, uint32_t nwait, const VkSemaphore *wait)
|
||||
{
|
||||
VkDevice device = (VkDevice)sc->dev;
|
||||
VkPipelineStageFlags *stages = alloca((nwait ? nwait : 1) * sizeof(VkPipelineStageFlags));
|
||||
for (uint32_t i = 0; i < nwait; i++) stages[i] = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
|
||||
VkSubmitInfo si = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, nwait, wait, stages, 0, NULL, 0, NULL};
|
||||
if (sc->copy_present) {
|
||||
VkResult r = ensure_copy_cbs(sc, ((vkb_queue *)queue)->family);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
si.commandBufferCount = 1;
|
||||
si.pCommandBuffers = &sc->copy_cbs[index];
|
||||
}
|
||||
VkResult r = vkb_wire_vkQueueSubmit(queue, 1, &si, sc->fence);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
r = vkb_wire_vkWaitForFences(device, 1, &sc->fence, VK_TRUE, 10ull * 1000 * 1000 * 1000);
|
||||
vkb_wire_vkResetFences(device, 1, &sc->fence);
|
||||
if (r == VK_TIMEOUT) {
|
||||
VKB_ERR("swapchain: a frame took more than 10 s to render");
|
||||
return VK_ERROR_DEVICE_LOST;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ swapchain */
|
||||
|
||||
static void swapchain_free(vkb_swapchain *sc)
|
||||
{
|
||||
VkDevice device = (VkDevice)sc->dev;
|
||||
if (sc->backend && sc->backend->destroy) sc->backend->destroy(sc);
|
||||
for (uint32_t i = 0; i < sc->count; i++) destroy_image(sc, &sc->images[i]);
|
||||
if (sc->pool) vkb_ep_vkDestroyCommandPool(device, sc->pool, NULL);
|
||||
if (sc->fence) vkb_wire_vkDestroyFence(device, sc->fence, NULL);
|
||||
if (sc->staging) vkb_ep_vkDestroyBuffer(device, sc->staging, NULL);
|
||||
if (sc->staging_mem) vkb_ep_vkFreeMemory(device, sc->staging_mem, NULL);
|
||||
free(sc->images);
|
||||
free(sc->copy_cbs);
|
||||
free(sc->view_formats);
|
||||
pthread_mutex_destroy(&sc->lock);
|
||||
free(sc);
|
||||
}
|
||||
|
||||
static VkResult swapchain_build(vkb_swapchain *sc, int dmabuf)
|
||||
{
|
||||
VkDevice device = (VkDevice)sc->dev;
|
||||
sc->dmabuf = dmabuf;
|
||||
for (uint32_t i = 0; i < sc->count; i++) {
|
||||
VkResult r = create_image(sc, &sc->images[i], dmabuf);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
sc->images[i].chain = sc;
|
||||
}
|
||||
if (sc->copy_present) {
|
||||
VkResult r = create_staging(sc);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
}
|
||||
VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
|
||||
VkResult r = vkb_wire_vkCreateFence(device, &fci, NULL, &sc->fence);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
return sc->backend->init(sc);
|
||||
}
|
||||
|
||||
static void swapchain_teardown(vkb_swapchain *sc)
|
||||
{
|
||||
VkDevice device = (VkDevice)sc->dev;
|
||||
if (sc->backend && sc->backend->destroy) sc->backend->destroy(sc);
|
||||
sc->bdata = NULL;
|
||||
for (uint32_t i = 0; i < sc->count; i++) {
|
||||
destroy_image(sc, &sc->images[i]);
|
||||
memset(&sc->images[i], 0, sizeof(sc->images[i]));
|
||||
sc->images[i].dmabuf_fd = -1;
|
||||
}
|
||||
if (sc->fence) vkb_wire_vkDestroyFence(device, sc->fence, NULL);
|
||||
sc->fence = VK_NULL_HANDLE;
|
||||
if (sc->staging) vkb_ep_vkDestroyBuffer(device, sc->staging, NULL);
|
||||
if (sc->staging_mem) vkb_ep_vkFreeMemory(device, sc->staging_mem, NULL);
|
||||
sc->staging = VK_NULL_HANDLE;
|
||||
sc->staging_mem = VK_NULL_HANDLE;
|
||||
sc->staging_ptr = NULL;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
|
||||
const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain)
|
||||
{
|
||||
(void)pAllocator;
|
||||
vkb_device *dev = vkb_dev(device);
|
||||
vkb_surface *s = surf(pCreateInfo->surface);
|
||||
if (pCreateInfo->oldSwapchain) chain(pCreateInfo->oldSwapchain)->retired = 1;
|
||||
vkb_swapchain *sc = calloc(1, sizeof(*sc));
|
||||
if (!sc) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
pthread_mutex_init(&sc->lock, NULL);
|
||||
sc->dev = dev;
|
||||
sc->surface = s;
|
||||
sc->info = *pCreateInfo;
|
||||
sc->info.pNext = NULL;
|
||||
sc->info.pQueueFamilyIndices = NULL;
|
||||
sc->info.queueFamilyIndexCount = 0;
|
||||
sc->info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
for (const VkBaseInStructure *b = pCreateInfo->pNext; b; b = b->pNext) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) {
|
||||
const VkImageFormatListCreateInfo *fl = (const void *)b;
|
||||
sc->view_formats = calloc(fl->viewFormatCount + 1, sizeof(VkFormat));
|
||||
memcpy(sc->view_formats, fl->pViewFormats, fl->viewFormatCount * sizeof(VkFormat));
|
||||
sc->view_format_count = fl->viewFormatCount;
|
||||
}
|
||||
}
|
||||
sc->count = pCreateInfo->minImageCount < 2 ? 2 : pCreateInfo->minImageCount;
|
||||
if (s->kind == VKB_SURF_WAYLAND && sc->count < 3) sc->count = 3;
|
||||
sc->images = calloc(sc->count, sizeof(vkb_swap_image));
|
||||
sc->copy_cbs = calloc(sc->count, sizeof(VkCommandBuffer));
|
||||
for (uint32_t i = 0; i < sc->count; i++) sc->images[i].dmabuf_fd = -1;
|
||||
VkResult r;
|
||||
switch (s->kind) {
|
||||
case VKB_SURF_WAYLAND:
|
||||
sc->backend = &vkb_wsi_wayland_backend;
|
||||
sc->copy_present = getenv("VKBRIDGE_WSI_SHM") != NULL;
|
||||
r = swapchain_build(sc, !sc->copy_present);
|
||||
if (r != VK_SUCCESS && !sc->copy_present) {
|
||||
VKB_WARN("Wayland: no dma-buf presentation (%d); falling back to copies through shared memory", r);
|
||||
swapchain_teardown(sc);
|
||||
sc->copy_present = 1;
|
||||
r = swapchain_build(sc, 0);
|
||||
}
|
||||
break;
|
||||
case VKB_SURF_XCB:
|
||||
sc->backend = &vkb_wsi_x11_backend;
|
||||
sc->copy_present = 1;
|
||||
r = swapchain_build(sc, 0);
|
||||
break;
|
||||
default:
|
||||
sc->backend = &vkb_wsi_headless_backend;
|
||||
r = swapchain_build(sc, 0);
|
||||
break;
|
||||
}
|
||||
if (r != VK_SUCCESS) {
|
||||
VKB_ERR("swapchain creation failed: %d", r);
|
||||
swapchain_free(sc);
|
||||
return r;
|
||||
}
|
||||
VKB_INFO("swapchain %ux%u format %d, %u images, %s", sc->info.imageExtent.width, sc->info.imageExtent.height, sc->info.imageFormat,
|
||||
sc->count, sc->copy_present ? "presented by copy" : sc->dmabuf ? "presented as dma-bufs" : "headless");
|
||||
*pSwapchain = (VkSwapchainKHR)(uintptr_t)sc;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator)
|
||||
{
|
||||
(void)pAllocator;
|
||||
if (!swapchain) return;
|
||||
vkb_swapchain *sc = chain(swapchain);
|
||||
/* Images may still be in use by the GPU (present waits are CPU-side, so not by us). */
|
||||
vkb_wire_vkDeviceWaitIdle(device);
|
||||
swapchain_free(sc);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pCount, VkImage *pImages)
|
||||
{
|
||||
(void)device;
|
||||
vkb_swapchain *sc = chain(swapchain);
|
||||
if (!pImages) {
|
||||
*pCount = sc->count;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
uint32_t n = *pCount < sc->count ? *pCount : sc->count;
|
||||
for (uint32_t i = 0; i < n; i++) pImages[i] = sc->images[i].image;
|
||||
*pCount = n;
|
||||
return n < sc->count ? VK_INCOMPLETE : VK_SUCCESS;
|
||||
}
|
||||
|
||||
/* Signals the app's semaphore/fence for an acquired image (no GPU work is involved). */
|
||||
static VkResult signal_acquire(vkb_device *dev, VkSemaphore sem, VkFence fence)
|
||||
{
|
||||
VkDevice device = (VkDevice)dev;
|
||||
if (sem) {
|
||||
VkImportSemaphoreFdInfoKHR si = {VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR, NULL, sem, VK_SEMAPHORE_IMPORT_TEMPORARY_BIT,
|
||||
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT, -1};
|
||||
VkResult r = vkb_wire_vkImportSemaphoreFdKHR(device, &si);
|
||||
if (r != VK_SUCCESS) {
|
||||
/* No sync-fd import: signal it with an empty submit. */
|
||||
if (!dev->queues) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
VkSubmitInfo s = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, 0, NULL, NULL, 0, NULL, 1, &sem};
|
||||
r = vkb_wire_vkQueueSubmit((VkQueue)dev->queues, 1, &s, fence);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
if (fence) {
|
||||
VkImportFenceFdInfoKHR fi = {VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR, NULL, fence, VK_FENCE_IMPORT_TEMPORARY_BIT,
|
||||
VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT, -1};
|
||||
VkResult r = vkb_wire_vkImportFenceFdKHR(device, &fi);
|
||||
if (r != VK_SUCCESS) {
|
||||
if (!dev->queues) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
r = vkb_wire_vkQueueSubmit((VkQueue)dev->queues, 0, NULL, fence);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static uint64_t now_ns(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, VkSemaphore semaphore,
|
||||
VkFence fence, uint32_t *pImageIndex)
|
||||
{
|
||||
vkb_swapchain *sc = chain(swapchain);
|
||||
if (sc->retired) return VK_ERROR_OUT_OF_DATE_KHR;
|
||||
if (sc->status != VK_SUCCESS) return sc->status;
|
||||
uint64_t deadline = timeout == UINT64_MAX ? UINT64_MAX : now_ns() + timeout;
|
||||
for (;;) {
|
||||
pthread_mutex_lock(&sc->lock);
|
||||
for (uint32_t i = 0; i < sc->count; i++) {
|
||||
if (!sc->images[i].busy && !sc->images[i].acquired) {
|
||||
sc->images[i].acquired = 1;
|
||||
pthread_mutex_unlock(&sc->lock);
|
||||
VkResult r = signal_acquire(vkb_dev(device), semaphore, fence);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
*pImageIndex = i;
|
||||
return sc->status;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&sc->lock);
|
||||
uint64_t now = now_ns();
|
||||
if (timeout == 0) return VK_NOT_READY;
|
||||
if (now >= deadline) return VK_TIMEOUT;
|
||||
VkResult r = sc->backend->wait_free(sc, deadline == UINT64_MAX ? UINT64_MAX : deadline - now);
|
||||
if (r < 0) {
|
||||
sc->status = r;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAcquireNextImage2KHR(VkDevice device, const VkAcquireNextImageInfoKHR *pInfo, uint32_t *pImageIndex)
|
||||
{
|
||||
return vkb_ep_vkAcquireNextImageKHR(device, pInfo->swapchain, pInfo->timeout, pInfo->semaphore, pInfo->fence, pImageIndex);
|
||||
}
|
||||
|
||||
/* VKBRIDGE_WSI_DUMP=/path/prefix: writes presented frame 60 as a PPM (debugging aid). */
|
||||
static void dump_frame(vkb_swapchain *sc, uint32_t index)
|
||||
{
|
||||
/* The 60th present, or every VKBRIDGE_WSI_DUMP_EVERY-th (watching a game get somewhere). */
|
||||
static long every = -1;
|
||||
if (every < 0) every = getenv("VKBRIDGE_WSI_DUMP_EVERY") ? atol(getenv("VKBRIDGE_WSI_DUMP_EVERY")) : 0;
|
||||
if (every > 0 ? sc->presents % every != 0 : sc->presents != 60) return;
|
||||
uint32_t w = sc->info.imageExtent.width, h = sc->info.imageExtent.height, stride = w * 4;
|
||||
const uint8_t *px = NULL;
|
||||
void *map = NULL;
|
||||
size_t maplen = 0;
|
||||
if (sc->copy_present) {
|
||||
px = sc->staging_ptr;
|
||||
} else if (sc->images[index].dmabuf_fd >= 0) {
|
||||
stride = sc->images[index].stride;
|
||||
maplen = (size_t)sc->images[index].offset + (size_t)stride * h;
|
||||
map = mmap(NULL, maplen, PROT_READ, MAP_SHARED, sc->images[index].dmabuf_fd, 0);
|
||||
if (map == MAP_FAILED) return;
|
||||
px = (const uint8_t *)map + sc->images[index].offset;
|
||||
}
|
||||
if (!px) return;
|
||||
char path[512];
|
||||
if (every > 0)
|
||||
snprintf(path, sizeof(path), "%s-%d-%06u.ppm", getenv("VKBRIDGE_WSI_DUMP"), (int)getpid(), (unsigned)sc->presents);
|
||||
else
|
||||
snprintf(path, sizeof(path), "%s-%d.ppm", getenv("VKBRIDGE_WSI_DUMP"), (int)getpid());
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (f) {
|
||||
int bgr = sc->info.imageFormat == VK_FORMAT_B8G8R8A8_UNORM || sc->info.imageFormat == VK_FORMAT_B8G8R8A8_SRGB;
|
||||
fprintf(f, "P6 %u %u 255\n", w, h);
|
||||
for (uint32_t y = 0; y < h; y++)
|
||||
for (uint32_t x = 0; x < w; x++) {
|
||||
const uint8_t *p = px + (size_t)y * stride + x * 4;
|
||||
uint8_t rgb[3] = {p[bgr ? 2 : 0], p[1], p[bgr ? 0 : 2]};
|
||||
fwrite(rgb, 1, 3, f);
|
||||
}
|
||||
fclose(f);
|
||||
VKB_INFO("dumped frame to %s", path);
|
||||
}
|
||||
if (map) munmap(map, maplen);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo)
|
||||
{
|
||||
const VkPresentRegionsKHR *regions = NULL;
|
||||
for (const VkBaseInStructure *b = pPresentInfo->pNext; b; b = b->pNext)
|
||||
if (b->sType == VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR) regions = (const void *)b;
|
||||
VkResult worst = VK_SUCCESS;
|
||||
for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
|
||||
vkb_swapchain *sc = chain(pPresentInfo->pSwapchains[i]);
|
||||
uint32_t index = pPresentInfo->pImageIndices[i];
|
||||
VkResult r = sc->status;
|
||||
if (index >= sc->count) r = VK_ERROR_OUT_OF_DATE_KHR;
|
||||
if (r >= 0) {
|
||||
r = vkb_wsi_wait_rendering(sc, queue, index, i == 0 ? pPresentInfo->waitSemaphoreCount : 0, pPresentInfo->pWaitSemaphores);
|
||||
}
|
||||
pthread_mutex_lock(&sc->lock);
|
||||
if (index < sc->count) {
|
||||
sc->images[index].acquired = 0;
|
||||
sc->images[index].busy = sc->dmabuf && !sc->copy_present;
|
||||
}
|
||||
pthread_mutex_unlock(&sc->lock);
|
||||
if (r >= 0 && getenv("VKBRIDGE_WSI_DUMP")) dump_frame(sc, index);
|
||||
if (r >= 0) {
|
||||
r = sc->backend->present(sc, index, regions && i < regions->swapchainCount ? ®ions->pRegions[i] : NULL);
|
||||
sc->presents++;
|
||||
}
|
||||
if (r < 0) {
|
||||
pthread_mutex_lock(&sc->lock);
|
||||
if (index < sc->count) sc->images[index].busy = 0;
|
||||
pthread_mutex_unlock(&sc->lock);
|
||||
if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_ERROR_SURFACE_LOST_KHR) sc->status = r;
|
||||
}
|
||||
if (r == VK_SUCCESS && sc->status == VK_SUBOPTIMAL_KHR) r = VK_SUBOPTIMAL_KHR;
|
||||
if (pPresentInfo->pResults) pPresentInfo->pResults[i] = r;
|
||||
if (r < 0 && (worst >= 0 || r == VK_ERROR_DEVICE_LOST)) worst = r;
|
||||
else if (worst == VK_SUCCESS && r == VK_SUBOPTIMAL_KHR) worst = r;
|
||||
}
|
||||
return worst;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ headless */
|
||||
|
||||
static VkResult hl_init(vkb_swapchain *sc) { (void)sc; return VK_SUCCESS; }
|
||||
static VkResult hl_present(vkb_swapchain *sc, uint32_t i, const VkPresentRegionKHR *r) { (void)sc; (void)i; (void)r; return VK_SUCCESS; }
|
||||
static VkResult hl_wait(vkb_swapchain *sc, uint64_t t) { (void)sc; (void)t; return VK_TIMEOUT; }
|
||||
static void hl_destroy(vkb_swapchain *sc) { (void)sc; }
|
||||
const vkb_wsi_backend vkb_wsi_headless_backend = {hl_init, hl_present, hl_wait, hl_destroy};
|
||||
@@ -0,0 +1,99 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Window-system integration implemented by the bridge client: the server's GPU has no Linux
|
||||
* window systems, so swapchains are built here from ordinary images and handed to the
|
||||
* compositor as dma-bufs (Wayland, zwp_linux_dmabuf_v1) or copied out through shared memory
|
||||
* (Wayland wl_shm, X11 PutImage), or not shown at all (VK_EXT_headless_surface).
|
||||
*/
|
||||
#ifndef VKB_WSI_H
|
||||
#define VKB_WSI_H
|
||||
|
||||
#include "vkb_client.h"
|
||||
|
||||
struct wl_display;
|
||||
struct wl_surface;
|
||||
|
||||
enum vkb_surface_kind { VKB_SURF_WAYLAND, VKB_SURF_XCB, VKB_SURF_HEADLESS };
|
||||
|
||||
typedef struct vkb_surface {
|
||||
enum vkb_surface_kind kind;
|
||||
struct wl_display *wl_display;
|
||||
struct wl_surface *wl_surface;
|
||||
void *xcb_conn; /* xcb_connection_t* */
|
||||
uint32_t xcb_window;
|
||||
struct vkb_wl_display_info *wl_info; /* cached formats/modifiers per wl_display */
|
||||
} vkb_surface;
|
||||
|
||||
typedef struct vkb_swapchain vkb_swapchain;
|
||||
|
||||
typedef struct vkb_swap_image {
|
||||
VkImage image;
|
||||
VkDeviceMemory memory;
|
||||
int busy; /* held by the compositor (or acquired by the app) */
|
||||
int acquired;
|
||||
void *backend; /* backend per-image data (wl_buffer...) */
|
||||
uint32_t stride, offset;
|
||||
uint64_t modifier;
|
||||
int dmabuf_fd;
|
||||
vkb_swapchain *chain;
|
||||
} vkb_swap_image;
|
||||
|
||||
typedef struct vkb_wsi_backend {
|
||||
/* Called after images exist; may fail (e.g. no dma-buf support) - the generic code then
|
||||
* retries with copy_present = 1. */
|
||||
VkResult (*init)(vkb_swapchain *sc);
|
||||
/* The image's rendering is complete (and, with copy_present, its pixels are in sc->staging). */
|
||||
VkResult (*present)(vkb_swapchain *sc, uint32_t index, const VkPresentRegionKHR *region);
|
||||
/* Wait until an image may be acquired (dispatching events); 0 timeout = poll. */
|
||||
VkResult (*wait_free)(vkb_swapchain *sc, uint64_t timeout_ns);
|
||||
void (*destroy)(vkb_swapchain *sc);
|
||||
} vkb_wsi_backend;
|
||||
|
||||
struct vkb_swapchain {
|
||||
vkb_device *dev;
|
||||
vkb_surface *surface;
|
||||
VkSwapchainCreateInfoKHR info;
|
||||
VkFormat *view_formats;
|
||||
uint32_t view_format_count;
|
||||
uint32_t count;
|
||||
vkb_swap_image *images;
|
||||
const vkb_wsi_backend *backend;
|
||||
void *bdata;
|
||||
int dmabuf; /* images are exported dma-bufs */
|
||||
int copy_present; /* pixels are read back into `staging` at present */
|
||||
/* Read-back resources (copy_present). */
|
||||
VkBuffer staging;
|
||||
VkDeviceMemory staging_mem;
|
||||
uint8_t *staging_ptr;
|
||||
VkDeviceSize staging_size;
|
||||
VkCommandPool pool;
|
||||
uint32_t pool_family;
|
||||
VkCommandBuffer *copy_cbs;
|
||||
VkFence fence;
|
||||
VkResult status; /* VK_ERROR_OUT_OF_DATE_KHR / SURFACE_LOST once broken */
|
||||
pthread_mutex_t lock;
|
||||
uint64_t presents;
|
||||
int retired;
|
||||
};
|
||||
|
||||
/* Formats every surface offers. */
|
||||
extern const VkSurfaceFormatKHR vkb_wsi_formats[];
|
||||
extern const uint32_t vkb_wsi_format_count;
|
||||
|
||||
/* Wayland */
|
||||
extern const vkb_wsi_backend vkb_wsi_wayland_backend;
|
||||
int vkb_wl_load(void);
|
||||
VkResult vkb_wl_surface_extent(vkb_surface *s, VkExtent2D *e);
|
||||
/* X11 */
|
||||
extern const vkb_wsi_backend vkb_wsi_x11_backend;
|
||||
int vkb_xcb_load(void);
|
||||
VkResult vkb_xcb_surface_extent(vkb_surface *s, VkExtent2D *e);
|
||||
void *vkb_xlib_to_xcb(void *dpy);
|
||||
/* Headless */
|
||||
extern const vkb_wsi_backend vkb_wsi_headless_backend;
|
||||
|
||||
/* Helpers in wsi.c */
|
||||
uint32_t vkb_drm_format(VkFormat f);
|
||||
VkResult vkb_wsi_wait_rendering(vkb_swapchain *sc, VkQueue queue, uint32_t index, uint32_t nwait, const VkSemaphore *wait);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,558 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Wayland presentation.
|
||||
*
|
||||
* - dma-buf: swapchain images are linear exportable images; each becomes a wl_buffer through
|
||||
* zwp_linux_dmabuf_v1 once, and a present is attach + commit (no copy). This is the path for
|
||||
* gamescope (and its WSI layer, which turns X11 games' swapchains into Wayland ones).
|
||||
* - wl_shm: for compositors without dma-buf (KWin compositing in software on the desktop): the
|
||||
* frame is read back and copied into a shared-memory buffer.
|
||||
*
|
||||
* libwayland-client is opened at run time, so the driver loads in programs that never touch
|
||||
* Wayland. Our objects live on a private event queue, like Mesa's WSI.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "wsi.h"
|
||||
#include "gen/vkb_client_gen.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* ------------------------------------------------------------------ libwayland-client */
|
||||
|
||||
struct wl_proxy;
|
||||
struct wl_event_queue;
|
||||
struct wl_message {
|
||||
const char *name;
|
||||
const char *signature;
|
||||
const struct wl_interface **types;
|
||||
};
|
||||
struct wl_interface {
|
||||
const char *name;
|
||||
int version;
|
||||
int method_count;
|
||||
const struct wl_message *methods;
|
||||
int event_count;
|
||||
const struct wl_message *events;
|
||||
};
|
||||
|
||||
static struct {
|
||||
void *lib;
|
||||
struct wl_proxy *(*proxy_marshal_constructor)(struct wl_proxy *, uint32_t, const struct wl_interface *, ...);
|
||||
struct wl_proxy *(*proxy_marshal_constructor_versioned)(struct wl_proxy *, uint32_t, const struct wl_interface *, uint32_t, ...);
|
||||
void (*proxy_marshal)(struct wl_proxy *, uint32_t, ...);
|
||||
int (*proxy_add_listener)(struct wl_proxy *, void (**)(void), void *);
|
||||
void (*proxy_destroy)(struct wl_proxy *);
|
||||
void *(*proxy_create_wrapper)(void *);
|
||||
void (*proxy_wrapper_destroy)(void *);
|
||||
void (*proxy_set_queue)(struct wl_proxy *, struct wl_event_queue *);
|
||||
uint32_t (*proxy_get_version)(struct wl_proxy *);
|
||||
struct wl_event_queue *(*display_create_queue)(struct wl_display *);
|
||||
void (*event_queue_destroy)(struct wl_event_queue *);
|
||||
int (*display_roundtrip_queue)(struct wl_display *, struct wl_event_queue *);
|
||||
int (*display_dispatch_queue_pending)(struct wl_display *, struct wl_event_queue *);
|
||||
int (*display_prepare_read_queue)(struct wl_display *, struct wl_event_queue *);
|
||||
int (*display_read_events)(struct wl_display *);
|
||||
void (*display_cancel_read)(struct wl_display *);
|
||||
int (*display_flush)(struct wl_display *);
|
||||
int (*display_get_fd)(struct wl_display *);
|
||||
int (*display_get_error)(struct wl_display *);
|
||||
const struct wl_interface *registry_iface, *callback_iface, *buffer_iface, *shm_iface, *shm_pool_iface;
|
||||
} wl;
|
||||
|
||||
int vkb_wl_load(void)
|
||||
{
|
||||
static int state;
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (!state) {
|
||||
wl.lib = dlopen("libwayland-client.so.0", RTLD_NOW | RTLD_LOCAL);
|
||||
state = -1;
|
||||
if (wl.lib) {
|
||||
#define L(f, n) (*(void **)&wl.f = dlsym(wl.lib, n))
|
||||
int ok = 1;
|
||||
ok &= !!L(proxy_marshal_constructor, "wl_proxy_marshal_constructor");
|
||||
ok &= !!L(proxy_marshal_constructor_versioned, "wl_proxy_marshal_constructor_versioned");
|
||||
ok &= !!L(proxy_marshal, "wl_proxy_marshal");
|
||||
ok &= !!L(proxy_add_listener, "wl_proxy_add_listener");
|
||||
ok &= !!L(proxy_destroy, "wl_proxy_destroy");
|
||||
ok &= !!L(proxy_create_wrapper, "wl_proxy_create_wrapper");
|
||||
ok &= !!L(proxy_wrapper_destroy, "wl_proxy_wrapper_destroy");
|
||||
ok &= !!L(proxy_set_queue, "wl_proxy_set_queue");
|
||||
ok &= !!L(proxy_get_version, "wl_proxy_get_version");
|
||||
ok &= !!L(display_create_queue, "wl_display_create_queue");
|
||||
ok &= !!L(event_queue_destroy, "wl_event_queue_destroy");
|
||||
ok &= !!L(display_roundtrip_queue, "wl_display_roundtrip_queue");
|
||||
ok &= !!L(display_dispatch_queue_pending, "wl_display_dispatch_queue_pending");
|
||||
ok &= !!L(display_prepare_read_queue, "wl_display_prepare_read_queue");
|
||||
ok &= !!L(display_read_events, "wl_display_read_events");
|
||||
ok &= !!L(display_cancel_read, "wl_display_cancel_read");
|
||||
ok &= !!L(display_flush, "wl_display_flush");
|
||||
ok &= !!L(display_get_fd, "wl_display_get_fd");
|
||||
ok &= !!L(display_get_error, "wl_display_get_error");
|
||||
ok &= !!L(registry_iface, "wl_registry_interface");
|
||||
ok &= !!L(callback_iface, "wl_callback_interface");
|
||||
ok &= !!L(buffer_iface, "wl_buffer_interface");
|
||||
ok &= !!L(shm_iface, "wl_shm_interface");
|
||||
ok &= !!L(shm_pool_iface, "wl_shm_pool_interface");
|
||||
#undef L
|
||||
if (ok) state = 1;
|
||||
else VKB_ERR("libwayland-client lacks functions this driver needs");
|
||||
} else {
|
||||
VKB_WARN("no libwayland-client: %s", dlerror());
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return state > 0;
|
||||
}
|
||||
|
||||
/* zwp_linux_dmabuf_v1 / zwp_linux_buffer_params_v1, as wayland-scanner would emit them. */
|
||||
static const struct wl_interface params_iface;
|
||||
static const struct wl_interface *dmabuf_types[8];
|
||||
static const struct wl_message dmabuf_requests[] = {
|
||||
{"destroy", "", dmabuf_types + 0},
|
||||
{"create_params", "n", dmabuf_types + 6},
|
||||
};
|
||||
static const struct wl_message dmabuf_events[] = {
|
||||
{"format", "u", dmabuf_types + 0},
|
||||
{"modifier", "3uuu", dmabuf_types + 0},
|
||||
};
|
||||
static const struct wl_interface dmabuf_iface = {"zwp_linux_dmabuf_v1", 3, 2, dmabuf_requests, 2, dmabuf_events};
|
||||
static const struct wl_message params_requests[] = {
|
||||
{"destroy", "", dmabuf_types + 0},
|
||||
{"add", "huuuuu", dmabuf_types + 0},
|
||||
{"create", "iiuu", dmabuf_types + 0},
|
||||
{"create_immed", "2niiuu", dmabuf_types + 7},
|
||||
};
|
||||
static const struct wl_message params_events[] = {
|
||||
{"created", "n", dmabuf_types + 7},
|
||||
{"failed", "", dmabuf_types + 0},
|
||||
};
|
||||
static const struct wl_interface params_iface = {"zwp_linux_buffer_params_v1", 3, 4, params_requests, 2, params_events};
|
||||
|
||||
static void init_types(void)
|
||||
{
|
||||
/* types[0..5] stay NULL (no object arguments); [6] = params, [7] = wl_buffer */
|
||||
dmabuf_types[6] = ¶ms_iface;
|
||||
dmabuf_types[7] = wl.buffer_iface;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ per-swapchain state */
|
||||
|
||||
#define WL_DISPLAY_GET_REGISTRY 1
|
||||
#define WL_REGISTRY_BIND 0
|
||||
#define WL_SURFACE_ATTACH 1
|
||||
#define WL_SURFACE_DAMAGE 2
|
||||
#define WL_SURFACE_FRAME 3
|
||||
#define WL_SURFACE_COMMIT 6
|
||||
#define WL_SURFACE_DAMAGE_BUFFER 9
|
||||
#define WL_BUFFER_DESTROY 0
|
||||
#define WL_SHM_CREATE_POOL 0
|
||||
#define WL_SHM_POOL_CREATE_BUFFER 0
|
||||
#define WL_SHM_POOL_DESTROY 1
|
||||
#define DMABUF_DESTROY 0
|
||||
#define DMABUF_CREATE_PARAMS 1
|
||||
#define PARAMS_DESTROY 0
|
||||
#define PARAMS_ADD 1
|
||||
#define PARAMS_CREATE_IMMED 3
|
||||
#define WL_SHM_FORMAT_ARGB8888 0u
|
||||
#define WL_SHM_FORMAT_XRGB8888 1u
|
||||
#define FOURCC(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
|
||||
#define DRM_XRGB8888 FOURCC('X', 'R', '2', '4')
|
||||
#define DRM_ARGB8888 FOURCC('A', 'R', '2', '4')
|
||||
#define DRM_XBGR8888 FOURCC('X', 'B', '2', '4')
|
||||
#define DRM_ABGR8888 FOURCC('A', 'B', '2', '4')
|
||||
#define MAX_FMTS 64
|
||||
|
||||
typedef struct shm_buf {
|
||||
struct wl_proxy *buffer;
|
||||
uint8_t *ptr;
|
||||
int busy;
|
||||
} shm_buf;
|
||||
|
||||
typedef struct wl_state {
|
||||
struct wl_display *display;
|
||||
struct wl_event_queue *queue;
|
||||
void *display_wrapper;
|
||||
void *surface_wrapper;
|
||||
struct wl_proxy *registry;
|
||||
struct wl_proxy *shm;
|
||||
struct wl_proxy *dmabuf;
|
||||
uint32_t dmabuf_version;
|
||||
uint32_t shm_formats[MAX_FMTS];
|
||||
int nshm;
|
||||
struct { uint32_t format; uint64_t modifier; } dmabuf_mods[256];
|
||||
int nmods;
|
||||
uint32_t dmabuf_formats[MAX_FMTS];
|
||||
int nformats;
|
||||
struct wl_proxy **buffers; /* dma-buf wl_buffers, one per image */
|
||||
shm_buf *shm_bufs;
|
||||
int nshm_bufs;
|
||||
struct wl_proxy *shm_pool;
|
||||
uint8_t *shm_map;
|
||||
size_t shm_size;
|
||||
uint32_t shm_stride, shm_format;
|
||||
int swizzle; /* RGBA frame into an ARGB/XRGB shm buffer */
|
||||
struct wl_proxy *frame; /* pending frame callback */
|
||||
vkb_swapchain *sc;
|
||||
} wl_state;
|
||||
|
||||
static void registry_global(void *data, struct wl_proxy *reg, uint32_t name, const char *iface, uint32_t version)
|
||||
{
|
||||
wl_state *s = data;
|
||||
if (!strcmp(iface, "wl_shm") && !s->shm) {
|
||||
s->shm = wl.proxy_marshal_constructor_versioned(reg, WL_REGISTRY_BIND, wl.shm_iface, 1, name, wl.shm_iface->name, 1, NULL);
|
||||
} else if (!strcmp(iface, "zwp_linux_dmabuf_v1") && !s->dmabuf && version >= 2) {
|
||||
uint32_t v = version < 3 ? version : 3;
|
||||
s->dmabuf = wl.proxy_marshal_constructor_versioned(reg, WL_REGISTRY_BIND, &dmabuf_iface, v, name, dmabuf_iface.name, v, NULL);
|
||||
s->dmabuf_version = v;
|
||||
}
|
||||
}
|
||||
|
||||
static void registry_global_remove(void *data, struct wl_proxy *reg, uint32_t name)
|
||||
{
|
||||
(void)data;
|
||||
(void)reg;
|
||||
(void)name;
|
||||
}
|
||||
|
||||
static const void *registry_listener[] = {(void *)registry_global, (void *)registry_global_remove};
|
||||
|
||||
static void shm_format(void *data, struct wl_proxy *shm, uint32_t format)
|
||||
{
|
||||
(void)shm;
|
||||
wl_state *s = data;
|
||||
if (s->nshm < MAX_FMTS) s->shm_formats[s->nshm++] = format;
|
||||
}
|
||||
|
||||
static const void *shm_listener[] = {(void *)shm_format};
|
||||
|
||||
static void dmabuf_format(void *data, struct wl_proxy *d, uint32_t format)
|
||||
{
|
||||
(void)d;
|
||||
wl_state *s = data;
|
||||
if (s->nformats < MAX_FMTS) s->dmabuf_formats[s->nformats++] = format;
|
||||
}
|
||||
|
||||
static void dmabuf_modifier(void *data, struct wl_proxy *d, uint32_t format, uint32_t hi, uint32_t lo)
|
||||
{
|
||||
(void)d;
|
||||
wl_state *s = data;
|
||||
if (s->nmods < 256) {
|
||||
s->dmabuf_mods[s->nmods].format = format;
|
||||
s->dmabuf_mods[s->nmods].modifier = ((uint64_t)hi << 32) | lo;
|
||||
s->nmods++;
|
||||
}
|
||||
}
|
||||
|
||||
static const void *dmabuf_listener[] = {(void *)dmabuf_format, (void *)dmabuf_modifier};
|
||||
|
||||
static void buffer_release(void *data, struct wl_proxy *buffer)
|
||||
{
|
||||
(void)buffer;
|
||||
vkb_swap_image *img = data;
|
||||
pthread_mutex_lock(&img->chain->lock);
|
||||
img->busy = 0;
|
||||
pthread_mutex_unlock(&img->chain->lock);
|
||||
}
|
||||
|
||||
static const void *buffer_listener[] = {(void *)buffer_release};
|
||||
|
||||
static void shm_buffer_release(void *data, struct wl_proxy *buffer)
|
||||
{
|
||||
(void)buffer;
|
||||
((shm_buf *)data)->busy = 0;
|
||||
}
|
||||
|
||||
static const void *shm_buffer_listener[] = {(void *)shm_buffer_release};
|
||||
|
||||
static void frame_done(void *data, struct wl_proxy *cb, uint32_t t)
|
||||
{
|
||||
(void)t;
|
||||
wl_state *s = data;
|
||||
if (s->frame == cb) s->frame = NULL;
|
||||
wl.proxy_destroy(cb);
|
||||
}
|
||||
|
||||
static const void *frame_listener[] = {(void *)frame_done};
|
||||
|
||||
/* Dispatch our queue, waiting up to timeout_ns for events. -1 on a dead connection. */
|
||||
static int dispatch_timeout(wl_state *s, uint64_t timeout_ns)
|
||||
{
|
||||
if (wl.display_dispatch_queue_pending(s->display, s->queue) < 0) return -1;
|
||||
while (wl.display_prepare_read_queue(s->display, s->queue) != 0) {
|
||||
if (wl.display_dispatch_queue_pending(s->display, s->queue) < 0) return -1;
|
||||
}
|
||||
if (wl.display_flush(s->display) < 0 && errno != EAGAIN) {
|
||||
wl.display_cancel_read(s->display);
|
||||
return -1;
|
||||
}
|
||||
struct pollfd p = {wl.display_get_fd(s->display), POLLIN, 0};
|
||||
int ms = timeout_ns == UINT64_MAX ? -1 : (int)((timeout_ns + 999999) / 1000000);
|
||||
int r = poll(&p, 1, ms);
|
||||
if (r <= 0) {
|
||||
wl.display_cancel_read(s->display);
|
||||
return r < 0 && errno != EINTR ? -1 : 0;
|
||||
}
|
||||
if (wl.display_read_events(s->display) < 0) return -1;
|
||||
return wl.display_dispatch_queue_pending(s->display, s->queue) < 0 ? -1 : 1;
|
||||
}
|
||||
|
||||
static int has_u32(const uint32_t *a, int n, uint32_t v)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
if (a[i] == v) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dmabuf_supports(wl_state *s, uint32_t fourcc, uint64_t modifier)
|
||||
{
|
||||
if (s->nmods) {
|
||||
for (int i = 0; i < s->nmods; i++) {
|
||||
/* DRM_FORMAT_MOD_INVALID means "implicit": treat as linear for our linear buffers. */
|
||||
if (s->dmabuf_mods[i].format == fourcc &&
|
||||
(s->dmabuf_mods[i].modifier == modifier || s->dmabuf_mods[i].modifier == 0x00ffffffffffffffull))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return has_u32(s->dmabuf_formats, s->nformats, fourcc);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ backend */
|
||||
|
||||
VkResult vkb_wl_surface_extent(vkb_surface *s, VkExtent2D *e)
|
||||
{
|
||||
(void)s;
|
||||
e->width = e->height = 0xFFFFFFFFu;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static void wl_teardown(wl_state *s);
|
||||
|
||||
static VkResult wl_init(vkb_swapchain *sc)
|
||||
{
|
||||
vkb_surface *surface = sc->surface;
|
||||
wl_state *s = calloc(1, sizeof(*s));
|
||||
sc->bdata = s;
|
||||
s->sc = sc;
|
||||
s->display = surface->wl_display;
|
||||
init_types();
|
||||
s->queue = wl.display_create_queue(s->display);
|
||||
s->display_wrapper = wl.proxy_create_wrapper(s->display);
|
||||
wl.proxy_set_queue(s->display_wrapper, s->queue);
|
||||
s->surface_wrapper = wl.proxy_create_wrapper(surface->wl_surface);
|
||||
wl.proxy_set_queue(s->surface_wrapper, s->queue);
|
||||
s->registry = wl.proxy_marshal_constructor(s->display_wrapper, WL_DISPLAY_GET_REGISTRY, wl.registry_iface, NULL);
|
||||
wl.proxy_add_listener(s->registry, (void (**)(void))registry_listener, s);
|
||||
if (wl.display_roundtrip_queue(s->display, s->queue) < 0) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
if (s->shm) wl.proxy_add_listener(s->shm, (void (**)(void))shm_listener, s);
|
||||
if (s->dmabuf) wl.proxy_add_listener(s->dmabuf, (void (**)(void))dmabuf_listener, s);
|
||||
if (wl.display_roundtrip_queue(s->display, s->queue) < 0) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
|
||||
uint32_t w = sc->info.imageExtent.width, h = sc->info.imageExtent.height;
|
||||
int opaque = sc->info.compositeAlpha == VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
int rgba = sc->info.imageFormat == VK_FORMAT_R8G8B8A8_UNORM || sc->info.imageFormat == VK_FORMAT_R8G8B8A8_SRGB;
|
||||
if (!sc->copy_present) {
|
||||
if (!s->dmabuf) {
|
||||
VKB_INFO("Wayland: the compositor has no zwp_linux_dmabuf_v1");
|
||||
return VK_ERROR_FEATURE_NOT_PRESENT;
|
||||
}
|
||||
uint32_t fourcc = rgba ? (opaque ? DRM_XBGR8888 : DRM_ABGR8888) : (opaque ? DRM_XRGB8888 : DRM_ARGB8888);
|
||||
uint64_t mod = sc->images[0].modifier;
|
||||
if (!dmabuf_supports(s, fourcc, mod)) {
|
||||
uint32_t alt = rgba ? DRM_ABGR8888 : DRM_ARGB8888;
|
||||
if (!dmabuf_supports(s, alt, mod)) {
|
||||
VKB_INFO("Wayland: the compositor takes no %.4s dma-buf with modifier 0x%llx", (char *)&fourcc, (unsigned long long)mod);
|
||||
return VK_ERROR_FORMAT_NOT_SUPPORTED;
|
||||
}
|
||||
fourcc = alt;
|
||||
}
|
||||
s->buffers = calloc(sc->count, sizeof(*s->buffers));
|
||||
for (uint32_t i = 0; i < sc->count; i++) {
|
||||
vkb_swap_image *img = &sc->images[i];
|
||||
struct wl_proxy *params = wl.proxy_marshal_constructor(s->dmabuf, DMABUF_CREATE_PARAMS, ¶ms_iface, NULL);
|
||||
wl.proxy_marshal(params, PARAMS_ADD, img->dmabuf_fd, 0u, img->offset, img->stride, (uint32_t)(img->modifier >> 32),
|
||||
(uint32_t)(img->modifier & 0xffffffffu));
|
||||
s->buffers[i] = wl.proxy_marshal_constructor(params, PARAMS_CREATE_IMMED, wl.buffer_iface, NULL, (int32_t)w, (int32_t)h, fourcc, 0u);
|
||||
wl.proxy_marshal(params, PARAMS_DESTROY);
|
||||
wl.proxy_destroy(params);
|
||||
wl.proxy_add_listener(s->buffers[i], (void (**)(void))buffer_listener, img);
|
||||
}
|
||||
if (wl.display_roundtrip_queue(s->display, s->queue) < 0 || wl.display_get_error(s->display)) {
|
||||
VKB_ERR("Wayland: the compositor refused the dma-bufs");
|
||||
return VK_ERROR_SURFACE_LOST_KHR;
|
||||
}
|
||||
VKB_INFO("Wayland: %u dma-buf buffers %ux%u %.4s, stride %u, modifier 0x%llx", sc->count, w, h, (char *)&fourcc,
|
||||
sc->images[0].stride, (unsigned long long)sc->images[0].modifier);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
if (!s->shm) {
|
||||
VKB_ERR("Wayland: the compositor offers neither dma-buf nor wl_shm");
|
||||
return VK_ERROR_SURFACE_LOST_KHR;
|
||||
}
|
||||
uint32_t fmt;
|
||||
if (rgba && has_u32(s->shm_formats, s->nshm, opaque ? DRM_XBGR8888 : DRM_ABGR8888)) {
|
||||
fmt = opaque ? DRM_XBGR8888 : DRM_ABGR8888;
|
||||
} else {
|
||||
fmt = opaque ? WL_SHM_FORMAT_XRGB8888 : WL_SHM_FORMAT_ARGB8888;
|
||||
s->swizzle = rgba;
|
||||
}
|
||||
s->shm_format = fmt;
|
||||
s->shm_stride = w * 4;
|
||||
s->nshm_bufs = (int)sc->count + 1;
|
||||
size_t one = (size_t)s->shm_stride * h;
|
||||
s->shm_size = one * (size_t)s->nshm_bufs;
|
||||
int fd = vkb_memfd("vkbridge-wl-shm", s->shm_size);
|
||||
if (fd < 0) return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
s->shm_map = mmap(NULL, s->shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (s->shm_map == MAP_FAILED) {
|
||||
s->shm_map = NULL;
|
||||
close(fd);
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
s->shm_pool = wl.proxy_marshal_constructor(s->shm, WL_SHM_CREATE_POOL, wl.shm_pool_iface, NULL, fd, (int32_t)s->shm_size);
|
||||
close(fd);
|
||||
s->shm_bufs = calloc((size_t)s->nshm_bufs, sizeof(shm_buf));
|
||||
for (int i = 0; i < s->nshm_bufs; i++) {
|
||||
s->shm_bufs[i].ptr = s->shm_map + one * (size_t)i;
|
||||
s->shm_bufs[i].buffer = wl.proxy_marshal_constructor(s->shm_pool, WL_SHM_POOL_CREATE_BUFFER, wl.buffer_iface, NULL,
|
||||
(int32_t)(one * (size_t)i), (int32_t)w, (int32_t)h, (int32_t)s->shm_stride, fmt);
|
||||
wl.proxy_add_listener(s->shm_bufs[i].buffer, (void (**)(void))shm_buffer_listener, &s->shm_bufs[i]);
|
||||
}
|
||||
VKB_INFO("Wayland: presenting %ux%u through wl_shm (format 0x%x%s)", w, h, fmt, s->swizzle ? ", swizzled" : "");
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static void copy_frame(wl_state *s, const uint8_t *src, uint8_t *dst, uint32_t w, uint32_t h)
|
||||
{
|
||||
size_t row = (size_t)w * 4;
|
||||
if (!s->swizzle) {
|
||||
memcpy(dst, src, row * h);
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < (size_t)w * h; i++) {
|
||||
dst[i * 4 + 0] = src[i * 4 + 2];
|
||||
dst[i * 4 + 1] = src[i * 4 + 1];
|
||||
dst[i * 4 + 2] = src[i * 4 + 0];
|
||||
dst[i * 4 + 3] = src[i * 4 + 3];
|
||||
}
|
||||
}
|
||||
|
||||
static VkResult wl_present(vkb_swapchain *sc, uint32_t index, const VkPresentRegionKHR *region)
|
||||
{
|
||||
wl_state *s = sc->bdata;
|
||||
uint32_t w = sc->info.imageExtent.width, h = sc->info.imageExtent.height;
|
||||
if (wl.display_get_error(s->display)) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
/* FIFO: one frame per compositor frame callback (bounded, so a hidden window cannot hang
|
||||
* the game forever). */
|
||||
if (sc->info.presentMode == VK_PRESENT_MODE_FIFO_KHR || sc->info.presentMode == VK_PRESENT_MODE_FIFO_RELAXED_KHR) {
|
||||
struct timespec t0;
|
||||
clock_gettime(CLOCK_MONOTONIC, &t0);
|
||||
while (s->frame) {
|
||||
if (dispatch_timeout(s, 20000000ull) < 0) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
struct timespec t1;
|
||||
clock_gettime(CLOCK_MONOTONIC, &t1);
|
||||
if ((t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000 > 250) break;
|
||||
}
|
||||
}
|
||||
struct wl_proxy *buffer;
|
||||
if (!sc->copy_present) {
|
||||
buffer = s->buffers[index];
|
||||
} else {
|
||||
shm_buf *b = NULL;
|
||||
for (int tries = 0; !b && tries < 50; tries++) {
|
||||
for (int i = 0; i < s->nshm_bufs; i++)
|
||||
if (!s->shm_bufs[i].busy) {
|
||||
b = &s->shm_bufs[i];
|
||||
break;
|
||||
}
|
||||
if (!b && dispatch_timeout(s, 10000000ull) < 0) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
}
|
||||
if (!b) return VK_SUCCESS; /* compositor holds every buffer: drop this frame */
|
||||
copy_frame(s, sc->staging_ptr, b->ptr, w, h);
|
||||
b->busy = 1;
|
||||
buffer = b->buffer;
|
||||
}
|
||||
if (sc->info.presentMode == VK_PRESENT_MODE_FIFO_KHR || sc->info.presentMode == VK_PRESENT_MODE_FIFO_RELAXED_KHR) {
|
||||
if (s->frame) wl.proxy_destroy(s->frame);
|
||||
s->frame = wl.proxy_marshal_constructor(s->surface_wrapper, WL_SURFACE_FRAME, wl.callback_iface, NULL);
|
||||
wl.proxy_add_listener(s->frame, (void (**)(void))frame_listener, s);
|
||||
}
|
||||
wl.proxy_marshal(s->surface_wrapper, WL_SURFACE_ATTACH, buffer, 0, 0);
|
||||
int v4 = wl.proxy_get_version(s->surface_wrapper) >= 4;
|
||||
if (region && region->rectangleCount && region->pRectangles) {
|
||||
for (uint32_t i = 0; i < region->rectangleCount; i++) {
|
||||
const VkRectLayerKHR *r = ®ion->pRectangles[i];
|
||||
wl.proxy_marshal(s->surface_wrapper, v4 ? WL_SURFACE_DAMAGE_BUFFER : WL_SURFACE_DAMAGE, r->offset.x, r->offset.y,
|
||||
(int32_t)r->extent.width, (int32_t)r->extent.height);
|
||||
}
|
||||
} else {
|
||||
wl.proxy_marshal(s->surface_wrapper, v4 ? WL_SURFACE_DAMAGE_BUFFER : WL_SURFACE_DAMAGE, 0, 0, (int32_t)w, (int32_t)h);
|
||||
}
|
||||
wl.proxy_marshal(s->surface_wrapper, WL_SURFACE_COMMIT);
|
||||
wl.display_flush(s->display);
|
||||
if (wl.display_dispatch_queue_pending(s->display, s->queue) < 0) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VkResult wl_wait_free(vkb_swapchain *sc, uint64_t timeout_ns)
|
||||
{
|
||||
wl_state *s = sc->bdata;
|
||||
int r = dispatch_timeout(s, timeout_ns);
|
||||
if (r < 0) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
return r == 0 ? VK_TIMEOUT : VK_SUCCESS;
|
||||
}
|
||||
|
||||
static void wl_teardown(wl_state *s)
|
||||
{
|
||||
if (!s) return;
|
||||
vkb_swapchain *sc = s->sc;
|
||||
if (s->frame) wl.proxy_destroy(s->frame);
|
||||
if (s->buffers) {
|
||||
for (uint32_t i = 0; i < sc->count; i++)
|
||||
if (s->buffers[i]) {
|
||||
wl.proxy_marshal(s->buffers[i], WL_BUFFER_DESTROY);
|
||||
wl.proxy_destroy(s->buffers[i]);
|
||||
}
|
||||
free(s->buffers);
|
||||
}
|
||||
if (s->shm_bufs) {
|
||||
for (int i = 0; i < s->nshm_bufs; i++)
|
||||
if (s->shm_bufs[i].buffer) {
|
||||
wl.proxy_marshal(s->shm_bufs[i].buffer, WL_BUFFER_DESTROY);
|
||||
wl.proxy_destroy(s->shm_bufs[i].buffer);
|
||||
}
|
||||
free(s->shm_bufs);
|
||||
}
|
||||
if (s->shm_pool) {
|
||||
wl.proxy_marshal(s->shm_pool, WL_SHM_POOL_DESTROY);
|
||||
wl.proxy_destroy(s->shm_pool);
|
||||
}
|
||||
if (s->shm_map) munmap(s->shm_map, s->shm_size);
|
||||
if (s->dmabuf) {
|
||||
wl.proxy_marshal(s->dmabuf, DMABUF_DESTROY);
|
||||
wl.proxy_destroy(s->dmabuf);
|
||||
}
|
||||
if (s->shm) wl.proxy_destroy(s->shm);
|
||||
if (s->registry) wl.proxy_destroy(s->registry);
|
||||
if (s->surface_wrapper) wl.proxy_wrapper_destroy(s->surface_wrapper);
|
||||
if (s->display_wrapper) wl.proxy_wrapper_destroy(s->display_wrapper);
|
||||
if (s->display) wl.display_flush(s->display);
|
||||
if (s->queue) wl.event_queue_destroy(s->queue);
|
||||
free(s);
|
||||
}
|
||||
|
||||
static void wl_destroy(vkb_swapchain *sc)
|
||||
{
|
||||
wl_teardown(sc->bdata);
|
||||
sc->bdata = NULL;
|
||||
}
|
||||
|
||||
const vkb_wsi_backend vkb_wsi_wayland_backend = {wl_init, wl_present, wl_wait_free, wl_destroy};
|
||||
@@ -0,0 +1,162 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* X11 presentation, software path: the frame is read back and drawn with PutImage. Used only
|
||||
* when nothing better exists - under gamescope its WSI layer turns X11 swapchains into Wayland
|
||||
* ones before they reach this driver. libxcb (and libX11-xcb for Xlib surfaces) is opened at
|
||||
* run time.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "wsi.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <string.h>
|
||||
#include <xcb/xcb.h>
|
||||
|
||||
static struct {
|
||||
void *lib, *x11xcb;
|
||||
xcb_get_geometry_cookie_t (*get_geometry)(xcb_connection_t *, xcb_drawable_t);
|
||||
xcb_get_geometry_reply_t *(*get_geometry_reply)(xcb_connection_t *, xcb_get_geometry_cookie_t, xcb_generic_error_t **);
|
||||
xcb_void_cookie_t (*put_image)(xcb_connection_t *, uint8_t, xcb_drawable_t, xcb_gcontext_t, uint16_t, uint16_t, int16_t, int16_t,
|
||||
uint8_t, uint8_t, uint32_t, const uint8_t *);
|
||||
xcb_void_cookie_t (*create_gc)(xcb_connection_t *, xcb_gcontext_t, xcb_drawable_t, uint32_t, const void *);
|
||||
xcb_void_cookie_t (*free_gc)(xcb_connection_t *, xcb_gcontext_t);
|
||||
uint32_t (*generate_id)(xcb_connection_t *);
|
||||
uint32_t (*get_maximum_request_length)(xcb_connection_t *);
|
||||
int (*flush)(xcb_connection_t *);
|
||||
int (*connection_has_error)(xcb_connection_t *);
|
||||
xcb_connection_t *(*xlib_get_xcb)(void *);
|
||||
} x;
|
||||
|
||||
int vkb_xcb_load(void)
|
||||
{
|
||||
static int state;
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (!state) {
|
||||
state = -1;
|
||||
x.lib = dlopen("libxcb.so.1", RTLD_NOW | RTLD_LOCAL);
|
||||
if (x.lib) {
|
||||
#define L(f, n) (*(void **)&x.f = dlsym(x.lib, n))
|
||||
int ok = 1;
|
||||
ok &= !!L(get_geometry, "xcb_get_geometry");
|
||||
ok &= !!L(get_geometry_reply, "xcb_get_geometry_reply");
|
||||
ok &= !!L(put_image, "xcb_put_image");
|
||||
ok &= !!L(create_gc, "xcb_create_gc");
|
||||
ok &= !!L(free_gc, "xcb_free_gc");
|
||||
ok &= !!L(generate_id, "xcb_generate_id");
|
||||
ok &= !!L(get_maximum_request_length, "xcb_get_maximum_request_length");
|
||||
ok &= !!L(flush, "xcb_flush");
|
||||
ok &= !!L(connection_has_error, "xcb_connection_has_error");
|
||||
#undef L
|
||||
if (ok) state = 1;
|
||||
}
|
||||
x.x11xcb = dlopen("libX11-xcb.so.1", RTLD_NOW | RTLD_LOCAL);
|
||||
if (x.x11xcb) *(void **)&x.xlib_get_xcb = dlsym(x.x11xcb, "XGetXCBConnection");
|
||||
if (state < 0) VKB_WARN("no usable libxcb: X11 presentation unavailable");
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return state > 0;
|
||||
}
|
||||
|
||||
void *vkb_xlib_to_xcb(void *dpy)
|
||||
{
|
||||
if (!vkb_xcb_load() || !x.xlib_get_xcb) return NULL;
|
||||
return x.xlib_get_xcb(dpy);
|
||||
}
|
||||
|
||||
static int geometry(vkb_surface *s, uint32_t *w, uint32_t *h, uint8_t *depth)
|
||||
{
|
||||
xcb_get_geometry_reply_t *g = x.get_geometry_reply(s->xcb_conn, x.get_geometry(s->xcb_conn, s->xcb_window), NULL);
|
||||
if (!g) return 0;
|
||||
*w = g->width;
|
||||
*h = g->height;
|
||||
if (depth) *depth = g->depth;
|
||||
free(g);
|
||||
return 1;
|
||||
}
|
||||
|
||||
VkResult vkb_xcb_surface_extent(vkb_surface *s, VkExtent2D *e)
|
||||
{
|
||||
uint32_t w, h;
|
||||
if (!geometry(s, &w, &h, NULL)) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
e->width = w;
|
||||
e->height = h;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
typedef struct x11_state {
|
||||
xcb_gcontext_t gc;
|
||||
uint8_t depth;
|
||||
uint8_t *swz;
|
||||
} x11_state;
|
||||
|
||||
static VkResult x11_init(vkb_swapchain *sc)
|
||||
{
|
||||
vkb_surface *s = sc->surface;
|
||||
x11_state *st = calloc(1, sizeof(*st));
|
||||
sc->bdata = st;
|
||||
uint32_t w, h;
|
||||
if (!geometry(s, &w, &h, &st->depth)) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
if (st->depth != 24 && st->depth != 32) {
|
||||
VKB_ERR("X11: window depth %u is not supported by the software present path", st->depth);
|
||||
return VK_ERROR_SURFACE_LOST_KHR;
|
||||
}
|
||||
st->gc = x.generate_id(s->xcb_conn);
|
||||
x.create_gc(s->xcb_conn, st->gc, s->xcb_window, 0, NULL);
|
||||
VKB_INFO("X11: presenting %ux%u by PutImage (software path)", sc->info.imageExtent.width, sc->info.imageExtent.height);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VkResult x11_present(vkb_swapchain *sc, uint32_t index, const VkPresentRegionKHR *region)
|
||||
{
|
||||
(void)index;
|
||||
(void)region;
|
||||
vkb_surface *s = sc->surface;
|
||||
x11_state *st = sc->bdata;
|
||||
if (x.connection_has_error(s->xcb_conn)) return VK_ERROR_SURFACE_LOST_KHR;
|
||||
uint32_t w = sc->info.imageExtent.width, h = sc->info.imageExtent.height;
|
||||
const uint8_t *src = sc->staging_ptr;
|
||||
int rgba = sc->info.imageFormat == VK_FORMAT_R8G8B8A8_UNORM || sc->info.imageFormat == VK_FORMAT_R8G8B8A8_SRGB;
|
||||
if (rgba) {
|
||||
if (!st->swz) st->swz = malloc((size_t)w * h * 4);
|
||||
for (size_t i = 0; i < (size_t)w * h; i++) {
|
||||
st->swz[i * 4 + 0] = src[i * 4 + 2];
|
||||
st->swz[i * 4 + 1] = src[i * 4 + 1];
|
||||
st->swz[i * 4 + 2] = src[i * 4 + 0];
|
||||
st->swz[i * 4 + 3] = src[i * 4 + 3];
|
||||
}
|
||||
src = st->swz;
|
||||
}
|
||||
/* Split into requests that fit the server's maximum request length (in 4-byte units). */
|
||||
size_t max = (size_t)x.get_maximum_request_length(s->xcb_conn) * 4;
|
||||
size_t row = (size_t)w * 4;
|
||||
uint32_t rows = max > 64 + row ? (uint32_t)((max - 64) / row) : 1;
|
||||
for (uint32_t y = 0; y < h; y += rows) {
|
||||
uint32_t n = h - y < rows ? h - y : rows;
|
||||
x.put_image(s->xcb_conn, XCB_IMAGE_FORMAT_Z_PIXMAP, s->xcb_window, st->gc, (uint16_t)w, (uint16_t)n, 0, (int16_t)y, 0, st->depth,
|
||||
(uint32_t)(row * n), src + row * y);
|
||||
}
|
||||
x.flush(s->xcb_conn);
|
||||
uint32_t cw, ch;
|
||||
if (geometry(s, &cw, &ch, NULL) && (cw != w || ch != h)) sc->status = VK_SUBOPTIMAL_KHR;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VkResult x11_wait_free(vkb_swapchain *sc, uint64_t timeout_ns)
|
||||
{
|
||||
(void)sc;
|
||||
(void)timeout_ns;
|
||||
return VK_TIMEOUT; /* images are free as soon as their present returns */
|
||||
}
|
||||
|
||||
static void x11_destroy(vkb_swapchain *sc)
|
||||
{
|
||||
x11_state *st = sc->bdata;
|
||||
if (!st) return;
|
||||
if (st->gc) x.free_gc(sc->surface->xcb_conn, st->gc);
|
||||
free(st->swz);
|
||||
free(st);
|
||||
sc->bdata = NULL;
|
||||
}
|
||||
|
||||
const vkb_wsi_backend vkb_wsi_x11_backend = {x11_init, x11_present, x11_wait_free, x11_destroy};
|
||||
Reference in new issue
Block a user