Skip to content

Commit

Permalink
postprocessing: ssao: have shader parameters here
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasdr committed Oct 24, 2023
1 parent 9d75292 commit ae9016d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
14 changes: 7 additions & 7 deletions shader/gl2/postprocessing/ssao_map_fragmentshader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
precision highp sampler2D;
precision highp float;

#define strength 0.25
#define area 0.0075
#define falloff 0.000001
#define radius 0.02
#define samples 8

// uniforms
uniform sampler2D colorBufferTextureUnit;
uniform sampler2D depthBufferTextureUnit;
uniform sampler2D randomTextureUnit;
uniform float bufferTexturePixelWidth;
uniform float bufferTexturePixelHeight;
uniform vec3 sphere[samples];
uniform vec3 sphere[16];

uniform float strength;
uniform float area;
uniform float falloff;
uniform float radius;
uniform int samples;

// passed from vertex shader
varying vec2 vsFragTextureUV;
Expand Down
14 changes: 7 additions & 7 deletions shader/gl3/postprocessing/ssao_map_fragmentshader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

#version 330 core

#define strength 0.25
#define area 0.0075
#define falloff 0.000001
#define radius 0.02
#define samples 16

// uniforms
uniform sampler2D colorBufferTextureUnit;
uniform sampler2D depthBufferTextureUnit;
uniform sampler2D randomTextureUnit;
uniform float bufferTexturePixelWidth;
uniform float bufferTexturePixelHeight;
uniform vec3 sphere[samples];
uniform vec3 sphere[16];

uniform float strength;
uniform float area;
uniform float falloff;
uniform float radius;
uniform int samples;

// passed from vertex shader
in vec2 vsFragTextureUV;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ void PostProcessingShaderSSAOImplementation::initialize()
if (initialized == false) return;

//
Engine::registerShader(Engine::ShaderType::SHADERTYPE_POSTPROCESSING, "ssao");
Engine::registerShader(
Engine::ShaderType::SHADERTYPE_POSTPROCESSING,
"ssao",
{
{ "strength", ShaderParameter(0.25f), ShaderParameter(0.0f), ShaderParameter(1.0f), ShaderParameter(0.05f) },
{ "area", ShaderParameter(0.0075f), ShaderParameter(0.0f), ShaderParameter(1.0f), ShaderParameter(0.05f) },
{ "falloff", ShaderParameter(0.1f), ShaderParameter(0.0f), ShaderParameter(1.0f), ShaderParameter(0.05f) },
{ "radius", ShaderParameter(0.02f), ShaderParameter(0.0f), ShaderParameter(1.0f), ShaderParameter(0.05f) },
{ "samples", ShaderParameter(8), ShaderParameter(1), ShaderParameter(16), ShaderParameter(1) }

}
);
}

void PostProcessingShaderSSAOImplementation::setShaderParameters(int contextIdx, Engine* engine) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,20 @@ void PostProcessingShaderSSAOMapImplementation::initialize()
// custom initialization
for (auto i = 0; i < uniformSphere.size(); i++) {
uniformSphere[i] = renderer->getProgramUniformLocation(programId, "sphere[" + to_string(i) + "]");
if (uniformSphere[i] == -1) {
initialized = false;
return;
}
if (uniformSphere[i] == -1) return;
}
uniformRandomTextureUnit = renderer->getProgramUniformLocation(programId, "randomTextureUnit");
if (uniformRandomTextureUnit == -1) {
initialized = false;
return;
}
if (uniformRandomTextureUnit == -1) return;
uniformStrength = renderer->getProgramUniformLocation(programId, "strength");
if (uniformStrength == -1) return;
uniformArea = renderer->getProgramUniformLocation(programId, "area");
if (uniformArea == -1) return;
uniformFallOff = renderer->getProgramUniformLocation(programId, "falloff");
if (uniformFallOff == -1) return;
uniformRadius = renderer->getProgramUniformLocation(programId, "radius");
if (uniformRadius == -1) return;
uniformSamples = renderer->getProgramUniformLocation(programId, "samples");
if (uniformSamples == -1) return;

//
loadTextures(".");
Expand Down Expand Up @@ -107,6 +111,11 @@ void PostProcessingShaderSSAOMapImplementation::useProgram(int contextIdx) {
}

void PostProcessingShaderSSAOMapImplementation::setShaderParameters(int contextIdx, Engine* engine) {
renderer->setProgramUniformFloat(contextIdx, uniformStrength, engine->getShaderParameter("ssao", "strength").getFloatValue());
renderer->setProgramUniformFloat(contextIdx, uniformArea, engine->getShaderParameter("ssao", "area").getFloatValue());
renderer->setProgramUniformFloat(contextIdx, uniformFallOff, engine->getShaderParameter("ssao", "falloff").getFloatValue() / 100000.0f);
renderer->setProgramUniformFloat(contextIdx, uniformRadius, engine->getShaderParameter("ssao", "radius").getFloatValue());
renderer->setProgramUniformInteger(contextIdx, uniformSamples, engine->getShaderParameter("ssao", "samples").getIntegerValue());
}

void PostProcessingShaderSSAOMapImplementation::unloadTextures() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class tdme::engine::subsystems::postprocessing::PostProcessingShaderSSAOMapImple
private:
array<int32_t, 16> uniformSphere;
int32_t uniformRandomTextureUnit { -1 };
int32_t uniformStrength { -1 };
int32_t uniformArea { -1 };
int32_t uniformFallOff { -1 };
int32_t uniformRadius { -1 };
int32_t uniformSamples { -1 };
Texture* randomTexture { nullptr };
int32_t randomTextureId { -1 };
};

0 comments on commit ae9016d

Please sign in to comment.