qrli

Is there any way to tell if a sampler has a texture attached in shaders The default behavior is getting black when no texture attached but I want it to be some other color.


Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Alexey Barkovoy

No, there is not such functionality available in shader.

If you want to use other color, just attach 1x1 sized texture in your code when no texture is needed. I think such things were actually used for emulating "default" black behaviour in some older HW.





Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Wessam Bahnassi

Alexey Barkovoy wrote:
No, there is not such functionality available in shader.

Indeed. Probably a clunky solution would be to intercept ID3DXEffect's texture changes when setting the shader on the device by using a state manager, or by simply GetTexture() on every slot to see what is being bound there... Yes ugly :|

Alexey Barkovoy wrote:
If you want to use other color, just attach 1x1 sized texture in your code when no texture is needed. I think such things were actually used for emulating "default" black behaviour in some older HW.

I'd use shader constants for that... You'll just introduce additional latency and overhead that way...






Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Wessam Bahnassi

qrli wrote:
The default behavior is getting black when no texture attached but I want it to be some other color.

No it's not the default behavior. Actually in my experience it's almost always white, but that's just relying on undocumented behavior. If you want to get a guarenteed black color then use shader constants or at least set a proper texture that contains your color if your design is too restricted.




Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

qrli

Seems ugly walkaround is the only way to go. Is there a chance for this ability to be supported in future e.g. in DX10.




Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Wessam Bahnassi

If you mean accessing sampler information from the effect, then that's a reasonable suggestion. You can send your feature request to directx@microsoft.com.




Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Alexey Barkovoy

 Wessam Bahnassi wrote:
 qrli wrote:
The default behavior is getting black when no texture attached but I want it to be some other color.


No it's not the default behavior. Actually in my experience it's almost always white, but that's just relying on undocumented behavior. If you want to get a guarenteed black color then use shader constants or at least set a proper texture that contains your color if your design is too restricted.

Actually IT IS.
DX10 debug runtime even emits warning about that, saying something like "You have not bound texture to sampler, this is unusial, but still has defined behaviour". BUT.... in real life drivers are sometimes buggy and don't follow spec 100%, so...





Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Wessam Bahnassi

I thought we were talking D3D9 here. Do you have a link to either the DDK docs or the SDK docs that says sampling an null texture must result in black




Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Alexey Barkovoy

It's defined for at least DX9 & DX10 (not quite sure about previous versions of DX).

From "texldl - ps" doc:

If a shader program samples from a sampler that does not have a texture set, then 0001 is obtained in the destination register.





Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Wessam Bahnassi

That's true only for the texldl instruction which is SM3.0 only and is not the one used by sampling a texture using tex2D. For the texld instruction, it is undefined for all shader models (including SM3.0).

Relying on this behavior is simply asking for trouble...




Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Alexey Barkovoy

Wessam, let me ask you: Why you state that it's undefined

The fact that it's not fully described in SDK doc - doesn't mean this (it only means that documentation is bugged). So you are saying that tex2D has undefined behaviour, but if you look in futher in texld doc you can read this:

If the source texture contains fewer than four components, defaults are placed in the missing components. Defaults depend on the texture format as shown in the following table:

Texture Format Default Values
D3DFMT_R5G6B5, D3DFMT_R8G8B8, D3DFMT_L8, D3DFMT_L16, D3DFMT_R3G3B2, D3DFMT_CxV8U8, D3DFMT_L6V5U5 A = 1.0
D3DFMT_V8U8, D3DFMT_V16U16, D3DFMT_G16R16, D3DFMT_G16R16F, D3DFMT_G32R32F B = A = 1.0
D3DFMT_A8 R = G = B = 0.0
D3DFMT_R16F, D3DFMT_R32F G = B = A = 1.0
All depth/stencil formats R = B = 0.0, A = 1.0

This snippet confirms that default values are (0, 0, 0, 1). If you still believe what behaviour it's undefined - please download Direct3D7 DDK (it should be freely available) and look at refrast implementation.





Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Wessam Bahnassi

That table shows values to expect when you have a texture bound to the device that is of a certain format. If you have no texture bound at all, then what format is going to be chosen from the table above

It is true that RefRast's behavior is to revert to pass a black color for unbound textures. But this is not enforced on driver writers. Drivers are supposed to follow the DDK guidelines first. Sometimes a point might not be very clear, so they revert to the SDK docs to see how it's supposed to work out from the application POV.

Now for this specific "feature", if D3D requires drivers to output black always for unbound texture samplers, then we would see all nvidia drivers failing WHQL tests because all of them give white actually. The whole point of leaving it like this is because D3D apps are not supposed to get into such a situation in the first place.

You want compatibility issues in your game Do it, it's a very good way :)




Re: Game Technologies: Graphics Is there any way to tell if a sampler has a texture attached in shaders?

Frank_Booth

I agree that assuming stuff like this is a very, very bad idea, and should never be done under any circumstances.

However... I was thinking about this very thing just the other day. It would be VERY useful if sampling from a sampler with a NULL texture was guaranteed to return white (or better yet, some value you could set in a register somewhere.) It would mean you didn't have to have different shaders for textured/non-textured objects.

Sure, you could work around this by just setting a small white texture instead. But if the driver is already checking for NULL, and returning something, maybe that'd be much quicker than a texture lookup.