More shadowmappingchanges

This commit is contained in:
2025-08-14 11:06:43 +02:00
parent 6537459b77
commit 7ab9e8a5e5
7 changed files with 100 additions and 70 deletions
@@ -3,6 +3,7 @@
#include "Graphics/Graphics.h"
#include "Graphics/Initializer.h"
#include "Graphics/Shader.h"
#include "Math/Matrix.h"
#include <glm/ext/matrix_transform.hpp>
#include <glm/matrix.hpp>
@@ -64,6 +65,7 @@ void ShadowPass::beginFrame(const Component::Camera& camera, const Component::Tr
cascadeSplits[i] = (d - nearClip) / clipRange;
cascades[i].viewParams.clear();
}
cascadeSplitsBuffer->updateContents(0, sizeof(float) * NUM_CASCADES, cascadeSplits);
// call this to update view params member, ignore descriptor set
updateViewParameters(camera, transform);
@@ -107,7 +109,7 @@ void ShadowPass::beginFrame(const Component::Camera& camera, const Component::Tr
Vector cameraPos = frustumCenter - lightDir * -minExtents.z;
Matrix4 viewMatrix = glm::lookAt(cameraPos, frustumCenter, Vector(0, 1, 0));
Matrix4 projectionMatrix =
glm::ortho(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, 0.0f, maxExtents.z - minExtents.z);
orthographicProjection(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, 0.0f, maxExtents.z - minExtents.z);
Matrix4 viewProjectionMatrix = projectionMatrix * viewMatrix;
viewParams.viewMatrix = viewMatrix;
viewParams.inverseViewMatrix = glm::inverse(viewMatrix);
+8 -8
View File
@@ -267,9 +267,9 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptor) {
}
}
}
if (descriptorSet->constantsBuffer != nullptr) {
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
boundResources.add(PBufferAllocation(descriptorSet->constantsBuffer));
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
VkDescriptorSet setHandle = descriptorSet->getHandle();
@@ -298,9 +298,9 @@ void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorS
}
}
}
if (descriptorSet->constantsBuffer != nullptr) {
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
boundResources.add(PBufferAllocation(descriptorSet->constantsBuffer));
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
sets[layout->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
}
@@ -448,9 +448,9 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet descriptor) {
}
}
}
if (descriptorSet->constantsBuffer != nullptr) {
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
boundResources.add(PBufferAllocation(descriptorSet->constantsBuffer));
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
VkDescriptorSet setHandle = descriptorSet->getHandle();
@@ -475,9 +475,9 @@ void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptor
}
}
}
if (descriptorSet->constantsBuffer != nullptr) {
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
boundResources.add(PBufferAllocation(descriptorSet->constantsBuffer));
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
sets[pipeline->getPipelineLayout()->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
}
+16 -21
View File
@@ -159,7 +159,7 @@ Gfx::ODescriptorSet DescriptorPool::allocateDescriptorSet() {
if (cachedHandles[setIndex] == nullptr) {
cachedHandles[setIndex] = new DescriptorSetHandle(graphics, layout->getName());
}
if (cachedHandles[setIndex]->isCurrentlyBound()) {
if (cachedHandles[setIndex]->isCurrentlyBound() || cachedHandles[setIndex]->isUsed) {
// Currently in use, skip
continue;
}
@@ -175,7 +175,7 @@ Gfx::ODescriptorSet DescriptorPool::allocateDescriptorSet() {
};
vkSetDebugUtilsObjectNameEXT(graphics->getDevice(), &nameInfo);
}
cachedHandles[setIndex]->isUsed = true;
// Found set, stop searching
return new DescriptorSet(graphics, this, cachedHandles[setIndex]);
}
@@ -187,20 +187,13 @@ Gfx::ODescriptorSet DescriptorPool::allocateDescriptorSet() {
// throw std::logic_error("Out of descriptor sets");
}
void DescriptorPool::reset() {
for (uint32 i = 0; i < cachedHandles.size(); ++i) {
if (cachedHandles[i] == nullptr) {
return;
}
}
if (nextAlloc != nullptr) {
nextAlloc->reset();
}
}
void DescriptorPool::reset() {}
DescriptorSetHandle::DescriptorSetHandle(PGraphics graphics, const std::string& name) : CommandBoundResource(graphics, name) {}
DescriptorSetHandle::~DescriptorSetHandle() {}
DescriptorSetHandle::~DescriptorSetHandle() {
graphics->getDestructionManager()->queueResourceForDestruction(std::move(constantsBuffer));
}
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner, PDescriptorSetHandle setHandle)
: Gfx::DescriptorSet(owner->getLayout()), setHandle(setHandle),
@@ -215,7 +208,9 @@ DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner, PDescrip
constantData.resize(owner->getLayout()->constantsSize);
}
DescriptorSet::~DescriptorSet() { graphics->getDestructionManager()->queueResourceForDestruction(std::move(constantsBuffer)); }
DescriptorSet::~DescriptorSet() {
setHandle->isUsed = false;
}
void DescriptorSet::updateConstants(const std::string& mappingName, uint32 offset, void* data) {
std::memcpy(constantData.data() + owner->getLayout()->mappings[mappingName].constantOffset, (char*)data + offset,
@@ -408,10 +403,10 @@ void DescriptorSet::updateAccelerationStructure(const std::string& mappingName,
void DescriptorSet::writeChanges() {
if (constantData.size() > 0) {
if (constantsBuffer != nullptr) {
graphics->getDestructionManager()->queueResourceForDestruction(std::move(constantsBuffer));
if (setHandle->constantsBuffer != nullptr) {
graphics->getDestructionManager()->queueResourceForDestruction(std::move(setHandle->constantsBuffer));
}
constantsBuffer = new BufferAllocation(graphics, owner->getLayout()->getName(),
setHandle->constantsBuffer = new BufferAllocation(graphics, owner->getLayout()->getName(),
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
@@ -422,13 +417,13 @@ void DescriptorSet::writeChanges() {
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
constantsBuffer->updateContents(0, constantData.size(), constantData.data());
constantsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
setHandle->constantsBuffer->updateContents(0, constantData.size(), constantData.data());
setHandle->constantsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_ALL_COMMANDS_BIT);
bufferInfos.add(VkDescriptorBufferInfo{
.buffer = constantsBuffer->buffer,
.buffer = setHandle->constantsBuffer->buffer,
.offset = 0,
.range = constantsBuffer->size,
.range = setHandle->constantsBuffer->size,
});
writeDescriptors.add(VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
+2 -1
View File
@@ -40,6 +40,8 @@ public:
constexpr VkDescriptorSet getHandle() const { return handle; }
PGraphics graphics;
VkDescriptorSet handle = VK_NULL_HANDLE;
OBufferAllocation constantsBuffer;
bool isUsed = false;
};
DEFINE_REF(DescriptorSetHandle)
@@ -81,7 +83,6 @@ class DescriptorSet : public Gfx::DescriptorSet {
private:
std::vector<uint8> constantData;
OBufferAllocation constantsBuffer;
List<VkDescriptorImageInfo> imageInfos;
List<VkDescriptorBufferInfo> bufferInfos;
List<VkWriteDescriptorSetAccelerationStructureKHR> accelerationInfos;
+3 -30
View File
@@ -1,4 +1,5 @@
#include "Window.h"
#include "Math/Matrix.h"
using namespace Seele;
using namespace Seele::Gfx;
@@ -20,37 +21,9 @@ Viewport::Viewport(PWindow owner, const ViewportCreateInfo& viewportInfo)
Viewport::~Viewport() {}
Matrix4 Viewport::getProjectionMatrix(float nearPlane, float farPlane) const {
Matrix4 correctionMatrix = Matrix4(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 / 2.f, 0, 0, 0, 1 / 2.f, 1);
if (fieldOfView > 0.0f) {
const float aspect = static_cast<float>(sizeX) / sizeY;
const float e = 1.0f / std::tan(fieldOfView * 0.5f);
return {
{
e / aspect,
0.0f,
0.0f,
0.0f,
},
{
0.0f,
-e,
0.0f,
0.0f,
},
{
0.0f,
0.0f,
0.5f * (farPlane + nearPlane) / (nearPlane - farPlane),
-1.0f,
},
{
0.0f,
0.0f,
(farPlane * nearPlane) / (nearPlane - farPlane),
0.0f,
},
};
return perspectiveProjection(fieldOfView, static_cast<float>(sizeX) / sizeY, nearPlane, farPlane);
} else {
return correctionMatrix * glm::ortho(orthoLeft, orthoRight, orthoBottom, orthoTop, nearPlane, farPlane);
return orthographicProjection(orthoLeft, orthoRight, orthoBottom, orthoTop, nearPlane, farPlane);
}
}