Jump to content
  • Advertisement
Sign in to follow this  
BlackJoker

Fill Texture resource with data from file

This topic is 1137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am started to deal with Direct3D 12 and want to understand how in 12 version I can load my texture from file to texture instance.

 

In DX 11 I used WIC loader solution from sharpdx to load textures from file.

For 2d textures I end my loading with DataBox array with one element, which contains start pointer and size of the texture. For texture cube dtabox aaray contains 6 elements correspondingly.

 

How to write that data to texture 2d in Direct3D 12?

I know that I can write to texture using that method:

texture.WriteToSubresource();

I suppose that corect code will:

texture.WriteToSubresource(0, null, databox.DataPointer, databox.RowPicth, dataBox.SlicePitch);

Am I correct with this?

Could anyone point me?

Edited by BlackJoker

Share this post


Link to post
Share on other sites
Advertisement

In DirectX12 things are a little bit different,

I'll use a simple texture (no arraysize and no mipmaps) to explain.

 

First you need to create a Buffer large enough to contain your image data, make sure this buffer is also using an Upload Heap

 

Next you create a Texture (with correct size/format) in Default heap.

 

Map your Buffer and copy Texture data to it.

 

Then you need to use CopyResourceRegion on a CommandList to perform the copy from upload heap to texture default heap, and also make sure that you have the right resource states (Texture needs to be in CopyDestination state before the copy, then you need to set it in GenericRead state again so it can be used as a shader view in your shaders pipeline)

TextureCopyLocation destLocation = new TextureCopyLocation(textureResource, 0);

TextureCopyLocation srcLocation = new TextureCopyLocation(uploadBuffer,
    new PlacedSubResourceFootprint()
    {
        Offset = 0,
        Footprint = new SubResourceFootprint()
        {
            Width = 1920,
            Depth = 1,
            Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
            Height = 1080,
            RowPitch = 1920 * 4
        }
});

commandList.ResourceBarrierTransition(textureResource, ResourceStates.GenericRead, ResourceStates.CopyDestination);
commandList.CopyTextureRegion(destLocation, 0, 0, 0, srcLocation, null);
commandList.ResourceBarrierTransition(textureResource, ResourceStates.CopyDestination, ResourceStates.GenericRead);





Share this post


Link to post
Share on other sites

Thanks for that, but I dont see here the code responsible for loading image data from file.

Lets assime that I already have DataBox with loaded info from texture file on my drive. How to fill my created texture with that data?

Share this post


Link to post
Share on other sites

To load image from file, you can use any of your favourite technique (it's not D3D dependent).

 

So WIC, System.Drawing or any of your own parser would do.

 

Once you get your decompressed image data in your ram, call:

 

IntPtr dataPtr = uploadBuffer.Map(0, null);?

 

And use one of the copy memory function (memcpy,CopyMemory, SharpDX.Utilities.CopyMemory), to copy to that pointer (It's actually somehow equivalent to MapSubResource in Direct3D11)

 

You then unmap with:

 

uploadBuffer.Unmap(0, null);

 

 

Share this post


Link to post
Share on other sites

And another one question regarding Texture cubes. How to correctly write to the buffer for 6 texture 2D?

For ex, I can create buffer of the size of 6 texture 2d, but how to write to each texture separately inside that buffer?

 

Update. OK, seems I understand how to do it. After loading Image in WIC solution, I already have DataPointer to the first image and size in bytes that contains all 6 textures. So, I suppose that with 6 textures everything must be the same as for one texture.

Edited by BlackJoker

Share this post


Link to post
Share on other sites

I did not try with cubemap yet, but it should be pretty much the same (it's just a TextureArray after all)

 

As far as I've seen, there's no pitch for depth slice, so I'm not certain if you can copy all in one go.

 

Alternatively, by setting the right subresouce in TextureCopyLocation:

 

TextureCopyLocation destLocation = new TextureCopyLocation(textureResource, cubeSliceIndex); ?

 

And setting the right offset location on your buffer source footprint

 

You can copy each slice separately, which is pretty easy also.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!