mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-22 10:29:47 +00:00
optimize ssao pass
This commit is contained in:
parent
25cee0284f
commit
2f87bbf608
5 changed files with 39 additions and 24 deletions
|
|
@ -3,22 +3,27 @@
|
||||||
|
|
||||||
#define CLOUDS_FOG_FACTOR_MUL 0.3f
|
#define CLOUDS_FOG_FACTOR_MUL 0.3f
|
||||||
#define CLOUDS_FOG_CURVE_MUL 0.4f
|
#define CLOUDS_FOG_CURVE_MUL 0.4f
|
||||||
#define COLOR_TEMPERATURE_COMPENSATION 1.12f
|
#define COLOR_TEMPERATURE_COMPENSATION 1.14f
|
||||||
|
|
||||||
vec4 effect() {
|
vec4 effect() {
|
||||||
vec4 pos = texture(u_position, v_uv);
|
vec4 pos = texture(u_position, v_uv);
|
||||||
float light = 1.0;
|
float light = 1.0;
|
||||||
|
|
||||||
#ifdef ENABLE_SSAO
|
#ifdef ENABLE_SSAO
|
||||||
light = 0.0;
|
vec2 texel = 2.0 / u_screenSize;
|
||||||
float z = pos.z;
|
float centerZ = pos.z;
|
||||||
for (int y = -2; y <= 2; y++) {
|
float sum = 0.0;
|
||||||
for (int x = -2; x <= 2; x++) {
|
float wsum = 0.0;
|
||||||
vec2 offset = vec2(x, y) / u_screenSize;
|
for (int y = -2; y < 2; y++) {
|
||||||
light += texture(u_ssao, v_uv + offset * 2.0).r;
|
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
|
#endif // ENABLE_SSAO
|
||||||
|
|
||||||
vec4 modelpos = u_inverseView * pos;
|
vec4 modelpos = u_inverseView * pos;
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ static debug::Logger logger("gl-gbuffer");
|
||||||
|
|
||||||
// TODO: REFACTOR
|
// TODO: REFACTOR
|
||||||
|
|
||||||
|
uint GBuffer::getSSAODownsample() const {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
void GBuffer::createColorBuffer() {
|
void GBuffer::createColorBuffer() {
|
||||||
if (colorBuffer == 0)
|
if (colorBuffer == 0)
|
||||||
glGenTextures(1, &colorBuffer);
|
glGenTextures(1, &colorBuffer);
|
||||||
|
|
@ -117,15 +121,17 @@ void GBuffer::createDepthBuffer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void GBuffer::createSSAOBuffer() {
|
void GBuffer::createSSAOBuffer() {
|
||||||
if (ssaoBuffer == 0)
|
if (ssaoBuffer == 0) {
|
||||||
glGenTextures(1, &ssaoBuffer);
|
glGenTextures(1, &ssaoBuffer);
|
||||||
|
}
|
||||||
|
uint ssaoDownsample = getSSAODownsample();
|
||||||
glBindTexture(GL_TEXTURE_2D, ssaoBuffer);
|
glBindTexture(GL_TEXTURE_2D, ssaoBuffer);
|
||||||
glTexImage2D(
|
glTexImage2D(
|
||||||
GL_TEXTURE_2D,
|
GL_TEXTURE_2D,
|
||||||
0,
|
0,
|
||||||
GL_R16F,
|
GL_R16F,
|
||||||
width,
|
width / ssaoDownsample,
|
||||||
height,
|
height / ssaoDownsample,
|
||||||
0,
|
0,
|
||||||
GL_RED,
|
GL_RED,
|
||||||
GL_FLOAT,
|
GL_FLOAT,
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,8 @@ public:
|
||||||
uint getHeight() const;
|
uint getHeight() const;
|
||||||
|
|
||||||
std::unique_ptr<ImageData> toImage() const;
|
std::unique_ptr<ImageData> toImage() const;
|
||||||
|
|
||||||
|
uint getSSAODownsample() const;
|
||||||
private:
|
private:
|
||||||
uint width;
|
uint width;
|
||||||
uint height;
|
uint height;
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,8 @@ PostProcessing::PostProcessing(size_t effectSlotsCount)
|
||||||
quadMesh = std::make_unique<Mesh<PostProcessingVertex>>(meshData, 6);
|
quadMesh = std::make_unique<Mesh<PostProcessingVertex>>(meshData, 6);
|
||||||
|
|
||||||
std::vector<glm::vec3> ssaoNoise;
|
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(
|
glm::vec3 noise(
|
||||||
(rand() / static_cast<float>(RAND_MAX)) * 2.0 - 1.0,
|
(rand() / static_cast<float>(RAND_MAX)) * 2.0 - 1.0,
|
||||||
|
|
@ -41,7 +42,7 @@ PostProcessing::PostProcessing(size_t effectSlotsCount)
|
||||||
}
|
}
|
||||||
glGenTextures(1, &noiseTexture);
|
glGenTextures(1, &noiseTexture);
|
||||||
glBindTexture(GL_TEXTURE_2D, 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_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
|
@ -89,7 +90,8 @@ void PostProcessing::configureEffect(
|
||||||
PostEffect& effect,
|
PostEffect& effect,
|
||||||
Shader& shader,
|
Shader& shader,
|
||||||
float timer,
|
float timer,
|
||||||
const Camera& camera
|
const Camera& camera,
|
||||||
|
unsigned int downsample
|
||||||
) {
|
) {
|
||||||
const auto& viewport = context.getViewport();
|
const auto& viewport = context.getViewport();
|
||||||
shader.uniform1i("u_screen", TARGET_COLOR);
|
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_noise", TARGET_SSAO); // used in SSAO pass
|
||||||
shader.uniform1i("u_ssao", TARGET_SSAO);
|
shader.uniform1i("u_ssao", TARGET_SSAO);
|
||||||
shader.uniform2i("u_screenSize", viewport);
|
shader.uniform2i("u_screenSize", viewport / downsample);
|
||||||
shader.uniform3f("u_cameraPos", camera.position);
|
shader.uniform3f("u_cameraPos", camera.position);
|
||||||
shader.uniform1f("u_timer", timer);
|
shader.uniform1f("u_timer", timer);
|
||||||
shader.uniformMatrix("u_projection", camera.getProjection());
|
shader.uniformMatrix("u_projection", camera.getProjection());
|
||||||
|
|
@ -128,15 +130,14 @@ void PostProcessing::renderDeferredShading(
|
||||||
|
|
||||||
auto& ssaoEffect = assets.require<PostEffect>("ssao");
|
auto& ssaoEffect = assets.require<PostEffect>("ssao");
|
||||||
auto& shader = ssaoEffect.use();
|
auto& shader = ssaoEffect.use();
|
||||||
configureEffect(
|
uint ssaoDownscale = gbuffer->getSSAODownsample();
|
||||||
context,
|
configureEffect(context, ssaoEffect, shader, timer, camera, ssaoDownscale);
|
||||||
ssaoEffect,
|
|
||||||
shader,
|
|
||||||
timer,
|
|
||||||
camera
|
|
||||||
);
|
|
||||||
gbuffer->bindSSAO();
|
gbuffer->bindSSAO();
|
||||||
quadMesh->draw();
|
{
|
||||||
|
auto ssaoContext = context.sub();
|
||||||
|
ssaoContext.setViewport(context.getViewport() / ssaoDownscale);
|
||||||
|
quadMesh->draw();
|
||||||
|
}
|
||||||
gbuffer->unbind();
|
gbuffer->unbind();
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,8 @@ private:
|
||||||
PostEffect& effect,
|
PostEffect& effect,
|
||||||
Shader& shader,
|
Shader& shader,
|
||||||
float timer,
|
float timer,
|
||||||
const Camera& camera
|
const Camera& camera,
|
||||||
|
unsigned downsample = 1U
|
||||||
);
|
);
|
||||||
|
|
||||||
void refreshFbos(uint width, uint height);
|
void refreshFbos(uint width, uint height);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue