mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
feat(vkbridge): Vulkan bridge core - codegen, wire runtime, server, client ICD, WSI
Generated from vk.xml 1.3.247; server shares host-visible memory via a self-tested strategy (hostptr/dmabuf/ahb). vulkaninfo runs through it on host. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KZqR8XUE8czvGzEpgSCVQR
This commit is contained in:
1 parent
8ac555664a
commit
a8fd343fc1
67 files changed
+82811
No files matched your search
@@ -0,0 +1,332 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Connections: one thread per client thread, requests executed in order, replies sent back.
|
||||
* A command buffer's recorded commands arrive in one message at vkEndCommandBuffer and are
|
||||
* replayed here.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_server.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static pthread_mutex_t tables_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static vkb_srv_table *tables[VKB_MAX_TABLES];
|
||||
static uint32_t next_table_id = 1;
|
||||
|
||||
static pthread_mutex_t procs_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static vkb_srv_proc *procs;
|
||||
|
||||
static __thread vkb_srv_table *tls_table;
|
||||
|
||||
vkb_srv_table *vkb_srv_current_table(void) { return tls_table; }
|
||||
void vkb_srv_set_current_table(vkb_srv_table *t) { tls_table = t; }
|
||||
|
||||
/* Server side of the generated encoders' helpers: handles are already the server's values. */
|
||||
uint64_t vkb_remote(const void *h) { return (uint64_t)(uintptr_t)h; }
|
||||
int vkb_cb_begin_is_secondary(const VkCommandBufferBeginInfo *s) { (void)s; return 1; }
|
||||
|
||||
uint32_t vkb_table_register(vkb_srv_table *t)
|
||||
{
|
||||
pthread_mutex_lock(&tables_lock);
|
||||
uint32_t id = 0;
|
||||
for (uint32_t tries = 0; tries < VKB_MAX_TABLES; tries++) {
|
||||
uint32_t cand = next_table_id++;
|
||||
if (next_table_id >= VKB_MAX_TABLES) next_table_id = 1;
|
||||
if (!tables[cand]) {
|
||||
id = cand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (id) {
|
||||
t->id = id;
|
||||
tables[id] = t;
|
||||
}
|
||||
pthread_mutex_unlock(&tables_lock);
|
||||
return id;
|
||||
}
|
||||
|
||||
void vkb_table_unregister(vkb_srv_table *t)
|
||||
{
|
||||
pthread_mutex_lock(&tables_lock);
|
||||
if (t->id && t->id < VKB_MAX_TABLES && tables[t->id] == t) tables[t->id] = NULL;
|
||||
pthread_mutex_unlock(&tables_lock);
|
||||
}
|
||||
|
||||
vkb_srv_table *vkb_table_get(uint32_t id)
|
||||
{
|
||||
if (!id || id >= VKB_MAX_TABLES) return NULL;
|
||||
pthread_mutex_lock(&tables_lock);
|
||||
vkb_srv_table *t = tables[id];
|
||||
pthread_mutex_unlock(&tables_lock);
|
||||
return t;
|
||||
}
|
||||
|
||||
void vkb_srv_reply_status(vkb_srv_call *c, uint32_t status)
|
||||
{
|
||||
c->status = status;
|
||||
}
|
||||
|
||||
void vkb_srv_bad_message(vkb_srv_call *c)
|
||||
{
|
||||
VKB_ERR("malformed request");
|
||||
c->status = VKB_ST_BAD_MESSAGE;
|
||||
}
|
||||
|
||||
void vkb_srv_missing(vkb_srv_call *c, const char *name)
|
||||
{
|
||||
VKB_ERR("the driver has no %s", name);
|
||||
c->status = VKB_ST_MISSING;
|
||||
}
|
||||
|
||||
void vkb_srv_close_after_send(vkb_srv_call *c, int fd)
|
||||
{
|
||||
if (c->nclose < VKB_MAX_FDS) c->close_after[c->nclose++] = fd;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ process cleanup */
|
||||
|
||||
void vkb_proc_cleanup(vkb_srv_proc *p)
|
||||
{
|
||||
/* Devices first, then their instances. */
|
||||
for (int pass = 0; pass < 2; pass++) {
|
||||
for (uint32_t i = 1; i < VKB_MAX_TABLES; i++) {
|
||||
pthread_mutex_lock(&tables_lock);
|
||||
vkb_srv_table *t = tables[i];
|
||||
int mine = t && t->proc == p && t->is_device == (pass == 0);
|
||||
if (mine) tables[i] = NULL;
|
||||
pthread_mutex_unlock(&tables_lock);
|
||||
if (!mine) continue;
|
||||
if (t->is_device) {
|
||||
VKB_INFO("process %d exited without destroying device %u; cleaning up", p->pid, t->id);
|
||||
if (t->real.vkDeviceWaitIdle) t->real.vkDeviceWaitIdle(t->device);
|
||||
vkb_emu_device_destroy(t);
|
||||
vkb_mem_free_all(t);
|
||||
t->real.vkDestroyDevice(t->device, NULL);
|
||||
} else {
|
||||
VKB_INFO("process %d exited without destroying instance %u; cleaning up", p->pid, t->id);
|
||||
t->real.vkDestroyInstance(t->instance, NULL);
|
||||
}
|
||||
pthread_mutex_destroy(&t->lock);
|
||||
free(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static vkb_srv_proc *proc_join(uint64_t token, int pid)
|
||||
{
|
||||
pthread_mutex_lock(&procs_lock);
|
||||
vkb_srv_proc *p = procs;
|
||||
while (p && p->token != token) p = p->next;
|
||||
if (!p) {
|
||||
p = calloc(1, sizeof(*p));
|
||||
p->token = token;
|
||||
p->pid = pid;
|
||||
p->next = procs;
|
||||
procs = p;
|
||||
VKB_INFO("client process %d connected", pid);
|
||||
}
|
||||
p->conns++;
|
||||
pthread_mutex_unlock(&procs_lock);
|
||||
return p;
|
||||
}
|
||||
|
||||
static void proc_leave(vkb_srv_proc *p)
|
||||
{
|
||||
pthread_mutex_lock(&procs_lock);
|
||||
int last = --p->conns == 0;
|
||||
if (last) {
|
||||
vkb_srv_proc **pp = &procs;
|
||||
while (*pp && *pp != p) pp = &(*pp)->next;
|
||||
if (*pp) *pp = p->next;
|
||||
}
|
||||
pthread_mutex_unlock(&procs_lock);
|
||||
if (last) {
|
||||
vkb_proc_cleanup(p);
|
||||
VKB_INFO("client process %d gone", p->pid);
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ command buffer replay */
|
||||
|
||||
static int is_stream_cmd(uint32_t id)
|
||||
{
|
||||
const char *n = vkb_cmd_names[id];
|
||||
return !strncmp(n, "vkCmd", 5) || id == VKB_CMD_vkBeginCommandBuffer || id == VKB_CMD_vkbPushDescRaw;
|
||||
}
|
||||
|
||||
void vkb_sv_vkEndCommandBuffer(vkb_srv_call *c)
|
||||
{
|
||||
VkCommandBuffer cb = (VkCommandBuffer)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
size_t len = 0;
|
||||
const uint8_t *s = vkb_dec_blob(&c->d, &len);
|
||||
if (c->d.err) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
VkResult begin_r = VK_SUCCESS;
|
||||
size_t pos = 0;
|
||||
vkb_enc scratch;
|
||||
vkb_enc_init(&scratch);
|
||||
uint32_t ncmds = 0;
|
||||
while (s && pos + 8 <= len) {
|
||||
uint32_t id, n;
|
||||
memcpy(&id, s + pos, 4);
|
||||
memcpy(&n, s + pos + 4, 4);
|
||||
pos += 8;
|
||||
if (id >= VKB_CMD_COUNT || !is_stream_cmd(id) || n > len - pos) {
|
||||
VKB_ERR("command stream: bad entry (id %u, %u bytes) after %u commands", id, n, ncmds);
|
||||
c->status = VKB_ST_BAD_MESSAGE;
|
||||
vkb_enc_free(&scratch);
|
||||
return;
|
||||
}
|
||||
vkb_srv_call sub;
|
||||
memset(&sub, 0, sizeof(sub));
|
||||
vkb_dec_init(&sub.d, s + pos, n, c->d.arena, NULL, 0);
|
||||
sub.r = scratch;
|
||||
vkb_enc_reset(&sub.r);
|
||||
sub.dt = c->dt;
|
||||
sub.table = c->table;
|
||||
sub.conn = c->conn;
|
||||
sub.in_stream = 1;
|
||||
vkb_srv_handlers[id](&sub);
|
||||
scratch = sub.r;
|
||||
if (sub.status != VKB_ST_OK) {
|
||||
VKB_ERR("command stream: %s failed (status %u)", vkb_cmd_names[id], sub.status);
|
||||
}
|
||||
if (id == VKB_CMD_vkBeginCommandBuffer && scratch.len >= 4) memcpy(&begin_r, scratch.buf, 4);
|
||||
pos += (n + 7) & ~(size_t)7;
|
||||
ncmds++;
|
||||
}
|
||||
vkb_enc_free(&scratch);
|
||||
VkResult r = c->dt->vkEndCommandBuffer(cb);
|
||||
if (begin_r != VK_SUCCESS) r = begin_r;
|
||||
if (vkb_verbose) VKB_DBG("command buffer %p: %u commands, %zu bytes -> %d", (void *)cb, ncmds, len, r);
|
||||
vkb_enc_u32(&c->r, (uint32_t)r);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ hello */
|
||||
|
||||
void vkb_sv_vkbHello(vkb_srv_call *c)
|
||||
{
|
||||
uint64_t hash = vkb_dec_u64(&c->d);
|
||||
uint64_t token = vkb_dec_u64(&c->d);
|
||||
uint32_t pid = vkb_dec_u32(&c->d);
|
||||
const char *comm = vkb_dec_str(&c->d);
|
||||
if (c->d.err) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
vkb_enc_u64(&c->r, VKB_PROTOCOL_HASH);
|
||||
if (hash != VKB_PROTOCOL_HASH) {
|
||||
VKB_ERR("client %d (%s) speaks protocol %016llx, this server %016llx: refusing", pid,
|
||||
comm ? comm : "?", (unsigned long long)hash, (unsigned long long)VKB_PROTOCOL_HASH);
|
||||
vkb_enc_u32(&c->r, 0);
|
||||
return;
|
||||
}
|
||||
if (!c->conn->proc) {
|
||||
c->conn->proc = proc_join(token, (int)pid);
|
||||
if (c->conn->proc->conns == 1) VKB_INFO("process %u is %s", pid, comm ? comm : "?");
|
||||
}
|
||||
vkb_enc_u32(&c->r, 1);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ connection loop */
|
||||
|
||||
static void *conn_main(void *arg)
|
||||
{
|
||||
vkb_srv_conn *conn = arg;
|
||||
vkb_srv_call c;
|
||||
vkb_enc_init(&c.r);
|
||||
int fds[VKB_MAX_FDS];
|
||||
for (;;) {
|
||||
vkb_msg_hdr h;
|
||||
int nfds = 0;
|
||||
if (vkb_recv_msg(conn->sock, &h, &conn->inbuf, &conn->incap, fds, &nfds) < 0) break;
|
||||
vkb_enc_reset(&c.r);
|
||||
vkb_dec_init(&c.d, conn->inbuf, h.size, &conn->arena, fds, nfds);
|
||||
c.conn = conn;
|
||||
c.status = VKB_ST_OK;
|
||||
c.nclose = 0;
|
||||
c.in_stream = 0;
|
||||
c.table = NULL;
|
||||
c.dt = &vkb_global_dt;
|
||||
if (h.cmd >= VKB_CMD_COUNT) {
|
||||
VKB_ERR("unknown command id %u", h.cmd);
|
||||
c.status = VKB_ST_BAD_MESSAGE;
|
||||
} else if (!conn->proc && h.cmd != VKB_CMD_vkbHello) {
|
||||
VKB_ERR("%s before hello", vkb_cmd_names[h.cmd]);
|
||||
c.status = VKB_ST_BAD_MESSAGE;
|
||||
} else {
|
||||
if (h.table) {
|
||||
c.table = vkb_table_get(h.table);
|
||||
if (!c.table || c.table->proc != conn->proc) {
|
||||
VKB_ERR("%s: unknown dispatch table %u", vkb_cmd_names[h.cmd], h.table);
|
||||
c.status = VKB_ST_BAD_TABLE;
|
||||
} else {
|
||||
c.dt = &c.table->dt;
|
||||
}
|
||||
}
|
||||
if (c.status == VKB_ST_OK) {
|
||||
vkb_srv_set_current_table(c.table);
|
||||
if (vkb_verbose) VKB_DBG("-> %s", vkb_cmd_names[h.cmd]);
|
||||
vkb_srv_handlers[h.cmd](&c);
|
||||
vkb_srv_set_current_table(NULL);
|
||||
}
|
||||
}
|
||||
vkb_dec_close_untaken(&c.d);
|
||||
if (!(h.flags & VKB_F_NOREPLY)) {
|
||||
vkb_msg_hdr rh = {VKB_MAGIC, c.status == VKB_ST_OK ? (uint32_t)c.r.len : 0, c.status, 0,
|
||||
c.status == VKB_ST_OK ? (uint32_t)c.r.nfds : 0, 0};
|
||||
if (c.r.oom) {
|
||||
rh.cmd = VKB_ST_BAD_MESSAGE;
|
||||
rh.size = 0;
|
||||
rh.nfds = 0;
|
||||
}
|
||||
if (vkb_send_msg(conn->sock, &rh, c.r.buf, c.r.fds, (int)rh.nfds) < 0) {
|
||||
for (int i = 0; i < c.nclose; i++) close(c.close_after[i]);
|
||||
break;
|
||||
}
|
||||
} else if (c.status != VKB_ST_OK) {
|
||||
VKB_ERR("asynchronous %s failed (status %u)", vkb_cmd_names[h.cmd], c.status);
|
||||
}
|
||||
for (int i = 0; i < c.nclose; i++) close(c.close_after[i]);
|
||||
vkb_arena_reset(&conn->arena);
|
||||
}
|
||||
close(conn->sock);
|
||||
vkb_enc_free(&c.r);
|
||||
free(conn->inbuf);
|
||||
vkb_arena_free(&conn->arena);
|
||||
if (conn->proc) proc_leave(conn->proc);
|
||||
free(conn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void vkb_srv_serve(int lfd)
|
||||
{
|
||||
for (;;) {
|
||||
int s = accept4(lfd, NULL, NULL, SOCK_CLOEXEC);
|
||||
if (s < 0) {
|
||||
if (errno == EINTR || errno == ECONNABORTED) continue;
|
||||
VKB_ERR("accept: %s", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
vkb_srv_conn *conn = calloc(1, sizeof(*conn));
|
||||
conn->sock = s;
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
/* Driver calls (pipeline compiles) can use a lot of stack. */
|
||||
pthread_attr_setstacksize(&attr, 4u << 20);
|
||||
if (pthread_create(&conn->thread, &attr, conn_main, conn) != 0) {
|
||||
VKB_ERR("cannot start a connection thread");
|
||||
close(s);
|
||||
free(conn);
|
||||
}
|
||||
pthread_attr_destroy(&attr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Feature emulation: entry point. What the GPU lacks and the bridge can make up for is decided
|
||||
* here per physical device; the client reports those features as present, the server strips
|
||||
* them from vkCreateDevice and installs wrappers in the device's dispatch table.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_server.h"
|
||||
#include "server_emu.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int env_off(const char *name)
|
||||
{
|
||||
const char *v = getenv(name);
|
||||
return v && !strcmp(v, "0");
|
||||
}
|
||||
|
||||
uint32_t vkb_emu_detect(const vkb_dispatch *idt, VkPhysicalDevice pd)
|
||||
{
|
||||
VkPhysicalDeviceFeatures f;
|
||||
idt->vkGetPhysicalDeviceFeatures(pd, &f);
|
||||
uint32_t n = 0;
|
||||
idt->vkEnumerateDeviceExtensionProperties(pd, NULL, &n, NULL);
|
||||
VkExtensionProperties *e = calloc(n ? n : 1, sizeof(*e));
|
||||
idt->vkEnumerateDeviceExtensionProperties(pd, NULL, &n, e);
|
||||
int divisor = 0, depth_clip = 0;
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
if (!strcmp(e[i].extensionName, "VK_EXT_vertex_attribute_divisor") ||
|
||||
!strcmp(e[i].extensionName, "VK_KHR_vertex_attribute_divisor"))
|
||||
divisor = 1;
|
||||
if (!strcmp(e[i].extensionName, "VK_EXT_depth_clip_enable")) depth_clip = 1;
|
||||
}
|
||||
free(e);
|
||||
/* VKBRIDGE_FAKE_MISSING=bc,divisor,clip,cull,depthclip pretends the GPU lacks them (host tests). */
|
||||
const char *fake = getenv("VKBRIDGE_FAKE_MISSING");
|
||||
if (fake) {
|
||||
if (strstr(fake, "bc")) f.textureCompressionBC = VK_FALSE;
|
||||
if (strstr(fake, "divisor")) divisor = 0;
|
||||
if (strstr(fake, "clip")) f.shaderClipDistance = VK_FALSE;
|
||||
if (strstr(fake, "cull")) f.shaderCullDistance = VK_FALSE;
|
||||
if (strstr(fake, "depthclip")) depth_clip = 0;
|
||||
}
|
||||
uint32_t emu = 0;
|
||||
if (!f.textureCompressionBC && vkb_emu_bcn_supported(idt, pd) && !env_off("VKBRIDGE_EMU_BCN")) emu |= VKB_EMU_BCN;
|
||||
if (!divisor && !env_off("VKBRIDGE_EMU_DIVISOR")) emu |= VKB_EMU_DIVISOR;
|
||||
if (!f.shaderClipDistance && !env_off("VKBRIDGE_EMU_CLIP")) emu |= VKB_EMU_CLIP_DISTANCE;
|
||||
if (!f.shaderCullDistance && !env_off("VKBRIDGE_EMU_CULL")) emu |= VKB_EMU_CULL_DISTANCE;
|
||||
if (!depth_clip && f.depthClamp && !env_off("VKBRIDGE_EMU_DEPTHCLIP")) emu |= VKB_EMU_DEPTH_CLIP;
|
||||
/* PointSize in geometry/tessellation stages: stripped when the feature is missing. */
|
||||
if (!f.shaderTessellationAndGeometryPointSize && (f.geometryShader || f.tessellationShader)) emu |= VKB_EMU_POINT_SIZE;
|
||||
return emu & VKB_EMU_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* Remove a structure from a pNext chain (the chain lives in the request's arena). */
|
||||
static void chain_remove(VkDeviceCreateInfo *ci, VkStructureType t)
|
||||
{
|
||||
VkBaseOutStructure *prev = (VkBaseOutStructure *)ci;
|
||||
while (prev->pNext) {
|
||||
if (prev->pNext->sType == t) prev->pNext = prev->pNext->pNext;
|
||||
else prev = prev->pNext;
|
||||
}
|
||||
}
|
||||
|
||||
static VkPhysicalDeviceFeatures *find_features(VkDeviceCreateInfo *ci)
|
||||
{
|
||||
if (ci->pEnabledFeatures) return (VkPhysicalDeviceFeatures *)ci->pEnabledFeatures;
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)ci->pNext; b; b = b->pNext)
|
||||
if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) return &((VkPhysicalDeviceFeatures2 *)b)->features;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void vkb_emu_device_create_info(vkb_srv_table *inst, VkPhysicalDevice pd, uint32_t emu, VkDeviceCreateInfo *ci, vkb_arena *a)
|
||||
{
|
||||
VkPhysicalDeviceFeatures real;
|
||||
inst->real.vkGetPhysicalDeviceFeatures(pd, &real);
|
||||
VkPhysicalDeviceFeatures *f = find_features(ci);
|
||||
if (f) {
|
||||
/* pEnabledFeatures came from the request: it is ours to change. Anything the client asked
|
||||
* for that the GPU does not have is either emulated or would fail the call. */
|
||||
VkBool32 *want = (VkBool32 *)f;
|
||||
const VkBool32 *have = (const VkBool32 *)ℜ
|
||||
for (size_t i = 0; i < sizeof(*f) / sizeof(VkBool32); i++) {
|
||||
if (want[i] && !have[i]) want[i] = VK_FALSE;
|
||||
}
|
||||
}
|
||||
if (emu & VKB_EMU_DIVISOR) chain_remove(ci, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT);
|
||||
if (emu & VKB_EMU_DEPTH_CLIP) chain_remove(ci, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT);
|
||||
(void)a;
|
||||
}
|
||||
|
||||
void vkb_emu_device_init(vkb_srv_table *dev, uint32_t emu)
|
||||
{
|
||||
if (!emu) return;
|
||||
vkb_emu_device *e = calloc(1, sizeof(*e));
|
||||
e->flags = emu;
|
||||
e->dev = dev;
|
||||
pthread_mutex_init(&e->lock, NULL);
|
||||
dev->emu = e;
|
||||
vkb_emu_install(dev);
|
||||
}
|
||||
|
||||
void vkb_emu_device_destroy(vkb_srv_table *dev)
|
||||
{
|
||||
if (!dev->emu) return;
|
||||
vkb_emu_uninstall(dev);
|
||||
pthread_mutex_destroy(&dev->emu->lock);
|
||||
free(dev->emu);
|
||||
dev->emu = NULL;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
#ifndef VKB_SERVER_EMU_H
|
||||
#define VKB_SERVER_EMU_H
|
||||
|
||||
#include "vkb_server.h"
|
||||
|
||||
/* Emulations that are implemented (the rest are never offered to the client). */
|
||||
#ifndef VKB_EMU_IMPLEMENTED
|
||||
#define VKB_EMU_IMPLEMENTED 0u
|
||||
#endif
|
||||
|
||||
typedef struct vkb_emu_device {
|
||||
uint32_t flags;
|
||||
vkb_srv_table *dev;
|
||||
pthread_mutex_t lock;
|
||||
void *bcn; /* server_emu_bcn.c state */
|
||||
void *shaders; /* server_emu_shader.c state */
|
||||
} vkb_emu_device;
|
||||
|
||||
int vkb_emu_bcn_supported(const vkb_dispatch *idt, VkPhysicalDevice pd);
|
||||
void vkb_emu_install(vkb_srv_table *dev);
|
||||
void vkb_emu_uninstall(vkb_srv_table *dev);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Emulation wrappers installed into a device's dispatch table.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "server_emu.h"
|
||||
|
||||
int vkb_emu_bcn_supported(const vkb_dispatch *idt, VkPhysicalDevice pd)
|
||||
{
|
||||
(void)idt;
|
||||
(void)pd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vkb_emu_install(vkb_srv_table *dev)
|
||||
{
|
||||
(void)dev;
|
||||
}
|
||||
|
||||
void vkb_emu_uninstall(vkb_srv_table *dev)
|
||||
{
|
||||
(void)dev;
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Instances, devices and the startup self-test.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_server.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
vkb_pd_knowledge vkb_pds[VKB_MAX_PDS];
|
||||
int vkb_npds;
|
||||
|
||||
static int has_ext(const VkExtensionProperties *e, uint32_t n, const char *name)
|
||||
{
|
||||
for (uint32_t i = 0; i < n; i++)
|
||||
if (!strcmp(e[i].extensionName, name)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const vkb_pd_knowledge *vkb_pd_lookup(const vkb_dispatch *dt, VkPhysicalDevice pd)
|
||||
{
|
||||
VkPhysicalDeviceIDProperties id = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES};
|
||||
VkPhysicalDeviceProperties2 p2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id};
|
||||
if (!dt->vkGetPhysicalDeviceProperties2) return NULL;
|
||||
dt->vkGetPhysicalDeviceProperties2(pd, &p2);
|
||||
for (int i = 0; i < vkb_npds; i++)
|
||||
if (vkb_pds[i].valid && !memcmp(vkb_pds[i].uuid, id.deviceUUID, VK_UUID_SIZE)) return &vkb_pds[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ startup self-test */
|
||||
|
||||
static void log_device(const vkb_dispatch *dt, VkPhysicalDevice pd, const VkPhysicalDeviceProperties *p)
|
||||
{
|
||||
VKB_INFO("GPU: %s (vendor 0x%04x device 0x%04x, Vulkan %u.%u.%u, driver 0x%08x)", p->deviceName,
|
||||
p->vendorID, p->deviceID, VK_API_VERSION_MAJOR(p->apiVersion), VK_API_VERSION_MINOR(p->apiVersion),
|
||||
VK_API_VERSION_PATCH(p->apiVersion), p->driverVersion);
|
||||
VkPhysicalDeviceFeatures f;
|
||||
dt->vkGetPhysicalDeviceFeatures(pd, &f);
|
||||
#define F(x) #x, f.x
|
||||
VKB_INFO("features: %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u", F(textureCompressionBC), F(shaderClipDistance),
|
||||
F(shaderCullDistance), F(geometryShader), F(tessellationShader), F(depthClamp), F(shaderFloat64), F(shaderInt64));
|
||||
VKB_INFO("features: %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u", F(multiViewport), F(independentBlend), F(logicOp),
|
||||
F(fillModeNonSolid), F(dualSrcBlend), F(sampleRateShading), F(depthBiasClamp), F(wideLines));
|
||||
VKB_INFO("features: %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u", F(drawIndirectFirstInstance), F(multiDrawIndirect),
|
||||
F(occlusionQueryPrecise), F(pipelineStatisticsQuery), F(vertexPipelineStoresAndAtomics),
|
||||
F(fragmentStoresAndAtomics), F(shaderStorageImageWriteWithoutFormat), F(imageCubeArray));
|
||||
VKB_INFO("features: %s=%u %s=%u %s=%u %s=%u %s=%u %s=%u", F(robustBufferAccess), F(fullDrawIndexUint32), F(samplerAnisotropy),
|
||||
F(depthBounds), F(largePoints), F(shaderStorageImageExtendedFormats));
|
||||
#undef F
|
||||
uint32_t n = 0;
|
||||
dt->vkEnumerateDeviceExtensionProperties(pd, NULL, &n, NULL);
|
||||
VkExtensionProperties *e = calloc(n ? n : 1, sizeof(*e));
|
||||
dt->vkEnumerateDeviceExtensionProperties(pd, NULL, &n, e);
|
||||
static const char *const watch[] = {
|
||||
"VK_EXT_depth_clip_enable", "VK_EXT_vertex_attribute_divisor", "VK_KHR_vertex_attribute_divisor",
|
||||
"VK_EXT_external_memory_dma_buf", "VK_EXT_image_drm_format_modifier", "VK_KHR_external_memory_fd",
|
||||
"VK_EXT_external_memory_host", "VK_ANDROID_external_memory_android_hardware_buffer",
|
||||
"VK_KHR_external_semaphore_fd", "VK_KHR_external_fence_fd", "VK_EXT_queue_family_foreign",
|
||||
"VK_EXT_robustness2", "VK_EXT_transform_feedback", "VK_EXT_custom_border_color",
|
||||
"VK_EXT_mutable_descriptor_type", "VK_VALVE_mutable_descriptor_type", "VK_EXT_extended_dynamic_state3",
|
||||
"VK_EXT_graphics_pipeline_library", "VK_EXT_physical_device_drm", "VK_KHR_push_descriptor",
|
||||
"VK_EXT_descriptor_indexing", "VK_KHR_timeline_semaphore", "VK_EXT_memory_budget",
|
||||
"VK_KHR_maintenance5", "VK_EXT_line_rasterization", "VK_EXT_provoking_vertex",
|
||||
};
|
||||
char line[1024];
|
||||
size_t len = 0;
|
||||
line[0] = 0;
|
||||
for (size_t i = 0; i < sizeof(watch) / sizeof(watch[0]); i++) {
|
||||
int ok = has_ext(e, n, watch[i]);
|
||||
len += (size_t)snprintf(line + len, sizeof(line) - len, "%s%s%s", len ? " " : "", ok ? "+" : "-", watch[i] + 3);
|
||||
if (len > 700 || i + 1 == sizeof(watch) / sizeof(watch[0])) {
|
||||
VKB_INFO("extensions: %s", line);
|
||||
len = 0;
|
||||
line[0] = 0;
|
||||
}
|
||||
}
|
||||
VKB_INFO("%u device extensions in total", n);
|
||||
for (uint32_t i = 0; i < n; i++) VKB_DBG(" %s (rev %u)", e[i].extensionName, e[i].specVersion);
|
||||
free(e);
|
||||
VkPhysicalDeviceMemoryProperties mp;
|
||||
dt->vkGetPhysicalDeviceMemoryProperties(pd, &mp);
|
||||
for (uint32_t i = 0; i < mp.memoryHeapCount; i++)
|
||||
VKB_INFO("memory heap %u: %llu MB flags 0x%x", i, (unsigned long long)(mp.memoryHeaps[i].size >> 20), mp.memoryHeaps[i].flags);
|
||||
for (uint32_t i = 0; i < mp.memoryTypeCount; i++)
|
||||
VKB_INFO("memory type %u: heap %u flags 0x%x", i, mp.memoryTypes[i].heapIndex, mp.memoryTypes[i].propertyFlags);
|
||||
}
|
||||
|
||||
void vkb_selftest_all(void)
|
||||
{
|
||||
uint32_t api = VK_API_VERSION_1_0;
|
||||
if (vkb_global_dt.vkEnumerateInstanceVersion) vkb_global_dt.vkEnumerateInstanceVersion(&api);
|
||||
if (api > VK_API_VERSION_1_3) api = VK_API_VERSION_1_3;
|
||||
VkApplicationInfo app = {VK_STRUCTURE_TYPE_APPLICATION_INFO, NULL, "vkbridge-selftest", 1, "vkbridge", 1, api};
|
||||
VkInstanceCreateInfo ci = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, NULL, 0, &app, 0, NULL, 0, NULL};
|
||||
VkInstance inst;
|
||||
VkResult r = vkb_global_dt.vkCreateInstance(&ci, NULL, &inst);
|
||||
if (r != VK_SUCCESS) {
|
||||
VKB_ERR("self-test: vkCreateInstance failed (%d); the driver is unusable", r);
|
||||
return;
|
||||
}
|
||||
vkb_dispatch dt;
|
||||
vkb_dispatch_load_instance(&dt, vkb_gipa, inst);
|
||||
uint32_t n = 0;
|
||||
dt.vkEnumeratePhysicalDevices(inst, &n, NULL);
|
||||
if (n > VKB_MAX_PDS) n = VKB_MAX_PDS;
|
||||
VkPhysicalDevice pds[VKB_MAX_PDS];
|
||||
dt.vkEnumeratePhysicalDevices(inst, &n, pds);
|
||||
VKB_INFO("self-test: loader Vulkan %u.%u, %u physical device(s)", VK_API_VERSION_MAJOR(api), VK_API_VERSION_MINOR(api), n);
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
VkPhysicalDeviceIDProperties id = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES};
|
||||
VkPhysicalDeviceProperties2 p2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id};
|
||||
dt.vkGetPhysicalDeviceProperties2(pds[i], &p2);
|
||||
log_device(&dt, pds[i], &p2.properties);
|
||||
vkb_pd_knowledge *k = &vkb_pds[vkb_npds++];
|
||||
memcpy(k->uuid, id.deviceUUID, VK_UUID_SIZE);
|
||||
snprintf(k->name, sizeof(k->name), "%s", p2.properties.deviceName);
|
||||
k->info.real_api_version = p2.properties.apiVersion;
|
||||
vkb_mem_selftest(k, inst, &dt, pds[i]);
|
||||
k->info.emu_available = vkb_emu_detect(&dt, pds[i]);
|
||||
k->info.emu = k->info.emu_available;
|
||||
const char *emu_env = getenv("VKBRIDGE_EMULATE");
|
||||
if (emu_env && !strcmp(emu_env, "0")) k->info.emu = 0;
|
||||
k->valid = 1;
|
||||
VKB_INFO("%s: memory sharing = %s, emulating 0x%x", k->name, vkb_mem_strategy_name(k->info.strategy), k->info.emu);
|
||||
}
|
||||
dt.vkDestroyInstance(inst, NULL);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ instance */
|
||||
|
||||
void vkb_sv_vkCreateInstance(vkb_srv_call *c)
|
||||
{
|
||||
VkInstanceCreateInfo *ci = NULL;
|
||||
if (vkb_dec_u8(&c->d)) {
|
||||
ci = vkb_dec_alloc(&c->d, sizeof(*ci));
|
||||
if (ci) vkb_dec_VkInstanceCreateInfo(&c->d, ci);
|
||||
}
|
||||
uint8_t want_out = vkb_dec_u8(&c->d);
|
||||
if (c->d.err || !ci || !want_out) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
VkApplicationInfo app;
|
||||
uint32_t loader_api = VK_API_VERSION_1_0;
|
||||
if (vkb_global_dt.vkEnumerateInstanceVersion) vkb_global_dt.vkEnumerateInstanceVersion(&loader_api);
|
||||
if (ci->pApplicationInfo) {
|
||||
app = *ci->pApplicationInfo;
|
||||
if (app.apiVersion > loader_api) app.apiVersion = loader_api;
|
||||
ci->pApplicationInfo = &app;
|
||||
}
|
||||
VkInstance inst = VK_NULL_HANDLE;
|
||||
VkResult r = vkb_global_dt.vkCreateInstance(ci, NULL, &inst);
|
||||
uint32_t id = 0;
|
||||
if (r == VK_SUCCESS) {
|
||||
vkb_srv_table *t = calloc(1, sizeof(*t));
|
||||
pthread_mutex_init(&t->lock, NULL);
|
||||
t->instance = inst;
|
||||
t->proc = c->conn->proc;
|
||||
t->api_version = ci->pApplicationInfo ? ci->pApplicationInfo->apiVersion : VK_API_VERSION_1_0;
|
||||
vkb_dispatch_load_instance(&t->real, vkb_gipa, inst);
|
||||
t->dt = t->real;
|
||||
id = vkb_table_register(t);
|
||||
if (!id) {
|
||||
t->real.vkDestroyInstance(inst, NULL);
|
||||
free(t);
|
||||
r = VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
inst = VK_NULL_HANDLE;
|
||||
} else {
|
||||
VKB_INFO("instance %u created (app \"%s\", engine \"%s\", Vulkan %u.%u)", id,
|
||||
ci->pApplicationInfo && ci->pApplicationInfo->pApplicationName ? ci->pApplicationInfo->pApplicationName : "",
|
||||
ci->pApplicationInfo && ci->pApplicationInfo->pEngineName ? ci->pApplicationInfo->pEngineName : "",
|
||||
VK_API_VERSION_MAJOR(t->api_version), VK_API_VERSION_MINOR(t->api_version));
|
||||
}
|
||||
} else {
|
||||
VKB_ERR("vkCreateInstance failed: %d", r);
|
||||
}
|
||||
vkb_enc_u32(&c->r, (uint32_t)r);
|
||||
vkb_enc_u64(&c->r, (uint64_t)(uintptr_t)inst);
|
||||
vkb_enc_u32(&c->r, id);
|
||||
}
|
||||
|
||||
void vkb_sv_vkDestroyInstance(vkb_srv_call *c)
|
||||
{
|
||||
VkInstance inst = (VkInstance)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
vkb_srv_table *t = c->table;
|
||||
if (!t || t->is_device || t->instance != inst) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
vkb_table_unregister(t);
|
||||
t->real.vkDestroyInstance(inst, NULL);
|
||||
pthread_mutex_destroy(&t->lock);
|
||||
free(t);
|
||||
c->table = NULL;
|
||||
}
|
||||
|
||||
void vkb_sv_vkbQueryServer(vkb_srv_call *c)
|
||||
{
|
||||
VkPhysicalDevice pd = (VkPhysicalDevice)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
if (c->d.err || !c->table) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
const vkb_pd_knowledge *k = vkb_pd_lookup(c->dt, pd);
|
||||
vkb_server_pd_info info;
|
||||
memset(&info, 0, sizeof(info));
|
||||
if (k) info = k->info;
|
||||
else VKB_WARN("query for a physical device the self-test did not see");
|
||||
vkb_enc_raw(&c->r, &info, sizeof(info));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ device */
|
||||
|
||||
static int list_has(const char *const *l, uint32_t n, const char *s)
|
||||
{
|
||||
for (uint32_t i = 0; i < n; i++)
|
||||
if (l[i] && !strcmp(l[i], s)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vkb_sv_vkCreateDevice(vkb_srv_call *c)
|
||||
{
|
||||
VkPhysicalDevice pd = (VkPhysicalDevice)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
VkDeviceCreateInfo *ci = NULL;
|
||||
if (vkb_dec_u8(&c->d)) {
|
||||
ci = vkb_dec_alloc(&c->d, sizeof(*ci));
|
||||
if (ci) vkb_dec_VkDeviceCreateInfo(&c->d, ci);
|
||||
}
|
||||
uint8_t want_out = vkb_dec_u8(&c->d);
|
||||
vkb_srv_table *inst = c->table;
|
||||
if (c->d.err || !ci || !want_out || !inst || inst->is_device) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
const vkb_pd_knowledge *k = vkb_pd_lookup(&inst->real, pd);
|
||||
uint32_t strategy = k ? k->info.strategy : VKB_MEM_NONE;
|
||||
uint32_t emu = k ? k->info.emu : 0;
|
||||
|
||||
/* Extensions the server itself needs (memory sharing), when the driver has them. */
|
||||
uint32_t avail_n = 0;
|
||||
inst->real.vkEnumerateDeviceExtensionProperties(pd, NULL, &avail_n, NULL);
|
||||
VkExtensionProperties *avail = vkb_dec_alloc(&c->d, (avail_n ? avail_n : 1) * sizeof(*avail));
|
||||
inst->real.vkEnumerateDeviceExtensionProperties(pd, NULL, &avail_n, avail);
|
||||
uint32_t extra_n = 0;
|
||||
const char *const *extra = vkb_mem_required_extensions(strategy, &extra_n);
|
||||
const char **exts = vkb_dec_alloc(&c->d, (ci->enabledExtensionCount + extra_n + 1) * sizeof(char *));
|
||||
uint32_t n = 0;
|
||||
for (uint32_t i = 0; i < ci->enabledExtensionCount; i++) {
|
||||
const char *e = ci->ppEnabledExtensionNames[i];
|
||||
if (!has_ext(avail, avail_n, e)) {
|
||||
VKB_DBG("device extension %s is emulated or client-side; not passed to the driver", e);
|
||||
continue;
|
||||
}
|
||||
exts[n++] = e;
|
||||
}
|
||||
for (uint32_t i = 0; i < extra_n; i++)
|
||||
if (has_ext(avail, avail_n, extra[i]) && !list_has(exts, n, extra[i])) exts[n++] = extra[i];
|
||||
ci->enabledExtensionCount = n;
|
||||
ci->ppEnabledExtensionNames = exts;
|
||||
ci->enabledLayerCount = 0;
|
||||
ci->ppEnabledLayerNames = NULL;
|
||||
|
||||
vkb_emu_device_create_info(inst, pd, emu, ci, c->d.arena);
|
||||
|
||||
VkDevice dev = VK_NULL_HANDLE;
|
||||
VkResult r = inst->real.vkCreateDevice(pd, ci, NULL, &dev);
|
||||
uint32_t id = 0;
|
||||
if (r == VK_SUCCESS) {
|
||||
vkb_srv_table *t = calloc(1, sizeof(*t));
|
||||
pthread_mutex_init(&t->lock, NULL);
|
||||
t->is_device = 1;
|
||||
t->proc = c->conn->proc;
|
||||
t->instance = inst->instance;
|
||||
t->device = dev;
|
||||
t->physical = pd;
|
||||
t->parent = inst;
|
||||
t->pd = k;
|
||||
t->api_version = inst->api_version;
|
||||
t->real = inst->real;
|
||||
PFN_vkGetDeviceProcAddr gdpa = (PFN_vkGetDeviceProcAddr)vkb_gipa(inst->instance, "vkGetDeviceProcAddr");
|
||||
vkb_dispatch_load_device(&t->real, gdpa, dev);
|
||||
t->dt = t->real;
|
||||
id = vkb_table_register(t);
|
||||
if (!id) {
|
||||
t->real.vkDestroyDevice(dev, NULL);
|
||||
free(t);
|
||||
dev = VK_NULL_HANDLE;
|
||||
r = VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
} else {
|
||||
vkb_emu_device_init(t, emu);
|
||||
VKB_INFO("device %u created on %s (%u extensions, memory sharing %s)", id, k ? k->name : "?", n,
|
||||
vkb_mem_strategy_name(strategy));
|
||||
for (uint32_t i = 0; i < n; i++) VKB_DBG(" device extension %s", exts[i]);
|
||||
}
|
||||
} else {
|
||||
VKB_ERR("vkCreateDevice failed: %d (%u extensions)", r, n);
|
||||
for (uint32_t i = 0; i < n; i++) VKB_ERR(" requested %s", exts[i]);
|
||||
}
|
||||
vkb_enc_u32(&c->r, (uint32_t)r);
|
||||
vkb_enc_u64(&c->r, (uint64_t)(uintptr_t)dev);
|
||||
vkb_enc_u32(&c->r, id);
|
||||
}
|
||||
|
||||
void vkb_sv_vkDestroyDevice(vkb_srv_call *c)
|
||||
{
|
||||
VkDevice dev = (VkDevice)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
vkb_srv_table *t = c->table;
|
||||
if (!t || !t->is_device || t->device != dev) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
vkb_table_unregister(t);
|
||||
vkb_emu_device_destroy(t);
|
||||
vkb_mem_free_all(t);
|
||||
t->real.vkDestroyDevice(dev, NULL);
|
||||
VKB_INFO("device %u destroyed", t->id);
|
||||
pthread_mutex_destroy(&t->lock);
|
||||
free(t);
|
||||
c->table = NULL;
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* vkbridge server entry point.
|
||||
*
|
||||
* libvkbridge_server.so --socket <path> [--log <file>] [--vulkan <libvulkan>] [--selftest]
|
||||
*
|
||||
* Packaged as a "library" so Android extracts it into the native library directory, from where
|
||||
* the app executes it like proot. It loads the system Vulkan loader (on the device: the vendor's
|
||||
* Mali driver behind /system/lib64/libvulkan.so), runs a self-test that decides how mapped memory
|
||||
* is shared with the Linux side, then serves the session's Vulkan calls until it is killed or its
|
||||
* parent dies.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_server.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <android/log.h>
|
||||
#endif
|
||||
|
||||
PFN_vkGetInstanceProcAddr vkb_gipa;
|
||||
vkb_dispatch vkb_global_dt;
|
||||
int vkb_verbose;
|
||||
|
||||
static FILE *log_file;
|
||||
static int log_level = VKB_LOG_INFO;
|
||||
static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
int vkb_log_level(void) { return log_level; }
|
||||
|
||||
void vkb_log(int level, const char *fmt, ...)
|
||||
{
|
||||
if (level > 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[] = {"E", "W", "I", "D"};
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
struct tm tm;
|
||||
localtime_r(&ts.tv_sec, &tm);
|
||||
pthread_mutex_lock(&log_lock);
|
||||
FILE *f = log_file ? log_file : stderr;
|
||||
fprintf(f, "%02d:%02d:%02d.%03ld vkbridge-server %s %s\n", tm.tm_hour, tm.tm_min, tm.tm_sec,
|
||||
ts.tv_nsec / 1000000, tags[level & 3], msg);
|
||||
fflush(f);
|
||||
pthread_mutex_unlock(&log_lock);
|
||||
#ifdef __ANDROID__
|
||||
static const int prio[] = {ANDROID_LOG_ERROR, ANDROID_LOG_WARN, ANDROID_LOG_INFO, ANDROID_LOG_DEBUG};
|
||||
__android_log_write(prio[level & 3], "vkbridge", msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void *load_vulkan(const char *override)
|
||||
{
|
||||
const char *names[] = {
|
||||
override,
|
||||
#ifdef __ANDROID__
|
||||
"libvulkan.so",
|
||||
#else
|
||||
"libvulkan.so.1",
|
||||
"libvulkan.so",
|
||||
#endif
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
|
||||
if (!names[i]) continue;
|
||||
void *h = dlopen(names[i], RTLD_NOW | RTLD_LOCAL);
|
||||
if (h) {
|
||||
VKB_INFO("Vulkan loader: %s", names[i]);
|
||||
return h;
|
||||
}
|
||||
VKB_WARN("cannot open %s: %s", names[i], dlerror());
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int listen_on(const char *path)
|
||||
{
|
||||
struct sockaddr_un a;
|
||||
if (strlen(path) >= sizeof(a.sun_path)) {
|
||||
VKB_ERR("socket path too long (%zu bytes): %s", strlen(path), path);
|
||||
return -1;
|
||||
}
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) {
|
||||
VKB_ERR("socket: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
memset(&a, 0, sizeof(a));
|
||||
a.sun_family = AF_UNIX;
|
||||
strcpy(a.sun_path, path);
|
||||
unlink(path);
|
||||
if (bind(fd, (struct sockaddr *)&a, sizeof(a)) < 0) {
|
||||
VKB_ERR("bind %s: %s", path, strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
chmod(path, 0600);
|
||||
if (listen(fd, 128) < 0) {
|
||||
VKB_ERR("listen: %s", strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void usage(void)
|
||||
{
|
||||
fprintf(stderr, "usage: vkbridge-server --socket PATH [--log FILE] [--vulkan LIB] [--selftest] [--verbose]\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *sock_path = getenv("VKBRIDGE_SOCKET");
|
||||
const char *log_path = getenv("VKBRIDGE_SERVER_LOG");
|
||||
const char *vk_lib = getenv("VKBRIDGE_SERVER_VULKAN");
|
||||
int selftest_only = 0;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "--socket") && i + 1 < argc) sock_path = argv[++i];
|
||||
else if (!strcmp(argv[i], "--log") && i + 1 < argc) log_path = argv[++i];
|
||||
else if (!strcmp(argv[i], "--vulkan") && i + 1 < argc) vk_lib = argv[++i];
|
||||
else if (!strcmp(argv[i], "--selftest")) selftest_only = 1;
|
||||
else if (!strcmp(argv[i], "--verbose")) vkb_verbose = 1;
|
||||
else {
|
||||
usage();
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
if (getenv("VKBRIDGE_DEBUG")) vkb_verbose = 1;
|
||||
if (vkb_verbose) log_level = VKB_LOG_DEBUG;
|
||||
if (log_path) {
|
||||
log_file = fopen(log_path, "ae");
|
||||
if (!log_file) fprintf(stderr, "vkbridge-server: cannot open log %s: %s\n", log_path, strerror(errno));
|
||||
}
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
/* The server is the app's child: when the app dies, so does the session's GPU. */
|
||||
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||
|
||||
VKB_INFO("starting (pid %d, protocol %016llx)", getpid(), (unsigned long long)VKB_PROTOCOL_HASH);
|
||||
void *lib = load_vulkan(vk_lib);
|
||||
if (!lib) {
|
||||
VKB_ERR("no Vulkan loader; nothing to serve");
|
||||
return 1;
|
||||
}
|
||||
vkb_gipa = (PFN_vkGetInstanceProcAddr)dlsym(lib, "vkGetInstanceProcAddr");
|
||||
if (!vkb_gipa) {
|
||||
VKB_ERR("the Vulkan loader exports no vkGetInstanceProcAddr");
|
||||
return 1;
|
||||
}
|
||||
vkb_dispatch_load_instance(&vkb_global_dt, vkb_gipa, VK_NULL_HANDLE);
|
||||
|
||||
vkb_selftest_all();
|
||||
if (selftest_only) return vkb_npds > 0 ? 0 : 1;
|
||||
|
||||
if (!sock_path) {
|
||||
usage();
|
||||
return 2;
|
||||
}
|
||||
int lfd = listen_on(sock_path);
|
||||
if (lfd < 0) return 1;
|
||||
VKB_INFO("listening on %s", sock_path);
|
||||
vkb_srv_serve(lfd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,667 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Host-visible memory shared with the client.
|
||||
*
|
||||
* A Linux program maps Vulkan memory and writes to it directly; those writes must land in the
|
||||
* pages the GPU reads. The server makes such memory shareable in one of three ways, chosen per
|
||||
* GPU by a self-test at startup that checks the GPU and both CPU mappings really see the same
|
||||
* bytes:
|
||||
* hostptr the client creates a memfd, the server maps it and imports it
|
||||
* (VK_EXT_external_memory_host);
|
||||
* dmabuf the server allocates exportable memory and hands the client its dma-buf
|
||||
* (VK_EXT_external_memory_dma_buf), which the client maps;
|
||||
* ahb the server allocates a BLOB AHardwareBuffer, imports it, and hands the client the
|
||||
* dma-buf behind it (Android only).
|
||||
* VKBRIDGE_MEMORY=hostptr|dmabuf|ahb forces one.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#ifdef __ANDROID__
|
||||
#define VK_USE_PLATFORM_ANDROID_KHR 1
|
||||
#endif
|
||||
#include "vkb_server.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <vulkan/vulkan_android.h>
|
||||
/* libnativewindow, opened at run time (getNativeHandle is not in the NDK headers). */
|
||||
typedef struct {
|
||||
uint32_t width, height, layers, format;
|
||||
uint64_t usage;
|
||||
uint32_t stride, rfu0;
|
||||
uint64_t rfu1;
|
||||
} vkb_ahb_desc;
|
||||
typedef struct {
|
||||
int version, numFds, numInts;
|
||||
int data[0];
|
||||
} vkb_native_handle;
|
||||
static int (*p_AHardwareBuffer_allocate)(const vkb_ahb_desc *, struct AHardwareBuffer **);
|
||||
static void (*p_AHardwareBuffer_release)(struct AHardwareBuffer *);
|
||||
static const vkb_native_handle *(*p_AHardwareBuffer_getNativeHandle)(const struct AHardwareBuffer *);
|
||||
#define AHB_FORMAT_BLOB 0x21
|
||||
#define AHB_USAGE_CPU_READ_OFTEN 3ull
|
||||
#define AHB_USAGE_CPU_WRITE_OFTEN (3ull << 4)
|
||||
#define AHB_USAGE_GPU_DATA_BUFFER (1ull << 24)
|
||||
|
||||
static int ahb_load(void)
|
||||
{
|
||||
static int state;
|
||||
if (state) return state > 0;
|
||||
void *h = dlopen("libnativewindow.so", RTLD_NOW);
|
||||
if (!h) h = dlopen("libandroid.so", RTLD_NOW);
|
||||
if (h) {
|
||||
p_AHardwareBuffer_allocate = dlsym(h, "AHardwareBuffer_allocate");
|
||||
p_AHardwareBuffer_release = dlsym(h, "AHardwareBuffer_release");
|
||||
p_AHardwareBuffer_getNativeHandle = dlsym(h, "AHardwareBuffer_getNativeHandle");
|
||||
}
|
||||
state = (p_AHardwareBuffer_allocate && p_AHardwareBuffer_release && p_AHardwareBuffer_getNativeHandle) ? 1 : -1;
|
||||
if (state < 0) VKB_WARN("AHardwareBuffer functions unavailable");
|
||||
return state > 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static const char *const ext_hostptr[] = {"VK_KHR_external_memory", "VK_EXT_external_memory_host"};
|
||||
static const char *const ext_dmabuf[] = {"VK_KHR_external_memory", "VK_KHR_external_memory_fd", "VK_EXT_external_memory_dma_buf"};
|
||||
#ifdef __ANDROID__
|
||||
static const char *const ext_ahb[] = {"VK_KHR_external_memory", "VK_ANDROID_external_memory_android_hardware_buffer",
|
||||
"VK_EXT_queue_family_foreign", "VK_KHR_sampler_ycbcr_conversion",
|
||||
"VK_KHR_dedicated_allocation", "VK_KHR_get_memory_requirements2"};
|
||||
#endif
|
||||
|
||||
const char *const *vkb_mem_required_extensions(uint32_t strategy, uint32_t *count)
|
||||
{
|
||||
switch (strategy) {
|
||||
case VKB_MEM_HOSTPTR: *count = 2; return ext_hostptr;
|
||||
case VKB_MEM_DMABUF: *count = 3; return ext_dmabuf;
|
||||
#ifdef __ANDROID__
|
||||
case VKB_MEM_AHB: *count = 6; return ext_ahb;
|
||||
#endif
|
||||
default: *count = 0; return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t strategy_handle_type(uint32_t s)
|
||||
{
|
||||
switch (s) {
|
||||
case VKB_MEM_HOSTPTR: return VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
|
||||
case VKB_MEM_DMABUF: return VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
|
||||
#ifdef __ANDROID__
|
||||
case VKB_MEM_AHB: return VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
|
||||
#endif
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int vkb_memfd(const char *name, size_t size)
|
||||
{
|
||||
int fd = (int)syscall(SYS_memfd_create, name, 1u /* MFD_CLOEXEC */);
|
||||
if (fd < 0) return -1;
|
||||
if (ftruncate(fd, (off_t)size) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ tracking */
|
||||
|
||||
static unsigned mem_hash(VkDeviceMemory m)
|
||||
{
|
||||
uint64_t v = (uint64_t)(uintptr_t)m;
|
||||
v ^= v >> 17;
|
||||
v *= 0x9E3779B97F4A7C15ull;
|
||||
return (unsigned)(v >> 56);
|
||||
}
|
||||
|
||||
void vkb_mem_track(vkb_srv_table *dev, vkb_srv_mem *m)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
unsigned h = mem_hash(m->mem);
|
||||
m->next = dev->mems[h];
|
||||
dev->mems[h] = m;
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
}
|
||||
|
||||
vkb_srv_mem *vkb_mem_find(vkb_srv_table *dev, VkDeviceMemory mem, int remove)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
vkb_srv_mem **pp = &dev->mems[mem_hash(mem)];
|
||||
while (*pp && (*pp)->mem != mem) pp = &(*pp)->next;
|
||||
vkb_srv_mem *m = *pp;
|
||||
if (m && remove) *pp = m->next;
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return m;
|
||||
}
|
||||
|
||||
/* Frees the server-side resources of an allocation (the VkDeviceMemory must already be freed). */
|
||||
static void mem_release_backing(vkb_srv_mem *m)
|
||||
{
|
||||
if (m->host_ptr) munmap(m->host_ptr, m->host_len);
|
||||
#ifdef __ANDROID__
|
||||
if (m->ahb) p_AHardwareBuffer_release(m->ahb);
|
||||
#endif
|
||||
free(m);
|
||||
}
|
||||
|
||||
void vkb_mem_release(vkb_srv_table *dev, vkb_srv_mem *m)
|
||||
{
|
||||
if (m->mapped) dev->real.vkUnmapMemory(dev->device, m->mem);
|
||||
dev->real.vkFreeMemory(dev->device, m->mem, NULL);
|
||||
mem_release_backing(m);
|
||||
}
|
||||
|
||||
void vkb_mem_free_all(vkb_srv_table *dev)
|
||||
{
|
||||
for (int i = 0; i < 256; i++) {
|
||||
vkb_srv_mem *m = dev->mems[i];
|
||||
dev->mems[i] = NULL;
|
||||
while (m) {
|
||||
vkb_srv_mem *n = m->next;
|
||||
vkb_mem_release(dev, m);
|
||||
m = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ allocation */
|
||||
|
||||
/*
|
||||
* Allocates shared memory with strategy `s`. `ai` is the app's (or the self-test's) allocate
|
||||
* info; for hostptr, `client_fd` is the client's memfd (consumed). On success *out_fd is the fd
|
||||
* to send to the client (-1 for hostptr, where the client already has its pages).
|
||||
*/
|
||||
static VkResult alloc_shared(const vkb_dispatch *dt, VkDevice dev, uint32_t s, const VkMemoryAllocateInfo *ai,
|
||||
int client_fd, vkb_srv_mem **out, int *out_fd)
|
||||
{
|
||||
*out_fd = -1;
|
||||
*out = NULL;
|
||||
vkb_srv_mem *m = calloc(1, sizeof(*m));
|
||||
if (!m) {
|
||||
if (client_fd >= 0) close(client_fd);
|
||||
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
m->size = ai->allocationSize;
|
||||
m->type = ai->memoryTypeIndex;
|
||||
m->strategy = s;
|
||||
VkMemoryAllocateInfo info = *ai;
|
||||
VkResult r = VK_ERROR_FEATURE_NOT_PRESENT;
|
||||
switch (s) {
|
||||
case VKB_MEM_HOSTPTR: {
|
||||
if (client_fd < 0) break;
|
||||
void *p = mmap(NULL, (size_t)ai->allocationSize, PROT_READ | PROT_WRITE, MAP_SHARED, client_fd, 0);
|
||||
close(client_fd);
|
||||
client_fd = -1;
|
||||
if (p == MAP_FAILED) {
|
||||
VKB_ERR("hostptr: mmap of the client's memory failed: %s", strerror(errno));
|
||||
r = VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
break;
|
||||
}
|
||||
m->host_ptr = p;
|
||||
m->host_len = (size_t)ai->allocationSize;
|
||||
VkMemoryHostPointerPropertiesEXT hp = {VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT};
|
||||
if (!dt->vkGetMemoryHostPointerPropertiesEXT ||
|
||||
dt->vkGetMemoryHostPointerPropertiesEXT(dev, VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, p, &hp) != VK_SUCCESS ||
|
||||
!(hp.memoryTypeBits & (1u << ai->memoryTypeIndex))) {
|
||||
r = VK_ERROR_INVALID_EXTERNAL_HANDLE;
|
||||
break;
|
||||
}
|
||||
VkImportMemoryHostPointerInfoEXT imp = {VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT, info.pNext,
|
||||
VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, p};
|
||||
info.pNext = &imp;
|
||||
r = dt->vkAllocateMemory(dev, &info, NULL, &m->mem);
|
||||
break;
|
||||
}
|
||||
case VKB_MEM_DMABUF: {
|
||||
VkExportMemoryAllocateInfo exp = {VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO, info.pNext,
|
||||
VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT};
|
||||
/* An export the app asked for itself is merged rather than duplicated. */
|
||||
for (VkBaseOutStructure *b = (VkBaseOutStructure *)info.pNext; b; b = b->pNext) {
|
||||
if (b->sType == VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO) {
|
||||
((VkExportMemoryAllocateInfo *)b)->handleTypes |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
|
||||
exp.sType = VK_STRUCTURE_TYPE_MAX_ENUM;
|
||||
}
|
||||
}
|
||||
if (exp.sType != VK_STRUCTURE_TYPE_MAX_ENUM) info.pNext = &exp;
|
||||
r = dt->vkAllocateMemory(dev, &info, NULL, &m->mem);
|
||||
if (r != VK_SUCCESS) break;
|
||||
VkMemoryGetFdInfoKHR gi = {VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, NULL, m->mem, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT};
|
||||
int fd = -1;
|
||||
if (!dt->vkGetMemoryFdKHR || dt->vkGetMemoryFdKHR(dev, &gi, &fd) != VK_SUCCESS || fd < 0) {
|
||||
dt->vkFreeMemory(dev, m->mem, NULL);
|
||||
m->mem = VK_NULL_HANDLE;
|
||||
r = VK_ERROR_INVALID_EXTERNAL_HANDLE;
|
||||
break;
|
||||
}
|
||||
*out_fd = fd;
|
||||
break;
|
||||
}
|
||||
#ifdef __ANDROID__
|
||||
case VKB_MEM_AHB: {
|
||||
if (!ahb_load()) break;
|
||||
static PFN_vkGetAndroidHardwareBufferPropertiesANDROID get_props;
|
||||
if (!get_props) {
|
||||
PFN_vkGetDeviceProcAddr gdpa = (PFN_vkGetDeviceProcAddr)vkb_gipa(VK_NULL_HANDLE, "vkGetDeviceProcAddr");
|
||||
if (gdpa) get_props = (PFN_vkGetAndroidHardwareBufferPropertiesANDROID)gdpa(dev, "vkGetAndroidHardwareBufferPropertiesANDROID");
|
||||
}
|
||||
if (!get_props) break;
|
||||
uint64_t sz = ai->allocationSize;
|
||||
if (sz > 0xFFFFFFFFull) {
|
||||
r = VK_ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
break;
|
||||
}
|
||||
vkb_ahb_desc d = {(uint32_t)sz, 1, 1, AHB_FORMAT_BLOB,
|
||||
AHB_USAGE_CPU_READ_OFTEN | AHB_USAGE_CPU_WRITE_OFTEN | AHB_USAGE_GPU_DATA_BUFFER, 0, 0, 0};
|
||||
struct AHardwareBuffer *ahb = NULL;
|
||||
if (p_AHardwareBuffer_allocate(&d, &ahb) != 0 || !ahb) {
|
||||
r = VK_ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
break;
|
||||
}
|
||||
m->ahb = ahb;
|
||||
VkAndroidHardwareBufferPropertiesANDROID props = {VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID};
|
||||
if (get_props(dev, ahb, &props) != VK_SUCCESS || !(props.memoryTypeBits & (1u << ai->memoryTypeIndex))) {
|
||||
r = VK_ERROR_INVALID_EXTERNAL_HANDLE;
|
||||
break;
|
||||
}
|
||||
VkImportAndroidHardwareBufferInfoANDROID imp = {VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID, info.pNext, ahb};
|
||||
info.pNext = &imp;
|
||||
info.allocationSize = props.allocationSize;
|
||||
r = dt->vkAllocateMemory(dev, &info, NULL, &m->mem);
|
||||
if (r != VK_SUCCESS) break;
|
||||
const vkb_native_handle *nh = p_AHardwareBuffer_getNativeHandle(ahb);
|
||||
if (!nh || nh->numFds < 1) {
|
||||
dt->vkFreeMemory(dev, m->mem, NULL);
|
||||
m->mem = VK_NULL_HANDLE;
|
||||
r = VK_ERROR_INVALID_EXTERNAL_HANDLE;
|
||||
break;
|
||||
}
|
||||
*out_fd = fcntl(nh->data[0], F_DUPFD_CLOEXEC, 0);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (client_fd >= 0) close(client_fd);
|
||||
if (r != VK_SUCCESS) {
|
||||
mem_release_backing(m);
|
||||
return r;
|
||||
}
|
||||
*out = m;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ handlers */
|
||||
|
||||
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) vkb_close_fd(((const VkImportMemoryFdInfoKHR *)b)->fd);
|
||||
}
|
||||
|
||||
void vkb_sv_vkAllocateMemory(vkb_srv_call *c)
|
||||
{
|
||||
VkDevice dev = (VkDevice)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
VkMemoryAllocateInfo ai;
|
||||
int have = vkb_dec_u8(&c->d);
|
||||
if (have) vkb_dec_VkMemoryAllocateInfo(&c->d, &ai);
|
||||
uint32_t mode = vkb_dec_u32(&c->d);
|
||||
int client_fd = vkb_dec_fd(&c->d);
|
||||
vkb_srv_table *t = c->table;
|
||||
if (c->d.err || !have || !t || !t->is_device || t->device != dev) {
|
||||
if (client_fd >= 0) close(client_fd);
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
VkResult r;
|
||||
vkb_srv_mem *m = NULL;
|
||||
int out_fd = -1;
|
||||
if (mode == VKB_ALLOC_SHARED) {
|
||||
uint32_t s = t->pd ? t->pd->info.strategy : VKB_MEM_NONE;
|
||||
r = alloc_shared(c->dt, dev, s, &ai, client_fd, &m, &out_fd);
|
||||
if (r != VK_SUCCESS) VKB_WARN("shared allocation of %llu bytes (type %u, %s) failed: %d",
|
||||
(unsigned long long)ai.allocationSize, ai.memoryTypeIndex, vkb_mem_strategy_name(s), r);
|
||||
} else {
|
||||
if (client_fd >= 0) close(client_fd);
|
||||
m = calloc(1, sizeof(*m));
|
||||
m->size = ai.allocationSize;
|
||||
m->type = ai.memoryTypeIndex;
|
||||
r = c->dt->vkAllocateMemory(dev, &ai, NULL, &m->mem);
|
||||
if (r != VK_SUCCESS) {
|
||||
close_import_fds(ai.pNext);
|
||||
free(m);
|
||||
m = NULL;
|
||||
}
|
||||
}
|
||||
if (m) vkb_mem_track(t, m);
|
||||
vkb_enc_u32(&c->r, (uint32_t)r);
|
||||
vkb_enc_u64(&c->r, m ? (uint64_t)(uintptr_t)m->mem : 0);
|
||||
vkb_enc_fd(&c->r, out_fd);
|
||||
if (out_fd >= 0) vkb_srv_close_after_send(c, out_fd);
|
||||
vkb_enc_u64(&c->r, m ? (uint64_t)m->size : 0);
|
||||
}
|
||||
|
||||
void vkb_sv_vkFreeMemory(vkb_srv_call *c)
|
||||
{
|
||||
VkDevice dev = (VkDevice)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
VkDeviceMemory mem;
|
||||
vkb_dec_bytes(&c->d, &mem, sizeof(mem));
|
||||
vkb_srv_table *t = c->table;
|
||||
if (c->d.err || !t || !t->is_device || t->device != dev) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
if (!mem) return;
|
||||
vkb_srv_mem *m = vkb_mem_find(t, mem, 1);
|
||||
if (m) {
|
||||
vkb_mem_release(t, m);
|
||||
} else {
|
||||
VKB_WARN("vkFreeMemory of untracked memory");
|
||||
c->dt->vkFreeMemory(dev, mem, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static VkResult ensure_server_map(vkb_srv_table *t, vkb_srv_mem *m)
|
||||
{
|
||||
if (m->mapped) return VK_SUCCESS;
|
||||
pthread_mutex_lock(&t->lock);
|
||||
VkResult r = VK_SUCCESS;
|
||||
if (!m->mapped) r = t->real.vkMapMemory(t->device, m->mem, 0, VK_WHOLE_SIZE, 0, &m->mapped);
|
||||
pthread_mutex_unlock(&t->lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void flush_or_invalidate(vkb_srv_call *c, int flush)
|
||||
{
|
||||
VkDevice dev = (VkDevice)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
uint32_t n = vkb_dec_u32(&c->d);
|
||||
vkb_srv_table *t = c->table;
|
||||
if (c->d.err || !t || !t->is_device || t->device != dev || n > 65536) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
VkMappedMemoryRange *ranges = vkb_dec_alloc(&c->d, (n ? n : 1) * sizeof(*ranges));
|
||||
for (uint32_t i = 0; ranges && i < n; i++) vkb_dec_VkMappedMemoryRange(&c->d, &ranges[i]);
|
||||
if (c->d.err) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
VkResult r = VK_SUCCESS;
|
||||
for (uint32_t i = 0; i < n && r == VK_SUCCESS; i++) {
|
||||
vkb_srv_mem *m = vkb_mem_find(t, ranges[i].memory, 0);
|
||||
if (m) r = ensure_server_map(t, m);
|
||||
}
|
||||
if (r == VK_SUCCESS && n) {
|
||||
r = flush ? c->dt->vkFlushMappedMemoryRanges(dev, n, ranges) : c->dt->vkInvalidateMappedMemoryRanges(dev, n, ranges);
|
||||
}
|
||||
vkb_enc_u32(&c->r, (uint32_t)r);
|
||||
}
|
||||
|
||||
void vkb_sv_vkFlushMappedMemoryRanges(vkb_srv_call *c) { flush_or_invalidate(c, 1); }
|
||||
void vkb_sv_vkInvalidateMappedMemoryRanges(vkb_srv_call *c) { flush_or_invalidate(c, 0); }
|
||||
|
||||
/* ------------------------------------------------------------------ self-test */
|
||||
|
||||
typedef struct {
|
||||
const vkb_dispatch *idt;
|
||||
vkb_dispatch dt;
|
||||
VkPhysicalDevice pd;
|
||||
VkDevice dev;
|
||||
VkQueue queue;
|
||||
VkCommandPool pool;
|
||||
uint32_t qf;
|
||||
VkPhysicalDeviceMemoryProperties mp;
|
||||
VkDeviceSize import_align;
|
||||
} selftest;
|
||||
|
||||
static int submit_and_wait(selftest *st, VkCommandBuffer cb)
|
||||
{
|
||||
VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO};
|
||||
VkFence f;
|
||||
if (st->dt.vkCreateFence(st->dev, &fci, NULL, &f) != VK_SUCCESS) return 0;
|
||||
VkSubmitInfo si = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, 0, NULL, NULL, 1, &cb, 0, NULL};
|
||||
int ok = st->dt.vkQueueSubmit(st->queue, 1, &si, f) == VK_SUCCESS &&
|
||||
st->dt.vkWaitForFences(st->dev, 1, &f, VK_TRUE, 5000000000ull) == VK_SUCCESS;
|
||||
st->dt.vkDestroyFence(st->dev, f, NULL);
|
||||
return ok;
|
||||
}
|
||||
|
||||
#define TEST_SIZE (256u * 1024u)
|
||||
|
||||
/* One allocation of `type` with strategy `s`, checked from both sides. 1 = shared and coherent
|
||||
* (no flush needed), 2 = shared when flushed, 0 = not shared. */
|
||||
static int test_type(selftest *st, uint32_t s, uint32_t type)
|
||||
{
|
||||
const vkb_dispatch *dt = &st->dt;
|
||||
int result = 0;
|
||||
VkExternalMemoryBufferCreateInfo ext = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO, NULL, strategy_handle_type(s)};
|
||||
VkBufferCreateInfo bci = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, &ext, 0, TEST_SIZE,
|
||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
|
||||
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT |
|
||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
||||
VK_SHARING_MODE_EXCLUSIVE, 0, NULL};
|
||||
VkBuffer buf = VK_NULL_HANDLE;
|
||||
if (dt->vkCreateBuffer(st->dev, &bci, NULL, &buf) != VK_SUCCESS) return 0;
|
||||
VkMemoryRequirements req;
|
||||
dt->vkGetBufferMemoryRequirements(st->dev, buf, &req);
|
||||
if (!(req.memoryTypeBits & (1u << type))) {
|
||||
dt->vkDestroyBuffer(st->dev, buf, NULL);
|
||||
return 0;
|
||||
}
|
||||
VkDeviceSize size = req.size;
|
||||
if (s == VKB_MEM_HOSTPTR) size = (size + st->import_align - 1) & ~(st->import_align - 1);
|
||||
VkMemoryAllocateInfo ai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, NULL, size, type};
|
||||
int cfd = -1;
|
||||
if (s == VKB_MEM_HOSTPTR) {
|
||||
cfd = vkb_memfd("vkbridge-selftest", (size_t)size);
|
||||
if (cfd < 0) {
|
||||
dt->vkDestroyBuffer(st->dev, buf, NULL);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int keep_fd = s == VKB_MEM_HOSTPTR ? fcntl(cfd, F_DUPFD_CLOEXEC, 0) : -1;
|
||||
vkb_srv_mem *m = NULL;
|
||||
int out_fd = -1;
|
||||
VkResult r = alloc_shared(dt, st->dev, s, &ai, cfd, &m, &out_fd);
|
||||
if (r != VK_SUCCESS) {
|
||||
VKB_INFO(" %s, type %u: allocation failed (%d)", vkb_mem_strategy_name(s), type, r);
|
||||
if (keep_fd >= 0) close(keep_fd);
|
||||
dt->vkDestroyBuffer(st->dev, buf, NULL);
|
||||
return 0;
|
||||
}
|
||||
int map_fd = s == VKB_MEM_HOSTPTR ? keep_fd : out_fd;
|
||||
uint8_t *cmap = mmap(NULL, (size_t)m->size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
|
||||
if (map_fd >= 0) close(map_fd);
|
||||
void *smap = NULL;
|
||||
if (cmap == MAP_FAILED) {
|
||||
VKB_INFO(" %s, type %u: the client cannot map the memory (%s)", vkb_mem_strategy_name(s), type, strerror(errno));
|
||||
cmap = NULL;
|
||||
goto out;
|
||||
}
|
||||
if (dt->vkBindBufferMemory(st->dev, buf, m->mem, 0) != VK_SUCCESS ||
|
||||
dt->vkMapMemory(st->dev, m->mem, 0, VK_WHOLE_SIZE, 0, &smap) != VK_SUCCESS) {
|
||||
VKB_INFO(" %s, type %u: bind/map failed", vkb_mem_strategy_name(s), type);
|
||||
goto out;
|
||||
}
|
||||
int coherent = (st->mp.memoryTypes[type].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0;
|
||||
VkMappedMemoryRange whole = {VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, NULL, m->mem, 0, VK_WHOLE_SIZE};
|
||||
|
||||
/* 1: the two CPU mappings are the same pages. */
|
||||
for (uint32_t i = 0; i < TEST_SIZE / 4; i += 997) ((volatile uint32_t *)cmap)[i] = 0x11220000u + i;
|
||||
__sync_synchronize();
|
||||
for (uint32_t i = 0; i < TEST_SIZE / 4; i += 997) {
|
||||
if (((volatile uint32_t *)smap)[i] != 0x11220000u + i) {
|
||||
VKB_INFO(" %s, type %u: CPU mappings differ at word %u", vkb_mem_strategy_name(s), type, i);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
/* 2: GPU writes, client reads. 3: client writes, GPU copies, client reads the copy. */
|
||||
for (uint32_t i = TEST_SIZE / 8; i < TEST_SIZE / 8 + TEST_SIZE / 32; i++) ((volatile uint32_t *)cmap)[i] = 0x5a000000u ^ (i * 2654435761u);
|
||||
if (!coherent) dt->vkFlushMappedMemoryRanges(st->dev, 1, &whole);
|
||||
VkCommandBufferAllocateInfo cai = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, NULL, st->pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1};
|
||||
VkCommandBuffer cb;
|
||||
if (dt->vkAllocateCommandBuffers(st->dev, &cai, &cb) != VK_SUCCESS) goto out;
|
||||
VkCommandBufferBeginInfo bi = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, NULL, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, NULL};
|
||||
dt->vkBeginCommandBuffer(cb, &bi);
|
||||
dt->vkCmdFillBuffer(cb, buf, 0, TEST_SIZE / 8, 0xA5A5A5A5u);
|
||||
/* Layout (bytes): [0, N/8) GPU fill; [N/2, N/2 + N/8) written by the client and copied by
|
||||
* the GPU to [3N/4, 3N/4 + N/8). */
|
||||
VkBufferCopy region = {TEST_SIZE / 2, TEST_SIZE / 4 * 3, TEST_SIZE / 8};
|
||||
dt->vkCmdCopyBuffer(cb, buf, buf, 1, ®ion);
|
||||
VkMemoryBarrier mb = {VK_STRUCTURE_TYPE_MEMORY_BARRIER, NULL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT};
|
||||
dt->vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &mb, 0, NULL, 0, NULL);
|
||||
dt->vkEndCommandBuffer(cb);
|
||||
int submitted = submit_and_wait(st, cb);
|
||||
dt->vkFreeCommandBuffers(st->dev, st->pool, 1, &cb);
|
||||
if (!submitted) {
|
||||
VKB_INFO(" %s, type %u: GPU submission failed", vkb_mem_strategy_name(s), type);
|
||||
goto out;
|
||||
}
|
||||
if (!coherent) dt->vkInvalidateMappedMemoryRanges(st->dev, 1, &whole);
|
||||
for (uint32_t i = 0; i < TEST_SIZE / 32; i++) {
|
||||
if (((volatile uint32_t *)cmap)[i] != 0xA5A5A5A5u) {
|
||||
VKB_INFO(" %s, type %u: the client does not see a GPU write (word %u = 0x%08x)", vkb_mem_strategy_name(s), type, i,
|
||||
((volatile uint32_t *)cmap)[i]);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
for (uint32_t i = 0; i < TEST_SIZE / 32; i++) {
|
||||
uint32_t want = 0x5a000000u ^ ((TEST_SIZE / 8 + i) * 2654435761u);
|
||||
uint32_t got = ((volatile uint32_t *)cmap)[TEST_SIZE / 16 * 3 + i];
|
||||
if (got != want) {
|
||||
VKB_INFO(" %s, type %u: the GPU does not see a client write (word %u = 0x%08x, want 0x%08x)", vkb_mem_strategy_name(s),
|
||||
type, i, got, want);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
result = coherent ? 1 : 2;
|
||||
VKB_INFO(" %s, type %u (flags 0x%x): shared OK%s", vkb_mem_strategy_name(s), type, st->mp.memoryTypes[type].propertyFlags,
|
||||
coherent ? ", coherent" : ", needs flushes");
|
||||
out:
|
||||
if (cmap) munmap(cmap, (size_t)m->size);
|
||||
if (smap) dt->vkUnmapMemory(st->dev, m->mem);
|
||||
dt->vkDestroyBuffer(st->dev, buf, NULL);
|
||||
dt->vkFreeMemory(st->dev, m->mem, NULL);
|
||||
mem_release_backing(m);
|
||||
return result;
|
||||
}
|
||||
|
||||
static int has_ext(const VkExtensionProperties *e, uint32_t n, const char *name)
|
||||
{
|
||||
for (uint32_t i = 0; i < n; i++)
|
||||
if (!strcmp(e[i].extensionName, name)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vkb_mem_selftest(vkb_pd_knowledge *k, VkInstance inst, const vkb_dispatch *idt, VkPhysicalDevice pd)
|
||||
{
|
||||
selftest st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
st.idt = idt;
|
||||
st.pd = pd;
|
||||
idt->vkGetPhysicalDeviceMemoryProperties(pd, &st.mp);
|
||||
|
||||
uint32_t nq = 0;
|
||||
idt->vkGetPhysicalDeviceQueueFamilyProperties(pd, &nq, NULL);
|
||||
VkQueueFamilyProperties qp[16];
|
||||
if (nq > 16) nq = 16;
|
||||
idt->vkGetPhysicalDeviceQueueFamilyProperties(pd, &nq, qp);
|
||||
st.qf = 0;
|
||||
for (uint32_t i = 0; i < nq; i++)
|
||||
if (qp[i].queueFlags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT)) {
|
||||
st.qf = i;
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t ne = 0;
|
||||
idt->vkEnumerateDeviceExtensionProperties(pd, NULL, &ne, NULL);
|
||||
VkExtensionProperties *ex = calloc(ne ? ne : 1, sizeof(*ex));
|
||||
idt->vkEnumerateDeviceExtensionProperties(pd, NULL, &ne, ex);
|
||||
|
||||
uint32_t candidates[3];
|
||||
int nc = 0;
|
||||
const char *force = getenv("VKBRIDGE_MEMORY");
|
||||
uint32_t order[] = {VKB_MEM_HOSTPTR, VKB_MEM_DMABUF, VKB_MEM_AHB};
|
||||
for (int i = 0; i < 3; i++) {
|
||||
uint32_t s = order[i];
|
||||
if (force && strcmp(force, vkb_mem_strategy_name(s))) continue;
|
||||
uint32_t n;
|
||||
const char *const *req = vkb_mem_required_extensions(s, &n);
|
||||
if (!req) continue;
|
||||
int ok = 1;
|
||||
for (uint32_t j = 0; j < n; j++)
|
||||
if (strcmp(req[j], "VK_KHR_external_memory") && strcmp(req[j], "VK_KHR_sampler_ycbcr_conversion") &&
|
||||
strcmp(req[j], "VK_KHR_dedicated_allocation") && strcmp(req[j], "VK_KHR_get_memory_requirements2") &&
|
||||
!has_ext(ex, ne, req[j]))
|
||||
ok = 0;
|
||||
if (ok) candidates[nc++] = s;
|
||||
else VKB_INFO("memory sharing: %s unavailable (driver lacks its extensions)", vkb_mem_strategy_name(s));
|
||||
}
|
||||
|
||||
/* One test device with every candidate's extensions. */
|
||||
const char *exts[16];
|
||||
uint32_t nx = 0;
|
||||
for (int i = 0; i < nc; i++) {
|
||||
uint32_t n;
|
||||
const char *const *req = vkb_mem_required_extensions(candidates[i], &n);
|
||||
for (uint32_t j = 0; j < n; j++) {
|
||||
if (!has_ext(ex, ne, req[j])) continue;
|
||||
int dup = 0;
|
||||
for (uint32_t q = 0; q < nx; q++) dup |= !strcmp(exts[q], req[j]);
|
||||
if (!dup && nx < 16) exts[nx++] = req[j];
|
||||
}
|
||||
}
|
||||
free(ex);
|
||||
float prio = 1.0f;
|
||||
VkDeviceQueueCreateInfo qci = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, NULL, 0, st.qf, 1, &prio};
|
||||
VkDeviceCreateInfo dci = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, NULL, 0, 1, &qci, 0, NULL, nx, exts, NULL};
|
||||
if (idt->vkCreateDevice(pd, &dci, NULL, &st.dev) != VK_SUCCESS) {
|
||||
VKB_ERR("memory self-test: cannot create a device");
|
||||
return;
|
||||
}
|
||||
st.dt = *idt;
|
||||
PFN_vkGetDeviceProcAddr gdpa = (PFN_vkGetDeviceProcAddr)vkb_gipa(inst, "vkGetDeviceProcAddr");
|
||||
vkb_dispatch_load_device(&st.dt, gdpa, st.dev);
|
||||
st.dt.vkGetDeviceQueue(st.dev, st.qf, 0, &st.queue);
|
||||
VkCommandPoolCreateInfo pci = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, NULL, 0, st.qf};
|
||||
st.dt.vkCreateCommandPool(st.dev, &pci, NULL, &st.pool);
|
||||
|
||||
VkPhysicalDeviceExternalMemoryHostPropertiesEXT hostp = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT};
|
||||
VkPhysicalDeviceProperties2 p2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &hostp};
|
||||
idt->vkGetPhysicalDeviceProperties2(pd, &p2);
|
||||
st.import_align = hostp.minImportedHostPointerAlignment ? hostp.minImportedHostPointerAlignment : 4096;
|
||||
if (st.import_align < 4096) st.import_align = 4096;
|
||||
|
||||
k->info.strategy = VKB_MEM_NONE;
|
||||
for (int i = 0; i < nc; i++) {
|
||||
uint32_t s = candidates[i];
|
||||
uint32_t shareable = 0, coherent = 0;
|
||||
for (uint32_t t = 0; t < st.mp.memoryTypeCount; t++) {
|
||||
if (!(st.mp.memoryTypes[t].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) continue;
|
||||
if (st.mp.memoryTypes[t].propertyFlags & VK_MEMORY_PROPERTY_PROTECTED_BIT) continue;
|
||||
int res = test_type(&st, s, t);
|
||||
if (res) shareable |= 1u << t;
|
||||
if (res == 1) coherent |= 1u << t;
|
||||
}
|
||||
if (coherent) {
|
||||
k->info.strategy = s;
|
||||
k->info.buffer_handle_types = strategy_handle_type(s);
|
||||
k->info.shareable_types = shareable;
|
||||
k->info.coherent_types = coherent;
|
||||
k->info.import_alignment = st.import_align;
|
||||
break;
|
||||
}
|
||||
VKB_INFO("memory sharing: %s failed the self-test", vkb_mem_strategy_name(s));
|
||||
}
|
||||
if (k->info.strategy == VKB_MEM_NONE)
|
||||
VKB_ERR("memory sharing: no method works on %s; Linux programs will get no host-visible memory", k->name);
|
||||
|
||||
st.dt.vkDestroyCommandPool(st.dev, st.pool, NULL);
|
||||
st.dt.vkDestroyDevice(st.dev, NULL);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* Descriptor update templates. The client repacks the app's data to the layout of the template
|
||||
* it created on the server (entries rewritten to packed offsets), so the blob is used as is.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include "vkb_server.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
void vkb_sv_vkbDescUpdateRaw(vkb_srv_call *c)
|
||||
{
|
||||
VkDevice dev = (VkDevice)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
VkDescriptorSet set;
|
||||
VkDescriptorUpdateTemplate tmpl;
|
||||
vkb_dec_bytes(&c->d, &set, sizeof(set));
|
||||
vkb_dec_bytes(&c->d, &tmpl, sizeof(tmpl));
|
||||
size_t n = 0;
|
||||
const void *data = vkb_dec_blob(&c->d, &n);
|
||||
if (c->d.err || !c->table) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
if (!c->dt->vkUpdateDescriptorSetWithTemplate) {
|
||||
vkb_srv_missing(c, "vkUpdateDescriptorSetWithTemplate");
|
||||
return;
|
||||
}
|
||||
c->dt->vkUpdateDescriptorSetWithTemplate(dev, set, tmpl, data);
|
||||
}
|
||||
|
||||
void vkb_sv_vkbPushDescRaw(vkb_srv_call *c)
|
||||
{
|
||||
VkCommandBuffer cb = (VkCommandBuffer)(uintptr_t)vkb_dec_u64(&c->d);
|
||||
VkDescriptorUpdateTemplate tmpl;
|
||||
VkPipelineLayout layout;
|
||||
vkb_dec_bytes(&c->d, &tmpl, sizeof(tmpl));
|
||||
vkb_dec_bytes(&c->d, &layout, sizeof(layout));
|
||||
uint32_t set = vkb_dec_u32(&c->d);
|
||||
size_t n = 0;
|
||||
const void *data = vkb_dec_blob(&c->d, &n);
|
||||
if (c->d.err || !c->table) {
|
||||
vkb_srv_bad_message(c);
|
||||
return;
|
||||
}
|
||||
if (!c->dt->vkCmdPushDescriptorSetWithTemplateKHR) {
|
||||
vkb_srv_missing(c, "vkCmdPushDescriptorSetWithTemplateKHR");
|
||||
return;
|
||||
}
|
||||
c->dt->vkCmdPushDescriptorSetWithTemplateKHR(cb, tmpl, layout, set, data);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
* DroidDeck Vulkan bridge - server side (bionic on the device, glibc for host tests).
|
||||
*
|
||||
* The server owns the real Vulkan objects. Linux programs in the session talk to it through the
|
||||
* client ICD (tools/vkbridge/client) over a Unix socket; every connection is one client thread.
|
||||
*/
|
||||
#ifndef VKB_SERVER_H
|
||||
#define VKB_SERVER_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "vkb_server_iface.h"
|
||||
#include "vkb_proto.h"
|
||||
#include "vkb_log.h"
|
||||
#include "gen/vkb_server_gen.h"
|
||||
|
||||
#define VKB_MAX_TABLES 65536
|
||||
#define VKB_MAX_PDS 16
|
||||
|
||||
/* A client process (all connections that said hello with the same token). */
|
||||
typedef struct vkb_srv_proc {
|
||||
uint64_t token;
|
||||
int pid;
|
||||
int conns;
|
||||
struct vkb_srv_proc *next;
|
||||
} vkb_srv_proc;
|
||||
|
||||
typedef struct vkb_srv_conn {
|
||||
int sock;
|
||||
vkb_srv_proc *proc;
|
||||
uint8_t *inbuf;
|
||||
size_t incap;
|
||||
vkb_arena arena;
|
||||
pthread_t thread;
|
||||
} vkb_srv_conn;
|
||||
|
||||
/* Startup knowledge about each physical device (keyed by deviceUUID). */
|
||||
typedef struct vkb_pd_knowledge {
|
||||
uint8_t uuid[VK_UUID_SIZE];
|
||||
char name[VK_MAX_PHYSICAL_DEVICE_NAME_SIZE];
|
||||
vkb_server_pd_info info;
|
||||
int valid;
|
||||
} vkb_pd_knowledge;
|
||||
|
||||
/* A tracked device memory allocation. */
|
||||
typedef struct vkb_srv_mem {
|
||||
VkDeviceMemory mem;
|
||||
VkDeviceSize size;
|
||||
uint32_t type;
|
||||
uint32_t strategy;
|
||||
void *host_ptr; /* hostptr: server's mapping of the client's memfd */
|
||||
size_t host_len;
|
||||
void *mapped; /* server-side vkMapMemory pointer (for flush/invalidate) */
|
||||
void *ahb; /* AHardwareBuffer* (Android) */
|
||||
struct vkb_srv_mem *next;
|
||||
} vkb_srv_mem;
|
||||
|
||||
struct vkb_emu_device;
|
||||
|
||||
typedef struct vkb_srv_table {
|
||||
uint32_t id;
|
||||
int is_device;
|
||||
vkb_srv_proc *proc;
|
||||
VkInstance instance;
|
||||
VkDevice device;
|
||||
VkPhysicalDevice physical; /* device tables */
|
||||
struct vkb_srv_table *parent; /* device -> instance */
|
||||
vkb_dispatch dt; /* what handlers call (may hold emulation wrappers) */
|
||||
vkb_dispatch real; /* the driver's own entry points */
|
||||
uint32_t api_version;
|
||||
const vkb_pd_knowledge *pd;
|
||||
pthread_mutex_t lock;
|
||||
vkb_srv_mem *mems[256]; /* hash by handle */
|
||||
struct vkb_emu_device *emu;
|
||||
} vkb_srv_table;
|
||||
|
||||
/* server_main.c */
|
||||
extern PFN_vkGetInstanceProcAddr vkb_gipa;
|
||||
extern vkb_dispatch vkb_global_dt;
|
||||
extern int vkb_verbose;
|
||||
|
||||
/* server_conn.c */
|
||||
void vkb_srv_serve(int listen_fd);
|
||||
vkb_srv_table *vkb_table_get(uint32_t id);
|
||||
uint32_t vkb_table_register(vkb_srv_table *t);
|
||||
void vkb_table_unregister(vkb_srv_table *t);
|
||||
void vkb_srv_reply_status(vkb_srv_call *c, uint32_t status);
|
||||
/* The table of the call being executed on this thread (emulation wrappers use it). */
|
||||
vkb_srv_table *vkb_srv_current_table(void);
|
||||
void vkb_srv_set_current_table(vkb_srv_table *t);
|
||||
void vkb_proc_cleanup(vkb_srv_proc *p);
|
||||
|
||||
/* server_instance.c */
|
||||
void vkb_selftest_all(void);
|
||||
const vkb_pd_knowledge *vkb_pd_lookup(const vkb_dispatch *dt, VkPhysicalDevice pd);
|
||||
extern vkb_pd_knowledge vkb_pds[VKB_MAX_PDS];
|
||||
extern int vkb_npds;
|
||||
|
||||
/* server_memory.c */
|
||||
void vkb_mem_selftest(vkb_pd_knowledge *k, VkInstance inst, const vkb_dispatch *idt, VkPhysicalDevice pd);
|
||||
const char *const *vkb_mem_required_extensions(uint32_t strategy, uint32_t *count);
|
||||
void vkb_mem_track(vkb_srv_table *dev, vkb_srv_mem *m);
|
||||
vkb_srv_mem *vkb_mem_find(vkb_srv_table *dev, VkDeviceMemory mem, int remove);
|
||||
void vkb_mem_release(vkb_srv_table *dev, vkb_srv_mem *m);
|
||||
void vkb_mem_free_all(vkb_srv_table *dev);
|
||||
|
||||
/* emulation (server_emu*.c) */
|
||||
uint32_t vkb_emu_detect(const vkb_dispatch *idt, VkPhysicalDevice pd);
|
||||
void vkb_emu_device_create_info(vkb_srv_table *inst, VkPhysicalDevice pd, uint32_t emu,
|
||||
VkDeviceCreateInfo *ci, vkb_arena *a);
|
||||
void vkb_emu_device_init(vkb_srv_table *dev, uint32_t emu);
|
||||
void vkb_emu_device_destroy(vkb_srv_table *dev);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user