libobs: Add color spaces to scale shaders

master
jpark37 2022-03-22 22:46:35 -07:00 committed by Jim
parent f0f0cb3038
commit a8bc994f07
8 changed files with 418 additions and 69 deletions

View File

@ -210,6 +210,7 @@ if(NOT OS_MACOS)
PRIVATE data/area.effect
data/bicubic_scale.effect
data/bilinear_lowres_scale.effect
data/color.effect
data/default.effect
data/default_rect.effect
data/deinterlace_base.effect

View File

@ -1,7 +1,10 @@
#include "color.effect"
uniform float4x4 ViewProj;
uniform float2 base_dimension;
uniform float2 base_dimension_i;
uniform texture2d image;
uniform float multiplier;
sampler_state textureSampler {
Filter = Linear;
@ -31,8 +34,9 @@ VertInOut VSDefault(VertData vert_in)
return vert_out;
}
float4 DrawArea(float2 uv)
float4 DrawArea(FragData frag_in)
{
float2 uv = frag_in.uv;
float2 uv_delta = float2(ddx(uv.x), ddy(uv.y));
// Handle potential OpenGL flip.
@ -84,18 +88,44 @@ float4 DrawArea(float2 uv)
float4 PSDrawAreaRGBA(FragData frag_in) : TARGET
{
return DrawArea(frag_in.uv);
return DrawArea(frag_in);
}
float4 PSDrawAreaRGBAMultiply(FragData frag_in) : TARGET
{
float4 rgba = DrawArea(frag_in);
rgba.rgb *= multiplier;
return rgba;
}
float4 PSDrawAreaRGBATonemap(FragData frag_in) : TARGET
{
float4 rgba = DrawArea(frag_in);
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawAreaRGBAMultiplyTonemap(FragData frag_in) : TARGET
{
float4 rgba = DrawArea(frag_in);
rgba.rgb *= multiplier;
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawAreaRGBADivide(FragData frag_in) : TARGET
{
float4 rgba = DrawArea(frag_in.uv);
float4 rgba = DrawArea(frag_in);
float alpha = rgba.a;
float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
return float4(rgba.rgb * multiplier, alpha);
}
float4 PSDrawAreaRGBAUpscale(FragData frag_in) : TARGET
float4 DrawAreaUpscale(FragData frag_in)
{
float2 uv = frag_in.uv;
float2 uv_delta = float2(ddx(uv.x), ddy(uv.y));
@ -124,6 +154,37 @@ float4 PSDrawAreaRGBAUpscale(FragData frag_in) : TARGET
return image.Sample(textureSampler, uv);
}
float4 PSDrawAreaRGBAUpscale(FragData frag_in) : TARGET
{
return DrawAreaUpscale(frag_in);
}
float4 PSDrawAreaRGBAUpscaleMultiply(FragData frag_in) : TARGET
{
float4 rgba = DrawAreaUpscale(frag_in);
rgba.rgb *= multiplier;
return rgba;
}
float4 PSDrawAreaRGBAUpscaleTonemap(FragData frag_in) : TARGET
{
float4 rgba = DrawAreaUpscale(frag_in);
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawAreaRGBAUpscaleMultiplyTonemap(FragData frag_in) : TARGET
{
float4 rgba = DrawAreaUpscale(frag_in);
rgba.rgb *= multiplier;
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
technique Draw
{
pass
@ -133,6 +194,33 @@ technique Draw
}
}
technique DrawMultiply
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBAMultiply(frag_in);
}
}
technique DrawTonemap
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBATonemap(frag_in);
}
}
technique DrawMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBAMultiplyTonemap(frag_in);
}
}
technique DrawAlphaDivide
{
pass
@ -150,3 +238,30 @@ technique DrawUpscale
pixel_shader = PSDrawAreaRGBAUpscale(frag_in);
}
}
technique DrawUpscaleMultiply
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBAUpscaleMultiply(frag_in);
}
}
technique DrawUpscaleTonemap
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBAUpscaleTonemap(frag_in);
}
}
technique DrawUpscaleMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBAUpscaleMultiplyTonemap(frag_in);
}
}

