From 87356a52117abd5bdab546bedad887d622717343 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 23 Jun 2023 20:37:06 +0900 Subject: [PATCH] [vulkan] Hook up sprite rendering in the new system That one went smoothly for a change. --- include/QF/Vulkan/qf_main.h | 2 - include/QF/Vulkan/qf_sprite.h | 4 - libs/video/renderer/vulkan/vulkan_main.c | 26 --- libs/video/renderer/vulkan/vulkan_sprite.c | 205 +++++---------------- 4 files changed, 51 insertions(+), 186 deletions(-) diff --git a/include/QF/Vulkan/qf_main.h b/include/QF/Vulkan/qf_main.h index ba6659086..faf8f4b2b 100644 --- a/include/QF/Vulkan/qf_main.h +++ b/include/QF/Vulkan/qf_main.h @@ -36,7 +36,5 @@ struct scene_s; void Vulkan_Main_CreateRenderPasses (struct vulkan_ctx_s *ctx); void Vulkan_NewScene (struct scene_s *scene, struct vulkan_ctx_s *ctx); void Vulkan_RenderView (struct qfv_orenderframe_s *rFrame); -void Vulkan_RenderEntities (struct entqueue_s *queue, - struct qfv_orenderframe_s *rFrame); #endif//__QF_Vulkan_qf_main_h diff --git a/include/QF/Vulkan/qf_sprite.h b/include/QF/Vulkan/qf_sprite.h index 827c26216..6a7359942 100644 --- a/include/QF/Vulkan/qf_sprite.h +++ b/include/QF/Vulkan/qf_sprite.h @@ -88,10 +88,6 @@ void Vulkan_Sprite_DescriptorSet (struct vulkan_ctx_s *ctx, void Vulkan_Mod_SpriteLoadFrames (struct mod_sprite_ctx_s *sprite_ctx, struct vulkan_ctx_s *ctx); -void Vulkan_SpriteBegin (struct qfv_orenderframe_s *rFrame); -void Vulkan_DrawSprite (struct entity_s ent, struct qfv_orenderframe_s *rFrame); -void Vulkan_SpriteEnd (struct qfv_orenderframe_s *rFrame); - void Vulkan_Sprite_Init (struct vulkan_ctx_s *ctx); void Vulkan_Sprite_Shutdown (struct vulkan_ctx_s *ctx); diff --git a/libs/video/renderer/vulkan/vulkan_main.c b/libs/video/renderer/vulkan/vulkan_main.c index bfccc3a4a..a7dc50fec 100644 --- a/libs/video/renderer/vulkan/vulkan_main.c +++ b/libs/video/renderer/vulkan/vulkan_main.c @@ -68,31 +68,6 @@ #include "r_internal.h" #include "vid_vulkan.h" -void -Vulkan_RenderEntities (entqueue_t *queue, qfv_orenderframe_t *rFrame) -{ - if (!r_drawentities) - return; - //FIXME need a better way (components? but HasComponent isn't free) -#define RE_LOOP(type_name, Type) \ - do { \ - int begun = 0; \ - for (size_t i = 0; i < queue->ent_queues[mod_##type_name].size; \ - i++) { \ - entity_t ent = queue->ent_queues[mod_##type_name].a[i]; \ - if (!begun) { \ - Vulkan_##Type##Begin (rFrame); \ - begun = 1; \ - } \ - Vulkan_Draw##Type (ent, rFrame); \ - } \ - if (begun) \ - Vulkan_##Type##End (rFrame); \ - } while (0) - - RE_LOOP (sprite, Sprite); -} - static void Vulkan_DrawViewModel (vulkan_ctx_t *ctx) { @@ -127,7 +102,6 @@ Vulkan_RenderView (qfv_orenderframe_t *rFrame) Vulkan_DrawWaterSurfaces (rFrame); Vulkan_DrawParticles (rFrame); Vulkan_Bsp_Flush (ctx); - Vulkan_RenderEntities (r_ent_queue, rFrame); Vulkan_Scene_Flush (ctx); } diff --git a/libs/video/renderer/vulkan/vulkan_sprite.c b/libs/video/renderer/vulkan/vulkan_sprite.c index 3f11f58e4..51656b1d4 100644 --- a/libs/video/renderer/vulkan/vulkan_sprite.c +++ b/libs/video/renderer/vulkan/vulkan_sprite.c @@ -68,24 +68,11 @@ #include "r_internal.h" #include "vid_vulkan.h" -static const char * __attribute__((used)) sprite_pass_names[] = { - "depth", - "g-buffer", - "translucent", -}; - -static QFV_Subpass subpass_map[] = { - QFV_passDepth, // QFV_spriteDepth - QFV_passGBuffer, // QFV_spriteGBuffer - QFV_passTranslucentFrag,// QFV_spriteTranslucent -}; - static void emit_commands (VkCommandBuffer cmd, qfv_sprite_t *sprite, int numPC, qfv_push_constants_t *constants, - qfv_orenderframe_t *rFrame, entity_t ent) + vulkan_ctx_t *ctx, entity_t ent) { - vulkan_ctx_t *ctx = rFrame->vulkan_ctx; qfv_device_t *device = ctx->device; qfv_devfuncs_t *dfunc = device->funcs; spritectx_t *sctx = ctx->sprite_context; @@ -103,123 +90,6 @@ emit_commands (VkCommandBuffer cmd, qfv_sprite_t *sprite, QFV_CmdEndLabel (device, cmd); } -void -Vulkan_DrawSprite (entity_t ent, qfv_orenderframe_t *rFrame) -{ - vulkan_ctx_t *ctx = rFrame->vulkan_ctx; - spritectx_t *sctx = ctx->sprite_context; - spriteframe_t *sframe = &sctx->frames.a[ctx->curFrame]; - renderer_t *renderer = Ent_GetComponent (ent.id, scene_renderer, ent.reg); - model_t *model = renderer->model; - msprite_t *sprite = model->cache.data; - - mat4f_t mat = {}; - uint32_t frame; - qfv_push_constants_t push_constants[] = { - { VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof (mat), mat }, - { VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, - 64, sizeof (frame), &frame }, - }; - - animation_t *animation = Ent_GetComponent (ent.id, scene_animation, - ent.reg); - frame = (ptrdiff_t) R_GetSpriteFrame (sprite, animation); - - transform_t transform = Entity_Transform (ent); - mat[3] = Transform_GetWorldPosition (transform); - vec4f_t cameravec = r_refdef.frame.position - mat[3]; - R_BillboardFrame (transform, sprite->type, cameravec, - &mat[2], &mat[1], &mat[0]); - mat[0] = -mat[0]; - - emit_commands (sframe->cmdSet.a[QFV_spriteDepth], - (qfv_sprite_t *) ((byte *) sprite + sprite->data), - 2, push_constants, rFrame, ent); - emit_commands (sframe->cmdSet.a[QFV_spriteGBuffer], - (qfv_sprite_t *) ((byte *) sprite + sprite->data), - 2, push_constants, rFrame, ent); -} - -static void -sprite_begin_subpass (QFV_SpriteSubpass subpass, VkPipeline pipeline, - qfv_orenderframe_t *rFrame) -{ - vulkan_ctx_t *ctx = rFrame->vulkan_ctx; - qfv_device_t *device = ctx->device; - qfv_devfuncs_t *dfunc = device->funcs; - spritectx_t *sctx = ctx->sprite_context; - spriteframe_t *sframe = &sctx->frames.a[ctx->curFrame]; - VkCommandBuffer cmd = sframe->cmdSet.a[subpass]; - - dfunc->vkResetCommandBuffer (cmd, 0); - VkCommandBufferInheritanceInfo inherit = { - VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, 0, - rFrame->renderpass->renderpass, subpass_map[subpass], - rFrame->framebuffer, - 0, 0, 0, - }; - VkCommandBufferBeginInfo beginInfo = { - VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, 0, - VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT - | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &inherit, - }; - dfunc->vkBeginCommandBuffer (cmd, &beginInfo); - - QFV_duCmdBeginLabel (device, cmd, va (ctx->va_ctx, "sprite:%s", - sprite_pass_names[subpass]), - { 0.6, 0.5, 0, 1}); - - dfunc->vkCmdBindPipeline (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); - VkDescriptorSet sets[] = { - Vulkan_Matrix_Descriptors (ctx, ctx->curFrame), - }; - dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, - sctx->layout, 0, 1, sets, 0, 0); - dfunc->vkCmdSetViewport (cmd, 0, 1, &rFrame->renderpass->viewport); - dfunc->vkCmdSetScissor (cmd, 0, 1, &rFrame->renderpass->scissor); - - //XXX glsl_Fog_GetColor (fog); - //XXX fog[3] = glsl_Fog_GetDensity () / 64.0; -} - -static void -sprite_end_subpass (VkCommandBuffer cmd, vulkan_ctx_t *ctx) -{ - qfv_device_t *device = ctx->device; - qfv_devfuncs_t *dfunc = device->funcs; - - QFV_duCmdEndLabel (device, cmd); - dfunc->vkEndCommandBuffer (cmd); -} - -void -Vulkan_SpriteBegin (qfv_orenderframe_t *rFrame) -{ - vulkan_ctx_t *ctx = rFrame->vulkan_ctx; - spritectx_t *sctx = ctx->sprite_context; - spriteframe_t *sframe = &sctx->frames.a[ctx->curFrame]; - - //XXX quat_t fog; - DARRAY_APPEND (&rFrame->subpassCmdSets[QFV_passDepth], - sframe->cmdSet.a[QFV_spriteDepth]); - DARRAY_APPEND (&rFrame->subpassCmdSets[QFV_passGBuffer], - sframe->cmdSet.a[QFV_spriteGBuffer]); - - sprite_begin_subpass (QFV_spriteDepth, sctx->depth, rFrame); - sprite_begin_subpass (QFV_spriteGBuffer, sctx->gbuf, rFrame); -} - -void -Vulkan_SpriteEnd (qfv_orenderframe_t *rFrame) -{ - vulkan_ctx_t *ctx = rFrame->vulkan_ctx; - spritectx_t *sctx = ctx->sprite_context; - spriteframe_t *sframe = &sctx->frames.a[ctx->curFrame]; - - sprite_end_subpass (sframe->cmdSet.a[QFV_spriteDepth], ctx); - sprite_end_subpass (sframe->cmdSet.a[QFV_spriteGBuffer], ctx); -} - static VkDescriptorBufferInfo base_buffer_info = { .range = VK_WHOLE_SIZE, }; @@ -285,9 +155,59 @@ Vulkan_Sprint_FreeDescriptors (vulkan_ctx_t *ctx, qfv_sprite_t *sprite) &sprite->descriptors); } +static void +sprite_draw_ent (qfv_taskctx_t *taskctx, entity_t ent) +{ + auto ctx = taskctx->ctx; + renderer_t *renderer = Ent_GetComponent (ent.id, scene_renderer, ent.reg); + auto model = renderer->model; + msprite_t *sprite = model->cache.data; + + mat4f_t mat = {}; + uint32_t frame; + qfv_push_constants_t push_constants[] = { + { VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof (mat), mat }, + { VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + 64, sizeof (frame), &frame }, + }; + + animation_t *animation = Ent_GetComponent (ent.id, scene_animation, + ent.reg); + frame = (ptrdiff_t) R_GetSpriteFrame (sprite, animation); + + transform_t transform = Entity_Transform (ent); + mat[3] = Transform_GetWorldPosition (transform); + vec4f_t cameravec = r_refdef.frame.position - mat[3]; + R_BillboardFrame (transform, sprite->type, cameravec, + &mat[2], &mat[1], &mat[0]); + mat[0] = -mat[0]; + + emit_commands (taskctx->cmd, + (qfv_sprite_t *) ((byte *) sprite + sprite->data), + 2, push_constants, ctx, ent); +} + static void sprite_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx) { + auto taskctx = (qfv_taskctx_t *) ectx; + auto ctx = taskctx->ctx; + auto device = ctx->device; + auto dfunc = device->funcs; + auto sctx = ctx->sprite_context; + auto cmd = taskctx->cmd; + + VkDescriptorSet sets[] = { + Vulkan_Matrix_Descriptors (ctx, ctx->curFrame), + }; + dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, + sctx->layout, 0, 1, sets, 0, 0); + + auto queue = r_ent_queue; //FIXME fetch from scene + for (size_t i = 0; i < queue->ent_queues[mod_sprite].size; i++) { + entity_t ent = queue->ent_queues[mod_sprite].a[i]; + sprite_draw_ent (taskctx, ent); + } } static exprfunc_t sprite_draw_func[] = { @@ -302,8 +222,6 @@ static exprsym_t sprite_task_syms[] = { void Vulkan_Sprite_Init (vulkan_ctx_t *ctx) { - qfv_device_t *device = ctx->device; - qfvPushDebug (ctx, "sprite init"); QFV_Render_AddTasks (ctx, sprite_task_syms); @@ -324,22 +242,6 @@ Vulkan_Sprite_Init (vulkan_ctx_t *ctx) sctx->pool = Vulkan_CreateDescriptorPool (ctx, "sprite_pool"); sctx->setLayout = Vulkan_CreateDescriptorSetLayout (ctx, "sprite_set"); - for (size_t i = 0; i < frames; i++) { - __auto_type sframe = &sctx->frames.a[i]; - - DARRAY_INIT (&sframe->cmdSet, QFV_spriteNumPasses); - DARRAY_RESIZE (&sframe->cmdSet, QFV_spriteNumPasses); - sframe->cmdSet.grow = 0; - - QFV_AllocateCommandBuffers (device, ctx->cmdpool, 1, &sframe->cmdSet); - - for (int j = 0; j < QFV_spriteNumPasses; j++) { - QFV_duSetObjectName (device, VK_OBJECT_TYPE_COMMAND_BUFFER, - sframe->cmdSet.a[j], - va (ctx->va_ctx, "cmd:sprite:%zd:%s", i, - sprite_pass_names[j])); - } - } qfvPopDebug (ctx); } @@ -350,11 +252,6 @@ Vulkan_Sprite_Shutdown (vulkan_ctx_t *ctx) qfv_devfuncs_t *dfunc = device->funcs; spritectx_t *sctx = ctx->sprite_context; - for (size_t i = 0; i < sctx->frames.size; i++) { - __auto_type sframe = &sctx->frames.a[i]; - free (sframe->cmdSet.a); - } - dfunc->vkDestroyPipeline (device->dev, sctx->depth, 0); dfunc->vkDestroyPipeline (device->dev, sctx->gbuf, 0); free (sctx->frames.a);