规划 DirectX 移植Plan your DirectX port

摘要Summary

规划从 DirectX 9 到 DirectX 11 和通用 Windows 平台 (UWP) 的游戏移植项目:升级图形代码并将游戏放置到 Windows 运行时环境中。Plan your game porting project from DirectX 9 to DirectX 11 and Universal Windows Platform (UWP): upgrade your graphics code, and put your game in the Windows Runtime environment.

规划移植图形代码Plan to port graphics code

在开始将游戏移植到 UWP 之前,请务必确保游戏没有任何 Direct3D 8 残余。Before you begin porting your game to UWP, it's important to ensure that your game does not have any holdovers from Direct3D 8. 确保游戏不包含固定函数管道的任何遗留物。Ensure that your game doesn't have any remnants of the fixed function pipeline. 有关已弃用功能的完整列表(包括固定管道功能),请参阅已弃用的功能For a complete list of deprecated features, including fixed pipeline functionality, see Deprecated Features.

从 Direct3D 9 升级到 Direct3D 11 不仅仅是搜索和替换的更改。Upgrading from Direct3D 9 to Direct3D 11 is more than a search-and-replace change. 你需要知道 Direct3D 设备、设备上下文以及图形基础结构之间的差别,并且需要了解自从 Direct3D 9 以来的其他重要更改。You need to know the difference between the Direct3D device, device context, and graphics infrastructure, and learn about other important changes since Direct3D 9. 你可以通过阅读本部分中的其他主题来开始这个过程。You can start this process by reading the other topics in this section.

你必须将 D3DX 和 DXUT 帮助程序库替换为你自己的帮助程序库或替换为社区工具。You must replace the D3DX and DXUT helper libraries with your own helper libraries, or with community tools. 有关详细信息,请参阅功能映射部分。See the Feature mapping section for more info.

注意   你可以使用DirectX工具包或DirectXTex来替换以前由 D3DX 和 DXUT 提供的某些功能。Note   You can use the DirectX Tool Kit or DirectXTex to replace some functionality that was formerly provided by D3DX and DXUT.

 

应使用着色器模型4级别 9 1 或 9 3 的功能将使用汇编语言编写的着色器升级到 HLSL _ _ ,并且需要将为效果库编写的着色器更新为较新版本的 HLSL 语法。Shaders written in assembly language should be upgraded to HLSL using shader model 4 level 9_1 or 9_3 functionality, and shaders written for the Effects library will need to be updated to a more recent version of HLSL syntax. 有关详细信息,请参阅功能映射部分。See the Feature mapping section for more info.

熟悉不同的 Direct3D 功能级别Get familiar with the different Direct3D feature levels. 功能级别通过定义已知的功能集对广泛的视频硬件进行分类。Feature levels classify a wide range of video hardware by defining sets of known functionality. 每个功能集都大致与 Direct3D 的版本相对应,从 9.1 到 11.2。Each set roughly corresponds to versions of Direct3D, from 9.1 through 11.2. 所有功能级别都使用 DirectX 11 API。All feature levels use the DirectX 11 API.

规划将 Win32 UI 代码移植到 CoreWindowPlan to port Win32 UI code to CoreWindow

UWP 应用在为应用容器创建的窗口(称为 CoreWindow)中运行。UWP apps run in a window created for an app container, called a CoreWindow. 你的游戏通过从 IFrameworkView 继承来控制该窗口,与桌面窗口相比,该窗口要求的实现细节较少。Your game controls the window by inheriting from IFrameworkView, which requires less implementation details than a desktop window. 游戏的主循环将位于 IFrameworkView::Run 方法中。Your game's main loop will be in the IFrameworkView::Run method.

UWP 应用的生命周期与桌面应用的生命周期大相径庭。The lifecycle of a UWP app is very different from a desktop app. 需要经常保存该游戏,因为当发生暂停事件时,应用 仅有有限的时间来停止运行代码,并且你也希望确保在应用恢复时玩家可以立即回到他们之前所在的位置。You'll need to save the game often, because when a suspend event happens your app only has a limited amount of time to stop running code, and you want to make sure the player can get back to where they were right away when your app resumes. 保存游戏的频率应该只需保持恢复时有连续的玩游戏体验即可,不能太过频繁,否则游戏保存会影响帧速率或者导致游戏时断时续。Games should save just often enough to maintain a continuous gameplay experience from resume, but not so often that the game saves impact framerate or cause the game to stutter. 当游戏从某个已终止状态恢复时,可能需要加载 游戏状态。Your game will potentially need to load game state when the game resumes from a terminated state.

DirectXMath 可以用作 D3DXMath 和 XNAMath 的替代项,如果你需要一个数学库,那么它可以派上用场。DirectXMath can be used as a replacement for D3DXMath and XNAMath, and it can come in handy if you need a math library. DirectXMath 包含快速、便携的数据类型以及进行对齐和打包操作以便用于着色器的类型。DirectXMath has fast, portable data types, and types that are aligned and packed for use with shaders.

已经对原生库(如互锁 API)进行了扩展,以支持 ARM 内部函数。Native libraries such as the Interlocked API have been expanded to support ARM intrinsics. 如果游戏使用互锁 API, 可以继续在 DirectX 11 和 UWP 中使用它们。If your game uses interlocked APIs, you can keep using them in DirectX 11 and UWP.

我们的模板和代码示例使用目前你可能还不熟悉的 C++ 新功能。Our templates and code samples use new C++ features that you might not be familiar with yet. 例如,将异步方法 与 lambda expressions 结合使用可加载 Direct3D 资源,而无需阻止 UI 线程。For example, asynchronous methods are used with lambda expressions to load Direct3D resources without blocking the UI thread.

有两个概念供你经常使用:There are two concepts you'll use often:

  • 托管参考(^ 运算符)和托管类(ref 类)是 Windows 运行时的基础部分。Managed references (^ operator) and managed classes (ref classes) are a fundamental part of the Windows Runtime. 你需要使用托管 ref 类与 Windows 运行时组件交互,例如 IFrameworkView(演练中有关于此内容的详细信息)。You will need to use managed ref classes to interface with Windows Runtime components, for example IFrameworkView (more on that in the walkthrough).
  • 使用 Direct3D 11 COM 接口时,使用 Microsoft::WRL::ComPtr 模板类型以使 COM 指针更易于使用。When working with Direct3D 11 COM interfaces, use the Microsoft::WRL::ComPtr template type to make COM pointers easier to use.