Programmatically creating directx 11 textures, pros and cons of the three different methods

≯℡__Kan透↙ 提交于 2019-11-30 02:05:57

The right answer depends on what you're going to use the texture for. Those three options are different ways of getting data from the CPU into the texture. If this is a rendertarget, you generally aren't providing initial data from the CPU, so you can ignore these: create the texture, and when you're ready render into it (perhaps Clear()ing it first).

So assuming you do have data in application memory that you want to get into the texture:

If this is just a static texture (by that I mean the texture is read from much more than it is written to), then you want a USAGE_DEFAULT or USAGE_IMMUTABLE texture. These are generally optimized for GPU read performance compared to USAGE_DYNAMIC. If you have the data handy when you create the texture, then option (1) is easiest, uses the least intermediate memory, and in DX11 the data transfer to the GPU can be done on a separate thread from your rendering thread. If you don't have the data at the time you create the texture, use UpdateSubresource() or option (3) to provide the data when you have it.

If it's a dynamic texture, meaning that you provide new contents from the CPU frequently (CPU-based video playback is the canonical case: data is provided by CPU once per frame, then read by the GPU once per frame) then you probably want to use USAGE_DYNAMIC and option (2). USAGE_DYNAMIC textures are optimized for streaming data from the CPU to the GPU rather than simply for GPU reads. The details (and performance implications) vary between hardware vendors, but usually you only want to use USAGE_DYNAMIC if you really are streaming data from CPU to GPU, rather than simply because it's a convenient way to load static data up-front.

Option (3) is more specialized, and can be used for either initial data load into a static texture (reuse the staging surface(s) for loading data for many textures) or for streaming data for relatively dynamic use. It gives you precise control over GPU/CPU synchronization and over the intermediate memory used for transfers. Usually you'd use a ring of staging buffers, and D3D11_MAP_FLAG_DO_NOT_WAIT to check whether each buffer is still in use by a previous CopyResource. I consider this an expert option -- if you're not careful you can hurt perf badly by preventing the CPU and GPU from running asynchronously.

Full disclosure: I work on the D3D driver at Nvidia, but these are my personal opinions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!