Graphics: Replace runtime structs offsets by compile-time struct offsets
now that nzsl::FieldOffsets is constexpr this is possible
This commit is contained in:
committed by
Jérôme Leclercq
parent
929b599337
commit
e10f15243d
@@ -1,84 +0,0 @@
|
||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/PredefinedShaderStructs.hpp>
|
||||
#include <NZSL/Math/FieldOffsets.hpp>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
// PredefinedLightData
|
||||
PredefinedLightData PredefinedLightData::GetOffsets()
|
||||
{
|
||||
PredefinedLightData lightData;
|
||||
|
||||
nzsl::FieldOffsets lightStruct(nzsl::StructLayout::Std140);
|
||||
lightData.lightMemberOffsets.type = lightStruct.AddField(nzsl::StructFieldType::Int1);
|
||||
lightData.lightMemberOffsets.color = lightStruct.AddField(nzsl::StructFieldType::Float4);
|
||||
lightData.lightMemberOffsets.factor = lightStruct.AddField(nzsl::StructFieldType::Float2);
|
||||
lightData.lightMemberOffsets.parameter1 = lightStruct.AddField(nzsl::StructFieldType::Float4);
|
||||
lightData.lightMemberOffsets.parameter2 = lightStruct.AddField(nzsl::StructFieldType::Float4);
|
||||
lightData.lightMemberOffsets.parameter3 = lightStruct.AddField(nzsl::StructFieldType::Float4);
|
||||
lightData.lightMemberOffsets.shadowMapSize = lightStruct.AddField(nzsl::StructFieldType::Float2);
|
||||
lightData.lightMemberOffsets.viewProjMatrix = lightStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
|
||||
lightData.lightSize = lightStruct.GetAlignedSize();
|
||||
|
||||
nzsl::FieldOffsets lightDataStruct(nzsl::StructLayout::Std140);
|
||||
lightData.lightsOffset = lightDataStruct.AddStructArray(lightStruct, MaxLightCount);
|
||||
lightData.lightCountOffset = lightDataStruct.AddField(nzsl::StructFieldType::UInt1);
|
||||
|
||||
lightData.totalSize = lightDataStruct.GetAlignedSize();
|
||||
|
||||
return lightData;
|
||||
}
|
||||
|
||||
// PredefinedInstanceData
|
||||
PredefinedInstanceData PredefinedInstanceData::GetOffsets()
|
||||
{
|
||||
nzsl::FieldOffsets viewerStruct(nzsl::StructLayout::Std140);
|
||||
|
||||
PredefinedInstanceData instanceData;
|
||||
instanceData.worldMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
instanceData.invWorldMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
|
||||
instanceData.totalSize = viewerStruct.GetAlignedSize();
|
||||
|
||||
return instanceData;
|
||||
}
|
||||
|
||||
// PredefinedSkeletalData
|
||||
PredefinedSkeletalData PredefinedSkeletalData::GetOffsets()
|
||||
{
|
||||
nzsl::FieldOffsets skeletalStruct(nzsl::StructLayout::Std140);
|
||||
|
||||
PredefinedSkeletalData skeletalData;
|
||||
skeletalData.jointMatricesOffset = skeletalStruct.AddMatrixArray(nzsl::StructFieldType::Float1, 4, 4, true, MaxMatricesCount);
|
||||
|
||||
skeletalData.totalSize = skeletalStruct.GetAlignedSize();
|
||||
|
||||
return skeletalData;
|
||||
}
|
||||
|
||||
// PredefinedViewerData
|
||||
PredefinedViewerData PredefinedViewerData::GetOffsets()
|
||||
{
|
||||
nzsl::FieldOffsets viewerStruct(nzsl::StructLayout::Std140);
|
||||
|
||||
PredefinedViewerData viewerData;
|
||||
viewerData.projMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
viewerData.invProjMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
viewerData.viewMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
viewerData.invViewMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
viewerData.viewProjMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
viewerData.invViewProjMatrixOffset = viewerStruct.AddMatrix(nzsl::StructFieldType::Float1, 4, 4, true);
|
||||
viewerData.targetSizeOffset = viewerStruct.AddField(nzsl::StructFieldType::Float2);
|
||||
viewerData.invTargetSizeOffset = viewerStruct.AddField(nzsl::StructFieldType::Float2);
|
||||
viewerData.eyePositionOffset = viewerStruct.AddField(nzsl::StructFieldType::Float3);
|
||||
|
||||
viewerData.totalSize = viewerStruct.GetAlignedSize();
|
||||
|
||||
return viewerData;
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,9 @@ namespace Nz
|
||||
{
|
||||
NazaraAssert(m_skeleton, "invalid skeleton");
|
||||
|
||||
PredefinedSkeletalData skeletalUboOffsets = PredefinedSkeletalData::GetOffsets();
|
||||
|
||||
m_skeletalDataBuffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(BufferType::Uniform, skeletalUboOffsets.totalSize, BufferUsage::DeviceLocal | BufferUsage::Dynamic | BufferUsage::Write);
|
||||
m_skeletalDataBuffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(BufferType::Uniform, PredefinedSkeletalOffsets.totalSize, BufferUsage::DeviceLocal | BufferUsage::Dynamic | BufferUsage::Write);
|
||||
m_skeletalDataBuffer->UpdateDebugName("Skeletal data");
|
||||
|
||||
m_onSkeletonJointsInvalidated.Connect(m_skeleton->OnSkeletonJointsInvalidated, [this](const Skeleton*)
|
||||
{
|
||||
m_dataInvalided = true;
|
||||
@@ -48,10 +47,8 @@ namespace Nz
|
||||
if (!m_dataInvalided)
|
||||
return;
|
||||
|
||||
PredefinedSkeletalData skeletalUboOffsets = PredefinedSkeletalData::GetOffsets();
|
||||
|
||||
auto& allocation = renderFrame.GetUploadPool().Allocate(m_skeletalDataBuffer->GetSize());
|
||||
Matrix4f* matrices = AccessByOffset<Matrix4f*>(allocation.mappedPtr, skeletalUboOffsets.jointMatricesOffset);
|
||||
Matrix4f* matrices = AccessByOffset<Matrix4f*>(allocation.mappedPtr, PredefinedSkeletalOffsets.jointMatricesOffset);
|
||||
|
||||
for (std::size_t i = 0; i < m_skeleton->GetJointCount(); ++i)
|
||||
matrices[i] = m_skeleton->GetJoint(i)->GetSkinningMatrix();
|
||||
|
||||
@@ -25,9 +25,7 @@ namespace Nz
|
||||
m_eyePosition(Vector3f::Zero()),
|
||||
m_dataInvalidated(true)
|
||||
{
|
||||
PredefinedViewerData viewerUboOffsets = PredefinedViewerData::GetOffsets();
|
||||
|
||||
m_viewerDataBuffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(BufferType::Uniform, viewerUboOffsets.totalSize, BufferUsage::DeviceLocal | BufferUsage::Dynamic | BufferUsage::Write);
|
||||
m_viewerDataBuffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(BufferType::Uniform, PredefinedViewerOffsets.totalSize, BufferUsage::DeviceLocal | BufferUsage::Dynamic | BufferUsage::Write);
|
||||
m_viewerDataBuffer->UpdateDebugName("Viewer data");
|
||||
}
|
||||
|
||||
@@ -36,7 +34,7 @@ namespace Nz
|
||||
if (!m_dataInvalidated)
|
||||
return;
|
||||
|
||||
PredefinedViewerData viewerDataOffsets = PredefinedViewerData::GetOffsets();
|
||||
constexpr auto& viewerDataOffsets = PredefinedViewerOffsets;
|
||||
|
||||
auto& allocation = renderFrame.GetUploadPool().Allocate(viewerDataOffsets.totalSize);
|
||||
AccessByOffset<Vector3f&>(allocation.mappedPtr, viewerDataOffsets.eyePositionOffset) = m_eyePosition;
|
||||
@@ -50,6 +48,9 @@ namespace Nz
|
||||
AccessByOffset<Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.viewProjMatrixOffset) = m_viewProjMatrix;
|
||||
AccessByOffset<Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.viewMatrixOffset) = m_viewMatrix;
|
||||
|
||||
AccessByOffset<float&>(allocation.mappedPtr, viewerDataOffsets.nearPlaneOffset) = m_nearPlane;
|
||||
AccessByOffset<float&>(allocation.mappedPtr, viewerDataOffsets.farPlaneOffset) = m_farPlane;
|
||||
|
||||
builder.CopyBuffer(allocation, m_viewerDataBuffer.get());
|
||||
|
||||
m_dataInvalidated = false;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Nz
|
||||
m_worldMatrix(Matrix4f::Identity()),
|
||||
m_dataInvalided(true)
|
||||
{
|
||||
PredefinedInstanceData instanceUboOffsets = PredefinedInstanceData::GetOffsets();
|
||||
constexpr auto& instanceUboOffsets = PredefinedInstanceOffsets;
|
||||
|
||||
m_instanceDataBuffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(BufferType::Uniform, instanceUboOffsets.totalSize, BufferUsage::DeviceLocal | BufferUsage::Dynamic | BufferUsage::Write);
|
||||
m_instanceDataBuffer->UpdateDebugName("Instance data");
|
||||
@@ -30,7 +30,7 @@ namespace Nz
|
||||
if (!m_dataInvalided)
|
||||
return;
|
||||
|
||||
PredefinedInstanceData instanceUboOffsets = PredefinedInstanceData::GetOffsets();
|
||||
constexpr auto& instanceUboOffsets = PredefinedInstanceOffsets;
|
||||
|
||||
auto& allocation = renderFrame.GetUploadPool().Allocate(m_instanceDataBuffer->GetSize());
|
||||
AccessByOffset<Matrix4f&>(allocation.mappedPtr, instanceUboOffsets.worldMatrixOffset) = m_worldMatrix;
|
||||
|
||||
Reference in New Issue
Block a user