Question

I'm using DirectX 11 targeting Shader Model 5 (well actually using SharpDX for directx 11.2 build) and i'm at loss with what is wrong with this simple shader i'm writing.

The pixel shader is applied on a flat high poly plane (so there are plenty of vertices to displace), the texture is sampled without issues (the pixel shader displays it fine), however the displacement in the vertex shader just doesn't work.

It's not an issue with the displacement itsels, replacing the += height.SampleLevel with += 0.5 shows all vertices displaced, it's not an issue with the sampling either since the very same code works in the pixel shader. And as far as i understand it's not an API usage issue since i hear SampleLevel (unlike sample) is perfectly usable in the VS (since you provide the LOD level).

Using a noise function to displace instead of a texture also works just fine, which leads me to think the issue is with the texture sampling and only Inside the VS, which is odd since i'm using a function that is supposedly VS compatible?

I've been trying random things for the past hour and i'm really clueless as to where to look, i also find nearly no info about displacement mapping in hlsl and even less so in DX11 + to use as reference.

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;
}

For reference in case it matters:

Texture description :

 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),
            };

Sampler description :

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
            });
Was it helpful?

Solution

So the answer was that Ronan wasn't setting the shader resource view to the vertex shader so it can access the textures and set the calculated values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top