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
+10 -9
View File
@@ -13,7 +13,7 @@ struct ShadowMappingData
SamplerState shadowSampler;
ConstantBuffer<float[NUM_CASCADES]> cascadeSplits;
ConstantBuffer<float4> cascadeSplits;
};
ParameterBlock<ShadowMappingData> pShadowMapping;
@@ -27,7 +27,7 @@ ParameterBlock<LightCullingData> pLightCullingData;
static const float4x4 biasMat = float4x4(
0.5, 0.0, 0.0, 0.5,
0.0, 0.5, 0.0, 0.5,
0.0, -0.5, 0.0, 0.5,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
@@ -46,11 +46,12 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
{
uint cascadeIndex = 0;
for (uint c = 0; c < NUM_CASCADES - 1; ++c) {
if (params.position_VS.z < pShadowMapping.cascadeSplits[c]) {
cascadeIndex = c + 1;
if (params.position_VS.z > pShadowMapping.cascadeSplits[c]) {
//cascadeIndex = c + 1;
}
}
float4 lightSpacePos = mul(biasMat, mul(pShadowMapping.lightSpaceMatrices[cascadeIndex][i], float4(params.position_WS, 1)));
float4 lightSpacePos = mul(pShadowMapping.lightSpaceMatrices[cascadeIndex][i], float4(params.position_WS, 1));
lightSpacePos = mul(biasMat, lightSpacePos);
float4 shadowCoord = lightSpacePos / lightSpacePos.w;
int3 texDim;
pShadowMapping.shadowMaps[cascadeIndex].GetDimensions(texDim.x, texDim.y, texDim.z);
@@ -67,7 +68,7 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
if (shadowCoord.z > 0.0 && shadowCoord.z < 1.0)
{
float dist = pShadowMapping.shadowMaps[cascadeIndex].Sample(pShadowMapping.shadowSampler, float3(shadowCoord.xy + float2(dx * x, dy * y), i)).r;
if (shadowCoord.w > 0 && dist > shadowCoord.z)
if (shadowCoord.w > 0 && dist > shadowCoord.z + 0.005f)
{
shadow = 0;
}
@@ -77,13 +78,13 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
}
}
result += (shadowFactor / count) * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
result = float3(pShadowMapping.shadowMaps[cascadeIndex].Sample(pShadowMapping.shadowSampler, float3(shadowCoord.xy, i)).r, 0, 0);//(shadowFactor / count) * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
}
for (uint i = 0; i < pLightEnv.numPointLights; ++i)
{
//uint lightIndex = pLightCullingData.lightIndexList[startOffset + i];
result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
//result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
}
result += brdf.evaluateAmbient(lightingParams.viewDir_WS);
//result += brdf.evaluateAmbient(lightingParams.viewDir_WS);
return float4(result, brdf.getAlpha());
}
@@ -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);
}
}
+58
View File
@@ -7,4 +7,62 @@ namespace Seele {
typedef glm::mat2 Matrix2;
typedef glm::mat3 Matrix3;
typedef glm::mat4 Matrix4;
static Matrix4 perspectiveProjection(float fov, float aspect, float nearPlane, float farPlane) {
const float e = 1.0f / std::tan(fov * 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,
},
};
}
static Matrix4 orthographicProjection(float left, float right, float bottom, float top, float nearPlane, float farPlane) {
return Matrix4{
{
2.0f / (right - left),
0.0f,
0.0f,
0.0f,
},
{
0.0f,
2.0f / (top - bottom),
0.0f,
0.0f,
},
{
0.0f,
0.0f,
-2.0f / (farPlane - nearPlane),
0.0f,
},
{
-(right + left) / (right - left),
-(top + bottom) / (top - bottom),
-(farPlane + nearPlane) / (farPlane - nearPlane),
1.0f,
},
};
}
} // namespace Seele