87 lines
3.0 KiB
HLSL
87 lines
3.0 KiB
HLSL
// HDR -> SDR tonemap for ByteSize screen capture. Hable filmic curve.
|
|
//
|
|
// Input: R16G16B16A16_FLOAT texture from Windows Graphics Capture, holding
|
|
// linear scRGB values (1.0 = SDR white ~80 nits, >1.0 = HDR highlights).
|
|
// Output: R8G8B8A8_UNORM render target, bt709-gamma-encoded SDR ready for the
|
|
// ffmpeg bgra pipe (matches what downstream encode.rs filters assume:
|
|
// in_transfer=bt709, tv range).
|
|
|
|
struct VsIn {
|
|
float2 pos : POSITION;
|
|
float2 uv : TEXCOORD0;
|
|
};
|
|
|
|
struct VsOut {
|
|
float4 pos : SV_POSITION;
|
|
float2 uv : TEXCOORD0;
|
|
};
|
|
|
|
VsOut fullscreen_vs(VsIn i) {
|
|
VsOut o;
|
|
o.pos = float4(i.pos, 0.0, 1.0);
|
|
o.uv = i.uv;
|
|
return o;
|
|
}
|
|
|
|
Texture2D<float4> captured : register(t0);
|
|
SamplerState point_clamp : register(s0);
|
|
|
|
// Hable filmic tonemap (Uncharted 2). Better midtone contrast than ACES for
|
|
// modest HDR ranges (our source peaks around 3x SDR white), so highlights
|
|
// roll off gradually instead of clamping flat near 1.0.
|
|
float3 hable(float3 x) {
|
|
const float A = 0.15; // shoulder strength
|
|
const float B = 0.50; // linear strength
|
|
const float C = 0.10; // linear angle
|
|
const float D = 0.20; // toe strength
|
|
const float E = 0.02; // toe numerator
|
|
const float F = 0.30; // toe denominator
|
|
return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
|
|
}
|
|
|
|
// bt709 OETF: linear light -> gamma-encoded signal. This is the exact ITU-R
|
|
// bt709 transfer, not the 1/2.2 approximation, so the output matches what
|
|
// ffmpeg's out_transfer=bt709 would produce.
|
|
float bt709_oetf(float lin) {
|
|
if (lin <= 0.018) {
|
|
return 4.5 * lin;
|
|
}
|
|
return 1.099 * pow(lin, 0.45) - 0.099;
|
|
}
|
|
|
|
// Compiled twice from this one source: TONEMAP=1 for an HDR output, TONEMAP=0
|
|
// for SDR. The capture surface is linear scRGB either way, but only HDR content
|
|
// reaches above 1.0 in it, so the same curve cannot be right for both.
|
|
#ifndef TONEMAP
|
|
#define TONEMAP 1
|
|
#endif
|
|
|
|
float4 tonemap_ps(VsOut i) : SV_TARGET {
|
|
// captured.rgb is linear scRGB, where 1.0 is 80 nits.
|
|
float4 c = captured.Sample(point_clamp, i.uv);
|
|
|
|
#if TONEMAP
|
|
float3 mapped = hable(c.rgb);
|
|
// Normalize by hable at the white point so 1.0 SDR white maps to ~1.0
|
|
// output rather than the raw hable value (which is >1). On an HDR desktop
|
|
// SDR white sits near 2.5 in these units (203 nits), so normalizing at 4.0
|
|
// is what leaves the highlights somewhere to roll off to.
|
|
float3 white = hable(float3(4.0, 4.0, 4.0));
|
|
float3 sdr = mapped / white;
|
|
#else
|
|
// SDR: 1.0 is already display white and there is nothing above it to
|
|
// compress. Running this through Hable regardless would map white to about
|
|
// 0.65 and darken the whole frame by a third.
|
|
float3 sdr = saturate(c.rgb);
|
|
#endif
|
|
|
|
// Apply bt709 OETF so the BGRA8 bytes leave the GPU already gamma-encoded,
|
|
// matching the SDR capture pipeline's contract.
|
|
float3 gamma = float3(
|
|
bt709_oetf(sdr.r),
|
|
bt709_oetf(sdr.g),
|
|
bt709_oetf(sdr.b)
|
|
);
|
|
return float4(gamma, 1.0);
|
|
}
|