0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 04:42:18 +02:00
obs-studio/libobs/data/area.effect

65 lines
1.7 KiB
Plaintext
Raw Normal View History

uniform float4x4 ViewProj;
uniform float2 base_dimension_i;
uniform texture2d image;
struct VertInOut {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
VertInOut VSDefault(VertInOut vert_in)
{
VertInOut vert_out;
vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
vert_out.uv = vert_in.uv;
return vert_out;
}
float4 PSDrawAreaRGBA(VertInOut vert_in) : TARGET
{
float4 totalcolor = float4(0.0, 0.0, 0.0, 0.0);
float2 uv = vert_in.uv;
float2 uvdelta = float2(ddx(uv.x), ddy(uv.y));
// Handle potential OpenGL flip.
uvdelta.y = abs(uvdelta.y);
float2 uvhalfdelta = 0.5 * uvdelta;
float2 uvmin = uv - uvhalfdelta;
float2 uvmax = uv + uvhalfdelta;
int2 loadindexmin = int2(uvmin / base_dimension_i);
int2 loadindexmax = int2(uvmax / base_dimension_i);
float2 targetpos = uv / uvdelta;
float2 targetposmin = targetpos - 0.5;
float2 targetposmax = targetpos + 0.5;
float2 scale = base_dimension_i / uvdelta;
for (int loadindexy = loadindexmin.y; loadindexy <= loadindexmax.y; ++loadindexy)
{
for (int loadindexx = loadindexmin.x; loadindexx <= loadindexmax.x; ++loadindexx)
{
int2 loadindex = int2(loadindexx, loadindexy);
float2 potentialtargetmin = float2(loadindex) * scale;
float2 potentialtargetmax = potentialtargetmin + scale;
float2 targetmin = max(potentialtargetmin, targetposmin);
float2 targetmax = min(potentialtargetmax, targetposmax);
float area = (targetmax.x - targetmin.x) * (targetmax.y - targetmin.y);
float4 sample = image.Load(int3(loadindex, 0));
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 12:54:17 +02:00
totalcolor += area * sample;
}
}
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 12:54:17 +02:00
return totalcolor;
}
technique Draw
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBA(vert_in);
}
}