规划从 OpenGL ES 2.0 到 Direct3D 的移植Plan your port from OpenGL ES 2.0 to Direct3D

重要的 APIImportant APIs

如果你移植 iOS 或 Android 平台中的游戏,那么你可能需要在 OpenGL ES 2.0 方面进行大量投资。If you are porting a game from the iOS or Android platforms, you have probably made a significant investment in OpenGL ES 2.0. 如果你准备将你的图形管道代码库移动到 Direct3D 11 和 Windows 运行时,那么在开始之前你应该考虑以下事项。When preparing to move your graphics pipeline codebase to Direct3D 11 and the Windows Runtime, there are a few things you should consider before you start.

大多数移植工作通常涉及初始浏览该代码库,然后在两个模型之间映射常用的 API 和模式。Most porting efforts usually involving initially walking the codebase and mapping common APIs and patterns between the two models. 你会发现,如果你花一段时间来阅读和查看该主题,那么这个过程就会变得容易一些。You'll find this process a bit easier if you take some time to read and review this topic.

下面是将图形从 OpenGL ES 2.0 移植到 Direct3D 11 时要注意的一些事项。Here are some things to be aware of when porting graphics from OpenGL ES 2.0 to Direct3D 11.

有关特定 OpenGL ES 2.0 提供商的说明Notes on specific OpenGL ES 2.0 providers

本部分中的移植主题参考了由 Khronos Group 创建的 OpenGL ES 2.0 规范的 Windows 实现。The porting topics in this section reference the Windows implementation of the OpenGL ES 2.0 specification created by the Khronos Group. 所有 OpenGL ES 2.0 代码示例都是使用 Visual Studio 2012 和 Windows C 基本语法开发的。All OpenGL ES 2.0 code samples were developed using Visual Studio 2012 and basic Windows C syntax. 如果你使用的是 Objective-C (iOS) 或 Java (Android) 代码库,那么请注意,所提供的 OpenGL ES 2.0 代码示例可能没有使用类似的 API 调用语法或参数。If you are coming from an Objective-C (iOS) or Java (Android) codebase, be aware that the provided OpenGL ES 2.0 code samples may not use similar API calling syntax or parameters. 本指南尽量保持与平台无关。This guidance tries to stay as platform agnostic as possible.

本文档对 OpenGL ES 代码和参考仅使用 2.0 规范 API。This documentation only uses the 2.0 specification APIs for the OpenGL ES code and reference. 如果是从 OpenGL ES 1.1 或 3.0 进行移植,那么该内容仍然有用,但是某些 OpenGL ES 2.0 代码示例和上下文可能不太熟悉。If you are porting from OpenGL ES 1.1 or 3.0, this content can still prove useful, although some of the OpenGL ES 2.0 code examples and context may be unfamiliar.

这些主题中的 Direct3D 11 示例使用具有组件扩展 (CX) 的 Microsoft Windows C++。The Direct3D 11 samples in these topics use Microsoft Windows C++ with Component Extensions (CX). 有关此版本的 c + + 语法的详细信息,请参阅运行时平台的组件扩展 (c + + \ CX) 的快速参考 Visual C++For more info on this version of the C++ syntax, read Visual C++, Component Extensions for Runtime Platforms, and Quick Reference (C++\CX).

了解硬件要求和资源Understand your hardware requirements and resources

OpenGL ES 2.0 所支持的图形处理功能集大致映射到 Direct3D 9.1 中提供的功能。The set of graphics processing features supported by OpenGL ES 2.0 roughly maps to the features provided in Direct3D 9.1. 如果你想利用 Direct3D 11 中提供的更高级的功能,请在规划移植时查看 Direct3D 11 文档, 或在完成初始工作之后查看从 DirectX 9 移植到通用 Windows 平台 (UWP) 主题。If you want to take advantage of the more advanced features provided in Direct3D 11, review the Direct3D 11 documentation when planning your port, or review the Port from DirectX 9 to Universal Windows Platform (UWP) topics when you're done with the initial effort.

