float lineMask(float coordinate, float target, float pixelThickness, float resolution)
{
	float halfWidth = max(pixelThickness, 0.5) * 0.5 / max(resolution, 1.0);
	float distanceToLine = abs(coordinate - target);
	float feather = halfWidth * 0.85 + (1.0 / max(resolution, 1.0));
	return 1.0 - smoothstep(halfWidth, feather, distanceToLine);
}

float thirdsMask(float2 uv, float2 resolution)
{
	float thickness = max(lineThicknessPixels, 0.5);
	float mask = 0.0;
	mask = max(mask, lineMask(uv.x, 1.0 / 3.0, thickness, resolution.x));
	mask = max(mask, lineMask(uv.x, 2.0 / 3.0, thickness, resolution.x));
	mask = max(mask, lineMask(uv.y, 1.0 / 3.0, thickness, resolution.y));
	mask = max(mask, lineMask(uv.y, 2.0 / 3.0, thickness, resolution.y));
	return mask;
}

float crosshairMask(float2 uv, float2 resolution)
{
	float2 pixel = (uv - 0.5) * resolution;
	float halfThickness = max(lineThicknessPixels, 0.5) * 0.5;
	float halfSize = max(crosshairSizePixels, 1.0) * 0.5;
	float halfGap = max(crosshairGapPixels, 0.0) * 0.5;

	float horizontalExtent = step(abs(pixel.x), halfSize) * (1.0 - step(abs(pixel.x), halfGap));
	float verticalExtent = step(abs(pixel.y), halfSize) * (1.0 - step(abs(pixel.y), halfGap));

	float horizontalLine = horizontalExtent * (1.0 - smoothstep(halfThickness, halfThickness + 1.5, abs(pixel.y)));
	float verticalLine = verticalExtent * (1.0 - smoothstep(halfThickness, halfThickness + 1.5, abs(pixel.x)));
	return max(horizontalLine, verticalLine);
}

float4 shadeVideo(ShaderContext context)
{
	float2 resolution = max(context.outputResolution, float2(1.0, 1.0));
	float mask = 0.0;

	if (showThirds)
		mask = max(mask, thirdsMask(context.uv, resolution));
	if (showCrosshair)
		mask = max(mask, crosshairMask(context.uv, resolution));

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