summaryrefslogtreecommitdiffstats
path: root/tone.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2024-11-18 17:51:12 -0800
committeryum <yum.food.vr@gmail.com>2024-11-18 17:51:12 -0800
commit5fdbeddf4b0002ae292c6f4081a0c9baeda919bf (patch)
treebe0ca6381b12adc4abfe79b4d81d64069b828696 /tone.cginc
parent43df9fc828a0103ebac98194dbf3f19b88ebed75 (diff)
Refactor tonemapping curves into their own headers
Also add iq's "almost identity" curve.
Diffstat (limited to 'tone.cginc')
-rw-r--r--tone.cginc51
1 files changed, 51 insertions, 0 deletions
diff --git a/tone.cginc b/tone.cginc
new file mode 100644
index 0000000..371a8db
--- /dev/null
+++ b/tone.cginc
@@ -0,0 +1,51 @@
+#include "tone_iq.cginc"
+
+#ifndef __TONE_INC
+#define __TONE_INC
+
+// This library contains a bunch of useful tonemapping curves.
+
+// https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
+// cc0
+float3 aces_filmic(float3 x) {
+ float a = 2.51f;
+ float b = 0.03f;
+ float c = 2.43f;
+ float d = 0.59f;
+ float e = 0.14f;
+ return saturate((x*(a*x+b))/(x*(c*x+d)+e));
+}
+
+// Clamp x to [0, k].
+// Assumes that x is already on [0, 1].
+// Nice properties:
+// 1. At x=0, the derivative is 1.
+// 2. No transcendental ops, and branchless.
+float3 smooth_clamp(float3 x, float k) {
+ // Derivation of `b` from `k`:
+ // f(x, b) = b * x / (x + b)
+ // We want f(1, b) = k.
+ // In other words, we want the max value the function can take on [0, 1] to
+ // be k.
+ // k = f(1, b)
+ // = b / (1 + b)
+ // b = k * (1 + b)
+ // = k + kb
+ // 1 = k/b + k
+ // 1 - k = k/b
+ // 1/(1-k) = b/k
+ // b = k/(1-k)
+ float e = 1E-4;
+ k = min(1-e, k);
+ float b = k/(1-k);
+ return b * x / (x + b);
+}
+float smooth_clamp(float x, float k) {
+ float e = 1E-4;
+ k = min(1-e, k);
+ float b = k/(1-k);
+ return b * x / (x + b);
+}
+
+#endif // __TONE_INC
+