2014-12-14 23:45:44 -08:00
|
|
|
/*
|
|
|
|
* bicubic sharper (better for downscaling)
|
|
|
|
* note - this shader is adapted from the GPL bsnes shader, very good stuff
|
|
|
|
* there.
|
|
|
|
*/
|
|
|
|
|
2022-03-22 22:46:35 -07:00
|
|
|
#include "color.effect"
|
|
|
|
|
2014-12-14 23:45:44 -08:00
|
|
|
uniform float4x4 ViewProj;
|
|
|
|
uniform texture2d image;
|
2019-07-25 22:21:11 -07:00
|
|
|
uniform float2 base_dimension;
|
2014-12-14 23:45:44 -08:00
|
|
|
uniform float2 base_dimension_i;
|
2017-01-30 05:48:24 -08:00
|
|
|
uniform float undistort_factor = 1.0;
|
2022-03-22 22:46:35 -07:00
|
|
|
uniform float multiplier;
|
2014-12-14 23:45:44 -08:00
|
|
|
|
|
|
|
sampler_state textureSampler {
|
|
|
|
Filter = Linear;
|
|
|
|
AddressU = Clamp;
|
|
|
|
AddressV = Clamp;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VertData {
|
|
|
|
float4 pos : POSITION;
|
|
|
|
float2 uv : TEXCOORD0;
|
|
|
|
};
|
|
|
|
|
2019-07-25 22:21:11 -07:00
|
|
|
struct VertOut {
|
|
|
|
float2 uv : TEXCOORD0;
|
|
|
|
float4 pos : POSITION;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FragData {
|
|
|
|
float2 uv : TEXCOORD0;
|
|
|
|
};
|
|
|
|
|
|
|
|
VertOut VSDefault(VertData v_in)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-07-25 22:21:11 -07:00
|
|
|
VertOut vert_out;
|
2019-08-25 10:00:10 -07:00
|
|
|
vert_out.uv = v_in.uv * base_dimension;
|
2015-01-07 16:41:37 -08:00
|
|
|
vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
|
2014-12-14 23:45:44 -08:00
|
|
|
return vert_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
float4 weight4(float x)
|
|
|
|
{
|
2019-08-25 10:00:10 -07:00
|
|
|
/* Sharper version. May look better in some cases. B=0, C=0.75 */
|
2014-12-14 23:45:44 -08:00
|
|
|
return float4(
|
2019-08-25 10:00:10 -07:00
|
|
|
((-0.75 * x + 1.5) * x - 0.75) * x,
|
|
|
|
(1.25 * x - 2.25) * x * x + 1.0,
|
|
|
|
((-1.25 * x + 1.5) * x + 0.75) * x,
|
|
|
|
(0.75 * x - 0.75) * x * x);
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2017-01-30 05:48:24 -08:00
|
|
|
float AspectUndistortX(float x, float a)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2017-01-30 05:48:24 -08:00
|
|
|
// The higher the power, the longer the linear part will be.
|
|
|
|
return (1.0 - a) * (x * x * x * x * x) + a * x;
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2017-01-30 05:48:24 -08:00
|
|
|
float AspectUndistortU(float u)
|
|
|
|
{
|
|
|
|
// Normalize texture coord to -1.0 to 1.0 range, and back.
|
|
|
|
return AspectUndistortX((u - 0.5) * 2.0, undistort_factor) * 0.5 + 0.5;
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:21:11 -07:00
|
|
|
float2 undistort_coord(float xpos, float ypos)
|
2017-01-30 05:48:24 -08:00
|
|
|
{
|
|
|
|
return float2(AspectUndistortU(xpos), ypos);
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:21:11 -07:00
|
|
|
float4 undistort_pixel(float xpos, float ypos)
|
2017-01-30 05:48:24 -08:00
|
|
|
{
|
2019-07-25 22:21:11 -07:00
|
|
|
return image.Sample(textureSampler, undistort_coord(xpos, ypos));
|
2017-01-30 05:48:24 -08:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:21:11 -07:00
|
|
|
float4 undistort_line(float4 xpos, float ypos, float4 rowtaps)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-07-25 22:21:11 -07:00
|
|
|
return undistort_pixel(xpos.x, ypos) * rowtaps.x +
|
|
|
|
undistort_pixel(xpos.y, ypos) * rowtaps.y +
|
|
|
|
undistort_pixel(xpos.z, ypos) * rowtaps.z +
|
|
|
|
undistort_pixel(xpos.w, ypos) * rowtaps.w;
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:21:11 -07:00
|
|
|
float4 DrawBicubic(FragData f_in, bool undistort)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-08-25 10:00:10 -07:00
|
|
|
float2 pos = f_in.uv;
|
|
|
|
float2 pos1 = floor(pos - 0.5) + 0.5;
|
|
|
|
float2 f = pos - pos1;
|
2014-12-14 23:45:44 -08:00
|
|
|
|
2019-08-25 10:00:10 -07:00
|
|
|
float4 rowtaps = weight4(f.x);
|
|
|
|
float4 coltaps = weight4(f.y);
|
2014-12-14 23:45:44 -08:00
|
|
|
|
2019-08-25 10:00:10 -07:00
|
|
|
float2 uv1 = pos1 * base_dimension_i;
|
|
|
|
float2 uv0 = uv1 - base_dimension_i;
|
|
|
|
float2 uv2 = uv1 + base_dimension_i;
|
|
|
|
float2 uv3 = uv2 + base_dimension_i;
|
2019-07-25 22:21:11 -07:00
|
|
|
|
|
|
|
if (undistort) {
|
|
|
|
float4 xpos = float4(uv0.x, uv1.x, uv2.x, uv3.x);
|
|
|
|
return undistort_line(xpos, uv0.y, rowtaps) * coltaps.x +
|
|
|
|
undistort_line(xpos, uv1.y, rowtaps) * coltaps.y +
|
|
|
|
undistort_line(xpos, uv2.y, rowtaps) * coltaps.z +
|
|
|
|
undistort_line(xpos, uv3.y, rowtaps) * coltaps.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
float u_weight_sum = rowtaps.y + rowtaps.z;
|
2019-08-25 10:00:10 -07:00
|
|
|
float u_middle_offset = rowtaps.z * base_dimension_i.x / u_weight_sum;
|
2019-07-25 22:21:11 -07:00
|
|
|
float u_middle = uv1.x + u_middle_offset;
|
|
|
|
|
|
|
|
float v_weight_sum = coltaps.y + coltaps.z;
|
2019-08-25 10:00:10 -07:00
|
|
|
float v_middle_offset = coltaps.z * base_dimension_i.y / v_weight_sum;
|
2019-07-25 22:21:11 -07:00
|
|
|
float v_middle = uv1.y + v_middle_offset;
|
|
|
|
|
|
|
|
int2 coord_top_left = int2(max(uv0 * base_dimension, 0.5));
|
|
|
|
int2 coord_bottom_right = int2(min(uv3 * base_dimension, base_dimension - 0.5));
|
|
|
|
|
|
|
|
float4 top = image.Load(int3(coord_top_left, 0)) * rowtaps.x;
|
|
|
|
top += image.Sample(textureSampler, float2(u_middle, uv0.y)) * u_weight_sum;
|
|
|
|
top += image.Load(int3(coord_bottom_right.x, coord_top_left.y, 0)) * rowtaps.w;
|
|
|
|
float4 total = top * coltaps.x;
|
|
|
|
|
|
|
|
float4 middle = image.Sample(textureSampler, float2(uv0.x, v_middle)) * rowtaps.x;
|
|
|
|
middle += image.Sample(textureSampler, float2(u_middle, v_middle)) * u_weight_sum;
|
|
|
|
middle += image.Sample(textureSampler, float2(uv3.x, v_middle)) * rowtaps.w;
|
|
|
|
total += middle * v_weight_sum;
|
|
|
|
|
|
|
|
float4 bottom = image.Load(int3(coord_top_left.x, coord_bottom_right.y, 0)) * rowtaps.x;
|
|
|
|
bottom += image.Sample(textureSampler, float2(u_middle, uv3.y)) * u_weight_sum;
|
|
|
|
bottom += image.Load(int3(coord_bottom_right, 0)) * rowtaps.w;
|
|
|
|
total += bottom * coltaps.w;
|
|
|
|
|
|
|
|
return total;
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:21:11 -07:00
|
|
|
float4 PSDrawBicubicRGBA(FragData f_in, bool undistort) : TARGET
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-07-25 22:21:11 -07:00
|
|
|
return DrawBicubic(f_in, undistort);
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2022-03-22 22:46:35 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:21:11 -07:00
|
|
|
float4 PSDrawBicubicRGBADivide(FragData f_in) : TARGET
|
libobs: Fix various alpha issues
There are cases where alpha is multiplied unnecessarily. This change
attempts to use premultiplied alpha blending for composition.
To keep this change simple, The filter chain will continue to use
straight alpha. Otherwise, every source would need to modified to output
premultiplied, and every filter modified for premultiplied input.
"DrawAlphaDivide" shader techniques have been added to convert from
premultiplied alpha to straight alpha for final output. "DrawMatrix"
techniques ignore alpha, so they do not appear to need changing.
One remaining issue is that scale effects are set up here to use the
same shader logic for both scale filters (straight alpha - incorrectly),
and output composition (premultiplied alpha - correctly). A fix could be
made to add additional shaders for straight alpha, but the "real" fix
may be to eliminate the straight alpha path at some point.
For graphics, SrcBlendAlpha and DestBlendAlpha were both ONE, and could
combine together to form alpha values greater than one. This is not as
noticeable of a problem for UNORM targets because the channels are
clamped, but it will likely become a problem in more situations if FLOAT
targets are used.
This change switches DestBlendAlpha to INVSRCALPHA. The blending
behavior of stacked transparents is preserved without overflowing the
alpha channel.
obs-transitions: Use premultiplied alpha blend, and simplify shaders
because both inputs and outputs use premultiplied alpha now.
Fixes https://obsproject.com/mantis/view.php?id=1108
2019-05-03 03:54:17 -07:00
|
|
|
{
|
2019-07-25 22:21:11 -07:00
|
|
|
float4 rgba = DrawBicubic(f_in, false);
|
2022-06-04 17:47:47 -07:00
|
|
|
rgba.rgb *= max(1. / rgba.a, 0.);
|
|
|
|
return rgba;
|
libobs: Fix various alpha issues
There are cases where alpha is multiplied unnecessarily. This change
attempts to use premultiplied alpha blending for composition.
To keep this change simple, The filter chain will continue to use
straight alpha. Otherwise, every source would need to modified to output
premultiplied, and every filter modified for premultiplied input.
"DrawAlphaDivide" shader techniques have been added to convert from
premultiplied alpha to straight alpha for final output. "DrawMatrix"
techniques ignore alpha, so they do not appear to need changing.
One remaining issue is that scale effects are set up here to use the
same shader logic for both scale filters (straight alpha - incorrectly),
and output composition (premultiplied alpha - correctly). A fix could be
made to add additional shaders for straight alpha, but the "real" fix
may be to eliminate the straight alpha path at some point.
For graphics, SrcBlendAlpha and DestBlendAlpha were both ONE, and could
combine together to form alpha values greater than one. This is not as
noticeable of a problem for UNORM targets because the channels are
clamped, but it will likely become a problem in more situations if FLOAT
targets are used.
This change switches DestBlendAlpha to INVSRCALPHA. The blending
behavior of stacked transparents is preserved without overflowing the
alpha channel.
obs-transitions: Use premultiplied alpha blend, and simplify shaders
because both inputs and outputs use premultiplied alpha now.
Fixes https://obsproject.com/mantis/view.php?id=1108
2019-05-03 03:54:17 -07:00
|
|
|
}
|
|
|
|
|
2014-12-14 23:45:44 -08:00
|
|
|
technique Draw
|
|
|
|
{
|
|
|
|
pass
|
|
|
|
{
|
2015-01-07 16:41:37 -08:00
|
|
|
vertex_shader = VSDefault(v_in);
|
2019-07-25 22:21:11 -07:00
|
|
|
pixel_shader = PSDrawBicubicRGBA(f_in, false);
|
2017-01-30 05:48:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 22:46:35 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libobs: Fix various alpha issues
There are cases where alpha is multiplied unnecessarily. This change
attempts to use premultiplied alpha blending for composition.
To keep this change simple, The filter chain will continue to use
straight alpha. Otherwise, every source would need to modified to output
premultiplied, and every filter modified for premultiplied input.
"DrawAlphaDivide" shader techniques have been added to convert from
premultiplied alpha to straight alpha for final output. "DrawMatrix"
techniques ignore alpha, so they do not appear to need changing.
One remaining issue is that scale effects are set up here to use the
same shader logic for both scale filters (straight alpha - incorrectly),
and output composition (premultiplied alpha - correctly). A fix could be
made to add additional shaders for straight alpha, but the "real" fix
may be to eliminate the straight alpha path at some point.
For graphics, SrcBlendAlpha and DestBlendAlpha were both ONE, and could
combine together to form alpha values greater than one. This is not as
noticeable of a problem for UNORM targets because the channels are
clamped, but it will likely become a problem in more situations if FLOAT
targets are used.
This change switches DestBlendAlpha to INVSRCALPHA. The blending
behavior of stacked transparents is preserved without overflowing the
alpha channel.
obs-transitions: Use premultiplied alpha blend, and simplify shaders
because both inputs and outputs use premultiplied alpha now.
Fixes https://obsproject.com/mantis/view.php?id=1108
2019-05-03 03:54:17 -07:00
|
|
|
technique DrawAlphaDivide
|
|
|
|
{
|
|
|
|
pass
|
|
|
|
{
|
|
|
|
vertex_shader = VSDefault(v_in);
|
2019-07-25 22:21:11 -07:00
|
|
|
pixel_shader = PSDrawBicubicRGBADivide(f_in);
|
libobs: Fix various alpha issues
There are cases where alpha is multiplied unnecessarily. This change
attempts to use premultiplied alpha blending for composition.
To keep this change simple, The filter chain will continue to use
straight alpha. Otherwise, every source would need to modified to output
premultiplied, and every filter modified for premultiplied input.
"DrawAlphaDivide" shader techniques have been added to convert from
premultiplied alpha to straight alpha for final output. "DrawMatrix"
techniques ignore alpha, so they do not appear to need changing.
One remaining issue is that scale effects are set up here to use the
same shader logic for both scale filters (straight alpha - incorrectly),
and output composition (premultiplied alpha - correctly). A fix could be
made to add additional shaders for straight alpha, but the "real" fix
may be to eliminate the straight alpha path at some point.
For graphics, SrcBlendAlpha and DestBlendAlpha were both ONE, and could
combine together to form alpha values greater than one. This is not as
noticeable of a problem for UNORM targets because the channels are
clamped, but it will likely become a problem in more situations if FLOAT
targets are used.
This change switches DestBlendAlpha to INVSRCALPHA. The blending
behavior of stacked transparents is preserved without overflowing the
alpha channel.
obs-transitions: Use premultiplied alpha blend, and simplify shaders
because both inputs and outputs use premultiplied alpha now.
Fixes https://obsproject.com/mantis/view.php?id=1108
2019-05-03 03:54:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 05:48:24 -08:00
|
|
|
technique DrawUndistort
|
|
|
|
{
|
|
|
|
pass
|
|
|
|
{
|
|
|
|
vertex_shader = VSDefault(v_in);
|
2019-07-25 22:21:11 -07:00
|
|
|
pixel_shader = PSDrawBicubicRGBA(f_in, true);
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 22:46:35 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|