XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Testing for null in hlsl

Last post 2/11/2009 8:59 PM by Taicoon. 4 replies.
  • 2/11/2009 2:24 PM

    Testing for null in hlsl

    While I can assign null to a texture in my effect, I fail to see how I test to see if it IS null and then render differently when the texture is and when it is not assigned.

    if(xTexture==null) and if(xTexture==0) both fail. First with unknown variable (null) and second with incomaptble types.
    Is the another way to do this or will I have to declare a boolean which I set and clear to flag the validity of the texture variable?
  • 2/11/2009 4:24 PM In reply to
    • (2342)
    • premium membership MVP
    • Posts 1,226

    Re: Testing for null in hlsl

    There is no way to do this.  With HLSL you can still sample from a sampler even if no texture is bound to it...with most drives you'll just get pure black when you do this.

    If you want different behavior, just set a boolean flag or something similar when you're setting up the Effect.
    Matt Pettineo | DirectX/XNA MVP


    Ride into The Danger Zone | PIX With XNA Tutorial
  • 2/11/2009 5:29 PM In reply to

    Re: Testing for null in hlsl

    Note that sampling from an unbound texture gives inconsistent results across different graphics cards. NVidia and ATI disagree about whether this should return zeros or ones, so you will get different colors depending what card you run on.

    As MJP says, the best solution is to pass in a flag indicating whether to use the texture, or perhaps just use an entirely different shader for untextured vs. textured objects. Or you could just put textures on everything in your game! (not actually a crazy solution: many games end up with 99% of their objects textured, in which case it may be more efficient to just slap a 1x1 white texture on the remaining 1% of their graphics, to avoid the cost of having to support untextured objects in their engine).

    See the Custom Model Effect or Non Photorealistic samples (both on this site) for examples of shaders that work both with or without a texture, via a TextureEnabled parameter. Or see BasicEffect (the HLSL source is available in the utilities section of this site) for an example of switching to an entirely different shader to handle this.
    XNA Framework Developer - blog - homepage
  • 2/11/2009 8:39 PM In reply to

    Re: Testing for null in hlsl

    The OpenGL specification defines unbound textures as sampling white, so that's probably why NVIDIA does it that way.

    What I do is just keep a small, white texture around, and bind it to texture units that are not used. (Actually, for emissive units, I bind black; for normal maps, I bind light blue; etc).

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 2/11/2009 8:59 PM In reply to

    Re: Testing for null in hlsl

    Thanks for the info.
    As you all state, it is not really a problem to use a flag og dummy texture. I just needed to know if I was missing the obvious, and correct, solution :-)
Page 1 of 1 (5 items) Previous Next