Started camera buffer

This commit is contained in:
antopilo
2024-12-21 11:51:30 -05:00
parent 24c41f778e
commit 002795d29d
3 changed files with 71 additions and 23 deletions

View File

@@ -101,6 +101,19 @@ void VkRenderer::Initialize()
rectangle = UploadMesh(rect_indices, rect_vertices);
CameraData camData{};
camData.View = Matrix4(1.0f);
camData.Projection = Matrix4(1.0f);
// init camera buffer
AllocatedBuffer camBuffer = AllocatedBuffer(sizeof(CameraData), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY);
void* mappedData;
vmaMapMemory(VulkanAllocator::Get().GetAllocator(), camBuffer.GetAllocation(), &mappedData);
// copy vertex buffer
memcpy(mappedData, &camData, sizeof(CameraData));
InitDescriptors();
InitPipeline();
@@ -108,6 +121,9 @@ void VkRenderer::Initialize()
InitImgui();
View = Matrix4(1.0f);
Projection = Matrix4(1.0f);
IsInitialized = true;
}
@@ -318,6 +334,13 @@ 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);
}
// Triangle vertex buffer layout
{
DescriptorLayoutBuilder builder;
@@ -327,6 +350,7 @@ void VkRenderer::InitDescriptors()
DrawImageDescriptors = GlobalDescriptorAllocator.Allocate(Device, DrawImageDescriptorLayout);
TriangleBufferDescriptors = GlobalDescriptorAllocator.Allocate(Device, TriangleBufferDescriptorLayout);
CameraBufferDescriptors = GlobalDescriptorAllocator.Allocate(Device, CameraBufferDescriptorLayout);
UpdateDescriptorSets();
}
@@ -346,7 +370,6 @@ void VkRenderer::UpdateDescriptorSets()
drawImageWrite.descriptorCount = 1;
drawImageWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
drawImageWrite.pImageInfo = &imgInfo;
vkUpdateDescriptorSets(Device, 1, &drawImageWrite, 0, nullptr);
// Update descriptor set for TriangleBufferDescriptors
@@ -364,6 +387,22 @@ void VkRenderer::UpdateDescriptorSets()
bufferWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
bufferWrite.pBufferInfo = &bufferInfo;
vkUpdateDescriptorSets(Device, 1, &bufferWrite, 0, nullptr);
// Update descriptor set for cameras
VkDescriptorBufferInfo camBufferInfo{};
//bufferInfo.buffer = 0;
bufferInfo.offset = 0;
bufferInfo.range = VK_WHOLE_SIZE;
VkWriteDescriptorSet bufferWriteCam = {};
bufferWriteCam.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
bufferWriteCam.pNext = nullptr;
bufferWriteCam.dstBinding = 0;
bufferWriteCam.dstSet = CameraBufferDescriptors;
bufferWriteCam.descriptorCount = 1;
bufferWriteCam.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
bufferWriteCam.pBufferInfo = &camBufferInfo;
vkUpdateDescriptorSets(Device, 1, &bufferWriteCam, 0, nullptr);
}
void VkRenderer::InitPipeline()
@@ -655,6 +694,11 @@ void VkRenderer::InitImgui()
});
}
void VkRenderer::BeginScene(const Matrix4 & view, const Matrix4 & projection)
{
}
void VkRenderer::Draw()
{
VK_CALL(vkWaitForFences(Device, 1, &GetCurrentFrame().RenderFence, true, 1000000000));

View File

@@ -118,7 +118,11 @@ namespace Nuake
constexpr unsigned int FRAME_OVERLAP = 2;
struct CameraData
{
Matrix4 View;
Matrix4 Projection;
};
class VkRenderer
@@ -165,6 +169,9 @@ namespace Nuake
VkDescriptorSet TriangleBufferDescriptors;
VkDescriptorSetLayout TriangleBufferDescriptorLayout;
VkDescriptorSet CameraBufferDescriptors;
VkDescriptorSetLayout CameraBufferDescriptorLayout;
// Pipeline
VkPipeline Pipeline;
VkPipelineLayout PipelineLayout;
@@ -184,6 +191,9 @@ namespace Nuake
// Buffers
Ref<GPUMeshBuffers> rectangle;
Matrix4 View;
Matrix4 Projection;
public:
static VkRenderer& Get()
{
@@ -220,6 +230,8 @@ namespace Nuake
void DrawGeometry(VkCommandBuffer cmd);
void InitImgui();
void BeginScene(const Matrix4& view, const Matrix4& projection);
void Draw();
void DrawBackground(VkCommandBuffer cmd);
void DrawImgui(VkCommandBuffer cmd, VkImageView targetImageView);

View File

@@ -1,3 +1,10 @@
// struct World
// {
// mat4x4 view;
// mat4x4 proj;
// }
// World worldSettings : register(t0);
struct Vertex
{
float3 position;
@@ -6,14 +13,11 @@ struct Vertex
float uv_y;
float4 color;
};
StructuredBuffer<Vertex> vertexBuffer : register(t1);
// Define the structured buffer for vertices
StructuredBuffer<Vertex> vertexBuffer : register(t1); // 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)
cbuffer PushConstants : register(b0)
{
float4x4 render_matrix;
};
// Outputs
@@ -24,22 +28,10 @@ struct VSOutput {
};
// Main vertex shader
VSOutput main(uint vertexIndex : SV_VertexID) {
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)
};
// 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
};
// Load vertex data from the buffer
Vertex v = vertexBuffer[vertexIndex];