关于HLSL的问题,HLSL
写了一个pixelshader,目的是将一张bayer格式的图像转换成一张彩色图像,代码如下:texture g_Texture;
int g_Width;
int g_Heigth;
int g_StartWidth;
int g_StartHeight;
struct VS_INPUT
{
vector position : POSITION;
float2 uvCoords : TEXCOORD;
};
sampler SamplerAll =
sampler_state
{
Texture = <g_Texture>;
AddressU = Clamp;
AddressV = Clamp;
MipFilter = Point;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
float2 PixelCoordsDownFilter[9] =
{
{ -1, -1 },
{ 0, -1 },
{ 1, -1 },
{ -1, 0 },
{ 0, 0 },
{ 1, 0 },
{ -1, 1 },
{ 0, 1 },
{ 1, 1 },
};
float2 TexelCoordsDownFilter[9]
<
string ConvertPixelsToTexels = "PixelCoordsDownFilter";
>;
float4 PS_Main(VS_INPUT input): COLOR0
{
int i,j;
float divR=0;
float divG=0;
float divB=0;
float4 colorR = 0;
float4 colorG = 0;
float4 colorB = 0;
int w,h;
int x,y;
x=input.position.x-g_StartWidth;
y=input.position.y-g_StartHeight;
int index = 0;
float4 colortmp = 0;
for(j=-1; j<=1; j++)
{
for(i=-1; i<=1; i++)
{
w=x+i;
h=y+j;
colortmp = tex2D( SamplerAll, input.uvCoords + TexelCoordsDownFilter[index].xy );
index += 1;
if((h%2) == 1)
{
if((w%2) == 1)
{
colorG += colortmp;
divG += 1;
}
else
{
colorB += colortmp;
divB += 1;
}
}
else
{
if((w%2) == 1)
{
colorR += colortmp;
divR += 1;
}
else
{
colorG += colortmp;
divG += 1;
}
}
}
}
if(divR > 0)
{
colorR /= divR;
}
if(divG > 0 )
{
colorG /= divG;
}
if(divB > 0)
{
colorB /= divB;
}
float4 Color = 0;// float4(colorR.x,colorG.x,colorB.x,0);
//Color.r = colorR.r;
//Color.g = colorG.g;
//Color.b = colorB.b;
return Color;
}
technique PhotoShow
{
pass P0
{
pixelShader = compile ps_2_0 PS_Main();
}
}
当我把Color.r = colorR.r;Color.g = colorG.g;Color.b = colorB.b;这几句加上的时候,程序报错,去掉的话就没有问题了
另外我用函数asint的时候也会出错,比方说int x = asint(3.0);程序报错
烦请各位高手指点
解决方案
float4x4 matViewProjection;
struct VS_INPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 Pos : POSITION0;
float2 Texcoord : TEXCOORD0;
float4 Position : TEXCOORD1;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Pos = mul( Input.Position, matViewProjection );
Output.Texcoord = Input.Texcoord;
Output.Position = Output.Pos;
return( Output );
}
struct VS_INPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 Pos : POSITION0;
float2 Texcoord : TEXCOORD0;
float4 Position : TEXCOORD1;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Pos = mul( Input.Position, matViewProjection );
Output.Texcoord = Input.Texcoord;
Output.Position = Output.Pos;
return( Output );
}
这样你的那个ps像8楼那样改应该差不多了 asint() 在ps4.0才支持,你的ps_2_0不支持
这个抱什么错? ps_4_0是Directx10里的,你要确定你装的是vista而且用的是directx10编的程序
你看看程序直接在“Effect.FromFile”的时候就出错了,在VS的输出窗口应该有信息打出来
我不确定C#是在那里输出出错信息的,在C++里是D3DXCreateEffectFromFile的最后一个参数,C#应该有类似的地方
晕了 我才看到,不好意思
struct VS_INPUT
{
vector position : POSITION;
float2 uvCoords : TEXCOORD;
};
{
vector position : POSITION;
float2 uvCoords : TEXCOORD;
};
默认是没有position 的,ps里面不支持POSITION; 除非你在VS里面自己传,可以保存在TEXCOORD1里面
struct VS_INPUT
{
float2 uvCoords : TEXCOORD0;
float4 position : TEXCOORD1;
};
{
float2 uvCoords : TEXCOORD0;
float4 position : TEXCOORD1;
};
出错并不是因为你是用了colorR,而是因为你使用了input.position。
如果你写了x=1,y=1,那么编译器在优化的时候就直接用x=1,y=1而不会去掉用input.position所以不会错,一旦你访问了input.position,编译器必须解析vector position : POSITION; 就解析不过了 Vertex Shader里才可以有 POSITION
用户评论