float edgeLine(float value, float target, float pixels, float resolution)
{
	float halfWidth = max(pixels, 0.5) * 0.5 / max(resolution, 1.0);
	return 1.0 - smoothstep(halfWidth, halfWidth * 2.0, abs(value - target));
}

float rectGuide(float2 uv, float2 inset, float2 resolution)
{
	float mask = 0.0;
	float thickness = max(lineThicknessPixels, 0.5);
	if (uv.x >= inset.x && uv.x <= 1.0 - inset.x && uv.y >= inset.y && uv.y <= 1.0 - inset.y)
	{
		mask = max(mask, edgeLine(uv.x, inset.x, thickness, resolution.x));
		mask = max(mask, edgeLine(uv.x, 1.0 - inset.x, thickness, resolution.x));
		mask = max(mask, edgeLine(uv.y, inset.y, thickness, resolution.y));
		mask = max(mask, edgeLine(uv.y, 1.0 - inset.y, thickness, resolution.y));
	}
	return mask;
}

float aspectMatte(float2 uv, float2 resolution)
{
	if (aspectMode == 0)
		return 0.0;

	float targetAspect = aspectMode == 1 ? 2.39 : (aspectMode == 2 ? 1.85 : 1.0);
	float frameAspect = resolution.x / max(resolution.y, 1.0);
	if (frameAspect <= targetAspect)
	{
		float contentHeight = frameAspect / targetAspect;
		float inset = (1.0 - contentHeight) * 0.5;
		return (uv.y < inset || uv.y > 1.0 - inset) ? 1.0 : 0.0;
	}

	float contentWidth = targetAspect / frameAspect;
	float inset = (1.0 - contentWidth) * 0.5;
	return (uv.x < inset || uv.x > 1.0 - inset) ? 1.0 : 0.0;
}

float4 shadeVideo(ShaderContext context)
{
	float2 resolution = max(context.outputResolution, float2(1.0, 1.0));
	float3 color = lerp(context.sourceColor.rgb, float3(0.0, 0.0, 0.0), aspectMatte(context.uv, resolution) * saturate(matteOpacity));

	float mask = 0.0;
	if (showActionSafe)
		mask = max(mask, rectGuide(context.uv, float2(0.05, 0.05), resolution));
	if (showTitleSafe)
		mask = max(mask, rectGuide(context.uv, float2(0.10, 0.10), resolution));
	if (showCenter)
	{
		mask = max(mask, edgeLine(context.uv.x, 0.5, lineThicknessPixels, resolution.x) * step(abs(context.uv.y - 0.5), 0.035));
		mask = max(mask, edgeLine(context.uv.y, 0.5, lineThicknessPixels, resolution.y) * step(abs(context.uv.x - 0.5), 0.035));
	}

	float opacity = mask * saturate(lineOpacity) * lineColor.a;
	return float4(lerp(color, lineColor.rgb, opacity), context.sourceColor.a);
}
