Skip to content

Commit 8147fbe

Browse files
committed
(vulkan) vulkan_common optimizations
1 parent 02c5a7c commit 8147fbe

2 files changed

Lines changed: 51 additions & 60 deletions

File tree

gfx/common/vulkan_common.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,9 @@ static void vulkan_acquire_clear_fences(gfx_ctx_vulkan_data_t *vk)
13441344
{
13451345
struct vulkan_context *ctx = &vk->context;
13461346
VkSemaphore sem = vk->context.swapchain_wait_semaphores[i];
1347-
assert(ctx->num_recycled_acquire_semaphores < VULKAN_MAX_SWAPCHAIN_IMAGES);
1347+
#ifdef DEBUG
1348+
retro_assert(ctx->num_recycled_acquire_semaphores < VULKAN_MAX_SWAPCHAIN_IMAGES);
1349+
#endif
13481350
ctx->swapchain_recycled_semaphores[ctx->num_recycled_acquire_semaphores++] = sem;
13491351
}
13501352
vk->context.swapchain_wait_semaphores[i] = VK_NULL_HANDLE;
@@ -1406,7 +1408,9 @@ static void vulkan_acquire_wait_fences(gfx_ctx_vulkan_data_t *vk)
14061408
{
14071409
struct vulkan_context *ctx = &vk->context;
14081410
VkSemaphore sem = vk->context.swapchain_wait_semaphores[index];
1409-
assert(ctx->num_recycled_acquire_semaphores < VULKAN_MAX_SWAPCHAIN_IMAGES);
1411+
#ifdef DEBUG
1412+
retro_assert(ctx->num_recycled_acquire_semaphores < VULKAN_MAX_SWAPCHAIN_IMAGES);
1413+
#endif
14101414
ctx->swapchain_recycled_semaphores[ctx->num_recycled_acquire_semaphores++] = sem;
14111415
}
14121416
vk->context.swapchain_wait_semaphores[index] = VK_NULL_HANDLE;
@@ -1469,8 +1473,10 @@ bool vulkan_buffer_chain_alloc(const struct vulkan_context *context,
14691473

14701474
chain->current = chain->current->next;
14711475
chain->offset = 0;
1476+
#ifdef DEBUG
14721477
/* This cannot possibly fail. */
14731478
retro_assert(vulkan_buffer_chain_suballoc(chain, len, range));
1479+
#endif
14741480
}
14751481
return true;
14761482
}
@@ -1597,15 +1603,16 @@ VkDescriptorSet vulkan_descriptor_manager_alloc(
15971603
}
15981604

15991605
manager->current->next = vulkan_alloc_descriptor_pool(device, manager);
1606+
#ifdef DEBUG
16001607
retro_assert(manager->current->next);
1608+
#endif
16011609

16021610
manager->current = manager->current->next;
16031611
manager->count = 0;
16041612
}
16051613
return manager->current->sets[manager->count++];
16061614
}
16071615

1608-
16091616
bool vulkan_surface_create(gfx_ctx_vulkan_data_t *vk,
16101617
enum vulkan_wsi_type type,
16111618
void *display, void *surface,
@@ -1872,7 +1879,9 @@ void vulkan_acquire_next_image(gfx_ctx_vulkan_data_t *vk)
18721879
}
18731880
}
18741881

1882+
#ifdef DEBUG
18751883
retro_assert(!(vk->context.flags & VK_CTX_FLAG_HAS_ACQUIRED_SWAPCHAIN));
1884+
#endif
18761885

