Now using vertex pulling with mesh buffers

This commit is contained in:
antopilo
2024-12-08 00:30:33 -05:00
parent 29130dde7f
commit dfc5b6fa69
7 changed files with 174 additions and 64 deletions

View File

@@ -0,0 +1,14 @@
#pragma once
#include "src/Core/Maths.h"
namespace Nuake
{
struct VkVertex
{
Vector3 position;
float uv_x;
Vector3 normal;
float uv_y;
Vector4 color;
};
}

View File

@@ -4,10 +4,9 @@
#include "VulkanCheck.h"
#include "VulkanRenderer.h"
#include "vk_mem_alloc.h"
using namespace Nuake;
#include "vk_mem_alloc.h"
AllocatedBuffer::AllocatedBuffer(size_t inSize, VkBufferUsageFlags inFlags, VmaMemoryUsage inUsage)
{
@@ -31,3 +30,25 @@ AllocatedBuffer::~AllocatedBuffer()
{
// TODO: deletion of buffer
}
GPUMeshBuffers::GPUMeshBuffers(std::vector<VkVertex> vertices, std::vector<uint32_t> indices)
{
const size_t vertexBufferSize = vertices.size() * sizeof(VkVertex);
const size_t indexBufferSize = indices.size() * sizeof(uint32_t);
vertexBuffer = AllocatedBuffer(vertexBufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
VMA_MEMORY_USAGE_GPU_ONLY);
//find the adress of the vertex buffer
VkBufferDeviceAddressInfo deviceAddressInfo
{
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = vertexBuffer.GetBuffer()
};
vertexBufferAddress = vkGetBufferDeviceAddress(VkRenderer::Get().GetDevice(), &deviceAddressInfo);
//create index buffer
indexBuffer = AllocatedBuffer(indexBufferSize, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VMA_MEMORY_USAGE_GPU_ONLY);
}

View File

@@ -1,10 +1,13 @@
#pragma once
#include "src/Core/Maths.h"
#include "src/Rendering/Vulkan/VkVertex.h"
#include <volk/volk.h>
#include <vk_mem_alloc.h>
#include <vector>
#include "volk/volk.h"
#include "vk_mem_alloc.h"
namespace Nuake
{
@@ -17,15 +20,13 @@ namespace Nuake
public:
AllocatedBuffer(size_t size, VkBufferUsageFlags flags, VmaMemoryUsage usage);
AllocatedBuffer() = default;
~AllocatedBuffer();
VkBuffer GetBuffer() const { return Buffer; }
VmaAllocation GetAllocation() const { return Allocation; }
};
struct GPUMeshBuffers
{
AllocatedBuffer indexBuffer;
AllocatedBuffer vertexBuffer;
VkDeviceAddress vertexBufferAddress;
};
// push constants for our mesh object draws
struct GPUDrawPushConstants
@@ -33,4 +34,18 @@ namespace Nuake
glm::mat4 worldMatrix;
VkDeviceAddress vertexBuffer;
};
// holds the resources needed for a mesh
class GPUMeshBuffers
{
public:
AllocatedBuffer indexBuffer;
AllocatedBuffer vertexBuffer;
VkDeviceAddress vertexBufferAddress;
GPUMeshBuffers(std::vector<VkVertex> vertices, std::vector<uint32_t> indices);
GPUMeshBuffers() = default;
~GPUMeshBuffers() = default;
};
}

View File

