Skip to content

Commit

Permalink
weighted auto-exposure test
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMoroz committed Sep 17, 2019
1 parent 7fc03da commit 470ae6c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
16 changes: 14 additions & 2 deletions game_folder/shaders/compute/auto_exposure_weighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
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;
layout(rgba32f, binding = 1) uniform image2D DE_input;
layout(rgba32f, binding = 2) uniform image2D output_w;

#include<camera.glsl>

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

vec4 sph = imageLoad(DE_input, global_pos);

ray rr = get_ray(vec2(global_pos)/img_size);
vec4 pos = vec4(rr.pos,0);
vec4 dir = vec4(rr.dir,0);

float td = dot(dir.xyz, sph.xyz - pos.xyz);//depth

vec4 color = imageLoad(final_color, global_pos);
float weight = max(1.0 - length(vec2(global_pos)/img_size - 0.5), 0.f);
//float weight = pow(max(1.0 - length(vec2(global_pos)/img_size - 0.5), 0.f),2);
float weight = 1.f/(td + Camera.size + 1e-5);
imageStore(output_w, global_pos, vec4(length(color.xyz)*weight, weight, 0, 1));
}
2 changes: 1 addition & 1 deletion game_folder/shaders/compute/shading.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ vec3 render_ray(in vec4 pos, in vec4 dir, float fov)

vec3 shading(in vec4 pos, in vec4 dir, float fov, float shadow)
{
if(pos.w < max(16*fovray*dir.w, MIN_DIST))
if(pos.w < max(2*fovray*dir.w, MIN_DIST))
{
//calculate the normal
float error = 0.5*fov*dir.w;
Expand Down
45 changes: 34 additions & 11 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,25 +273,48 @@ 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++)
//if coordinate/depth map available
if (main_textures.size() > 0)
{
glBindImageTexture(k, 0, 0, 0, 0, 0, 0);
//precalculation
glBindImageTexture(0, shader_textures[global_size.size() - 1][0], 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glBindImageTexture(1, main_textures[0], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); //depth
glBindImageTexture(2, illumination_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
weight_shader.setCamera(camera.GetGLdata());
weight_shader.Run(vec2(ceil(width / 8.f), ceil(height / 8.f)));
for (int k = 0; k < 3; k++)
{
glBindImageTexture(k, 0, 0, 0, 0, 0, 0);
}
}

if (main_textures.size() > 0)
{
glBindTexture(GL_TEXTURE_2D, illumination_texture);
}
else
{
//just use the final texture
glBindTexture(GL_TEXTURE_2D, shader_textures[global_size.size() - 1][0]);
}

//get the average of the texture using mipmaps
float* avg = new float[4];
float avg[16*4];
int mipmap_level = floor(log2(float(std::max(width, height))));
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 avg[0] / avg[1];

if (main_textures.size() > 0)
{
return avg[0] / avg[1];
}
else
{
return sqrt(avg[0] * avg[0] + avg[1] * avg[1] + avg[2] * avg[2]);
}

}

GLuint Renderer::GenerateTexture(float w, float h)
Expand All @@ -301,7 +324,7 @@ GLuint Renderer::GenerateTexture(float w, float h)
glBindTexture(GL_TEXTURE_2D, texture);
//HDR texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, w, h, 0, GL_RGBA, GL_FLOAT, NULL);
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return texture;
}

0 comments on commit 470ae6c

Please sign in to comment.