18771886
if (vk->flags & VK_DATA_FLAG_EMULATING_MAILBOX)
18781887
{
@@ -1903,16 +1912,16 @@ void vulkan_acquire_next_image(gfx_ctx_vulkan_data_t *vk)
19031912
vkWaitForFences(vk->context.device, 1, &fence, true, UINT64_MAX);
19041913
vk->context.flags |= VK_CTX_FLAG_HAS_ACQUIRED_SWAPCHAIN;
19051914

1906-
if (vk->context.swapchain_acquire_semaphore)
1915+
/* Recycle the previous acquire semaphore rather than stalling the device.
1916+
* vkDeviceWaitIdle here was a significant hot-path bottleneck. */
1917+
if (vk->context.swapchain_acquire_semaphore != VK_NULL_HANDLE)
19071918
{
1908-
#ifdef HAVE_THREADS
1909-
slock_lock(vk->context.queue_lock);
1910-
#endif
1911-
vkDeviceWaitIdle(vk->context.device);
1912-
vkDestroySemaphore(vk->context.device, vk->context.swapchain_acquire_semaphore, NULL);
1913-
#ifdef HAVE_THREADS
1914-
slock_unlock(vk->context.queue_lock);
1919+
struct vulkan_context *ctx = &vk->context;
1920+
#ifdef DEBUG
1921+
retro_assert(ctx->num_recycled_acquire_semaphores < VULKAN_MAX_SWAPCHAIN_IMAGES);
19151922
#endif
1923+
ctx->swapchain_recycled_semaphores[ctx->num_recycled_acquire_semaphores++] =
1924+
vk->context.swapchain_acquire_semaphore;
19161925
}
19171926
vk->context.swapchain_acquire_semaphore = semaphore;
19181927
}
@@ -1923,7 +1932,9 @@ void vulkan_acquire_next_image(gfx_ctx_vulkan_data_t *vk)
19231932
{
19241933
struct vulkan_context *ctx = &vk->context;
19251934
VkSemaphore sem = semaphore;
1926-
assert(ctx->num_recycled_acquire_semaphores < VULKAN_MAX_SWAPCHAIN_IMAGES);
1935+
#ifdef DEBUG
1936+
retro_assert(ctx->num_recycled_acquire_semaphores < VULKAN_MAX_SWAPCHAIN_IMAGES);
1937+
#endif
19271938
ctx->swapchain_recycled_semaphores[ctx->num_recycled_acquire_semaphores++] = sem;
19281939
}
19291940
}

gfx/common/vulkan_common.h

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,60 +146,40 @@
146146
vkUpdateDescriptorSets(_device, 1, &write, 0, NULL); \
147147
}
148148

