diff --git a/CHANGELOG.md b/CHANGELOG.md index 33a522df41..6e7034b9cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed + +- Make SSAO optional (#1396, **@tomas7770**). + ## [v0.5.0] - 2024-12-01 ### Added diff --git a/engine/assets/render/deferred_shading.fs b/engine/assets/render/deferred_shading.fs index 504dfc39ba..0f05dff3bc 100644 --- a/engine/assets/render/deferred_shading.fs +++ b/engine/assets/render/deferred_shading.fs @@ -78,6 +78,7 @@ layout(std140) uniform PerScene uint numSpotLights; int directionalLightWithShadowsId; // index of directional light that casts shadows, or -1 if none + int useSSAO; }; layout(location = 0) out vec3 color; @@ -318,7 +319,7 @@ void main() { vec3 albedo = texture(albedoTexture, uv).rgb; vec3 position = texture(positionTexture, uv).xyz; - float ssao = texture(ssaoTexture, uv).r; + float ssao = useSSAO == 1 ? texture(ssaoTexture, uv).r : 1.0; // Calculate lighting from each light source. vec3 lighting = ambientLight.rgb * ssao; diff --git a/engine/include/cubos/engine/render/defaults/target.hpp b/engine/include/cubos/engine/render/defaults/target.hpp index e45531ca85..73c0c68ef3 100644 --- a/engine/include/cubos/engine/render/defaults/target.hpp +++ b/engine/include/cubos/engine/render/defaults/target.hpp @@ -22,11 +22,14 @@ #include #include - +// Export types so that code using the engine shared library can share reflection data, +// otherwise we may end up with e.g. a Opt type in the engine +// and another one in the external code. namespace cubos::core::memory { CUBOS_ENGINE_EXTERN template class CUBOS_ENGINE_API Opt; CUBOS_ENGINE_EXTERN template class CUBOS_ENGINE_API Opt; + CUBOS_ENGINE_EXTERN template class CUBOS_ENGINE_API Opt; } // namespace cubos::core::memory namespace cubos::engine @@ -56,7 +59,7 @@ namespace cubos::engine GBufferRasterizer gBufferRasterizer{}; /// @brief SSAO component. - SSAO ssao{}; + core::memory::Opt ssao{SSAO{}}; /// @brief Tone Mapping component. ToneMapping toneMapping{}; diff --git a/engine/src/render/defaults/plugin.cpp b/engine/src/render/defaults/plugin.cpp index 187536279e..1a66371a03 100644 --- a/engine/src/render/defaults/plugin.cpp +++ b/engine/src/render/defaults/plugin.cpp @@ -74,7 +74,6 @@ void cubos::engine::renderDefaultsPlugin(Cubos& cubos) .add(entity, defaults.picker) .add(entity, defaults.depth) .add(entity, defaults.gBufferRasterizer) - .add(entity, defaults.ssao) .add(entity, defaults.toneMapping) .add(entity, defaults.fxaa) .add(entity, defaults.deferredShading); @@ -88,6 +87,11 @@ void cubos::engine::renderDefaultsPlugin(Cubos& cubos) { cmds.add(entity, defaults.bloom.value()); } + + if (defaults.ssao) + { + cmds.add(entity, defaults.ssao.value()); + } } }); } diff --git a/engine/src/render/defaults/target.cpp b/engine/src/render/defaults/target.cpp index 75ddcd050e..b6e96adbe7 100644 --- a/engine/src/render/defaults/target.cpp +++ b/engine/src/render/defaults/target.cpp @@ -5,6 +5,7 @@ template class cubos::core::memory::Opt; template class cubos::core::memory::Opt; +template class cubos::core::memory::Opt; CUBOS_REFLECT_IMPL(cubos::engine::RenderTargetDefaults) { diff --git a/engine/src/render/deferred_shading/plugin.cpp b/engine/src/render/deferred_shading/plugin.cpp index a5f642a012..c06a73d9b9 100644 --- a/engine/src/render/deferred_shading/plugin.cpp +++ b/engine/src/render/deferred_shading/plugin.cpp @@ -116,6 +116,7 @@ namespace glm::uint numSpotLights{0}; int directionalLightWithShadowsId; + int useSSAO; }; struct State @@ -218,7 +219,7 @@ void cubos::engine::deferredShadingPlugin(Cubos& cubos) directionalLights, Query> pointLights, Query> spotLights, - Query targets, + Query, DeferredShading&> targets, Query cameras) { auto& rd = window->renderDevice(); @@ -441,7 +442,15 @@ void cubos::engine::deferredShadingPlugin(Cubos& cubos) state.positionBP->bind(gBuffer.position); state.normalBP->bind(gBuffer.normal); state.albedoBP->bind(gBuffer.albedo); - state.ssaoBP->bind(ssao.blurTexture); + if (ssao.contains()) + { + state.ssaoBP->bind(ssao.value().blurTexture); + perScene.useSSAO = 1; + } + else + { + perScene.useSSAO = 0; + } state.spotShadowAtlasBP->bind(spotShadowAtlas.atlas); state.pointShadowAtlasBP->bind(pointShadowAtlas.atlas); // directionalShadowMap needs to be bound even if it's null, or else errors may occur on some GPUs