Fixed camera view array

This commit is contained in:
antopilo
2025-01-10 23:36:25 -05:00
parent d419d6019f
commit 72440a362d
10 changed files with 91 additions and 10 deletions

View File

@@ -28,9 +28,13 @@ namespace Nuake
Vector3 Position;
float Near;
float Far;
float pad;
float pad2;
float pad3;
//char padding[64]; // 124 bytes to reach 128 bytes
};
struct LightResource
struct LightResources
{
};
@@ -45,6 +49,7 @@ namespace Nuake
std::map<UUID, Ref<VulkanImage>> Light;
std::vector<CameraView> Cameras;
std::array<CameraView, MAX_CAMERAS> CamerasData;
// Bindless buffer layouts
VkDescriptorSetLayout CameraDescriptorLayout;
VkDescriptorSetLayout TriangleBufferDescriptorLayout;

View File

@@ -212,7 +212,7 @@ void GPUResources::CreateBindlessLayout()
// bindless cameras
{
DescriptorLayoutBuilder builder;
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, MAX_CAMERAS);
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
CamerasDescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS);
}
@@ -259,6 +259,13 @@ void GPUResources::RecreateBindlessCameras()
CreateBindlessLayout();
}
int i = 0;
for (auto& c : Cameras)
{
CamerasData[i] = c;
i++;
}
void* mappedData;
auto allocator = VulkanAllocator::Get().GetAllocator();
vmaMapMemory(allocator, (VkRenderer::Get().GetCurrentFrame().CamerasStagingBuffer->GetAllocation()), &mappedData);

View File

@@ -96,6 +96,7 @@ void VkSceneRenderer::BeginScene(RenderContext inContext)
camData.Near = camera->Near;
camData.Far = camera->Far;
GPUResources::Get().AddCamera(camera->ID, camData);
GPUResources::Get().AddCamera(camera->ID + 1, camData);
auto view = scene->m_Registry.view<TransformComponent, CameraComponent>();
for (auto e : view)
@@ -112,12 +113,36 @@ void VkSceneRenderer::BeginScene(RenderContext inContext)
GPUResources::Get().AddCamera(camera.ID, camData);
}
}
// Build light view list
{
auto view = scene->m_Registry.view<TransformComponent, LightComponent>();
for (auto e : view)
{
auto [transform, light] = view.get<TransformComponent, LightComponent>(e);
//light.CalculateViewProjection();
for (int i = 0; i < light.m_LightViews.size(); i++)
{
LightView& view = light.m_LightViews[i];
CameraView viewData{};
viewData.View = view.View;
viewData.Projection = view.Proj;
viewData.InverseView = glm::inverse(view.View);
viewData.InverseProjection = glm::inverse(view.Proj);
viewData.Position = transform.GetGlobalTransform()[3];
viewData.Near = 25.0;
viewData.Far = -25.0;
GPUResources::Get().AddCamera(view.CameraID, viewData);
}
}
}
{
auto view = scene->m_Registry.view<TransformComponent, LightComponent>();
for (auto e : view)
{
auto [transform, light] = view.get<TransformComponent, LightComponent>(e);
auto cam = GPUResources::Get().GetCamera(inContext.CameraID);
light.CalculateViewProjection(cam.View, cam.Projection);
}
}
@@ -128,9 +153,28 @@ void VkSceneRenderer::BeginScene(RenderContext inContext)
passCtx.scene = inContext.CurrentScene;
passCtx.commandBuffer = inContext.CommandBuffer;
passCtx.resolution = Context.Size;
passCtx.cameraID = GPUResources::Get().GetBindlessCameraID(Context.CameraID);
//ShadowPipeline.Execute(passCtx);
auto view = scene->m_Registry.view<TransformComponent, LightComponent>();
for (auto e : view)
{
auto [transform, light] = view.get<TransformComponent, LightComponent>(e);
if (light.Type != LightType::Directional)
{
continue;
}
passCtx.cameraID = GPUResources::Get().GetBindlessCameraID(light.m_LightViews[0].CameraID);
ShadowPipeline.Execute(passCtx);
for (int i = 0; i < CSM_AMOUNT; i++)
{
}
}
passCtx.cameraID = GPUResources::Get().GetBindlessCameraID(Context.CameraID);
GBufferPipeline.Execute(passCtx);
}
ModelPushConstant modelPushConstant{};