View File

@ -4,11 +4,14 @@
* there.
*/
#include "color.effect"
uniform float4x4 ViewProj;
uniform texture2d image;
uniform float2 base_dimension;
uniform float2 base_dimension_i;
uniform float undistort_factor = 1.0;
uniform float multiplier;
sampler_state textureSampler {
Filter = Linear;
@ -134,6 +137,32 @@ float4 PSDrawBicubicRGBA(FragData f_in, bool undistort) : TARGET
return DrawBicubic(f_in, undistort);
}
float4 PSDrawBicubicRGBAMultiply(FragData f_in, bool undistort) : TARGET
{
float4 rgba = DrawBicubic(f_in, undistort);
rgba.rgb *= multiplier;
return rgba;
}
float4 PSDrawBicubicRGBATonemap(FragData f_in, bool undistort) : TARGET
{
float4 rgba = DrawBicubic(f_in, undistort);
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawBicubicRGBAMultiplyTonemap(FragData f_in, bool undistort) : TARGET
{
float4 rgba = DrawBicubic(f_in, undistort);
rgba.rgb *= multiplier;
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawBicubicRGBADivide(FragData f_in) : TARGET
{
float4 rgba = DrawBicubic(f_in, false);
@ -151,6 +180,33 @@ technique Draw
}
}
technique DrawMultiply
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawBicubicRGBAMultiply(f_in, false);
}
}
technique DrawTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawBicubicRGBATonemap(f_in, false);
}
}
technique DrawMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawBicubicRGBAMultiplyTonemap(f_in, false);
}
}
technique DrawAlphaDivide
{
pass
@ -168,3 +224,30 @@ technique DrawUndistort
pixel_shader = PSDrawBicubicRGBA(f_in, true);
}
}
technique DrawUndistortMultiply
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawBicubicRGBAMultiply(f_in, true);
}
}
technique DrawUndistortTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawBicubicRGBATonemap(f_in, true);
}
}
technique DrawUndistortMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawBicubicRGBAMultiplyTonemap(f_in, true);
}
}

View File

@ -3,8 +3,11 @@
* low resolution image below half size
*/
#include "color.effect"
uniform float4x4 ViewProj;
uniform texture2d image;
uniform float multiplier;
sampler_state textureSampler {
Filter = Linear;
@ -30,9 +33,9 @@ float4 pixel(float2 uv)
return image.Sample(textureSampler, uv);
}
float4 DrawLowresBilinear(VertData v_in)
float4 DrawLowresBilinear(VertData f_in)
{
float2 uv = v_in.uv;
float2 uv = f_in.uv;
float2 stepxy = float2(ddx(uv.x), ddy(uv.y));
float2 stepxy1 = stepxy * 0.0625;
float2 stepxy3 = stepxy * 0.1875;
@ -52,14 +55,40 @@ float4 DrawLowresBilinear(VertData v_in)
return out_color * 0.125;
}
float4 PSDrawLowresBilinearRGBA(VertData v_in) : TARGET
float4 PSDrawLowresBilinearRGBA(VertData f_in) : TARGET
{
return DrawLowresBilinear(v_in);
return DrawLowresBilinear(f_in);
}
float4 PSDrawLowresBilinearRGBADivide(VertData v_in) : TARGET
float4 PSDrawLowresBilinearRGBAMultiply(VertData f_in) : TARGET
{
float4 rgba = DrawLowresBilinear(v_in);
float4 rgba = DrawLowresBilinear(f_in);
rgba.rgb *= multiplier;
return rgba;
}
float4 PSDrawLowresBilinearRGBATonemap(VertData f_in) : TARGET
{
float4 rgba = DrawLowresBilinear(f_in);
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawLowresBilinearRGBAMultiplyTonemap(VertData f_in) : TARGET
{
float4 rgba = DrawLowresBilinear(f_in);
rgba.rgb *= multiplier;
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawLowresBilinearRGBADivide(VertData f_in) : TARGET
{
float4 rgba = DrawLowresBilinear(f_in);
float alpha = rgba.a;
float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
return float4(rgba.rgb * multiplier, alpha);
@ -70,7 +99,34 @@ technique Draw
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLowresBilinearRGBA(v_in);
pixel_shader = PSDrawLowresBilinearRGBA(f_in);
}
}
technique DrawMultiply
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLowresBilinearRGBAMultiply(f_in);
}
}
technique DrawTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLowresBilinearRGBATonemap(f_in);
}
}
technique DrawMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLowresBilinearRGBAMultiplyTonemap(f_in);
}
}
@ -79,7 +135,6 @@ technique DrawAlphaDivide
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLowresBilinearRGBADivide(v_in);
pixel_shader = PSDrawLowresBilinearRGBADivide(f_in);
}
}

