Skip to content

Commit

Permalink
feat(rendering): add SSAO resolution scale
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7770 committed Jan 10, 2025
1 parent 395ff0d commit da5f5e3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- SSAO resolution scale (#1423, **@tomas7770**).

### Changed

- Make SSAO optional (#1396, **@tomas7770**).
Expand Down
4 changes: 3 additions & 1 deletion engine/assets/render/ssao_blur.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ uniform sampler2D ssaoTexture;
uniform vec2 viewportOffset;
uniform vec2 viewportSize;

uniform float resolutionScale;

layout (location = 0) out float color;

void main() {
vec2 uv = fragUv * viewportSize + viewportOffset;
vec2 texelSize = 1.0 / vec2(textureSize(ssaoTexture, 0));
vec2 texelSize = resolutionScale / vec2(textureSize(ssaoTexture, 0));
float result = 0.0;
for (int x = -2; x < 2; ++x)
{
Expand Down
3 changes: 3 additions & 0 deletions engine/include/cubos/engine/render/ssao/ssao.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace cubos::engine
/// @brief Size of the SSAO textures, in pixels.
glm::uvec2 size = {0, 0};

/// @brief Scale value to configure SSAO texture size.
float resolutionScale = 0.5F;

/// @brief Number of samples to use.
int samples = 64;

Expand Down
29 changes: 23 additions & 6 deletions engine/src/render/ssao/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using cubos::core::io::Window;
CUBOS_DEFINE_TAG(cubos::engine::drawToSSAOTag);

#define MAX_KERNEL_SIZE 64
#define MAX_SSAO_RESOLUTION 2.0F

namespace
{
Expand Down Expand Up @@ -60,6 +61,7 @@ namespace
ShaderBindingPoint blurSSAOBP;
ShaderBindingPoint blurViewportOffsetBP;
ShaderBindingPoint blurViewportSizeBP;
ShaderBindingPoint blurResolutionScaleBP;
VertexArray blurScreenQuad;

ConstantBuffer perSceneCB;
Expand All @@ -86,8 +88,9 @@ namespace
blurSSAOBP = blurPipeline->getBindingPoint("ssaoTexture");
blurViewportOffsetBP = blurPipeline->getBindingPoint("viewportOffset");
blurViewportSizeBP = blurPipeline->getBindingPoint("viewportSize");
CUBOS_ASSERT(blurSSAOBP && blurViewportOffsetBP && blurViewportSizeBP,
"ssaoTexture, viewportOffset and viewportSize binding points must exist");
blurResolutionScaleBP = blurPipeline->getBindingPoint("resolutionScale");
CUBOS_ASSERT(blurSSAOBP && blurViewportOffsetBP && blurViewportSizeBP && blurResolutionScaleBP,
"ssaoTexture, viewportOffset, viewportSize and resolutionScale binding points must exist");
generateScreenQuad(renderDevice, blurPipeline, blurScreenQuad);

perSceneCB = renderDevice.createConstantBuffer(sizeof(PerScene), nullptr, Usage::Dynamic);
Expand Down Expand Up @@ -197,15 +200,28 @@ void cubos::engine::ssaoPlugin(Cubos& cubos)
}

// Check if we need to resize the SSAO textures.
if (ssao.size != gBuffer.size)
if (ssao.resolutionScale > MAX_SSAO_RESOLUTION)
{
ssao.size = gBuffer.size;
// There's probably not much benefit to a scale larger than 1.0, so don't allow very large scale
CUBOS_WARN("SSAO resolution scale is too large, clamping to {}", MAX_SSAO_RESOLUTION);
ssao.resolutionScale = MAX_SSAO_RESOLUTION;
}
else if (ssao.resolutionScale <= 0.0F)
{
CUBOS_WARN("SSAO resolution scale must be positive, setting to 0.5");
ssao.resolutionScale = 0.5F;
}
auto ssaoConfigSize = glm::uvec2(float(gBuffer.size.x) * ssao.resolutionScale,
float(gBuffer.size.y) * ssao.resolutionScale);
if (ssao.size != ssaoConfigSize)
{
ssao.size = ssaoConfigSize;

// Prepare common texture description.
Texture2DDesc desc{};
desc.format = TextureFormat::R32Float;
desc.width = gBuffer.size.x;
desc.height = gBuffer.size.y;
desc.width = ssao.size.x;
desc.height = ssao.size.y;
desc.usage = Usage::Dynamic;

// Create base and blur textures.
Expand Down Expand Up @@ -268,6 +284,7 @@ void cubos::engine::ssaoPlugin(Cubos& cubos)
state.blurSSAOBP->bind(ssao.baseTexture);
state.blurViewportOffsetBP->setConstant(drawsTo.viewportOffset);
state.blurViewportSizeBP->setConstant(drawsTo.viewportSize);
state.blurResolutionScaleBP->setConstant(ssao.resolutionScale);
rd.setVertexArray(state.blurScreenQuad);
rd.drawTriangles(0, 6);
}
Expand Down
1 change: 1 addition & 0 deletions engine/src/render/ssao/ssao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CUBOS_REFLECT_IMPL(cubos::engine::SSAO)
{
return core::ecs::TypeBuilder<SSAO>("cubos::engine::SSAO")
.withField("size", &SSAO::size)
.withField("resolutionScale", &SSAO::resolutionScale)
.withField("samples", &SSAO::samples)
.withField("radius", &SSAO::radius)
.withField("bias", &SSAO::bias)
Expand Down

0 comments on commit da5f5e3

Please sign in to comment.