mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-09 20:08:57 +03:00
Continued abstracting command buffer
This commit is contained in:
139
Nuake/src/Rendering/Vulkan/Cmd.cpp
Normal file
139
Nuake/src/Rendering/Vulkan/Cmd.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#include "Cmd.h"
|
||||
using namespace Nuake;
|
||||
|
||||
void Cmd::BeginRendering(VkRenderingInfo renderInfo)
|
||||
{
|
||||
vkCmdBeginRendering(CmdBuffer, &renderInfo);
|
||||
}
|
||||
|
||||
void Cmd::EndRendering()
|
||||
{
|
||||
vkCmdEndRendering(CmdBuffer);
|
||||
}
|
||||
|
||||
void Cmd::BindPipeline(VkPipeline pipeline) const
|
||||
{
|
||||
vkCmdBindPipeline(CmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||
}
|
||||
|
||||
void Cmd::SetViewport(const Vector2 & size)
|
||||
{
|
||||
VkViewport viewport = {};
|
||||
viewport.x = 0;
|
||||
viewport.y = 0;
|
||||
viewport.width = size.x;
|
||||
viewport.height = size.y;
|
||||
viewport.minDepth = 0.f;
|
||||
viewport.maxDepth = 1.f;
|
||||
|
||||
vkCmdSetViewport(CmdBuffer, 0, 1, &viewport);
|
||||
}
|
||||
|
||||
void Cmd::SetScissor(const Vector2 & size)
|
||||
{
|
||||
VkRect2D scissor = {};
|
||||
scissor.offset.x = 0;
|
||||
scissor.offset.y = 0;
|
||||
scissor.extent.width = size.x;
|
||||
scissor.extent.height = size.y;
|
||||
vkCmdSetScissor(CmdBuffer, 0, 1, &scissor);
|
||||
}
|
||||
|
||||
void Cmd::ClearColorImage(Ref<VulkanImage> img, Color color) const
|
||||
{
|
||||
VkClearColorValue clearValue = { {color.r, color.g, color.b, color.a } };
|
||||
VkImageSubresourceRange clearRange{};
|
||||
clearRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
clearRange.baseMipLevel = 0;
|
||||
clearRange.levelCount = VK_REMAINING_MIP_LEVELS;
|
||||
clearRange.baseArrayLayer = 0;
|
||||
clearRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
||||
vkCmdClearColorImage(CmdBuffer, img->GetImage(), VK_IMAGE_LAYOUT_GENERAL, &clearValue, 1, &clearRange);
|
||||
}
|
||||
|
||||
void Cmd::BindDescriptorSet(VkPipelineLayout pipeline, VkDescriptorSet descriptor, uint32_t set) const
|
||||
{
|
||||
vkCmdBindDescriptorSets(
|
||||
CmdBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
pipeline,
|
||||
0, // firstSet
|
||||
set, // descriptorSetCount
|
||||
&descriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
}
|
||||
|
||||
void Cmd::BindIndexBuffer(VkBuffer buffer) const
|
||||
{
|
||||
vkCmdBindIndexBuffer(CmdBuffer, buffer, 0, VK_INDEX_TYPE_UINT32);
|
||||
}
|
||||
|
||||
void Cmd::DrawIndexed(uint32_t count) const
|
||||
{
|
||||
vkCmdDrawIndexed(CmdBuffer, count, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
void Cmd::PushConstants(VkPipelineLayout pipeline, size_t size, void* data) const
|
||||
{
|
||||
vkCmdPushConstants(
|
||||
CmdBuffer,
|
||||
pipeline,
|
||||
VK_SHADER_STAGE_ALL_GRAPHICS,
|
||||
0, // Offset
|
||||
size, // Size of the push constant
|
||||
data // Pointer to the value
|
||||
);
|
||||
}
|
||||
|
||||
void Cmd::CopyBuffer(VkBuffer src, VkBuffer dst, size_t size) const
|
||||
{
|
||||
VkBufferCopy copy{ 0 };
|
||||
copy.dstOffset = 0;
|
||||
copy.srcOffset = 0;
|
||||
copy.size = size;
|
||||
|
||||
vkCmdCopyBuffer(CmdBuffer, src, dst, 1, ©);
|
||||
}
|
||||
|
||||
void Cmd::TransitionImageLayout(Ref<VulkanImage> img, VkImageLayout layout)
|
||||
{
|
||||
img->TransitionLayout(CmdBuffer, layout);
|
||||
}
|
||||
|
||||
void Cmd::CopyImageToImage(Ref<VulkanImage> src, Ref<VulkanImage> dst) const
|
||||
{
|
||||
VkImageBlit2 blitRegion{ .sType = VK_STRUCTURE_TYPE_IMAGE_BLIT_2, .pNext = nullptr };
|
||||
|
||||
blitRegion.srcOffsets[1].x = src->GetSize().x;
|
||||
blitRegion.srcOffsets[1].y = src->GetSize().y;
|
||||
blitRegion.srcOffsets[1].z = 1;
|
||||
|
||||
blitRegion.dstOffsets[1].x = dst->GetSize().x;
|
||||
blitRegion.dstOffsets[1].y = dst->GetSize().y;
|
||||
blitRegion.dstOffsets[1].z = 1;
|
||||
|
||||
blitRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
blitRegion.srcSubresource.baseArrayLayer = 0;
|
||||
blitRegion.srcSubresource.layerCount = 1;
|
||||
blitRegion.srcSubresource.mipLevel = 0;
|
||||
|
||||
blitRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
blitRegion.dstSubresource.baseArrayLayer = 0;
|
||||
blitRegion.dstSubresource.layerCount = 1;
|
||||
blitRegion.dstSubresource.mipLevel = 0;
|
||||
|
||||
VkBlitImageInfo2 blitInfo{ .sType = VK_STRUCTURE_TYPE_BLIT_IMAGE_INFO_2, .pNext = nullptr };
|
||||
blitInfo.dstImage = dst->GetImage();
|
||||
blitInfo.dstImageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
blitInfo.srcImage = src->GetImage();
|
||||
blitInfo.srcImageLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
|
||||
blitInfo.filter = VK_FILTER_LINEAR;
|
||||
blitInfo.regionCount = 1;
|
||||
blitInfo.pRegions = &blitRegion;
|
||||
|
||||
vkCmdBlitImage2(CmdBuffer, &blitInfo);
|
||||
}
|
||||
|
||||
|
||||
37
Nuake/src/Rendering/Vulkan/Cmd.h
Normal file
37
Nuake/src/Rendering/Vulkan/Cmd.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <volk/volk.h>
|
||||
|
||||
#include "src/Core/Core.h"
|
||||
#include "src/Rendering/Vulkan/VulkanImage/VulkanImage.h"
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
class Cmd
|
||||
{
|
||||
private:
|
||||
VkCommandBuffer CmdBuffer;
|
||||
|
||||
public:
|
||||
Cmd() = default;
|
||||
~Cmd() = default;
|
||||
|
||||
Cmd(VkCommandBuffer cmdBuffer) : CmdBuffer(cmdBuffer) {}
|
||||
VkCommandBuffer GetCmdBuffer() { return CmdBuffer; }
|
||||
|
||||
public:
|
||||
void BeginRendering(VkRenderingInfo renderInfo);
|
||||
void EndRendering();
|
||||
|
||||
void BindPipeline(VkPipeline pipeline) const;
|
||||
void SetViewport(const Vector2& size);
|
||||
void SetScissor(const Vector2& size);
|
||||
void ClearColorImage(Ref<VulkanImage> img, Color color = Color(0, 0, 0, 1)) const;
|
||||
void BindDescriptorSet(VkPipelineLayout pipeline, VkDescriptorSet descriptor, uint32_t set) const;
|
||||
void BindIndexBuffer(VkBuffer buffer) const;
|
||||
void DrawIndexed(uint32_t count) const;
|
||||
void PushConstants(VkPipelineLayout pipeline, size_t size, void* data ) const;
|
||||
void CopyBuffer(VkBuffer src, VkBuffer dst, size_t size) const;
|
||||
void TransitionImageLayout(Ref<VulkanImage> img, VkImageLayout layout);
|
||||
void CopyImageToImage(Ref<VulkanImage> src, Ref<VulkanImage> dst) const;
|
||||
};
|
||||
}
|
||||
@@ -37,13 +37,11 @@ void RenderPass::Execute(PassRenderContext& ctx, PassAttachments& inputs)
|
||||
void RenderPass::ClearAttachments(PassRenderContext& ctx, PassAttachments& inputs)
|
||||
{
|
||||
// Clear all color attachments
|
||||
VkClearColorValue clearValue = { { 0.0f, 0.0f, 0.0f, 1.0f } };
|
||||
VkImageSubresourceRange clearRange = VulkanInit::ImageSubResourceRange(VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
for (auto& attachment : inputs)
|
||||
{
|
||||
if (attachment->GetUsage() != ImageUsage::Depth)
|
||||
{
|
||||
vkCmdClearColorImage(ctx.commandBuffer, attachment->GetImage(), VK_IMAGE_LAYOUT_GENERAL, &clearValue, 1, &clearRange);
|
||||
ctx.commandBuffer.ClearColorImage(attachment);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,7 +53,7 @@ void RenderPass::TransitionAttachments(PassRenderContext& ctx, PassAttachments&
|
||||
{
|
||||
if (attachment->GetUsage() != ImageUsage::Depth)
|
||||
{
|
||||
attachment->TransitionLayout(ctx.commandBuffer, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
ctx.commandBuffer.TransitionImageLayout(attachment, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,8 +63,8 @@ void RenderPass::UntransitionAttachments(PassRenderContext& ctx, PassAttachments
|
||||
for (auto& attachment : inputs)
|
||||
{
|
||||
// Transform from color attachment to transfer src for next pass
|
||||
attachment->TransitionLayout(ctx.commandBuffer, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||
attachment->TransitionLayout(ctx.commandBuffer, VK_IMAGE_LAYOUT_GENERAL);
|
||||
ctx.commandBuffer.TransitionImageLayout(attachment, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||
ctx.commandBuffer.TransitionImageLayout(attachment, VK_IMAGE_LAYOUT_GENERAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,33 +100,19 @@ void RenderPass::Render(PassRenderContext& ctx, PassAttachments& inputs)
|
||||
renderInfo.colorAttachmentCount = std::size(renderAttachmentInfos);
|
||||
renderInfo.pColorAttachments = renderAttachmentInfos.data();
|
||||
|
||||
vkCmdBeginRendering(ctx.commandBuffer, &renderInfo);
|
||||
auto& cmd = ctx.commandBuffer;
|
||||
cmd.BeginRendering(renderInfo);
|
||||
{
|
||||
vkCmdBindPipeline(ctx.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline);
|
||||
|
||||
VkViewport viewport = {};
|
||||
viewport.x = 0;
|
||||
viewport.y = 0;
|
||||
viewport.width = ctx.resolution.x;
|
||||
viewport.height = ctx.resolution.y;
|
||||
viewport.minDepth = 0.f;
|
||||
viewport.maxDepth = 1.f;
|
||||
|
||||
vkCmdSetViewport(ctx.commandBuffer, 0, 1, &viewport);
|
||||
|
||||
VkRect2D scissor = {};
|
||||
scissor.offset.x = 0;
|
||||
scissor.offset.y = 0;
|
||||
scissor.extent.width = ctx.resolution.x;
|
||||
scissor.extent.height = ctx.resolution.y;
|
||||
vkCmdSetScissor(ctx.commandBuffer, 0, 1, &scissor);
|
||||
cmd.BindPipeline(Pipeline);
|
||||
cmd.SetViewport(ctx.resolution);
|
||||
cmd.SetScissor(ctx.resolution);
|
||||
|
||||
if (RenderCb)
|
||||
{
|
||||
RenderCb(ctx);
|
||||
}
|
||||
}
|
||||
vkCmdEndRendering(ctx.commandBuffer);
|
||||
cmd.EndRendering();
|
||||
|
||||
if (PostRender)
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "src/Core/Maths.h"
|
||||
#include "src/Rendering/Vulkan/VulkanImage/VulkanImage.h"
|
||||
#include "src/Rendering/Vulkan/VulkanShader.h"
|
||||
|
||||
#include "src/Rendering/Vulkan/Cmd.h"
|
||||
#include <any>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
@@ -18,7 +18,7 @@ namespace Nuake
|
||||
struct PassRenderContext
|
||||
{
|
||||
Ref<Scene> scene;
|
||||
VkCommandBuffer commandBuffer;
|
||||
Cmd commandBuffer;
|
||||
Vector2 resolution;
|
||||
RenderPass* renderPass = nullptr;
|
||||
UUID cameraID;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "src/Core/Core.h"
|
||||
#include "src/Resource/UUID.h"
|
||||
#include "src/Rendering/Vulkan/Cmd.h"
|
||||
|
||||
#include <volk/volk.h>
|
||||
|
||||
@@ -11,7 +12,7 @@ namespace Nuake
|
||||
struct RenderContext
|
||||
{
|
||||
Ref<Scene> CurrentScene; // The scene we are rendering
|
||||
VkCommandBuffer CommandBuffer; // The command buffer we are recording into
|
||||
Cmd CommandBuffer; // The command buffer we are recording into
|
||||
Vector2 Size;
|
||||
UUID CameraID;
|
||||
// ...
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "SceneRenderPipeline.h"
|
||||
|
||||
#include "src/Rendering/Vulkan/ShaderManager.h"
|
||||
#include "src/Rendering/Vulkan/VkShaderManager.h"
|
||||
#include "src/Rendering/Vulkan/VkResources.h"
|
||||
#include <Tracy.hpp>
|
||||
|
||||
using namespace Nuake;
|
||||
|
||||
@@ -17,17 +18,33 @@ SceneRenderPipeline::SceneRenderPipeline()
|
||||
gBufferPass.AddAttachment("Normal", ImageFormat::RGBA8);
|
||||
gBufferPass.AddAttachment("Material", ImageFormat::RGBA8);
|
||||
gBufferPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth);
|
||||
gBufferPass.SetPreRender([&](PassRenderContext& ctx) {
|
||||
auto layout = ctx.renderPass->PipelineLayout;
|
||||
auto vk = VkRenderer::Get();
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, vk.CameraBufferDescriptors, 0);
|
||||
//ctx.commandBuffer.BindDescriptorSet(layout, ModelBufferDescriptor, 1);
|
||||
//ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3);
|
||||
//ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4);
|
||||
//ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5);
|
||||
//ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6);
|
||||
//ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7);
|
||||
});
|
||||
gBufferPass.SetRender([&](PassRenderContext& ctx) {
|
||||
|
||||
});
|
||||
//gBufferPass.SetPushConstant<ModelPushConstant>(modelPushConstant);
|
||||
|
||||
auto& shadingPass = GBufferPipeline.AddPass("Shading");
|
||||
shadingPass.SetShaders(shaderMgr.GetShader("shading_vert"), shaderMgr.GetShader("shading_frag"));
|
||||
//shadingPass.SetPushConstant<ShadingPushConstant>(shadingPushConstant);
|
||||
//shadingPass.SetPushConstant<ShadingPushConstant>(ShadingPushConstant()));
|
||||
shadingPass.AddAttachment("Output", ImageFormat::RGBA8);
|
||||
shadingPass.SetDepthTest(false);
|
||||
shadingPass.AddInput("Albedo");
|
||||
shadingPass.AddInput("Normal");
|
||||
shadingPass.AddInput("Depth");
|
||||
shadingPass.AddInput("Material");
|
||||
|
||||
GBufferPipeline.Build();
|
||||
}
|
||||
|
||||
void SceneRenderPipeline::SetCamera(UUID camera)
|
||||
@@ -46,8 +63,9 @@ void SceneRenderPipeline::Render(PassRenderContext& ctx)
|
||||
|
||||
PipelineAttachments pipelineInputs
|
||||
{
|
||||
{ GBufferMaterial, GBufferDepth, GBufferNormal, GBufferMaterial }, // GBuffer
|
||||
{ ShadingOutput } // Shading
|
||||
{ GBufferMaterial, GBufferDepth, GBufferNormal, GBufferMaterial }, // GBuffer
|
||||
{ ShadingOutput } // Shading
|
||||
// ... other passes
|
||||
};
|
||||
|
||||
ctx.cameraID = CurrentCameraID;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ShaderManager.h"
|
||||
#include "VkShaderManager.h"
|
||||
|
||||
using namespace Nuake;
|
||||
|
||||
@@ -490,48 +490,6 @@ void VkRenderer::DrawScene(RenderContext ctx)
|
||||
SceneRenderer->EndScene();
|
||||
}
|
||||
|
||||
|
||||
void VkRenderer::DrawGeometry(VkCommandBuffer cmd)
|
||||
{
|
||||
//begin a render pass connected to our draw image
|
||||
VkRenderingAttachmentInfo colorAttachment = VulkanInit::AttachmentInfo(DrawImage->GetImageView(), nullptr, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
|
||||
|
||||
//VkRenderingInfo renderInfo = VulkanInit::RenderingInfo({ DrawExtent.width, DrawExtent.height }, &colorAttachment, nullptr);
|
||||
//vkCmdBeginRendering(cmd, &renderInfo);
|
||||
|
||||
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, TrianglePipeline);
|
||||
|
||||
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, TrianglePipelineLayout, 0, 2, std::array{ CameraBufferDescriptors, TriangleBufferDescriptors }.data(), 0, nullptr);
|
||||
|
||||
//set dynamic viewport and scissor
|
||||
VkViewport viewport = {};
|
||||
viewport.x = 0;
|
||||
viewport.y = 0;
|
||||
viewport.width = DrawExtent.width;
|
||||
viewport.height = DrawExtent.height;
|
||||
viewport.minDepth = 0.f;
|
||||
viewport.maxDepth = 1.f;
|
||||
|
||||
vkCmdSetViewport(cmd, 0, 1, &viewport);
|
||||
|
||||
VkRect2D scissor = {};
|
||||
scissor.offset.x = 0;
|
||||
scissor.offset.y = 0;
|
||||
scissor.extent.width = DrawExtent.width;
|
||||
scissor.extent.height = DrawExtent.height;
|
||||
|
||||
vkCmdSetScissor(cmd, 0, 1, &scissor);
|
||||
|
||||
vkCmdBindIndexBuffer(cmd, rectangle->GetIndexBuffer()->GetBuffer(), 0, VK_INDEX_TYPE_UINT32);
|
||||
|
||||
vkCmdDrawIndexed(cmd, 6, 1, 0, 0, 0);
|
||||
//launch a draw command to draw 3 vertices
|
||||
//vkCmdDraw(cmd, 3, 1, 0, 0);
|
||||
|
||||
vkCmdEndRendering(cmd);
|
||||
}
|
||||
|
||||
void VkRenderer::InitImgui()
|
||||
{
|
||||
// 1: create descriptor pool for IMGUI
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
#include "VkMesh.h"
|
||||
#include "DescriptorAllocatorGrowable.h"
|
||||
|
||||
#include "src/Rendering/Vulkan/Cmd.h"
|
||||
|
||||
namespace Nuake
|
||||
{
|
||||
class VulkanShader;
|
||||
@@ -165,6 +167,7 @@ namespace Nuake
|
||||
// Descriptors
|
||||
DescriptorAllocator GlobalDescriptorAllocator;
|
||||
|
||||
public:
|
||||
VkDescriptorSet DrawImageDescriptors;
|
||||
VkDescriptorSetLayout DrawImageDescriptorLayout;
|
||||
|
||||
@@ -237,7 +240,6 @@ namespace Nuake
|
||||
void InitBackgroundPipeline();
|
||||
void InitTrianglePipeline();
|
||||
void DrawScene(RenderContext ctx);
|
||||
void DrawGeometry(VkCommandBuffer cmd);
|
||||
void InitImgui();
|
||||
|
||||
void BeginScene(const UUID& camera);
|
||||
@@ -251,9 +253,9 @@ namespace Nuake
|
||||
return GlobalDescriptorAllocator;
|
||||
}
|
||||
|
||||
VkCommandBuffer GetCurrentCmdBuffer()
|
||||
Cmd GetCurrentCmdBuffer()
|
||||
{
|
||||
return GetCurrentFrame().CommandBuffer;
|
||||
return Cmd(GetCurrentFrame().CommandBuffer);
|
||||
}
|
||||
|
||||
void DrawBackground(VkCommandBuffer cmd);
|
||||
|
||||
@@ -18,8 +18,10 @@
|
||||
#include "Pipeline/RenderPipeline.h"
|
||||
|
||||
#include "src/Rendering/Vulkan/DescriptorLayoutBuilder.h"
|
||||
#include <src\Scene\Components\CameraComponent.h>
|
||||
#include "src/Rendering/Vulkan/ShaderManager.h"
|
||||
#include "src/Scene/Components/CameraComponent.h"
|
||||
#include "src/Rendering/Vulkan/VkShaderManager.h"
|
||||
|
||||
|
||||
using namespace Nuake;
|
||||
|
||||
VkSceneRenderer::VkSceneRenderer()
|
||||
@@ -69,17 +71,10 @@ void VkSceneRenderer::BeginScene(RenderContext inContext)
|
||||
Context.CameraID = inContext.CameraID;
|
||||
|
||||
// Collect all global transform of things we will render
|
||||
|
||||
|
||||
auto& cmd = Context.CommandBuffer;
|
||||
auto& scene = Context.CurrentScene;
|
||||
auto& vk = VkRenderer::Get();
|
||||
|
||||
// Ensure the command buffer is valid
|
||||
if (cmd == VK_NULL_HANDLE) {
|
||||
throw std::runtime_error("Invalid command buffer");
|
||||
}
|
||||
|
||||
// Ensure the draw image is valid
|
||||
if (!vk.DrawImage) {
|
||||
throw std::runtime_error("Draw image is not initialized");
|
||||
@@ -189,6 +184,8 @@ void VkSceneRenderer::BeginScene(RenderContext inContext)
|
||||
|
||||
//passCtx.cameraID = GPUResources::Get().GetBindlessCameraID(Context.CameraID);
|
||||
//GBufferPipeline.Execute(passCtx);
|
||||
|
||||
sceneRenderPipeline.Render(passCtx);
|
||||
}
|
||||
ModelPushConstant modelPushConstant{};
|
||||
ShadingPushConstant shadingPushConstant;
|
||||
@@ -203,12 +200,14 @@ void VkSceneRenderer::EndScene()
|
||||
auto& shading = GBufferPipeline.GetRenderPass("Shading").GetAttachment("Output");
|
||||
auto& shadow = ShadowPipeline.GetRenderPass("Shadow").GetDepthAttachment();
|
||||
auto& selectedOutput = shading;
|
||||
selectedOutput.Image->TransitionLayout(cmd, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||
vk.DrawImage->TransitionLayout(cmd, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
||||
shadow.Image->TransitionLayout(cmd, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||
VulkanUtil::CopyImageToImage(cmd, selectedOutput.Image->GetImage(), vk.GetDrawImage()->GetImage(), selectedOutput.Image->GetSize(), vk.DrawImage->GetSize());
|
||||
vk.DrawImage->TransitionLayout(cmd, VK_IMAGE_LAYOUT_GENERAL);
|
||||
selectedOutput.Image->TransitionLayout(cmd, VK_IMAGE_LAYOUT_GENERAL);
|
||||
|
||||
cmd.TransitionImageLayout(selectedOutput.Image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
|
||||
cmd.TransitionImageLayout(vk.DrawImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
||||
cmd.CopyImageToImage(selectedOutput.Image, vk.GetDrawImage());
|
||||
cmd.TransitionImageLayout(vk.DrawImage, VK_IMAGE_LAYOUT_GENERAL);
|
||||
cmd.TransitionImageLayout(selectedOutput.Image, VK_IMAGE_LAYOUT_GENERAL);
|
||||
|
||||
cmd.TransitionImageLayout(shadow.Image, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||
}
|
||||
|
||||
void VkSceneRenderer::CreateBuffers()
|
||||
@@ -379,64 +378,14 @@ void VkSceneRenderer::CreatePipelines()
|
||||
shadowPass.SetShaders(Shaders["shadow_vert"], Shaders["shadow_frag"]);
|
||||
shadowPass.SetPushConstant<ModelPushConstant>(modelPushConstant);
|
||||
shadowPass.SetPreRender([&](PassRenderContext& ctx) {
|
||||
std::vector<VkDescriptorSet> descriptors2 = { CameraBufferDescriptors, ModelBufferDescriptor };
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
0, // firstSet
|
||||
2, // descriptorSetCount
|
||||
descriptors2.data(), // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
// Bind material
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
4, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&MaterialBufferDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
5, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&GPUResources::Get().TextureDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
6, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&LightBufferDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
7, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&GPUResources::Get().CamerasDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(ctx.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx.renderPass->PipelineLayout, 3, 1, &SamplerDescriptor, 0, nullptr);
|
||||
auto layout = ctx.renderPass->PipelineLayout;
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, CameraBufferDescriptors, 0);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, ModelBufferDescriptor, 1);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7);
|
||||
});
|
||||
shadowPass.SetRender([&](PassRenderContext& ctx) {
|
||||
auto& cmd = ctx.commandBuffer;
|
||||
@@ -462,16 +411,7 @@ void VkSceneRenderer::CreatePipelines()
|
||||
Matrix4 globalTransform = transform.GetGlobalTransform();
|
||||
|
||||
auto descSet = vkMesh->GetDescriptorSet();
|
||||
vkCmdBindDescriptorSets(
|
||||
cmd,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
2, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&descSet, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, descSet, 2);
|
||||
|
||||
// Bind texture descriptor set
|
||||
Ref<Material> material = m->GetMaterial();
|
||||
@@ -480,17 +420,10 @@ void VkSceneRenderer::CreatePipelines()
|
||||
modelPushConstant.Index = ModelMatrixMapping[entity.GetID()];
|
||||
modelPushConstant.MaterialIndex = MeshMaterialMapping[vkMesh->GetID()];
|
||||
modelPushConstant.CameraID = ctx.cameraID;
|
||||
vkCmdPushConstants(
|
||||
cmd,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
VK_SHADER_STAGE_ALL_GRAPHICS, // Stage matching the pipeline layout
|
||||
0, // Offset
|
||||
sizeof(ModelPushConstant), // Size of the push constant
|
||||
&modelPushConstant // Pointer to the value
|
||||
);
|
||||
|
||||
vkCmdBindIndexBuffer(cmd, vkMesh->GetIndexBuffer()->GetBuffer(), 0, VK_INDEX_TYPE_UINT32);
|
||||
vkCmdDrawIndexed(cmd, vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t), 1, 0, 0, 0);
|
||||
cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ModelPushConstant), &modelPushConstant);
|
||||
cmd.BindIndexBuffer(vkMesh->GetIndexBuffer()->GetBuffer());
|
||||
cmd.DrawIndexed(vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -506,64 +439,14 @@ void VkSceneRenderer::CreatePipelines()
|
||||
gBufferPass.AddAttachment("Depth", ImageFormat::D32F, ImageUsage::Depth);
|
||||
gBufferPass.SetPushConstant<ModelPushConstant>(modelPushConstant);
|
||||
gBufferPass.SetPreRender([&](PassRenderContext& ctx) {
|
||||
std::vector<VkDescriptorSet> descriptors2 = { CameraBufferDescriptors, ModelBufferDescriptor };
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
0, // firstSet
|
||||
2, // descriptorSetCount
|
||||
descriptors2.data(), // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
// Bind material
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
4, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&MaterialBufferDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
5, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&GPUResources::Get().TextureDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
6, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&LightBufferDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
7, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&GPUResources::Get().CamerasDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(ctx.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx.renderPass->PipelineLayout, 3, 1, &SamplerDescriptor, 0, nullptr);
|
||||
auto layout = ctx.renderPass->PipelineLayout;
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, CameraBufferDescriptors, 0);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, ModelBufferDescriptor, 1);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, SamplerDescriptor, 3);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, MaterialBufferDescriptor, 4);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, LightBufferDescriptor, 6);
|
||||
ctx.commandBuffer.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7);
|
||||
});
|
||||
gBufferPass.SetRender([&](PassRenderContext& ctx){
|
||||
auto& cmd = ctx.commandBuffer;
|
||||
@@ -589,16 +472,7 @@ void VkSceneRenderer::CreatePipelines()
|
||||
Matrix4 globalTransform = transform.GetGlobalTransform();
|
||||
|
||||
auto descSet = vkMesh->GetDescriptorSet();
|
||||
vkCmdBindDescriptorSets(
|
||||
cmd,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
2, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&descSet, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, descSet, 2);
|
||||
|
||||
// Bind texture descriptor set
|
||||
Ref<Material> material = m->GetMaterial();
|
||||
@@ -607,17 +481,10 @@ void VkSceneRenderer::CreatePipelines()
|
||||
modelPushConstant.Index = ModelMatrixMapping[entity.GetID()];
|
||||
modelPushConstant.MaterialIndex = MeshMaterialMapping[vkMesh->GetID()];
|
||||
modelPushConstant.CameraID = ctx.cameraID;
|
||||
vkCmdPushConstants(
|
||||
cmd,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
VK_SHADER_STAGE_ALL_GRAPHICS, // Stage matching the pipeline layout
|
||||
0, // Offset
|
||||
sizeof(ModelPushConstant), // Size of the push constant
|
||||
&modelPushConstant // Pointer to the value
|
||||
);
|
||||
cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ModelPushConstant), &modelPushConstant);
|
||||
|
||||
vkCmdBindIndexBuffer(cmd, vkMesh->GetIndexBuffer()->GetBuffer(), 0, VK_INDEX_TYPE_UINT32);
|
||||
vkCmdDrawIndexed(cmd, vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t), 1, 0, 0, 0);
|
||||
cmd.BindIndexBuffer(vkMesh->GetIndexBuffer()->GetBuffer());
|
||||
cmd.DrawIndexed(vkMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -634,64 +501,15 @@ void VkSceneRenderer::CreatePipelines()
|
||||
shadingPass.AddInput("Depth");
|
||||
shadingPass.AddInput("Material");
|
||||
shadingPass.SetPreRender([&](PassRenderContext& ctx) {
|
||||
std::vector<VkDescriptorSet> descriptors2 = { CameraBufferDescriptors, ModelBufferDescriptor };
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
0, // firstSet
|
||||
2, // descriptorSetCount
|
||||
descriptors2.data(), // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(ctx.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx.renderPass->PipelineLayout, 3, 1, &SamplerDescriptor, 0, nullptr);
|
||||
|
||||
// Bind material
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
4, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&MaterialBufferDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
5, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&GPUResources::Get().TextureDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
6, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&LightBufferDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
7, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&GPUResources::Get().CamerasDescriptor, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
auto layout = ctx.renderPass->PipelineLayout;
|
||||
auto cmd = ctx.commandBuffer;
|
||||
cmd.BindDescriptorSet(layout, CameraBufferDescriptors, 0);
|
||||
cmd.BindDescriptorSet(layout, ModelBufferDescriptor, 1);
|
||||
cmd.BindDescriptorSet(layout, SamplerDescriptor, 3);
|
||||
cmd.BindDescriptorSet(layout, MaterialBufferDescriptor, 4);
|
||||
cmd.BindDescriptorSet(layout, GPUResources::Get().TextureDescriptor, 5);
|
||||
cmd.BindDescriptorSet(layout, LightBufferDescriptor, 6);
|
||||
cmd.BindDescriptorSet(layout, GPUResources::Get().CamerasDescriptor, 7);
|
||||
|
||||
auto& gpu = GPUResources::Get();
|
||||
auto& gbufferPass = GBufferPipeline.GetRenderPass("GBuffer");
|
||||
@@ -702,29 +520,13 @@ void VkSceneRenderer::CreatePipelines()
|
||||
shadingPushConstant.CameraID = ctx.cameraID;
|
||||
});
|
||||
shadingPass.SetRender([](PassRenderContext& ctx) {
|
||||
vkCmdPushConstants(
|
||||
ctx.commandBuffer,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
VK_SHADER_STAGE_ALL_GRAPHICS, // Stage matching the pipeline layout
|
||||
0, // Offset
|
||||
sizeof(ShadingPushConstant), // Size of the push constant
|
||||
&shadingPushConstant // Pointer to the value
|
||||
);
|
||||
|
||||
auto descSet = quadMesh->GetDescriptorSet();
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx.commandBuffer,
|
||||
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx.renderPass->PipelineLayout,
|
||||
2, // firstSet
|
||||
1, // descriptorSetCount
|
||||
&descSet, // pointer to the descriptor set(s)
|
||||
0, // dynamicOffsetCount
|
||||
nullptr // dynamicOffsets
|
||||
);
|
||||
auto& cmd = ctx.commandBuffer;
|
||||
cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(ShadingPushConstant), &shadingPushConstant);
|
||||
|
||||
vkCmdBindIndexBuffer(ctx.commandBuffer, quadMesh->GetIndexBuffer()->GetBuffer(), 0, VK_INDEX_TYPE_UINT32);
|
||||
vkCmdDrawIndexed(ctx.commandBuffer, quadMesh->GetIndexBuffer()->GetSize() / sizeof(uint32_t), 1, 0, 0, 0);
|
||||
// Draw full screen quad
|
||||
cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, quadMesh->GetDescriptorSet(), 2);
|
||||
cmd.BindIndexBuffer(quadMesh->GetIndexBuffer()->GetBuffer());
|
||||
cmd.DrawIndexed(6);
|
||||
});
|
||||
|
||||
GBufferPipeline.Build();
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace Nuake
|
||||
|
||||
class VkSceneRenderer
|
||||
{
|
||||
private:
|
||||
public:
|
||||
RenderContext Context;
|
||||
|
||||
std::map<std::string, Ref<VulkanShader>> Shaders;
|
||||
|
||||
Reference in New Issue
Block a user