45
libobs/data/color.effect Normal file
View File

@ -0,0 +1,45 @@
float srgb_linear_to_nonlinear_channel(float u)
{
return (u <= 0.0031308) ? (12.92 * u) : ((1.055 * pow(u, 1.0 / 2.4)) - 0.055);
}
float3 srgb_linear_to_nonlinear(float3 v)
{
return float3(srgb_linear_to_nonlinear_channel(v.r), srgb_linear_to_nonlinear_channel(v.g), srgb_linear_to_nonlinear_channel(v.b));
}
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));
}
float3 rec709_to_rec2020(float3 v)
{
float r = dot(v, float3(0.6274040f, 0.3292820f, 0.0433136f));
float g = dot(v, float3(0.0690970f, 0.9195400f, 0.0113612f));
float b = dot(v, float3(0.0163916f, 0.0880132f, 0.8955950f));
return float3(r, g, b);
}
float3 rec2020_to_rec709(float3 v)
{
float r = dot(v, float3(1.6604910, -0.5876411, -0.0728499));
float g = dot(v, float3(-0.1245505, 1.1328999, -0.0083494));
float b = dot(v, float3(-0.0181508, -0.1005789, 1.1187297));
return float3(r, g, b);
}
float reinhard_channel(float x)
{
return x / (x + 1.0);
}
float3 reinhard(float3 rgb)
{
return float3(reinhard_channel(rgb.r), reinhard_channel(rgb.g), reinhard_channel(rgb.b));
}

View File

@ -1,3 +1,5 @@
#include "color.effect"
uniform float4x4 ViewProj;
uniform texture2d image;
uniform float multiplier;
@ -34,26 +36,6 @@ float4 PSDrawAlphaDivide(VertInOut vert_in) : TARGET
return float4(rgba.rgb * multiplier, alpha);
}
float srgb_linear_to_nonlinear_channel(float u)
{
return (u <= 0.0031308) ? (12.92 * u) : ((1.055 * pow(u, 1.0 / 2.4)) - 0.055);
}
float3 srgb_linear_to_nonlinear(float3 v)
{
return float3(srgb_linear_to_nonlinear_channel(v.r), srgb_linear_to_nonlinear_channel(v.g), srgb_linear_to_nonlinear_channel(v.b));
}
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 PSDrawNonlinearAlpha(VertInOut vert_in) : TARGET
{
float4 rgba = image.Sample(def_sampler, vert_in.uv);
@ -77,32 +59,6 @@ float4 PSDrawMultiply(VertInOut vert_in) : TARGET
return rgba;
}
float3 rec709_to_rec2020(float3 v)
{
float r = dot(v, float3(0.6274040f, 0.3292820f, 0.0433136f));
float g = dot(v, float3(0.0690970f, 0.9195400f, 0.0113612f));
float b = dot(v, float3(0.0163916f, 0.0880132f, 0.8955950f));
return float3(r, g, b);
}
float3 rec2020_to_rec709(float3 v)
{
float r = dot(v, float3(1.6604910, -0.5876411, -0.0728499));
float g = dot(v, float3(-0.1245505, 1.1328999, -0.0083494));
float b = dot(v, float3(-0.0181508, -0.1005789, 1.1187297));
return float3(r, g, b);
}
float reinhard_channel(float x)
{
return x / (x + 1.0);
}
float3 reinhard(float3 rgb)
{
return float3(reinhard_channel(rgb.r), reinhard_channel(rgb.g), reinhard_channel(rgb.b));
}
float4 PSDrawTonemap(VertInOut vert_in) : TARGET
{
float4 rgba = image.Sample(def_sampler, vert_in.uv);
@ -112,6 +68,16 @@ float4 PSDrawTonemap(VertInOut vert_in) : TARGET
return rgba;
}
float4 PSDrawMultiplyTonemap(VertInOut vert_in) : TARGET
{
float4 rgba = image.Sample(def_sampler, vert_in.uv);
rgba.rgb *= multiplier;
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
technique Draw
{
pass
@ -165,3 +131,12 @@ technique DrawTonemap
pixel_shader = PSDrawTonemap(vert_in);
}
}
technique DrawMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawMultiplyTonemap(vert_in);
}
}

