選擇資源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-ResourceViewShader-ResourceView 緩衝區, Texture1D, Texture2D, Texture3DBuffer, Texture1D, Texture2D, Texture3D
著色器階段Shader Stages 位於In Shader-Constant BufferShader-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/an/a
固定Immutable CPU (只能在資源建立的時間)CPU (only at resource creation time) n/an/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