我正在尝试在HLSL中实现高斯模糊着色器,代码似乎是正确的,但结果图像全是白色,除非我通过将结果颜色跳过1000来进行肮脏的黑客攻击。即使它现在显示一些东西,因为颜色被改变,这是不正确的:
sampler2D input : register(s0);
// new HLSL shader
// modify the comment parameters to reflect your shader parameters
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>0,0/minValue>
/// <maxValue>1024,1024</maxValue>
/// <defaultValue>599,124</defaultValue>
float2 InputSize : register(C0);
const float pi = 3.141592f;
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 color = tex2D( input , uv.xy);
float sigma = 1.4;
float sigmaSq = sigma * sigma;
int kernel = 21;
int radius = kernel / 2;
for(int i = -radius; i <= radius; i++)
{
float x = 1.0f / radius * i;
float xSq = x * x;
float gb = 1.0f / sqrt(2.0f * pi * sigmaSq) * exp( -( xSq / (2.0f * sigmaSq)));
float2 st = float2(1.0f / InputSize.x * i, 0.0f);
float3 c = tex2D(input, uv + st).xyz;
color.xyz += (c * gb) / 1000.0;
}
return color;
}
这是我的参考图片(使用AForge.NET制作,Sigma = 5,尺寸= 21)
请注意,与此图片相比,我只在X轴上进行了此操作,我试图先将其设为正确。
如果您想尝试原始图片,可以将着色器代码粘贴到Shazzam中:
答案 0 :(得分:0)
sampler2D input : register(s0);
// new HLSL shader
// modify the comment parameters to reflect your shader parameters
/// <summary>Explain the purpose of this variable.</summary>
/// <minValue>1, 1/minValue>
/// <maxValue>1024, 1024</maxValue>
/// <defaultValue>599, 124</defaultValue>
float2 InputSize : register(C0);
static const float kernel[441] = {
{
0.0001253219113395547, 0.00018325629967027186, 0.000257465496677347, 0.00034754206833684524, 0.0004507377648081042, 0.0005616538403243442, 0.0006724217297003478, 0.0007734690974565646, 0.0008548155525392126, 0.0009076743942415001, 0.0009260106333131518, 0.0009076743942415001, 0.0008548155525392126, 0.0007734690974565646, 0.0006724217297003478, 0.0005616538403243442, 0.0004507377648081042, 0.00034754206833684524, 0.000257465496677347, 0.00018325629967027186, 0.0001253219113395547,
0.00018325629967027186, 0.000267972862924577, 0.00037648782810231063, 0.0005082054107090616, 0.0006591068873549146, 0.0008212977553028339, 0.0009832719329415178, 0.0011310319416143256, 0.0012499836092867847, 0.0013272782789233823, 0.00135409107874604, 0.0013272782789233823, 0.0012499836092867847, 0.0011310319416143256, 0.0009832719329415178, 0.0008212977553028339, 0.0006591068873549146, 0.0005082054107090616, 0.00037648782810231063, 0.000267972862924577, 0.00018325629967027186,
0.000257465496677347, 0.00037648782810231063, 0.0005289456669688587, 0.0007140019672870738, 0.0009260106333131518, 0.001153880302447998, 0.001381445533054985, 0.0015890406012215013, 0.0017561614601114424, 0.0018647564199807886, 0.0019024269984878957, 0.0018647564199807886, 0.0017561614601114424, 0.0015890406012215013, 0.001381445533054985, 0.001153880302447998, 0.0009260106333131518, 0.0007140019672870738, 0.0005289456669688587, 0.00037648782810231063, 0.000257465496677347,
0.00034754206833684524, 0.0005082054107090616, 0.0007140019672870738, 0.0009638018441690448, 0.0012499836092867847, 0.0015575754891478846, 0.0018647564199807886, 0.0021449804511547003, 0.0023705700144569524, 0.0025171578774949516, 0.002568007839679253, 0.0025171578774949516, 0.0023705700144569524, 0.0021449804511547003, 0.0018647564199807886, 0.0015575754891478846, 0.0012499836092867847, 0.0009638018441690448, 0.0007140019672870738, 0.0005082054107090616, 0.00034754206833684524,
0.0004507377648081042, 0.0006591068873549146, 0.0009260106333131518, 0.0012499836092867847, 0.0016211413507230966, 0.0020200665141290397, 0.002418458705376228, 0.002781889682412439, 0.003074463574296922, 0.003264577784210943, 0.0033305266300735804, 0.003264577784210943, 0.003074463574296922, 0.002781889682412439, 0.002418458705376228, 0.0020200665141290397, 0.0016211413507230966, 0.0012499836092867847, 0.0009260106333131518, 0.0006591068873549146, 0.0004507377648081042,
0.0005616538403243442, 0.0008212977553028339, 0.001153880302447998, 0.0015575754891478846, 0.0020200665141290397, 0.0025171578774949516, 0.003013585116655789, 0.003466448000315246, 0.0038310175189698874, 0.004067914412097752, 0.004150091734336279, 0.004067914412097752, 0.0038310175189698874, 0.003466448000315246, 0.003013585116655789, 0.0025171578774949516, 0.0020200665141290397, 0.0015575754891478846, 0.001153880302447998, 0.0008212977553028339, 0.0005616538403243442,
0.0006724217297003478, 0.0009832719329415178, 0.001381445533054985, 0.0018647564199807886, 0.002418458705376228, 0.003013585116655789, 0.003607916426905765, 0.004150091734336279, 0.004586560692134574, 0.004870177765856866, 0.004968561882895685, 0.004870177765856866, 0.004586560692134574, 0.004150091734336279, 0.003607916426905765, 0.003013585116655789, 0.002418458705376228, 0.0018647564199807886, 0.001381445533054985, 0.0009832719329415178, 0.0006724217297003478,
0.0007734690974565646, 0.0011310319416143256, 0.0015890406012215013, 0.0021449804511547003, 0.002781889682412439, 0.003466448000315246, 0.004150091734336279, 0.004773741784860959, 0.005275800591030858, 0.005602037879842168, 0.005715206551895624, 0.005602037879842168, 0.005275800591030858, 0.004773741784860959, 0.004150091734336279, 0.003466448000315246, 0.002781889682412439, 0.0021449804511547003, 0.0015890406012215013, 0.0011310319416143256, 0.0007734690974565646,
0.0008548155525392126, 0.0012499836092867847, 0.0017561614601114424, 0.0023705700144569524, 0.003074463574296922, 0.0038310175189698874, 0.004586560692134574, 0.005275800591030858, 0.005830661382773607, 0.0061912093467597126, 0.006316280071950432, 0.0061912093467597126, 0.005830661382773607, 0.005275800591030858, 0.004586560692134574, 0.0038310175189698874, 0.003074463574296922, 0.0023705700144569524, 0.0017561614601114424, 0.0012499836092867847, 0.0008548155525392126,
0.0009076743942415001, 0.0013272782789233823, 0.0018647564199807886, 0.0025171578774949516, 0.003264577784210943, 0.004067914412097752, 0.004870177765856866, 0.005602037879842168, 0.0061912093467597126, 0.006574052351702679, 0.0067068570186131155, 0.006574052351702679, 0.0061912093467597126, 0.005602037879842168, 0.004870177765856866, 0.004067914412097752, 0.003264577784210943, 0.0025171578774949516, 0.0018647564199807886, 0.0013272782789233823, 0.0009076743942415001,
0.0009260106333131518, 0.00135409107874604, 0.0019024269984878957, 0.002568007839679253, 0.0033305266300735804, 0.004150091734336279, 0.004968561882895685, 0.005715206551895624, 0.006316280071950432, 0.0067068570186131155, 0.006842344517756951, 0.0067068570186131155, 0.006316280071950432, 0.005715206551895624, 0.004968561882895685, 0.004150091734336279, 0.0033305266300735804, 0.002568007839679253, 0.0019024269984878957, 0.00135409107874604, 0.0009260106333131518,
0.0009076743942415001, 0.0013272782789233823, 0.0018647564199807886, 0.0025171578774949516, 0.003264577784210943, 0.004067914412097752, 0.004870177765856866, 0.005602037879842168, 0.0061912093467597126, 0.006574052351702679, 0.0067068570186131155, 0.006574052351702679, 0.0061912093467597126, 0.005602037879842168, 0.004870177765856866, 0.004067914412097752, 0.003264577784210943, 0.0025171578774949516, 0.0018647564199807886, 0.0013272782789233823, 0.0009076743942415001,
0.0008548155525392126, 0.0012499836092867847, 0.0017561614601114424, 0.0023705700144569524, 0.003074463574296922, 0.0038310175189698874, 0.004586560692134574, 0.005275800591030858, 0.005830661382773607, 0.0061912093467597126, 0.006316280071950432, 0.0061912093467597126, 0.005830661382773607, 0.005275800591030858, 0.004586560692134574, 0.0038310175189698874, 0.003074463574296922, 0.0023705700144569524, 0.0017561614601114424, 0.0012499836092867847, 0.0008548155525392126,
0.0007734690974565646, 0.0011310319416143256, 0.0015890406012215013, 0.0021449804511547003, 0.002781889682412439, 0.003466448000315246, 0.004150091734336279, 0.004773741784860959, 0.005275800591030858, 0.005602037879842168, 0.005715206551895624, 0.005602037879842168, 0.005275800591030858, 0.004773741784860959, 0.004150091734336279, 0.003466448000315246, 0.002781889682412439, 0.0021449804511547003, 0.0015890406012215013, 0.0011310319416143256, 0.0007734690974565646,
0.0006724217297003478, 0.0009832719329415178, 0.001381445533054985, 0.0018647564199807886, 0.002418458705376228, 0.003013585116655789, 0.003607916426905765, 0.004150091734336279, 0.004586560692134574, 0.004870177765856866, 0.004968561882895685, 0.004870177765856866, 0.004586560692134574, 0.004150091734336279, 0.003607916426905765, 0.003013585116655789, 0.002418458705376228, 0.0018647564199807886, 0.001381445533054985, 0.0009832719329415178, 0.0006724217297003478,
0.0005616538403243442, 0.0008212977553028339, 0.001153880302447998, 0.0015575754891478846, 0.0020200665141290397, 0.0025171578774949516, 0.003013585116655789, 0.003466448000315246, 0.0038310175189698874, 0.004067914412097752, 0.004150091734336279, 0.004067914412097752, 0.0038310175189698874, 0.003466448000315246, 0.003013585116655789, 0.0025171578774949516, 0.0020200665141290397, 0.0015575754891478846, 0.001153880302447998, 0.0008212977553028339, 0.0005616538403243442,
0.0004507377648081042, 0.0006591068873549146, 0.0009260106333131518, 0.0012499836092867847, 0.0016211413507230966, 0.0020200665141290397, 0.002418458705376228, 0.002781889682412439, 0.003074463574296922, 0.003264577784210943, 0.0033305266300735804, 0.003264577784210943, 0.003074463574296922, 0.002781889682412439, 0.002418458705376228, 0.0020200665141290397, 0.0016211413507230966, 0.0012499836092867847, 0.0009260106333131518, 0.0006591068873549146, 0.0004507377648081042,
0.00034754206833684524, 0.0005082054107090616, 0.0007140019672870738, 0.0009638018441690448, 0.0012499836092867847, 0.0015575754891478846, 0.0018647564199807886, 0.0021449804511547003, 0.0023705700144569524, 0.0025171578774949516, 0.002568007839679253, 0.0025171578774949516, 0.0023705700144569524, 0.0021449804511547003, 0.0018647564199807886, 0.0015575754891478846, 0.0012499836092867847, 0.0009638018441690448, 0.0007140019672870738, 0.0005082054107090616, 0.00034754206833684524,
0.000257465496677347, 0.00037648782810231063, 0.0005289456669688587, 0.0007140019672870738, 0.0009260106333131518, 0.001153880302447998, 0.001381445533054985, 0.0015890406012215013, 0.0017561614601114424, 0.0018647564199807886, 0.0019024269984878957, 0.0018647564199807886, 0.0017561614601114424, 0.0015890406012215013, 0.001381445533054985, 0.001153880302447998, 0.0009260106333131518, 0.0007140019672870738, 0.0005289456669688587, 0.00037648782810231063, 0.000257465496677347,
0.00018325629967027186, 0.000267972862924577, 0.00037648782810231063, 0.0005082054107090616, 0.0006591068873549146, 0.0008212977553028339, 0.0009832719329415178, 0.0011310319416143256, 0.0012499836092867847, 0.0013272782789233823, 0.00135409107874604, 0.0013272782789233823, 0.0012499836092867847, 0.0011310319416143256, 0.0009832719329415178, 0.0008212977553028339, 0.0006591068873549146, 0.0005082054107090616, 0.00037648782810231063, 0.000267972862924577, 0.00018325629967027186,
0.0001253219113395547, 0.00018325629967027186, 0.000257465496677347, 0.00034754206833684524, 0.0004507377648081042, 0.0005616538403243442, 0.0006724217297003478, 0.0007734690974565646, 0.0008548155525392126, 0.0009076743942415001, 0.0009260106333131518, 0.0009076743942415001, 0.0008548155525392126, 0.0007734690974565646, 0.0006724217297003478, 0.0005616538403243442, 0.0004507377648081042, 0.00034754206833684524, 0.000257465496677347, 0.00018325629967027186, 0.0001253219113395547,
}};
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 color = float4(0,0,0,0);
float w = 1.0f / InputSize.x;
float h = 1.0f / InputSize.y;
int width = 21;
int center = width / 2;
for(int x = -center; x <= center; x++)
for(int y = -center; y <= center; y++)
{
//float2 p = float2((x - center) * w, (y - center) * h);
//float4 c = tex2D(input, saturate(uv.xy + p));
float4 c = tex2D(input, saturate(uv.xy + float2(x * w, y * h)));
int offset = (y + center) * width + (x + center);
float g = kernel[offset];
color += c * g;
}
return color;
}