我正在使用DirectX 11定位Shader Model 5(实际上使用SharpDX进行directx 11.2版本),我正在编写这个简单着色器的错误.
像素着色器应用于平坦的高多边形平面(因此有大量顶点可以移位),纹理被采样而没有问题(像素着色器显示它很好),但是顶点着色器中的位移不起作用.
这不是位移itsels的问题,用= 0.5替换= height.SampleLevel显示所有顶点位移,这不是采样的问题,因为相同的代码在像素着色器中工作.据我所知,这不是API使用问题,因为我听说SampleLevel(与示例不同)在VS中完全可用(因为您提供LOD级别).
使用噪声函数代替纹理也可以正常工作,这让我觉得问题在于纹理采样和仅在VS内部,这是奇怪的,因为我使用的是一个据称与VS兼容的函数?
我一直在尝试过去一小时的随机事情,我真的很无知在哪里看,我也发现几乎没有关于hlsl中的位移映射的信息,甚至更少在DX11中用作参考.
struct VS_IN
{
float4 pos : POSITION;
float2 tex : TEXCOORD;
};
struct PS_IN
{
float4 pos : SV_POSITION;
float2 tex : TEXCOORD;
};
float4x4 worldViewProj;
Texture2D<float4> diffuse: register(t0);
Texture2D<float4> height: register(t1);
Texture2D<float4> lightmap: register(t2);
SamplerState pictureSampler;
PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN) 0;
input.pos.z += height.SampleLevel(pictureSampler, input.tex, 0).r;
output.pos = mul(input.pos, worldViewProj);
output.tex = input.tex;
return output;
}
float4 PS( PS_IN input ) : SV_Target
{
return height.SampleLevel(pictureSampler, input.tex, 0).rrrr;
}
如有重要,请参考:
纹理描述:
new SharpDX.Direct3D11.Texture2DDescription()
{
Width = bitmapSource.Size.Width,
Height = bitmapSource.Size.Height,
ArraySize = 1,
BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource ,
Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
MipLevels = 1,
OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
};
采样器说明:
var sampler = new SamplerState(device, new SamplerStateDescription()
{
Filter = Filter.MinMagMipLinear,
AddressU = TextureAddressMode.Clamp,
AddressV = TextureAddressMode.Clamp,
AddressW = TextureAddressMode.Clamp,
BorderColor = Color.Pink,
ComparisonFunction = Comparison.Never,
MaximumAnisotropy = 16,
MipLodBias = 0,
MinimumLod = 0,
MaximumLod = 16
});
最佳答案 所以答案是Ronan没有将着色器资源视图设置为顶点着色器,因此它可以访问纹理并设置计算值.