OpenGL ES 编程指南一:iOS构建清单

logo.png

Checklist for Building OpenGL ES Apps for iOS

The OpenGL ES specification defines a platform-neutral API for using GPU hardware to render graphics. Platforms implementing OpenGL ES provide a rendering context for executing OpenGL ES commands, framebuffers to hold rendering results, and one or more rendering destinations that present the contents of a framebuffer for display. In iOS, the EAGLContext class implements a rendering context. iOS provides only one type of framebuffer, the OpenGL ES framebuffer object, and the GLKView and CAEAGLLayer classes implement rendering destinations.

OpenGL ES规范定义了一个跨平台的API,用于使用GPU硬件渲染图形。 支持OpenGL ES的平台提供了可执行OpenGL ES命令的渲染上下文,可用于保存渲染结果的帧缓冲区以及可显示帧缓冲区内容的一个或多个渲染目标。 在iOS中,EAGLContext类实现渲染上下文。 iOS只提供一种类型的framebuffer,OpenGL ES framebuffer对象,以及GLKView和CAEAGLLayer类实现渲染目标。

Building an OpenGL ES app in iOS requires several considerations, some of which are generic to OpenGL ES programming and some of which are specific to iOS. Follow this checklist and the detailed sections below to get started:

  1. Determine which version(s) of OpenGL ES have the right feature set for your app, and create an OpenGL ES context.
  2. Verify at runtime that the device supports the OpenGL ES capabilities you want to use.
  3. Choose where to render your OpenGL ES content.
  4. Make sure your app runs correctly in iOS.
  5. Implement your rendering engine.
  6. Use Xcode and Instruments to debug your OpenGL ES app and tune it for optimal performance.

在iOS中构建OpenGL ES应用程序需要几个注意事项,其中一些是OpenGL ES编程通用的,其中一些是特定于iOS的。 按照此清单和下面的详细部分开始:

  1. 确定哪个版本的OpenGL ES具有适合您应用程序的功能集,并创建一个OpenGL ES上下文。
  2. 在运行时验证设备是否支持您要使用的OpenGL ES功能。
  3. 选择在哪里渲染您的OpenGL ES内容。
  4. 确保您的应用程序在iOS中正常运行。
  5. 实现您的渲染引擎。
  6. 使用Xcode和Instruments调试您的OpenGL ES应用程序,并调整它以获得最佳性能。

Choosing Which OpenGL ES Versions to Support(选择可支持的OpenGL ES版本)

Decide whether your app should support OpenGL ES 3.0, OpenGL ES 2.0, OpenGL ES 1.1, or multiple versions.

  • OpenGL ES 3.0 is new in iOS 7. It adds a number of new features that enable higher performance, general-purpose GPU computing techniques, and more complex visual effects previously only possible on desktop-class hardware and game consoles.
  • OpenGL ES 2.0 is the baseline profile for iOS devices, featuring a configurable graphics pipeline based on programmable shaders.
  • OpenGL ES 1.1 provides only a basic fixed-function graphics pipeline and is available in iOS primarily for backward compatibility.

决定您的应用程序是否应该支持OpenGL ES 3.0,OpenGL ES 2.0,OpenGL ES 1.1或多个版本。

  • OpenGL ES 3.0是iOS 7中的新功能。它增加了许多新功能,可以实现更高性能,通用GPU计算技术,以及以前只能在桌面级硬件和游戏机上使用更复杂的视觉效果。
  • OpenGL ES 2.0是iOS设备的基准配置文件,具有基于可编程着色器的可配置图形管道。
  • OpenGL ES 1.1仅提供基本的固定功能图形管道,主要用于向后兼容的iOS中。

You should target the version or versions of OpenGL ES that support the features and devices most relevant to your app. To learn more about the OpenGL ES capabilities of iOS devices, read iOS Device Compatibility Reference.
您应该使用与应用程序最相关的功能和设备的OpenGL ES的版本或版本。 要了解有关iOS设备的OpenGL ES功能的更多信息,请阅读iOS设备兼容性参考。

To create contexts for the versions of OpenGL ES you plan to support, read Configuring OpenGL ES Contexts. To learn how your choice of OpenGL ES version relates to the rendering algorithms you might use in your app, read OpenGL ES Versions and Renderer Architecture.
要为您打算支持的OpenGL ES版本创建上下文,请阅读配置OpenGL ES上下文。要了解OpenGL ES版本的选择与您可能在应用程序中使用的渲染算法相关,请参阅OpenGL ES版本和渲染器架构。

Verifying OpenGL ES Capabilities(验证OpenGL ES功能)

The iOS Device Compatibility Reference summarizes the capabilities and extensions available on shipping iOS devices. However, to allow your app to run on as many devices and iOS versions as possible, your app should always query the OpenGL ES implementation for its capabilities at runtime.

iOS设备兼容性参考汇总了可用于运送iOS设备的功能和扩展。 但是,为了让应用程序尽可能多地运行设备和iOS版本,您的应用程序应始终在运行时查询OpenGL ES实现的功能。

To determine implementation specific limits such as the maximum texture size or maximum number of vertex attributes, look up the value for the corresponding token (such as MAX_TEXTURE_SIZE or MAX_VERTEX_ATTRIBS, as found in the gl.h header) using the appropriate glGet function for its data type.

