From a7ac188d1d1808c34f58be980bf9804659f24e30 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 13 Jan 2021 10:43:23 +0900 Subject: [PATCH] [vulkan] Use a scrap texture for draw The scrap texture did very good things for the glsl renderer and the better control over data copying might help it do even better things for vulkan, especially with lots of little icons. --- include/QF/Vulkan/command.h | 6 + include/QF/Vulkan/staging.h | 2 + include/QF/Vulkan/texture.h | 43 ++++ libs/video/renderer/Makemodule.am | 1 + libs/video/renderer/vulkan/staging.c | 13 + libs/video/renderer/vulkan/texture.c | 299 +++++++++++++++++++++++ libs/video/renderer/vulkan/vulkan_draw.c | 90 ++----- 7 files changed, 390 insertions(+), 64 deletions(-) create mode 100644 include/QF/Vulkan/texture.h create mode 100644 libs/video/renderer/vulkan/texture.c diff --git a/include/QF/Vulkan/command.h b/include/QF/Vulkan/command.h index 046c8b2b3..925a63781 100644 --- a/include/QF/Vulkan/command.h +++ b/include/QF/Vulkan/command.h @@ -12,6 +12,12 @@ typedef struct qfv_semaphoreset_s typedef struct qfv_fenceset_s DARRAY_TYPE (VkFence) qfv_fenceset_t; +typedef struct qfv_bufferimagecopy_s + DARRAY_TYPE (VkBufferImageCopy) qfv_bufferimagecopy_t; + +#define QFV_AllocBufferImageCopy(num, allocator) \ + DARRAY_ALLOCFIXED (qfv_bufferimagecopy_t, num, allocator) + struct qfv_queue_s; struct qfv_device_s; VkCommandPool QFV_CreateCommandPool (struct qfv_device_s *device, diff --git a/include/QF/Vulkan/staging.h b/include/QF/Vulkan/staging.h index d281f1854..9a90edd6e 100644 --- a/include/QF/Vulkan/staging.h +++ b/include/QF/Vulkan/staging.h @@ -6,6 +6,7 @@ typedef struct qfv_stagebuf_s { VkBuffer buffer; VkDeviceMemory memory; size_t size; + size_t offset; ///< for batching building void *data; } qfv_stagebuf_t; @@ -14,5 +15,6 @@ qfv_stagebuf_t *QFV_CreateStagingBuffer (struct qfv_device_s *device, size_t size); void QFV_DestroyStagingBuffer (qfv_stagebuf_t *stage); void QFV_FlushStagingBuffer (qfv_stagebuf_t *stage, size_t offset, size_t size); +void *QFV_ClaimStagingBuffer (qfv_stagebuf_t *stage, size_t size); #endif//__QF_Vulkan_staging_h diff --git a/include/QF/Vulkan/texture.h b/include/QF/Vulkan/texture.h new file mode 100644 index 000000000..6a55815f6 --- /dev/null +++ b/include/QF/Vulkan/texture.h @@ -0,0 +1,43 @@ +#ifndef __QF_Vulkan_texture_h +#define __QF_Vulkan_texture_h + +typedef enum { + QFV_LUMINANCE, + QFV_LUMINANCE_ALPHA, + QFV_RGB, + QFV_RGBA, +} QFVFormat; + +typedef struct scrap_s scrap_t; + +scrap_t *QFV_CreateScrap (struct qfv_device_s *device, int size, + QFVFormat format); +void QFV_ScrapClear (scrap_t *scrap); +void QFV_DestroyScrap (scrap_t *scrap); +VkImageView QFV_ScrapImageView (scrap_t *scrap) __attribute__((pure)); +subpic_t *QFV_ScrapSubpic (scrap_t *scrap, int width, int height); +void QFV_SubpicDelete (subpic_t *subpic); + +/** Add an update region to the batch queue. + * + * The region to be updated is recorded in the batch queue, space allocated + * in the staging buffer, and a pointer to the allocated space is returned. + * + * \note No data is stransfered. This facilitates writing generated data + * directly to the staging buffer. + */ +void *QFV_SubpicBatch (subpic_t *subpic, struct qfv_stagebuf_s *stage); + +/** Flush all batched subpic updates. + * + * The offset in the staging bufffer \a stage is reset to 0. The command + * buffer is populated with the appropriate image layout barriers and the + * necessary copy commands. + * + * \note The command buffer is neither begun nor ended, nor is it submitted + * to a queue. This is to maximize flexibility in command buffer usage. + */ +void QFV_ScrapFlush (scrap_t *scrap, struct qfv_stagebuf_s *stage, + VkCommandBuffer cmd); + +#endif//__QF_Vulkan_texture_h diff --git a/libs/video/renderer/Makemodule.am b/libs/video/renderer/Makemodule.am index 05740af60..6f306ffe5 100644 --- a/libs/video/renderer/Makemodule.am +++ b/libs/video/renderer/Makemodule.am @@ -230,6 +230,7 @@ libs_video_renderer_vid_render_vulkan_la_SOURCES = \ libs/video/renderer/vulkan/shader.c \ libs/video/renderer/vulkan/staging.c \ libs/video/renderer/vulkan/swapchain.c \ + libs/video/renderer/vulkan/texture.c \ libs/video/renderer/vulkan/util.c \ libs/video/renderer/vulkan/util.h \ libs/video/renderer/vulkan/vkparse.c \ diff --git a/libs/video/renderer/vulkan/staging.c b/libs/video/renderer/vulkan/staging.c index de791239c..04e3897db 100644 --- a/libs/video/renderer/vulkan/staging.c +++ b/libs/video/renderer/vulkan/staging.c @@ -59,6 +59,8 @@ QFV_CreateStagingBuffer (qfv_device_t *device, size_t size) qfv_devfuncs_t *dfunc = device->funcs; qfv_stagebuf_t *stage = malloc (sizeof (qfv_stagebuf_t)); + stage->size = size; + stage->offset = 0; stage->device = device; stage->buffer = QFV_CreateBuffer (device, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT); @@ -94,3 +96,14 @@ QFV_FlushStagingBuffer (qfv_stagebuf_t *stage, size_t offset, size_t size) }; dfunc->vkFlushMappedMemoryRanges (device->dev, 1, &range); } + +void * +QFV_ClaimStagingBuffer (qfv_stagebuf_t *stage, size_t size) +{ + if (stage->offset + size > stage->size) { + return 0; + } + void *data = (byte *) stage->data + stage->offset; + stage->offset += (size + 3) & ~3; + return data; +} diff --git a/libs/video/renderer/vulkan/texture.c b/libs/video/renderer/vulkan/texture.c new file mode 100644 index 000000000..6529800b1 --- /dev/null +++ b/libs/video/renderer/vulkan/texture.c @@ -0,0 +1,299 @@ +/* + texuture.c + + Vulkan texuture manager + + Copyright (C) 2021 Bill Currie + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + +*/ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_MATH_H +# include +#endif +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_STRINGS_H +# include +#endif + +#include "QF/alloc.h" +#include "QF/cvar.h" +#include "QF/dstring.h" +#include "QF/hash.h" +#include "QF/qfplist.h" +#include "QF/quakefs.h" +#include "QF/render.h" +#include "QF/sys.h" +#include "QF/Vulkan/qf_vid.h" +#include "QF/Vulkan/barrier.h" +#include "QF/Vulkan/buffer.h" +#include "QF/Vulkan/command.h" +#include "QF/Vulkan/device.h" +#include "QF/Vulkan/image.h" +#include "QF/Vulkan/instance.h" +#include "QF/Vulkan/staging.h" +#include "QF/Vulkan/texture.h" + +#include "r_scrap.h" +#include "vid_vulkan.h" + +struct scrap_s { + rscrap_t rscrap; + VkImage image; + VkDeviceMemory memory; + VkImageView view; + size_t bpp; + vrect_t *batch; + vrect_t **batch_tail; + vrect_t *batch_free; + size_t batch_count; + subpic_t *subpics; + qfv_device_t *device; +}; + +scrap_t * +QFV_CreateScrap (qfv_device_t *device, int size, QFVFormat format) +{ + int bpp = 0; + VkFormat fmt = VK_FORMAT_UNDEFINED; + + switch (format) { + case QFV_LUMINANCE: + bpp = 1; + fmt = VK_FORMAT_R8_UNORM; + break; + case QFV_LUMINANCE_ALPHA: + bpp = 2; + fmt = VK_FORMAT_R8G8_UNORM; + break; + case QFV_RGB: + bpp = 3; + fmt = VK_FORMAT_R8G8B8_UNORM; + break; + case QFV_RGBA: + bpp = 4; + fmt = VK_FORMAT_R8G8B8A8_UNORM; + break; + } + + scrap_t *scrap = malloc (sizeof (scrap_t)); + + R_ScrapInit (&scrap->rscrap, size, size); + + // R_ScrapInit rounds sizes up to next power of 2 + size = scrap->rscrap.width; + VkExtent3D extent = { size, size, 1 }; + scrap->image = QFV_CreateImage (device, 0, VK_IMAGE_TYPE_2D, fmt, + extent, 1, 1, VK_SAMPLE_COUNT_1_BIT, + VK_IMAGE_USAGE_TRANSFER_DST_BIT + | VK_IMAGE_USAGE_SAMPLED_BIT); + scrap->memory = QFV_AllocImageMemory (device, scrap->image, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + 0, 0); + QFV_BindImageMemory (device, scrap->image, scrap->memory, 0); + scrap->view = QFV_CreateImageView (device, scrap->image, + VK_IMAGE_VIEW_TYPE_2D, fmt, + VK_IMAGE_ASPECT_COLOR_BIT); + scrap->bpp = bpp; + scrap->subpics = 0; + scrap->device = device; + scrap->batch = 0; + scrap->batch_tail = &scrap->batch; + scrap->batch_free = 0; + scrap->batch_count = 0; + return scrap; +} + +void +QFV_ScrapClear (scrap_t *scrap) +{ + subpic_t *sp; + while (scrap->subpics) { + sp = scrap->subpics; + scrap->subpics = (subpic_t *) sp->next; + free (sp); + } + R_ScrapClear (&scrap->rscrap); +} + +void +QFV_DestroyScrap (scrap_t *scrap) +{ + qfv_device_t *device = scrap->device; + qfv_devfuncs_t *dfunc = device->funcs; + + QFV_ScrapClear (scrap); + R_ScrapDelete (&scrap->rscrap); + dfunc->vkDestroyImageView (device->dev, scrap->view, 0); + dfunc->vkDestroyImage (device->dev, scrap->image, 0); + dfunc->vkFreeMemory (device->dev, scrap->memory, 0); + + while (scrap->batch_free) { + vrect_t *b = scrap->batch_free; + scrap->batch_free = b->next; + VRect_Delete (b); + } + free (scrap); +} + +VkImageView +QFV_ScrapImageView (scrap_t *scrap) +{ + return scrap->view; +} + +subpic_t * +QFV_ScrapSubpic (scrap_t *scrap, int width, int height) +{ + vrect_t *rect; + subpic_t *subpic; + + rect = R_ScrapAlloc (&scrap->rscrap, width, height); + if (!rect) { + return 0; + } + + subpic = malloc (sizeof (subpic_t)); + *((subpic_t **) &subpic->next) = scrap->subpics; + scrap->subpics = subpic; + *((scrap_t **) &subpic->scrap) = scrap; + *((vrect_t **) &subpic->rect) = rect; + *((int *) &subpic->width) = width; + *((int *) &subpic->height) = height; + *((float *) &subpic->size) = 1.0 / scrap->rscrap.width; + return subpic; +} + +void +QFV_SubpicDelete (subpic_t *subpic) +{ + scrap_t *scrap = (scrap_t *) subpic->scrap; + vrect_t *rect = (vrect_t *) subpic->rect; + subpic_t **sp; + + for (sp = &scrap->subpics; *sp; sp = (subpic_t **) &(*sp)->next) { + if (*sp == subpic) { + break; + } + } + if (*sp != subpic) { + Sys_Error ("QFV_SubpicDelete: broken subpic"); + } + *sp = (subpic_t *) subpic->next; + free (subpic); + R_ScrapFree (&scrap->rscrap, rect); +} + +void * +QFV_SubpicBatch (subpic_t *subpic, qfv_stagebuf_t *stage) +{ + scrap_t *scrap = (scrap_t *) subpic->scrap; + vrect_t *rect = (vrect_t *) subpic->rect; + vrect_t *batch; + byte *dest; + size_t size; + + size = subpic->width * subpic->height * scrap->bpp; + if (!(dest = QFV_ClaimStagingBuffer (stage, size))) { + return 0; + } + + if (scrap->batch_free) { + batch = scrap->batch_free; + scrap->batch_free = batch->next; + } else { + batch = VRect_New (rect->x, rect->y, subpic->width, subpic->height); + } + *scrap->batch_tail = batch; + scrap->batch_tail = &batch->next; + batch->next = 0; + scrap->batch_count++; + return dest; +} + +void +QFV_ScrapFlush (scrap_t *scrap, qfv_stagebuf_t *stage, VkCommandBuffer cmd) +{ + qfv_device_t *device = scrap->device; + qfv_devfuncs_t *dfunc = device->funcs; + + if (!scrap->batch_count) { + return; + } + + size_t i; + __auto_type copy = QFV_AllocBufferImageCopy (128, alloca); + memset (copy->a, 0, 128 * sizeof (copy->a[0])); + + for (i = 0; i < scrap->batch_count && i < 128; i++) { + copy->a[i].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + copy->a[i].imageSubresource.layerCount = 1; + copy->a[i].imageExtent.depth = 1; + } + + QFV_FlushStagingBuffer (stage, 0, stage->offset); + + VkImageMemoryBarrier barrier; + qfv_pipelinestagepair_t stages; + + stages = imageLayoutTransitionStages[qfv_LT_Undefined_to_TransferDst]; + barrier = imageLayoutTransitionBarriers[qfv_LT_Undefined_to_TransferDst]; + barrier.image = scrap->image; + dfunc->vkCmdPipelineBarrier (cmd, stages.src, stages.dst, 0, 0, 0, 0, 0, + 1, &barrier); + + size_t offset = 0, size; + vrect_t *batch = scrap->batch; + while (scrap->batch_count) { + for (i = 0; i < scrap->batch_count && i < 128; i++) { + size = batch->width * batch->height * scrap->bpp; + + copy->a[i].bufferOffset = offset; + copy->a[i].imageOffset.x = batch->x; + copy->a[i].imageOffset.y = batch->y; + copy->a[i].imageExtent.width = batch->width; + copy->a[i].imageExtent.height = batch->height; + + offset += (size + 3) & ~3; + batch = batch->next; + } + dfunc->vkCmdCopyBufferToImage (cmd, stage->buffer, scrap->image, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + i, copy->a); + scrap->batch_count -= i; + } + + stages = imageLayoutTransitionStages[qfv_LT_TransferDst_to_ShaderReadOnly]; + barrier=imageLayoutTransitionBarriers[qfv_LT_TransferDst_to_ShaderReadOnly]; + barrier.image = scrap->image; + dfunc->vkCmdPipelineBarrier (cmd, stages.src, stages.dst, 0, 0, 0, 0, 0, + 1, &barrier); + + *scrap->batch_tail = scrap->batch_free; + scrap->batch_free = scrap->batch; + scrap->batch = 0; + scrap->batch_tail = &scrap->batch; + stage->offset = 0; +} diff --git a/libs/video/renderer/vulkan/vulkan_draw.c b/libs/video/renderer/vulkan/vulkan_draw.c index 89ead5380..badc01332 100644 --- a/libs/video/renderer/vulkan/vulkan_draw.c +++ b/libs/video/renderer/vulkan/vulkan_draw.c @@ -46,6 +46,7 @@ #include "QF/dstring.h" #include "QF/hash.h" #include "QF/quakefs.h" +#include "QF/render.h" #include "QF/sys.h" #include "QF/vid.h" @@ -59,6 +60,7 @@ #include "QF/Vulkan/device.h" #include "QF/Vulkan/image.h" #include "QF/Vulkan/staging.h" +#include "QF/Vulkan/texture.h" #include "r_internal.h" #include "vid_vulkan.h" @@ -75,10 +77,9 @@ typedef struct { #define INDS_PER_QUAD (5) // one per vert plus primitive reset //FIXME move into a context struct -VkImage conchars_image; -VkDeviceMemory conchars_memory; -VkImageView conchars_view; VkSampler conchars_sampler; +scrap_t *draw_scrap; +subpic_t *conchars; VkBuffer quad_vert_buffer; VkBuffer quad_ind_buffer; @@ -201,9 +202,7 @@ Vulkan_Draw_Shutdown (vulkan_ctx_t *ctx) destroy_quad_buffers (ctx); dfunc->vkDestroyPipeline (device->dev, twod_pipeline, 0); - dfunc->vkDestroyImageView (device->dev, conchars_view, 0); - dfunc->vkFreeMemory (device->dev, conchars_memory, 0); - dfunc->vkDestroyImage (device->dev, conchars_image, 0); + QFV_DestroyScrap (draw_scrap); } void @@ -214,37 +213,24 @@ Vulkan_Draw_Init (vulkan_ctx_t *ctx) VkCommandBuffer cmd = ctx->cmdbuffer; create_quad_buffers (ctx); - - qpic_t *charspic = Draw_Font8x8Pic (); - VkExtent3D extent = { charspic->width, charspic->height, 1 }; - conchars_image = QFV_CreateImage (device, 0, VK_IMAGE_TYPE_2D, - VK_FORMAT_A1R5G5B5_UNORM_PACK16, - extent, 1, 1, - VK_SAMPLE_COUNT_1_BIT, - VK_IMAGE_USAGE_TRANSFER_DST_BIT - | VK_IMAGE_USAGE_TRANSFER_SRC_BIT - | VK_IMAGE_USAGE_SAMPLED_BIT); - conchars_memory = QFV_AllocImageMemory (device, conchars_image, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - 0, 0); - QFV_BindImageMemory (device, conchars_image, conchars_memory, 0); - conchars_view = QFV_CreateImageView (device, conchars_image, - VK_IMAGE_VIEW_TYPE_2D, - VK_FORMAT_A1R5G5B5_UNORM_PACK16, - VK_IMAGE_ASPECT_COLOR_BIT); + draw_scrap = QFV_CreateScrap (device, 2048, QFV_RGBA); conchars_sampler = QFV_GetSampler (ctx, "quakepic"); - uint16_t *chars_data = ctx->staging[0]->data; + qpic_t *charspic = Draw_Font8x8Pic (); + + conchars = QFV_ScrapSubpic (draw_scrap, charspic->width, charspic->height); + + byte *chars_data = QFV_SubpicBatch (conchars, ctx->staging[0]); size_t size = charspic->width * charspic->height; for (size_t i = 0; i < size; i++) { - // convert 0xff = transparent, 0xfe = white to 0x0000 and 0xffff - // for a1r5g5b5 - uint16_t pix = (charspic->data[i] & 1) - 1; - chars_data[i] = pix; + byte pix = charspic->data[i]; + byte *col = vid.palette + pix * 3; + *chars_data++ = *col++; + *chars_data++ = *col++; + *chars_data++ = *col++; + *chars_data++ = (pix == 255) - 1; } - QFV_FlushStagingBuffer (ctx->staging[0], 0, size * sizeof (uint16_t)); - VkImageMemoryBarrier barrier; - qfv_pipelinestagepair_t stages; + dfunc->vkWaitForFences (device->dev, 1, &ctx->fence, VK_TRUE, ~0ull); dfunc->vkResetCommandBuffer (cmd, 0); VkCommandBufferBeginInfo beginInfo = { @@ -252,33 +238,7 @@ Vulkan_Draw_Init (vulkan_ctx_t *ctx) VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, 0, }; dfunc->vkBeginCommandBuffer (cmd, &beginInfo); - - stages = imageLayoutTransitionStages[qfv_LT_Undefined_to_TransferDst]; - barrier=imageLayoutTransitionBarriers[qfv_LT_Undefined_to_TransferDst]; - barrier.image = conchars_image; - dfunc->vkCmdPipelineBarrier (cmd, stages.src, stages.dst, 0, - 0, 0, - 0, 0, - 1, &barrier); - - VkBufferImageCopy region = { - 0, 0, 0, - { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }, - { 0, 0, 0 }, - { charspic->width, charspic->height, 1 }, - }; - dfunc->vkCmdCopyBufferToImage (cmd, - ctx->staging[0]->buffer, conchars_image, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - 1, ®ion); - - stages = imageLayoutTransitionStages[qfv_LT_TransferDst_to_ShaderReadOnly]; - barrier=imageLayoutTransitionBarriers[qfv_LT_TransferDst_to_ShaderReadOnly]; - barrier.image = conchars_image; - dfunc->vkCmdPipelineBarrier (cmd, stages.src, stages.dst, 0, - 0, 0, - 0, 0, - 1, &barrier); + QFV_ScrapFlush (draw_scrap, ctx->staging[0], cmd); dfunc->vkEndCommandBuffer (cmd); VkSubmitInfo submitInfo = { @@ -305,7 +265,7 @@ Vulkan_Draw_Init (vulkan_ctx_t *ctx) }; VkDescriptorImageInfo imageInfo = { conchars_sampler, - conchars_view, + QFV_ScrapImageView (draw_scrap), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, }; __auto_type cmdBuffers @@ -347,10 +307,12 @@ draw_pic (float x, float y, int w, int h, qpic_t *pic, drawvert_t *verts = quad_verts + num_quads * VERTS_PER_QUAD; num_quads += VERTS_PER_QUAD; - float sl = (srcx + 0.03125) / 128.0; - float sr = (srcx + srcw - 0.03125) / 128.0; - float st = (srcy + 0.03125) / 128.0; - float sb = (srcy + srch - 0.03125) / 128.0; + //FIXME extract subpic from pic + float size = conchars->size; + float sl = (srcx + 0.03125) * size; + float sr = (srcx + srcw - 0.03125) * size; + float st = (srcy + 0.03125) * size; + float sb = (srcy + srch - 0.03125) * size; verts[0].xy[0] = x; verts[0].xy[1] = y;