2014-01-05 67 views
4

我正在使用針對Shader Model 5的DirectX 11(實際上使用SharpDX for directx 11.2版本),並且我對這個簡單着色器寫錯了有什麼問題。在頂點着色器中抽樣紋理?

像素着色器應用在平坦的高多邊形平面上(因此有很多頂點要移位),紋理採樣時沒有問題(像素着色器顯示正常),但是頂點着色器中的位移只是不會沒有工作。

這不是一個與位移itsels的問題,用+ = height.SampleLevel替換+ = 0.5顯示所有頂點移位,這不是取樣的問題,因爲在像素着色器中工作的代碼相同。據我所知,這不是一個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 
      }); 
+0

我; m假設你正在做2遍......在你的第二遍是你再次渲染紋理?這聽起來像一個輸出合併問題。 – Miguel

+0

不要做2遍,只有1個着色器&1沒有其他的通過。 –

+0

另外我從來沒有重新初始化紋理,它是從文件加載的靜態紋理,它從不改變我看到的 –

回答

2

所以答案是羅南沒有設置着色器資源視圖到頂點着色器,以便它可以訪問紋理並設置計算值。

相關問題