Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1import bevy_render::{
2 view::ColorGrading,
3 color_operations::{hsv_to_rgb, rgb_to_hsv},
4 maths::{PI_2, powsafe},
5};
7import package::tonemapping::lut_bindings::{
8 dt_lut_texture,
9 dt_lut_sampler,
10};
12/// Half the size of the crossfade region between shadows and midtones and
13/// between midtones and highlights. This value, 0.1, corresponds to 10% of the
14/// gamut on either side of the cutoff point.
15const LEVEL_MARGIN: f32 = 0.1;
17/// The inverse reciprocal of twice the above, used when scaling the midtone
18/// region.
19const LEVEL_MARGIN_DIV: f32 = 0.5 / LEVEL_MARGIN;
21fn sample_current_lut(p: vec3<f32>) -> vec3<f32> {
22 // Don't include code that will try to sample from LUTs if tonemap method doesn't require it
23 // Allows this file to be imported without necessarily needing the lut texture bindings
24@if(TONEMAP_METHOD_AGX)
25 return textureSampleLevel(dt_lut_texture, dt_lut_sampler, p, 0.0).rgb;
26@elif(TONEMAP_METHOD_TONY_MC_MAPFACE)
27 return textureSampleLevel(dt_lut_texture, dt_lut_sampler, p, 0.0).rgb;
28@elif(TONEMAP_METHOD_BLENDER_FILMIC)
29 return textureSampleLevel(dt_lut_texture, dt_lut_sampler, p, 0.0).rgb;
30@else
31 return vec3(1.0, 0.0, 1.0);
34// --------------------------------------
35// --- SomewhatBoringDisplayTransform ---
36// --------------------------------------
37// By Tomasz Stachowiak
39fn rgb_to_ycbcr(col: vec3<f32>) -> vec3<f32> {
40 let m = mat3x3<f32>(
41 0.2126, 0.7152, 0.0722,
42 -0.1146, -0.3854, 0.5,
43 0.5, -0.4542, -0.0458
44 );
45 return col * m;
48fn tonemap_curve(v: f32) -> f32 {
49@if(false) {
50 // Large linear part in the lows, but compresses highs.
51 let c = v + v * v + 0.5 * v * v * v;
52 return c / (1.0 + c);
53} @else {
54 return 1.0 - exp(-v);
58fn tonemap_curve3_(v: vec3<f32>) -> vec3<f32> {
59 return vec3(tonemap_curve(v.r), tonemap_curve(v.g), tonemap_curve(v.b));
62fn somewhat_boring_display_transform(col: vec3<f32>) -> vec3<f32> {
63 var boring_color = col;
64 let ycbcr = rgb_to_ycbcr(boring_color);
66 let bt = tonemap_curve(length(ycbcr.yz) * 2.4);
67 var desat = max((bt - 0.7) * 0.8, 0.0);
68 desat *= desat;
70 let desat_col = mix(boring_color.rgb, ycbcr.xxx, desat);
72 let tm_luma = tonemap_curve(ycbcr.x);
73 let tm0 = boring_color.rgb * max(0.0, tm_luma / max(1e-5, tonemapping_luminance(boring_color.rgb)));
74 let final_mult = 0.97;
75 let tm1 = tonemap_curve3_(desat_col);
77 boring_color = mix(tm0, tm1, bt * bt);
79 return boring_color * final_mult;
82// ------------------------------------------
83// ------------- Tony McMapface -------------
84// ------------------------------------------
85// By Tomasz Stachowiak
86// https://github.com/h3r2tic/tony-mc-mapface
88const TONY_MC_MAPFACE_LUT_DIMS: f32 = 48.0;
90fn sample_tony_mc_mapface_lut(stimulus: vec3<f32>) -> vec3<f32> {
91 var uv = (stimulus / (stimulus + 1.0)) * (f32(TONY_MC_MAPFACE_LUT_DIMS - 1.0) / f32(TONY_MC_MAPFACE_LUT_DIMS)) + 0.5 / f32(TONY_MC_MAPFACE_LUT_DIMS);
92 return sample_current_lut(saturate(uv)).rgb;
95// ---------------------------------
96// ---------- ACES Fitted ----------
97// ---------------------------------
99// Same base implementation that Godot 4.0 uses for Tonemap ACES.
101// https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
103// The code in this file was originally written by Stephen Hill (@self_shadow), who deserves all
104// credit for coming up with this fit and implementing it. Buy him a beer next time you see him. :)
106fn RRTAndODTFit(v: vec3<f32>) -> vec3<f32> {
107 let a = v * (v + 0.0245786) - 0.000090537;
108 let b = v * (0.983729 * v + 0.4329510) + 0.238081;
109 return a / b;
112fn ACESFitted(color: vec3<f32>) -> vec3<f32> {
113 var fitted_color = color;
115 // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
116 let rgb_to_rrt = mat3x3<f32>(
117 vec3(0.59719, 0.35458, 0.04823),
118 vec3(0.07600, 0.90834, 0.01566),
119 vec3(0.02840, 0.13383, 0.83777)
120 );
122 // ODT_SAT => XYZ => D60_2_D65 => sRGB
123 let odt_to_rgb = mat3x3<f32>(
124 vec3(1.60475, -0.53108, -0.07367),
125 vec3(-0.10208, 1.10813, -0.00605),
126 vec3(-0.00327, -0.07276, 1.07602)
127 );
129 fitted_color *= rgb_to_rrt;
131 // Apply RRT and ODT
132 fitted_color = RRTAndODTFit(fitted_color);
134 fitted_color *= odt_to_rgb;
136 // Clamp to [0, 1]
137 fitted_color = saturate(fitted_color);
139 return fitted_color;
142// -------------------------------
143// ------------- AgX -------------
144// -------------------------------
145// By Troy Sobotka
146// https://github.com/MrLixm/AgXc
147// https://github.com/sobotka/AgX
149/*
150 Increase color saturation of the given color data.
151 :param color: expected sRGB primaries input
152 :param saturationAmount: expected 0-1 range with 1=neutral, 0=no saturation.
153 -- ref[2] [4]
154*/
155fn saturation(color: vec3<f32>, saturationAmount: f32) -> vec3<f32> {
156 let luma = tonemapping_luminance(color);
157 return mix(vec3(luma), color, vec3(saturationAmount));
160/*
161 Output log domain encoded data.
162 Similar to OCIO lg2 AllocationTransform.
163 ref[0]
164*/
165fn convertOpenDomainToNormalizedLog2_(color: vec3<f32>, minimum_ev: f32, maximum_ev: f32) -> vec3<f32> {
166 let in_midgray = 0.18;
168 // remove negative before log transform
169 var normalized_color = max(vec3(0.0), color);
170 // avoid infinite issue with log -- ref[1]
171 normalized_color = select(normalized_color, 0.00001525878 + normalized_color, normalized_color < vec3<f32>(0.00003051757));
172 normalized_color = clamp(
173 log2(normalized_color / in_midgray),
174 vec3(minimum_ev),
175 vec3(maximum_ev)
176 );
177 let total_exposure = maximum_ev - minimum_ev;
179 return (normalized_color - minimum_ev) / total_exposure;
182// Inverse of above
183fn convertNormalizedLog2ToOpenDomain(color: vec3<f32>, minimum_ev: f32, maximum_ev: f32) -> vec3<f32> {
184 var open_color = color;
185 let in_midgray = 0.18;
186 let total_exposure = maximum_ev - minimum_ev;
188 open_color = (open_color * total_exposure) + minimum_ev;
189 open_color = pow(vec3(2.0), open_color);
190 open_color = open_color * in_midgray;
192 return open_color;
196/*=================
197 Main processes
198=================*/
200/// Prepare the data for display encoding. Converted to log domain.
201fn applyAgXLog(Image: vec3<f32>) -> vec3<f32> {
202 var prepared_image = max(vec3(0.0), Image); // clamp negatives
203 let r = dot(prepared_image, vec3(0.84247906, 0.0784336, 0.07922375));
204 let g = dot(prepared_image, vec3(0.04232824, 0.87846864, 0.07916613));
205 let b = dot(prepared_image, vec3(0.04237565, 0.0784336, 0.87914297));
206 prepared_image = vec3(r, g, b);
208 prepared_image = convertOpenDomainToNormalizedLog2_(prepared_image, -10.0, 6.5);
210 prepared_image = clamp(prepared_image, vec3(0.0), vec3(1.0));
211 return prepared_image;
214fn applyLUT3D(Image: vec3<f32>, block_size: f32) -> vec3<f32> {
215 return sample_current_lut(Image * ((block_size - 1.0) / block_size) + 0.5 / block_size).rgb;
218// -------------------------
219// -------------------------
220// -------------------------
222fn sample_blender_filmic_lut(stimulus: vec3<f32>) -> vec3<f32> {
223 let block_size = 64.0;
224 let normalized = saturate(convertOpenDomainToNormalizedLog2_(stimulus, -11.0, 12.0));
225 return applyLUT3D(normalized, block_size);
228/// from https://64.github.io/tonemapping/
229/// reinhard on RGB oversaturates colors
230fn tonemapping_reinhard(color: vec3<f32>) -> vec3<f32> {
231 return color / (1.0 + color);
234/// luminance coefficients from Rec. 709.
235/// https://en.wikipedia.org/wiki/Rec._709
236fn tonemapping_luminance(v: vec3<f32>) -> f32 {
237 return dot(v, vec3<f32>(0.2126, 0.7152, 0.0722));
240fn tonemapping_change_luminance(c_in: vec3<f32>, l_out: f32) -> vec3<f32> {
241 let l_in = tonemapping_luminance(c_in);
242 return c_in * (l_out / l_in);
245fn tonemapping_reinhard_luminance(color: vec3<f32>) -> vec3<f32> {
246 let l_old = tonemapping_luminance(color);
247 let l_new = l_old / (1.0 + l_old);
248 return tonemapping_change_luminance(color, l_new);
251fn rgb_to_srgb_simple(color: vec3<f32>) -> vec3<f32> {
252 return pow(color, vec3<f32>(1.0 / 2.2));
255/// PBR Neutral tone mapping
256/// https://github.com/KhronosGroup/ToneMapping/blob/main/PBR_Neutral/pbrNeutral.glsl
257/// Adapted under Apache 2.0, per https://github.com/KhronosGroup/ToneMapping/tree/main/LICENSES
258fn tonemapping_pbr_neutral(color_in: vec3<f32>) -> vec3<f32> {
259 // Parameter controlling when highlight compression starts
260 // (`K_s` in specification)
261 const start_compression: f32 = 0.8 - 0.04;
263 // Parameter controlling the speed of desaturation
264 // (`K-d` in specification)
265 const desaturation: f32 = 0.15;
267 // `x` in the specification equation
268 let min_channel = min(color_in.r, min(color_in.g, color_in.b));
270 // The amount that the "toe" adjustment reduces the color
271 // `f` in the specification equation
272 let offset = select(0.04, min_channel - 6.25 * min_channel * min_channel, min_channel < 0.08);
274 // The original color, minus the "toe" reduction
275 // `c_in - f` in the specification equation
276 let offset_color = color_in - offset;
278 // Maximum of all offset color channels
279 // `p` in the specification equation
280 let max_channel = max(offset_color.r, max(offset_color.g, offset_color.b));
281 if max_channel < start_compression {
282 // "toe" at the low-end; or uncompressed, offset color for most of the range
283 return offset_color;
284 }
286 // This doesn't exist in the specification equation. It is part of optimizing
287 // the math for computation.
288 let d = 1.0 - start_compression;
290 // Maximum color channel, scaled to asymptotically approach 1.0
291 let new_max_channel = 1.0 - d * d / (max_channel + d - start_compression);
293 // Full color, from offset color, with the same scale applied as `new_max_channel`
294 // `p_n` in the specification equation
295 let color = offset_color * (new_max_channel / max_channel);
297 // Amount to desaturate, used as the blend factor when mixing between
298 // full color and desaturated.
299 let g = 1.0 - 1.0 / (desaturation * (max_channel - new_max_channel) + 1.0);
300 return mix(color, vec3(new_max_channel), g);
303/// Source: Advanced VR Rendering, GDC 2015, Alex Vlachos, Valve, Slide 49
304/// https://media.steampowered.com/apps/valve/2015/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
305fn screen_space_dither(frag_coord: vec2<f32>) -> vec3<f32> {
306 var dither = vec3<f32>(dot(vec2<f32>(171.0, 231.0), frag_coord)).xxx;
307 dither = fract(dither.rgb / vec3<f32>(103.0, 71.0, 97.0));
308 return (dither - 0.5) / 255.0;
311/// Performs the "sectional" color grading: i.e. the color grading that applies
312/// individually to shadows, midtones, and highlights.
313fn sectional_color_grading(
314 in: vec3<f32>,
315 color_grading: ptr<function, ColorGrading>,
316) -> vec3<f32> {
317 var color = in;
319 // Determine whether the color is a shadow, midtone, or highlight. Colors
320 // close to the edges are considered a mix of both, to avoid sharp
321 // discontinuities. The formulas are taken from Blender's compositor.
323 let level = (color.r + color.g + color.b) / 3.0;
325 // Determine whether this color is a shadow, midtone, or highlight. If close
326 // to the cutoff points, blend between the two to avoid sharp color
327 // discontinuities.
328 var levels = vec3(0.0);
329 let midtone_range = (*color_grading).midtone_range;
330 if (level < midtone_range.x - LEVEL_MARGIN) {
331 levels.x = 1.0;
332 } else if (level < midtone_range.x + LEVEL_MARGIN) {
333 levels.y = ((level - midtone_range.x) * LEVEL_MARGIN_DIV) + 0.5;
334 levels.z = 1.0 - levels.y;
335 } else if (level < midtone_range.y - LEVEL_MARGIN) {
336 levels.y = 1.0;
337 } else if (level < midtone_range.y + LEVEL_MARGIN) {
338 levels.z = ((level - midtone_range.y) * LEVEL_MARGIN_DIV) + 0.5;
339 levels.y = 1.0 - levels.z;
340 } else {
341 levels.z = 1.0;
342 }
344 // Calculate contrast/saturation/gamma/gain/lift.
345 let contrast = dot(levels, (*color_grading).contrast);
346 let saturation = dot(levels, (*color_grading).saturation);
347 let gamma = dot(levels, (*color_grading).gamma);
348 let gain = dot(levels, (*color_grading).gain);
349 let lift = dot(levels, (*color_grading).lift);
351 // Adjust saturation and contrast.
352 let luma = tonemapping_luminance(color);
353 color = luma + saturation * (color - luma);
354 color = 0.5 + (color - 0.5) * contrast;
356 // The [ASC CDL] formula for color correction. Given *i*, an input color, we
357 // have:
358 //
359 // out = (i × s + o)ⁿ
360 //
361 // Following the normal photographic naming convention, *gain* is the *s*
362 // factor, *lift* is the *o* term, and the inverse of *gamma* is the *n*
363 // exponent.
364 //
365 // [ASC CDL]: https://en.wikipedia.org/wiki/ASC_CDL#Combined_Function
366 color = powsafe(color * gain + lift, 1.0 / gamma);
368 // Account for exposure.
369 color = color * powsafe(vec3(2.0), (*color_grading).exposure);
370 return max(color, vec3(0.0));
373fn tone_mapping(in: vec4<f32>, in_color_grading: ColorGrading) -> vec4<f32> {
374 var color = max(in.rgb, vec3(0.0));
375 var color_grading = in_color_grading; // So we can take pointers to it.
377 // Rotate hue if needed, by converting to and from HSV. Remember that hue is
378 // an angle, so it needs to be modulo 2π.
379@if(HUE_ROTATE) {
380 var hsv = rgb_to_hsv(color);
381 hsv.r = (hsv.r + color_grading.hue) % PI_2;
382 color = hsv_to_rgb(hsv);
385 // Perform white balance correction. Conveniently, this is a linear
386 // transform. The matrix was pre-calculated from the temperature and tint
387 // values on the CPU.
388@if(WHITE_BALANCE) {
389 color = max(color_grading.balance * color, vec3(0.0));
392 // Perform the "sectional" color grading: i.e. the color grading that
393 // applies individually to shadows, midtones, and highlights.
394@if(SECTIONAL_COLOR_GRADING) {
395 color = sectional_color_grading(color, &color_grading);
396} @else {
397 // If we're not doing sectional color grading, the exposure might still need
398 // to be applied, for example when using auto exposure.
399 color = color * powsafe(vec3(2.0), color_grading.exposure);
402 // tone_mapping
403@if(TONEMAP_METHOD_LINEAR) {
404 color = color;
405} @elif(TONEMAP_METHOD_REINHARD) {
406 color = tonemapping_reinhard(color.rgb);
407} @elif(TONEMAP_METHOD_REINHARD_LUMINANCE) {
408 color = tonemapping_reinhard_luminance(color.rgb);
409} @elif(TONEMAP_METHOD_ACES_FITTED) {
410 color = ACESFitted(color.rgb);
411} @elif(TONEMAP_METHOD_AGX) {
412 color = applyAgXLog(color);
413 color = applyLUT3D(color, 32.0);
414} @elif(TONEMAP_METHOD_SOMEWHAT_BORING_DISPLAY_TRANSFORM) {
415 color = somewhat_boring_display_transform(color.rgb);
416} @elif(TONEMAP_METHOD_TONY_MC_MAPFACE) {
417 color = sample_tony_mc_mapface_lut(color);
418} @elif(TONEMAP_METHOD_BLENDER_FILMIC) {
419 color = sample_blender_filmic_lut(color.rgb);
420} @elif(TONEMAP_METHOD_PBR_NEUTRAL) {
421 color = tonemapping_pbr_neutral(color.rgb);
424 // Perceptual post tonemapping grading
425 color = saturation(color, color_grading.post_saturation);
427 return vec4(color, in.a);
430/// This is an **incredibly crude** approximation of the inverse of the tone mapping function.
431/// We assume here that there's a simple linear relationship between the input and output
432/// which is not true at all, but useful to at least preserve the overall luminance of colors
433/// when sampling from an already tonemapped image. (e.g. for transmissive materials when HDR is off)
434fn approximate_inverse_tone_mapping(in: vec4<f32>, color_grading: ColorGrading) -> vec4<f32> {
435 let out = tone_mapping(in, color_grading);
436 let approximate_ratio = length(in.rgb) / length(out.rgb);
437 return vec4(in.rgb * approximate_ratio, in.a);
Contents