Skip to content

Commit

Permalink
Improved auto-exposure and bloom
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMoroz committed Sep 15, 2019
1 parent 08a31ba commit 7fc03da
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion game_folder/shaders/compute/Bloom_horiz.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void main() {

vec3 conv1 = vec3(0), conv2 = vec3(0);
int center = local_indx.x+group_size/2;
for(int i = max(center-int(width*5),0); i < min(center+int(width*5),buffer_size); i++)
for(int i = max(center-int(width*15),0); i < min(center+int(width*15),buffer_size); i++)
{
float coord = float(center-i)/width;
conv1 += exp(-k1*pow(coord,2))*color_buffer[i];
Expand Down
2 changes: 1 addition & 1 deletion game_folder/shaders/compute/Bloom_vertic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void main() {

vec3 conv1 = vec3(0), conv2 = vec3(0);
int center = local_indx.y+group_size/2;
for(int i = max(center-int(width*5),0); i < min(center+int(width*5),buffer_size); i++)
for(int i = max(center-int(width*15),0); i < min(center+int(width*15),buffer_size); i++)
{
float coord = float(center-i)/width;
conv1 += exp(-k1*pow(coord,2))*color_buffer_hoz1[i];
Expand Down
19 changes: 19 additions & 0 deletions game_folder/shaders/compute/auto_exposure_weighting.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 430

/// DO NOT CHANGE
#define group_size 16

layout(local_size_x = group_size, local_size_y = group_size) in;

layout(rgba8, binding = 0) uniform image2D final_color;
layout(rgba32f, binding = 1) uniform image2D output_w;

void main() {
ivec2 global_pos = ivec2(gl_GlobalInvocationID.xy);
ivec2 local_indx = ivec2(gl_LocalInvocationID.xy);
vec2 img_size = vec2(imageSize(final_color));

vec4 color = imageLoad(final_color, global_pos);
float weight = max(1.0 - length(vec2(global_pos)/img_size - 0.5), 0.f);
imageStore(output_w, global_pos, vec4(length(color.xyz)*weight, weight, 0, 1));
}
4 changes: 2 additions & 2 deletions game_folder/shaders/compute/bloom.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ vec3 bloom_radius(vec3 color, float exposure)
vec3 bloom_treshold(vec3 color, float exposure)
{
color = clamp(color*exposure, 0, 30)/exposure;
vec3 mapped = Camera.bloomradius*(0.5f * tanh(2*(color * exposure - Camera.bloomtreshold)) + 0.5f);
return color*mapped;
vec3 mapped = Camera.bloomintensity*pow(color,vec3(2.f));
return mapped;
}
28 changes: 0 additions & 28 deletions game_folder/shaders/compute/summing.glsl

This file was deleted.

2 changes: 1 addition & 1 deletion src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Camera::SetDirZ(vec3 dir)
dirz.z = dir.z;
}

#define target_brightness 0.7f
#define target_brightness 0.85f
void Camera::UpdateExposure(float illumination)
{
exposure += auto_exposure_speed*(target_brightness - illumination)*exposure;
Expand Down
20 changes: 18 additions & 2 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ void Renderer::Initialize(int w, int h, std::string config_f)
}
}

illumination_texture = GenerateTexture(width, height);
weight_shader = ComputeShader(compute_folder + "/auto_exposure_weighting.glsl");

config.close();
}

Expand Down Expand Up @@ -184,6 +187,8 @@ void Renderer::ReInitialize(int w, int h)
}
}

illumination_texture = GenerateTexture(width, height);

config.close();
}

Expand Down Expand Up @@ -268,14 +273,25 @@ void Renderer::Render()

float Renderer::EvaluateAvgIllumination()
{
//precalculation
glBindImageTexture(0, shader_textures[global_size.size() - 1][0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindImageTexture(1, illumination_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
weight_shader.Run(vec2(ceil(width/16.f), ceil(height / 16.f)));
for (int k = 0; k < 2; k++)
{
glBindImageTexture(k, 0, 0, 0, 0, 0, 0);
}


//get the average of the texture using mipmaps
float* avg = new float[4];
int mipmap_level = floor(log2(float(std::max(width, height))));
glBindTexture(GL_TEXTURE_2D, shader_textures[global_size.size()-1][0]);
glBindTexture(GL_TEXTURE_2D, illumination_texture);
glGenerateMipmap(GL_TEXTURE_2D);
glGetTexImage(GL_TEXTURE_2D, mipmap_level, GL_RGBA, GL_FLOAT, avg);
GLenum err = glGetError();
glBindTexture(GL_TEXTURE_2D, 0);
return sqrt(avg[0] * avg[0] + avg[1] * avg[1] + avg[2] * avg[2]);
return avg[0] / avg[1];
}

GLuint Renderer::GenerateTexture(float w, float h)
Expand Down
3 changes: 3 additions & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ class Renderer
std::vector<GLuint> main_textures;
std::vector<std::vector<GLuint>> shader_textures;
std::vector<sf::Texture> input_textures;

GLuint illumination_texture;
ComputeShader weight_shader;
};

0 comments on commit 7fc03da

Please sign in to comment.