2014-12-14 23:45:44 -08:00
|
|
|
/*
|
|
|
|
* lanczos sharper
|
|
|
|
* note - this shader is adapted from the GPL bsnes shader, very good stuff
|
|
|
|
* there.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uniform float4x4 ViewProj;
|
|
|
|
uniform texture2d image;
|
2019-07-26 20:45:33 -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;
|
2014-12-14 23:45:44 -08:00
|
|
|
|
|
|
|
sampler_state textureSampler
|
|
|
|
{
|
|
|
|
AddressU = Clamp;
|
|
|
|
AddressV = Clamp;
|
|
|
|
Filter = Linear;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VertData {
|
|
|
|
float4 pos : POSITION;
|
|
|
|
float2 uv : TEXCOORD0;
|
|
|
|
};
|
|
|
|
|
2019-07-26 20:45:33 -07:00
|
|
|
struct VertOut {
|
|
|
|
float2 uv : TEXCOORD0;
|
2016-03-23 17:58:22 -07:00
|
|
|
float4 pos : POSITION;
|
|
|
|
};
|
|
|
|
|
2019-07-26 20:45:33 -07:00
|
|
|
struct FragData {
|
|
|
|
float2 uv : TEXCOORD0;
|
|
|
|
};
|
|
|
|
|
|
|
|
VertOut VSDefault(VertData v_in)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-07-26 20:45:33 -07:00
|
|
|
VertOut vert_out;
|
2019-08-25 10:00:23 -07:00
|
|
|
vert_out.uv = v_in.uv * base_dimension;
|
2019-07-26 20:45:33 -07:00
|
|
|
vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
|
2016-03-23 17:58:22 -07:00
|
|
|
|
2014-12-14 23:45:44 -08:00
|
|
|
return vert_out;
|
|
|
|
}
|
|
|
|
|
2019-07-26 20:45:33 -07:00
|
|
|
float weight(float x)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-08-25 10:00:23 -07:00
|
|
|
float x_pi = x * 3.141592654;
|
|
|
|
return 3.0 * sin(x_pi) * sin(x_pi * (1.0 / 3.0)) / (x_pi * x_pi);
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
void weight6(float f_neg, out float3 tap012, out float3 tap345)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-08-25 10:00:23 -07:00
|
|
|
tap012 = float3(
|
|
|
|
weight(f_neg - 2.0),
|
|
|
|
weight(f_neg - 1.0),
|
|
|
|
min(1.0, weight(f_neg))); // Replace NaN with 1.0.
|
|
|
|
tap345 = float3(
|
|
|
|
weight(f_neg + 1.0),
|
|
|
|
weight(f_neg + 2.0),
|
|
|
|
weight(f_neg + 3.0));
|
|
|
|
|
|
|
|
// Normalize weights
|
|
|
|
float sum = tap012.x + tap012.y + tap012.z + tap345.x + tap345.y + tap345.z;
|
|
|
|
float sum_i = 1.0 / sum;
|
|
|
|
tap012 = tap012 * sum_i;
|
|
|
|
tap345 = tap345 * sum_i;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-26 20:45:33 -07:00
|
|
|
float2 undistort_coord(float xpos, float ypos)
|
2017-01-30 05:48:24 -08:00
|
|
|
{
|
|
|
|
return float2(AspectUndistortU(xpos), ypos);
|
|
|
|
}
|
|
|
|
|
2019-07-26 20:45:33 -07:00
|
|
|
float4 undistort_pixel(float xpos, float ypos)
|
2017-01-30 05:48:24 -08:00
|
|
|
{
|
2019-07-26 20:45:33 -07:00
|
|
|
return image.Sample(textureSampler, undistort_coord(xpos, ypos));
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float4 undistort_line(float3 xpos012, float3 xpos345, float ypos, float3 rowtap012,
|
|
|
|
float3 rowtap345)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
|
|
|
return
|
2019-08-25 10:00:23 -07:00
|
|
|
undistort_pixel(xpos012.x, ypos) * rowtap012.x +
|
|
|
|
undistort_pixel(xpos012.y, ypos) * rowtap012.y +
|
|
|
|
undistort_pixel(xpos012.z, ypos) * rowtap012.z +
|
|
|
|
undistort_pixel(xpos345.x, ypos) * rowtap345.x +
|
|
|
|
undistort_pixel(xpos345.y, ypos) * rowtap345.y +
|
|
|
|
undistort_pixel(xpos345.z, ypos) * rowtap345.z;
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2019-07-26 20:45:33 -07:00
|
|
|
float4 DrawLanczos(FragData f_in, bool undistort)
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-08-25 10:00:23 -07:00
|
|
|
float2 pos = f_in.uv;
|
|
|
|
float2 pos2 = floor(pos - 0.5) + 0.5;
|
|
|
|
float2 f_neg = pos2 - pos;
|
|
|
|
|
|
|
|
float3 rowtap012, rowtap345;
|
|
|
|
weight6(f_neg.x, rowtap012, rowtap345);
|
|
|
|
|
|
|
|
float3 coltap012, coltap345;
|
|
|
|
weight6(f_neg.y, coltap012, coltap345);
|
|
|
|
|
|
|
|
float2 uv2 = pos2 * base_dimension_i;
|
|
|
|
float2 uv1 = uv2 - base_dimension_i;
|
|
|
|
float2 uv0 = uv1 - base_dimension_i;
|
|
|
|
float2 uv3 = uv2 + base_dimension_i;
|
|
|
|
float2 uv4 = uv3 + base_dimension_i;
|
|
|
|
float2 uv5 = uv4 + base_dimension_i;
|
2019-07-26 20:45:33 -07:00
|
|
|
|
|
|
|
if (undistort) {
|
|
|
|
float3 xpos012 = float3(uv0.x, uv1.x, uv2.x);
|
|
|
|
float3 xpos345 = float3(uv3.x, uv4.x, uv5.x);
|
2019-08-25 10:00:23 -07:00
|
|
|
return undistort_line(xpos012, xpos345, uv0.y, rowtap012, rowtap345) * coltap012.x +
|
|
|
|
undistort_line(xpos012, xpos345, uv1.y, rowtap012, rowtap345) * coltap012.y +
|
|
|
|
undistort_line(xpos012, xpos345, uv2.y, rowtap012, rowtap345) * coltap012.z +
|
|
|
|
undistort_line(xpos012, xpos345, uv3.y, rowtap012, rowtap345) * coltap345.x +
|
|
|
|
undistort_line(xpos012, xpos345, uv4.y, rowtap012, rowtap345) * coltap345.y +
|
|
|
|
undistort_line(xpos012, xpos345, uv5.y, rowtap012, rowtap345) * coltap345.z;
|
2019-07-26 20:45:33 -07:00
|
|
|
}
|
2014-12-14 23:45:44 -08:00
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float u_weight_sum = rowtap012.z + rowtap345.x;
|
|
|
|
float u_middle_offset = rowtap345.x * base_dimension_i.x / u_weight_sum;
|
2019-07-26 20:45:33 -07:00
|
|
|
float u_middle = uv2.x + u_middle_offset;
|
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float v_weight_sum = coltap012.z + coltap345.x;
|
|
|
|
float v_middle_offset = coltap345.x * base_dimension_i.y / v_weight_sum;
|
2019-07-26 20:45:33 -07:00
|
|
|
float v_middle = uv2.y + v_middle_offset;
|
|
|
|
|
|
|
|
float2 coord_limit = base_dimension - 0.5;
|
|
|
|
float2 coord0_f = max(uv0 * base_dimension, 0.5);
|
2019-08-25 10:00:23 -07:00
|
|
|
float2 coord1_f = max(uv1 * base_dimension, 0.5);
|
|
|
|
float2 coord4_f = min(uv4 * base_dimension, coord_limit);
|
|
|
|
float2 coord5_f = min(uv5 * base_dimension, coord_limit);
|
2019-07-26 20:45:33 -07:00
|
|
|
|
|
|
|
int2 coord0 = int2(coord0_f);
|
|
|
|
int2 coord1 = int2(coord1_f);
|
|
|
|
int2 coord4 = int2(coord4_f);
|
|
|
|
int2 coord5 = int2(coord5_f);
|
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float4 row0 = image.Load(int3(coord0, 0)) * rowtap012.x;
|
|
|
|
row0 += image.Load(int3(coord1.x, coord0.y, 0)) * rowtap012.y;
|
2019-07-26 20:45:33 -07:00
|
|
|
row0 += image.Sample(textureSampler, float2(u_middle, uv0.y)) * u_weight_sum;
|
2019-08-25 10:00:23 -07:00
|
|
|
row0 += image.Load(int3(coord4.x, coord0.y, 0)) * rowtap345.y;
|
|
|
|
row0 += image.Load(int3(coord5.x, coord0.y, 0)) * rowtap345.z;
|
|
|
|
float4 total = row0 * coltap012.x;
|
2019-07-26 20:45:33 -07:00
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float4 row1 = image.Load(int3(coord0.x, coord1.y, 0)) * rowtap012.x;
|
|
|
|
row1 += image.Load(int3(coord1.x, coord1.y, 0)) * rowtap012.y;
|
2019-07-26 20:45:33 -07:00
|
|
|
row1 += image.Sample(textureSampler, float2(u_middle, uv1.y)) * u_weight_sum;
|
2019-08-25 10:00:23 -07:00
|
|
|
row1 += image.Load(int3(coord4.x, coord1.y, 0)) * rowtap345.y;
|
|
|
|
row1 += image.Load(int3(coord5.x, coord1.y, 0)) * rowtap345.z;
|
|
|
|
total += row1 * coltap012.y;
|
2019-07-26 20:45:33 -07:00
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float4 row23 = image.Sample(textureSampler, float2(uv0.x, v_middle)) * rowtap012.x;
|
|
|
|
row23 += image.Sample(textureSampler, float2(uv1.x, v_middle)) * rowtap012.y;
|
2019-07-26 20:45:33 -07:00
|
|
|
row23 += image.Sample(textureSampler, float2(u_middle, v_middle)) * u_weight_sum;
|
2019-08-25 10:00:23 -07:00
|
|
|
row23 += image.Sample(textureSampler, float2(uv4.x, v_middle)) * rowtap345.y;
|
|
|
|
row23 += image.Sample(textureSampler, float2(uv5.x, v_middle)) * rowtap345.z;
|
2019-07-26 20:45:33 -07:00
|
|
|
total += row23 * v_weight_sum;
|
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float4 row4 = image.Load(int3(coord0.x, coord4.y, 0)) * rowtap012.x;
|
|
|
|
row4 += image.Load(int3(coord1.x, coord4.y, 0)) * rowtap012.y;
|
2019-07-26 20:45:33 -07:00
|
|
|
row4 += image.Sample(textureSampler, float2(u_middle, uv4.y)) * u_weight_sum;
|
2019-08-25 10:00:23 -07:00
|
|
|
row4 += image.Load(int3(coord4.x, coord4.y, 0)) * rowtap345.y;
|
|
|
|
row4 += image.Load(int3(coord5.x, coord4.y, 0)) * rowtap345.z;
|
|
|
|
total += row4 * coltap345.y;
|
2019-07-26 20:45:33 -07:00
|
|
|
|
2019-08-25 10:00:23 -07:00
|
|
|
float4 row5 = image.Load(int3(coord0.x, coord5.y, 0)) * rowtap012.x;
|
|
|
|
row5 += image.Load(int3(coord1.x, coord5.y, 0)) * rowtap012.y;
|
2019-07-26 20:45:33 -07:00
|
|
|
row5 += image.Sample(textureSampler, float2(u_middle, uv5.y)) * u_weight_sum;
|
2019-08-25 10:00:23 -07:00
|
|
|
row5 += image.Load(int3(coord4.x, coord5.y, 0)) * rowtap345.y;
|
|
|
|
row5 += image.Load(int3(coord5, 0)) * rowtap345.z;
|
|
|
|
total += row5 * coltap345.z;
|
2019-07-26 20:45:33 -07:00
|
|
|
|
|
|
|
return total;
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2019-07-26 20:45:33 -07:00
|
|
|
float4 PSDrawLanczosRGBA(FragData f_in, bool undistort) : TARGET
|
2014-12-14 23:45:44 -08:00
|
|
|
{
|
2019-07-26 20:45:33 -07:00
|
|
|
return DrawLanczos(f_in, undistort);
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
|
2019-07-26 20:45:33 -07:00
|
|
|
float4 PSDrawLanczosRGBADivide(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-26 20:45:33 -07:00
|
|
|
float4 rgba = DrawLanczos(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
|
|
|
float alpha = rgba.a;
|
|
|
|
float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
|
|
|
|
return float4(rgba.rgb * multiplier, alpha);
|
|
|
|
}
|
|
|
|
|
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-26 20:45:33 -07:00
|
|
|
pixel_shader = PSDrawLanczosRGBA(f_in, false);
|
2017-01-30 05:48:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-26 20:45:33 -07:00
|
|
|
pixel_shader = PSDrawLanczosRGBADivide(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-26 20:45:33 -07:00
|
|
|
pixel_shader = PSDrawLanczosRGBA(f_in, true);
|
2014-12-14 23:45:44 -08:00
|
|
|
}
|
|
|
|
}
|