fix shadow problem for v3.3.0 (#3674)

This commit is contained in:
troublemaker52025
2021-07-14 09:26:23 +08:00
committed by GitHub
parent 87f62fcbd3
commit 3e568df2ed
6 changed files with 60 additions and 65 deletions
+10 -10
View File
@@ -412,16 +412,16 @@ struct CC_DLL UBOCamera : public Object {
};
struct CC_DLL UBOShadow : public Object {
static constexpr uint MAT_LIGHT_PLANE_PROJ_OFFSET = 0;
static constexpr uint MAT_LIGHT_VIEW_OFFSET = MAT_LIGHT_PLANE_PROJ_OFFSET + 16;
static constexpr uint MAT_LIGHT_VIEW_PROJ_OFFSET = UBOShadow::MAT_LIGHT_VIEW_OFFSET + 16;
static constexpr uint SHADOW_NEAR_FAR_LINEAR_SELF_INFO_OFFSET = UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET + 16;
static constexpr uint SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET = UBOShadow::SHADOW_NEAR_FAR_LINEAR_SELF_INFO_OFFSET + 4;
static constexpr uint SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET = UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 4;
static constexpr uint SHADOW_COLOR_OFFSET = UBOShadow::SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 4;
static constexpr uint COUNT = UBOShadow::SHADOW_COLOR_OFFSET + 4;
static constexpr uint SIZE = UBOShadow::COUNT * 4;
static constexpr uint BINDING = static_cast<uint>(PipelineGlobalBindings::UBO_SHADOW);
static constexpr uint MAT_LIGHT_PLANE_PROJ_OFFSET = 0;
static constexpr uint MAT_LIGHT_VIEW_OFFSET = MAT_LIGHT_PLANE_PROJ_OFFSET + 16;
static constexpr uint MAT_LIGHT_VIEW_PROJ_OFFSET = UBOShadow::MAT_LIGHT_VIEW_OFFSET + 16;
static constexpr uint SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET = UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET + 16;
static constexpr uint SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET = UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 4;
static constexpr uint SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET = UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 4;
static constexpr uint SHADOW_COLOR_OFFSET = UBOShadow::SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 4;
static constexpr uint COUNT = UBOShadow::SHADOW_COLOR_OFFSET + 4;
static constexpr uint SIZE = UBOShadow::COUNT * 4;
static constexpr uint BINDING = static_cast<uint>(PipelineGlobalBindings::UBO_SHADOW);
static const gfx::DescriptorSetLayoutBinding DESCRIPTOR;
static const gfx::UniformBlock LAYOUT;
static const String NAME;
+16 -16
View File
@@ -168,6 +168,7 @@ void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array
auto *const shadowInfo = sceneData->getSharedData()->shadow;
auto & shadowUBO = *bufferView;
auto * sphere = sceneData->getSphere();
const bool hFTexture = supportsHalfFloatTexture(device);
if (shadowInfo->enabled) {
if (mainLight && shadowInfo->shadowType == scene::ShadowType::SHADOWMAP) {
@@ -183,7 +184,7 @@ void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array
getShadowWorldMatrix(sphere, node->getWorldRotation(), mainLight->getDirection(), &matShadowCamera, &tmpCenter);
const float radius = sphere->getRadius();
x = radius * shadowInfo->aspect;
x = radius;
y = radius;
const float halfFar = tmpCenter.distance(sphere->getCenter());
@@ -191,7 +192,7 @@ void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array
} else {
matShadowCamera = mainLight->getNode()->getWorldMatrix();
x = shadowInfo->orthoSize * shadowInfo->aspect;
x = shadowInfo->orthoSize;
y = shadowInfo->orthoSize;
farClamp = shadowInfo->farValue;
@@ -205,15 +206,14 @@ void PipelineUBO::updateShadowUBOView(const RenderPipeline *pipeline, std::array
matShadowViewProj.multiply(matShadowView);
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
const auto isSupportHalfFloat = supportsHalfFloatTexture(device);
const auto linear = (static_cast<bool>(shadowInfo->linear) && isSupportHalfFloat) ? 1.0F : 0.0F;
float shadowNFLSInfos[4] = {shadowInfo->nearValue, farClamp, linear, static_cast<float>(shadowInfo->selfShadow)};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SELF_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
const float linear = hFTexture ? 1.0F : 0.0F;
float shadowNFLSInfos[4] = {shadowInfo->nearValue, farClamp, linear, 1.0F - shadowInfo->saturation};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowWHPBInfos[4] = {shadowInfo->size.x, shadowInfo->size.y, static_cast<float>(shadowInfo->pcfType), shadowInfo->bias};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET, &shadowWHPBInfos, sizeof(shadowWHPBInfos));
const auto packing = static_cast<bool>(shadowInfo->packing) ? 1.0F : (isSupportHalfFloat ? 0.0F : 1.0F);
const float packing = hFTexture ? 0.0F : 1.0F;
float shadowLPNNInfos[4] = {0.0F, packing, shadowInfo->normalBias, 0.0F};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET, &shadowLPNNInfos, sizeof(shadowLPNNInfos));
} else if (mainLight && shadowInfo->shadowType == scene::ShadowType::PLANAR) {
@@ -231,9 +231,9 @@ void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::
auto * device = gfx::Device::getInstance();
auto * sphere = sceneData->getSphere();
auto & shadowUBO = *bufferView;
const auto isTextureHalfFloat = device->hasFeature(cc::gfx::Feature::TEXTURE_HALF_FLOAT);
const auto linear = (static_cast<bool>(shadowInfo->linear) && isTextureHalfFloat) ? 1.0F : 0.0F;
const auto packing = static_cast<bool>(shadowInfo->packing) ? 1.0F : (isTextureHalfFloat ? 0.0F : 1.0F);
const bool hFTexture = supportsHalfFloatTexture(device);
const float linear = hFTexture ? 1.0F : 0.0F;
const float packing = hFTexture ? 0.0F : 1.0F;
switch (light->getType()) {
case scene::LightType::DIRECTIONAL: {
const auto *directionalLight = static_cast<const scene::DirectionalLight *>(light);
@@ -247,7 +247,7 @@ void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::
getShadowWorldMatrix(sphere, directionalLight->getNode()->getWorldRotation(), directionalLight->getDirection(), &matShadowCamera, &tmpCenter);
const auto radius = sphere->getRadius();
x = radius * shadowInfo->aspect;
x = radius;
y = radius;
const float halfFar = tmpCenter.distance(sphere->getCenter());
@@ -255,7 +255,7 @@ void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::
} else {
matShadowCamera = light->getNode()->getWorldMatrix();
x = shadowInfo->orthoSize * shadowInfo->aspect;
x = shadowInfo->orthoSize;
y = shadowInfo->orthoSize;
farClamp = shadowInfo->farValue;
@@ -269,8 +269,8 @@ void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::
matShadowViewProj.multiply(matShadowView);
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
float shadowNFLSInfos[4] = {shadowInfo->nearValue, farClamp, linear, static_cast<float>(shadowInfo->selfShadow)};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SELF_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowNFLSInfos[4] = {shadowInfo->nearValue, farClamp, linear, 1.0F - shadowInfo->saturation};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowLPNNInfos[4] = {0.0F, packing, shadowInfo->normalBias, 0.0F};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET, &shadowLPNNInfos, sizeof(shadowLPNNInfos));
@@ -286,8 +286,8 @@ void PipelineUBO::updateShadowUBOLightView(const RenderPipeline *pipeline, std::
matShadowViewProj.multiply(matShadowView);
memcpy(shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
float shadowNFLSInfos[4] = {0.01F, spotLight->getRange(), linear, static_cast<float>(shadowInfo->selfShadow)};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SELF_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowNFLSInfos[4] = {0.01F, spotLight->getRange(), linear, 1.0F - shadowInfo->saturation};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowLPNNInfos[4] = {1.0F, packing, shadowInfo->normalBias, 0.0F};
memcpy(shadowUBO.data() + UBOShadow::SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET, &shadowLPNNInfos, sizeof(shadowLPNNInfos));
@@ -310,9 +310,9 @@ void RenderAdditiveLightQueue::updateLightDescriptorSet(const scene::Camera *cam
auto * shadowInfo = sceneData->getSharedData()->shadow;
const auto *const scene = camera->scene;
auto * device = gfx::Device::getInstance();
const auto isTextureHalfFloat = device->hasFeature(cc::gfx::Feature::TEXTURE_HALF_FLOAT);
const auto linear = (static_cast<bool>(shadowInfo->linear) && isTextureHalfFloat) ? 1.0F : 0.0F;
const auto packing = static_cast<bool>(shadowInfo->packing) ? 1.0F : (isTextureHalfFloat ? 0.0F : 1.0F);
const bool hFTexture = supportsHalfFloatTexture(device);
const float linear = hFTexture ? 1.0F : 0.0F;
const float packing = hFTexture ? 0.0F : 1.0F;
const scene::Light *mainLight = scene->getMainLight();
for (uint i = 0; i < _validLights.size(); ++i) {
@@ -358,8 +358,8 @@ void RenderAdditiveLightQueue::updateLightDescriptorSet(const scene::Camera *cam
memcpy(_shadowUBO.data() + UBOShadow::MAT_LIGHT_VIEW_PROJ_OFFSET, matShadowViewProj.m, sizeof(matShadowViewProj));
// shadow info
float shadowNFLSInfos[4] = {0.1F, spotLight->getRange(), linear, static_cast<float>(shadowInfo->selfShadow)};
memcpy(_shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SELF_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowNFLSInfos[4] = {0.1F, spotLight->getRange(), linear, 1.0F - shadowInfo->saturation};
memcpy(_shadowUBO.data() + UBOShadow::SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET, &shadowNFLSInfos, sizeof(shadowNFLSInfos));
float shadowWHPBInfos[4] = {shadowInfo->size.x, shadowInfo->size.y, static_cast<float>(shadowInfo->pcfType), shadowInfo->bias};
memcpy(_shadowUBO.data() + UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET, &shadowWHPBInfos, sizeof(shadowWHPBInfos));
@@ -74,6 +74,10 @@ void ShadowFlow::render(scene::Camera *camera) {
return;
}
if (shadowInfo->shadowMapDirty) {
resizeShadowMap(&shadowInfo);
}
const auto &shadowFramebufferMap = sceneData->getShadowFramebufferMap();
for (const auto *light : _validLights) {
if (!shadowFramebufferMap.count(light)) {
@@ -81,9 +85,7 @@ void ShadowFlow::render(scene::Camera *camera) {
}
auto *shadowFrameBuffer = shadowFramebufferMap.at(light);
if (shadowInfo->shadowMapDirty) {
resizeShadowMap(light, &shadowInfo);
}
for (auto *stage : _stages) {
auto *shadowStage = dynamic_cast<ShadowStage *>(stage);
shadowStage->setUseData(light, shadowFrameBuffer);
@@ -113,20 +115,18 @@ void ShadowFlow::clearShadowMap(scene::Camera *camera) {
}
}
void ShadowFlow::resizeShadowMap(const scene::Light *light, scene::Shadow **shadowInfo) {
void ShadowFlow::resizeShadowMap(scene::Shadow **shadowInfo) {
auto * sceneData = _pipeline->getPipelineSceneData();
auto * device = gfx::Device::getInstance();
const auto width = static_cast<uint>((*shadowInfo)->size.x);
const auto height = static_cast<uint>((*shadowInfo)->size.y);
const auto format = supportsHalfFloatTexture(device)
? ((*shadowInfo)->packing ? gfx::Format::RGBA8 : gfx::Format::RGBA16F)
: gfx::Format::RGBA8;
const auto format = supportsHalfFloatTexture(device) ? gfx::Format::R16F : gfx::Format::RGBA8;
if (sceneData->getShadowFramebufferMap().count(light)) {
auto *framebuffer = sceneData->getShadowFramebufferMap().at(light);
for (const auto &pair : sceneData->getShadowFramebufferMap()) {
gfx::Framebuffer *framebuffer = pair.second;
if (!framebuffer) {
return;
continue;
}
auto renderTargets = framebuffer->getColorTextures();
@@ -174,10 +174,8 @@ void ShadowFlow::initShadowFrameBuffer(RenderPipeline *pipeline, const scene::Li
const auto shadowMapSize = shadowInfo->size;
const auto width = static_cast<uint>(shadowMapSize.x);
const auto height = static_cast<uint>(shadowMapSize.y);
const auto format = supportsHalfFloatTexture(device)
? (shadowInfo->packing ? gfx::Format::RGBA8 : gfx::Format::RGBA16F)
: gfx::Format::RGBA8;
const auto format = supportsHalfFloatTexture(device) ? gfx::Format::R16F : gfx::Format::RGBA8;
const gfx::ColorAttachment colorAttachment = {
format,
gfx::SampleCount::X1,
@@ -51,7 +51,7 @@ public:
private:
void clearShadowMap(scene::Camera *camera);
void resizeShadowMap(const scene::Light *light, scene::Shadow **shadowInfo);
void resizeShadowMap(scene::Shadow **shadowInfo);
void initShadowFrameBuffer(RenderPipeline *pipeline, const scene::Light *light);
+16 -19
View File
@@ -41,12 +41,12 @@ class Pass;
struct Fog {
bool enabled{false};
uint32_t type{0};
float density{0};
float start{0};
float end{0};
float atten{0};
float top{0};
float range{0};
float density{0.0F};
float start{0.0F};
float end{0.0F};
float atten{0.0F};
float top{0.0F};
float range{0.0F};
Vec4 color;
};
@@ -69,21 +69,18 @@ struct Shadow {
bool enabled{false};
bool dirty{false};
bool shadowMapDirty{false};
bool selfShadow{false};
bool autoAdapt{false};
ShadowType shadowType{ShadowType::PLANAR};
float distance{0};
float distance{0.0F};
Pass * instancePass{nullptr};
Pass * planarPass{nullptr};
float nearValue{0};
float farValue{0};
float aspect{0};
float nearValue{0.0F};
float farValue{0.0F};
uint32_t pcfType{0};
float bias{0};
uint32_t packing{0};
uint32_t linear{0};
float normalBias{0};
float orthoSize{0};
float bias{0.0F};
float normalBias{0.0F};
float saturation{0.0F};
float orthoSize{0.0F};
Vec4 color;
Vec2 size;
@@ -100,15 +97,15 @@ struct Skybox {
struct Ambient {
bool enabled{false};
float skyIllum{0};
float skyIllum{0.0F};
Vec4 skyColor;
Vec4 groundAlbedo;
};
struct PipelineSharedSceneData {
bool isHDR{false};
float shadingScale{0};
float fpScale{0};
float shadingScale{0.0F};
float fpScale{0.0F};
Ambient * ambient{nullptr};
Shadow * shadow{nullptr};
Skybox * skybox{nullptr};