(Vulkan) Move some code around

This commit is contained in:
libretroadmin 2023-01-24 04:40:17 +01:00
parent bb48289dd4
commit 1a93bb3844
3 changed files with 149 additions and 146 deletions

View File

@ -374,7 +374,7 @@ static void vulkan_debug_mark_object(VkDevice device,
}
}
void vulkan_debug_mark_buffer(VkDevice device, VkBuffer buffer)
static void vulkan_debug_mark_buffer(VkDevice device, VkBuffer buffer)
{
static unsigned object_count;
vulkan_debug_mark_object(device, VK_OBJECT_TYPE_BUFFER, (uint64_t)buffer, "RetroArch buffer", ++object_count);
@ -392,6 +392,25 @@ void vulkan_debug_mark_memory(VkDevice device, VkDeviceMemory memory)
vulkan_debug_mark_object(device, VK_OBJECT_TYPE_DEVICE_MEMORY, (uint64_t)memory, "RetroArch memory", ++object_count);
}
static INLINE unsigned vulkan_format_to_bpp(VkFormat format)
{
switch (format)
{
case VK_FORMAT_B8G8R8A8_UNORM:
return 4;
case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
case VK_FORMAT_R5G6B5_UNORM_PACK16:
return 2;
case VK_FORMAT_R8_UNORM:
return 1;
default: /* Unknown format */
break;
}
return 0;
}
struct vk_texture vulkan_create_texture(vk_t *vk,
struct vk_texture *old,
unsigned width, unsigned height,
@ -866,7 +885,7 @@ void vulkan_destroy_texture(
tex->layout = VK_IMAGE_LAYOUT_UNDEFINED;
}
static void vulkan_write_quad_descriptors(
void vulkan_write_quad_descriptors(
VkDevice device,
VkDescriptorSet set,
VkBuffer buffer,
@ -1039,125 +1058,6 @@ void vulkan_draw_triangles(vk_t *vk, const struct vk_draw_triangles *call)
vkCmdDraw(vk->cmd, call->vertices, 1, 0, 0);
}
void vulkan_draw_quad(vk_t *vk, const struct vk_draw_quad *quad)
{
if (quad->texture && quad->texture->image)
vulkan_transition_texture(vk, vk->cmd, quad->texture);
if (quad->pipeline != vk->tracker.pipeline)
{
VkRect2D sci;
vkCmdBindPipeline(vk->cmd,
VK_PIPELINE_BIND_POINT_GRAPHICS, quad->pipeline);
vk->tracker.pipeline = quad->pipeline;
/* Changing pipeline invalidates dynamic state. */
vk->tracker.dirty |= VULKAN_DIRTY_DYNAMIC_BIT;
if (vk->flags & VK_FLAG_TRACKER_USE_SCISSOR)
sci = vk->tracker.scissor;
else
{
/* No scissor -> viewport */
sci.offset.x = vk->vp.x;
sci.offset.y = vk->vp.y;
sci.extent.width = vk->vp.width;
sci.extent.height = vk->vp.height;
}
vkCmdSetViewport(vk->cmd, 0, 1, &vk->vk_vp);
vkCmdSetScissor (vk->cmd, 0, 1, &sci);
vk->tracker.dirty &= ~VULKAN_DIRTY_DYNAMIC_BIT;
}
else if (vk->tracker.dirty & VULKAN_DIRTY_DYNAMIC_BIT)
{
VkRect2D sci;
if (vk->flags & VK_FLAG_TRACKER_USE_SCISSOR)
sci = vk->tracker.scissor;
else
{
/* No scissor -> viewport */
sci.offset.x = vk->vp.x;
sci.offset.y = vk->vp.y;
sci.extent.width = vk->vp.width;
sci.extent.height = vk->vp.height;
}
vkCmdSetViewport(vk->cmd, 0, 1, &vk->vk_vp);
vkCmdSetScissor (vk->cmd, 0, 1, &sci);
vk->tracker.dirty &= ~VULKAN_DIRTY_DYNAMIC_BIT;
}
/* Upload descriptors */
{
VkDescriptorSet set;
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->ubo,
sizeof(*quad->mvp), &range))
return;
if (
string_is_equal_fast(quad->mvp,
&vk->tracker.mvp, sizeof(*quad->mvp))
|| quad->texture->view != vk->tracker.view
|| quad->sampler != vk->tracker.sampler)
{
/* Upload UBO */
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->ubo,
sizeof(*quad->mvp), &range))
return;
memcpy(range.data, quad->mvp, sizeof(*quad->mvp));
set = vulkan_descriptor_manager_alloc(
vk->context->device,
&vk->chain->descriptor_manager);
vulkan_write_quad_descriptors(
vk->context->device,
set,
range.buffer,
range.offset,
sizeof(*quad->mvp),
quad->texture,
quad->sampler);
vkCmdBindDescriptorSets(vk->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
vk->pipelines.layout, 0,
1, &set, 0, NULL);
vk->tracker.view = quad->texture->view;
vk->tracker.sampler = quad->sampler;
vk->tracker.mvp = *quad->mvp;
}
}
/* Upload VBO */
{
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->vbo,
6 * sizeof(struct vk_vertex), &range))
return;
{
struct vk_vertex *pv = (struct vk_vertex*)range.data;
const struct vk_color *color = &quad->color;
VULKAN_WRITE_QUAD_VBO(pv, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, color);
}
vkCmdBindVertexBuffers(vk->cmd, 0, 1,
&range.buffer, &range.offset);
}
/* Draw the quad */
vkCmdDraw(vk->cmd, 6, 1, 0, 0);
}
struct vk_buffer vulkan_create_buffer(
const struct vulkan_context *context,
size_t size, VkBufferUsageFlags usage)

