以编程方式创建DirectX 11纹理,三种不同方法的优缺点 [英] Programmatically creating directx 11 textures, pros and cons of the three different methods

查看:134
本文介绍了以编程方式创建DirectX 11纹理,三种不同方法的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

msdn文档解释了在Directx 11中有多种方法可以通过编程方式填充DirectX 11纹理:

The msdn documentation explains that in directx 11 there are multiple ways to fill a directx 11 texture programmatically:

(1)使用默认用法创建纹理并使用内存中的数据对其进行初始化

(1) Create the texture with default usage texture and initialize it with data from memory

(2)创建具有动态用法的纹理,使用DeviceContext Map获取指向纹理内存的指针,对其进行写入,然后使用Unmap指示已完成(此时我猜它已被复制)

(2) Create the texture with dynamic usage, use DeviceContext Map to get a pointer to texture memory, write to it, then use Unmap to indicate you are done (at which point I guess it is copied to the gpu)

(3)创建具有临时用途的纹理,并遵循与动态纹理相同的步骤,但是在此之后调用ID3D11DeviceContext.CopyResource来使用它暂存纹理以依次填充(非不变的)默认或动态纹理。

(3) Create the texture with staging usage and follow same steps as for dynamic texture, but follow that with a call to ID3D11DeviceContext.CopyResource to use this staging texture to in turn fill a (non immutable) default or dynamic texture.

但是,文档并没有说明每种方法的优缺点,我是对DirectX来说还是很新的东西,所以对我来说一点都不清楚。

However the documentation doesn't explain pros and cons of each method at all, and I am still quite new to directx, so it is not at all clear to me.

在DirectX 11中以编程方式创建纹理的每种方法的优缺点是什么?

What are the pros and cons of each of these ways of creating a texture programmatically in directx 11?

旁注:我已经读到,在暂存纹理的上下文中,不缓冲从gpu读回 ,因此您必须自己进行双缓冲。但是我不知道这是否正确,是否适用于使用暂存纹理的书写(甚至实际上是什么意思)。

Side note: I have read that in the context of staging textures, reading back from the gpu isn't buffered so you have to do your own double buffering. But I don't know whether this was accurate and whether it applies to writing using staging textures (or even really what it means).

第二点说明: Map方法文档说,它获得了指向子资源中数据的指针,并拒绝GPU访问该子资源。当GPU要访问其基础数据已被Map调用的纹理时,该怎么做?失速了吗(我问,因为这听起来像是我所询问的利弊的一部分)

Second side note: The Map method documentation says it gets a pointer to data in a subresource and denies the GPU access to that subresource. When the GPU wants to access a texture whose underlying data has been called by Map, what does it do? Stall? (I ask because this sounds like part of the pros and cons I inquired about)

推荐答案

正确的答案取决于您的重新使用纹理。这三个选项是将数据从CPU放入纹理的不同方式。如果这是一个渲染目标,则通常不提供来自CPU的初始数据,因此您可以忽略以下内容:创建纹理,以及准备渲染到其中(也许先进行Clear()处理)。

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:

如果这只是一个静态纹理(我的意思是读取的纹理远比写入的要多),那么您需要USAGE_DEFAULT或USAGE_IMMUTABLE纹理。与USAGE_DYNAMIC相比,这些通常针对GPU读取性能进行了优化。如果创建纹理时方便使用数据,则选项(1)最简单,使用的中间内存最少,在DX11中,可以在与渲染线程不同的线程上完成向GPU的数据传输。如果创建纹理时没有数据,请使用UpdateSubresource()或选项(3)来提供数据。

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.

如果它是动态纹理,这意味着您经常从CPU提供新内容(基于CPU的视频回放是典型的情况:数据每帧由CPU提供一次,然后每帧由GPU读取一次),那么您可能想使用USAGE_DYNAMIC和选项(2)。 USAGE_DYNAMIC纹理经过优化,可将数据从CPU传输到GPU,而不是简单地用于GPU读取。细节(和性能影响)因硬件供应商而异,但是通常您只想使用USAGE_DYNAMIC(如果您确实是将数据从CPU传输到GPU),而不仅仅是因为它是一种方便的方式来预先加载静态数据。

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.

选项(3)更专业,可用于将初始数据加载到静态纹理中(重新使用临时表面来加载许多纹理的数据)或流数据以便相对动态地使用。它使您可以精确控制GPU / CPU同步以及用于传输的中间存储器。通常,您将使用一组暂存缓冲区,然后使用D3D11_MAP_FLAG_DO_NOT_WAIT来检查以前的CopyResource是否仍在使用每个缓冲区。我认为这是一个专家选择-如果您不小心,可能会阻止CPU和GPU异步运行,从而严重损害性能。

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.

完全公开:我正在研究是Nvidia的D3D驱动程序,但这是我个人的看法。

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

这篇关于以编程方式创建DirectX 11纹理,三种不同方法的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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