Advances in OpenGL and OpenGL ES - 开放文档 - Free and...

102
These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 513 Advances in OpenGL and OpenGL ES Chris Niederauer GPU Software

Transcript of Advances in OpenGL and OpenGL ES - 开放文档 - Free and...

Page 1: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

Session 513

Advances in OpenGL and OpenGL ES

Chris NiederauerGPU Software

Page 2: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

High-performance renderingOpenGL and OpenGL ES

• Access to vastly powerful GPUs• Broadly used for games, visualization, and entertainment• Foundation of visual technologies in iOS and OS X

Page 3: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

OpenGL and OpenGL ES

Graphics Hardware

Modern OpenGLProgrammable Shaders

Legacy OpenGLFixed Function

Graphics FrameworksGLKit

Page 4: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

OpenGL and OpenGL ES

Graphics Hardware

Modern OpenGLOpenGL ES 2.x

Legacy OpenGLOpenGL ES 1.x

Graphics FrameworksGLKit

iOS

Page 5: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

OpenGL and OpenGL ES

Graphics Hardware

Modern OpenGLOpenGL ES 2.x

Legacy OpenGLOpenGL ES 1.x

Graphics FrameworksGLKit

iOS

Graphics Hardware

Modern OpenGLOpenGL 3.x Core Profile

Legacy OpenGLOpenGL 2.x

Graphics Frameworks

OS X

Page 6: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

OpenGL and OpenGL ES

Graphics Hardware

Modern OpenGLOpenGL ES 2.x

Legacy OpenGLOpenGL ES 1.x

Graphics FrameworksGLKit

iOS

Graphics Hardware

Modern OpenGLOpenGL 3.x Core Profile

Legacy OpenGLOpenGL 2.x

Graphics FrameworksGLKit

OS X

Page 7: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

AgendaIntroduction

• New extensions for iOS• GLKit refresher• High resolution for OS X

Page 8: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Programmable Blending

Allan SchafferGraphics and Game Technologies Evangelist

Page 9: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Programmable graphics pipelineOpenGL ES 2.0

DepthStencil Blend Dither Frame Buffer

PrimitiveProcessing

VertexShader

PrimitiveAssembly

Fragment Shader

Rasterizer

Page 10: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Frame Buffer

Standard blendingOpenGL ES 2.0

BlendFragment Shader

Source Destination(func)

Page 11: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Frame Buffer

Programmable blendingOpenGL ES 2.0

BlendFragment Shader

Source Destination(func)

Page 12: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Frame Buffer

Programmable blendingOpenGL ES 2.0

BlendFragment Shader

Source Destination(func)

Page 13: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

APPLE_shader_framebuffer_fetchFramebuffer Fetch

• gl_LastFragData[0]■ Provides current framebuffer color in fragment shader■ Read-only■ All iOS 6 devices■ Coexists with built-in blending

Page 14: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Example shaderFramebuffer Fetch

#extension GL_APPLE_shader_framebuffer_fetch : requirevarying lowp vec4 color;

