From 96501e16cea78a4556212cae34067b266659aa09 Mon Sep 17 00:00:00 2001 From: tadgem <98434757+tadgem@users.noreply.github.com> Date: Sat, 13 Jul 2024 00:29:36 +0100 Subject: [PATCH] add non callback pipeline+api to begin/end cmd buf --- examples/pipeline-abstraction/main.cpp | 28 ++++++++------ lvk/include/lvk/Pipeline.h | 51 +++++++++++++++++++++++++- lvk/include/lvk/VulkanAPI.h | 2 + lvk/src/lvk/VulkanAPI.cpp | 16 ++++++++ 4 files changed, 84 insertions(+), 13 deletions(-) diff --git a/examples/pipeline-abstraction/main.cpp b/examples/pipeline-abstraction/main.cpp index f98176c..5a3ce82 100644 --- a/examples/pipeline-abstraction/main.cpp +++ b/examples/pipeline-abstraction/main.cpp @@ -19,14 +19,14 @@ using VkRecordCommandCallback = std::function m_Pipeline; + PipelineT m_Pipeline; View m_View; Mesh m_ViewQuad; }; -Pipeline CreateViewPipeline(VulkanAPI& vk, LvkIm3dState& im3dState, ShaderProgram& gbufferProg, ShaderProgram& lightPassProg) +PipelineT CreateViewPipeline(VulkanAPI& vk, LvkIm3dState& im3dState, ShaderProgram& gbufferProg, ShaderProgram& lightPassProg) { - Pipeline p{}; + PipelineT p{}; auto* gbuffer = p.AddFramebuffer(vk); gbuffer->AddColourAttachment(vk, ResolutionScale::Full, 1, VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R16G16B16A16_SFLOAT, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_IMAGE_ASPECT_COLOR_BIT); @@ -53,10 +53,12 @@ Pipeline CreateViewPipeline(VulkanAPI& vk, LvkIm3dState // create gbuffer pipeline VkPipelineLayout gbufferPipelineLayout; + auto gbufferBindingDescriptions = Vector{VertexDataPosNormalUv::GetBindingDescription() }; + auto gbufferAttributeDescriptions = VertexDataPosNormalUv::GetAttributeDescriptions(); VkPipeline gbufferPipeline = vk.CreateRasterizationGraphicsPipeline( gbufferProg, - Vector{VertexDataPosNormalUv::GetBindingDescription() }, - VertexDataPosNormalUv::GetAttributeDescriptions(), + gbufferBindingDescriptions, + gbufferAttributeDescriptions, gbuffer->m_RenderPass, vk.m_SwapChainImageExtent.width, vk.m_SwapChainImageExtent.height, VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, false, @@ -65,10 +67,12 @@ Pipeline CreateViewPipeline(VulkanAPI& vk, LvkIm3dState // create present graphics pipeline // Pipeline stage? VkPipelineLayout lightPassPipelineLayout; + auto lightPassBindingDescriptions = Vector{VertexDataPosUv::GetBindingDescription() }; + auto lightPassAttributeDescriptions = VertexDataPosUv::GetAttributeDescriptions(); VkPipeline lightPassPipeline = vk.CreateRasterizationGraphicsPipeline( lightPassProg, - Vector{VertexDataPosUv::GetBindingDescription() }, - VertexDataPosUv::GetAttributeDescriptions(), + lightPassBindingDescriptions, + lightPassAttributeDescriptions, lightPassImage->m_RenderPass, vk.m_SwapChainImageExtent.width, vk.m_SwapChainImageExtent.height, VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, false, @@ -211,7 +215,7 @@ Pipeline CreateViewPipeline(VulkanAPI& vk, LvkIm3dState ViewData CreateView(VulkanAPI& vk, LvkIm3dState im3dState, ShaderProgram gbufferProg, ShaderProgram lightPassProg) { - Pipeline pipeline = CreateViewPipeline(vk, im3dState, gbufferProg, lightPassProg); + PipelineT pipeline = CreateViewPipeline(vk, im3dState, gbufferProg, lightPassProg); static Vector screenQuadVerts = { { { -1.0f, -1.0f , 0.0f}, { 0.0f, 0.0f } }, @@ -526,14 +530,14 @@ void OnIm3D() int main() { VulkanAPI_SDL vk; bool enableMSAA = false; - vk.Start(1920, 1080, enableMSAA); + vk.Start("Pipeline Example",1920, 1080, enableMSAA); auto im3dState = LoadIm3D(vk); DeferredLightData lightDataCpu{}; FillExampleLightData(lightDataCpu); - ShaderProgram gbufferProg = ShaderProgram::Create(vk, "shaders/gbuffer.vert.spv", "shaders/gbuffer.frag.spv"); - ShaderProgram lightPassProg = ShaderProgram::Create(vk, "shaders/lights.vert.spv", "shaders/lights.frag.spv"); + ShaderProgram gbufferProg = ShaderProgram::Create(vk, "../shaders/gbuffer.vert.spv", "../shaders/gbuffer.frag.spv"); + ShaderProgram lightPassProg = ShaderProgram::Create(vk, "../shaders/lights.vert.spv", "../shaders/lights.frag.spv"); ViewData viewA = CreateView(vk, im3dState, gbufferProg, lightPassProg); viewA.m_View.m_Camera.Position = { -40.0, 10.0f, 30.0f }; @@ -544,7 +548,7 @@ int main() { // create vertex and index buffer // allocate materials instead of raw buffers etc. - RenderModel m = CreateRenderModelGbuffer(vk, "assets/sponza/sponza.gltf", gbufferProg); + RenderModel m = CreateRenderModelGbuffer(vk, "../assets/sponza/sponza.gltf", gbufferProg); while (vk.ShouldRun()) { diff --git a/lvk/include/lvk/Pipeline.h b/lvk/include/lvk/Pipeline.h index a8b66df..c3c437a 100644 --- a/lvk/include/lvk/Pipeline.h +++ b/lvk/include/lvk/Pipeline.h @@ -16,7 +16,6 @@ namespace lvk VkPipelineLayout m_PipelineLayout; }; - template class Pipeline { public: @@ -27,6 +26,56 @@ namespace lvk Optional m_OutputFramebuffer; LvkIm3dViewState* m_Im3dState = nullptr; + Framebuffer* AddFramebuffer(VulkanAPI& vk) + { + m_FBs.push_back(new Framebuffer()); + return m_FBs[m_FBs.size() - 1]; + } + + void SetOutputFramebuffer(Framebuffer* fb) + { + m_OutputFramebuffer = fb; + } + + Framebuffer* GetOutputFramebuffer() + { + if (m_OutputFramebuffer.has_value()) + { + return m_OutputFramebuffer.value(); + } + return nullptr; + } + + Material* AddMaterial(VulkanAPI& vk, ShaderProgram& prog) + { + m_PipelineMaterials.push_back(new Material(Material::Create(vk, prog))); + return m_PipelineMaterials.back(); + } + + VkPipelineData* AddPipeline(VulkanAPI& vk, VkPipeline pipeline, VkPipelineLayout layout) + { + m_PipelineDatas.emplace_back(new VkPipelineData(pipeline, layout)); + return m_PipelineDatas.back(); + } + + LvkIm3dViewState* AddIm3d(VulkanAPI& vk, LvkIm3dState im3dState) + { + m_Im3dState = new LvkIm3dViewState(AddIm3dForViewport(vk, im3dState, m_OutputFramebuffer.value()->m_RenderPass, false)); + return m_Im3dState; + } + }; + + template + class PipelineT + { + public: + Vector m_FBs; + Vector m_PipelineMaterials; + Vector m_PipelineDatas; + + Optional m_OutputFramebuffer; + LvkIm3dViewState* m_Im3dState = nullptr; + Optional<_Ty> m_CommandCallback; Framebuffer* AddFramebuffer(VulkanAPI& vk) diff --git a/lvk/include/lvk/VulkanAPI.h b/lvk/include/lvk/VulkanAPI.h index 0def3f2..14f3f6e 100644 --- a/lvk/include/lvk/VulkanAPI.h +++ b/lvk/include/lvk/VulkanAPI.h @@ -292,6 +292,8 @@ namespace lvk void CopyBufferToImage(VkBuffer& src, VkImage& image, uint32_t width, uint32_t height); VkCommandBuffer BeginSingleTimeCommands(); void EndSingleTimeCommands(VkCommandBuffer& commandBuffer); + VkCommandBuffer& BeginGraphicsCommands(uint32_t frameIndex); + void EndGraphicsCommands(uint32_t frameIndex); void RecordGraphicsCommands(std::function graphicsCommandsCallback); void TransitionImageLayout(VkImage image, VkFormat format, uint32_t numMips, VkImageLayout oldLayout, VkImageLayout newLayout); void GenerateMips(VkImage image, VkFormat format, uint32_t imageWidth, uint32_t imageHeight, uint32_t numMips, VkFilter filterMethod); diff --git a/lvk/src/lvk/VulkanAPI.cpp b/lvk/src/lvk/VulkanAPI.cpp index bc5afcb..f9b41d8 100644 --- a/lvk/src/lvk/VulkanAPI.cpp +++ b/lvk/src/lvk/VulkanAPI.cpp @@ -2194,6 +2194,22 @@ void lvk::VulkanAPI::EndSingleTimeCommands(VkCommandBuffer& commandBuffer) vkFreeCommandBuffers(m_LogicalDevice , m_GraphicsQueueCommandPool, 1, &commandBuffer); } +VkCommandBuffer& VulkanAPI::BeginGraphicsCommands(uint32_t frameIndex) { + VkCommandBufferBeginInfo commandBufferBeginInfo{}; + commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + commandBufferBeginInfo.flags = 0; + commandBufferBeginInfo.pInheritanceInfo = nullptr; + + VK_CHECK(vkBeginCommandBuffer(m_CommandBuffers[frameIndex], &commandBufferBeginInfo)); + return m_CommandBuffers[frameIndex]; +} + +void VulkanAPI::EndGraphicsCommands(uint32_t frameIndex) { + VK_CHECK(vkEndCommandBuffer(m_CommandBuffers[frameIndex])); +} + + + void lvk::VulkanAPI::RecordGraphicsCommands(std::function graphicsCommandsCallback) { for (uint32_t i = 0; i < m_CommandBuffers.size(); i++)