/*

#include <metal_stdlib>
using namespace metal;

// 원형 모션 블러 셰이더 - Mirza Beig 참조 구현 (수정)
// 원본: https://x.com/TheMirzaBeig/status/1887798802641211801

constexpr sampler s(coord::normalized, address::clamp_to_edge, filter::linear);

#define PI 3.14159265359
#define MOTION_BLUR_STEPS 128
#define MOTION_BLUR_POWER 0.1
#define MOTION_BLUR_AMPLITUDE 35.0 // 각도 단위 (도)

// 각도를 라디안으로 변환하는 함수 (Metal에는 radians 내장 함수가 없음)
float degreesToRadians(float degrees) {
    return degrees * (PI / 180.0);
}

// 시계 방향 회전 행렬 구현 (음수 각도는 반시계 방향)
float2x2 GetRotationMatrix2D(float angle) {
    float s = sin(angle);
    float c = cos(angle);
    return float2x2(c, -s, s, c);
}

// Mirza Beig의 CircularBlur 함수를 Metal로 포팅 (균등 블러 적용)
float4 CircularBlur(
    texture2d<float, access::sample> tex,
    float2 uv,
    float2 center,
    float aspectRatio,
    float rotation,
    float power,
    int steps,
    sampler texSampler
) {
    float4 colour = float4(0.0);
    float totalWeight = 0.0;
    
    float steps_float = float(steps);
    float stepsMinusOne = steps_float - 1.0;
    
    // 중심에서 현재 UV까지의 오프셋
    float2 offsetFromCenterToUV = uv - center;
    
    // 블러 루프
    for (int i = 0; i < steps; i++) {
        float i_float = float(i);
        float progress = i_float / stepsMinusOne;
        float angle = progress * rotation;
        
        // 종횡비 보정된 오프셋
        float2 aspectCorrectedOffsetFromCenterToUV = offsetFromCenterToUV;
        aspectCorrectedOffsetFromCenterToUV.x *= aspectRatio;
        
        // 원형 회전 적용
        float2 rotatedOffsetFromCenterToUV = GetRotationMatrix2D(angle) * aspectCorrectedOffsetFromCenterToUV;
        
        // 종횡비 보정 복원
        rotatedOffsetFromCenterToUV.x /= aspectRatio;
        float2 offset = rotatedOffsetFromCenterToUV + center;
        
        // 가중치 계산 - 진행률이 낮을수록 높은 가중치
        float weight = pow(1.0 - progress, power);
        
        // 경계 검사를 추가하여 안전성 강화
        if (offset.x >= 0.0 && offset.x <= 1.0 && 
            offset.y >= 0.0 && offset.y <= 1.0) {
            colour += tex.sample(texSampler, offset) * weight;
            totalWeight += weight;
        }
    }
    
    // 가중치 합이 0인 경우 처리 (안전 장치)
    if (totalWeight > 0.0) {
        return colour / totalWeight;
    } else {
        return tex.sample(texSampler, uv);
    }
}

kernel void Lens_Screw(
    texture2d<float, access::sample> input [[texture(0)]],
    texture2d<float, access::write> output [[texture(1)]],
    constant float& radius [[buffer(0)]],        // 중심부 마스크 반경 (선명 영역 크기)
    constant float& blurAmount [[buffer(1)]],    // 블러 강도 조절 인자
    uint2 gid [[thread_position_in_grid]]
) {
    float2 resolution = float2(input.get_width(), input.get_height());
    float2 uv = float2(gid) / resolution;
    
    // 화면 비율 계산
    float aspectRatio = resolution.x / resolution.y;
    
    // 블러 중심점은 화면 중앙 고정
    float2 center = float2(0.5, 0.5);
    
    // 종횡비 보정
    float2 aspectCorrectedUV = uv;
    aspectCorrectedUV.x *= aspectRatio;
    
    float2 aspectCorrectedCenter = center;
    aspectCorrectedCenter.x *= aspectRatio;
    
    // 중심에서의 거리 계산 (마스크 용)
    float dist = length(aspectCorrectedUV - aspectCorrectedCenter);
    float mask = smoothstep(radius, radius + 0.3, dist);
    
    // 균등한 회전 블러 강도 설정
    float uniformRotation = degreesToRadians(blurAmount * MOTION_BLUR_AMPLITUDE);
    
    // 원본 색상 가져오기
    float4 originalColor = input.sample(s, uv);
    
    // 모든 픽셀에 균등하게 CircularBlur 적용
    float4 blurredColor = CircularBlur(
        input,
        uv,
        center,
        aspectRatio,
        uniformRotation,
        MOTION_BLUR_POWER,
        MOTION_BLUR_STEPS,
        s
    );
    
    // 마스크에 따라 원본과 블러 이미지 혼합
    float4 finalColor = mix(originalColor, blurredColor, mask);
    
    // 출력
    output.write(finalColor, gid);
}


*/