From 05b507d9008edc84fa82edb657fbb15880b79210 Mon Sep 17 00:00:00 2001 From: jpark37 Date: Tue, 22 Jun 2021 04:34:48 -0700 Subject: [PATCH] libobs: DrawSrgbDecompressPremultiplied technique Necessary for an upcoming fix to browser source. --- libobs/data/default.effect | 17 +++++++++++++++++ libobs/data/default_rect.effect | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/libobs/data/default.effect b/libobs/data/default.effect index 0d754e1fd..bc343ad14 100644 --- a/libobs/data/default.effect +++ b/libobs/data/default.effect @@ -62,6 +62,14 @@ float4 PSDrawNonlinearAlpha(VertInOut vert_in) : TARGET return rgba; } +float4 PSDrawSrgbDecompressPremultiplied(VertInOut vert_in) : TARGET +{ + float4 rgba = image.Sample(def_sampler, vert_in.uv); + rgba.rgb = max(float3(0.0, 0.0, 0.0), rgba.rgb / rgba.a); + rgba.rgb = srgb_nonlinear_to_linear(rgba.rgb); + return rgba; +} + technique Draw { pass @@ -88,3 +96,12 @@ technique DrawNonlinearAlpha pixel_shader = PSDrawNonlinearAlpha(vert_in); } } + +technique DrawSrgbDecompressPremultiplied +{ + pass + { + vertex_shader = VSDefault(vert_in); + pixel_shader = PSDrawSrgbDecompressPremultiplied(vert_in); + } +} diff --git a/libobs/data/default_rect.effect b/libobs/data/default_rect.effect index dabb43294..e8f114fde 100644 --- a/libobs/data/default_rect.effect +++ b/libobs/data/default_rect.effect @@ -30,6 +30,24 @@ float4 PSDrawOpaque(VertInOut vert_in) : TARGET return float4(image.Sample(def_sampler, vert_in.uv).rgb, 1.0); } +float srgb_nonlinear_to_linear_channel(float u) +{ + return (u <= 0.04045) ? (u / 12.92) : pow((u + 0.055) / 1.055, 2.4); +} + +float3 srgb_nonlinear_to_linear(float3 v) +{ + return float3(srgb_nonlinear_to_linear_channel(v.r), srgb_nonlinear_to_linear_channel(v.g), srgb_nonlinear_to_linear_channel(v.b)); +} + +float4 PSDrawSrgbDecompressPremultiplied(VertInOut vert_in) : TARGET +{ + float4 rgba = image.Sample(def_sampler, vert_in.uv); + rgba.rgb = max(float3(0.0, 0.0, 0.0), rgba.rgb / rgba.a); + rgba.rgb = srgb_nonlinear_to_linear(rgba.rgb); + return rgba; +} + technique Draw { pass @@ -47,3 +65,12 @@ technique DrawOpaque pixel_shader = PSDrawOpaque(vert_in); } } + +technique DrawSrgbDecompressPremultiplied +{ + pass + { + vertex_shader = VSDefault(vert_in); + pixel_shader = PSDrawSrgbDecompressPremultiplied(vert_in); + } +}