77 lines
4.4 KiB
HLSL

#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma target 2.0
#ifndef AA_SHADER_INCLUDED
#define AA_SHADER_INCLUDED
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
// 미리 계산된 17x17 가우시안 가중치
static const float gaussianWeights[17][17] = {
{0.000012, 0.000015, 0.000018, 0.000020, 0.000021, 0.000022, 0.000022, 0.000022, 0.000022, 0.000022, 0.000022, 0.000021, 0.000020, 0.000018, 0.000015, 0.000012, 0.000009},
{0.000015, 0.000020, 0.000025, 0.000028, 0.000030, 0.000031, 0.000031, 0.000031, 0.000031, 0.000031, 0.000031, 0.000030, 0.000028, 0.000025, 0.000020, 0.000015, 0.000012},
{0.000018, 0.000025, 0.000031, 0.000035, 0.000037, 0.000038, 0.000039, 0.000039, 0.000039, 0.000039, 0.000038, 0.000037, 0.000035, 0.000031, 0.000025, 0.000018, 0.000015},
{0.000020, 0.000028, 0.000035, 0.000039, 0.000042, 0.000043, 0.000044, 0.000044, 0.000044, 0.000044, 0.000043, 0.000042, 0.000039, 0.000035, 0.000028, 0.000020, 0.000016},
{0.000021, 0.000030, 0.000037, 0.000042, 0.000044, 0.000046, 0.000046, 0.000047, 0.000047, 0.000046, 0.000046, 0.000044, 0.000042, 0.000037, 0.000030, 0.000021, 0.000017},
{0.000022, 0.000031, 0.000038, 0.000043, 0.000046, 0.000047, 0.000048, 0.000048, 0.000048, 0.000048, 0.000047, 0.000046, 0.000043, 0.000038, 0.000031, 0.000022, 0.000018},
{0.000022, 0.000031, 0.000039, 0.000044, 0.000046, 0.000048, 0.000048, 0.000049, 0.000049, 0.000048, 0.000048, 0.000046, 0.000044, 0.000039, 0.000031, 0.000022, 0.000018},
{0.000022, 0.000031, 0.000039, 0.000044, 0.000047, 0.000048, 0.000049, 0.000049, 0.000049, 0.000049, 0.000048, 0.000047, 0.000044, 0.000039, 0.000031, 0.000022, 0.000018},
{0.000022, 0.000031, 0.000039, 0.000044, 0.000047, 0.000048, 0.000049, 0.000049, 0.000049, 0.000049, 0.000048, 0.000047, 0.000044, 0.000039, 0.000031, 0.000022, 0.000018},
{0.000022, 0.000031, 0.000039, 0.000044, 0.000046, 0.000048, 0.000049, 0.000049, 0.000049, 0.000049, 0.000048, 0.000046, 0.000044, 0.000039, 0.000031, 0.000022, 0.000018},
{0.000022, 0.000031, 0.000038, 0.000043, 0.000046, 0.000047, 0.000048, 0.000048, 0.000048, 0.000048, 0.000047, 0.000046, 0.000043, 0.000038, 0.000031, 0.000022, 0.000018},
{0.000021, 0.000030, 0.000037, 0.000042, 0.000044, 0.000046, 0.000046, 0.000047, 0.000047, 0.000046, 0.000046, 0.000044, 0.000042, 0.000037, 0.000030, 0.000021, 0.000017},
{0.000020, 0.000028, 0.000035, 0.000039, 0.000042, 0.000043, 0.000044, 0.000044, 0.000044, 0.000044, 0.000043, 0.000042, 0.000039, 0.000035, 0.000028, 0.000020, 0.000016},
{0.000018, 0.000025, 0.000031, 0.000035, 0.000037, 0.000038, 0.000039, 0.000039, 0.000039, 0.000039, 0.000038, 0.000037, 0.000035, 0.000031, 0.000025, 0.000018, 0.000015},
{0.000015, 0.000020, 0.000025, 0.000028, 0.000030, 0.000031, 0.000031, 0.000031, 0.000031, 0.000031, 0.000031, 0.000030, 0.000028, 0.000025, 0.000020, 0.000015, 0.000012},
{0.000012, 0.000015, 0.000018, 0.000020, 0.000021, 0.000022, 0.000022, 0.000022, 0.000022, 0.000022, 0.000022, 0.000021, 0.000020, 0.000018, 0.000015, 0.000012, 0.000009},
{0.000009, 0.000012, 0.000015, 0.000016, 0.000017, 0.000018, 0.000018, 0.000018, 0.000018, 0.000018, 0.000018, 0.000017, 0.000016, 0.000015, 0.000012, 0.000009, 0.000007}
};
void AAFromGreenChannel_float(
float2 UV,
UnityTexture2D Tex,
UnitySamplerState Samp,
float2 Resolution,
float Strength,
out float Out)
{
float2 texelSize = 1.0 / Resolution;
float centerG = SAMPLE_TEXTURE2D(Tex, Samp, UV).g;
float sum = 0;
float weightSum = 0;
// 미리 계산된 가중치를 사용한 16x16 그리드 샘플링
for(int y = 0; y < 17; y++)
{
for(int x = 0; x < 17; x++)
{
float2 offset = float2(x - 8, y - 8) * texelSize;
float2 sampleUV = UV + offset;
float weight = gaussianWeights[y][x];
float sampleG = SAMPLE_TEXTURE2D(Tex, Samp, sampleUV).g;
sum += sampleG * weight;
weightSum += weight;
}
}
float avgG = sum / weightSum;
float diff = abs(avgG - centerG) * Strength;
Out = lerp(centerG, avgG, saturate(diff));
}
// half 정밀도 버전도 제공 (성능 최적화용)
void AAFromGreenChannel_half(
float2 UV,
UnityTexture2D Tex,
UnitySamplerState Samp,
float2 Resolution,
float Strength,
out half Out)
{
Out = (half)AAFromGreenChannel_float(UV, Tex, Samp, Resolution, Strength, Out);
}
#endif