Cleaned up VulkanRenderer
This commit is contained in:
@@ -32,7 +32,7 @@
|
||||
|
||||
#include <array>
|
||||
|
||||
bool NKUseValidationLayer = true;
|
||||
bool NKUseValidationLayer = false;
|
||||
|
||||
using namespace Nuake;
|
||||
|
||||
@@ -91,11 +91,6 @@ void VkRenderer::Initialize()
|
||||
|
||||
InitSync();
|
||||
|
||||
ShaderCompiler& shaderCompiler = ShaderCompiler::Get();
|
||||
TriangleVertShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/triangle.vert");
|
||||
BackgroundShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/background.comp");
|
||||
TriangleFragShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/triangle.frag");
|
||||
|
||||
std::vector<Vertex> rect_vertices;
|
||||
rect_vertices.resize(4);
|
||||
|
||||
@@ -128,14 +123,8 @@ void VkRenderer::Initialize()
|
||||
camData.Projection = Matrix4(1.0f);
|
||||
|
||||
// init camera buffer
|
||||
CameraBuffer = resources.CreateBuffer(sizeof(CameraData), BufferUsage::STORAGE_BUFFER | BufferUsage::TRANSFER_DST, MemoryUsage::GPU_ONLY, "CameraBuffer");
|
||||
UploadCameraData(camData);
|
||||
|
||||
InitDescriptors();
|
||||
|
||||
//InitPipeline();
|
||||
//InitTrianglePipeline();
|
||||
|
||||
InitImgui();
|
||||
|
||||
SceneRenderer = CreateRef<VkSceneRenderer>();
|
||||
@@ -179,7 +168,6 @@ void VkRenderer::CleanUp()
|
||||
vkDestroyInstance(Instance, nullptr);
|
||||
}
|
||||
|
||||
|
||||
void VkRenderer::GetInstance()
|
||||
{
|
||||
vkb::InstanceBuilder builder;
|
||||
@@ -234,7 +222,6 @@ void VkRenderer::RecreateSwapchain()
|
||||
CreateSwapchain(Window::Get()->GetSize());
|
||||
UpdateDescriptorSets();
|
||||
|
||||
// Update sceneRenderer
|
||||
SceneRenderer->SetGBufferSize(Window::Get()->GetSize());
|
||||
}
|
||||
|
||||
@@ -355,23 +342,7 @@ void VkRenderer::InitDescriptors()
|
||||
DrawImageDescriptorLayout = builder.Build(Device, VK_SHADER_STAGE_COMPUTE_BIT);
|
||||
}
|
||||
|
||||
// Camera buffer
|
||||
{
|
||||
DescriptorLayoutBuilder builder;
|
||||
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
||||
CameraBufferDescriptorLayout = builder.Build(Device, VK_SHADER_STAGE_ALL_GRAPHICS);
|
||||
}
|
||||
|
||||
// Triangle vertex buffer layout
|
||||
{
|
||||
DescriptorLayoutBuilder builder;
|
||||
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
|
||||
TriangleBufferDescriptorLayout = builder.Build(Device, VK_SHADER_STAGE_ALL);
|
||||
}
|
||||
|
||||
DrawImageDescriptors = GlobalDescriptorAllocator.Allocate(Device, DrawImageDescriptorLayout);
|
||||
TriangleBufferDescriptors = GlobalDescriptorAllocator.Allocate(Device, TriangleBufferDescriptorLayout);
|
||||
CameraBufferDescriptors = GlobalDescriptorAllocator.Allocate(Device, CameraBufferDescriptorLayout);
|
||||
|
||||
UpdateDescriptorSets();
|
||||
|
||||
@@ -409,78 +380,6 @@ void VkRenderer::UpdateDescriptorSets()
|
||||
vkUpdateDescriptorSets(Device, 1, &drawImageWrite, 0, nullptr);
|
||||
}
|
||||
|
||||
void VkRenderer::InitPipeline()
|
||||
{
|
||||
InitBackgroundPipeline();
|
||||
}
|
||||
|
||||
void VkRenderer::InitBackgroundPipeline()
|
||||
{
|
||||
VkPipelineLayoutCreateInfo computeLayout{};
|
||||
computeLayout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
computeLayout.pNext = nullptr;
|
||||
computeLayout.pSetLayouts = &DrawImageDescriptorLayout;
|
||||
computeLayout.setLayoutCount = 1;
|
||||
|
||||
VK_CALL(vkCreatePipelineLayout(Device, &computeLayout, nullptr, &PipelineLayout));
|
||||
|
||||
VkPipelineShaderStageCreateInfo stageinfo{};
|
||||
stageinfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
stageinfo.pNext = nullptr;
|
||||
stageinfo.stage = VK_SHADER_STAGE_COMPUTE_BIT;
|
||||
stageinfo.module = BackgroundShader->GetModule();
|
||||
stageinfo.pName = "main";
|
||||
|
||||
VkComputePipelineCreateInfo computePipelineCreateInfo{};
|
||||
computePipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
|
||||
computePipelineCreateInfo.pNext = nullptr;
|
||||
computePipelineCreateInfo.layout = PipelineLayout;
|
||||
computePipelineCreateInfo.stage = stageinfo;
|
||||
|
||||
VK_CALL(vkCreateComputePipelines(Device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &Pipeline));
|
||||
|
||||
MainDeletionQueue.push_function([&]() {
|
||||
vkDestroyPipelineLayout(Device, PipelineLayout, nullptr);
|
||||
vkDestroyPipeline(Device, Pipeline, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
void VkRenderer::InitTrianglePipeline()
|
||||
{
|
||||
VkPushConstantRange bufferRange{};
|
||||
bufferRange.offset = 0;
|
||||
bufferRange.size = sizeof(GPUDrawPushConstants);
|
||||
bufferRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
|
||||
VkDescriptorSetLayout layouts[] = { CameraBufferDescriptorLayout, TriangleBufferDescriptorLayout };
|
||||
|
||||
VkPipelineLayoutCreateInfo pipeline_layout_info = VulkanInit::PipelineLayoutCreateInfo();
|
||||
pipeline_layout_info.pPushConstantRanges = &bufferRange;
|
||||
pipeline_layout_info.pushConstantRangeCount = 0;
|
||||
pipeline_layout_info.pSetLayouts = layouts;
|
||||
pipeline_layout_info.setLayoutCount = 2;
|
||||
VK_CALL(vkCreatePipelineLayout(Device, &pipeline_layout_info, nullptr, &TrianglePipelineLayout));
|
||||
|
||||
//use the triangle layout we created
|
||||
PipelineBuilder pipelineBuilder;
|
||||
pipelineBuilder.PipelineLayout = TrianglePipelineLayout;
|
||||
pipelineBuilder.SetShaders(TriangleVertShader->GetModule(), TriangleFragShader->GetModule());
|
||||
pipelineBuilder.SetInputTopology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
|
||||
pipelineBuilder.SetPolygonMode(VK_POLYGON_MODE_FILL);
|
||||
pipelineBuilder.SetCullMode(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
|
||||
pipelineBuilder.SetMultiSamplingNone();
|
||||
pipelineBuilder.DisableBlending();
|
||||
pipelineBuilder.DisableDepthTest();
|
||||
pipelineBuilder.SetColorAttachment(static_cast<VkFormat>(DrawImage->GetFormat()));
|
||||
pipelineBuilder.SetDepthFormat(VK_FORMAT_UNDEFINED);
|
||||
TrianglePipeline = pipelineBuilder.BuildPipeline(Device);
|
||||
|
||||
MainDeletionQueue.push_function([&]() {
|
||||
vkDestroyPipelineLayout(Device, TrianglePipelineLayout, nullptr);
|
||||
vkDestroyPipeline(Device, TrianglePipeline, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
void VkRenderer::DrawScene(RenderContext ctx)
|
||||
{
|
||||
SceneRenderer->BeginScene(ctx);
|
||||
@@ -759,21 +658,6 @@ void VkRenderer::EndDraw()
|
||||
FrameNumber++;
|
||||
}
|
||||
|
||||
void VkRenderer::DrawBackground(VkCommandBuffer cmd)
|
||||
{
|
||||
// This works
|
||||
VkClearColorValue clearValue;
|
||||
//float flash = std::abs(std::sin(FrameNumber / 120.f));
|
||||
clearValue = { { 0.0f, 0.0f, 0.0f, 1.0f } };
|
||||
VkImageSubresourceRange clearRange = VulkanInit::ImageSubResourceRange(VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
vkCmdClearColorImage(cmd, DrawImage->GetImage(), VK_IMAGE_LAYOUT_GENERAL, &clearValue, 1, &clearRange);
|
||||
|
||||
// This doesnt!!
|
||||
//vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, Pipeline);
|
||||
//vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_COMPUTE, PipelineLayout, 0, 1, &DrawImageDescriptors, 0, nullptr);
|
||||
//vkCmdDispatch(cmd, std::ceil(DrawExtent.width / 16.0), std::ceil(DrawExtent.height / 16.0), 1);
|
||||
}
|
||||
|
||||
void VkRenderer::DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView)
|
||||
{
|
||||
VkRenderingAttachmentInfo colorAttachment = VulkanInit::AttachmentInfo(targetImageView, nullptr, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
@@ -812,33 +696,6 @@ void VkRenderer::ImmediateSubmit(std::function<void(VkCommandBuffer cmd)>&& func
|
||||
VK_CALL(vkWaitForFences(Device, 1, &ImguiFence, true, 9999999999));
|
||||
}
|
||||
|
||||
void VkRenderer::UploadCameraData(const CameraData& data)
|
||||
{
|
||||
CameraData adjustedData = data;
|
||||
adjustedData.View = Matrix4(1.0f); //data.View;
|
||||
adjustedData.View = data.View;
|
||||
adjustedData.Projection = glm::perspective(glm::radians(70.f), (float)DrawExtent.width / (float)DrawExtent.height, 0.0001f, 10000.0f);
|
||||
adjustedData.Position = data.View[3];
|
||||
//adjustedData.Projection[1][1] *= -1;
|
||||
|
||||
void* mappedData;
|
||||
vmaMapMemory(VulkanAllocator::Get().GetAllocator(), (GetCurrentFrame().CameraStagingBuffer->GetAllocation()), &mappedData);
|
||||
memcpy(mappedData, &adjustedData, sizeof(CameraData));
|
||||
|
||||
ImmediateSubmit([&](VkCommandBuffer cmd) {
|
||||
VkBufferCopy copy{ 0 };
|
||||
copy.dstOffset = 0;
|
||||
copy.srcOffset = 0;
|
||||
copy.size = sizeof(CameraData);
|
||||
|
||||
vkCmdCopyBuffer(cmd, GetCurrentFrame().CameraStagingBuffer->GetBuffer(), CameraBuffer->GetBuffer(), 1, ©);
|
||||
});
|
||||
|
||||
vmaUnmapMemory(VulkanAllocator::Get().GetAllocator(), GetCurrentFrame().CameraStagingBuffer->GetAllocation());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DescriptorAllocator::InitPool(VkDevice device, uint32_t maxSets, std::span<PoolSizeRatio> poolRatios)
|
||||
{
|
||||
std::vector<VkDescriptorPoolSize> poolSizes;
|
||||
|
||||
@@ -90,6 +90,7 @@ namespace Nuake
|
||||
Ref<AllocatedBuffer> MaterialStagingBuffer; // Materials
|
||||
Ref<AllocatedBuffer> LightStagingBuffer; // Lights
|
||||
Ref<AllocatedBuffer> CamerasStagingBuffer; // Draw image
|
||||
|
||||
// Semaphore are for GPU -> GPU sync
|
||||
// Fence are for CPU -> GPU
|
||||
// Two, one for window, other for rendering
|
||||
@@ -160,50 +161,20 @@ namespace Nuake
|
||||
uint32_t GPUQueueFamily;
|
||||
|
||||
DeletionQueue MainDeletionQueue;
|
||||
|
||||
// Descriptors
|
||||
DescriptorAllocator GlobalDescriptorAllocator;
|
||||
|
||||
public:
|
||||
VkDescriptorSet DrawImageDescriptors;
|
||||
VkDescriptorSetLayout DrawImageDescriptorLayout;
|
||||
|
||||
VkDescriptorSet TriangleBufferDescriptors;
|
||||
VkDescriptorSetLayout TriangleBufferDescriptorLayout;
|
||||
|
||||
VkDescriptorSet CameraBufferDescriptors;
|
||||
VkDescriptorSetLayout CameraBufferDescriptorLayout;
|
||||
|
||||
// Pipeline
|
||||
VkPipeline Pipeline;
|
||||
VkPipelineLayout PipelineLayout;
|
||||
|
||||
VkPipelineLayout TrianglePipelineLayout;
|
||||
VkPipeline TrianglePipeline;
|
||||
|
||||
Ref<VulkanShader> BackgroundShader;
|
||||
Ref<VulkanShader> TriangleVertShader;
|
||||
Ref<VulkanShader> TriangleFragShader;
|
||||
|
||||
// Imgui
|
||||
// Imgui stuff
|
||||
VkFence ImguiFence;
|
||||
VkCommandBuffer ImguiCommandBuffer;
|
||||
VkCommandPool ImguiCommandPool;
|
||||
|
||||
// Buffers
|
||||
|
||||
Ref<AllocatedBuffer> CameraBuffer;
|
||||
Ref<VkSceneRenderer> SceneRenderer;
|
||||
|
||||
public:
|
||||
Ref<VkMesh> Rect;
|
||||
VkQueue GPUQueue;
|
||||
|
||||
Ref<VulkanImage> DrawImage;
|
||||
Ref<VulkanImage> DepthImage;
|
||||
VkExtent2D DrawExtent;
|
||||
FrameData& GetCurrentFrame() { return Frames[FrameNumber % FRAME_OVERLAP]; };
|
||||
|
||||
static VkRenderer& Get()
|
||||
{
|
||||
static VkRenderer instance;
|
||||
@@ -213,10 +184,13 @@ namespace Nuake
|
||||
VkRenderer() = default;
|
||||
~VkRenderer();
|
||||
|
||||
VkDevice GetDevice() const
|
||||
{
|
||||
return Device;
|
||||
}
|
||||
public:
|
||||
Ref<VkMesh> Rect;
|
||||
VkQueue GPUQueue;
|
||||
|
||||
Ref<VulkanImage> DrawImage;
|
||||
Ref<VulkanImage> DepthImage;
|
||||
VkExtent2D DrawExtent;
|
||||
|
||||
public:
|
||||
void Initialize();
|
||||
@@ -233,18 +207,24 @@ namespace Nuake
|
||||
void InitSync();
|
||||
void InitDescriptors();
|
||||
void UpdateDescriptorSets();
|
||||
void InitPipeline();
|
||||
void InitBackgroundPipeline();
|
||||
void InitTrianglePipeline();
|
||||
void DrawScene(RenderContext ctx);
|
||||
void InitImgui();
|
||||
|
||||
void BeginScene(const UUID& camera);
|
||||
|
||||
bool Draw();
|
||||
|
||||
void EndDraw();
|
||||
|
||||
public:
|
||||
VkDevice GetDevice() const
|
||||
{
|
||||
return Device;
|
||||
}
|
||||
|
||||
FrameData& GetCurrentFrame()
|
||||
{
|
||||
return Frames[FrameNumber % FRAME_OVERLAP];
|
||||
};
|
||||
|
||||
DescriptorAllocator& GetDescriptorAllocator()
|
||||
{
|
||||
return GlobalDescriptorAllocator;
|
||||
@@ -255,15 +235,10 @@ namespace Nuake
|
||||
return Cmd(GetCurrentFrame().CommandBuffer);
|
||||
}
|
||||
|
||||
void DrawBackground(VkCommandBuffer cmd);
|
||||
void DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView);
|
||||
|
||||
void ImmediateSubmit(std::function<void(VkCommandBuffer cmd)>&& function);
|
||||
|
||||
void UploadCameraData(const CameraData& data);
|
||||
//auto& GetRenderPipeline() { return this->SceneRenderer->GetRenderPipeline(); }
|
||||
VkDescriptorSet GetViewportDescriptor() const { return DrawImageDescriptors; }
|
||||
Ref<VulkanImage> GetDrawImage() const { return DrawImage; }
|
||||
|
||||
void DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView);
|
||||
void ImmediateSubmit(std::function<void(VkCommandBuffer cmd)>&& function);
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user