mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 06:15:58 +00:00
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=<s>. - 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 <noreply@anthropic.com>
This commit is contained in:
1 parent
308b200350
commit
a4bd2eb271
14 files changed
+1247
-314
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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=<seconds>: per-command counts of round trips and async sends, logged. */
|
||||
static uint32_t stat_sync[VKB_CMD_COUNT], stat_async[VKB_CMD_COUNT];
|
||||
static uint64_t stat_bytes;
|
||||
static uint64_t stat_last;
|
||||
void vkb_stats_count(uint32_t cmd, int async, size_t bytes)
|
||||
{
|
||||
static int period = -1;
|
||||
if (period < 0) {
|
||||
const char *v = getenv("VKBRIDGE_STATS");
|
||||
period = v ? atoi(v) : 0;
|
||||
}
|
||||
if (!period) return;
|
||||
__atomic_add_fetch(async ? &stat_async[cmd] : &stat_sync[cmd], 1, __ATOMIC_RELAXED);
|
||||
__atomic_add_fetch(&stat_bytes, bytes, __ATOMIC_RELAXED);
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
uint64_t now = (uint64_t)ts.tv_sec;
|
||||
uint64_t last = __atomic_load_n(&stat_last, __ATOMIC_RELAXED);
|
||||
if (!last) {
|
||||
__atomic_store_n(&stat_last, now, __ATOMIC_RELAXED);
|
||||
return;
|
||||
}
|
||||
if (now - last < (uint64_t)period || !__atomic_compare_exchange_n(&stat_last, &last, now, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED))
|
||||
return;
|
||||
uint32_t tot_s = 0, tot_a = 0;
|
||||
for (uint32_t i = 0; i < VKB_CMD_COUNT; i++) {
|
||||
tot_s += stat_sync[i];
|
||||
tot_a += stat_async[i];
|
||||
}
|
||||
vkb_log(VKB_LOG_ERROR, "stats over %us: %u round trips, %u async, %llu KB sent", period, tot_s, tot_a,
|
||||
(unsigned long long)(stat_bytes >> 10));
|
||||
for (int k = 0; k < 8; k++) {
|
||||
uint32_t best = 0, bi = 0;
|
||||
for (uint32_t i = 0; i < VKB_CMD_COUNT; i++)
|
||||
if (stat_sync[i] + stat_async[i] > best) {
|
||||
best = stat_sync[i] + stat_async[i];
|
||||
bi = i;
|
||||
}
|
||||
if (!best) break;
|
||||
vkb_log(VKB_LOG_ERROR, " %-40s %6u sync %6u async", vkb_cmd_names[bi], stat_sync[bi], stat_async[bi]);
|
||||
stat_sync[bi] = stat_async[bi] = 0;
|
||||
}
|
||||
memset(stat_sync, 0, sizeof(stat_sync));
|
||||
memset(stat_async, 0, sizeof(stat_async));
|
||||
stat_bytes = 0;
|
||||
}
|
||||
|
||||
uint32_t vkb_table_of(const void *h)
|
||||
{
|
||||
return h ? ((const vkb_object *)h)->table : 0;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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. */
|
||||
|
||||
Reference in new issue
Block a user