Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSAO resolution scale #1424

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
ShaderBindingPoint blurSSAOBP;
ShaderBindingPoint blurViewportOffsetBP;
ShaderBindingPoint blurViewportSizeBP;
ShaderBindingPoint blurResolutionScaleBP;
VertexArray blurScreenQuad;

ConstantBuffer perSceneCB;
Expand All @@ -86,8 +88,9 @@
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,

Check warning on line 92 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L91-L92

Added lines #L91 - L92 were not covered by tests
"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 @@
}

// 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;

Check warning on line 207 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L206-L207

Added lines #L206 - L207 were not covered by tests
}
else if (ssao.resolutionScale <= 0.0F)

Check warning on line 209 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L209

Added line #L209 was not covered by tests
{
CUBOS_WARN("SSAO resolution scale must be positive, setting to 0.5");
ssao.resolutionScale = 0.5F;

Check warning on line 212 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L211-L212

Added lines #L211 - L212 were not covered by tests
}
auto ssaoConfigSize = glm::uvec2(float(gBuffer.size.x) * ssao.resolutionScale,
float(gBuffer.size.y) * ssao.resolutionScale);

Check warning on line 215 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L214-L215

Added lines #L214 - L215 were not covered by tests
if (ssao.size != ssaoConfigSize)
{
ssao.size = ssaoConfigSize;

Check warning on line 218 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L218

Added line #L218 was not covered by tests

// 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;

Check warning on line 224 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L223-L224

Added lines #L223 - L224 were not covered by tests
desc.usage = Usage::Dynamic;

// Create base and blur textures.
Expand Down Expand Up @@ -268,6 +284,7 @@
state.blurSSAOBP->bind(ssao.baseTexture);
state.blurViewportOffsetBP->setConstant(drawsTo.viewportOffset);
state.blurViewportSizeBP->setConstant(drawsTo.viewportSize);
state.blurResolutionScaleBP->setConstant(ssao.resolutionScale);

Check warning on line 287 in engine/src/render/ssao/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/plugin.cpp#L287

Added line #L287 was not covered by tests
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 @@
{
return core::ecs::TypeBuilder<SSAO>("cubos::engine::SSAO")
.withField("size", &SSAO::size)
.withField("resolutionScale", &SSAO::resolutionScale)

Check warning on line 11 in engine/src/render/ssao/ssao.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/render/ssao/ssao.cpp#L11

Added line #L11 was not covered by tests
.withField("samples", &SSAO::samples)
.withField("radius", &SSAO::radius)
.withField("bias", &SSAO::bias)
Expand Down
Loading