2020-05-18 16:56:22 +08:00
|
|
|
#[compute]
|
2020-02-11 21:01:43 +08:00
|
|
|
|
2020-01-10 08:40:26 +08:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
VERSION_DEFINES
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 8
|
|
|
|
|
|
|
|
layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;
|
|
|
|
|
2020-02-11 21:01:43 +08:00
|
|
|
shared float tmp_data[BLOCK_SIZE * BLOCK_SIZE];
|
2020-01-10 08:40:26 +08:00
|
|
|
|
|
|
|
#ifdef READ_TEXTURE
|
|
|
|
|
|
|
|
//use for main texture
|
2020-01-12 09:26:52 +08:00
|
|
|
layout(set = 0, binding = 0) uniform sampler2D source_texture;
|
2020-01-10 08:40:26 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
//use for intermediate textures
|
2020-01-12 09:26:52 +08:00
|
|
|
layout(r32f, set = 0, binding = 0) uniform restrict readonly image2D source_luminance;
|
2020-01-10 08:40:26 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-01-12 09:26:52 +08:00
|
|
|
layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D dest_luminance;
|
2020-01-10 08:40:26 +08:00
|
|
|
|
2020-01-12 09:26:52 +08:00
|
|
|
#ifdef WRITE_LUMINANCE
|
|
|
|
layout(set = 2, binding = 0) uniform sampler2D prev_luminance;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
layout(push_constant, binding = 1, std430) uniform Params {
|
2020-01-10 08:40:26 +08:00
|
|
|
ivec2 source_size;
|
2020-01-12 09:26:52 +08:00
|
|
|
float max_luminance;
|
|
|
|
float min_luminance;
|
|
|
|
float exposure_adjust;
|
|
|
|
float pad[3];
|
2020-02-11 21:01:43 +08:00
|
|
|
}
|
|
|
|
params;
|
2020-01-10 08:40:26 +08:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
uint t = gl_LocalInvocationID.y * BLOCK_SIZE + gl_LocalInvocationID.x;
|
|
|
|
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
|
|
|
|
|
2020-02-11 21:01:43 +08:00
|
|
|
if (any(lessThan(pos, params.source_size))) {
|
2020-01-10 08:40:26 +08:00
|
|
|
#ifdef READ_TEXTURE
|
2020-02-11 21:01:43 +08:00
|
|
|
vec3 v = texelFetch(source_texture, pos, 0).rgb;
|
|
|
|
tmp_data[t] = max(v.r, max(v.g, v.b));
|
2020-01-10 08:40:26 +08:00
|
|
|
#else
|
2020-01-12 09:26:52 +08:00
|
|
|
tmp_data[t] = imageLoad(source_luminance, pos).r;
|
2020-01-10 08:40:26 +08:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
tmp_data[t] = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
groupMemoryBarrier();
|
|
|
|
barrier();
|
|
|
|
|
2020-02-11 21:01:43 +08:00
|
|
|
uint size = (BLOCK_SIZE * BLOCK_SIZE) >> 1;
|
2020-01-10 08:40:26 +08:00
|
|
|
|
|
|
|
do {
|
2020-02-11 21:01:43 +08:00
|
|
|
if (t < size) {
|
|
|
|
tmp_data[t] += tmp_data[t + size];
|
2020-01-10 08:40:26 +08:00
|
|
|
}
|
|
|
|
groupMemoryBarrier();
|
|
|
|
barrier();
|
|
|
|
|
2020-02-11 21:01:43 +08:00
|
|
|
size >>= 1;
|
|
|
|
} while (size >= 1);
|
2020-01-10 08:40:26 +08:00
|
|
|
|
2020-02-11 21:01:43 +08:00
|
|
|
if (t == 0) {
|
2020-01-10 08:40:26 +08:00
|
|
|
//compute rect size
|
2020-02-11 21:01:43 +08:00
|
|
|
ivec2 rect_size = min(params.source_size - pos, ivec2(BLOCK_SIZE));
|
|
|
|
float avg = tmp_data[0] / float(rect_size.x * rect_size.y);
|
2020-01-12 09:26:52 +08:00
|
|
|
//float avg = tmp_data[0] / float(BLOCK_SIZE*BLOCK_SIZE);
|
2020-02-11 21:01:43 +08:00
|
|
|
pos /= ivec2(BLOCK_SIZE);
|
2020-01-12 09:26:52 +08:00
|
|
|
#ifdef WRITE_LUMINANCE
|
|
|
|
float prev_lum = texelFetch(prev_luminance, ivec2(0, 0), 0).r; //1 pixel previous exposure
|
2020-02-11 21:01:43 +08:00
|
|
|
avg = clamp(prev_lum + (avg - prev_lum) * params.exposure_adjust, params.min_luminance, params.max_luminance);
|
2020-01-12 09:26:52 +08:00
|
|
|
#endif
|
2020-01-10 08:40:26 +08:00
|
|
|
imageStore(dest_luminance, pos, vec4(avg));
|
|
|
|
}
|
|
|
|
}
|