void main(){! // GL_ONE, GL_ONE_MINUS_SRC_ALPHA! gl_FragColor = color + (gl_LastFragData[0] * (1.0 - color.a));

or..

// Difference Blend gl_FragColor = abs(color - gl_LastFragData[0]);}

Page 15: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Example shaderFramebuffer Fetch

#extension GL_APPLE_shader_framebuffer_fetch : requirevarying lowp vec4 color;

void main(){! // GL_ONE, GL_ONE_MINUS_SRC_ALPHA! gl_FragColor = color + (gl_LastFragData[0] * (1.0 - color.a));

or..

// Difference Blend gl_FragColor = abs(color - gl_LastFragData[0]);}

Page 16: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Example shaderFramebuffer Fetch

#extension GL_APPLE_shader_framebuffer_fetch : requirevarying lowp vec4 color;

void main(){! // GL_ONE, GL_ONE_MINUS_SRC_ALPHA! gl_FragColor = color + (gl_LastFragData[0] * (1.0 - color.a));

or..

// Difference Blend gl_FragColor = abs(color - gl_LastFragData[0]);}

Page 17: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Example shaderFramebuffer Fetch

#extension GL_APPLE_shader_framebuffer_fetch : requirevarying lowp vec4 color;

void main(){! // GL_ONE, GL_ONE_MINUS_SRC_ALPHA! gl_FragColor = color + (gl_LastFragData[0] * (1.0 - color.a));

or..

// Difference Blend gl_FragColor = abs(color - gl_LastFragData[0]);}

Page 18: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

#extension GL_APPLE_shader_framebuffer_fetch : requirevarying lowp vec4 colorVarying;

// “Hard Light” Blend#define BlendHardLight(color, dest) (color < 0.5 ? \ (2.0 * color * dest) : (1.0 - 2.0 * (1.0 - color) * (1.0 - dest)))!

void main(){ gl_FragColor.r = BlendHardLight(colorVarying.r, gl_LastFragData[0].r); gl_FragColor.g = BlendHardLight(colorVarying.g, gl_LastFragData[0].g); gl_FragColor.b = BlendHardLight(colorVarying.b, gl_LastFragData[0].b); gl_FragColor.a = colorVarying.a;}

Hard light shaderFramebuffer Fetch

Page 19: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

#extension GL_APPLE_shader_framebuffer_fetch : requirevarying lowp vec4 colorVarying;

// “Hard Light” Blend#define BlendHardLight(color, dest) (color < 0.5 ? \ (2.0 * color * dest) : (1.0 - 2.0 * (1.0 - color) * (1.0 - dest)))!

void main(){ gl_FragColor.r = BlendHardLight(colorVarying.r, gl_LastFragData[0].r); gl_FragColor.g = BlendHardLight(colorVarying.g, gl_LastFragData[0].g); gl_FragColor.b = BlendHardLight(colorVarying.b, gl_LastFragData[0].b); gl_FragColor.a = colorVarying.a;}

Hard light shaderFramebuffer Fetch

Page 20: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

#extension GL_APPLE_shader_framebuffer_fetch : requirevarying lowp vec4 colorVarying;

// “Hard Light” Blend#define BlendHardLight(color, dest) (color < 0.5 ? \ (2.0 * color * dest) : (1.0 - 2.0 * (1.0 - color) * (1.0 - dest)))!

void main(){ gl_FragColor.r = BlendHardLight(colorVarying.r, gl_LastFragData[0].r); gl_FragColor.g = BlendHardLight(colorVarying.g, gl_LastFragData[0].g); gl_FragColor.b = BlendHardLight(colorVarying.b, gl_LastFragData[0].b); gl_FragColor.a = colorVarying.a;}

Hard light shaderFramebuffer Fetch

Page 21: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

• Step one: Draw scene• Step two: Draw full-screen quad with shader, e.g.:#extension GL_APPLE_shader_framebuffer_fetch : requirevoid main(){! // RGB to grayscale! mediump float lum = dot(gl_LastFragData[0], vec4(0.30,0.59,0.11,0.0));! gl_FragColor =  vec4(lum, lum, lum, 1.0);}

• More efficient than render-to-texture

Local postprocessing effectsFramebuffer Fetch

Page 22: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

• Step one: Draw scene• Step two: Draw full-screen quad with shader, e.g.:#extension GL_APPLE_shader_framebuffer_fetch : requirevoid main(){! // RGB to grayscale! mediump float lum = dot(gl_LastFragData[0], vec4(0.30,0.59,0.11,0.0));! gl_FragColor =  vec4(lum, lum, lum, 1.0);}

• More efficient than render-to-texture

Local postprocessing effectsFramebuffer Fetch

Page 23: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

• Unique blends using extended GLSL operations■ Advanced blends■ Custom overlays■ Lighting effects

• Using framebuffer RGB values to do a texture lookup■ Color grading/globally modified color

• Blends with noncolor framebuffer data■ Normals, ambient occlusion, etc.

Going furtherProgrammable Blending

Page 24: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Fine-Grain Texture Copy

Page 25: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Target audienceFine-Grain Texture Copy

• Most games: Load all textures■ Only have a few textures■ Only load as many as fits in RAM

Page 26: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Target audienceFine-Grain Texture Copy

• Most games: Load all textures■ Only have a few textures■ Only load as many as fits in RAM

• Some games: Dynamic texture loading■ Load and delete textures between levels, etc.

Page 27: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Target audienceFine-Grain Texture Copy

• Most games: Load all textures■ Only have a few textures■ Only load as many as fits in RAM

• Some games: Dynamic texture loading■ Load and delete textures between levels, etc.

• Few games: Fully dynamic texture management■ Keep a runtime texture “budget”■ Load and delete textures during gameplay■ Wish to have even finer grain control

Page 28: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

ConceptFine-Grain Texture Copy

• For games with dynamic texture management■ Quickly create storage and copy texture contents■ Enables a “texture swap” algorithm

■ Add higher resolution mipmap base level as needed■ Remove mipmap base level to reduce memory footprint

• Enabled by two new extensions■ EXT_texture_storage■ APPLE_texture_copy

Page 29: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

EXT_texture_storageFine-Grain Texture Copy

• Immutable texture object■ Defines all texture properties in a single call■ Memory allocations and completeness checked up front■ Texture data uploaded via glTexSubImage2D

• Supported on all iOS 6 devices

Page 30: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Create and bind texture nameglGenTextures(1, &name);glBindTexture(GL_TEXTURE_2D, name);

// Define & allocate texture storageglTexStorage2DEXT(GL_TEXTURE_2D, num_levels, GL_RGBA8_OES, width, height);

// Load datafor (int level=0; level<num_levels; level++) glTexSubImage2D(GL_TEXTURE_2D, level, ..., data[level]);

EXT_texture_storage usageFine-Grain Texture Copy

Page 31: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Create and bind texture nameglGenTextures(1, &name);glBindTexture(GL_TEXTURE_2D, name);

// Define & allocate texture storageglTexStorage2DEXT(GL_TEXTURE_2D, num_levels, GL_RGBA8_OES, width, height);

// Load datafor (int level=0; level<num_levels; level++) glTexSubImage2D(GL_TEXTURE_2D, level, ..., data[level]);

EXT_texture_storage usageFine-Grain Texture Copy

Page 32: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Create and bind texture nameglGenTextures(1, &name);glBindTexture(GL_TEXTURE_2D, name);

// Define & allocate texture storageglTexStorage2DEXT(GL_TEXTURE_2D, num_levels, GL_RGBA8_OES, width, height);

// Load datafor (int level=0; level<num_levels; level++) glTexSubImage2D(GL_TEXTURE_2D, level, ..., data[level]);

EXT_texture_storage usageFine-Grain Texture Copy

Page 33: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Create and bind texture nameglGenTextures(1, &name);glBindTexture(GL_TEXTURE_2D, name);

// Define & allocate texture storageglTexStorage2DEXT(GL_TEXTURE_2D, num_levels, GL_RGBA8_OES, width, height);

// Load datafor (int level=0; level<num_levels; level++) glTexSubImage2D(GL_TEXTURE_2D, level, ..., data[level]);

EXT_texture_storage usageFine-Grain Texture Copy

Page 34: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

APPLE_copy_texture_levelsFine-Grain Texture Copy

• Fast copy of specified mipmap levels between textures■ Based on dimensions, for example:

■ [1x1] thru [512x512] levels of source copied to[1x1] thru [512x512] levels of destination

• Enables fine-grain memory management of levels• Requires immutable textures (EXT_texture_storage)• Supported on all iOS 6 devices

Page 35: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Fine-Grain Texture Copy

1024x1024 1024x1024 1024x1024

512x512512x512512x512

512x512512x512512x512

512x512

512x512

512x512512x512

512x512 512x512

Page 36: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Fine-Grain Texture Copy

1024x1024 1024x1024 1024x1024

512x512512x512512x512

512x512512x512512x512

512x512

512x512

512x512512x512

512x512 512x512

1024x1024

Page 37: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

1024x10241024x1024

512x512

Fine-Grain Texture Copy

512x512

Page 38: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Create temp textureglGenTextures(1, &temp_texture);glBindTexture(GL_TEXTURE_2D, temp_texture);glTexStorage2DEXT(GL_TEXTURE_2D, 10, GL_RGBA8_OES, 512, 512);

1024x10241024x1024

512x512

Fine-Grain Texture Copy

512x512

Page 39: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Create temp textureglGenTextures(1, &temp_texture);glBindTexture(GL_TEXTURE_2D, temp_texture);glTexStorage2DEXT(GL_TEXTURE_2D, 10, GL_RGBA8_OES, 512, 512);

1024x10241024x1024

512x512 512x512

Fine-Grain Texture Copy

512x512

Page 40: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

1024x10241024x1024

512x512

// Copy 10 “blue” levels starting at level 1 to tempglCopyTextureLevelsAPPLE(temp_texture, blue_texture, 1, 10);

512x512

Fine-Grain Texture Copy

512x512

Page 41: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

1024x10241024x1024

512x512

// Copy 10 “blue” levels starting at level 1 to tempglCopyTextureLevelsAPPLE(temp_texture, blue_texture, 1, 10);

512x512512x512

Fine-Grain Texture Copy

512x512

Page 42: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

1024x10241024x1024

512x512

// Copy 10 “red” levels starting at level 0 to “blue”glCopyTextureLevelsAPPLE(blue_texture, red_texture, 0, 10);

512x512512x512

Fine-Grain Texture Copy

512x512

Page 43: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

1024x10241024x1024

512x512512x512

// Copy 10 “red” levels starting at level 0 to “blue”glCopyTextureLevelsAPPLE(blue_texture, red_texture, 0, 10);

512x512512x512

Fine-Grain Texture Copy

512x512

Page 44: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// upload new “red” level 0 data into “blue”glBindTexture(GL_TEXTURE_2D, blue_texture);glTexSubImage2D(GL_TEXTURE_2D, 0, ... , new_data);

1024x10241024x1024

512x512512x512 512x512512x512

Fine-Grain Texture Copy

512x512

Page 45: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// upload new “red” level 0 data into “blue”glBindTexture(GL_TEXTURE_2D, blue_texture);glTexSubImage2D(GL_TEXTURE_2D, 0, ... , new_data);

1024x10241024x1024

512x512512x512

1024x1024

512x512512x512

Fine-Grain Texture Copy

512x512

Page 46: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Delete “red” or keep for later re-use as tempglDeleteTextures(red_texture);

1024x10241024x1024

512x512512x512

1024x1024

512x512512x512

Fine-Grain Texture Copy

512x512

Page 47: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Delete “red” or keep for later re-use as tempglDeleteTextures(red_texture);

1024x10241024x1024

512x512512x512

1024x1024

512x512512x512

Fine-Grain Texture Copy

Page 48: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Fast VBO Updates

Page 49: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Vertex Buffer Objects are essentialVertex Buffer Objects

• Fundamental method of defining geometry■ Vertices, normals, colors, texture coordinates

• High performance■ Buffer object data directly addressable by GPU

• Supported on all devices■ Required by Core Profile on OS X

Page 50: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Typical exampleDynamic VBO Updates

glBindBuffer(GL_ARRAY_BUFFER, vbo_positions); void *data = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY); memcpy(&data[offset], new_data, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

OpenGL ES

Buffer Objects

VBO Positions

VBO TexCoords

App

App memory

Page 51: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Typical exampleDynamic VBO Updates

glBindBuffer(GL_ARRAY_BUFFER, vbo_positions); void *data = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY); memcpy(&data[offset], new_data, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

OpenGL ES

Buffer Objects

VBO PositionsVBO Positions

VBO TexCoords

App

App memory

Page 52: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Typical exampleDynamic VBO Updates

glBindBuffer(GL_ARRAY_BUFFER, vbo_positions); void *data = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY); memcpy(&data[offset], new_data, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

OpenGL ES

Buffer Objects

VBO PositionsVBO Positions

VBO TexCoords

App

App memory

void *data

Page 53: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Typical exampleDynamic VBO Updates

glBindBuffer(GL_ARRAY_BUFFER, vbo_positions); void *data = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY); memcpy(&data[offset], new_data, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

OpenGL ES

Buffer Objects

VBO PositionsVBO PositionsVBO Positions

VBO TexCoords

App

App memory

void *data

Page 54: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Typical exampleDynamic VBO Updates

glBindBuffer(GL_ARRAY_BUFFER, vbo_positions); void *data = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY); memcpy(&data[offset], new_data, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

OpenGL ES

Buffer Objects

VBO PositionsVBO PositionsVBO Positions

VBO TexCoords

App

App memory

Page 55: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Two issuesDynamic VBO Updates

• Map can force GPU to sync with CPU■ CPU waits for draw to finish before update

• Unmap requires CPU memory caches to be flushed■ To ensure all modifications visible to GPU

OpenGL ES

Buffer Objects

VBO PositionsVBO PositionsVBO Positions

VBO TexCoords

App

App memory

void *data

Page 56: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

APPLE_map_buffer_range and APPLE_syncFast VBO Updates

• APPLE_map_buffer_range ■ Provides explicit sub-range flushing

■ Specify subrange that’s been modified■ Identifies data to be flushed

■ Enables asynchronous buffer modification

• APPLE_sync■ Provides sync object■ Know when commands are completed

• Supported on all iOS 6 devices

Page 57: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind vertex positions bufferglBindBuffer(GL_ARRAY_BUFFER, vbo_positions);

// Get pointer to exact sub-range to modifyvoid *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE );

// Copy new datamemcpy(data, new_data, length);

// Flush mapped sub-range and un-mapglFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

APPLE_map_buffer_range exampleFast VBO Updates

Page 58: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind vertex positions bufferglBindBuffer(GL_ARRAY_BUFFER, vbo_positions);

// Get pointer to exact sub-range to modifyvoid *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE );

// Copy new datamemcpy(data, new_data, length);

// Flush mapped sub-range and un-mapglFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

APPLE_map_buffer_range exampleFast VBO Updates

Page 59: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind vertex positions bufferglBindBuffer(GL_ARRAY_BUFFER, vbo_positions);

// Get pointer to exact sub-range to modifyvoid *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE );

// Copy new datamemcpy(data, new_data, length);

// Flush mapped sub-range and un-mapglFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

APPLE_map_buffer_range exampleFast VBO Updates

Page 60: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind vertex positions bufferglBindBuffer(GL_ARRAY_BUFFER, vbo_positions);

// Get pointer to exact sub-range to modifyvoid *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE );

// Copy new datamemcpy(data, new_data, length);

// Flush mapped sub-range and un-mapglFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

APPLE_map_buffer_range exampleFast VBO Updates

Page 61: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind vertex positions bufferglBindBuffer(GL_ARRAY_BUFFER, vbo_positions);

// Get pointer to exact sub-range to modifyvoid *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE );

// Copy new datamemcpy(data, new_data, length);

// Flush mapped sub-range and un-mapglFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

APPLE_map_buffer_range exampleFast VBO Updates

Page 62: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind and Map bufferglBindBuffer(GL_ARRAY_BUFFER, this_vbo);void *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE );

// Modify buffer, flush, unmapmemcpy(data, new_data, length);glFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

// Issue draw commandsdrawThisVBO(this_vbo);

Fast VBO Updates

Page 63: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind and Map bufferglBindBuffer(GL_ARRAY_BUFFER, this_vbo);void *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE );

// Modify buffer, flush, unmapmemcpy(data, new_data, length);glFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

// Issue draw commandsdrawThisVBO(this_vbo);

Fast VBO Updates

Page 64: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind and Map bufferglBindBuffer(GL_ARRAY_BUFFER, this_vbo);void *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE | GL_MAP_UNSYNCHRONIZED_BIT_APPLE );

// Modify buffer, flush, unmapmemcpy(data, new_data, length);glFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

// Issue draw commands drawThisVBO(this_vbo);

Fast VBO Updates

Page 65: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// Bind and Map bufferglBindBuffer(GL_ARRAY_BUFFER, this_vbo);void *data = glMapBufferRangeAPPLE(GL_ARRAY_BUFFER, offset, length, GL_MAP_WRITE_BIT_APPLE | GL_MAP_FLUSH_EXPLICIT_BIT_APPLE | GL_MAP_UNSYNCHRONIZED_BIT_APPLE );

// Wait for fence (set below) before modifying bufferglClientWaitSyncAPPLE(fence, GL_SYNC_FLUSH_COMMANDS_BIT_APPLE, GL_TIMEOUT_IGNORED_APPLE);

// Modify buffer, flush, unmapmemcpy(data, new_data, length);glFlushMappedBufferRangeAPPLE(GL_ARRAY_BUFFER, 0, length); success = glUnmapBufferOES(GL_ARRAY_BUFFER);

// Issue draw commands, then insert fence drawThisVBO(this_vbo);fence = glFenceSyncAPPLE(GL_SYNC_GPU_COMMANDS_COMPLETE_APPLE, 0);

Fast VBO Updates

Page 66: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Fast nonblocking VBO updatesGoing Further

• Tuning OpenGL ES Games■ iOS Tech Talk 2011■ Creating combined arrays and flattening transformations in 2D games

■ http://developer.apple.com/videos/iOS

Page 67: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

GLKit

Page 68: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer
Page 69: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer
Page 70: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

• Create modern GL apps easily

• Move to the shader pipeline

• Gateway to more complex effects

• Provided for iOS and OS X

Page 71: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Great visual effects with minimal effortGLKEffects

• GLKBaseEffect■ Bridge from fixed-function vertex and fragment processing

■ Mimics fixed-function lighting, textures, transformations, etc.■ Uses ES 2.0 shader pipeline on iOS■ Uses Core Profile on OS X

• GLKReflectionMapEffect■ Cube map reflection class

• GLKSkyboxEffect■ Textured backdrop enclosing scene

Page 72: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

OverviewGLKView and GLKViewController

• GLKView■ Provides OpenGL ES compatible view■ Easy renderbuffer setup■ Straightforward draw methods

• GLKViewController■ Provides render loop synchronized to display updates■ Handles device orientation changes■ Pause when transitioning to background■ Frame timing information

• OS X: NSOpenGLView

Page 73: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

OverviewGLKTextureLoader

• Texture loading made simple■ Many common image formats: PNG, JPEG, TIFF, etc.

• Flexible■ Load textures from file, URL, NSData, CGImageRef■ 2D or cubemap textures■ Synchronous or asynchronous loading

• Convenient loading options■ Generate mipmaps■ Flip origin■ Premultiply alpha

Page 74: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

3D graphics math libraryGLKMath

• Over 175 math functions■ 2, 3, and 4 component vectors■ 4x4 and 3x3 matrix type■ Quaternions

• High performance, C-based, inline■ iOS: Scalar and NEON implementations■ OS X: Intel SSE-optimized

• Matrix stack library■ Easily maintain projection and modelview matrix stack■ Simplify migration

Page 75: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

GLKEffects

Harnessing GLKit and OpenGL ES■ http://developer.apple.com/videos/ios/

Going Further

GLKView and GLKViewController GLKTextureLoader GLKMath

Page 76: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Supporting Retina Displays

Page 77: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Supporting Retina Displays

Page 78: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Supporting Retina DisplaysVery similar approaches

Page 79: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Supporting Retina DisplaysVery similar approaches

iOS

Tune performance

Set scale factor

Allocate color buffer, depth, etc.

Setup viewport

Render

Core Animation scales

Page 80: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Supporting Retina DisplaysVery similar approaches

iOS

Tune performance

Set scale factor

Allocate color buffer, depth, etc.

Setup viewport

Render

Core Animation scales

OS X

Tune performance

Enable Retina support

Allocate color buffer, depth, etc.

Setup viewport

Render to FBO

Blit/scale FBO

Render to full-resolution

buffers

Page 81: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// NSOpenGLView subclass- (id) initWithFrame:(NSRect)rect{ // Enable Retina support [self setWantsBestResolutionOpenGLSurface:YES]; ... return self;}

Enable Retina supportSupporting Retina Displays

Page 82: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

// NSOpenGLView subclass- (id) initWithFrame:(NSRect)rect{ // Enable Retina support [self setWantsBestResolutionOpenGLSurface:YES]; ... return self;}

Enable Retina supportSupporting Retina Displays

Page 83: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

- (void)drawRect:(NSRect)rect // NSOpenGLView subclass{ // Get view dimensions in pixels NSRect backingBounds = [self convertRectToBacking:[self bounds]];

GLsizei backingPixelWidth = (GLsizei)(backingBounds.size.width), backingPixelHeight = (GLsizei)(backingBounds.size.height);

// Set viewport glViewport(0, 0, backingPixelWidth, backingPixelHeight);! // draw…}

Set up the viewport and drawSupporting Retina Displays

Page 84: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

- (void)drawRect:(NSRect)rect // NSOpenGLView subclass{ // Get view dimensions in pixels NSRect backingBounds = [self convertRectToBacking:[self bounds]];

GLsizei backingPixelWidth = (GLsizei)(backingBounds.size.width), backingPixelHeight = (GLsizei)(backingBounds.size.height);

// Set viewport glViewport(0, 0, backingPixelWidth, backingPixelHeight);! // draw…}

Set up the viewport and drawSupporting Retina Displays

Page 85: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

- (void)drawRect:(NSRect)rect // NSOpenGLView subclass{ // Get view dimensions in pixels NSRect backingBounds = [self convertRectToBacking:[self bounds]];

GLsizei backingPixelWidth = (GLsizei)(backingBounds.size.width), backingPixelHeight = (GLsizei)(backingBounds.size.height);

// Set viewport glViewport(0, 0, backingPixelWidth, backingPixelHeight);! // draw…}

Set up the viewport and drawSupporting Retina Displays

Page 86: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Set up the viewport and drawSupporting Retina Displays

• Check for calls defined in pixel dimensionsglViewport (GLint x, GLint y, GLsizei width, GLsizei height)glScissor (GLint x, GLint y, GLsizei width, GLsizei height)glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, ...)glLineWidth (GLfloat width)glRenderbufferStorage (..., GLsizei width, GLsizei height)

