Skip to content

Commit

Permalink
Fix tonemap sub-UV lookup
Browse files Browse the repository at this point in the history
 + remove manually interpolated version
  • Loading branch information
sergcpp committed May 13, 2024
1 parent 881ce28 commit ef2a1a1
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 83 deletions.
48 changes: 1 addition & 47 deletions internal/shaders/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ vec3 TonemapLUT(sampler3D lut, float inv_gamma, vec3 col) {

// Align the encoded range to texel centers
const float LUT_DIMS = 48.0;
const vec3 uv = encoded * (LUT_DIMS - 1.0) / LUT_DIMS;
const vec3 uv = (encoded + (0.5 / LUT_DIMS)) * ((LUT_DIMS - 1.0) / LUT_DIMS);

vec3 ret = textureLod(lut, uv, 0.0).xyz;
if (inv_gamma != 1.0) {
Expand All @@ -272,52 +272,6 @@ vec4 TonemapLUT(sampler3D lut, float inv_gamma, vec4 col) {
return vec4(TonemapLUT(lut, inv_gamma, col.xyz), col.w);
}

// Manual interpolation gives better result for some reason
vec3 TonemapLUT_manual(sampler3D lut, float inv_gamma, vec3 col) {
const vec3 encoded = col / (col + 1.0);

// Align the encoded range to texel centers
const float LUT_DIMS = 48;
const vec3 uv = encoded * (LUT_DIMS - 1.0);
const ivec3 xyz = ivec3(uv);
const ivec3 xyz_next = min(xyz + 1, ivec3(LUT_DIMS - 1));
const vec3 f = fract(uv);

const int ix = xyz.x, iy = xyz.y, iz = xyz.z;
const int jx = xyz_next.x, jy = xyz_next.y, jz = xyz_next.z;
const float fx = f.x, fy = f.y, fz = f.z;

const vec3 c000 = texelFetch(lut, ivec3(ix, iy, iz), 0).xyz;
const vec3 c001 = texelFetch(lut, ivec3(jx, iy, iz), 0).xyz;
const vec3 c010 = texelFetch(lut, ivec3(ix, jy, iz), 0).xyz;
const vec3 c011 = texelFetch(lut, ivec3(jx, jy, iz), 0).xyz;
const vec3 c100 = texelFetch(lut, ivec3(ix, iy, jz), 0).xyz;
const vec3 c101 = texelFetch(lut, ivec3(jx, iy, jz), 0).xyz;
const vec3 c110 = texelFetch(lut, ivec3(ix, jy, jz), 0).xyz;
const vec3 c111 = texelFetch(lut, ivec3(jx, jy, jz), 0).xyz;

const vec3 c00x = (1.0 - fx) * c000 + fx * c001;
const vec3 c01x = (1.0 - fx) * c010 + fx * c011;
const vec3 c10x = (1.0 - fx) * c100 + fx * c101;
const vec3 c11x = (1.0 - fx) * c110 + fx * c111;

const vec3 c0xx = (1.0 - fy) * c00x + fy * c01x;
const vec3 c1xx = (1.0 - fy) * c10x + fy * c11x;

vec3 cxxx = (1.0 - fz) * c0xx + fz * c1xx;

vec3 ret = cxxx;
if (inv_gamma != 1.0) {
ret = pow(ret, vec3(inv_gamma));
}

return ret;
}

vec4 TonemapLUT_manual(sampler3D lut, float inv_gamma, vec4 col) {
return vec4(TonemapLUT_manual(lut, inv_gamma, col.xyz), col.w);
}

// https://gpuopen.com/learn/optimized-reversible-tonemapper-for-resolve/
vec3 reversible_tonemap(vec3 c) { return c / (max(c.x, max(c.y, c.z)) + 1.0); }
vec3 reversible_tonemap_invert(vec3 c) { return c / (1.0 - max(c.x, max(c.y, c.z))); }
Expand Down
8 changes: 4 additions & 4 deletions internal/shaders/convolution.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ void main() {
val0 = TonemapStandard(g_params.inv_gamma, val0);
val1 = TonemapStandard(g_params.inv_gamma, val1);
} else {
val0 = TonemapLUT_manual(g_tonemap_lut, g_params.inv_gamma, val0);
val1 = TonemapLUT_manual(g_tonemap_lut, g_params.inv_gamma, val1);
val0 = TonemapLUT(g_tonemap_lut, g_params.inv_gamma, val0);
val1 = TonemapLUT(g_tonemap_lut, g_params.inv_gamma, val1);
}
#endif
imageStore(g_out_tonemapped_img, ivec2(x + j * 16 + jj, y), val0);
Expand Down Expand Up @@ -829,8 +829,8 @@ void main() {
val0 = TonemapStandard(g_params.inv_gamma, val0);
val1 = TonemapStandard(g_params.inv_gamma, val1);
} else {
val0 = TonemapLUT_manual(g_tonemap_lut, g_params.inv_gamma, val0);
val1 = TonemapLUT_manual(g_tonemap_lut, g_params.inv_gamma, val1);
val0 = TonemapLUT(g_tonemap_lut, g_params.inv_gamma, val0);
val1 = TonemapLUT(g_tonemap_lut, g_params.inv_gamma, val1);
}
#endif
imageStore(g_out_tonemapped_img, ivec2(x + j, y), val0);
Expand Down
2 changes: 1 addition & 1 deletion internal/shaders/nlm_filter.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void main() {
[[dont_flatten]] if (g_params.tonemap_mode == 0) {
sum_output = TonemapStandard(g_params.inv_gamma, sum_output);
} else {
sum_output = TonemapLUT_manual(g_tonemap_lut, g_params.inv_gamma, sum_output);
sum_output = TonemapLUT(g_tonemap_lut, g_params.inv_gamma, sum_output);
}
imageStore(g_out_img, gi, sum_output);
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/nlm_filter.comp.cso.inl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/nlm_filter.comp.spv.inl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/postprocess.comp.cso.inl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/postprocess.comp.spv.inl

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/shaders/postprocess.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void main() {
[[dont_flatten]] if (g_params.tonemap_mode == 0) {
tonemapped_res = TonemapStandard(g_params.inv_gamma, full_val);
} else {
tonemapped_res = TonemapLUT_manual(g_tonemap_lut, g_params.inv_gamma, full_val);
tonemapped_res = TonemapLUT(g_tonemap_lut, g_params.inv_gamma, full_val);
}
#if DEBUG_ADAPTIVE_SAMPLING
if ((g_params.iteration % 5) != 0 /*&& req_samples >= g_params.iteration*/) {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_materials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,7 @@ void test_complex_mat5_dir_light(const char *arch_list[], const char *preferred_
void test_complex_mat5_sun_light(const char *arch_list[], const char *preferred_device) {
const int SampleCount = 13;
const double MinPSNR = 23.0;
const int PixThres = 6274;
const int PixThres = 6319;

Ray::principled_mat_desc_t metal_mat_desc;
metal_mat_desc.base_texture = Ray::TextureHandle{0};
Expand Down Expand Up @@ -1806,7 +1806,7 @@ void test_complex_mat6_spot_light(const char *arch_list[], const char *preferred
void test_complex_mat6_dir_light(const char *arch_list[], const char *preferred_device) {
const int SampleCount = 87;
const double MinPSNR = 18.0;
const int PixThres = 9370;
const int PixThres = 9394;

Ray::principled_mat_desc_t olive_mat_desc;
olive_mat_desc.base_color[0] = 0.836164f;
Expand All @@ -1823,7 +1823,7 @@ void test_complex_mat6_dir_light(const char *arch_list[], const char *preferred_
void test_complex_mat6_sun_light(const char *arch_list[], const char *preferred_device) {
const int SampleCount = 16;
const double MinPSNR = 19.0;
const int PixThres = 14398;
const int PixThres = 14457;

Ray::principled_mat_desc_t olive_mat_desc;
olive_mat_desc.base_color[0] = 0.836164f;
Expand Down

0 comments on commit ef2a1a1

Please sign in to comment.