Search code examples
texturesdirectx-12

DX12, How to implement a standard texture class?


I'm working on the texture class of our engine and I came across one problem.

Our API supports different actions like updating some texels of the texture and reading data from the texture.

This texture could be bound to the pipeline as an UAV or maybe as a SRV. It could also be created as a RTV or a DSV.

How should I approach the creation and updates of textures? Should I just create all textures as UPLOAD heaps? This will be the most standard solution as I will easily read and write from/to the data, but it also means less GPU bandwidth.

I could also detect if I'm creating a texture from file or a procedural texture, in the first case I will upload the texture to a DEFAULT heap.

What do you think?

Thanks!


Solution

  • You won't be able to create texture in the upload heap, you would face an eternal cycle of validation error while trying to solve them. It is because texture are not using a linear representation in memory. But at least for you, the copy and update operations are the simplest of your concerns here.

    When you want to perform transfer between the CPU and the GPU, you need to create a buffer in the upload or readback heaps, use GetCopyableFootprints to know how to store/read the image in it and call CopyTextureRegion to perform the actual copy. This is a few pages of codes if you want to support partial updates and reads, but this operations are straightforward.

    After that, many consideration can influence your texture class, how you want to abstract the views in descriptor heaps, do you want to support reserved textures with partial residency, do you want to support memory aliasing, how do you manage the lifetime of textures in use by the gpu while updating/destroying them, streaming, etc. There is no perfect and solve it all solutions and it all depends on your needs.