From a4bd2eb27158f4344bca2c94bd621debeffe6e69 Mon Sep 17 00:00:00 2001 From: Ahmad Sayed Alsayed Ebrahim Date: Sat, 10 Oct 2026 05:39:32 +0300 Subject: [PATCH] perf(vkbridge): async requests with ordering, batched descriptor sets, buffered server reads - Ordered async protocol: vkb_msg_hdr seq/barrier, per-process ordering gate on the server; void commands and VkResult commands without outputs (submits, resets, binds, vkEndCommandBuffer replay) no longer wait for a reply. VKBRIDGE_SYNC=1 restores synchronous calls for debugging. - Client async queue batched across threads with a ~100us background flusher. - Descriptor sets allocated in batches per (pool, layout); the client keeps the pool's books (layout descriptor counts vs pool sizes) so batches ask only for what fits and a full pool answers OUT_OF_POOL_MEMORY without a round trip. Caches for a destroyed layout are dropped (its handle may be reused). - Server parses requests out of one receive buffer (descriptor FIFO for SCM_RIGHTS) instead of two reads per message. - Server logs async VkResult failures (generated vkb_cmd_result table) and per-command timing with VKBRIDGE_STATS=. - tests/d3d11_bench.c: game-like D3D11 frame (1000 draws, Map DISCARD, rebinds). d3d11_bench 1000 draws on RADV: native 0.30 ms/frame; bridge 26.3 ms (sync) -> 1.43 ms. Host suite 14/14 in plain and Mali-imitation modes. Co-Authored-By: Claude Opus 5.5 --- app/src/main/cpp/vkbridge/server_conn.c | 203 ++++++++++- app/src/main/cpp/vkbridge/vkb_server.h | 15 +- tools/vkbridge/client/cmdbuf.c | 5 +- tools/vkbridge/client/conn.c | 218 +++++++++++- tools/vkbridge/client/descriptor.c | 296 +++++++++++++++- tools/vkbridge/client/memory.c | 2 +- tools/vkbridge/client/vkb_client.h | 4 + tools/vkbridge/codegen/gen_vkbridge.py | 26 ++ tools/vkbridge/common/gen/vkb_client_gen.h | 6 + tools/vkbridge/common/gen/vkb_gen.h | 1 + tools/vkbridge/common/gen/vkb_gen_client.c | 370 ++++---------------- tools/vkbridge/common/gen/vkb_gen_structs.c | 292 +++++++++++++++ tools/vkbridge/common/vkb_wire.h | 7 +- tools/vkbridge/tests/d3d11_bench.c | 116 ++++++ 14 files changed, 1247 insertions(+), 314 deletions(-) create mode 100644 tools/vkbridge/tests/d3d11_bench.c diff --git a/app/src/main/cpp/vkbridge/server_conn.c b/app/src/main/cpp/vkbridge/server_conn.c index e85cb3b59..d2836fb34 100644 --- a/app/src/main/cpp/vkbridge/server_conn.c +++ b/app/src/main/cpp/vkbridge/server_conn.c @@ -11,6 +11,7 @@ #include #include #include +#include #include static pthread_mutex_t tables_lock = PTHREAD_MUTEX_INITIALIZER; @@ -126,6 +127,8 @@ static vkb_srv_proc *proc_join(uint64_t token, int pid) p = calloc(1, sizeof(*p)); p->token = token; p->pid = pid; + pthread_mutex_init(&p->olock, NULL); + pthread_cond_init(&p->ocond, NULL); p->next = procs; procs = p; VKB_INFO("client process %d connected", pid); @@ -148,10 +151,53 @@ static void proc_leave(vkb_srv_proc *p) if (last) { vkb_proc_cleanup(p); VKB_INFO("client process %d gone", p->pid); + pthread_mutex_destroy(&p->olock); + pthread_cond_destroy(&p->ocond); free(p); + } else { + pthread_mutex_lock(&p->olock); + p->conn_closed = 1; + pthread_cond_broadcast(&p->ocond); + pthread_mutex_unlock(&p->olock); } } +/* Waits until every async request of the process up to `barrier` has run. */ +static void order_wait(vkb_srv_proc *p, uint64_t barrier) +{ + if (!p || !barrier) return; + pthread_mutex_lock(&p->olock); + int waited = 0; + while (p->watermark < barrier) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += 1; + if (pthread_cond_timedwait(&p->ocond, &p->olock, &ts) != 0 && ++waited >= 3 && p->conn_closed) { + /* A connection of this process is gone with requests it never sent: go on. */ + VKB_WARN("process %d: async request %llu never arrived; continuing", p->pid, (unsigned long long)(p->watermark + 1)); + p->watermark = barrier; + memset(p->done, 0, sizeof(p->done)); + break; + } + } + pthread_mutex_unlock(&p->olock); +} + +static void order_done(vkb_srv_proc *p, uint64_t seq) +{ + if (!p || !seq) return; + pthread_mutex_lock(&p->olock); + if (seq > p->watermark) { + p->done[seq % VKB_SEQ_RING] = 1; + while (p->done[(p->watermark + 1) % VKB_SEQ_RING]) { + p->done[(p->watermark + 1) % VKB_SEQ_RING] = 0; + p->watermark++; + } + pthread_cond_broadcast(&p->ocond); + } + pthread_mutex_unlock(&p->olock); +} + /* ------------------------------------------------------------------ command buffer replay */ static int is_stream_cmd(uint32_t id) @@ -238,8 +284,144 @@ void vkb_sv_vkbHello(vkb_srv_call *c) vkb_enc_u32(&c->r, 1); } +/* ------------------------------------------------------------------ statistics */ + +/* VKBRIDGE_STATS= on the server: time spent per command, waiting for order, idle. */ +static uint64_t st_ns[VKB_CMD_COUNT], st_n[VKB_CMD_COUNT], st_wait_ns, st_last; +static int st_period = -1; + +static uint64_t mono_ns(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; +} + +static void stats_add(uint32_t cmd, uint64_t ns, uint64_t wait_ns) +{ + if (st_period < 0) { + const char *v = getenv("VKBRIDGE_STATS"); + st_period = v ? atoi(v) : 0; + } + if (!st_period) return; + __atomic_add_fetch(&st_ns[cmd], ns, __ATOMIC_RELAXED); + __atomic_add_fetch(&st_n[cmd], 1, __ATOMIC_RELAXED); + __atomic_add_fetch(&st_wait_ns, wait_ns, __ATOMIC_RELAXED); + uint64_t now = mono_ns(), last = __atomic_load_n(&st_last, __ATOMIC_RELAXED); + if (!last) { + __atomic_store_n(&st_last, now, __ATOMIC_RELAXED); + return; + } + if (now - last < (uint64_t)st_period * 1000000000ull || !__atomic_compare_exchange_n(&st_last, &last, now, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) + return; + uint64_t total = 0; + for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) total += st_ns[i]; + VKB_INFO("stats over %ds: %.1f ms in handlers, %.1f ms waiting for order", st_period, total / 1e6, st_wait_ns / 1e6); + for (int k = 0; k < 8; k++) { + uint64_t best = 0; + uint32_t bi = 0; + for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) + if (st_ns[i] > best) { + best = st_ns[i]; + bi = i; + } + if (!best) break; + VKB_INFO(" %-40s %8llu calls %9.2f ms (%.2f us each)", vkb_cmd_names[bi], (unsigned long long)st_n[bi], best / 1e6, + best / 1e3 / (double)st_n[bi]); + st_ns[bi] = 0; + } + memset(st_ns, 0, sizeof(st_ns)); + memset(st_n, 0, sizeof(st_n)); + st_wait_ns = 0; +} + /* ------------------------------------------------------------------ connection loop */ +/* Reads the next request. Buffered: a burst of small asynchronous requests costs one recvmsg, not + * two reads each. Descriptors arrive with the first bytes of their message, so they queue up in + * order and each header takes its nfds from the front. The payload points into the buffer and + * stays valid until the next call. */ +static int srv_recv(vkb_srv_conn *conn, vkb_msg_hdr *h, uint8_t **payload, int *fds, int *nfds) +{ + *nfds = 0; + if (conn->rpos == conn->rlen && conn->rcap > (16u << 20)) { + free(conn->rb); + conn->rb = NULL; + conn->rcap = conn->rpos = conn->rlen = 0; + } + for (;;) { + size_t avail = conn->rlen - conn->rpos; + size_t need = sizeof(*h); + if (avail >= sizeof(*h)) { + memcpy(h, conn->rb + conn->rpos, sizeof(*h)); + if (h->magic != VKB_MAGIC) { + VKB_ERR("transport: bad message magic 0x%08x", h->magic); + return -1; + } + if (h->size > (1u << 30) || h->nfds > VKB_MAX_FDS) { + VKB_ERR("transport: bad message (%u bytes, %u descriptors)", h->size, h->nfds); + return -1; + } + need += h->size; + if (avail >= need) { + if ((int)h->nfds > conn->fdn) { + VKB_ERR("transport: message arrived without its %u descriptors", h->nfds); + return -1; + } + *nfds = (int)h->nfds; + memcpy(fds, conn->fdq, sizeof(int) * h->nfds); + conn->fdn -= (int)h->nfds; + memmove(conn->fdq, conn->fdq + h->nfds, sizeof(int) * conn->fdn); + *payload = conn->rb + conn->rpos + sizeof(*h); + conn->rpos += need; + return 0; + } + } + if (conn->rpos) { + memmove(conn->rb, conn->rb + conn->rpos, avail); + conn->rpos = 0; + conn->rlen = avail; + } + if (need < (256u << 10)) need = 256u << 10; + if (need > conn->rcap) { + size_t nc = conn->rcap ? conn->rcap : (256u << 10); + while (nc < need) nc *= 2; + uint8_t *nb = realloc(conn->rb, nc); + if (!nb) return -1; + conn->rb = nb; + conn->rcap = nc; + } + union { + char buf[CMSG_SPACE(sizeof(int) * VKB_MAX_FDS)]; + struct cmsghdr align; + } cm; + struct iovec iov = {conn->rb + conn->rlen, conn->rcap - conn->rlen}; + struct msghdr msg; + memset(&msg, 0, sizeof(msg)); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = cm.buf; + msg.msg_controllen = sizeof(cm.buf); + ssize_t r; + do { + r = recvmsg(conn->sock, &msg, MSG_CMSG_CLOEXEC); + } while (r < 0 && errno == EINTR); + if (r <= 0) return -1; + for (struct cmsghdr *cmh = CMSG_FIRSTHDR(&msg); cmh; cmh = CMSG_NXTHDR(&msg, cmh)) { + if (cmh->cmsg_level != SOL_SOCKET || cmh->cmsg_type != SCM_RIGHTS) continue; + int n = (int)((cmh->cmsg_len - CMSG_LEN(0)) / sizeof(int)); + for (int i = 0; i < n; i++) { + int fd; + memcpy(&fd, CMSG_DATA(cmh) + i * sizeof(int), sizeof(int)); + if (conn->fdn < (int)(sizeof(conn->fdq) / sizeof(conn->fdq[0]))) conn->fdq[conn->fdn++] = fd; + else close(fd); + } + } + if (msg.msg_flags & MSG_CTRUNC) VKB_WARN("transport: descriptors truncated"); + conn->rlen += (size_t)r; + } +} + static void *conn_main(void *arg) { vkb_srv_conn *conn = arg; @@ -249,9 +431,10 @@ static void *conn_main(void *arg) for (;;) { vkb_msg_hdr h; int nfds = 0; - if (vkb_recv_msg(conn->sock, &h, &conn->inbuf, &conn->incap, fds, &nfds) < 0) break; + uint8_t *payload; + if (srv_recv(conn, &h, &payload, fds, &nfds) < 0) break; vkb_enc_reset(&c.r); - vkb_dec_init(&c.d, conn->inbuf, h.size, &conn->arena, fds, nfds); + vkb_dec_init(&c.d, payload, h.size, &conn->arena, fds, nfds); c.conn = conn; c.status = VKB_ST_OK; c.nclose = 0; @@ -275,18 +458,23 @@ static void *conn_main(void *arg) } } if (c.status == VKB_ST_OK) { + uint64_t t0 = st_period ? mono_ns() : 0; + order_wait(conn->proc, h.barrier); + uint64_t t1 = st_period ? mono_ns() : 0; vkb_srv_set_current_table(c.table); if (vkb_verbose) VKB_DBG("-> %s", vkb_cmd_names[h.cmd]); vkb_srv_current_cmd = vkb_cmd_names[h.cmd]; vkb_srv_handlers[h.cmd](&c); vkb_srv_current_cmd = NULL; vkb_srv_set_current_table(NULL); + if (st_period) stats_add(h.cmd, mono_ns() - t1, t1 - t0); } } vkb_dec_close_untaken(&c.d); + order_done(conn->proc, h.seq); 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}; + c.status == VKB_ST_OK ? (uint32_t)c.r.nfds : 0, 0, 0, 0}; if (c.r.oom) { rh.cmd = VKB_ST_BAD_MESSAGE; rh.size = 0; @@ -298,13 +486,19 @@ static void *conn_main(void *arg) } } else if (c.status != VKB_ST_OK) { VKB_ERR("asynchronous %s failed (status %u)", vkb_cmd_names[h.cmd], c.status); + } else if (vkb_cmd_result[h.cmd] && c.r.len >= sizeof(int32_t)) { + /* The client already told the app VK_SUCCESS; a failure can only be reported here. */ + int32_t vr; + memcpy(&vr, c.r.buf, sizeof(vr)); + if (vr < 0) VKB_ERR("asynchronous %s returned %d", vkb_cmd_names[h.cmd], vr); } 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); + free(conn->rb); + for (int i = 0; i < conn->fdn; i++) close(conn->fdq[i]); vkb_arena_free(&conn->arena); if (conn->proc) proc_leave(conn->proc); free(conn); @@ -313,6 +507,7 @@ static void *conn_main(void *arg) void vkb_srv_serve(int lfd) { + stats_add(0, 0, 0); for (;;) { int s = accept4(lfd, NULL, NULL, SOCK_CLOEXEC); if (s < 0) { diff --git a/app/src/main/cpp/vkbridge/vkb_server.h b/app/src/main/cpp/vkbridge/vkb_server.h index 331cd8616..8c5fa9a56 100644 --- a/app/src/main/cpp/vkbridge/vkb_server.h +++ b/app/src/main/cpp/vkbridge/vkb_server.h @@ -20,18 +20,29 @@ #define VKB_MAX_PDS 16 /* A client process (all connections that said hello with the same token). */ +#define VKB_SEQ_RING 65536 + typedef struct vkb_srv_proc { uint64_t token; int pid; int conns; struct vkb_srv_proc *next; + /* Ordering of the process's asynchronous requests (vkb_msg_hdr.seq/barrier). */ + pthread_mutex_t olock; + pthread_cond_t ocond; + uint64_t watermark; /* every async seq <= this has run */ + uint8_t done[VKB_SEQ_RING]; /* seqs above the watermark that have run */ + int conn_closed; /* a connection ended (a seq may never arrive) */ } vkb_srv_proc; typedef struct vkb_srv_conn { int sock; vkb_srv_proc *proc; - uint8_t *inbuf; - size_t incap; + /* Receive buffer: messages are parsed out of it in place (srv_recv). */ + uint8_t *rb; + size_t rcap, rpos, rlen; + int fdq[2 * VKB_MAX_FDS]; /* descriptors received ahead of their message's turn */ + int fdn; vkb_arena arena; pthread_t thread; } vkb_srv_conn; diff --git a/tools/vkbridge/client/cmdbuf.c b/tools/vkbridge/client/cmdbuf.c index 349b89703..5d7e7f624 100644 --- a/tools/vkbridge/client/cmdbuf.c +++ b/tools/vkbridge/client/cmdbuf.c @@ -192,7 +192,10 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkEndCommandBuffer(VkCommandBuffer command 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_call_exec(&c)) { + 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); } diff --git a/tools/vkbridge/client/conn.c b/tools/vkbridge/client/conn.c index 0a7321011..b48fb8190 100644 --- a/tools/vkbridge/client/conn.c +++ b/tools/vkbridge/client/conn.c @@ -79,6 +79,8 @@ typedef struct vkb_conn { } 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; @@ -99,11 +101,102 @@ 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 */ } @@ -111,6 +204,7 @@ 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(); } @@ -172,7 +266,7 @@ static int do_connect(vkb_conn *c) fclose(f); } vkb_enc_str(&e, comm); - vkb_msg_hdr h = {VKB_MAGIC, (uint32_t)e.len, VKB_CMD_vkbHello, 0, 0, 0}; + 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; @@ -279,8 +373,62 @@ int vkb_call_exec(vkb_call *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}; - if (vkb_send_msg(conn->sock, &h, c->e.buf, c->e.fds, c->e.nfds) < 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); @@ -323,6 +471,70 @@ void vkb_call_end(vkb_call *c) 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=: 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; diff --git a/tools/vkbridge/client/descriptor.c b/tools/vkbridge/client/descriptor.c index 24bcceba4..2a18f5246 100644 --- a/tools/vkbridge/client/descriptor.c +++ b/tools/vkbridge/client/descriptor.c @@ -134,7 +134,7 @@ VKAPI_ATTR void VKAPI_CALL vkb_ep_vkUpdateDescriptorSetWithTemplate(VkDevice dev 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); - if (vkb_call_exec(&c)) vkb_call_end(&c); + vkb_call_exec_async(&c); } VKAPI_ATTR void VKAPI_CALL vkb_ep_vkCmdPushDescriptorSetWithTemplate(VkCommandBuffer commandBuffer, VkDescriptorUpdateTemplate tmpl, @@ -306,3 +306,297 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkTransitionImageLayout(VkDevice device, u (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; +} + +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); + } + 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); +} diff --git a/tools/vkbridge/client/memory.c b/tools/vkbridge/client/memory.c index 9dae36b8d..903c8bc32 100644 --- a/tools/vkbridge/client/memory.c +++ b/tools/vkbridge/client/memory.c @@ -370,7 +370,7 @@ VKAPI_ATTR void VKAPI_CALL vkb_ep_vkFreeMemory(VkDevice device, VkDeviceMemory m 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); - if (vkb_call_exec(&c)) vkb_call_end(&c); + vkb_call_exec_async(&c); if (m) vkb_memory_unmap_free(m); } diff --git a/tools/vkbridge/client/vkb_client.h b/tools/vkbridge/client/vkb_client.h index b40455f03..37682316e 100644 --- a/tools/vkbridge/client/vkb_client.h +++ b/tools/vkbridge/client/vkb_client.h @@ -145,6 +145,10 @@ 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. */ diff --git a/tools/vkbridge/codegen/gen_vkbridge.py b/tools/vkbridge/codegen/gen_vkbridge.py index c181c6a15..cb6592ba7 100644 --- a/tools/vkbridge/codegen/gen_vkbridge.py +++ b/tools/vkbridge/codegen/gen_vkbridge.py @@ -77,6 +77,8 @@ LOCAL_COMMANDS_PREFIX = ('vkCreateDebugUtilsMessengerEXT', 'vkDestroyDebugUtilsM # Commands whose client entry point is hand-written (the generated wire stub may still be used). CLIENT_CUSTOM = { + 'vkAllocateDescriptorSets', 'vkResetDescriptorPool', 'vkDestroyDescriptorPool', 'vkCreateDescriptorPool', + 'vkCreateDescriptorSetLayout', 'vkDestroyDescriptorSetLayout', 'vkCreateInstance', 'vkDestroyInstance', 'vkEnumerateInstanceExtensionProperties', 'vkEnumerateInstanceLayerProperties', 'vkEnumerateInstanceVersion', 'vkGetInstanceProcAddr', 'vkGetDeviceProcAddr', 'vkEnumeratePhysicalDevices', 'vkEnumeratePhysicalDeviceGroups', @@ -119,6 +121,12 @@ SERVER_CUSTOM = { 'vkFlushMappedMemoryRanges', 'vkInvalidateMappedMemoryRanges', 'vkEndCommandBuffer', } +# VkResult commands sent without waiting (VK_SUCCESS returned at once; a failure is logged by the +# server). What an app does next with their objects is ordered after them by the protocol. +ASYNC_RESULT = {'vkQueueSubmit', 'vkQueueSubmit2', 'vkResetDescriptorPool', 'vkResetCommandPool', + 'vkResetCommandBuffer', 'vkResetFences', 'vkResetEvent', 'vkSetEvent', 'vkFreeDescriptorSets', + 'vkBindBufferMemory', 'vkBindBufferMemory2', 'vkBindImageMemory', 'vkBindImageMemory2'} + # File descriptors: (struct, member) and (command, param). FD_MEMBERS = {('VkImportMemoryFdInfoKHR', 'fd'), ('VkImportSemaphoreFdInfoKHR', 'fd'), ('VkImportFenceFdInfoKHR', 'fd')} @@ -871,6 +879,7 @@ for i, c in enumerate(cmd_ids): hw(f'VKB_CMD_COUNT = {len(cmd_ids)}') hw('};') hw('extern const char *const vkb_cmd_names[VKB_CMD_COUNT];') +hw('extern const unsigned char vkb_cmd_result[VKB_CMD_COUNT]; /* returns VkResult (first in the reply) */') hw() hw('/* Every bridged command, by its canonical name (aliases resolve to these). */') hw('typedef struct vkb_dispatch {') @@ -907,6 +916,10 @@ names('const char *const vkb_cmd_names[VKB_CMD_COUNT] = {') for c in cmd_ids: names(f'"{c}",') names('};') +names('const unsigned char vkb_cmd_result[VKB_CMD_COUNT] = {') +for c in cmd_ids: + names(f'{1 if c in reg.commands and reg.commands[c]["ret"] == "VkResult" else 0},') +names('};') names('const vkb_ext_desc vkb_exts[VKB_EXT_COUNT] = {') for e in bridged_exts: names(f'{{"{e}", {1 if ext_info[e][0] == "device" else 0}, {1 if e in CLIENT_EXTENSIONS else 0}}},') @@ -1085,6 +1098,19 @@ for c in wire_cmds: cw(f'vkb_cmd_record_end({first});') cw('}') cw() + elif ret == 'void' and not outs and not any((c, p.name) in FD_PARAMS for p in params): + # Nothing comes back: sent without waiting (ordered by the protocol's sequence numbers). + cw('vkb_call_exec_async(&call_);') + cw('}') + cw() + elif c in ASYNC_RESULT and not outs: + cw('if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST;') + cw('if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST;') + cw('VkResult r_ = (VkResult)vkb_dec_u32(&call_.d);') + cw('vkb_call_end(&call_);') + cw('return r_;') + cw('}') + cw() else: cw('if (!vkb_call_exec(&call_)) {') if ret == 'VkResult': diff --git a/tools/vkbridge/common/gen/vkb_client_gen.h b/tools/vkbridge/common/gen/vkb_client_gen.h index eb843dddf..572009749 100644 --- a/tools/vkbridge/common/gen/vkb_client_gen.h +++ b/tools/vkbridge/common/gen/vkb_client_gen.h @@ -292,6 +292,7 @@ VkResult vkb_wire_vkWaitSemaphores(VkDevice device, const VkSemaphoreWaitInfo* p VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAcquireNextImage2KHR(VkDevice device, const VkAcquireNextImageInfoKHR* pAcquireInfo, uint32_t* pImageIndex); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers); +VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pDescriptorSets); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo* pBeginInfo); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset); @@ -309,6 +310,8 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCopyImageToMemory(VkDevice device, const VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCopyMemoryToImage(VkDevice device, const VkCopyMemoryToImageInfo* pCopyMemoryToImageInfo); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pMessenger); +VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorPool(VkDevice device, const VkDescriptorPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDescriptorPool* pDescriptorPool); +VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDescriptorSetLayout* pSetLayout); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorUpdateTemplate(VkDevice device, const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateHeadlessSurfaceEXT(VkInstance instance, const VkHeadlessSurfaceCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSurfaceKHR* pSurface); @@ -322,6 +325,8 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateXlibSurfaceKHR(VkInstance instance VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks* pAllocator); VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator); VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT messenger, const VkAllocationCallbacks* pAllocator); +VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, const VkAllocationCallbacks* pAllocator); +VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout descriptorSetLayout, const VkAllocationCallbacks* pAllocator); VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorUpdateTemplate(VkDevice device, VkDescriptorUpdateTemplate descriptorUpdateTemplate, const VkAllocationCallbacks* pAllocator); VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator); VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks* pAllocator); @@ -403,6 +408,7 @@ VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueueSubmit2(VkQueue queue, uint32_t sub VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueueWaitIdle(VkQueue queue); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetCommandBuffer(VkCommandBuffer commandBuffer, VkCommandBufferResetFlags flags); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags); +VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkSetDebugUtilsObjectNameEXT(VkDevice device, const VkDebugUtilsObjectNameInfoEXT* pNameInfo); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkSetDebugUtilsObjectTagEXT(VkDevice device, const VkDebugUtilsObjectTagInfoEXT* pTagInfo); VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkSetPrivateData(VkDevice device, VkObjectType objectType, uint64_t objectHandle, VkPrivateDataSlot privateDataSlot, uint64_t data); diff --git a/tools/vkbridge/common/gen/vkb_gen.h b/tools/vkbridge/common/gen/vkb_gen.h index 4df87b7d1..422fb53fa 100644 --- a/tools/vkbridge/common/gen/vkb_gen.h +++ b/tools/vkbridge/common/gen/vkb_gen.h @@ -307,6 +307,7 @@ enum vkb_cmd_id { VKB_CMD_COUNT = 290 }; extern const char *const vkb_cmd_names[VKB_CMD_COUNT]; +extern const unsigned char vkb_cmd_result[VKB_CMD_COUNT]; /* returns VkResult (first in the reply) */ /* Every bridged command, by its canonical name (aliases resolve to these). */ typedef struct vkb_dispatch { diff --git a/tools/vkbridge/common/gen/vkb_gen_client.c b/tools/vkbridge/common/gen/vkb_gen_client.c index 921fc9c62..8a9468f68 100644 --- a/tools/vkbridge/common/gen/vkb_gen_client.c +++ b/tools/vkbridge/common/gen/vkb_gen_client.c @@ -143,13 +143,9 @@ VkResult vkb_wire_vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceM vkb_enc_bytes(e, &buffer, sizeof(buffer)); vkb_enc_bytes(e, &memory, sizeof(memory)); vkb_enc_bytes(e, &memoryOffset, sizeof(memoryOffset)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -168,13 +164,9 @@ VkResult vkb_wire_vkBindBufferMemory2(VkDevice device, uint32_t bindInfoCount, c } else { vkb_enc_u8(e, 0); } - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -188,13 +180,9 @@ VkResult vkb_wire_vkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemo vkb_enc_bytes(e, &image, sizeof(image)); vkb_enc_bytes(e, &memory, sizeof(memory)); vkb_enc_bytes(e, &memoryOffset, sizeof(memoryOffset)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -213,13 +201,9 @@ VkResult vkb_wire_vkBindImageMemory2(VkDevice device, uint32_t bindInfoCount, co } else { vkb_enc_u8(e, 0); } - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -2950,12 +2934,7 @@ void vkb_wire_vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocati vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &buffer, sizeof(buffer)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyBufferView(VkDevice device, VkBufferView bufferView, const VkAllocationCallbacks* pAllocator) @@ -2966,12 +2945,7 @@ void vkb_wire_vkDestroyBufferView(VkDevice device, VkBufferView bufferView, cons vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &bufferView, sizeof(bufferView)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator) @@ -2982,12 +2956,7 @@ void vkb_wire_vkDestroyCommandPool(VkDevice device, VkCommandPool commandPool, c vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &commandPool, sizeof(commandPool)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, const VkAllocationCallbacks* pAllocator) @@ -2998,12 +2967,7 @@ void vkb_wire_vkDestroyDescriptorPool(VkDevice device, VkDescriptorPool descript vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &descriptorPool, sizeof(descriptorPool)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout descriptorSetLayout, const VkAllocationCallbacks* pAllocator) @@ -3014,12 +2978,7 @@ void vkb_wire_vkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayou vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &descriptorSetLayout, sizeof(descriptorSetLayout)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyDescriptorUpdateTemplate(VkDevice device, VkDescriptorUpdateTemplate descriptorUpdateTemplate, const VkAllocationCallbacks* pAllocator) @@ -3030,12 +2989,7 @@ void vkb_wire_vkDestroyDescriptorUpdateTemplate(VkDevice device, VkDescriptorUpd vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &descriptorUpdateTemplate, sizeof(descriptorUpdateTemplate)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) @@ -3045,12 +2999,7 @@ void vkb_wire_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAll vkb_enc *e = &call_.e; vkb_enc_u64(e, vkb_remote((const void *)device)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks* pAllocator) @@ -3061,12 +3010,7 @@ void vkb_wire_vkDestroyEvent(VkDevice device, VkEvent event, const VkAllocationC vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &event, sizeof(event)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyFence(VkDevice device, VkFence fence, const VkAllocationCallbacks* pAllocator) @@ -3077,12 +3021,7 @@ void vkb_wire_vkDestroyFence(VkDevice device, VkFence fence, const VkAllocationC vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &fence, sizeof(fence)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, const VkAllocationCallbacks* pAllocator) @@ -3093,12 +3032,7 @@ void vkb_wire_vkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, c vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &framebuffer, sizeof(framebuffer)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks* pAllocator) @@ -3109,12 +3043,7 @@ void vkb_wire_vkDestroyImage(VkDevice device, VkImage image, const VkAllocationC vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &image, sizeof(image)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyImageView(VkDevice device, VkImageView imageView, const VkAllocationCallbacks* pAllocator) @@ -3125,12 +3054,7 @@ void vkb_wire_vkDestroyImageView(VkDevice device, VkImageView imageView, const V vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &imageView, sizeof(imageView)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator) @@ -3140,12 +3064,7 @@ void vkb_wire_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks vkb_enc *e = &call_.e; vkb_enc_u64(e, vkb_remote((const void *)instance)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationCallbacks* pAllocator) @@ -3156,12 +3075,7 @@ void vkb_wire_vkDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAl vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &pipeline, sizeof(pipeline)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyPipelineCache(VkDevice device, VkPipelineCache pipelineCache, const VkAllocationCallbacks* pAllocator) @@ -3172,12 +3086,7 @@ void vkb_wire_vkDestroyPipelineCache(VkDevice device, VkPipelineCache pipelineCa vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &pipelineCache, sizeof(pipelineCache)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyPipelineLayout(VkDevice device, VkPipelineLayout pipelineLayout, const VkAllocationCallbacks* pAllocator) @@ -3188,12 +3097,7 @@ void vkb_wire_vkDestroyPipelineLayout(VkDevice device, VkPipelineLayout pipeline vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &pipelineLayout, sizeof(pipelineLayout)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyQueryPool(VkDevice device, VkQueryPool queryPool, const VkAllocationCallbacks* pAllocator) @@ -3204,12 +3108,7 @@ void vkb_wire_vkDestroyQueryPool(VkDevice device, VkQueryPool queryPool, const V vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &queryPool, sizeof(queryPool)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks* pAllocator) @@ -3220,12 +3119,7 @@ void vkb_wire_vkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, cons vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &renderPass, sizeof(renderPass)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroySampler(VkDevice device, VkSampler sampler, const VkAllocationCallbacks* pAllocator) @@ -3236,12 +3130,7 @@ void vkb_wire_vkDestroySampler(VkDevice device, VkSampler sampler, const VkAlloc vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &sampler, sizeof(sampler)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroySamplerYcbcrConversion(VkDevice device, VkSamplerYcbcrConversion ycbcrConversion, const VkAllocationCallbacks* pAllocator) @@ -3252,12 +3141,7 @@ void vkb_wire_vkDestroySamplerYcbcrConversion(VkDevice device, VkSamplerYcbcrCon vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &ycbcrConversion, sizeof(ycbcrConversion)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroySemaphore(VkDevice device, VkSemaphore semaphore, const VkAllocationCallbacks* pAllocator) @@ -3268,12 +3152,7 @@ void vkb_wire_vkDestroySemaphore(VkDevice device, VkSemaphore semaphore, const V vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &semaphore, sizeof(semaphore)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyShaderModule(VkDevice device, VkShaderModule shaderModule, const VkAllocationCallbacks* pAllocator) @@ -3284,12 +3163,7 @@ void vkb_wire_vkDestroyShaderModule(VkDevice device, VkShaderModule shaderModule vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &shaderModule, sizeof(shaderModule)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyTensorARM(VkDevice device, VkTensorARM tensor, const VkAllocationCallbacks* pAllocator) @@ -3300,12 +3174,7 @@ void vkb_wire_vkDestroyTensorARM(VkDevice device, VkTensorARM tensor, const VkAl vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &tensor, sizeof(tensor)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkDestroyTensorViewARM(VkDevice device, VkTensorViewARM tensorView, const VkAllocationCallbacks* pAllocator) @@ -3316,12 +3185,7 @@ void vkb_wire_vkDestroyTensorViewARM(VkDevice device, VkTensorViewARM tensorView vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &tensorView, sizeof(tensorView)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } VkResult vkb_wire_vkDeviceWaitIdle(VkDevice device) @@ -3653,12 +3517,7 @@ void vkb_wire_vkFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, u } else { vkb_enc_u8(e, 0); } - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } VkResult vkb_wire_vkFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount, const VkDescriptorSet* pDescriptorSets) @@ -3676,13 +3535,9 @@ VkResult vkb_wire_vkFreeDescriptorSets(VkDevice device, VkDescriptorPool descrip } else { vkb_enc_u8(e, 0); } - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -3695,12 +3550,7 @@ void vkb_wire_vkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAlloc vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &memory, sizeof(memory)); (void)pAllocator; - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } VkDeviceAddress vkb_wire_vkGetBufferDeviceAddress(VkDevice device, const VkBufferDeviceAddressInfo* pInfo) @@ -5408,13 +5258,9 @@ VkResult vkb_wire_vkQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSub vkb_enc_u8(e, 0); } vkb_enc_bytes(e, &fence, sizeof(fence)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5434,13 +5280,9 @@ VkResult vkb_wire_vkQueueSubmit2(VkQueue queue, uint32_t submitCount, const VkSu vkb_enc_u8(e, 0); } vkb_enc_bytes(e, &fence, sizeof(fence)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5469,13 +5311,9 @@ VkResult vkb_wire_vkResetCommandBuffer(VkCommandBuffer commandBuffer, VkCommandB vkb_enc *e = &call_.e; vkb_enc_u64(e, vkb_remote((const void *)commandBuffer)); vkb_enc_bytes(e, &flags, sizeof(flags)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5488,13 +5326,9 @@ VkResult vkb_wire_vkResetCommandPool(VkDevice device, VkCommandPool commandPool, vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &commandPool, sizeof(commandPool)); vkb_enc_bytes(e, &flags, sizeof(flags)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5507,13 +5341,9 @@ VkResult vkb_wire_vkResetDescriptorPool(VkDevice device, VkDescriptorPool descri vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &descriptorPool, sizeof(descriptorPool)); vkb_enc_bytes(e, &flags, sizeof(flags)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5525,13 +5355,9 @@ VkResult vkb_wire_vkResetEvent(VkDevice device, VkEvent event) vkb_enc *e = &call_.e; vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &event, sizeof(event)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5550,13 +5376,9 @@ VkResult vkb_wire_vkResetFences(VkDevice device, uint32_t fenceCount, const VkFe } else { vkb_enc_u8(e, 0); } - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5570,12 +5392,7 @@ void vkb_wire_vkResetQueryPool(VkDevice device, VkQueryPool queryPool, uint32_t vkb_enc_bytes(e, &queryPool, sizeof(queryPool)); vkb_enc_bytes(e, &firstQuery, sizeof(firstQuery)); vkb_enc_bytes(e, &queryCount, sizeof(queryCount)); - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkSetDeviceMemoryPriorityEXT(VkDevice device, VkDeviceMemory memory, float priority) @@ -5586,12 +5403,7 @@ void vkb_wire_vkSetDeviceMemoryPriorityEXT(VkDevice device, VkDeviceMemory memor vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &memory, sizeof(memory)); vkb_enc_bytes(e, &priority, sizeof(priority)); - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } VkResult vkb_wire_vkSetEvent(VkDevice device, VkEvent event) @@ -5601,13 +5413,9 @@ VkResult vkb_wire_vkSetEvent(VkDevice device, VkEvent event) vkb_enc *e = &call_.e; vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &event, sizeof(event)); - if (!vkb_call_exec(&call_)) { - return VK_ERROR_DEVICE_LOST; - } - vkb_dec *d = &call_.d; - (void)d; - VkResult r_; - vkb_dec_bytes(d, &r_, sizeof(r_)); + if (vkb_async_results()) return vkb_call_exec_async(&call_) ? VK_SUCCESS : VK_ERROR_DEVICE_LOST; + if (!vkb_call_exec(&call_)) return VK_ERROR_DEVICE_LOST; + VkResult r_ = (VkResult)vkb_dec_u32(&call_.d); vkb_call_end(&call_); return r_; } @@ -5644,12 +5452,7 @@ void vkb_wire_vkTrimCommandPool(VkDevice device, VkCommandPool commandPool, VkCo vkb_enc_u64(e, vkb_remote((const void *)device)); vkb_enc_bytes(e, &commandPool, sizeof(commandPool)); vkb_enc_bytes(e, &flags, sizeof(flags)); - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } void vkb_wire_vkUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount, const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount, const VkCopyDescriptorSet* pDescriptorCopies) @@ -5674,12 +5477,7 @@ void vkb_wire_vkUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCo } else { vkb_enc_u8(e, 0); } - if (!vkb_call_exec(&call_)) { - return; - } - vkb_dec *d = &call_.d; - (void)d; - vkb_call_end(&call_); + vkb_call_exec_async(&call_); } VkResult vkb_wire_vkWaitForFences(VkDevice device, uint32_t fenceCount, const VkFence* pFences, VkBool32 waitAll, uint64_t timeout) @@ -5735,11 +5533,6 @@ VkResult vkb_wire_vkWaitSemaphores(VkDevice device, const VkSemaphoreWaitInfo* p } /* Default entry points. */ -static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pDescriptorSets) -{ - return vkb_wire_vkAllocateDescriptorSets(device, pAllocateInfo, pDescriptorSets); -} - static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkBindTensorMemoryARM(VkDevice device, uint32_t bindInfoCount, const VkBindTensorMemoryInfoARM* pBindInfos) { return vkb_wire_vkBindTensorMemoryARM(device, bindInfoCount, pBindInfos); @@ -6420,16 +6213,6 @@ static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateComputePipelines(VkDevice d return vkb_wire_vkCreateComputePipelines(device, pipelineCache, createInfoCount, pCreateInfos, pAllocator, pPipelines); } -static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorPool(VkDevice device, const VkDescriptorPoolCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDescriptorPool* pDescriptorPool) -{ - return vkb_wire_vkCreateDescriptorPool(device, pCreateInfo, pAllocator, pDescriptorPool); -} - -static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDescriptorSetLayout* pSetLayout) -{ - return vkb_wire_vkCreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout); -} - static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkCreateEvent(VkDevice device, const VkEventCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkEvent* pEvent) { return vkb_wire_vkCreateEvent(device, pCreateInfo, pAllocator, pEvent); @@ -6515,16 +6298,6 @@ static VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyBufferView(VkDevice device, Vk vkb_wire_vkDestroyBufferView(device, bufferView, pAllocator); } -static VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, const VkAllocationCallbacks* pAllocator) -{ - vkb_wire_vkDestroyDescriptorPool(device, descriptorPool, pAllocator); -} - -static VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout descriptorSetLayout, const VkAllocationCallbacks* pAllocator) -{ - vkb_wire_vkDestroyDescriptorSetLayout(device, descriptorSetLayout, pAllocator); -} - static VKAPI_ATTR void VKAPI_CALL vkb_ep_vkDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks* pAllocator) { vkb_wire_vkDestroyEvent(device, event, pAllocator); @@ -6760,11 +6533,6 @@ static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkQueueBindSparse(VkQueue queue, ui return vkb_wire_vkQueueBindSparse(queue, bindInfoCount, pBindInfo, fence); } -static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags) -{ - return vkb_wire_vkResetDescriptorPool(device, descriptorPool, flags); -} - static VKAPI_ATTR VkResult VKAPI_CALL vkb_ep_vkResetEvent(VkDevice device, VkEvent event) { return vkb_wire_vkResetEvent(device, event); diff --git a/tools/vkbridge/common/gen/vkb_gen_structs.c b/tools/vkbridge/common/gen/vkb_gen_structs.c index fde6b9d1c..ea3d988e8 100644 --- a/tools/vkbridge/common/gen/vkb_gen_structs.c +++ b/tools/vkbridge/common/gen/vkb_gen_structs.c @@ -15937,6 +15937,298 @@ const char *const vkb_cmd_names[VKB_CMD_COUNT] = { "vkWaitForFences", "vkWaitSemaphores", }; +const unsigned char vkb_cmd_result[VKB_CMD_COUNT] = { + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, +}; const vkb_ext_desc vkb_exts[VKB_EXT_COUNT] = { {"VK_KHR_surface", 0, 1}, {"VK_KHR_swapchain", 1, 1}, diff --git a/tools/vkbridge/common/vkb_wire.h b/tools/vkbridge/common/vkb_wire.h index 6930123be..304239977 100644 --- a/tools/vkbridge/common/vkb_wire.h +++ b/tools/vkbridge/common/vkb_wire.h @@ -100,8 +100,13 @@ typedef struct vkb_msg_hdr { uint32_t table; /* request: server dispatch table id */ uint32_t nfds; uint32_t flags; + /* Ordering of asynchronous requests across a process's connections: an async request has + * seq > 0; every request carries barrier = the highest async seq its process had issued + * before it, and is not executed until all of those have been. */ + uint64_t seq; + uint64_t barrier; } vkb_msg_hdr; -#define VKB_MAGIC 0x31424b56u /* "VKB1" */ +#define VKB_MAGIC 0x32424b56u /* "VKB2" */ #define VKB_F_NOREPLY 1u /* Reply status codes (vkb_msg_hdr.cmd in replies). */ diff --git a/tools/vkbridge/tests/d3d11_bench.c b/tools/vkbridge/tests/d3d11_bench.c new file mode 100644 index 000000000..122cfc214 --- /dev/null +++ b/tools/vkbridge/tests/d3d11_bench.c @@ -0,0 +1,116 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * + * d3d11_bench.exe [draws] [frames]: a game-like D3D11 frame - many small draws, each with a + * Map(WRITE_DISCARD) constant buffer update and its own texture/sampler binding - rendered + * offscreen and flushed every frame (with a readback every 30th, so the GPU cannot fall behind). + * Prints milliseconds per frame: the bridge's overhead shows as the difference to native. + */ +#define COBJMACROS +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int draws = argc > 1 ? atoi(argv[1]) : 1000; + int frames = argc > 2 ? atoi(argv[2]) : 300; + ID3D11Device *dev; + ID3D11DeviceContext *ctx; + D3D_FEATURE_LEVEL fl = D3D_FEATURE_LEVEL_11_0; + if (FAILED(D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &fl, 1, D3D11_SDK_VERSION, &dev, NULL, &ctx))) { + printf("no device\n"); + return 1; + } + const char *vs = "cbuffer C : register(b0) { float4 pos; float4 col; };" + "struct O { float4 p : SV_Position; float4 c : COLOR; float2 uv : TEXCOORD; };" + "O main(uint id : SV_VertexID) { O o; float2 t = float2((id << 1) & 2, id & 2) * 0.05;" + "o.p = float4(pos.xy + t, 0, 1); o.c = col; o.uv = t * 20; return o; }"; + const char *ps = "Texture2D t : register(t0); SamplerState s : register(s0);" + "float4 main(float4 p : SV_Position, float4 c : COLOR, float2 uv : TEXCOORD) : SV_Target { return c * t.Sample(s, uv); }"; + ID3DBlob *vb, *pb; + D3DCompile(vs, strlen(vs), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &vb, NULL); + D3DCompile(ps, strlen(ps), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &pb, NULL); + ID3D11VertexShader *vsh; + ID3D11PixelShader *psh; + ID3D11Device_CreateVertexShader(dev, ID3D10Blob_GetBufferPointer(vb), ID3D10Blob_GetBufferSize(vb), NULL, &vsh); + ID3D11Device_CreatePixelShader(dev, ID3D10Blob_GetBufferPointer(pb), ID3D10Blob_GetBufferSize(pb), NULL, &psh); + D3D11_BUFFER_DESC bd = {32, D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, D3D11_CPU_ACCESS_WRITE, 0, 0}; + ID3D11Buffer *cb; + ID3D11Device_CreateBuffer(dev, &bd, NULL, &cb); + /* 8 small BC1 textures (block-compressed like a game's) */ + ID3D11ShaderResourceView *srv[8]; + uint8_t blocks[16 * 8]; + for (int i = 0; i < (int)sizeof(blocks); i++) blocks[i] = (uint8_t)(i * 37 + 11); + for (int i = 0; i < 8; i++) { + D3D11_TEXTURE2D_DESC td = {16, 16, 1, 1, DXGI_FORMAT_BC1_UNORM, {1, 0}, D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, 0}; + D3D11_SUBRESOURCE_DATA init = {blocks, 32, 0}; + ID3D11Texture2D *t; + ID3D11Device_CreateTexture2D(dev, &td, &init, &t); + ID3D11Device_CreateShaderResourceView(dev, (ID3D11Resource *)t, NULL, &srv[i]); + } + D3D11_SAMPLER_DESC sd = {D3D11_FILTER_MIN_MAG_MIP_LINEAR, D3D11_TEXTURE_ADDRESS_WRAP, D3D11_TEXTURE_ADDRESS_WRAP, D3D11_TEXTURE_ADDRESS_WRAP, + 0, 1, D3D11_COMPARISON_NEVER, {0}, 0, D3D11_FLOAT32_MAX}; + ID3D11SamplerState *smp; + ID3D11Device_CreateSamplerState(dev, &sd, &smp); + D3D11_TEXTURE2D_DESC rd = {1280, 720, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D11_USAGE_DEFAULT, D3D11_BIND_RENDER_TARGET, 0, 0}; + ID3D11Texture2D *rt, *st; + ID3D11Device_CreateTexture2D(dev, &rd, NULL, &rt); + rd.Usage = D3D11_USAGE_STAGING; + rd.BindFlags = 0; + rd.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + rd.Width = rd.Height = 16; + ID3D11Device_CreateTexture2D(dev, &rd, NULL, &st); + ID3D11RenderTargetView *rtv; + ID3D11Device_CreateRenderTargetView(dev, (ID3D11Resource *)rt, NULL, &rtv); + D3D11_VIEWPORT vp = {0, 0, 1280, 720, 0, 1}; + LARGE_INTEGER f, t0, t1; + QueryPerformanceFrequency(&f); + double total = 0, worst = 0; + for (int fr = 0; fr < frames + 10; fr++) { + QueryPerformanceCounter(&t0); + float clear[4] = {0.1f, 0.1f, 0.1f, 1}; + ID3D11DeviceContext_OMSetRenderTargets(ctx, 1, &rtv, NULL); + ID3D11DeviceContext_RSSetViewports(ctx, 1, &vp); + ID3D11DeviceContext_ClearRenderTargetView(ctx, rtv, clear); + ID3D11DeviceContext_IASetPrimitiveTopology(ctx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ID3D11DeviceContext_VSSetShader(ctx, vsh, NULL, 0); + ID3D11DeviceContext_PSSetShader(ctx, psh, NULL, 0); + ID3D11DeviceContext_VSSetConstantBuffers(ctx, 0, 1, &cb); + ID3D11DeviceContext_PSSetSamplers(ctx, 0, 1, &smp); + for (int d = 0; d < draws; d++) { + D3D11_MAPPED_SUBRESOURCE m; + ID3D11DeviceContext_Map(ctx, (ID3D11Resource *)cb, 0, D3D11_MAP_WRITE_DISCARD, 0, &m); + float *p = m.pData; + p[0] = -1 + 2.0f * (d % 40) / 40; + p[1] = -1 + 2.0f * ((d / 40) % 40) / 40; + p[4] = (d & 1) ? 1.0f : 0.5f; + p[5] = (d & 2) ? 1.0f : 0.5f; + p[6] = (d & 4) ? 1.0f : 0.5f; + p[7] = 1; + ID3D11DeviceContext_Unmap(ctx, (ID3D11Resource *)cb, 0); + ID3D11DeviceContext_PSSetShaderResources(ctx, 0, 1, &srv[d & 7]); + ID3D11DeviceContext_Draw(ctx, 3, 0); + } + if (fr % 30 == 29) { + D3D11_BOX box = {0, 0, 0, 16, 16, 1}; + ID3D11DeviceContext_CopySubresourceRegion(ctx, (ID3D11Resource *)st, 0, 0, 0, 0, (ID3D11Resource *)rt, 0, &box); + D3D11_MAPPED_SUBRESOURCE m; + ID3D11DeviceContext_Map(ctx, (ID3D11Resource *)st, 0, D3D11_MAP_READ, 0, &m); + ID3D11DeviceContext_Unmap(ctx, (ID3D11Resource *)st, 0); + } else { + ID3D11DeviceContext_Flush(ctx); + } + QueryPerformanceCounter(&t1); + double ms = (double)(t1.QuadPart - t0.QuadPart) * 1000.0 / (double)f.QuadPart; + if (fr >= 10) { + total += ms; + if (ms > worst) worst = ms; + } + } + printf("%d draws/frame: %.2f ms/frame average (%.0f fps), worst %.2f ms\n", draws, total / frames, 1000.0 * frames / total, worst); + return 0; +}