@@ -12,15 +12,16 @@
#include "imgui/imgui_impl_vulkan.h"
#include <imgui/imgui_impl_glfw.h>
#include "src/Resource/StaticResources.h"
#include "vk_mem_alloc.h"
#include "VulkanInit.h"
#include "VulkanAllocator.h"
#include "VulkanCheck.h"
#include "PipelineBuilder.h"
#include "VulkanAllocatedBuffer.h"
using namespace Nuake;
#include "vk_mem_alloc.h"
bool NKUseValidationLayer = true;
@@ -76,6 +77,33 @@ void VkRenderer::Initialize()
BackgroundShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/background.comp");
TriangleFragShader = shaderCompiler.CompileShader("../Resources/Shaders/Vulkan/triangle.frag");
std::vector<VkVertex> rect_vertices;
rect_vertices.resize(4);
rect_vertices[0].position = { 0.5,-0.5, 0 };
rect_vertices[1].position = { 0.5,0.5, 0 };
rect_vertices[2].position = { -0.5,-0.5, 0 };
rect_vertices[3].position = { -0.5,0.5, 0 };
rect_vertices[0].color = { 0,0, 0,1 };
rect_vertices[1].color = { 0.5,0.5,0.5 ,1 };
rect_vertices[2].color = { 1,0, 0,1 };
rect_vertices[3].color = { 0,1, 0,1 };
std::vector<uint32_t> rect_indices;
rect_indices.resize(6);
rect_indices[0] = 0;
rect_indices[1] = 1;
rect_indices[2] = 2;
rect_indices[3] = 2;
rect_indices[4] = 1;
rect_indices[5] = 3;
rectangle = UploadMesh(rect_indices, rect_vertices);
InitPipeline();
InitTrianglePipeline();
@@ -351,7 +379,15 @@ void VkRenderer::InitBackgroundPipeline()
void VkRenderer::InitTrianglePipeline()
{
VkPushConstantRange bufferRange{};
bufferRange.offset = 0;
bufferRange.size = sizeof(GPUDrawPushConstants);
bufferRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
VkPipelineLayoutCreateInfo pipeline_layout_info = VulkanInit::PipelineLayoutCreateInfo();
pipeline_layout_info.pPushConstantRanges = &bufferRange;
pipeline_layout_info.pushConstantRangeCount = 1;
VK_CALL(vkCreatePipelineLayout(Device, &pipeline_layout_info, nullptr, &TrianglePipelineLayout));
PipelineBuilder pipelineBuilder;
@@ -739,6 +775,41 @@ void VkRenderer::ImmediateSubmit(std::function<void(VkCommandBuffer cmd)>&& func
VK_CALL(vkWaitForFences(Device, 1, &ImguiFence, true, 9999999999));
}
Ref<GPUMeshBuffers> VkRenderer::UploadMesh(std::vector<uint32_t> indices, std::vector<VkVertex> vertices)
{
auto newSurface = CreateRef<GPUMeshBuffers>(vertices, indices);
AllocatedBuffer staging = AllocatedBuffer(std::size(vertices) + std::size(indices), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
void* mappedData;
vmaMapMemory(VulkanAllocator::Get().GetAllocator(), staging.GetAllocation(), &mappedData);
// copy vertex buffer
memcpy(mappedData, vertices.data(), std::size(vertices));
// copy index buffer
memcpy((char*)mappedData + std::size(vertices), indices.data(), std::size(indices));
ImmediateSubmit([&](VkCommandBuffer cmd) {
VkBufferCopy vertexCopy{ 0 };
vertexCopy.dstOffset = 0;
vertexCopy.srcOffset = 0;
vertexCopy.size = std::size(vertices);
vkCmdCopyBuffer(cmd, staging.GetBuffer(), newSurface->vertexBuffer.GetBuffer(), 1, &vertexCopy);
VkBufferCopy indexCopy{ 0 };
indexCopy.dstOffset = 0;
indexCopy.srcOffset = std::size(vertices);
indexCopy.size = std::size(indices);
vkCmdCopyBuffer(cmd, staging.GetBuffer(), newSurface->indexBuffer.GetBuffer(), 1, &indexCopy);
});
// TODO: destroy_buffer(staging);
return newSurface;
}
void DescriptorLayoutBuilder::AddBinding(uint32_t binding, VkDescriptorType type)
{
VkDescriptorSetLayoutBinding newbind{};

View File

@@ -6,15 +6,21 @@
#include "vkb/VkBootstrap.h"
#include "VulkanImage/VulkanImage.h"
#include "VulkanTypes.h"
#include "VkVertex.h"
#include <functional>
#include <span>
#include "vk_mem_alloc.h"
namespace Nuake
{
class VulkanShader;
class GPUMeshBuffers;
struct VmaAllocation {};
struct AllocatedImage {
VkImage image;
VkImageView imageView;
@@ -149,8 +155,6 @@ namespace Nuake
DeletionQueue MainDeletionQueue;
// Descriptors
DescriptorAllocator GlobalDescriptorAllocator;
@@ -167,11 +171,15 @@ namespace Nuake
Ref<VulkanShader> BackgroundShader;
Ref<VulkanShader> TriangleVertShader;
Ref<VulkanShader> TriangleFragShader;
// Imgui
VkFence ImguiFence;
VkCommandBuffer ImguiCommandBuffer;
VkCommandPool ImguiCommandPool;
// Buffers
Ref<GPUMeshBuffers> rectangle;
public:
static VkRenderer& Get()
{
@@ -214,6 +222,8 @@ namespace Nuake
void ImmediateSubmit(std::function<void(VkCommandBuffer cmd)>&& function);
Ref<GPUMeshBuffers> UploadMesh(std::vector<uint32_t> indices, std::vector<VkVertex> vertices);
VkDescriptorSet GetViewportDescriptor() const { return DrawImageDescriptors; }
Ref<VulkanImage> GetDrawImage() const { return DrawImage; }
};

View File

@@ -1,32 +1,2 @@
#pragma once
#include <src/Core/Maths.h>
#include "src/Rendering/Vulkan/VulkanAllocatedBuffer.h"
namespace Nuake
{
struct Vertex
{
Vector3 position;
float uv_x;
Vector3 normal;
float uv_y;
Vector4 color;
};
// holds the resources needed for a mesh
struct GPUMeshBuffers
{
AllocatedBuffer indexBuffer;
AllocatedBuffer vertexBuffer;
VkDeviceAddress vertexBufferAddress;
};
// push constants for our mesh object draws
struct GPUDrawPushConstants
{
Matrix4 worldMatrix;
VkDeviceAddress vertexBuffer;
};
}

View File

@@ -1,30 +1,39 @@
// HLSL version for Shader Model 6.1
struct Vertex
{
float3 position;
float uv_x;
float3 normal;
float uv_y;
float4 color;
};
// Define the structured buffer for vertices
StructuredBuffer<Vertex> vertexBuffer : register(t0); // Binding of vertex buffer (example: t0)
// Define push constants block
cbuffer PushConstants : register(b0) { // Push constants binding (example: b0)
float4x4 render_matrix; // Matrix for rendering
uint64_t vertexBufferAddress; // Buffer reference address (Vulkan-specific handling required)
};
// Outputs
struct VSOutput {
float4 Position : SV_Position;
float3 Color : TEXCOORD0;
float2 UV : TEXCOORD1;
};
VSOutput main(uint vertexIndex : SV_VertexID)
{
// Main vertex shader
VSOutput main(uint vertexIndex : SV_VertexID) {
VSOutput output;
// Constant array of positions for the triangle
float3 positions[3] = {
float3(1.0f, 1.0f, 0.0f),
float3(-1.0f, 1.0f, 0.0f),
float3(0.0f, -1.0f, 0.0f)
};
// Load vertex data from the buffer
Vertex v = vertexBuffer[vertexIndex];
// Constant array of colors for the triangle
float3 colors[3] = {
float3(1.0f, 0.0f, 0.0f), // red
float3(0.0f, 1.0f, 0.0f), // green
float3(0.0f, 0.0f, 1.0f) // blue
};
// Output the position of each vertex
output.Position = float4(positions[vertexIndex], 1.0f);
output.Color = colors[vertexIndex];
// Transform and output vertex data
output.Position = mul(render_matrix, float4(v.position, 1.0f));
output.Color = v.color.xyz;
output.UV = float2(v.uv_x, v.uv_y);
return output;
}