Search code examples
c#xnaxna-4.0hlsllighting

Setting TextureAddressMode with HLSL


I've been implementing a lighting system in XNA using shaders I didn't write myself, and I've run into an issue: the system works perfectly if the dimensions of the game are powers of two (right now it's running at 1024x512), but will otherwise break down at the line GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, Vertices, 0, 2);, throwing an exception which states that "XNA Framework Reach profile requires TextureAddressMode to be Clamp when using texture sizes that are not powers of two."

In a search of the internet, the most common solution is to put the line GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp directly above the line above. I've tried this, and it has had no effect. In fact, during my attempts to tease out a solution, it seemed to be that GraphicsDevice.SamplerStates[0] was null before I set it with that line, leading me to believe that this problem could also perhaps be solved by selecting that as the active sampler state--although I'm also not sure how to do that.

Otherwise, based on other information I've found, I believe that I need to set the TextureAddressMode from within the shader. I was directed to this page for information on how to do so, but I confess to still being baffled--I've never used HLSL before, and I'm unsure of how to use the information on that page.

If there's any more information I need to provide, I'd be more than happy to do so, and I first asked a question which led me towards my present one here.


Solution

  • In your HLSL look for the line that declares the sampler that the pixel shader is using.

    You can set the address mode to clamp in this line.

    SamplerState somethingLikeThis {
        Filter = MIN_MAG_MIP_LINEAR;
        AddressU = Clamp;
        AddressV = Clamp;
    };