diff --git a/native/cocos/renderer/pipeline/Define.h b/native/cocos/renderer/pipeline/Define.h index bb9629b25a..0b2c111ef5 100644 --- a/native/cocos/renderer/pipeline/Define.h +++ b/native/cocos/renderer/pipeline/Define.h @@ -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(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(PipelineGlobalBindings::UBO_SHADOW); static const gfx::DescriptorSetLayoutBinding DESCRIPTOR; static const gfx::UniformBlock LAYOUT; static const String NAME; diff --git a/native/cocos/renderer/pipeline/PipelineUBO.cpp b/native/cocos/renderer/pipeline/PipelineUBO.cpp index a4509d5fd0..9117bdc61a 100644 --- a/native/cocos/renderer/pipeline/PipelineUBO.cpp +++ b/native/cocos/renderer/pipeline/PipelineUBO.cpp @@ -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(shadowInfo->linear) && isSupportHalfFloat) ? 1.0F : 0.0F; - float shadowNFLSInfos[4] = {shadowInfo->nearValue, farClamp, linear, static_cast(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(shadowInfo->pcfType), shadowInfo->bias}; memcpy(shadowUBO.data() + UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET, &shadowWHPBInfos, sizeof(shadowWHPBInfos)); - const auto packing = static_cast(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(shadowInfo->linear) && isTextureHalfFloat) ? 1.0F : 0.0F; - const auto packing = static_cast(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(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(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(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)); diff --git a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp index 25c14435ef..b262748d5f 100644 --- a/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp +++ b/native/cocos/renderer/pipeline/RenderAdditiveLightQueue.cpp @@ -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(shadowInfo->linear) && isTextureHalfFloat) ? 1.0F : 0.0F; - const auto packing = static_cast(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(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(shadowInfo->pcfType), shadowInfo->bias}; memcpy(_shadowUBO.data() + UBOShadow::SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET, &shadowWHPBInfos, sizeof(shadowWHPBInfos)); diff --git a/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp b/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp index 26cd7338c8..8867636b31 100644 --- a/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp +++ b/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp @@ -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(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((*shadowInfo)->size.x); const auto height = static_cast((*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(shadowMapSize.x); const auto height = static_cast(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, diff --git a/native/cocos/renderer/pipeline/shadow/ShadowFlow.h b/native/cocos/renderer/pipeline/shadow/ShadowFlow.h index 0f6750cd6a..5fa9ed6ce4 100644 --- a/native/cocos/renderer/pipeline/shadow/ShadowFlow.h +++ b/native/cocos/renderer/pipeline/shadow/ShadowFlow.h @@ -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); diff --git a/native/cocos/scene/Define.h b/native/cocos/scene/Define.h index 625988dab4..e57c4f2462 100644 --- a/native/cocos/scene/Define.h +++ b/native/cocos/scene/Define.h @@ -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};