float mirroredCoordinate(float coordinate)
{
	float wrapped = frac(coordinate * 0.5) * 2.0;
	return wrapped <= 1.0 ? wrapped : 2.0 - wrapped;
}

float roundedBoxMask(float2 point, float2 halfSize, float radius, float feather)
{
	float2 distanceToEdge = abs(point) - (halfSize - radius);
	float outsideDistance = length(max(distanceToEdge, float2(0.0, 0.0))) - radius;
	float insideDistance = min(max(distanceToEdge.x, distanceToEdge.y), 0.0);
	float signedDistance = outsideDistance + insideDistance;
	return 1.0 - smoothstep(0.0, max(feather, 0.00001), signedDistance);
}

float sourceBoundsMask(float2 uv, float2 resolution)
{
	float2 pixel = 1.0 / max(resolution, float2(1.0, 1.0));
	float2 feather = pixel * max(sourceEdgeFeather, 0.0);
	float left = smoothstep(0.0, max(feather.x, 0.00001), uv.x);
	float right = 1.0 - smoothstep(1.0 - max(feather.x, 0.00001), 1.0, uv.x);
	float top = smoothstep(0.0, max(feather.y, 0.00001), uv.y);
	float bottom = 1.0 - smoothstep(1.0 - max(feather.y, 0.00001), 1.0, uv.y);
	return saturate(left * right * top * bottom);
}

float2 applyBulge(float2 uv, float2 resolution)
{
	float2 centered = uv * 2.0 - 1.0;
	float aspect = resolution.x / max(resolution.y, 1.0);
	float2 aspectCentered = float2(centered.x * aspect, centered.y);
	float radiusSq = dot(aspectCentered, aspectCentered);
	float amount = clamp(bulgeAmount, -0.95, 0.95);
	float scale = 1.0 / max(1.0 + amount * radiusSq, 0.05);
	return centered * scale / max(zoom, 0.001) * 0.5 + 0.5;
}

float4 sampleWarped(float2 uv, float2 resolution, out bool insideSource)
{
	insideSource = uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0;

	if (edgeMode == 1)
		return sampleVideo(clamp(uv, 0.0, 1.0));
	if (edgeMode == 2)
		return sampleVideo(float2(mirroredCoordinate(uv.x), mirroredCoordinate(uv.y)));

	float edgeMask = sourceBoundsMask(uv, resolution);
	float4 color = sampleVideo(clamp(uv, 0.0, 1.0));
	return lerp(outsideColor, color, edgeMask);
}

float4 shadeVideo(ShaderContext context)
{
	float2 resolution = max(context.outputResolution, float2(1.0, 1.0));
	float2 sourceUv = applyBulge(context.uv, resolution);

	bool insideSource = false;
	float4 color = sampleWarped(sourceUv, resolution, insideSource);

	float2 centered = context.uv * 2.0 - 1.0;
	float feather = max(edgeFeather, 0.0) / min(resolution.x, resolution.y);
	float screenMask = roundedBoxMask(centered, float2(1.0, 1.0), saturate(edgeRoundness), feather);
	color = lerp(outsideColor, color, screenMask);

	float2 aspectCentered = float2(centered.x * resolution.x / max(resolution.y, 1.0), centered.y);
	float edgeDistance = saturate(length(aspectCentered) * 0.72);
	float vignette = lerp(1.0, 1.0 - saturate(vignetteAmount), smoothstep(0.35, 1.05, edgeDistance));
	color.rgb *= vignette;

	return saturate(color);
}
