From 9f6441684611a432521b770130173c929084a90a Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 27 Jan 2021 16:16:28 +0900 Subject: [PATCH] [vulkan] Create the lights buffer It's a tad bogus as it's the lights close to the camera, but it should at least be a good start once things are working. There's currently something very wrong with the state of things. --- include/QF/Vulkan/buffer.h | 5 +++ libs/video/renderer/vulkan/instance.c | 9 ++++- libs/video/renderer/vulkan/vulkan_alias.c | 46 ++++++++++++++++++++++- libs/video/renderer/vulkan/vulkan_main.c | 6 +-- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/include/QF/Vulkan/buffer.h b/include/QF/Vulkan/buffer.h index 9346d0466..712b14a96 100644 --- a/include/QF/Vulkan/buffer.h +++ b/include/QF/Vulkan/buffer.h @@ -18,6 +18,11 @@ typedef struct qfv_buffertransitionset_s typedef struct qfv_bufferbarrierset_s DARRAY_TYPE (VkBufferMemoryBarrier) qfv_bufferbarrierset_t; +typedef struct qfv_bufferset_s + DARRAY_TYPE (VkBuffer) qfv_bufferset_t; +#define QFV_AllocBufferSet(num, allocator) \ + DARRAY_ALLOCFIXED (qfv_bufferset_t, num, allocator) + struct qfv_device_s; VkBuffer QFV_CreateBuffer (struct qfv_device_s *device, VkDeviceSize size, diff --git a/libs/video/renderer/vulkan/instance.c b/libs/video/renderer/vulkan/instance.c index 617d792e8..d1c845c6b 100644 --- a/libs/video/renderer/vulkan/instance.c +++ b/libs/video/renderer/vulkan/instance.c @@ -125,6 +125,11 @@ static int message_types = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; +static void +debug_breakpoint (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity) +{ +} + static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, @@ -144,7 +149,9 @@ debug_callback (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) { msgSev = "error: "; } - fprintf (stderr, "validation layer: %s%s\n", msgSev, callbackData->pMessage); + fprintf (stderr, "validation layer: %s%s\n", msgSev, + callbackData->pMessage); + debug_breakpoint (messageSeverity); return VK_FALSE; } diff --git a/libs/video/renderer/vulkan/vulkan_alias.c b/libs/video/renderer/vulkan/vulkan_alias.c index 3fa9d7c52..4ba872a1a 100644 --- a/libs/video/renderer/vulkan/vulkan_alias.c +++ b/libs/video/renderer/vulkan/vulkan_alias.c @@ -55,6 +55,7 @@ #include "QF/Vulkan/qf_alias.h" #include "QF/Vulkan/qf_texture.h" +#include "QF/Vulkan/buffer.h" #include "QF/Vulkan/command.h" #include "QF/Vulkan/descriptor.h" #include "QF/Vulkan/device.h" @@ -131,8 +132,9 @@ Vulkan_DrawAlias (struct entity_s *ent, struct vulkan_ctx_s *ctx) dfunc->vkCmdPushDescriptorSetKHR (aframe->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, actx->layout, - 0, 2, aframe->descriptors - + ALIAS_BUFFER_INFOS); + ALIAS_BUFFER_INFOS, ALIAS_IMAGE_INFOS, + aframe->descriptors + + ALIAS_BUFFER_INFOS); dfunc->vkCmdDrawIndexed (aframe->cmd, 3 * hdr->mdl.numtris, 1, 0, 0, 0); } @@ -142,6 +144,8 @@ Vulkan_AliasBegin (vulkan_ctx_t *ctx) qfv_device_t *device = ctx->device; qfv_devfuncs_t *dfunc = device->funcs; aliasctx_t *actx = ctx->alias_context; + + dlight_t *lights[ALIAS_LIGHTS]; //XXX quat_t fog; __auto_type cframe = &ctx->framebuffers.a[ctx->curFrame]; @@ -149,6 +153,18 @@ Vulkan_AliasBegin (vulkan_ctx_t *ctx) VkCommandBuffer cmd = aframe->cmd; DARRAY_APPEND (cframe->subCommand, cmd); + R_FindNearLights (r_origin, ALIAS_LIGHTS, lights); + aframe->lights->light_count = 0; + for (int i = 0; i < ALIAS_LIGHTS; i++) { + if (!lights[i]) { + break; + } + aframe->lights->light_count++; + VectorCopy (lights[i]->color, aframe->lights->lights[i].color); + VectorCopy (lights[i]->origin, aframe->lights->lights[i].position); + aframe->lights->lights[i].dist = lights[i]->radius; + } + //FIXME need per frame matrices aframe->bufferInfo[0].buffer = ctx->matrices.buffer_3d; aframe->bufferInfo[1].buffer = aframe->light_buffer; @@ -169,6 +185,12 @@ Vulkan_AliasBegin (vulkan_ctx_t *ctx) dfunc->vkCmdBindPipeline (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, actx->pipeline); + VkDescriptorSet sets[] = { + aframe->descriptors[0].dstSet, + aframe->descriptors[1].dstSet, + }; + dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, + actx->layout, 0, 2, sets, 0, 0); VkViewport viewport = {0, 0, vid.width, vid.height, 0, 1}; VkRect2D scissor = { {0, 0}, {vid.width, vid.height} }; dfunc->vkCmdSetViewport (cmd, 0, 1, &viewport); @@ -214,6 +236,7 @@ void Vulkan_Alias_Init (vulkan_ctx_t *ctx) { qfv_device_t *device = ctx->device; + qfv_devfuncs_t *dfunc = device->funcs; aliasctx_t *actx = calloc (1, sizeof (aliasctx_t)); ctx->alias_context = actx; @@ -239,10 +262,29 @@ Vulkan_Alias_Init (vulkan_ctx_t *ctx) __auto_type cmdBuffers = QFV_AllocCommandBufferSet (frames, alloca); QFV_AllocateCommandBuffers (device, ctx->cmdpool, 1, cmdBuffers); + __auto_type lbuffers = QFV_AllocBufferSet (frames, alloca); + for (size_t i = 0; i < frames; i++) { + lbuffers->a[i] = QFV_CreateBuffer (device, sizeof (qfv_light_buffer_t), + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); + } + VkMemoryRequirements requirements; + dfunc->vkGetBufferMemoryRequirements (device->dev, lbuffers->a[0], + &requirements); + actx->light_memory = QFV_AllocBufferMemory (device, lbuffers->a[0], + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, + frames * requirements.size, 0); + byte *light_data; + dfunc->vkMapMemory (device->dev, actx->light_memory, 0, + frames * requirements.size, 0, (void **) &light_data); + __auto_type sets = QFV_AllocateDescriptorSet (device, pool, layouts); for (size_t i = 0; i < frames; i++) { __auto_type aframe = &actx->frames.a[i]; aframe->cmd = cmdBuffers->a[i]; + aframe->light_buffer = lbuffers->a[i]; + aframe->lights = (qfv_light_buffer_t *) (light_data + i * requirements.size); + QFV_BindBufferMemory (device, lbuffers->a[i], actx->light_memory, + i * requirements.size); for (int j = 0; j < ALIAS_BUFFER_INFOS; j++) { aframe->bufferInfo[j] = base_buffer_info; diff --git a/libs/video/renderer/vulkan/vulkan_main.c b/libs/video/renderer/vulkan/vulkan_main.c index 95fcd02ab..8df89eaa1 100644 --- a/libs/video/renderer/vulkan/vulkan_main.c +++ b/libs/video/renderer/vulkan/vulkan_main.c @@ -109,14 +109,12 @@ setup_view (vulkan_ctx_t *ctx) static void R_RenderEntities (vulkan_ctx_t *ctx) { - entity_t *ent; - int begun; - if (!r_drawentities->int_val) return; #define RE_LOOP(type_name, Type) \ do { \ - begun = 0; \ + entity_t *ent; \ + int begun = 0; \ for (ent = r_ent_queue; ent; ent = ent->next) { \ if (ent->model->type != mod_##type_name) \ continue; \