share
Stack OverflowWhat is the result of sample with an uninitialized texture in DirectX?
[+1] [2] user1655141
[2016-11-09 05:03:11]
[ directx directx-11 ]
[ https://stackoverflow.com/questions/40500421/what-is-the-result-of-sample-with-an-uninitialized-texture-in-directx ]

The OpenGL has its description [1] about this, but how about DirectX?

In my guess, the sample result is float(0, 0, 0, 0), or arise a crash by the driver. Whatever, it just my guess, or partial case if tested it myself only. I want to make it clear.

The uninitialized texture means, did not pass any data with D3DDevice::CreateTexture2D() [2], and also, did not map [3] nor update resource [4].

I want to take the description about DirectX 11 version if possible.

[0] [2016-11-13 06:13:50] Justin William Stanley Bryant

Yes you get 0 for all values its the same if you sample from a null texture.

I cant remember where it was that I read it but I know its in the MSDN doc, I also happen to do this from time to time because I allocate most of the textures\buffers\views at the start and fill as I go and if I sample from null or uninitialized then it just read 0.


He wasn't asking about an unbound texture, he was asking about a texture whose contents have not been initialized. - MuertoExcobito
It still returns 0's does it not? I even said that "if I sample from null or uninitialized then it just reads 0." I just tested it and you get float4(0,0,0,0) - Justin William Stanley Bryant
Your driver returns float4(0,0,0,0) with your sample. Doesn't mean that everyone's will, or even yours will under different circumstances. See my answer. - MuertoExcobito
This is interesting, only because I have been doing things like this over two rewrites and 3 different GPU's 2 from AMD and one from Nvida and the same thing happens. Like reading from a SRV before it gets filled or is that a special case? Either way thanks, like I said interesting! - Justin William Stanley Bryant
There is no such thing as an uninitialized SRV... CreateShaderResourceView always fully initializes it. If you mean reading from an SRV that points to an uninitialized resource, yes, it applies to all resource types, there is no guarantee what you'll get back from an uninitialized resource read. Frequently it's 0, but, I've seen it before where it isn't. It's bad practice to do things this way - just unbind the resource if you want zeros. - MuertoExcobito
Sorry "SRV that points to an uninitialized resource" is just what I was saying, also thanks for that little tid bit of bad practice I'm no pro so I miss things.... Goes off to look at my code base and make some changes - Justin William Stanley Bryant
1
[0] [2016-11-14 08:25:35] MuertoExcobito [ACCEPTED]

From the Create2DTexture function (that you linked):

If you don't pass anything to pInitialData, the initial content of the memory for the resource is undefined. In this case, you need to write the resource content some other way before the resource is read.

Undefined can mean anything, the driver will determine the exact behavior. It's unlikely that it will crash, but as with undefined behavior, anything is possible. A sample from this texture is certainly not guaranteed to be float4(0,0,0,0) as it is with an unbound texture.

It's analogous to accessing uninitialized system memory. The contents might be filled memory written from previous operations that had allocated the same memory (depending on the allocator's behavior). I would suggest if you want consistent behavior, either use an unbound texture instead, or, initialize the contents.


2