View File

@ -1,3 +1,5 @@
#include "color.effect"
uniform float4x4 ViewProj;
uniform texture_rect image;
@ -30,16 +32,6 @@ 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 PSDrawSrgbDecompress(VertInOut vert_in) : TARGET
{
float4 rgba = image.Sample(def_sampler, vert_in.uv);

View File

@ -4,11 +4,14 @@
* there.
*/
#include "color.effect"
uniform float4x4 ViewProj;
uniform texture2d image;
uniform float2 base_dimension;
uniform float2 base_dimension_i;
uniform float undistort_factor = 1.0;
uniform float multiplier;
sampler_state textureSampler
{
@ -190,6 +193,32 @@ float4 PSDrawLanczosRGBA(FragData f_in, bool undistort) : TARGET
return DrawLanczos(f_in, undistort);
}
float4 PSDrawLanczosRGBAMultiply(FragData f_in, bool undistort) : TARGET
{
float4 rgba = DrawLanczos(f_in, undistort);
rgba.rgb *= multiplier;
return rgba;
}
float4 PSDrawLanczosRGBATonemap(FragData f_in, bool undistort) : TARGET
{
float4 rgba = DrawLanczos(f_in, undistort);
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawLanczosRGBAMultiplyTonemap(FragData f_in, bool undistort) : TARGET
{
float4 rgba = DrawLanczos(f_in, undistort);
rgba.rgb *= multiplier;
rgba.rgb = rec709_to_rec2020(rgba.rgb);
rgba.rgb = reinhard(rgba.rgb);
rgba.rgb = rec2020_to_rec709(rgba.rgb);
return rgba;
}
float4 PSDrawLanczosRGBADivide(FragData f_in) : TARGET
{
float4 rgba = DrawLanczos(f_in, false);
@ -207,6 +236,33 @@ technique Draw
}
}
technique DrawMultiply
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLanczosRGBAMultiply(f_in, false);
}
}
technique DrawTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLanczosRGBATonemap(f_in, false);
}
}
technique DrawMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLanczosRGBAMultiplyTonemap(f_in, false);
}
}
technique DrawAlphaDivide
{
pass
@ -224,3 +280,30 @@ technique DrawUndistort
pixel_shader = PSDrawLanczosRGBA(f_in, true);
}
}
technique DrawUndistortMultiply
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLanczosRGBAMultiply(f_in, true);
}
}
technique DrawUndistortTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLanczosRGBATonemap(f_in, true);
}
}
technique DrawUndistortMultiplyTonemap
{
pass
{
vertex_shader = VSDefault(v_in);
pixel_shader = PSDrawLanczosRGBAMultiplyTonemap(f_in, true);
}
}