| 1 |
#[vertex] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(location = 0) out vec2 uv_interp; |
| 8 |
|
| 9 |
void main() { |
| 10 |
// old code, ARM driver bug on Mali-GXXx GPUs and Vulkan API 1.3.xxx |
| 11 |
// https://github.com/godotengine/godot/pull/92817#issuecomment-2168625982 |
| 12 |
//vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0)); |
| 13 |
//gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0); |
| 14 |
//uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0 |
| 15 |
|
| 16 |
vec2 vertex_base; |
| 17 |
if (gl_VertexIndex == 0) { |
| 18 |
vertex_base = vec2(-1.0, -1.0); |
| 19 |
} else if (gl_VertexIndex == 1) { |
| 20 |
vertex_base = vec2(-1.0, 3.0); |
| 21 |
} else { |
| 22 |
vertex_base = vec2(3.0, -1.0); |
| 23 |
} |
| 24 |
gl_Position = vec4(vertex_base, 0.0, 1.0); |
| 25 |
uv_interp = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0 |
| 26 |
} |
| 27 |
|
| 28 |
#[fragment] |
| 29 |
|
| 30 |
#version 450 |
| 31 |
|
| 32 |
#VERSION_DEFINES |
| 33 |
|
| 34 |
#ifdef USE_MULTIVIEW |
| 35 |
#extension GL_EXT_multiview : enable |
| 36 |
#define ViewIndex gl_ViewIndex |
| 37 |
#endif //USE_MULTIVIEW |
| 38 |
|
| 39 |
layout(location = 0) in vec2 uv_interp; |
| 40 |
|
| 41 |
#ifdef USE_MULTIVIEW |
| 42 |
#define SAMPLER_FORMAT sampler2DArray |
| 43 |
#else |
| 44 |
#define SAMPLER_FORMAT sampler2D |
| 45 |
#endif |
| 46 |
|
| 47 |
// All uniforms must be on set 0 to prevent MSAA from crashing Mali GPUs. See GH-114785. |
| 48 |
#ifdef SUBPASS |
| 49 |
layout(input_attachment_index = 0, set = 0, binding = 0) uniform subpassInput input_color; |
| 50 |
#else |
| 51 |
layout(set = 0, binding = 0) uniform SAMPLER_FORMAT source_color; |
| 52 |
#endif |
| 53 |
|
| 54 |
layout(set = 0, binding = 1) uniform SAMPLER_FORMAT source_glow; |
| 55 |
layout(set = 0, binding = 2) uniform sampler2D glow_map; |
| 56 |
|
| 57 |
#ifdef USE_1D_LUT |
| 58 |
layout(set = 0, binding = 3) uniform sampler2D source_color_correction; |
| 59 |
#else |
| 60 |
layout(set = 0, binding = 3) uniform sampler3D source_color_correction; |
| 61 |
#endif |
| 62 |
|
| 63 |
layout(constant_id = 0) const bool use_bcs = false; |
| 64 |
layout(constant_id = 1) const bool use_glow = false; |
| 65 |
layout(constant_id = 2) const bool use_glow_map = false; |
| 66 |
layout(constant_id = 3) const bool use_color_correction = false; |
| 67 |
layout(constant_id = 4) const bool use_fxaa = false; |
| 68 |
layout(constant_id = 5) const bool deband_8_bit = false; |
| 69 |
layout(constant_id = 6) const bool deband_10_bit = false; |
| 70 |
layout(constant_id = 7) const bool convert_to_srgb = false; |
| 71 |
layout(constant_id = 8) const bool tonemapper_linear = false; |
| 72 |
layout(constant_id = 9) const bool tonemapper_reinhard = false; |
| 73 |
layout(constant_id = 10) const bool tonemapper_filmic = false; |
| 74 |
layout(constant_id = 11) const bool tonemapper_aces = false; |
| 75 |
layout(constant_id = 12) const bool tonemapper_agx = false; |
| 76 |
layout(constant_id = 13) const bool glow_mode_add = false; |
| 77 |
layout(constant_id = 14) const bool glow_mode_screen = false; |
| 78 |
layout(constant_id = 15) const bool glow_mode_softlight = false; |
| 79 |
layout(constant_id = 16) const bool glow_mode_replace = false; |
| 80 |
layout(constant_id = 17) const bool glow_mode_mix = false; |
| 81 |
|
| 82 |
layout(push_constant, std430) uniform Params { |
| 83 |
vec3 bcs; |
| 84 |
float luminance_multiplier; |
| 85 |
|
| 86 |
vec2 src_pixel_size; |
| 87 |
vec2 dest_pixel_size; |
| 88 |
|
| 89 |
float glow_intensity; |
| 90 |
float glow_map_strength; |
| 91 |
float exposure; |
| 92 |
float white; |
| 93 |
|
| 94 |
vec4 tonemapper_params; |
| 95 |
} |
| 96 |
params; |
| 97 |
|
| 98 |
layout(location = 0) out vec4 frag_color; |
| 99 |
|
| 100 |
// Based on Reinhard's extended formula, see equation 4 in https://doi.org/cjbgrt |
| 101 |
vec3 tonemap_reinhard(vec3 color) { |
| 102 |
float white_squared = params.tonemapper_params.x; |
| 103 |
vec3 white_squared_color = white_squared * color; |
| 104 |
// Equivalent to color * (1 + color / white_squared) / (1 + color) |
| 105 |
return (white_squared_color + color * color) / (white_squared_color + white_squared); |
| 106 |
} |
| 107 |
|
| 108 |
vec3 tonemap_filmic(vec3 color) { |
| 109 |
// These constants must match the those in the C++ code that calculates the parameters. |
| 110 |
// exposure_bias: Input scale (color *= bias, env->white *= bias) to make the brightness consistent with other tonemappers. |
| 111 |
// Also useful to scale the input to the range that the tonemapper is designed for (some require very high input values). |
| 112 |
// Has no effect on the curve's general shape or visual properties. |
| 113 |
const float exposure_bias = 2.0f; |
| 114 |
const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance |
| 115 |
const float B = 0.30f * exposure_bias; |
| 116 |
const float C = 0.10f; |
| 117 |
const float D = 0.20f; |
| 118 |
const float E = 0.01f; |
| 119 |
const float F = 0.30f; |
| 120 |
|
| 121 |
vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; |
| 122 |
|
| 123 |
return color_tonemapped / params.tonemapper_params.x; |
| 124 |
} |
| 125 |
|
| 126 |
// Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl |
| 127 |
// (MIT License). |
| 128 |
vec3 tonemap_aces(vec3 color) { |
| 129 |
// These constants must match the those in the C++ code that calculates the parameters. |
| 130 |
const float exposure_bias = 1.8f; |
| 131 |
const float A = 0.0245786f; |
| 132 |
const float B = 0.000090537f; |
| 133 |
const float C = 0.983729f; |
| 134 |
const float D = 0.432951f; |
| 135 |
const float E = 0.238081f; |
| 136 |
|
| 137 |
// Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` |
| 138 |
const mat3 rgb_to_rrt = mat3( |
| 139 |
vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), |
| 140 |
vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), |
| 141 |
vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); |
| 142 |
|
| 143 |
const mat3 odt_to_rgb = mat3( |
| 144 |
vec3(1.60475f, -0.53108f, -0.07367f), |
| 145 |
vec3(-0.10208f, 1.10813f, -0.00605f), |
| 146 |
vec3(-0.00327f, -0.07276f, 1.07602f)); |
| 147 |
|
| 148 |
color *= rgb_to_rrt; |
| 149 |
vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); |
| 150 |
color_tonemapped *= odt_to_rgb; |
| 151 |
|
| 152 |
return color_tonemapped / params.tonemapper_params.x; |
| 153 |
} |
| 154 |
|
| 155 |
// allenwp tonemapping curve; developed for use in the Godot game engine. |
| 156 |
// Source and details: https://allenwp.com/blog/2025/05/29/allenwp-tonemapping-curve/ |
| 157 |
// Input must be a non-negative linear scene value. |
| 158 |
vec3 allenwp_curve(vec3 x) { |
| 159 |
const float output_max_value = 1.0; // SDR always has an output_max_value of 1.0 |
| 160 |
|
| 161 |
// These constants must match the those in the C++ code that calculates the parameters. |
| 162 |
// 18% "middle gray" is perceptually 50% of the brightness of reference white. |
| 163 |
const float awp_crossover_point = 0.18; |
| 164 |
// When output_max_value and/or awp_crossover_point are no longer constant, |
| 165 |
// awp_shoulder_max can be calculated on the CPU and passed in as params.tonemap_e. |
| 166 |
const float awp_shoulder_max = output_max_value - awp_crossover_point; |
| 167 |
|
| 168 |
float awp_contrast = params.tonemapper_params.x; |
| 169 |
float awp_toe_a = params.tonemapper_params.y; |
| 170 |
float awp_slope = params.tonemapper_params.z; |
| 171 |
float awp_w = params.tonemapper_params.w; |
| 172 |
|
| 173 |
// Reinhard-like shoulder: |
| 174 |
vec3 s = x - awp_crossover_point; |
| 175 |
vec3 slope_s = awp_slope * s; |
| 176 |
s = slope_s * (1.0 + s / awp_w) / (1.0 + (slope_s / awp_shoulder_max)); |
| 177 |
s += awp_crossover_point; |
| 178 |
|
| 179 |
// Sigmoid power function toe: |
| 180 |
vec3 t = pow(x, vec3(awp_contrast)); |
| 181 |
t = t / (t + awp_toe_a); |
| 182 |
|
| 183 |
return mix(s, t, lessThan(x, vec3(awp_crossover_point))); |
| 184 |
} |
| 185 |
|
| 186 |
// This is an approximation and simplification of EaryChow's AgX implementation that is used by Blender. |
| 187 |
// This code is based off of the script that generates the AgX_Base_sRGB.cube LUT that Blender uses. |
| 188 |
// Source: https://github.com/EaryChow/AgX_LUT_Gen/blob/main/AgXBasesRGB.py |
| 189 |
// Colorspace transformation source: https://www.colour-science.org:8010/apps/rgb_colourspace_transformation_matrix |
| 190 |
vec3 tonemap_agx(vec3 color) { |
| 191 |
// Input color should be non-negative! |
| 192 |
// Large negative values in one channel and large positive values in other |
| 193 |
// channels can result in a colour that appears darker and more saturated than |
| 194 |
// desired after passing it through the inset matrix. For this reason, it is |
| 195 |
// best to prevent negative input values. |
| 196 |
// This is done before the Rec. 2020 transform to allow the Rec. 2020 |
| 197 |
// transform to be combined with the AgX inset matrix. This results in a loss |
| 198 |
// of color information that could be correctly interpreted within the |
| 199 |
// Rec. 2020 color space as positive RGB values, but is often not worth |
| 200 |
// the performance cost of an additional matrix multiplication. |
| 201 |
// |
| 202 |
// Additionally, this AgX configuration was created subjectively based on |
| 203 |
// output appearance in the Rec. 709 color gamut, so it is possible that these |
| 204 |
// matrices will not perform well with non-Rec. 709 output (more testing with |
| 205 |
// future wide-gamut displays is be needed). |
| 206 |
// See this comment from the author on the decisions made to create the matrices: |
| 207 |
// https://github.com/godotengine/godot-proposals/issues/12317#issuecomment-2835824250 |
| 208 |
|
| 209 |
// Combined Rec. 709 to Rec. 2020 and Blender AgX inset matrices: |
| 210 |
const mat3 rec709_to_rec2020_agx_inset_matrix = mat3( |
| 211 |
0.544814746488245, 0.140416948464053, 0.0888104196149096, |
| 212 |
0.373787398372697, 0.754137554567394, 0.178871756420858, |
| 213 |
0.0813978551390581, 0.105445496968552, 0.732317823964232); |
| 214 |
|
| 215 |
// Combined inverse AgX outset matrix and Rec. 2020 to Rec. 709 matrices. |
| 216 |
const mat3 agx_outset_rec2020_to_rec709_matrix = mat3( |
| 217 |
1.96488741169489, -0.299313364904742, -0.164352742528393, |
| 218 |
-0.855988495690215, 1.32639796461980, -0.238183969428088, |
| 219 |
-0.108898916004672, -0.0270845997150571, 1.40253671195648); |
| 220 |
|
| 221 |
const float output_max_value = 1.0; // SDR always has an output_max_value of 1.0 |
| 222 |
|
| 223 |
// Apply inset matrix. |
| 224 |
color = rec709_to_rec2020_agx_inset_matrix * color; |
| 225 |
|
| 226 |
// Use the allenwp tonemapping curve to match the Blender AgX curve while |
| 227 |
// providing stability across all variable dyanimc range (SDR, HDR, EDR). |
| 228 |
color = allenwp_curve(color); |
| 229 |
|
| 230 |
// Clipping to output_max_value is required to address a cyan colour that occurs |
| 231 |
// with very bright inputs. |
| 232 |
color = min(vec3(output_max_value), color); |
| 233 |
|
| 234 |
// Apply outset to make the result more chroma-laden and then go back to Rec. 709. |
| 235 |
color = agx_outset_rec2020_to_rec709_matrix * color; |
| 236 |
|
| 237 |
// Blender's lusRGB.compensate_low_side is too complex for this shader, so |
| 238 |
// simply return the color, even if it has negative components. These negative |
| 239 |
// components may be useful for subsequent color adjustments. |
| 240 |
return color; |
| 241 |
} |
| 242 |
|
| 243 |
vec3 linear_to_srgb(vec3 color) { |
| 244 |
const vec3 a = vec3(0.055f); |
| 245 |
return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); |
| 246 |
} |
| 247 |
|
| 248 |
vec3 srgb_to_linear(vec3 color) { |
| 249 |
const vec3 a = vec3(0.055f); |
| 250 |
return mix(pow((color.rgb + a) * (1.0f / (vec3(1.0f) + a)), vec3(2.4f)), color.rgb * (1.0f / 12.92f), lessThan(color.rgb, vec3(0.04045f))); |
| 251 |
} |
| 252 |
|
| 253 |
vec3 apply_tonemapping(vec3 color) { // inputs are LINEAR |
| 254 |
if (tonemapper_linear) { |
| 255 |
return color; |
| 256 |
} |
| 257 |
|
| 258 |
// Ensure color values passed to tonemappers are positive. |
| 259 |
// They can be negative in the case of negative lights, which leads to undesired behavior. |
| 260 |
color = max(vec3(0.0), color); |
| 261 |
|
| 262 |
if (tonemapper_reinhard) { |
| 263 |
return tonemap_reinhard(color); |
| 264 |
} else if (tonemapper_filmic) { |
| 265 |
return tonemap_filmic(color); |
| 266 |
} else if (tonemapper_aces) { |
| 267 |
return tonemap_aces(color); |
| 268 |
} else { // tonemapper_agx |
| 269 |
return tonemap_agx(color); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
#ifdef USE_MULTIVIEW |
| 274 |
vec3 gather_glow() { |
| 275 |
vec2 uv = gl_FragCoord.xy * params.dest_pixel_size; |
| 276 |
return textureLod(source_glow, vec3(uv, ViewIndex), 0.0).rgb * params.luminance_multiplier; |
| 277 |
} |
| 278 |
#else |
| 279 |
vec3 gather_glow() { |
| 280 |
vec2 uv = gl_FragCoord.xy * params.dest_pixel_size; |
| 281 |
return textureLod(source_glow, uv, 0.0).rgb * params.luminance_multiplier; |
| 282 |
} |
| 283 |
#endif // !USE_MULTIVIEW |
| 284 |
|
| 285 |
// Applies glow using the selected blending mode. Does not handle the mix blend mode. |
| 286 |
vec3 apply_glow(vec3 color, vec3 glow, float white) { |
| 287 |
if (glow_mode_add) { |
| 288 |
return color + glow; |
| 289 |
} else if (glow_mode_screen) { |
| 290 |
// Glow cannot be above 1.0 after normalizing and should be non-negative |
| 291 |
// to produce expected results. It is possible that glow can be negative |
| 292 |
// if negative lights were used in the scene. |
| 293 |
// We clamp to white because glow will be normalized to this range. |
| 294 |
// Note: white cannot be smaller than the maximum output value. |
| 295 |
glow.rgb = clamp(glow.rgb, 0.0, white); |
| 296 |
|
| 297 |
// Normalize to white range. |
| 298 |
//glow.rgb /= white; |
| 299 |
//color.rgb /= white; |
| 300 |
//color.rgb = (color.rgb + glow.rgb) - (color.rgb * glow.rgb); |
| 301 |
// Expand back to original range. |
| 302 |
//color.rgb *= white; |
| 303 |
|
| 304 |
// The following is a mathematically simplified version of the above. |
| 305 |
color.rgb = color.rgb + glow.rgb - (color.rgb * glow.rgb / white); |
| 306 |
|
| 307 |
return color; |
| 308 |
} else if (glow_mode_softlight) { |
| 309 |
// Glow cannot be above 1.0 should be non-negative to produce |
| 310 |
// expected results. It is possible that glow can be negative |
| 311 |
// if negative lights were used in the scene. |
| 312 |
// Note: This approach causes a discontinuity with scene values |
| 313 |
// at 1.0, but because this glow should have its strongest influence |
| 314 |
// anchored at 0.25 there is no way around this. |
| 315 |
glow.rgb = clamp(glow.rgb, 0.0, 1.0); |
| 316 |
|
| 317 |
color.r = color.r > 1.0 ? color.r : color.r + glow.r * ((color.r <= 0.25f ? ((16.0f * color.r - 12.0f) * color.r + 4.0f) * color.r : sqrt(color.r)) - color.r); |
| 318 |
color.g = color.g > 1.0 ? color.g : color.g + glow.g * ((color.g <= 0.25f ? ((16.0f * color.g - 12.0f) * color.g + 4.0f) * color.g : sqrt(color.g)) - color.g); |
| 319 |
color.b = color.b > 1.0 ? color.b : color.b + glow.b * ((color.b <= 0.25f ? ((16.0f * color.b - 12.0f) * color.b + 4.0f) * color.b : sqrt(color.b)) - color.b); |
| 320 |
|
| 321 |
return color; |
| 322 |
} else { //replace |
| 323 |
return glow; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
#ifdef USE_1D_LUT |
| 328 |
vec3 apply_color_correction(vec3 color) { |
| 329 |
color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r; |
| 330 |
color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g; |
| 331 |
color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b; |
| 332 |
return color; |
| 333 |
} |
| 334 |
#else |
| 335 |
vec3 apply_color_correction(vec3 color) { |
| 336 |
return textureLod(source_color_correction, color, 0.0).rgb; |
| 337 |
} |
| 338 |
#endif |
| 339 |
|
| 340 |
#ifndef SUBPASS |
| 341 |
|
| 342 |
// FXAA 3.11 compact, Ported from https://github.com/kosua20/Rendu/blob/master/resources/common/shaders/screens/fxaa.frag |
| 343 |
/////////////////////////////////////////////////////////////////////////////////// |
| 344 |
// MIT License |
| 345 |
// |
| 346 |
// Copyright (c) 2017 Simon Rodriguez |
| 347 |
// |
| 348 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 349 |
// of this software and associated documentation files (the "Software"), to deal |
| 350 |
// in the Software without restriction, including without limitation the rights |
| 351 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 352 |
// copies of the Software, and to permit persons to whom the Software is |
| 353 |
// furnished to do so, subject to the following conditions: |
| 354 |
// |
| 355 |
// The above copyright notice and this permission notice shall be included in all |
| 356 |
// copies or substantial portions of the Software. |
| 357 |
// |
| 358 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 359 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 360 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 361 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 362 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 363 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 364 |
// SOFTWARE. |
| 365 |
/////////////////////////////////////////////////////////////////////////////////// |
| 366 |
|
| 367 |
// Nvidia Original FXAA 3.11 License |
| 368 |
//---------------------------------------------------------------------------------- |
| 369 |
// File: es3-kepler\FXAA/FXAA3_11.h |
| 370 |
// SDK Version: v3.00 |
| 371 |
// Email: [email protected] |
| 372 |
// Site: http://developer.nvidia.com/ |
| 373 |
// |
| 374 |
// Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved. |
| 375 |
// |
| 376 |
// Redistribution and use in source and binary forms, with or without |
| 377 |
// modification, are permitted provided that the following conditions |
| 378 |
// are met: |
| 379 |
// * Redistributions of source code must retain the above copyright |
| 380 |
// notice, this list of conditions and the following disclaimer. |
| 381 |
// * Redistributions in binary form must reproduce the above copyright |
| 382 |
// notice, this list of conditions and the following disclaimer in the |
| 383 |
// documentation and/or other materials provided with the distribution. |
| 384 |
// * Neither the name of NVIDIA CORPORATION nor the names of its |
| 385 |
// contributors may be used to endorse or promote products derived |
| 386 |
// from this software without specific prior written permission. |
| 387 |
// |
| 388 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 389 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 390 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 391 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 392 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 393 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 394 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 395 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 396 |
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 397 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 398 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 399 |
// |
| 400 |
//---------------------------------------------------------------------------------- |
| 401 |
// |
| 402 |
// NVIDIA FXAA 3.11 by TIMOTHY LOTTES |
| 403 |
// |
| 404 |
//---------------------------------------------------------------------------------- |
| 405 |
|
| 406 |
float QUALITY(float q) { |
| 407 |
return (q < 5 ? 1.0 : (q > 5 ? (q < 10 ? 2.0 : (q < 11 ? 4.0 : 8.0)) : 1.5)); |
| 408 |
} |
| 409 |
|
| 410 |
float rgb2luma(vec3 rgb) { |
| 411 |
return sqrt(dot(rgb, vec3(0.299, 0.587, 0.114))); |
| 412 |
} |
| 413 |
|
| 414 |
vec3 do_fxaa(vec3 color, float exposure, vec2 uv_interp) { |
| 415 |
const float EDGE_THRESHOLD_MIN = 0.0312; |
| 416 |
const float EDGE_THRESHOLD_MAX = 0.125; |
| 417 |
const int ITERATIONS = 12; |
| 418 |
const float SUBPIXEL_QUALITY = 0.75; |
| 419 |
|
| 420 |
#ifdef USE_MULTIVIEW |
| 421 |
float lumaUp = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(0, 1)).xyz * exposure * params.luminance_multiplier); |
| 422 |
float lumaDown = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(0, -1)).xyz * exposure * params.luminance_multiplier); |
| 423 |
float lumaLeft = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(-1, 0)).xyz * exposure * params.luminance_multiplier); |
| 424 |
float lumaRight = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(1, 0)).xyz * exposure * params.luminance_multiplier); |
| 425 |
|
| 426 |
float lumaCenter = rgb2luma(color); |
| 427 |
|
| 428 |
float lumaMin = min(lumaCenter, min(min(lumaUp, lumaDown), min(lumaLeft, lumaRight))); |
| 429 |
float lumaMax = max(lumaCenter, max(max(lumaUp, lumaDown), max(lumaLeft, lumaRight))); |
| 430 |
|
| 431 |
float lumaRange = lumaMax - lumaMin; |
| 432 |
|
| 433 |
if (lumaRange < max(EDGE_THRESHOLD_MIN, lumaMax * EDGE_THRESHOLD_MAX)) { |
| 434 |
return color; |
| 435 |
} |
| 436 |
|
| 437 |
float lumaDownLeft = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(-1, -1)).xyz * exposure * params.luminance_multiplier); |
| 438 |
float lumaUpRight = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(1, 1)).xyz * exposure * params.luminance_multiplier); |
| 439 |
float lumaUpLeft = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(-1, 1)).xyz * exposure * params.luminance_multiplier); |
| 440 |
float lumaDownRight = rgb2luma(textureLodOffset(source_color, vec3(uv_interp, ViewIndex), 0.0, ivec2(1, -1)).xyz * exposure * params.luminance_multiplier); |
| 441 |
|
| 442 |
float lumaDownUp = lumaDown + lumaUp; |
| 443 |
float lumaLeftRight = lumaLeft + lumaRight; |
| 444 |
|
| 445 |
float lumaLeftCorners = lumaDownLeft + lumaUpLeft; |
| 446 |
float lumaDownCorners = lumaDownLeft + lumaDownRight; |
| 447 |
float lumaRightCorners = lumaDownRight + lumaUpRight; |
| 448 |
float lumaUpCorners = lumaUpRight + lumaUpLeft; |
| 449 |
|
| 450 |
float edgeHorizontal = abs(-2.0 * lumaLeft + lumaLeftCorners) + abs(-2.0 * lumaCenter + lumaDownUp) * 2.0 + abs(-2.0 * lumaRight + lumaRightCorners); |
| 451 |
float edgeVertical = abs(-2.0 * lumaUp + lumaUpCorners) + abs(-2.0 * lumaCenter + lumaLeftRight) * 2.0 + abs(-2.0 * lumaDown + lumaDownCorners); |
| 452 |
|
| 453 |
bool isHorizontal = (edgeHorizontal >= edgeVertical); |
| 454 |
|
| 455 |
float stepLength = isHorizontal ? params.src_pixel_size.y : params.src_pixel_size.x; |
| 456 |
|
| 457 |
float luma1 = isHorizontal ? lumaDown : lumaLeft; |
| 458 |
float luma2 = isHorizontal ? lumaUp : lumaRight; |
| 459 |
float gradient1 = luma1 - lumaCenter; |
| 460 |
float gradient2 = luma2 - lumaCenter; |
| 461 |
|
| 462 |
bool is1Steepest = abs(gradient1) >= abs(gradient2); |
| 463 |
|
| 464 |
float gradientScaled = 0.25 * max(abs(gradient1), abs(gradient2)); |
| 465 |
|
| 466 |
float lumaLocalAverage = 0.0; |
| 467 |
if (is1Steepest) { |
| 468 |
stepLength = -stepLength; |
| 469 |
lumaLocalAverage = 0.5 * (luma1 + lumaCenter); |
| 470 |
} else { |
| 471 |
lumaLocalAverage = 0.5 * (luma2 + lumaCenter); |
| 472 |
} |
| 473 |
|
| 474 |
vec2 currentUv = uv_interp; |
| 475 |
if (isHorizontal) { |
| 476 |
currentUv.y += stepLength * 0.5; |
| 477 |
} else { |
| 478 |
currentUv.x += stepLength * 0.5; |
| 479 |
} |
| 480 |
|
| 481 |
vec2 offset = isHorizontal ? vec2(params.src_pixel_size.x, 0.0) : vec2(0.0, params.src_pixel_size.y); |
| 482 |
vec3 uv1 = vec3(currentUv - offset * QUALITY(0), ViewIndex); |
| 483 |
vec3 uv2 = vec3(currentUv + offset * QUALITY(0), ViewIndex); |
| 484 |
|
| 485 |
float lumaEnd1 = rgb2luma(textureLod(source_color, uv1, 0.0).xyz * exposure * params.luminance_multiplier); |
| 486 |
float lumaEnd2 = rgb2luma(textureLod(source_color, uv2, 0.0).xyz * exposure * params.luminance_multiplier); |
| 487 |
lumaEnd1 -= lumaLocalAverage; |
| 488 |
lumaEnd2 -= lumaLocalAverage; |
| 489 |
|
| 490 |
bool reached1 = abs(lumaEnd1) >= gradientScaled; |
| 491 |
bool reached2 = abs(lumaEnd2) >= gradientScaled; |
| 492 |
bool reachedBoth = reached1 && reached2; |
| 493 |
|
| 494 |
if (!reached1) { |
| 495 |
uv1 -= vec3(offset * QUALITY(1), 0.0); |
| 496 |
} |
| 497 |
if (!reached2) { |
| 498 |
uv2 += vec3(offset * QUALITY(1), 0.0); |
| 499 |
} |
| 500 |
|
| 501 |
if (!reachedBoth) { |
| 502 |
for (int i = 2; i < ITERATIONS; i++) { |
| 503 |
if (!reached1) { |
| 504 |
lumaEnd1 = rgb2luma(textureLod(source_color, uv1, 0.0).xyz * exposure * params.luminance_multiplier); |
| 505 |
lumaEnd1 = lumaEnd1 - lumaLocalAverage; |
| 506 |
} |
| 507 |
if (!reached2) { |
| 508 |
lumaEnd2 = rgb2luma(textureLod(source_color, uv2, 0.0).xyz * exposure * params.luminance_multiplier); |
| 509 |
lumaEnd2 = lumaEnd2 - lumaLocalAverage; |
| 510 |
} |
| 511 |
reached1 = abs(lumaEnd1) >= gradientScaled; |
| 512 |
reached2 = abs(lumaEnd2) >= gradientScaled; |
| 513 |
reachedBoth = reached1 && reached2; |
| 514 |
if (!reached1) { |
| 515 |
uv1 -= vec3(offset * QUALITY(i), 0.0); |
| 516 |
} |
| 517 |
if (!reached2) { |
| 518 |
uv2 += vec3(offset * QUALITY(i), 0.0); |
| 519 |
} |
| 520 |
if (reachedBoth) { |
| 521 |
break; |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
float distance1 = isHorizontal ? (uv_interp.x - uv1.x) : (uv_interp.y - uv1.y); |
| 527 |
float distance2 = isHorizontal ? (uv2.x - uv_interp.x) : (uv2.y - uv_interp.y); |
| 528 |
|
| 529 |
bool isDirection1 = distance1 < distance2; |
| 530 |
float distanceFinal = min(distance1, distance2); |
| 531 |
|
| 532 |
float edgeThickness = (distance1 + distance2); |
| 533 |
|
| 534 |
bool isLumaCenterSmaller = lumaCenter < lumaLocalAverage; |
| 535 |
|
| 536 |
bool correctVariation1 = (lumaEnd1 < 0.0) != isLumaCenterSmaller; |
| 537 |
bool correctVariation2 = (lumaEnd2 < 0.0) != isLumaCenterSmaller; |
| 538 |
|
| 539 |
bool correctVariation = isDirection1 ? correctVariation1 : correctVariation2; |
| 540 |
|
| 541 |
float pixelOffset = -distanceFinal / edgeThickness + 0.5; |
| 542 |
|
| 543 |
float finalOffset = correctVariation ? pixelOffset : 0.0; |
| 544 |
|
| 545 |
float lumaAverage = (1.0 / 12.0) * (2.0 * (lumaDownUp + lumaLeftRight) + lumaLeftCorners + lumaRightCorners); |
| 546 |
|
| 547 |
float subPixelOffset1 = clamp(abs(lumaAverage - lumaCenter) / lumaRange, 0.0, 1.0); |
| 548 |
float subPixelOffset2 = (-2.0 * subPixelOffset1 + 3.0) * subPixelOffset1 * subPixelOffset1; |
| 549 |
|
| 550 |
float subPixelOffsetFinal = subPixelOffset2 * subPixelOffset2 * SUBPIXEL_QUALITY; |
| 551 |
|
| 552 |
finalOffset = max(finalOffset, subPixelOffsetFinal); |
| 553 |
|
| 554 |
vec3 finalUv = vec3(uv_interp, ViewIndex); |
| 555 |
if (isHorizontal) { |
| 556 |
finalUv.y += finalOffset * stepLength; |
| 557 |
} else { |
| 558 |
finalUv.x += finalOffset * stepLength; |
| 559 |
} |
| 560 |
|
| 561 |
vec3 finalColor = textureLod(source_color, finalUv, 0.0).xyz * exposure * params.luminance_multiplier; |
| 562 |
return finalColor; |
| 563 |
|
| 564 |
#else |
| 565 |
float lumaUp = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(0, 1)).xyz * exposure * params.luminance_multiplier); |
| 566 |
float lumaDown = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(0, -1)).xyz * exposure * params.luminance_multiplier); |
| 567 |
float lumaLeft = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(-1, 0)).xyz * exposure * params.luminance_multiplier); |
| 568 |
float lumaRight = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(1, 0)).xyz * exposure * params.luminance_multiplier); |
| 569 |
|
| 570 |
float lumaCenter = rgb2luma(color); |
| 571 |
|
| 572 |
float lumaMin = min(lumaCenter, min(min(lumaUp, lumaDown), min(lumaLeft, lumaRight))); |
| 573 |
float lumaMax = max(lumaCenter, max(max(lumaUp, lumaDown), max(lumaLeft, lumaRight))); |
| 574 |
|
| 575 |
float lumaRange = lumaMax - lumaMin; |
| 576 |
|
| 577 |
if (lumaRange < max(EDGE_THRESHOLD_MIN, lumaMax * EDGE_THRESHOLD_MAX)) { |
| 578 |
return color; |
| 579 |
} |
| 580 |
|
| 581 |
float lumaDownLeft = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(-1, -1)).xyz * exposure * params.luminance_multiplier); |
| 582 |
float lumaUpRight = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(1, 1)).xyz * exposure * params.luminance_multiplier); |
| 583 |
float lumaUpLeft = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(-1, 1)).xyz * exposure * params.luminance_multiplier); |
| 584 |
float lumaDownRight = rgb2luma(textureLodOffset(source_color, uv_interp, 0.0, ivec2(1, -1)).xyz * exposure * params.luminance_multiplier); |
| 585 |
|
| 586 |
float lumaDownUp = lumaDown + lumaUp; |
| 587 |
float lumaLeftRight = lumaLeft + lumaRight; |
| 588 |
|
| 589 |
float lumaLeftCorners = lumaDownLeft + lumaUpLeft; |
| 590 |
float lumaDownCorners = lumaDownLeft + lumaDownRight; |
| 591 |
float lumaRightCorners = lumaDownRight + lumaUpRight; |
| 592 |
float lumaUpCorners = lumaUpRight + lumaUpLeft; |
| 593 |
|
| 594 |
float edgeHorizontal = abs(-2.0 * lumaLeft + lumaLeftCorners) + abs(-2.0 * lumaCenter + lumaDownUp) * 2.0 + abs(-2.0 * lumaRight + lumaRightCorners); |
| 595 |
float edgeVertical = abs(-2.0 * lumaUp + lumaUpCorners) + abs(-2.0 * lumaCenter + lumaLeftRight) * 2.0 + abs(-2.0 * lumaDown + lumaDownCorners); |
| 596 |
|
| 597 |
bool isHorizontal = (edgeHorizontal >= edgeVertical); |
| 598 |
|
| 599 |
float stepLength = isHorizontal ? params.src_pixel_size.y : params.src_pixel_size.x; |
| 600 |
|
| 601 |
float luma1 = isHorizontal ? lumaDown : lumaLeft; |
| 602 |
float luma2 = isHorizontal ? lumaUp : lumaRight; |
| 603 |
float gradient1 = luma1 - lumaCenter; |
| 604 |
float gradient2 = luma2 - lumaCenter; |
| 605 |
|
| 606 |
bool is1Steepest = abs(gradient1) >= abs(gradient2); |
| 607 |
|
| 608 |
float gradientScaled = 0.25 * max(abs(gradient1), abs(gradient2)); |
| 609 |
|
| 610 |
float lumaLocalAverage = 0.0; |
| 611 |
if (is1Steepest) { |
| 612 |
stepLength = -stepLength; |
| 613 |
lumaLocalAverage = 0.5 * (luma1 + lumaCenter); |
| 614 |
} else { |
| 615 |
lumaLocalAverage = 0.5 * (luma2 + lumaCenter); |
| 616 |
} |
| 617 |
|
| 618 |
vec2 currentUv = uv_interp; |
| 619 |
if (isHorizontal) { |
| 620 |
currentUv.y += stepLength * 0.5; |
| 621 |
} else { |
| 622 |
currentUv.x += stepLength * 0.5; |
| 623 |
} |
| 624 |
|
| 625 |
vec2 offset = isHorizontal ? vec2(params.src_pixel_size.x, 0.0) : vec2(0.0, params.src_pixel_size.y); |
| 626 |
vec2 uv1 = currentUv - offset * QUALITY(0); |
| 627 |
vec2 uv2 = currentUv + offset * QUALITY(0); |
| 628 |
|
| 629 |
float lumaEnd1 = rgb2luma(textureLod(source_color, uv1, 0.0).xyz * exposure * params.luminance_multiplier); |
| 630 |
float lumaEnd2 = rgb2luma(textureLod(source_color, uv2, 0.0).xyz * exposure * params.luminance_multiplier); |
| 631 |
lumaEnd1 -= lumaLocalAverage; |
| 632 |
lumaEnd2 -= lumaLocalAverage; |
| 633 |
|
| 634 |
bool reached1 = abs(lumaEnd1) >= gradientScaled; |
| 635 |
bool reached2 = abs(lumaEnd2) >= gradientScaled; |
| 636 |
bool reachedBoth = reached1 && reached2; |
| 637 |
|
| 638 |
if (!reached1) { |
| 639 |
uv1 -= offset * QUALITY(1); |
| 640 |
} |
| 641 |
if (!reached2) { |
| 642 |
uv2 += offset * QUALITY(1); |
| 643 |
} |
| 644 |
|
| 645 |
if (!reachedBoth) { |
| 646 |
for (int i = 2; i < ITERATIONS; i++) { |
| 647 |
if (!reached1) { |
| 648 |
lumaEnd1 = rgb2luma(textureLod(source_color, uv1, 0.0).xyz * exposure * params.luminance_multiplier); |
| 649 |
lumaEnd1 = lumaEnd1 - lumaLocalAverage; |
| 650 |
} |
| 651 |
if (!reached2) { |
| 652 |
lumaEnd2 = rgb2luma(textureLod(source_color, uv2, 0.0).xyz * exposure * params.luminance_multiplier); |
| 653 |
lumaEnd2 = lumaEnd2 - lumaLocalAverage; |
| 654 |
} |
| 655 |
reached1 = abs(lumaEnd1) >= gradientScaled; |
| 656 |
reached2 = abs(lumaEnd2) >= gradientScaled; |
| 657 |
reachedBoth = reached1 && reached2; |
| 658 |
if (!reached1) { |
| 659 |
uv1 -= offset * QUALITY(i); |
| 660 |
} |
| 661 |
if (!reached2) { |
| 662 |
uv2 += offset * QUALITY(i); |
| 663 |
} |
| 664 |
if (reachedBoth) { |
| 665 |
break; |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
float distance1 = isHorizontal ? (uv_interp.x - uv1.x) : (uv_interp.y - uv1.y); |
| 671 |
float distance2 = isHorizontal ? (uv2.x - uv_interp.x) : (uv2.y - uv_interp.y); |
| 672 |
|
| 673 |
bool isDirection1 = distance1 < distance2; |
| 674 |
float distanceFinal = min(distance1, distance2); |
| 675 |
|
| 676 |
float edgeThickness = (distance1 + distance2); |
| 677 |
|
| 678 |
bool isLumaCenterSmaller = lumaCenter < lumaLocalAverage; |
| 679 |
|
| 680 |
bool correctVariation1 = (lumaEnd1 < 0.0) != isLumaCenterSmaller; |
| 681 |
bool correctVariation2 = (lumaEnd2 < 0.0) != isLumaCenterSmaller; |
| 682 |
|
| 683 |
bool correctVariation = isDirection1 ? correctVariation1 : correctVariation2; |
| 684 |
|
| 685 |
float pixelOffset = -distanceFinal / edgeThickness + 0.5; |
| 686 |
|
| 687 |
float finalOffset = correctVariation ? pixelOffset : 0.0; |
| 688 |
|
| 689 |
float lumaAverage = (1.0 / 12.0) * (2.0 * (lumaDownUp + lumaLeftRight) + lumaLeftCorners + lumaRightCorners); |
| 690 |
|
| 691 |
float subPixelOffset1 = clamp(abs(lumaAverage - lumaCenter) / lumaRange, 0.0, 1.0); |
| 692 |
float subPixelOffset2 = (-2.0 * subPixelOffset1 + 3.0) * subPixelOffset1 * subPixelOffset1; |
| 693 |
|
| 694 |
float subPixelOffsetFinal = subPixelOffset2 * subPixelOffset2 * SUBPIXEL_QUALITY; |
| 695 |
|
| 696 |
finalOffset = max(finalOffset, subPixelOffsetFinal); |
| 697 |
|
| 698 |
vec2 finalUv = uv_interp; |
| 699 |
if (isHorizontal) { |
| 700 |
finalUv.y += finalOffset * stepLength; |
| 701 |
} else { |
| 702 |
finalUv.x += finalOffset * stepLength; |
| 703 |
} |
| 704 |
|
| 705 |
vec3 finalColor = textureLod(source_color, finalUv, 0.0).xyz * exposure * params.luminance_multiplier; |
| 706 |
return finalColor; |
| 707 |
|
| 708 |
#endif |
| 709 |
} |
| 710 |
#endif // !SUBPASS |
| 711 |
|
| 712 |
// From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf |
| 713 |
// and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom) |
| 714 |
// NOTE: `frag_coord` is in pixels (i.e. not normalized UV). |
| 715 |
// This dithering must be applied after encoding changes (linear/nonlinear) have been applied |
| 716 |
// as the final step before quantization from floating point to integer values. |
| 717 |
vec3 screen_space_dither(vec2 frag_coord, float bit_alignment_diviser) { |
| 718 |
// Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR. |
| 719 |
// Removed the time component to avoid passing time into this shader. |
| 720 |
vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord)); |
| 721 |
dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0)); |
| 722 |
|
| 723 |
// Subtract 0.5 to avoid slightly brightening the whole viewport. |
| 724 |
// Use a dither strength of 100% rather than the 37.5% suggested by the original source. |
| 725 |
return (dither.rgb - 0.5) / bit_alignment_diviser; |
| 726 |
} |
| 727 |
|
| 728 |
void main() { |
| 729 |
#ifdef SUBPASS |
| 730 |
// SUBPASS and USE_MULTIVIEW can be combined but in that case we're already reading from the correct layer |
| 731 |
#ifdef USE_MULTIVIEW |
| 732 |
// In order to ensure the `SpvCapabilityMultiView` is included in the SPIR-V capabilities, gl_ViewIndex must |
| 733 |
// be read in the shader. Without this, transpilation to Metal fails to include the multi-view variant. |
| 734 |
uint vi = ViewIndex; |
| 735 |
#endif |
| 736 |
vec4 color = subpassLoad(input_color); |
| 737 |
#elif defined(USE_MULTIVIEW) |
| 738 |
vec4 color = textureLod(source_color, vec3(uv_interp, ViewIndex), 0.0f); |
| 739 |
#else |
| 740 |
vec4 color = textureLod(source_color, uv_interp, 0.0f); |
| 741 |
#endif |
| 742 |
color.rgb *= params.luminance_multiplier; |
| 743 |
|
| 744 |
// Exposure |
| 745 |
|
| 746 |
color.rgb *= params.exposure; |
| 747 |
|
| 748 |
#ifndef SUBPASS |
| 749 |
// Single-pass FXAA and pre-tonemap glow. |
| 750 |
if (use_fxaa) { |
| 751 |
// FXAA must be performed before glow to preserve the "bleed" effect of glow. |
| 752 |
color.rgb = do_fxaa(color.rgb, params.exposure, uv_interp); |
| 753 |
} |
| 754 |
|
| 755 |
if (use_glow && !glow_mode_softlight) { |
| 756 |
vec3 glow = gather_glow() * params.glow_intensity; |
| 757 |
if (use_glow_map) { |
| 758 |
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength); |
| 759 |
} |
| 760 |
|
| 761 |
if (glow_mode_mix) { |
| 762 |
color.rgb = color.rgb * (1.0 - params.glow_intensity) + glow; |
| 763 |
} else { |
| 764 |
color.rgb = apply_glow(color.rgb, glow, params.white); |
| 765 |
} |
| 766 |
} |
| 767 |
#endif |
| 768 |
|
| 769 |
// Tonemap to lower dynamic range. |
| 770 |
|
| 771 |
color.rgb = apply_tonemapping(color.rgb); |
| 772 |
|
| 773 |
#ifndef SUBPASS |
| 774 |
// Post-tonemap glow. |
| 775 |
|
| 776 |
if (use_glow && glow_mode_softlight) { |
| 777 |
// Apply soft light after tonemapping to mitigate the issue of discontinuity |
| 778 |
// at 1.0 and higher. This makes the issue only appear with HDR output that |
| 779 |
// can exceed a 1.0 output value. |
| 780 |
vec3 glow = gather_glow() * params.glow_intensity; |
| 781 |
if (use_glow_map) { |
| 782 |
glow = mix(glow, texture(glow_map, uv_interp).rgb * glow, params.glow_map_strength); |
| 783 |
} |
| 784 |
glow = apply_tonemapping(glow); |
| 785 |
color.rgb = apply_glow(color.rgb, glow, params.white); |
| 786 |
} |
| 787 |
#endif |
| 788 |
|
| 789 |
// Additional effects. |
| 790 |
|
| 791 |
if (use_bcs) { |
| 792 |
// Apply brightness: |
| 793 |
// Apply to relative luminance. This ensures that the hue and saturation of |
| 794 |
// colors is not affected by the adjustment, but requires the multiplication |
| 795 |
// to be performed on linear-encoded values. |
| 796 |
color.rgb = color.rgb * params.bcs.x; |
| 797 |
|
| 798 |
color.rgb = linear_to_srgb(color.rgb); |
| 799 |
|
| 800 |
// Apply contrast: |
| 801 |
// By applying contrast to RGB values that are perceptually uniform (nonlinear), |
| 802 |
// the darkest values are not hard-clipped as badly, which produces a |
| 803 |
// higher quality contrast adjustment and maintains compatibility with |
| 804 |
// existing projects. |
| 805 |
color.rgb = mix(vec3(0.5), color.rgb, params.bcs.y); |
| 806 |
|
| 807 |
// Apply saturation: |
| 808 |
// By applying saturation adjustment to nonlinear sRGB-encoded values with |
| 809 |
// even weights the preceived brightness of blues are affected, but this |
| 810 |
// maintains compatibility with existing projects. |
| 811 |
color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * (1.0 / 3.0)), color.rgb, params.bcs.z); |
| 812 |
|
| 813 |
if (use_color_correction) { |
| 814 |
color.rgb = clamp(color.rgb, vec3(0.0), vec3(1.0)); |
| 815 |
color.rgb = apply_color_correction(color.rgb); |
| 816 |
// When using color correction and convert_to_srgb is false, there |
| 817 |
// is no need to convert back to linear because the color correction |
| 818 |
// texture sampling does this for us. |
| 819 |
} else if (!convert_to_srgb) { |
| 820 |
color.rgb = srgb_to_linear(color.rgb); |
| 821 |
} |
| 822 |
} else if (convert_to_srgb) { |
| 823 |
color.rgb = linear_to_srgb(color.rgb); // Regular linear -> SRGB conversion. |
| 824 |
} |
| 825 |
|
| 826 |
// Debanding should be done at the end of tonemapping, but before writing to the LDR buffer. |
| 827 |
// Otherwise, we're adding noise to an already-quantized image. |
| 828 |
if (deband_8_bit) { |
| 829 |
// Divide by 255 to align to 8-bit quantization. |
| 830 |
color.rgb += screen_space_dither(gl_FragCoord.xy, 255.0); |
| 831 |
} else if (deband_10_bit) { |
| 832 |
// Divide by 1023 to align to 10-bit quantization. |
| 833 |
color.rgb += screen_space_dither(gl_FragCoord.xy, 1023.0); |
| 834 |
} |
| 835 |
|
| 836 |
frag_color = color; |
| 837 |
} |