> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CreateShaderResourceView

> 建立用來存取資源中資料的著色器資源檢視。(ID3D12Device.CreateShaderResourceView)

建立用來存取資源中資料的著色器資源檢視。

## 語法

```cpp theme={null}
void CreateShaderResourceView(
    ID3D12Resource  pResource,
    const D3D12_SHADER_RESOURCE_VIEW_DESC  pDesc,
    D3D12_CPU_DESCRIPTOR_HANDLE DestDescriptor
)
```

### 參數

*pResource \[in, optional]*\
類型：ID3D12Resource \*

指向代表著色器資源之 [ID3D12Resource](/zh-TW/reference/graphics/d3d12_xs/interfaces/ID3D12Resource/id3d12resource_xs) 物件的指標。

必須至少提供 *pResource* 或 *pDesc* 其中之一。Null *pResource* 用於初始化 Null 描述元，以保證類似 D3D11 的 Null 繫結行為 (讀取 0，捨棄寫入)，但必須具有有效的 *pDesc* 才能判斷描述元類型。

*pDesc \[in, optional]*\
類型：const D3D12\_SHADER\_RESOURCE\_VIEW\_DESC \*

指向描述著色器資源檢視之 [D3D12\_SHADER\_RESOURCE\_VIEW\_DESC](/zh-TW/reference/graphics/d3d12/structs/d3d12_shader_resource_view_desc_public) 結構的指標。

Null *pDesc* 用於在可能的情況下初始化預設描述元。此行為與 D3D11 Null 描述元行為相同，會填入預設值。此行為會繼承資源格式和維度 (若非無類型)；對於緩衝區，SRV 會以完整緩衝區為目標且為具類型 (非原始或結構化)；對於紋理，SRV 會以完整紋理、所有 mip 和所有陣列配量為目標。並非所有資源都支援 Null 描述元初始化。

*DestDescriptor \[in]*\
類型：D3D12\_CPU\_DESCRIPTOR\_HANDLE

描述代表著色器資源檢視的 CPU 描述元控制代碼。此控制代碼可以在著色器可見或非著色器可見的描述元堆積中建立。

### 傳回值

類型：void

無。

## 備註

處理 YUV 4:2:0 視訊格式 應用程式必須將明亮度 (Y) 平面與色度 (UV) 平面分開對應。開發人員可以針對同一個紋理呼叫 **CreateShaderResourceView** 兩次，並傳入 1 通道和 2 通道格式來執行此操作。傳入與 Y 平面相容的 1 通道格式只會對應 Y 平面。傳入與 UV 平面 (一起) 相容的 2 通道格式只會將 U 和 V 平面對應為單一資源檢視。

YUV 4:2:0 格式列於 [DXGI\_FORMAT](https://learn.microsoft.com/en-us/windows/desktop/api/dxgiformat/ne-dxgiformat-dxgi_format) 中。

#### 範例

[D3D12nBodyGravity](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/working-samples) 範例使用 **ID3D12Device::CreateShaderResourceView** 的方式如下：

根據一個描述來描述並建立兩個著色器資源檢視。

```cpp theme={null}
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Format = DXGI_FORMAT_UNKNOWN;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
srvDesc.Buffer.FirstElement = 0;
srvDesc.Buffer.NumElements = ParticleCount;
srvDesc.Buffer.StructureByteStride = sizeof(Particle);
srvDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;

CD3DX12_CPU_DESCRIPTOR_HANDLE srvHandle0(m_srvUavHeap->GetCPUDescriptorHandleForHeapStart(), SrvParticlePosVelo0 + index, m_srvUavDescriptorSize);
CD3DX12_CPU_DESCRIPTOR_HANDLE srvHandle1(m_srvUavHeap->GetCPUDescriptorHandleForHeapStart(), SrvParticlePosVelo1 + index, m_srvUavDescriptorSize);
m_device->CreateShaderResourceView(m_particleBuffer0[index].Get(), &srvDesc, srvHandle0);
m_device->CreateShaderResourceView(m_particleBuffer1[index].Get(), &srvDesc, srvHandle1);
```

請參閱 [D3D12 參考中的範例程式碼](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/notes-on-example-code)。

## 需求

**標頭：** d3d12\_xs.h 或 d3d12\_x.h\
**程式庫：** d3d12\_xs.lib 或 d3d12\_x.lib\
**支援的平台：** XBOX Series 主機和 XBOX One 系列主機

## 另請參閱

[ID3D12Device](/zh-TW/reference/graphics/d3d12_x/interfaces/id3d12device/id3d12device)


## Related topics

- [D3D12_SHADER_RESOURCE_VIEW_DESC](/zh-TW/reference/graphics/d3d12/structs/d3d12_shader_resource_view_desc_public.md)
- [D3D12_TEX1D_SRV](/zh-TW/reference/graphics/d3d12/structs/d3d12_tex1d_srv_public.md)
- [D3D12_CPU_DESCRIPTOR_HANDLE](/zh-TW/reference/graphics/d3d12/structs/d3d12_cpu_descriptor_handle_public.md)
- [D3D12_TEX3D_SRV](/zh-TW/reference/graphics/d3d12/structs/d3d12_tex3d_srv_public.md)
- [D3D12_TEXCUBE_SRV](/zh-TW/reference/graphics/d3d12/structs/d3d12_texcube_srv_public.md)
