optimize ssao pass

This commit is contained in:
MihailRis 2026-10-06 18:41:26 +03:00
parent 25cee0284f
commit 2f87bbf608
5 changed files with 39 additions and 24 deletions

View file

@ -3,22 +3,27 @@
#define CLOUDS_FOG_FACTOR_MUL 0.3f
#define CLOUDS_FOG_CURVE_MUL 0.4f
#define COLOR_TEMPERATURE_COMPENSATION 1.12f
#define COLOR_TEMPERATURE_COMPENSATION 1.14f
vec4 effect() {
vec4 pos = texture(u_position, v_uv);
float light = 1.0;
#ifdef ENABLE_SSAO
light = 0.0;
float z = pos.z;
for (int y = -2; y <= 2; y++) {
for (int x = -2; x <= 2; x++) {
vec2 offset = vec2(x, y) / u_screenSize;
light += texture(u_ssao, v_uv + offset * 2.0).r;
vec2 texel = 2.0 / u_screenSize;
float centerZ = pos.z;
float sum = 0.0;
float wsum = 0.0;
for (int y = -2; y < 2; y++) {
for (int x = -2; x < 2; x++) {
vec2 uv = v_uv + vec2(x, y) * texel;
float z = texture(u_position, uv).z;
float w = exp(-abs(centerZ - z) / (abs(centerZ) * 0.05 + 0.001));
sum += texture(u_ssao, uv).r * w;
wsum += w;
}
}
light /= 24.0;
light = sum / wsum;
#endif // ENABLE_SSAO
vec4 modelpos = u_inverseView * pos;

View file

@ -10,6 +10,10 @@ static debug::Logger logger("gl-gbuffer");
// TODO: REFACTOR
uint GBuffer::getSSAODownsample() const {
return 2;
}
void GBuffer::createColorBuffer() {
if (colorBuffer == 0)
glGenTextures(1, &colorBuffer);
@ -117,15 +121,17 @@ void GBuffer::createDepthBuffer() {
}
void GBuffer::createSSAOBuffer() {
if (ssaoBuffer == 0)
if (ssaoBuffer == 0) {
glGenTextures(1, &ssaoBuffer);
}
uint ssaoDownsample = getSSAODownsample();
glBindTexture(GL_TEXTURE_2D, ssaoBuffer);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_R16F,
width,
height,
width / ssaoDownsample,
height / ssaoDownsample,
0,
GL_RED,
GL_FLOAT,

View file

@ -24,6 +24,8 @@ public:
uint getHeight() const;
std::unique_ptr<ImageData> toImage() const;
uint getSSAODownsample() const;
private:
uint width;
uint height;

View file

@ -31,7 +31,8 @@ PostProcessing::PostProcessing(size_t effectSlotsCount)
quadMesh = std::make_unique<Mesh<PostProcessingVertex>>(meshData, 6);
std::vector<glm::vec3> ssaoNoise;
for (unsigned int i = 0; i < 16; i++)
int res = 4;
for (unsigned int i = 0; i < res*res; i++)
{
glm::vec3 noise(
(rand() / static_cast<float>(RAND_MAX)) * 2.0 - 1.0,
@ -41,7 +42,7 @@ PostProcessing::PostProcessing(size_t effectSlotsCount)
}
glGenTextures(1, &noiseTexture);
glBindTexture(GL_TEXTURE_2D, noiseTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, 4, 4, 0, GL_RGB, GL_FLOAT, ssaoNoise.data());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, res, res, 0, GL_RGB, GL_FLOAT, ssaoNoise.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -89,7 +90,8 @@ void PostProcessing::configureEffect(
PostEffect& effect,
Shader& shader,
float timer,
const Camera& camera
const Camera& camera,
unsigned int downsample
) {
const auto& viewport = context.getViewport();
shader.uniform1i("u_screen", TARGET_COLOR);
@ -101,7 +103,7 @@ void PostProcessing::configureEffect(
}
shader.uniform1i("u_noise", TARGET_SSAO); // used in SSAO pass
shader.uniform1i("u_ssao", TARGET_SSAO);
shader.uniform2i("u_screenSize", viewport);
shader.uniform2i("u_screenSize", viewport / downsample);
shader.uniform3f("u_cameraPos", camera.position);
shader.uniform1f("u_timer", timer);
shader.uniformMatrix("u_projection", camera.getProjection());
@ -128,15 +130,14 @@ void PostProcessing::renderDeferredShading(
auto& ssaoEffect = assets.require<PostEffect>("ssao");
auto& shader = ssaoEffect.use();
configureEffect(
context,
ssaoEffect,
shader,
timer,
camera
);
uint ssaoDownscale = gbuffer->getSSAODownsample();
configureEffect(context, ssaoEffect, shader, timer, camera, ssaoDownscale);
gbuffer->bindSSAO();
quadMesh->draw();
{
auto ssaoContext = context.sub();
ssaoContext.setViewport(context.getViewport() / ssaoDownscale);
quadMesh->draw();
}
gbuffer->unbind();
{

View file

@ -68,7 +68,8 @@ private:
PostEffect& effect,
Shader& shader,
float timer,
const Camera& camera
const Camera& camera,
unsigned downsample = 1U
);
void refreshFbos(uint width, uint height);