View File

@ -728,36 +728,20 @@ void vulkan_destroy_texture(
if (((tex).flags & VK_TEX_FLAG_NEED_MANUAL_CACHE_MANAGEMENT) && (tex).memory != VK_NULL_HANDLE) \
VULKAN_SYNC_TEXTURE_TO_GPU(vk->context->device, (tex).memory) \
/* VBO will be written to here. */
void vulkan_draw_quad(vk_t *vk, const struct vk_draw_quad *quad);
void vulkan_write_quad_descriptors(
VkDevice device,
VkDescriptorSet set,
VkBuffer buffer,
VkDeviceSize offset,
VkDeviceSize range,
const struct vk_texture *texture,
VkSampler sampler);
/* The VBO needs to be written to before calling this.
* Use vulkan_buffer_chain_alloc.
*/
void vulkan_draw_triangles(vk_t *vk, const struct vk_draw_triangles *call);
static INLINE unsigned vulkan_format_to_bpp(VkFormat format)
{
switch (format)
{
case VK_FORMAT_B8G8R8A8_UNORM:
return 4;
case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
case VK_FORMAT_R5G6B5_UNORM_PACK16:
return 2;
case VK_FORMAT_R8_UNORM:
return 1;
default: /* Unknown format */
break;
}
return 0;
}
struct vk_buffer vulkan_create_buffer(
const struct vulkan_context *context,
size_t size, VkBufferUsageFlags usage);
@ -798,7 +782,6 @@ void vulkan_set_uniform_buffer(
VkDeviceSize offset,
VkDeviceSize range);
void vulkan_debug_mark_buffer(VkDevice device, VkBuffer buffer);
void vulkan_debug_mark_image(VkDevice device, VkImage image);
void vulkan_debug_mark_memory(VkDevice device, VkDeviceMemory memory);

View File

@ -2066,6 +2066,126 @@ static void vulkan_inject_black_frame(vk_t *vk, video_frame_info_t *video_info)
#endif
}
/* VBO will be written to here. */
static void vulkan_draw_quad(vk_t *vk, const struct vk_draw_quad *quad)
{
if (quad->texture && quad->texture->image)
vulkan_transition_texture(vk, vk->cmd, quad->texture);
if (quad->pipeline != vk->tracker.pipeline)
{
VkRect2D sci;
vkCmdBindPipeline(vk->cmd,
VK_PIPELINE_BIND_POINT_GRAPHICS, quad->pipeline);
vk->tracker.pipeline = quad->pipeline;
/* Changing pipeline invalidates dynamic state. */
vk->tracker.dirty |= VULKAN_DIRTY_DYNAMIC_BIT;
if (vk->flags & VK_FLAG_TRACKER_USE_SCISSOR)
sci = vk->tracker.scissor;
else
{
/* No scissor -> viewport */
sci.offset.x = vk->vp.x;
sci.offset.y = vk->vp.y;
sci.extent.width = vk->vp.width;
sci.extent.height = vk->vp.height;
}
vkCmdSetViewport(vk->cmd, 0, 1, &vk->vk_vp);
vkCmdSetScissor (vk->cmd, 0, 1, &sci);
vk->tracker.dirty &= ~VULKAN_DIRTY_DYNAMIC_BIT;
}
else if (vk->tracker.dirty & VULKAN_DIRTY_DYNAMIC_BIT)
{
VkRect2D sci;
if (vk->flags & VK_FLAG_TRACKER_USE_SCISSOR)
sci = vk->tracker.scissor;
else
{
/* No scissor -> viewport */
sci.offset.x = vk->vp.x;
sci.offset.y = vk->vp.y;
sci.extent.width = vk->vp.width;
sci.extent.height = vk->vp.height;
}
vkCmdSetViewport(vk->cmd, 0, 1, &vk->vk_vp);
vkCmdSetScissor (vk->cmd, 0, 1, &sci);
vk->tracker.dirty &= ~VULKAN_DIRTY_DYNAMIC_BIT;
}
/* Upload descriptors */
{
VkDescriptorSet set;
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->ubo,
sizeof(*quad->mvp), &range))
return;
if (
string_is_equal_fast(quad->mvp,
&vk->tracker.mvp, sizeof(*quad->mvp))
|| quad->texture->view != vk->tracker.view
|| quad->sampler != vk->tracker.sampler)
{
/* Upload UBO */
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->ubo,
sizeof(*quad->mvp), &range))
return;
memcpy(range.data, quad->mvp, sizeof(*quad->mvp));
set = vulkan_descriptor_manager_alloc(
vk->context->device,
&vk->chain->descriptor_manager);
vulkan_write_quad_descriptors(
vk->context->device,
set,
range.buffer,
range.offset,
sizeof(*quad->mvp),
quad->texture,
quad->sampler);
vkCmdBindDescriptorSets(vk->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
vk->pipelines.layout, 0,
1, &set, 0, NULL);
vk->tracker.view = quad->texture->view;
vk->tracker.sampler = quad->sampler;
vk->tracker.mvp = *quad->mvp;
}
}
/* Upload VBO */
{
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->vbo,
6 * sizeof(struct vk_vertex), &range))
return;
{
struct vk_vertex *pv = (struct vk_vertex*)range.data;
const struct vk_color *color = &quad->color;
VULKAN_WRITE_QUAD_VBO(pv, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, color);
}
vkCmdBindVertexBuffers(vk->cmd, 0, 1,
&range.buffer, &range.offset);
}
/* Draw the quad */
vkCmdDraw(vk->cmd, 6, 1, 0, 0);
}
static bool vulkan_frame(void *data, const void *frame,
unsigned frame_width, unsigned frame_height,
uint64_t frame_count,