View File

@@ -13,7 +13,13 @@ namespace Nuake
Color(1, 1, 1),
Strength(10.0f),
Direction(0, 1, 0)
{
m_LightViews = std::vector<LightView>(CSM_AMOUNT);
for (int i = 0; i < CSM_AMOUNT; i++)
{
m_LightViews[i] = LightView{};
}
}
void LightComponent::SetCastShadows(bool toggle)

View File

@@ -18,6 +18,14 @@ namespace Nuake
Directional, Point, Spot
};
struct LightView
{
UUID CameraID;
Matrix4 View;
Matrix4 Proj;
};
const int CSM_AMOUNT = 4;
class LightComponent : public Component
{
@@ -38,6 +46,7 @@ namespace Nuake
Ref<FrameBuffer> m_Framebuffers[CSM_AMOUNT];
Matrix4 mViewProjections[CSM_AMOUNT];
std::vector<LightView> m_LightViews;
float mCascadeSplitDepth[CSM_AMOUNT];
float mCascadeSplits[CSM_AMOUNT];
@@ -52,7 +61,9 @@ namespace Nuake
void CalculateViewProjection(glm::mat4& view, const glm::mat4& projection)
{
glm::mat4 viewProjection = projection * view;
Matrix4 normalProj = projection;
normalProj *= -1.0f;
glm::mat4 viewProjection = normalProj * view;
glm::mat4 inverseViewProjection = glm::inverse(viewProjection);
// TODO: Automate this
@@ -147,6 +158,10 @@ namespace Nuake
roundOffset.w = 0.0f;
lightProjectionMatrix[3] += roundOffset;
m_LightViews[cascade].View = view;
m_LightViews[cascade].Proj = projection;
//m_LightViews[cascade].Proj[1][1] *= -1.0f;
// Store SplitDistance and ViewProjection-Matrix
mCascadeSplitDepth[cascade] = (nearClip + splitDist * clipRange) * 1.0f;
mViewProjections[cascade] = shadowMatrix;

View File

@@ -234,6 +234,7 @@ void Window::EndDraw()
}
auto& vkRenderer = VkRenderer::Get();
GPUResources::Get().RecreateBindlessTextures();
if(vkRenderer.Draw())
{
// Render whatever we want in here :)
@@ -242,10 +243,11 @@ void Window::EndDraw()
RenderContext ctx
{
scene,
vkRenderer.GetCurrentCmdBuffer()
vkRenderer.GetCurrentCmdBuffer(),
Vector2{},
scene->GetCurrentCamera()->ID
};
GPUResources::Get().RecreateBindlessTextures();
vkRenderer.DrawScene(ctx);
}
}

View File

@@ -46,6 +46,7 @@ struct Light
[[vk::binding(0, 6)]]
StructuredBuffer<Light> lights;
struct CameraView {
float4x4 View;
float4x4 Projection;

View File

@@ -45,6 +45,7 @@ struct Light
[[vk::binding(0, 6)]]
StructuredBuffer<Light> lights;
struct CameraView {
float4x4 View;
float4x4 Projection;

View File

@@ -87,7 +87,7 @@ VSOutput main(uint vertexIndex : SV_VertexID)
Vertex v = vertexBuffer[vertexIndex];
// Output the position of each vertex
output.Position = mul(camView.Projection, mul(camView.View, mul(modelData.model, float4(v.position, 1.0f))));
output.Position = mul(camView.Projection, mul(camView.View,mul(modelData.model, float4(v.position, 1.0f))));
return output;
}

View File

@@ -83,7 +83,7 @@ VSOutput main(uint vertexIndex : SV_VertexID)
VSOutput output;
Camera camData = camera[0];
CameraView camView = cameras[pushConstants.cameraID];
CameraView camView = cameras[1];
ModelData modelData = model[pushConstants.modelIndex];
// Load vertex data from the buffer