tonemap hdr captures on the gpu
firefox playing hdr youtube renders the desktop in linear scrgb, and capturing it as bgra8 clamps the >1.0 highlights to pure white. capture in r16g16b16a16_float instead and run a hable filmic + bt709 pixel shader directly on the captured d3d11 texture (which windows-capture already exposes via frame.as_raw_texture), then read back bgra8 so the ffmpeg pipe stays unchanged from the sdr path. the shader is compiled offline with fxc at build time and embedded. the fullscreen-triangle draw needs an explicit cull-none rasterizer state since the default back-face cull silently drops it.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// 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;
|
||||
}
|
||||
|
||||
float4 tonemap_ps(VsOut i) : SV_TARGET {
|
||||
// captured.rgb is linear scRGB (1.0 = SDR white; HDR highlights >1.0).
|
||||
float4 c = captured.Sample(point_clamp, i.uv);
|
||||
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).
|
||||
float3 white = hable(float3(4.0, 4.0, 4.0));
|
||||
float3 sdr = mapped / white;
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user