Skip to content

Commit

Permalink
feat(rendering): make SSAO optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7770 committed Dec 14, 2024
1 parent 9d530fe commit 395ff0d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion engine/assets/render/deferred_shading.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions engine/include/cubos/engine/render/defaults/target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
#include <cubos/engine/render/tone_mapping/fxaa.hpp>
#include <cubos/engine/render/tone_mapping/tone_mapping.hpp>


// Export types so that code using the engine shared library can share reflection data,
// otherwise we may end up with e.g. a Opt<cubos::engine::SplitScreen> 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::SplitScreen>;
CUBOS_ENGINE_EXTERN template class CUBOS_ENGINE_API Opt<cubos::engine::Bloom>;
CUBOS_ENGINE_EXTERN template class CUBOS_ENGINE_API Opt<cubos::engine::SSAO>;
} // namespace cubos::core::memory

namespace cubos::engine
Expand Down Expand Up @@ -56,7 +59,7 @@ namespace cubos::engine
GBufferRasterizer gBufferRasterizer{};

/// @brief SSAO component.
SSAO ssao{};
core::memory::Opt<SSAO> ssao{SSAO{}};

/// @brief Tone Mapping component.
ToneMapping toneMapping{};
Expand Down
6 changes: 5 additions & 1 deletion engine/src/render/defaults/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
}
});
}
1 change: 1 addition & 0 deletions engine/src/render/defaults/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

template class cubos::core::memory::Opt<cubos::engine::SplitScreen>;
template class cubos::core::memory::Opt<cubos::engine::Bloom>;
template class cubos::core::memory::Opt<cubos::engine::SSAO>;

CUBOS_REFLECT_IMPL(cubos::engine::RenderTargetDefaults)
{
Expand Down
13 changes: 11 additions & 2 deletions engine/src/render/deferred_shading/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ namespace
glm::uint numSpotLights{0};

int directionalLightWithShadowsId;
int useSSAO;
};

struct State
Expand Down Expand Up @@ -218,7 +219,7 @@ void cubos::engine::deferredShadingPlugin(Cubos& cubos)
directionalLights,
Query<const LocalToWorld&, const PointLight&, Opt<const PointShadowCaster&>> pointLights,
Query<const LocalToWorld&, const SpotLight&, Opt<const SpotShadowCaster&>> spotLights,
Query<Entity, const HDR&, const GBuffer&, const SSAO&, DeferredShading&> targets,
Query<Entity, const HDR&, const GBuffer&, Opt<const SSAO&>, DeferredShading&> targets,
Query<Entity, const LocalToWorld&, const Camera&, const DrawsTo&> cameras) {
auto& rd = window->renderDevice();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 395ff0d

Please sign in to comment.