瞭解您的 Direct3D 9 遊戲功能如何轉換成 Direct3D 11 和通用 Windows 平台 (UWP)。

另請參閱 規劃您的 DirectX 連線埠, 以及從 Direct3D 9 到 Direct3D 11 的重要變更。

將 Direct3D 9 對應至 DirectX 11 API

Direct3D (Direct3D) 仍然是 DirectX 圖形的基礎,但 API 自 DirectX 9 以來已經改變:

  • Microsoft DirectX Graphics Infrastructure (DXGI) 用於設定圖形介面卡。 使用 DXGI,可選取緩衝區格式、 建立交換鏈、顯示影格,以及建立共用資源。 請參閱 DXGI Overview。
  • Direct3D 裝置內容用來設定流程狀態並產生轉譯命令。 我們的大多數範例使用即時前後關聯直接呈現至裝置;Direct3D 11 也支援多執行緒呈現,在此情況下,會使用延緩前後關聯。 請參閱 Introduction to a Device in Direct3D 11。
  • 有些功能已被取代,最著名的就是固定函式流程。 請參閱 Deprecated Features。
  • 如需 Direct3D 11 功能的完整清單, 請參閱 Direct3D 11 Features 和 Direct3D 11 Features。

    從 Direct2D 9 移至 Direct2D 11

    Direct2D (Windows), 仍然是 DirectX 圖形和 Windows 的重要組成部分。 您仍然可以使用 Direct2D 來繪製 2D 遊戲,並在 Direct3D 上繪製疊加 (HUD)。

    Direct2D 運行於 Direct3D 之上;2D 遊戲可以使用任一 API 實現。 例如,使用 Direct3D 實現的 2D 遊戲可以使用正交投影,設定 Z 值來控制圖元繪製順序,並使用畫素著色器新增特殊效果。

    由於 Direct2D 是以 Direct3D 為基礎的,因此它也使用 DXGI 和裝置背景內容。 請參閱 Direct2D API Overview。

    DirectWrite API 增加對使用 Direct2D 的格式化文本的支援。 請參閱 Introducing DirectWrite。

    取代已停用的協助程式庫

    D3DX 和 DXUT 已被取代,不能用於 UWP 遊戲。 這些協助程式庫為紋理載入和網格載入等作業提供資源。

  • 從 Direct3D 9到 UWP Walkthrough的 Simple 連線埠示範如何設定視窗、 初始化 Direct3D 及執行基本的 3D 轉譯。
  • DirectX Walkthrough 的簡單 UWP 遊戲示範常見的遊 戲程式設計任務,包括圖形、載入檔案、UI、控制元件和聲音。
  • DirectX Tool Kit 社區專案提供協助程式類別,可搭 配 Direct3D 11 和 UWP 應用程式使用。
  • 將著色器程式從 FX 移至 HLSL

    D3DX 公用程式庫 (D3DX 9、D3DX 10 和 D3DX 11)(包括 Effects) 已被 UWP 取代。 所有 UWP 的 DirectX 遊戲都使用 HLSL without Effects 驅動圖形管道。

    Visual Studio 仍然使用引擎蓋下的 FXC 來編譯著色器物件。 UWP 遊戲著色器提前編譯。 在執行階段載入位元碼,然後在適當的轉譯路徑期間,將每個著色器資源繫結至圖形配管。 著色器應該移動到它們各自獨立的 .HLSL 檔案,並且著色技術應該在 C++ 程式碼中實現。

    如需快速瞭解載入著色器資源,請參閱 Direct3D 9 到 UWP 的 Simple port。

    Direct3D 11 引入 Shader Model 5,它需要 Direct3D 特徵層級 11_0 (或更高)。 請參閱 Direct3D 11 的 HLSL Shader Model 5 功能。

    取代 XNAMath 和 D3DXMath

    使用 XNAMath (或 D3DXMath) 的程式碼應該移 轉至 DirectX Math。 DirectX Math 包括可跨 x86、x64 和 Arm 移植的型別。 請參閱 XNA Math Library 中的 Code Migration。

    請備註,DirectX Math 浮點型別便於與著色器一起使用。 例如: XMFLOAT4、 XMFLOAT4X4, 可以方 便地 對齊 常數緩衝區 的資料。

    以 XAudio2 (和背景音訊) 取代 DirectSound

    UWP 不支援 DirectSound:

  • 使用 XAudio2 將音 效新增至您的遊戲。
  • 以 XInput 和 Windows 執行階段 API 取代 DirectInput

    UWP 不支援 DirectInput:

  • 使用 CoreWindow input 事件回呼進行滑鼠、 鍵盤和觸 控輸入。
  • 使用 XInput 1.4 支援遊戲控制器 (和遊戲控制器耳機支援)。 如果您使用桌面和 UWP 的共 用程式碼庫,請參閱 XInput 版本,以取得關於向後 相容性的資訊。
  • 如果您的 遊戲需要使用應 用程式列, 請註冊 EdgeGesture。
  • 使用 Microsoft Media Foundation 而非 DirectShow

    DirectShow 不再是 DirectX API (或 Windows API) 的一部分。 Microsoft Media Foundation Direct3D 使用共用曲面提供視訊內容。 請參閱 Direct3D 11 Video APIs。

    以網路程式碼取代 DirectPlay

    Microsoft DirectPlay 已被取代。 如果您的遊戲使用網路服務,您需要提供符合 UWP 要求的網路程式碼。 使用下列 API:

  • UWP 應用程式的 Win32 和 COM (網路) (Windows)
  • Windows.Networking 名稱 空間 (Windows)
  • Windows.Networking.Sockets 名稱 空間 (Windows)
  • Windows.Networking.Connectivity 名稱 空間 (Windows)
  • Windows.ApplicationModel.Background 名稱 空間 (Windows)
  • 下列文章可協助您在應用程式的套件資訊清單中新增網路功能並宣告支援網路。

  • 連線至插槽 (使用 C#/VB/C++ 和 XAML 的 UWP 應用程式) (Windows)
  • 使用 WebSockets (使用 C#/VB/C++ 和 XAML 的 UWP 應用程式) (Windows)
  • 連線至 Web 服務 (使用 C#/VB/C++ 和 XAML 的 UWP 應用程式) (Windows)
  • 網路功能基本知識
  • 請備註,所有 UWP 應用程式 (包括遊戲)都會使用特定型別的背景工作來維持應用程式暫停時的連線。 如果您的遊戲在暫停 期間需要保持連線狀態, 請參閱網絡基礎。

    使用下表幫助將程式碼從 Direct3D 9 轉換為 Direct3D 11。 這也有助於區分裝置和裝置背景內容。

    IDirect3DDevice9::TestCooperativeLevel

    呼叫 IDXGISwapChain1::Present1,並設定 DXGI _PRESENT_TEST 旗標。

    IDirect3DBaseTexture9

    IDirect3DTexture9

    IDirect3DCubeTexture9

    IDirect3DVolumeTexture9

    IDirect3DIndexBuffer9

    IDirect3DVertexBuffer9

    ID3D11Buffer

    ID3D11Texture1D

    ID3D11Texture2D

    ID3D11Texture3D

    ID3D11ShaderResourceView

    ID3D11RenderTargetView

    ID3D11DepthStencilView

    IDirect3DVertexShader9

    IDirect3DPixelShader9

    ID3D11VertexShader

    ID3D11PixelShader

    IDirect3DVertexDeclaration9

    ID3D11InputLayout

    IDirect3DDevice9::SetRenderState

    IDirect3DDevice9::SetSamplerState

    ID3D11BlendState1

    ID3D11DepthStencilState

    ID3D11RasterizerState1

    ID3D11SamplerState

    IDirect3DDevice9::DrawIndexedPrimitive

    IDirect3DDevice9::DrawPrimitive

    ID3D11DeviceContext::Draw

    ID3D11DeviceContext::DrawIndexed

    ID3D11DeviceContext::DrawIndexedInstanced

    ID3D11DeviceContext::DrawInstanced

    ID3D11DeviceContext::IASetPrimitiveTopology

    ID3D11DeviceContext::DrawAuto

    IDirect3DDevice9::BeginScene

    IDirect3DDevice9::EndScene

    IDirect3DDevice9::DrawPrimitiveUP

    IDirect3DDevice9::DrawIndexedPrimitiveUP

    無直接對等

    IDirect3DDevice9::ShowCursor

    IDirect3DDevice9::SetCursorPosition

    IDirect3DDevice9::SetCursorProperties

    使用標準游標 API。

    IDirect3DDevice9::Reset

    LOST 裝置和 POOL_MANAGED 已不存在。 IDXGI SwapChain1::Present1, 可能會失敗,並傳回 DXGI _ERROR_DEVICE_REMOVED 值。

    IDirect3DDevice9:DrawRectPatch

    IDirect3DDevice9:DrawTriPatch

    IDirect3DDevice9:LightEnable

    IDirect3DDevice9:MultiplyTransform

    IDirect3DDevice9:SetLight

    IDirect3DDevice9:SetMaterial

    IDirect3DDevice9:SetNPatchMode

    IDirect3DDevice9:SetTransform

    IDirect3DDevice9:SetFVF

    IDirect3DDevice9:SetTextureStageState

    固定函式流程已被取代。

    IDirect3DDevice9:CheckDepthStencilMatch

    IDirect3DDevice9:CheckDeviceFormat

    IDirect3DDevice9:GetDeviceCaps

    IDirect3DDevice9:ValidateDevice

    功能位由特徵層級取代。 對於任何指定的功能層級,只有少數格式和功能使用案例是可選的。 可以使用 ID3D11Device::CheckFeatureSupport ID3D11Device::CheckFormatSupport 檢查這些專案。

    曲面格式對映

    使用下表將 Direct3D 9 格式轉換為 DXGI 格式。

  • IDirect3D Device9::SetCursorPosition SetCursorPos 代。
  • IDirect3D Device9::SetCursorProperties SetCursor 代。
  • IDirect3D Device9::SetIndices ID3D11DeviceContext::IASetIndexBuffer 代。
  • IDirect3D Device9::SetRenderTarget ID3D11DeviceContext::OMSetRenderTargets 代。
  • IDirect3D Device9::SetScissorRect ID3D11DeviceContext::RSSetScissorRect 代。
  • IDirect3D Device9::SetStreamSource ID3D11DeviceContext::IASetVertexBuffers 代。
  • IDirect3D Device9::SetVertexDeclaration ID3D11DeviceContext::IASetInputLayout 代。
  • IDirect3D Device9::SetViewport ID3D11DeviceContext::RSSetViewports 代。
  • IDirect3D Device9::ShowCursor ShowCursor 代。
  • 透過 IDirect3D Device9::SetGammaRamp, 視訊卡硬體 gammaRamp 的控制由 IDXGI Output::SetGammaControl 取代。 請參閱 Using gamma correction。

    IDirect3D Device9::ProcessVertices, 會由幾何著色器的串流輸出功能所取代。 請參閱 Getting started with the Stream-Output Stage。

    IDirect3DDevice9::SetClipPlane 的方法已被 VS_4_0 及更高 版本中可用的 HLSL SV_ClipDistance vertex 著色器 輸出語義 (請參閱 語義 ) 或新的 HLSL clipplanes 函式屬性 (請參閱功能層級 9 硬體上的 使用者剪輯平面) 所 取代。

    IDirect3DDevice9::SetPaletteEntries IDirect3DDevice9::SetCurrentTexturePalette 已過時。 取代這些選項,使用畫素著色器來尋找 256x1 R8G8B8A8 紋理中的顏色。

    固定函式 鑲嵌 函式,如 DrawRectPatch、 DrawTriPatch、 SetNPatchMode,和 DeletePatch, 已被取代。 以程式化流程 SM5.0 鑲嵌著色器取代它們 (如果硬體支援鑲嵌著色器)。

    不再支援 IDirect3DDevice9::SetFVF, 以及 FVF 程式碼。 您應該先從 D3D8/D3D9 FVF 程式碼連線至 D3D9 頂點宣告,再連線至 D3D11 輸入配置。

    在 VS_4_0 及更高版本中,所有不直接支援的 D3DDECLTYPE,只要在頂點 著色器開頭處執行少量的位元運算,就可以相當有效地模擬。