DirectX11中的纹理流,不可变与动态 [英] Texture streaming in DirectX11, Immutable vs Dynamic

查看:133
本文介绍了DirectX11中的纹理流,不可变与动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们经常遇到需要将纹理流传输到图形卡的情况(在游戏中是:地形,在我的案例中是来自不同输入源(如相机/捕获卡/视频)的图像)

We often have the case where we need to stream textures to the graphics card (in game case: terrains, in my case image from different input sources like cameras/capture cards/videos)

当然,在相机的情况下,我是在单独的线程中接收数据的,但是仍然需要将该数据上传到GPU进行显示。

Of course in camera case, I receive my data in a separate thread, but still need to upload that data to the GPU for display.

I知道2个模型。


  • 使用动态资源:
    您创建一个具有相同大小的动态纹理并设置为输入图像的格式,当您收到新图像时,设置一个标志来告知您需要上载,然后在设备上下文中使用map上载纹理数据(当然,最终会使用双缓冲区)。

  • Use a dynamic resource: You create a dynamic texture which has the same size and format as your input image, when you receive a new image you set a flag that tells you need upload, and then use map in the device context to upload the texture data (with eventual double buffer of course).

优点是您只有一个内存位置,因此随着时间的推移您不会出现内存碎片。

Advantage is you have a single memory location, hence you don't have memory fragmentation over time.

缺点是您需要在即时上下文中上传,因此您的上传必须在渲染循环中。

Drawback is you need to upload in immediate context, so your upload had to be in your render loop.

使用不可变并加载/丢弃
如果您在图像接收线程中上传,则通过创建新资源,推送数据并丢弃旧资源。

Use immutable and load/discard In that case you upload in the image receiving thread, by creating a new resource, push the data and discard the old resource.

优点是您应该有一个停顿的免费上传(没有需要即时上下文,您仍然可以在上载纹理的同时运行命令列表),一旦可用(可以交换SRV)就可以使用简单的触发器来使用资源。

Advantage is you should have a stall free upload (no need for immediate context, you can still run your command list while texture is uploading), resource can be used with a simple trigger once available (to swap SRV).

缺点是您可以随着时间的推移来分散内存(通过以恒定的方式分配和释放资源(例如,标准相机的30 fps))。

Drawback is you can fragment memory over time (by allocating and freeing resources in a constant manner (30 fps for a standard camera as example).

此外,您还必须处理节流问题自己(但是那部分没什么大不了的。)

Also you have to deal with throttling yourself (but that part is not a big deal).

那么我在那些技术中是否缺少某些东西,或者

So is there something I missed in those techniques, or is there an even better way to handle this?

推荐答案

这是更新纹理D3D11的两种主要方法。

These are the two main methods of updating textures D3D11.

但是,第一种方法不会导致与第二种情况相同的内存使用模式的假设取决于驱动程序,并且可能不正确。如果要覆盖整个图像(听起来像您在做的事情),则可以使用 D3D11_MAP_WRITE_DISCARD ,这意味着缓冲区的当前内容未定义。但是,仅从CPU的角度来看这是正确的。如果它们可能在挂起的绘制操作中使用,则将它们保留给GPU。在这种情况下,大多数(也许是所有?)驱动程序实际上都会为映射纹理的写入位置分配新的存储,否则命令缓冲区处理将需要暂停。如果您不使用丢弃标志,也是如此。相反,当在命令缓冲区中处理map命令时,资源的缓冲区将更新为 D3D11_MAPPED_SUBRESOURCE 中的Map返回的值。

However, the assumption that the first method will not result in memory usage patterns identical to the second case is dependent on the driver, and likely is not true. You would use D3D11_MAP_WRITE_DISCARD if you are overwriting the whole image (which it sounds like what you are doing), meaning that the current contents of the buffer become undefined. However, this is only true from the CPU's point-of-view. They are retained for the GPU, if they are potentially used in a pending draw operation. Most (maybe all?) drivers will actually allocate new storage for the write location of the mapped texture in this case, otherwise command buffer processing would need to stall. The same holds if you do not use the discard flag. Instead, when the map command is processed in the command buffer, the resource's buffer is updated to the value returned from Map in D3D11_MAPPED_SUBRESOURCE.

此外,必须在即时上下文中更新动态纹理也不是正确的。仅当您在延迟的上下文中更新它们时,才必须使用 D3D11_MAP_DISCARD 标志。这意味着如果您要覆盖整个纹理,则可以在辅助线程上更新纹理。

Also, it is not true that you must update dynamic textures in the immediate context. Only that if you update them in a deferred context, you must use the D3D11_MAP_DISCARD flag. This means you could update the texture on a worker thread, if you are overwriting the entire texture.

最重要的是,因为PC上的CPU / GPU系统如果您使用的不是统一的内存系统,则更新来自CPU的GPU资源时将出现同步问题。

The bottom line is that, since the CPU/GPU system on a PC is not a unified memory system, there will be synchronization issues updating GPU resources coming from the CPU.

这篇关于DirectX11中的纹理流,不可变与动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