4

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
            });
Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
  • I;m assuming you are doing 2 passes... On your second pass are you rendering to the texture again? This sounds like an Output merger issue. – Miguel Jan 05 '14 at 23:39
  • Nope not doing 2 passes, only 1 shader & 1 pass nothing else. – Ronan Thibaudau Jan 06 '14 at 00:01
  • Also i'm never redendering to the texture to begin with, it's a static texture loaded from file, it never changes – Ronan Thibaudau Jan 06 '14 at 00:02
  • I see. I'm not really experienced with sharpdx, but something that you can try is implementing a GS or CS to do this calculations, maybe you are dealing with a driver bug here... – Miguel Jan 06 '14 at 00:22
  • Are you giving the pixel shader access to the textures? Have you done some hlsl debugging wth PIX or visual studio? – Miguel Jan 06 '14 at 00:25
  • SharpDX shouldn't make any diference, it's just a managed wrapper around DirectX. Also i really don't see what a CS would do there, nor a GS for that matter? Driver bug sounds highly unlikely too. – Ronan Thibaudau Jan 06 '14 at 00:26
  • Err actually i think you've got something about giving access to the texture, i didn't think of it (since it's declared in the same effect file) but yea i'm missing a vertexshader.setshaderresource like i have for the pixelshader i assume. Going to try this and see! – Ronan Thibaudau Jan 06 '14 at 00:27
  • Yup was just that, was missing SetShaderRessource for the VS, thanks! :) – Ronan Thibaudau Jan 06 '14 at 00:28
  • do you want to post the answer or do you want me to post it? – Miguel Jan 06 '14 at 01:45
  • Go ahead you found it. – Ronan Thibaudau Jan 06 '14 at 02:25

1 Answers1

2

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.

Miguel
  • 872
  • 1
  • 12
  • 26