• Use higher resolution assetsglTexImage2D (..., GLsizei width, GLsizei height, ...)

Page 87: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Blit FBO to backing bufferSupporting Retina Displays

-(void)presentFBO:(GLint)fbo width:(GLint)fbWidth height:(GLint)fbHeight {

// Drawing finished, bind FBO for reading and back buffer for writing glBindFramebuffer(GL_READ_FRAMEBUFFER, fboName); glReadBuffer(GL_COLOR_ATTACHMENT0); // Source from 1st attachment

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffer(GL_BACK); // Copy into drawable backing

glBlitFramebuffer( 0, 0, fbWidth, fbHeight, 0, 0, drawableWidth, drawableHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR );

[[oglView openGLContext] flushBuffer]; // Swap onscreen glDrawBuffer(GL_COLOR_ATTACHMENT0); // Optional}

Page 88: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Blit FBO to backing bufferSupporting Retina Displays

-(void)presentFBO:(GLint)fbo width:(GLint)fbWidth height:(GLint)fbHeight {

// Drawing finished, bind FBO for reading and back buffer for writing glBindFramebuffer(GL_READ_FRAMEBUFFER, fboName); glReadBuffer(GL_COLOR_ATTACHMENT0); // Source from 1st attachment

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffer(GL_BACK); // Copy into drawable backing

glBlitFramebuffer( 0, 0, fbWidth, fbHeight, 0, 0, drawableWidth, drawableHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR );

[[oglView openGLContext] flushBuffer]; // Swap onscreen glDrawBuffer(GL_COLOR_ATTACHMENT0); // Optional}

Page 89: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Blit FBO to backing bufferSupporting Retina Displays

-(void)presentFBO:(GLint)fbo width:(GLint)fbWidth height:(GLint)fbHeight {

// Drawing finished, bind FBO for reading and back buffer for writing glBindFramebuffer(GL_READ_FRAMEBUFFER, fboName); glReadBuffer(GL_COLOR_ATTACHMENT0); // Source from 1st attachment

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffer(GL_BACK); // Copy into drawable backing

glBlitFramebuffer( 0, 0, fbWidth, fbHeight, 0, 0, drawableWidth, drawableHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR );

[[oglView openGLContext] flushBuffer]; // Swap onscreen glDrawBuffer(GL_COLOR_ATTACHMENT0); // Optional}

Page 90: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Blit FBO to backing bufferSupporting Retina Displays

-(void)presentFBO:(GLint)fbo width:(GLint)fbWidth height:(GLint)fbHeight {

// Drawing finished, bind FBO for reading and back buffer for writing glBindFramebuffer(GL_READ_FRAMEBUFFER, fboName); glReadBuffer(GL_COLOR_ATTACHMENT0); // Source from 1st attachment

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffer(GL_BACK); // Copy into drawable backing

glBlitFramebuffer( 0, 0, fbWidth, fbHeight, 0, 0, drawableWidth, drawableHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR );

[[oglView openGLContext] flushBuffer]; // Swap onscreen glDrawBuffer(GL_COLOR_ATTACHMENT0); // Optional}

Page 91: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Blit FBO to backing bufferSupporting Retina Displays

-(void)presentFBO:(GLint)fbo width:(GLint)fbWidth height:(GLint)fbHeight {

// Drawing finished, bind FBO for reading and back buffer for writing glBindFramebuffer(GL_READ_FRAMEBUFFER, fboName); glReadBuffer(GL_COLOR_ATTACHMENT0); // Source from 1st attachment

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffer(GL_BACK); // Copy into drawable backing

glBlitFramebuffer( 0, 0, fbWidth, fbHeight, 0, 0, drawableWidth, drawableHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR );

[[oglView openGLContext] flushBuffer]; // Swap onscreen glDrawBuffer(GL_COLOR_ATTACHMENT0); // Optional}

Page 92: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Tune performanceSupporting Retina Displays

• Strategies for handling increased fragment workload■ Tune: Render at 2x, optimize fragment shaders■ Experiment: Render at 1x and enable anti-aliasing■ Iterate: Try fractional sizes between 1x and 2x while optimizing shaders

Page 93: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

• Advanced topics■ Scaling anti-aliased FBO■ Handling multiple displays■ Responding to resolution changes

• See GLFullScreen sample• Get started today with /Applications/Graphics Tools/Quartz Debug.app

Going furtherSupporting Retina Displays

Page 94: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Wrap Up

Page 95: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Summary

• APPLE_shader_framebuffer_fetch• EXT_texture_storage• APPLE_copy_texture_levels• APPLE_map_buffer_range• APPLE_sync• GLKit for iOS and OS X• Supporting Retina displays

Page 96: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

More Information

Allan SchafferGraphics and Game Technologies [email protected]

Apple Developer Forumshttp://devforums.apple.com/

Tuning OpenGL ES Gameshttp://developer.apple.com/videos/ios/

Harnessing GLKit and OpenGL ES http://developer.apple.com/videos/ios/

GLFullScreenhttp://developer.apple.com/library/mac/

Page 97: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

Advanced Tips and Tricks for High Resolution on OS X MissionFriday 10:15AM

Adopting OpenCL in Your Application Nob HillThursday 4:30PM

Related Sessions

OpenGL ES Tools and Techniques Pacific HeightsWednesday 3:15PM

Page 98: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

OpenGL ES Lab Graphics, Media, & Games Lab AThursday 9:00AM

OpenGL Lab Graphics, Media, & Games Lab BThursday 9:00AM

Labs

Page 99: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer
Page 100: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

The last 3 slides after the logo are intentionally left blank for all presentations.

Page 101: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

The last 3 slides after the logo are intentionally left blank for all presentations.

Page 102: Advances in OpenGL and OpenGL ES - 开放文档 - Free and ...docs.huihoo.com/.../2012/session_513__advances_in_opengl_and_op… · Advances in OpenGL and OpenGL ES Chris Niederauer

The last 3 slides after the logo are intentionally left blank for all presentations.