选择资源Choosing a resource

资源是 3D 管道所使用的数据集合。A resource is a collection of data that is used by the 3D pipeline. 对应用程序进行编程的第一步是创建资源并定义其行为。Creating resources and defining their behavior is the first step toward programming your application. 本指南介绍了选择应用程序所需资源的基本主题。This guide covers basic topics for choosing the resources required by your application.

确认需要资源的管道阶段Identify pipeline stages that need resources

第一步是选择使用资源的图形管道阶段。The first step is to choose the graphics pipeline stage (or stages) that will use a resource. 即,确认从资源中读取数据的每个阶段和将数据写入资源的每个阶段。That is, identify each stage that will read data from a resource as well as the stages that will write data out to a resource. 了解使用资源的管道阶段将确定被调用以将资源绑定到此阶段的 API。Knowing the pipeline stages where the resources will be used determines the APIs that will be called to bind the resource to the stage.

此表列出了可绑定到每个管道阶段的资源类型。This table lists the types of resources that can be bound to each pipeline stage. 包括资源是作为输入还是作为输出进行绑定。It includes whether the resource can be bound as an input or an output.

管道阶段Pipeline Stage 输入/输出In/Out 资源Resource 资源类型Resource Type
输入组装器Input Assembler In 顶点缓冲区Vertex Buffer BufferBuffer
输入组装器Input Assembler In 索引缓冲区Index Buffer BufferBuffer
着色器阶段Shader Stages In 着色器资源视图Shader-ResourceView 缓冲区,Texture1D,Texture2D,Texture3DBuffer, Texture1D, Texture2D, Texture3D
着色器阶段Shader Stages In 着色器常量缓冲区Shader-Constant Buffer BufferBuffer
流输出Stream Output Out BufferBuffer BufferBuffer
输出合并器Output Merger Out 呈现器目标视图Render-target View 缓冲区,Texture1D,Texture2D,Texture3DBuffer, Texture1D, Texture2D, Texture3D
输出合并器Output Merger Out 深度/模具视图Depth/Stencil View Texture1D,Texture2DTexture1D, Texture2D

 

确定将如何使用每个资源Identify how each resource will be used

选择你的应用程序要使用的管道阶段(和每个阶段需要的资源)后,下一步是确定如何使用每个资源,即资源能被 CPU 还是 GPU 访问。Once you have chosen the pipeline stages that your application will use (and therefore the resources that each stage will require), the next step is to determine how each resource will be used, that is, whether a resource can be accessed by the CPU or the GPU.

运行你的应用程序的硬件至少需要一个 CPU 和一个 GPU。The hardware that your application is running on will have a minimum of at least one CPU and one GPU. 若要获取使用情况值,请在以下选项中考虑哪种类型的处理器需要读取或写入资源。To pick a usage value, consider which type of processor needs to read or write to the resource from the following options.

资源使用情况Resource Usage 可如何更新Can be updated by 更新频率Frequency of Update
默认Default GPUGPU 不频繁infrequently
动态Dynamic CPUCPU 频繁frequently
过渡Staging GPUGPU 不适用n/a
不可变Immutable CPU(仅在创建资源时)CPU (only at resource creation time) 不适用n/a

 

默认用法应用于 CPU 预计不经常更新的资源(每帧少于一次)。Default usage should be used for a resource that is expected to be updated by the CPU infrequently (less than once per frame). 理想情况下,CPU 从不将其直接写入采用默认用法的资源,以避免引起可能的性能损失。Ideally, the CPU would never write directly to a resource with default usage so as to avoid potential performance penalties.

动态用法应用于 CPU 相对经常更新的资源(每帧一次或多次)。Dynamic usage should be used for a resource that the CPU updates relatively frequently (once or more per frame). 动态资源的典型方案是创建动态顶点和索引缓冲区,这些顶点和缓冲区将在运行时用从用户视角可见的每帧几何图形相关数据进行填充。A typical scenario for a dynamic resource would be to create dynamic vertex and index buffers that would be filled at runtime with data about the geometry visible from the point of view of the user for each frame. 这些缓冲区会用于仅呈现该帧对用户可见的几何图形。These buffers would be used to render only the geometry visible to the user for that frame.

临时用法应用于将数据复制到其他资源和从其他资源复制数据。Staging usage should be used to copy data to and from other resources. 典型方案为将采用默认用法的资源(CPU 不可访问)中的数据复制到采用临时用法的资源(CPU 可访问)。A typical scenario would be to copy data in a resource with default usage (which the CPU cannot access) to a resource with staging usage (which the CPU can access).

资源中的数据从不改变时,应使用不可变资源。Immutable resources should be used when the data in the resource will never change.

评估这一理念的另一种方法是了解应用程序是如何处理资源的。Another way of looking at the same idea is to think of what an application does with a resource.

应用程序如何使用资源How Application uses the Resource 资源使用情况Resource Usage
一旦加载从不更新Load once and never update 不可变或默认Immutable or Default
应用程序重复填充资源Application fills resource repeatedly 动态Dynamic
渲染到纹理Render to texture 默认Default
CPU 访问 GPU 数据CPU access of GPU data 过渡Staging

 

如果你不确定要选择哪个用法,请先使用默认用法,因为它应为最常见的情况。If you are unsure what usage to choose, start with the default usage as it is expected to be the most common case. 着色器常量缓冲区是一个应始终采用默认用法的资源类型。A Shader-Constant buffer is the one resource type that should always have default usage.

将资源绑定到管道阶段Binding resources to pipeline stages

只要满足创建资源时指定的限制条件,就可以同时将一个资源绑定到多个管道阶段。A resource can be bound to more than one pipeline stage at the same time as long as the restrictions specified when the resource was created are met. 指定的这些限制为用法标志、绑定标志或 cpu 访问标志。These restrictions are specified as usage flags, bind flags, or cpu access flags. 更具体地说,只要不可同时读取和写入资源的一部分,即可将资源作为输入和输出同时绑定。More specifically, a resource can be bound as an input and an output simultaneously as long as reading and writing part of a resource cannot happen at the same time.

绑定资源时,请考虑 GPU 和 CPU 如何访问资源。When binding a resource, think about how the GPU and the CPU will access the resource. 用于单一目的的资源(不使用多个用法、绑定和 cpu 访问标志)的性能可能更佳。Resources that are designed for a single purpose (do not use multiple usage, bind, and cpu access flags) will more than likely result in better performance.

例如,考虑多次将呈现器目标用作纹理的情况。For example, consider the case of a render target used as a texture multiple times. 使用两个资源可能更快:一个呈现器目标和一个用作着色器资源的纹理。It may be faster to have two resources: a render target and a texture used as a shader resource. 每个资源只使用一个绑定标志,用于指示“呈现器目标”或“着色器资源”。Each resource would use only one bind flag, indicating "render target" or "shader resource". 数据将从呈现器目标纹理复制到着色器纹理。The data would be copied from the render-target texture to the shader texture.

此示例中的此技术可能会通过将呈现器目标写入与着色器纹理读取隔离来提高性能。This technique in this example may improve performance by isolating the render-target write from the shader-texture read. 确保实现此目的的唯一方法是实现这两种方法并度量特定应用程序的性能差异。The only way to be sure is to implement both approaches and measure the performance difference in your particular application.

相关主题Related topics

资源Resources