obs-studio/libobs/data/area.effect

120 lines
3.4 KiB
Plaintext
Raw Normal View History

uniform float4x4 ViewProj;
uniform float4x4 color_matrix;
uniform float3 color_range_min = {0.0, 0.0, 0.0};
uniform float3 color_range_max = {1.0, 1.0, 1.0};
uniform float2 base_dimension_i;
uniform texture2d image;
sampler_state def_sampler {
Filter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
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));
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 targetposleft = targetpos - 0.5;
float2 targetposright = targetpos + 0.5;
for (int loadindexy = loadindexmin.y; loadindexy <= loadindexmax.y; ++loadindexy)
{
for (int loadindexx = loadindexmin.x; loadindexx <= loadindexmax.x; ++loadindexx)
{
float2 loadindex = float2(loadindexx, loadindexy);
float2 potentialtargetmin = loadindex / uvdelta * base_dimension_i;
float2 potentialtargetmax = (loadindex + 1.0) / uvdelta * base_dimension_i;
float2 targetmin = max(potentialtargetmin, targetposleft);
float2 targetmax = min(potentialtargetmax, targetposright);
float area = (targetmax.x - targetmin.x) * (targetmax.y - targetmin.y);
float4 sample = image.SampleLevel(def_sampler, (loadindex + 0.5) * base_dimension_i, 0.0);
totalcolor += area * float4(sample.rgb * sample.a, sample.a);
}
}
return float4(totalcolor.rgb / totalcolor.a, totalcolor.a);
}
float3 ConvertFromYuv(float3 yuv)
{
yuv = clamp(yuv, color_range_min, color_range_max);
return saturate(mul(float4(yuv, 1.0), color_matrix)).rgb;
}
float4 PSDrawAreaMatrix(VertInOut vert_in) : TARGET
{
float3 totalcolor = float3(0.0, 0.0, 0.0);
float2 uv = vert_in.uv;
float2 uvdelta = float2(ddx(uv.x), ddy(uv.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 targetposleft = targetpos - 0.5;
float2 targetposright = targetpos + 0.5;
for (int loadindexy = loadindexmin.y; loadindexy <= loadindexmax.y; ++loadindexy)
{
for (int loadindexx = loadindexmin.x; loadindexx <= loadindexmax.x; ++loadindexx)
{
float2 loadindex = float2(loadindexx, loadindexy);
float2 potentialtargetmin = loadindex / uvdelta * base_dimension_i;
float2 potentialtargetmax = (loadindex + 1.0) / uvdelta * base_dimension_i;
float2 targetmin = max(potentialtargetmin, targetposleft);
float2 targetmax = min(potentialtargetmax, targetposright);
float area = (targetmax.x - targetmin.x) * (targetmax.y - targetmin.y);
float3 yuv = image.SampleLevel(def_sampler, (loadindex + 0.5) * base_dimension_i, 0.0).xyz;
totalcolor += area * ConvertFromYuv(yuv);
}
}
return float4(totalcolor, 1.0);
}
technique Draw
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaRGBA(vert_in);
}
}
technique DrawMatrix
{
pass
{
vertex_shader = VSDefault(vert_in);
pixel_shader = PSDrawAreaMatrix(vert_in);
}
}