若要使你的初始移植工作更简单,请从 Visual Studio Direct3D 模板开始。To make your initial porting effort simple, start with a Visual Studio Direct3D template. 该模板为你提供已配置好的基本呈现器,并且支持 UWP 应用功能 (如在窗口更改时重新创建资源)以及 Direct3D 功能级别。It provides a basic renderer already configured for you, and supports UWP app features like recreating resources on window changes and Direct3D feature levels.

了解 Direct3D 功能级别Understand Direct3D feature levels

Direct3D 11 为 _ 11 1 (direct3d 9.1) 提供对硬件 "功能级别" 的支持 _ 。Direct3D 11 provides support for hardware "feature levels" from 9_1 (Direct3D 9.1) for 11_1. 这些功能级别指示某些图形功能和资源的可用性。These feature levels indicate the availability of certain graphics features and resources. 通常,大多数 OpenGL ES 2.0 平台支持 Direct3D 9.1 (功能级别 9 _ 1) 功能集。Typically, most OpenGL ES 2.0 platforms support a Direct3D 9.1 (feature level 9_1) set of features.

查看 DirectX 图形功能和 APIReview DirectX graphics features and APIs

API 系列API Family 描述Description
DXGIDXGI DirectX 图形基础结构 (DXGI) 提供图形硬件和 Direct3D 之间的接口。The DirectX Graphics Infrastructure (DXGI) provides an interface between the graphics hardware and Direct3D. 它使用 IDXGIAdapterIDXGIDevice1 COM 接口设置设备适配器和硬件配置。It sets the device adapter and hardware configuration using the IDXGIAdapter and IDXGIDevice1 COM interfaces. 使用它来创建和配置你的缓冲区以及其他窗口资源。Use it to create and configure your buffers and other window resources. 值得注意的是,IDXGIFactory2 工厂模式 iis 用来获取图形资源,包括交换链(一组帧缓冲区)。Notably, the IDXGIFactory2 factory pattern iis used to acquire the graphics resources, including the swap chain (a set of frame buffers). 由于 DXGI 拥有交换链,因此 IDXGISwapChain1 接口用来向屏幕提供帧。Since DXGI owns the swap chain, the IDXGISwapChain1 interface is used to present frames to the screen.
Direct3DDirect3D Direct3D 是一组 API,这些 API 提供图形接口的视觉表示并且允许你使用它绘制图形。Direct3D is the set of APIs that provide a virtual representation of the graphics interface and allow you to draw graphics using it. 在功能方面,版本 11 大致能够与 OpenGL 4.3 相媲美。Version 11, is roughly comparable, feature-wise, to OpenGL 4.3. (另一方面,尽管 OpenGL ES 2.0 与 DirectX9 和 OpenGL 2.0 在功能方面非常相似,但它具有 OpenGL 3.0 的统一着色器管道。)大多数繁重的工作是通过 ID3D11Device1 和 ID3D11DeviceContext1 接口完成的,这两个接口分别提供对各个资源和子资源以及呈现上下文的访问。(OpenGL ES 2.0, on the other hand, is similar to DirectX9, feature-wise, and OpenGL 2.0, but with OpenGL 3.0's unified shader pipeline.) Most of the heavy lifting is done with the ID3D11Device1 and ID3D11DeviceContext1 interfaces which provide access to individual resources and subresources, and the rendering context, respectively.
Direct2DDirect2D Direct2D 提供一组用于 GPU 加速 2D 呈现的 API。Direct2D provides a set of APIs for GPU-accelerated 2D rendering. 在作用方面,它与 OpenVG 非常相似。It can be considered similar in purpose to OpenVG.
DirectWriteDirectWrite DirectWrite 提供一组用于 GPU 加速高质量字体呈现的 API。DirectWrite provides a set of APIs for GPU-accelerated, high-quality font rendering.
DirectXMathDirectXMath DirectXMath 提供一组 API 和宏,用于处理常见的线性代数和三角函数类型、值以及函数。DirectXMath provides a set of APIs and macros for handling common linear algebra and trigonometric types, values, and functions. 这些类型和函数设计为与 Direct3D 及其着色器操作很好地结合使用。These types and functions are designed to work well with Direct3D and its shader operations.
DirectX HLSLDirectX HLSL Direct3D 着色器使用的当前 HLSL 语法。The current HLSL syntax used by Direct3D shaders. 它实现 Direct3D 着色器模型 5.0。It implements Direct3D Shader Model 5.0.

 

查看 Windows 运行时 API 和模板库Review the Windows Runtime APIs and template library

Windows 运行时 API 为 UWP 应用提供整体基础结构。The Windows Runtime APIs provide the overall infrastructure for UWP apps. 在此处进行查看。Review them here.

移植图形管道时使用的主要 Windows 运行时 API 包括:Key Windows Runtime APIs used in porting your graphics pipeline include:

此外,Windows 运行时 C++ 模板库 (WRL) 是提供创作和使用 Windows 运行时组件的低级方法模板库。Additionally, the Windows Runtime C++ Template Library (WRL) is a template library that provides a low-level way to author and use Windows Runtime components. 最好将 UWP 应用的 Direct3D 11 API 与该库中的接口和类型结合使用,如智能指针 (ComPtr)。The Direct3D 11 APIs for UWP apps are best used in conjunctions with the interfaces and types in this library, such as smart pointers (ComPtr). 有关 WRL 的详细信息,请阅读 Windows 运行时 C++ 模板库(WRL)For more info on the WRL, read Windows Runtime C++ Template Library (WRL).

更改坐标系Change your coordinate system

有时容易让移植工作混乱的一个不同之处是要将 OpenGL 的传统右手坐标系更改为 Direct3D 默认的左手坐标系。One difference that sometimes confuses early port efforts is the change from OpenGL's traditional right-handed coordinate system to Direct3D's default left-handed coordinate system. 坐标模型方面的这个更改会影响游戏的很多部分,从顶点缓冲区的设置和配置到很多矩阵数学函数。This change in coordinate modeling affects many parts of your game, from the setup and configuration of your vertex buffers to many of your matrix math functions. 需要进行两个最重要的更改是:The two most important changes to make are:

  • 调换三角形顶点的顺序以便 Direct3D 从前面按顺时针方向遍历它们。Flip the order of triangle vertices so that Direct3D traverses them clockwise from the front. 例如,如果你的顶点在 OpenGL 管道中的索引为 0、1 和 2,则会按照 0、2、1 的顺序将它们传递给 Direct3D。For example, if your vertices are indexed as 0, 1, and 2 in your OpenGL pipeline, pass them to Direct3D as 0, 2, 1 instead.
  • 使用视图矩阵将世界空间在 z 方向缩放 -1.0f,从而有效地反转 z 轴坐标。Use the view matrix to scale world space by -1.0f in the z direction, effectively reversing the z-axis coordinates. 为此,请在视图矩阵中将 M31、M32 以及 M33 位置处的值的符号反向(当将其移植到 Matrix 类型时)。To do this, flip the sign of the values at positions M31, M32, and M33 in your view matrix (when porting it to the Matrix type). 如果 M34 不为 0,则也要将它的符号反向。If M34 is not 0, flip its sign as well.

但是,Direct3D 可以支持右手坐标系。However, Direct3D can support a right-handed coordinate system. DirectXMath 提供了很多可以在左手和右手坐标系上操作以及跨两个坐标系操作的函数。DirectXMath provides a number of functions that operate on and across both left-handed and right-handed coordinate systems. 这些函数可以用来保存某些原始网格数据和矩阵处理。They can be used to preserve some of your original mesh data and matrix processing. 它们包括:They include:

DirectXMath 矩阵函数DirectXMath matrix function 描述Description
XMMatrixLookAtLHXMMatrixLookAtLH 使用一个相机位置、一个向上的方向和一个焦点为左手坐标系构建一个视图矩阵。Builds a view matrix for a left-handed coordinate system using a camera position, an up direction, and a focal point.
XMMatrixLookAtRHXMMatrixLookAtRH 使用一个相机位置、一个向上的方向和一个焦点为右手坐标系构建一个视图矩阵。Builds a view matrix for a right-handed coordinate system using a camera position, an up direction, and a focal point.
XMMatrixLookToLHXMMatrixLookToLH 使用一个相机位置、一个向上的方向和一个相机方向为左手坐标系构建一个视图矩阵。Builds a view matrix for a left-handed coordinate system using a camera position, an up direction, and a camera direction.
XMMatrixLookToRHXMMatrixLookToRH 使用一个相机位置、一个向上的方向和一个相机方向为右手坐标系构建一个视图矩阵。Builds a view matrix for a right-handed coordinate system using a camera position, an up direction, and a camera direction.
XMMatrixOrthographicLHXMMatrixOrthographicLH 为左手坐标系构建一个正交投影矩阵。Builds an orthogonal projection matrix for a left-handed coordinate system.
XMMatrixOrthographicOffCenterLHXMMatrixOrthographicOffCenterLH 为左手坐标系构建一个自定义正交投影矩阵。Builds a custom orthogonal projection matrix for a left-handed coordinate system.
XMMatrixOrthographicOffCenterRHXMMatrixOrthographicOffCenterRH 为右手坐标系构建一个自定义正交投影矩阵。Builds a custom orthogonal projection matrix for a right-handed coordinate system.
XMMatrixOrthographicRHXMMatrixOrthographicRH 为右手坐标系构建一个正交投影矩阵。Builds an orthogonal projection matrix for a right-handed coordinate system.
XMMatrixPerspectiveFovLHXMMatrixPerspectiveFovLH 根据视野构建一个左手透视投影矩阵。Builds a left-handed perspective projection matrix based on a field of view.
XMMatrixPerspectiveFovRHXMMatrixPerspectiveFovRH 根据视野构建一个右手透视投影矩阵。Builds a right-handed perspective projection matrix based on a field of view.
XMMatrixPerspectiveLHXMMatrixPerspectiveLH 构建一个左手透视投影矩阵。Builds a left-handed perspective projection matrix.
XMMatrixPerspectiveOffCenterLHXMMatrixPerspectiveOffCenterLH 构建一个自定义版本的左手透视投影矩阵。Builds a custom version of a left-handed perspective projection matrix.
XMMatrixPerspectiveOffCenterRHXMMatrixPerspectiveOffCenterRH 构建一个自定义版本的右手透视投影矩阵。Builds a custom version of a right-handed perspective projection matrix.
XMMatrixPerspectiveRHXMMatrixPerspectiveRH 构建一个右手透视投影矩阵。Builds a right-handed perspective projection matrix.

 

OpenGL ES2.0 到 Direct3D 11 移植常见问题解答OpenGL ES2.0-to-Direct3D 11 porting Frequently Asked Questions

  • 问题:“通常情况下,我可以在我的 OpenGL 代码中搜索某些字符串或模式并将它们替换为 Direct3D 同等内容?”Question: "In general, can I search for certain strings or patterns in my OpenGL code and replace them with the Direct3D equivalents?"
  • 解答:不可以。Answer: No. OpenGL ES 2.0 和 Direct3D 11 来自不同的图形管道模型。OpenGL ES 2.0 and Direct3D 11 come from different generations of graphics pipeline modeling. 尽管它们表面上在概念和 API 之间有一些相似之处,如呈现上下文和着色器的实例化,但你也应该查看本指南以及 Direct3D 11 参考以便再次创建管道时你可以做出最佳选择,而不是尝试 1 对 1 映射。While there are some surface similarities between concepts and APIs, such as the rendering context and the instancing of shaders, you should review this guidance as well as the Direct3D 11 reference so you can make the best choices when recreating your pipeline instead of attempting a 1-to-1 mapping. 但是,如果你从 GLSL 移植到 HLSL,那么为 GLSL 变量、内部函数以及函数创建一组常用的别名不仅会使移植更加容易,而且还可以保留唯一一组着色器代码文件。However, if you are porting from GLSL to HLSL, creating a set of common aliases for GLSL variables, intrinsincs, and functions can not only make porting easier, it allows you to maintain only one set of shader code files.