149+
/*
150+
* Vertex layout: two triangles forming a quad (CCW winding).
151+
* v0 (TL) -- v2/v4 (TR)
152+
* | / |
153+
* v1/v5 (BL) -- v3 (BR)
154+
*/
149155
#define VULKAN_WRITE_QUAD_VBO(pv, _x, _y, _width, _height, _tex_x, _tex_y, _tex_width, _tex_height, vulkan_color) \
150156
{ \
151157
float r = (vulkan_color)->r; \
152158
float g = (vulkan_color)->g; \
153159
float b = (vulkan_color)->b; \
154160
float a = (vulkan_color)->a; \
155-
pv[0].x = (_x) + 0.0f * (_width); \
156-
pv[0].y = (_y) + 0.0f * (_height); \
157-
pv[0].tex_x = (_tex_x) + 0.0f * (_tex_width); \
158-
pv[0].tex_y = (_tex_y) + 0.0f * (_tex_height); \
159-
pv[0].color.r = r; \
160-
pv[0].color.g = g; \
161-
pv[0].color.b = b; \
162-
pv[0].color.a = a; \
163-
pv[1].x = (_x) + 0.0f * (_width); \
164-
pv[1].y = (_y) + 1.0f * (_height); \
165-
pv[1].tex_x = (_tex_x) + 0.0f * (_tex_width); \
166-
pv[1].tex_y = (_tex_y) + 1.0f * (_tex_height); \
167-
pv[1].color.r = r; \
168-
pv[1].color.g = g; \
169-
pv[1].color.b = b; \
170-
pv[1].color.a = a; \
171-
pv[2].x = (_x) + 1.0f * (_width); \
172-
pv[2].y = (_y) + 0.0f * (_height); \
173-
pv[2].tex_x = (_tex_x) + 1.0f * (_tex_width); \
174-
pv[2].tex_y = (_tex_y) + 0.0f * (_tex_height); \
175-
pv[2].color.r = r; \
176-
pv[2].color.g = g; \
177-
pv[2].color.b = b; \
178-
pv[2].color.a = a; \
179-
pv[3].x = (_x) + 1.0f * (_width); \
180-
pv[3].y = (_y) + 1.0f * (_height); \
181-
pv[3].tex_x = (_tex_x) + 1.0f * (_tex_width); \
182-
pv[3].tex_y = (_tex_y) + 1.0f * (_tex_height); \
183-
pv[3].color.r = r; \
184-
pv[3].color.g = g; \
185-
pv[3].color.b = b; \
186-
pv[3].color.a = a; \
187-
pv[4].x = (_x) + 1.0f * (_width); \
188-
pv[4].y = (_y) + 0.0f * (_height); \
189-
pv[4].tex_x = (_tex_x) + 1.0f * (_tex_width); \
190-
pv[4].tex_y = (_tex_y) + 0.0f * (_tex_height); \
191-
pv[4].color.r = r; \
192-
pv[4].color.g = g; \
193-
pv[4].color.b = b; \
194-
pv[4].color.a = a; \
195-
pv[5].x = (_x) + 0.0f * (_width); \
196-
pv[5].y = (_y) + 1.0f * (_height); \
197-
pv[5].tex_x = (_tex_x) + 0.0f * (_tex_width); \
198-
pv[5].tex_y = (_tex_y) + 1.0f * (_tex_height); \
199-
pv[5].color.r = r; \
200-
pv[5].color.g = g; \
201-
pv[5].color.b = b; \
202-
pv[5].color.a = a; \
161+
float _x1 = (float)(_x); \
162+
float _y1 = (float)(_y); \
163+
float _x2 = _x1 + (float)(_width); \
164+
float _y2 = _y1 + (float)(_height); \
165+
float _tx1 = (float)(_tex_x); \
166+
float _ty1 = (float)(_tex_y); \
167+
float _tx2 = _tx1 + (float)(_tex_width); \
168+
float _ty2 = _ty1 + (float)(_tex_height); \
169+
/* Triangle 1: TL, BL, TR */ \
170+
pv[0].x = _x1; pv[0].y = _y1; pv[0].tex_x = _tx1; pv[0].tex_y = _ty1; \
171+
pv[0].color.r = r; pv[0].color.g = g; pv[0].color.b = b; pv[0].color.a = a; \
172+
pv[1].x = _x1; pv[1].y = _y2; pv[1].tex_x = _tx1; pv[1].tex_y = _ty2; \
173+
pv[1].color.r = r; pv[1].color.g = g; pv[1].color.b = b; pv[1].color.a = a; \
174+
pv[2].x = _x2; pv[2].y = _y1; pv[2].tex_x = _tx2; pv[2].tex_y = _ty1; \
175+
pv[2].color.r = r; pv[2].color.g = g; pv[2].color.b = b; pv[2].color.a = a; \
176+
/* Triangle 2: TR, BL, BR */ \
177+
pv[3].x = _x2; pv[3].y = _y2; pv[3].tex_x = _tx2; pv[3].tex_y = _ty2; \
178+
pv[3].color.r = r; pv[3].color.g = g; pv[3].color.b = b; pv[3].color.a = a; \
179+
pv[4].x = _x2; pv[4].y = _y1; pv[4].tex_x = _tx2; pv[4].tex_y = _ty1; \
180+
pv[4].color.r = r; pv[4].color.g = g; pv[4].color.b = b; pv[4].color.a = a; \
181+
pv[5].x = _x1; pv[5].y = _y2; pv[5].tex_x = _tx1; pv[5].tex_y = _ty2; \
182+
pv[5].color.r = r; pv[5].color.g = g; pv[5].color.b = b; pv[5].color.a = a; \
203183
}
204184

205185
/* We don't have to sync against previous TRANSFER,

0 commit comments

Comments
 (0)