UpdateSubresource using ID3D11Texture2D

Started by
22 comments, last by unbird 5 years, 5 months ago

Hi,

I am trying to initialize SkyBox/Cubemap in Oculus app.  Oculus's sample shows how to initialize texture swap chain texture.  The difference in my use case is that I do not initialize from .dds bytes read from disk, I already have ID3D11Texture2D.  I see samples online that allow getting Texture bytes, but that will involve CPU copying of memory, I wonder if it can be avoided.  Here's roughly what I am doing:


		int numFaces = 6;
		for (int i = 0; i < numFaces; ++i)
		{
			ID3D11Texture2D *faceSrc = textures->handle;
			++textures;
			context->UpdateSubresource(tex, i, nullptr, (const void*)faceSrc, srcDesc.Width * 4, srcDesc.Width * srcDesc.Height * 4);
		}

However, that crashes with AV in Nvidia driver.  Any suggestions?  Thanks!

Advertisement

I think that the source of the access violation may be the "faceSrc" that you are passing to UpdateSubresource(...). I don't think that an ID3D11Texture2D* is valid for the "pSrcData" parameter. If you are trying to copy data from one texture to another, then CopyResource(...) might be a better option.

4 minutes ago, Aerodactyl55 said:

I think that the source of the access violation may be the "faceSrc" that you are passing to UpdateSubresource(...). I don't think that an ID3D11Texture2D* is valid for the "pSrcData" parameter. If you are trying to copy data from one texture to another, then CopyResource(...) might be a better option.

Yes, I am using CopyResource in other places.  However, in this case texture pointed by tex variable was acquired by specifying ArraySize of 6 when created by Oculus' swap chain.   I guess I'll keep digging to figure out how to get one of the 6 out of Oculus SDK.

Well in that case, i don't know if there is solution other than copy the texture data to a memory buffer and then copying the data in the buffer to the cube faces.

CopySubresourceRegion

If you're getting a crash in a D3D function, then make sure that you enable the D3D debug layer for your non-release builds. The debug layer will tell you when pass invalid parameters to API functions, or otherwise trigger invalid behavior.

You're casting the 4th argument from (ID3D11Texture2D*) to (const void*). This argument is supposed to point to normal CPU-visible memory containing linear texel data but you're pointing to an D3D interface instead. You can't access the texels via ID3D11Texture2D at all (the texels are well hidden somewhere in the driver managed memory), you have to use ID3D11DeviceCtx::Map or similar to get the texels back to CPU.

This won't do what you expected (and should crash as there's a high chance it'll access unmapped memory as the ID3D11Texture2D class is rather small).

Please note that the D3D debug layer will not, unfortunately, catch the reason why this doesn't work because this 4th argument's semantics are "some bytes" and if that memcpy hidden in UpdateSubresource doesn't crash reading those bytes, the D3D debug layer can't draw any conclusions about why it doesn't do what you want :) It's still advisable to have the debug layer ON, though!

22 hours ago, unbird said:

Thank you guys for all the responses.  CopySubresourceRegion is what I needed.  I was aware that CPU cannot access Texture's texels directly, but my hope was that maybe the API I used to call would do things needed internally.  I knew I could copy texels out using map, but trying to avoid that was the whole point of this post, still thanks for clarifying. 

Now I need to figure out how to control orientation of copied textures, because they seem to be rotated by -180.

Will look into enabling debug layer thingie :)

Edit: looks like I can't use debug layer because I don't create device, it comes from somewhere else.  But maybe it can be forced using swap context, will look into that.

42 minutes ago, VytsL said:

Now I need to figure out how to control orientation of copied textures, because they seem to be rotated by -180.

There's always an obstacle, isn't there ? :P

Copy won't help, it can't do such things unfortunately. You need to render now. Seems your target is already a render target so you can avoid an intermediate texture (and a copy).

8 hours ago, unbird said:

 There's always an obstacle, isn't there ? :P

Copy won't help, it can't do such things unfortunately. You need to render now. Seems your target is already a render target so you can avoid an intermediate texture (and a copy).

Yeah,  that's graphics :)  But I am so excited, did graphics years ago, so fun to get back into it, even briefly.

I still hope that I will find some flag or something that will allow specifying texture orientation in Oculus cubemap...  Also, I need to make sure my order of textures is right, I just figured out how to color them correctly in RGBA so that I can identify which is which, but still didn't get to test in HMD.  To add more fun, just figured out - Oculus explodes if I cube width != height, that'll be interesting to deal with ...

But suppose if I can't find a way, could you drop me sample of fastest way to rotate 2D texture?   Thanks in advance, you rock.

Edit: I wonder if D3D11_BOX can be used to flip texture?   My textures look upside down and flipped atm.

This topic is closed to new replies.

Advertisement