Jump to content
  • Advertisement
Sign in to follow this  
Doggolainen

DX11 Sampler not working outside pixelshader

Recommended Posts

Hello, 

I am, like many others before me, making a displacement map tesselator. I want render some terrain using a quad, a texture containing heightdata and the geometryshader/tesselator.

So far, Ive managed the utilize the texture on the pixelshader (I return different colors depending on the height). I have also managed to tesselate my surface, i.e. subdivided my quad into lots of triangles .

 

What doesnt work however is the sampling step on the domain shader. I want to offset the vertices using the heightmap.

I tried calling the same function "textureMap.Sample(textureSampler, texcoord)" as on the pixelshader but got compiling errors. Instead I am now using the "SampleLevel" function to use the 0 mipmap version of the input texture.

But yeah non of this seem to be working. I wont get anything except [0, 0, 0, 0] from my sampler.

Below is some code: The working pixelshader, the broken domain shader where I want to sample, and the instanciations of the samplerstates on the CPU side.

Been stuck on this for a while! Any help would be much appreciated!
 

 

Texture2D textureMap: register(t0);
SamplerState textureSampler : register(s0);



//Pixel shader

float4 PS(PS_IN input) : SV_TARGET
{
    float4 textureColor = textureMap.Sample(textureSampler, input.texcoord);

    return textureColor;

}

GS_IN DS(HS_CONSTANT_DATA input, float3 uvwCoord : SV_DomainLocation, const OutputPatch<DS_IN, 3> patch)
{
    GS_IN output;

    float2 texcoord = uvwCoord.x * patch[0].texcoord.xy + uvwCoord.y * patch[1].texcoord.xy + uvwCoord.z *                    patch[2].texcoord.xy;
    float4 textureColor = textureMap.SampleLevel(textureSampler, texcoord.xy, 0);

     //fill  and return output.... 

}
            //Sampler
            SharpDX.Direct3D11.SamplerStateDescription samplerDescription;

            samplerDescription = SharpDX.Direct3D11.SamplerStateDescription.Default();
            samplerDescription.Filter = SharpDX.Direct3D11.Filter.MinMagMipLinear;
            samplerDescription.AddressU = SharpDX.Direct3D11.TextureAddressMode.Wrap;
            samplerDescription.AddressV = SharpDX.Direct3D11.TextureAddressMode.Wrap;
            this.samplerStateTextures = new SharpDX.Direct3D11.SamplerState(d3dDevice, samplerDescription);

            d3dDeviceContext.PixelShader.SetSampler(0, samplerStateTextures);
            d3dDeviceContext.VertexShader.SetSampler(0, samplerStateTextures);
            d3dDeviceContext.HullShader.SetSampler(0, samplerStateTextures);

            d3dDeviceContext.DomainShader.SetSampler(0, samplerStateTextures);

            d3dDeviceContext.GeometryShader.SetSampler(0, samplerStateTextures);

 

Share this post


Link to post
Share on other sites
Advertisement

It looks like you're binding your sampler state to the Domain Shader stage, but I don't see any code for binding the texture's shader resource view (SRV) to the DS stage. Are you doing that somewhere? If not, that would explain why you're getting all black as the result.

BTW, the reason that you can't use Sample() in a non-pixel shader is because that function automatically computes the mip level for your. The way it does is by looking at the neighboring pixels and figures out how much the UV coordinates change from one pixel to the next (effectively computing the partial derivatives of U and V with respect to screen-space X and Y), and then uses that value to compute the appropriate mip level as well as the amount of anisotropic filtering (if enabled). This doesn't work in other shader types because there's no grid of pixels, so you have to manually specify the mip level or provide the partial derivatives.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!