Optimize fixedArea mode culling process for v3.4.1 (#4124)

This commit is contained in:
troublemaker52025
2021-12-28 11:15:32 +08:00
committed by GitHub
parent 4462b96617
commit d65a00dee7
2 changed files with 114 additions and 123 deletions
+18 -40
View File
@@ -176,29 +176,18 @@ void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array
if (shadowInfo->enabled) {
if (mainLight && shadowInfo->shadowType == scene::ShadowType::SHADOWMAP) {
const Mat4 &matShadowView = sceneData->getMatShadowView();
const Mat4 &matShadowProj = sceneData->getMatShadowProj();
const Mat4 &matShadowViewProj = sceneData->getMatShadowViewProj();
float nearClamp;
float farClamp;
Mat4 matShadowView;
Mat4 matShadowProj;
Mat4 matShadowViewProj;
if (!shadowInfo->fixedArea) {
nearClamp = shadowInfo->nearValue;
farClamp = sceneData->getShadowCameraFar();
matShadowView = sceneData->getMatShadowView();
matShadowProj = sceneData->getMatShadowProj();
matShadowViewProj = sceneData->getMatShadowViewProj();
if (shadowInfo->fixedArea) {
nearClamp = shadowInfo->nearValue;
farClamp = shadowInfo->farValue;
} else {
const auto *node = mainLight->getNode();
matShadowView = node->getWorldMatrix().getInversed();
const float x = shadowInfo->orthoSize;
const float y = shadowInfo->orthoSize;
nearClamp = shadowInfo->nearValue;
farClamp = shadowInfo->farValue;
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4::createOrthographicOffCenter(-x, x, -y, y, -nearClamp,
farClamp, clipSpaceMinZ, projectionSinY, &matShadowProj);
matShadowViewProj = matShadowProj * matShadowView;
nearClamp = shadowInfo->nearValue;
farClamp = sceneData->getShadowCameraFar();
}
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_OFFSET, matShadowView.m, sizeof(matShadowView));
@@ -241,29 +230,18 @@ void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::
const float packing = hFTexture ? 0.0F : 1.0F;
switch (light->getType()) {
case scene::LightType::DIRECTIONAL: {
const Mat4 &matShadowView = sceneData->getMatShadowView();
const Mat4 &matShadowProj = sceneData->getMatShadowProj();
const Mat4 &matShadowViewProj = sceneData->getMatShadowViewProj();
float nearClamp;
float farClamp;
Mat4 matShadowView;
Mat4 matShadowProj;
Mat4 matShadowViewProj;
if (!shadowInfo->fixedArea) {
nearClamp = 0.1F;
farClamp = sceneData->getShadowCameraFar();
matShadowView = sceneData->getMatShadowView();
matShadowProj = sceneData->getMatShadowProj();
matShadowViewProj = sceneData->getMatShadowViewProj();
if (shadowInfo->fixedArea) {
nearClamp = shadowInfo->nearValue;
farClamp = shadowInfo->farValue;
} else {
auto *const node = light->getNode();
matShadowView = node->getWorldMatrix().getInversed();
const float x = shadowInfo->orthoSize;
const float y = shadowInfo->orthoSize;
nearClamp = shadowInfo->nearValue;
farClamp = shadowInfo->farValue;
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4::createOrthographicOffCenter(-x, x, -y, y, -nearClamp,
farClamp, clipSpaceMinZ, projectionSinY, &matShadowProj);
matShadowViewProj = matShadowProj * matShadowView;
nearClamp = 0.1F;
farClamp = sceneData->getShadowCameraFar();
}
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_OFFSET, matShadowView.m, sizeof(matShadowView));
+96 -83
View File
@@ -215,85 +215,109 @@ void quantizeDirLightShadowCamera(RenderPipeline *pipeline, const scene::Camera
const float shadowMapWidth = shadowInfo->size.x;
const auto * node = mainLight->getNode();
const Quaternion & rotation = node->getWorldRotation();
const bool fixedArea = shadowInfo->fixedArea;
// Raw data.
const Mat4 matWorldTrans = getCameraWorldMatrix(camera);
scene::Frustum validFrustum;
validFrustum.type = scene::ShapeEnums::SHAPE_FRUSTUM_ACCURATE;
validFrustum.split(0.1F, shadowInfo->shadowDistance, camera->aspect, camera->fov, matWorldTrans);
scene::Frustum lightViewFrustum = validFrustum.clone();
if (fixedArea) {
const float x = shadowInfo->orthoSize;
const float y = shadowInfo->orthoSize;
const float nearClamp = shadowInfo->nearValue;
const float farClamp = shadowInfo->farValue;
// view matrix with range back.
Mat4 matShadowTrans;
Mat4::fromRT(rotation, Vec3::ZERO, &matShadowTrans);
Mat4 matShadowView = matShadowTrans.getInversed();
Mat4 matShadowViewInv = matShadowTrans.clone();
Mat4 matShadowTrans;
Mat4::fromRT(rotation, node->getWorldPosition(), &matShadowTrans);
Mat4 matShadowView = matShadowTrans.getInversed();
Mat4 matShadowViewInv = matShadowTrans.clone();
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4 matShadowProj;
Mat4::createOrthographicOffCenter(-x, x, -y, y, -nearClamp,
farClamp, clipSpaceMinZ, projectionSinY, &matShadowProj);
Mat4 matShadowViewProj = matShadowProj * matShadowView;
sceneData->setMatShadowView(matShadowView);
sceneData->setMatShadowProj(matShadowProj);
sceneData->setMatShadowViewProj(matShadowViewProj);
const Mat4 shadowViewArbitraryPos = matShadowView.clone();
lightViewFrustum.transform(matShadowView);
// bounding box in light space.
scene::AABB castLightViewBounds;
scene::AABB::fromPoints(Vec3(10000000.0F, 10000000.0F, 10000000.0F), Vec3(-10000000.0F, -10000000.0F, -10000000.0F), &castLightViewBounds);
castLightViewBounds.merge(lightViewFrustum);
out->createOrtho(x * 2.0F, y * 2.0F, nearClamp, farClamp, matShadowViewInv);
} else {
// Raw data.
const Mat4 matWorldTrans = getCameraWorldMatrix(camera);
scene::Frustum validFrustum;
validFrustum.type = scene::ShapeEnums::SHAPE_FRUSTUM_ACCURATE;
validFrustum.split(0.1F, shadowInfo->shadowDistance, camera->aspect, camera->fov, matWorldTrans);
scene::Frustum lightViewFrustum = validFrustum.clone();
const float r = castLightViewBounds.getHalfExtents().z * 2.0F;
Vec3 shadowPos(castLightViewBounds.getCenter().x, castLightViewBounds.getCenter().y,
castLightViewBounds.getCenter().z + castLightViewBounds.getHalfExtents().z + invisibleOcclusionRange);
shadowPos.transformMat4(shadowPos, matShadowViewInv);
// view matrix with range back.
Mat4 matShadowTrans;
Mat4::fromRT(rotation, Vec3::ZERO, &matShadowTrans);
Mat4 matShadowView = matShadowTrans.getInversed();
Mat4 matShadowViewInv = matShadowTrans.clone();
Mat4::fromRT(rotation, shadowPos, &matShadowTrans);
matShadowView = matShadowTrans.getInversed();
matShadowViewInv = matShadowTrans.clone();
const Mat4 shadowViewArbitraryPos = matShadowView.clone();
lightViewFrustum.transform(matShadowView);
// bounding box in light space.
scene::AABB castLightViewBounds;
scene::AABB::fromPoints(Vec3(10000000.0F, 10000000.0F, 10000000.0F), Vec3(-10000000.0F, -10000000.0F, -10000000.0F), &castLightViewBounds);
castLightViewBounds.merge(lightViewFrustum);
// calculate projection matrix params.
// min value may lead to some shadow leaks.
const float orthoSizeMin = validFrustum.vertices[0].distance(validFrustum.vertices[6]);
// max value is accurate but poor usage for shadowMap
scene::Sphere cameraBoundingSphere;
cameraBoundingSphere.setCenter(Vec3(0.0F, 0.0F, 0.0F));
cameraBoundingSphere.setRadius(-1.0F);
cameraBoundingSphere.merge(validFrustum);
const float orthoSizeMax = cameraBoundingSphere.getRadius() * 2.0F;
// use lerp(min, accurate_max) to save shadowMap usage
const float orthoSize = orthoSizeMin * 0.8F + orthoSizeMax * 0.2F;
sceneData->setShadowCameraFar(r + invisibleOcclusionRange);
const float r = castLightViewBounds.getHalfExtents().z * 2.0F;
Vec3 shadowPos(castLightViewBounds.getCenter().x, castLightViewBounds.getCenter().y,
castLightViewBounds.getCenter().z + castLightViewBounds.getHalfExtents().z + invisibleOcclusionRange);
shadowPos.transformMat4(shadowPos, matShadowViewInv);
// snap to whole texels
const float halfOrthoSize = orthoSize * 0.5F;
Mat4 matShadowProj;
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4::createOrthographicOffCenter(-halfOrthoSize, halfOrthoSize, -halfOrthoSize, halfOrthoSize, 0.1F, sceneData->getShadowCameraFar(),
clipSpaceMinZ, projectionSinY, &matShadowProj);
if (shadowMapWidth > 0.0F) {
const Mat4 matShadowViewProjArbitraryPos = matShadowProj * shadowViewArbitraryPos;
Vec3 projPos;
projPos.transformMat4(shadowPos, matShadowViewProjArbitraryPos);
const float invActualSize = 2.0F / shadowMapWidth;
const Vec2 texelSize(invActualSize, invActualSize);
const float modX = fmodf(projPos.x, texelSize.x);
const float modY = fmodf(projPos.y, texelSize.y);
const Vec3 projSnap(projPos.x - modX, projPos.y - modY, projPos.z);
const Mat4 matShadowViewProjArbitaryPosInv = matShadowViewProjArbitraryPos.getInversed();
Vec3 snap;
snap.transformMat4(projSnap, matShadowViewProjArbitaryPosInv);
Mat4::fromRT(rotation, snap, &matShadowTrans);
Mat4::fromRT(rotation, shadowPos, &matShadowTrans);
matShadowView = matShadowTrans.getInversed();
matShadowViewInv = matShadowTrans.clone();
out->createOrtho(orthoSize, orthoSize, 0.1F, sceneData->getShadowCameraFar(), matShadowViewInv);
} else {
for (uint i = 0; i < 8; i++) {
out->vertices[i].setZero();
}
out->updatePlanes();
}
const Mat4 matShadowViewProj = matShadowProj * matShadowView;
sceneData->setMatShadowView(matShadowView);
sceneData->setMatShadowProj(matShadowProj);
sceneData->setMatShadowViewProj(matShadowViewProj);
// calculate projection matrix params.
// min value may lead to some shadow leaks.
const float orthoSizeMin = validFrustum.vertices[0].distance(validFrustum.vertices[6]);
// max value is accurate but poor usage for shadowMap
scene::Sphere cameraBoundingSphere;
cameraBoundingSphere.setCenter(Vec3(0.0F, 0.0F, 0.0F));
cameraBoundingSphere.setRadius(-1.0F);
cameraBoundingSphere.merge(validFrustum);
const float orthoSizeMax = cameraBoundingSphere.getRadius() * 2.0F;
// use lerp(min, accurate_max) to save shadowMap usage
const float orthoSize = orthoSizeMin * 0.8F + orthoSizeMax * 0.2F;
sceneData->setShadowCameraFar(r + invisibleOcclusionRange);
// snap to whole texels
const float halfOrthoSize = orthoSize * 0.5F;
Mat4 matShadowProj;
const float projectionSinY = device->getCapabilities().clipSpaceSignY;
const float clipSpaceMinZ = device->getCapabilities().clipSpaceMinZ;
Mat4::createOrthographicOffCenter(-halfOrthoSize, halfOrthoSize, -halfOrthoSize, halfOrthoSize, 0.1F, sceneData->getShadowCameraFar(),
clipSpaceMinZ, projectionSinY, &matShadowProj);
if (shadowMapWidth > 0.0F) {
const Mat4 matShadowViewProjArbitraryPos = matShadowProj * shadowViewArbitraryPos;
Vec3 projPos;
projPos.transformMat4(shadowPos, matShadowViewProjArbitraryPos);
const float invActualSize = 2.0F / shadowMapWidth;
const Vec2 texelSize(invActualSize, invActualSize);
const float modX = fmodf(projPos.x, texelSize.x);
const float modY = fmodf(projPos.y, texelSize.y);
const Vec3 projSnap(projPos.x - modX, projPos.y - modY, projPos.z);
const Mat4 matShadowViewProjArbitaryPosInv = matShadowViewProjArbitraryPos.getInversed();
Vec3 snap;
snap.transformMat4(projSnap, matShadowViewProjArbitaryPosInv);
Mat4::fromRT(rotation, snap, &matShadowTrans);
matShadowView = matShadowTrans.getInversed();
matShadowViewInv = matShadowTrans.clone();
out->createOrtho(orthoSize, orthoSize, 0.1F, sceneData->getShadowCameraFar(), matShadowViewInv);
} else {
for (uint i = 0; i < 8; i++) {
out->vertices[i].setZero();
}
out->updatePlanes();
}
const Mat4 matShadowViewProj = matShadowProj * matShadowView;
sceneData->setMatShadowView(matShadowView);
sceneData->setMatShadowProj(matShadowProj);
sceneData->setMatShadowViewProj(matShadowViewProj);
}
}
void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
PipelineSceneData *const sceneData = pipeline->getPipelineSceneData();
@@ -353,11 +377,7 @@ void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
if (isShadowMap) {
std::vector<scene::Model *> casters;
casters.reserve(scene->getModels().size() / 4);
if (shadowInfo->fixedArea) {
octree->queryVisibility(camera, camera->frustum, true, casters);
} else {
octree->queryVisibility(camera, dirLightFrustum, true, casters);
}
octree->queryVisibility(camera, dirLightFrustum, true, casters);
for (const auto *model : casters) {
dirShadowObjects.emplace_back(genRenderObject(model, camera));
}
@@ -392,15 +412,8 @@ void sceneCulling(RenderPipeline *pipeline, scene::Camera *camera) {
// dir shadow render Object
if (isShadowMap && model->getCastShadow()) {
if (shadowInfo->fixedArea) {
model->getWorldBounds()->transform(shadowInfo->matLight, &ab);
if (ab.aabbFrustum(camera->frustum)) {
dirShadowObjects.emplace_back(genRenderObject(model, camera));
}
} else {
if (modelWorldBounds->aabbFrustum(dirLightFrustum)) {
dirShadowObjects.emplace_back(genRenderObject(model, camera));
}
if (modelWorldBounds->aabbFrustum(dirLightFrustum)) {
dirShadowObjects.emplace_back(genRenderObject(model, camera));
}
}