add ACES tonemapping, fix #120

This commit is contained in:
Ivan Avdeev 2021-11-02 10:06:48 -07:00 committed by Ivan Avdeev
parent cfbce2c4b9
commit 54e549d726
1 changed files with 25 additions and 3 deletions

View File

@ -61,7 +61,7 @@ bool shadowed(vec3 pos, vec3 dir, float dist) {
return payload_shadow.shadow;
}
const float color_factor = 1000.;
const float color_factor = 600.;
vec3 sampleSurfaceTriangle(vec3 color, vec3 view_dir, MaterialProperties material, mat4x3 emissive_transform, mat3 emissive_transform_normal, uint triangle_index, uint index_offset, uint vertex_offset) {
const uint first_index_offset = index_offset + triangle_index * 3;
@ -134,7 +134,6 @@ vec3 sampleSurfaceTriangle(vec3 color, vec3 view_dir, MaterialProperties materia
if (shadowed(payload_opaque.hit_pos_t.xyz, light_dir, sqrt(light_dist2)))
return vec3(0.);
//return vec3(color_factor);// color;
return color;
}
@ -291,6 +290,24 @@ vec3 traceAdditive(vec3 origin, vec3 direction, float ray_distance) {
return payload_additive.color * color_factor;
}
// Blatantly copypasted from https://www.shadertoy.com/view/XsGfWV
vec3 aces_tonemap(vec3 color){
mat3 m1 = mat3(
0.59719, 0.07600, 0.02840,
0.35458, 0.90834, 0.13383,
0.04823, 0.01566, 0.83777
);
mat3 m2 = mat3(
1.60475, -0.10208, -0.00327,
-0.53108, 1.10813, -0.07276,
-0.07367, -0.00605, 1.07602
);
vec3 v = m1 * color;
vec3 a = v * (v + 0.0245786) - 0.000090537;
vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
return pow(clamp(m2 * (a / b), 0.0, 1.0), vec3(1.0 / 2.2));
}
void main() {
rand01_state = push_constants.random_seed + gl_LaunchIDEXT.x * 1833 + gl_LaunchIDEXT.y * 31337;
vec2 uv = (gl_LaunchIDEXT.xy + .5) / gl_LaunchSizeEXT.xy * 2. - 1.;
@ -423,11 +440,16 @@ void main() {
throughput *= brdfWeight;
} // for all bounces
if (false)
{
vec3 prev_frame = imageLoad(previous_frame, ivec2(gl_LaunchIDEXT.xy)).rgb;
prev_frame *= prev_frame;
prev_frame *= color_factor;
C = mix(C, prev_frame, push_constants.prev_frame_blend_factor);
}
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(sqrt(C/color_factor), 1.));
C /= color_factor;
C = aces_tonemap(C);
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(C, 1.));
}