要确定实现特定的限制,例如最大纹理大小或顶点属性的最大数量,请使用适当的glGet函数为其数据查找相应令牌的值(例如,在gl.h头中找到的MAX_TEXTURE_SIZE或MAX_VERTEX_ATTRIBS) 类型。

To check for OpenGL ES 3.0 extensions, use the glGetIntegerv and glGetStringi functions as in the following code example:
要检查OpenGL ES 3.0扩展,请使用glGetIntegerv和glGetStringi函数,如以下代码示例所示:

BOOL CheckForExtension(NSString *searchName)
{
  // Create a set containing all extension names.
  // (For better performance, create the set only once and cache it for future use.)
  int max = 0;
  glGetIntegerv(GL_NUM_EXTENSIONS, &max);
  NSMutableSet *extensions = [NSMutableSet set];
  for (int i = 0; i < max; i++) {
     [extensions addObject: @( 
     (char*)glGetStringi(GL_EXTENSIONS, i) )];
  }
  return [extensions containsObject: searchName];
}

To check for OpenGL ES 1.1 and 2.0 extensions, call glGetString(GL_EXTENSIONS) to get a space-delimited list of all extension names.
要检查OpenGL ES 1.1和2.0扩展名,请调用glGetString(GL_EXTENSIONS)获取所有扩展名的空格分隔列表。

Choosing a Rendering Destination(选择渲染目标)

In iOS, a framebuffer object stores the results of drawing commands. (iOS does not implement window-system-provided framebuffers.) You can use the contents of a framebuffer object in multiple ways:

  • The GLKit framework provides a view that draws OpenGL ES content and manages its own framebuffer object, and a view controller that supports animating OpenGL ES content. Use these classes to create full screen views or to fit your OpenGL ES content into a UIKit view hierarchy. To learn about these classes, read Drawing with OpenGL ES and GLKit.
  • The CAEAGLLayer class provides a way to draw OpenGL ES content as part of a Core Animation layer composition. You must create your own framebuffer object when using this class.
  • As with any OpenGL ES implementation, you can also use framebuffers for offscreen graphics processing or rendering to a texture for use elsewhere in the graphics pipeline. With OpenGL ES 3.0, offscreen buffers can be used in rendering algorithms that utilize multiple render targets.

To learn about rendering to an offscreen buffer, a texture, or a Core Animation layer, read Drawing to Other Rendering Destinations.

在iOS中,一个framebuffer对象存储绘制命令的结果。 (iOS不实现窗口系统提供的帧缓冲区。)可以通过多种方式使用framebuffer对象的内容:

  • GLKit框架提供了一个视图,它绘制了OpenGL ES内容并管理自己的framebuffer对象,以及支持动画化OpenGL ES内容的视图控制器。使用这些类创建全屏视图或将OpenGL ES内容适合于UIKit视图层次结构。要了解这些类,请阅读使用OpenGL ES和GLKit绘图。
  • CAEAGLLayer类提供了一种绘制OpenGL ES内容作为Core Animation图层组合的一部分的方法。使用此类时必须创建自己的framebuffer对象。
  • 与任何OpenGL ES实现一样,您还可以使用帧缓冲区进行屏幕外图形处理或渲染到图形管道中其他位置的纹理。使用OpenGL ES 3.0,可以在使用多个渲染目标的渲染算法中使用屏幕外缓冲区。

要了解如何渲染到屏幕外缓冲区,纹理或核心动画层,Drawing to Other Rendering Destinations。

Integrating with iOS(在iOS中集成)

iOS apps support multitasking by default, but handling this feature correctly in an OpenGL ES app requires additional consideration. Improper use of OpenGL ES can result in your app being killed by the system when in the background.
iOS应用程序默认情况下支持多任务处理,但在OpenGL ES应用程序中正确处理此功能需要额外考虑。 OpenGL ES的不当使用可能导致您的应用程序在后台被系统杀死。

Many iOS devices include high-resolution displays, so your app should support multiple display sizes and resolutions.
许多iOS设备包括高分辨率显示器,因此您的应用程序应支持多种显示尺寸和分辨率。

To learn about supporting these and other iOS features, read Multitasking, High Resolution, and Other iOS Features.
要了解支持这些和其他iOS功能,请阅读多任务,高分辨率和其他iOS功能。

Implementing a Rendering Engine(实现渲染引擎)

There are many possible strategies for designing your OpenGL ES drawing code, the full details of which are beyond the scope of this document. Many aspects of rendering engine design are generic to all implementations of OpenGL and OpenGL ES.
设计OpenGL ES绘图代码有许多可能的策略,其全部细节超出了本文档的范围。 OpenGL和OpenGL ES渲染引擎的许多方面都是通用的。

To learn about design considerations important for iOS devices, read OpenGL ES Design Guidelines and Concurrency and OpenGL ES.
要了解iOS设备重要的设计注意事项,请阅读OpenGL ES设计指南、并发和OpenGL ES。

Debugging and Profiling(调试和优化)

Xcode and Instruments provide a number of tools for tracking down rendering problems and analyzing OpenGL ES performance in your app.
Xcode和Instruments为您的应用程序提供了许多用于跟踪渲染问题和分析OpenGL ES性能的工具。

To learn more about solving problems and improving performance in your OpenGL ES app, read Tuning Your OpenGL ES App.
要了解有关解决问题并提高OpenGL ES应用程序性能的更多信息,请参阅调整OpenGL ES应用程序。

logo.png

推荐阅读更多精彩内容