Metal. GPU-accelerated advanced 3D graphics rendering and data-parallel computation. source rebelsmarket.com

Similar documents
Metal for OpenGL Developers

Working with Metal Overview

Working With Metal Advanced

Metal for Ray Tracing Acceleration

Rendering Objects. Need to transform all geometry then

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

COMP371 COMPUTER GRAPHICS

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Dave Shreiner, ARM March 2009

Shaders. Slide credit to Prof. Zwicker

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

CS559 Computer Graphics Fall 2015

What s New in Metal, Part 2

OPENGL RENDERING PIPELINE

Computer Graphics Coursework 1

The Application Stage. The Game Loop, Resource Management and Renderer Design

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

Lecture 9(B): GPUs & GPGPU

Introducing Metal 2. Graphics and Games #WWDC17. Michal Valient, GPU Software Engineer Richard Schreyer, GPU Software Engineer

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

GPU Memory Model. Adapted from:

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Bringing AAA graphics to mobile platforms. Niklas Smedberg Senior Engine Programmer, Epic Games

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

Windowing System on a 3D Pipeline. February 2005

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Copyright Khronos Group Page 1

Shaders (some slides taken from David M. course)

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

CS451Real-time Rendering Pipeline

Spring 2009 Prof. Hyesoon Kim

CS 432 Interactive Computer Graphics

Spring 2011 Prof. Hyesoon Kim

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

OpenGL ES for iphone Games. Erik M. Buck

Lecture 13: OpenGL Shading Language (GLSL)

Programming shaders & GPUs Christian Miller CS Fall 2011

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

Coding OpenGL ES 3.0 for Better Graphics Quality

Homework 3: Programmable Shaders

Ciril Bohak. - INTRODUCTION TO WEBGL

Programmable GPUs Outline

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

The Rasterization Pipeline

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Mobile graphics API Overview

Introduction to OpenGL. CS 4620 Balazs Kovacs, 2014 Daniel Schroeder, 2013 Pramook Khungurn, 2012

Motivation Hardware Overview Programming model. GPU computing. Part 1: General introduction. Ch. Hoelbling. Wuppertal University

SIGGRAPH Briefing August 2014

Understanding M3G 2.0 and its Effect on Producing Exceptional 3D Java-Based Graphics. Sean Ellis Consultant Graphics Engineer ARM, Maidenhead

Could you make the XNA functions yourself?

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Cornell University CS 569: Interactive Computer Graphics. Introduction. Lecture 1. [John C. Stone, UIUC] NASA. University of Calgary

Understanding Shaders and WebGL. Chris Dalton & Olli Etuaho

OpenGL Programmable Shaders

Real - Time Rendering. Pipeline optimization. Michal Červeňanský Juraj Starinský

Mali & OpenGL ES 3.0. Dave Shreiner Jon Kirkham ARM. Game Developers Conference 27 March 2013

CSE 167: Lecture #8: Lighting. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

Introduction to Shaders.

Project 1, 467. (Note: This is not a graphics class. It is ok if your rendering has some flaws, like those gaps in the teapot image above ;-)

Copyright Khronos Group, Page Graphic Remedy. All Rights Reserved

CS8803SC Software and Hardware Cooperative Computing GPGPU. Prof. Hyesoon Kim School of Computer Science Georgia Institute of Technology

Direct Rendering. Direct Rendering Goals

Metal Shading Language for Core Image Kernels

Vulkan C++ Markus Tavenrath, Senior DevTech Software Engineer Professional Visualization

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Next Generation OpenGL Neil Trevett Khronos President NVIDIA VP Mobile Copyright Khronos Group Page 1

BCA611 Video Oyunları için 3B Grafik

EECS 487: Interactive Computer Graphics

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

2.11 Particle Systems

NVIDIA Parallel Nsight. Jeff Kiel

Graphics Pipeline 2D Geometric Transformations

E6895 Advanced Big Data Analytics Lecture 8: GPU Examples and GPU on ios devices

Profiling and Debugging Games on Mobile Platforms

Graphics Hardware. Instructor Stephen J. Guy

CS 4620 Program 3: Pipeline

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

CS 130 Final. Fall 2015

Computer graphics 2: Graduate seminar in computational aesthetics

GeForce3 OpenGL Performance. John Spitzer

Core Graphics and OpenGL ES. Dr. Sarah Abraham

Animation Essentially a question of flipping between many still images, fast enough

Vulkan and Animation 3/13/ &height=285&playerId=

The Graphics Pipeline and OpenGL I: Transformations!

DirectX 11 First Elements

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

CSE 167: Introduction to Computer Graphics Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Copyright Khronos Group Page 1. Vulkan Overview. June 2015

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

PowerVR Series5. Architecture Guide for Developers

Transcription:

Metal GPU-accelerated advanced 3D graphics rendering and data-parallel computation source rebelsmarket.com

Maths The heart and foundation of computer graphics source wallpoper.com

Metalmatics There are no computer graphics without mathematics Rasterized 3D graphics requires algebra Vector operations Matrix operations Interpolation techniques

My first triangle

Why triangles? Easiest surface that can be formed by straight lines Other surfaces can be composed by a sequence of triangles http://en.wikipedia.org/wiki/ Polygon_mesh source wikipedia.org

3D graphics pipeline Draw Call Vertex Function Rasterization Fragment Function Screen

Draw Call A collection of states to render our triangle Vertices Uniform data Vertex Function Fragment Function

Vertex (Vertices) Structure that holds all the information of a triangle s corner For our first triangle Position: x, y Color: red, green, blue

Vertex (Vertices) struct Vertex { 0, 1 float x, y; float red, green, blue; }; Vertex triangle[3] = { { -1.0, -1.0, 1.0, 0.0, 0.0 }, { 1.0, -1.0, 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0, 1.0 } }; -1, -1 1, -1

Coordinate System Metal uses a right handed coordinate system Positive X axis points right +y Positive Y axis points up Positive Z axis point forward +x Use your right hand for visualization http://en.wikipedia.org/wiki/coordinate_system +z

Uniform data A structure that holds all data that is the same per draw call For our triangle it is a rotation around the z-axis by angle α struct Uniforms { }; float_matrix_4x4 transformation; Uniforms uniform = { cos(a), sin(a), 0.0, 0.0, -sin(a), cos(a), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };

Transformation matrices All transformations for 3D math can be expressed by a 4 4 matrix easy to create fast to apply (to each vertex) http://en.wikipedia.org/wiki/transformation_matrix

CGAffineTransform Representation of a 2 3 matrix for 2D projection CGAffineTransformIdentity CGAffineTransformMakeRotation(angle) CGAffineTransformMakeScale(x, y) CGAffineTransformMakeTranslation(x, y)

Demo Yes, it is Comic Sans

Vertex Function Handles a single vertex Called for each vertex in the draw call Transforms the position to the final position Optionally passes other uniforms to the fragment function

Vertex Function struct Output { float4 position; float3 color; }; Output vertexfunction(vertex v, Uniforms u) { Output o; o.position = u.transform * v.position; o.color = v.color; return o; }

Rasterization Converts the transformed positions into fragments A fragment is a future pixel Interpolates the other vertex values (color) http://en.wikipedia.org/wiki/rasterisation

Fragment Function Handles a single fragment Called once per fragment Converts the fragment into a pixel on the screen

Fragment Function // Output of the Vertex Function without position struct Input { float3 color; }; float3 fragmentfunction(input i) { float3 r; r = 1.0 - i.color; return r; }

Homework Write your own software rasterizer

API

What is Apple s Metal API? The Metal framework supports GPU-accelerated advanced 3D graphics rendering and data-parallel computation workloads. Metal provides a modern and streamlined API for fine-grain, low-level control of the organization, processing, and submission of graphics and computation commands and the management of the associated data and resources for these commands. A primary goal of Metal is to minimize the CPU overhead necessary for executing these GPU workloads. Metal Programming Guide

Apple s Graphics APIs Is there room left for more graphics APIs? CoreGraphics / Quartz CoreImage CoreText OpenGL / OpenGLES SpriteKit (2D) / SceneKit (3D)

Metal features Designed for performance Reduce CPU overhead while maximizing the GPU performance 3D raster graphics pipeline Parallel computing pipeline Replaces OpenCL (was never public on ios)

Metal requirements ios8 only Mac support would require unified RAM ARM64 only iphone 5s, ipad Air, ipad mini with Retina Screen Device only no simulator support Xcode 6

Metal API Quite an unusual API for an Apple framework 1 Function 23 Classes (all helpers) 18 Protocols (core) a few enumerations and type definitions

MTLDevice First step is to get the GPU id<mtldevice> device = MTLCreateSystemDefaultDevice();

CAMetalLayer Metal will draw to a layer, there is no UIMetalView #import <QuartzCore/CAMetalLayer.h> CAMetalLayer *layer = [CAMetalLayer layer]; layer.device = device; layer.pixelformat = MTLPixelFormatBGRA8Unorm; layer.frame = self.view.layer.bounds; [self.view.layer addsublayer:layer];

CAMetalDrawable Request a drawable to render into Might return nil when rendering too fast id<cametaldrawable> drawable = layer.nextdrawable;

MTLBuffer A buffer contains data that is shared between CPU and GPU You are responsible for synchronized access! id<mtlbuffer> vertexbuffer = [device newbufferwithbytes:vertices length:sizeof(vertices) options:mtlresourceoptioncpucachemodedefault]; id<mtlbuffer> uniformbuffer = [device newbufferwithbytes:uniforms length:sizeof(uniforms) options:mtlresourceoptioncpucachemodedefault];

MTLLibrary A library is containing all the shaders (vertex and fragment functions) There might be multiple libraries id<mtllibrary> library = [device newdefaultlibrary];

MTLFunction MTLFunction (shader) is code that is executed on the GPU. id<mtlfunction> vertexfunction = [library newfunctionwithname:@"vertexfunction"]; id<mtlfunction> fragmentfunction = [library newfunctionwithname:@"fragmentfunction"];

MTLCommandQueue Command Queues execute Command Buffers in serial order id<mtlcommandqueue> commandqueue = [device newcommandqueue];

MTLRenderPassDescriptor Describes the buffer use to render to MTLRenderPassDescriptor *renderpass = [MTLRenderPassDescriptor renderpassdescriptor]; renderpass.colorattachments[0].texture = drawable.texture; renderpass.colorattachments[0].loadaction = MTLLoadActionClear; renderpass.colorattachments[0].clearcolor = MTLClearColorMake(1.0f, 1.0f, 1.0f, 1.0f); renderpass.colorattachments[0].storeaction = MTLStoreActionStore;

MTLRenderPipelineDescriptor Describes the pipeline that is used for rendering Should be prepared in the setup phase MTLRenderPipelineDescriptor *pipelinedesc = [[MTLRenderPipelineDescriptor alloc] init]; [pipelinedesc setvertexfunction:vertexfunction]; [pipelinedesc setfragmentfunction:fragmentfunction]; id<mtlrenderpipelinestate> pipeline = [device newrenderpipelinestatewithdescriptor:pipelinedesc error:nil];

MTLCommandBuffer Command Buffers hold series of Command Encoders id<mtlcommandbuffer> commandbuffer = [commandqueue commandbuffer]; // fill with Command Encoders (next slide) [commandbuffer addcompletedhandler: ]; [commandbuffer presentdrawable:drawable]; [commandbuffer commit];

MTLCommandEncoder Command Encoders hold a sequence of GPU instructions id<mtlrendercommandencoder> commandencoder = [commandbuffer rendercommandencoderwithdescriptor:renderpass]; [commandencoder setrenderpipelinestate:pipeline]; [commandencoder setvertexbuffer:vertices offset:0 atindex:0]; [commandencoder setvertexbuffer:uniforms offset:0 atindex:1]; [commandencoder drawprimitives:mtlprimitivetypetriangle vertexstart:0 vertexcount:3 instancecount:1]; [commandencoder endencoding];

Demo

LANGUAGE

Metal Shading Language The shading language is C++11 Custom standard library Math libraries Additional keywords

Restrictions Some C++11 features are missing new and delete operator Exceptions Lambdas Exceptions virtual functions

Benefits Shared code between CPU and GPU Helpful for vertex and uniforms structs Compiled with the clang toolchain Precompiled at compile time

Shared types typedef struct { matrix_float4x4 transform; } Uniforms; typedef struct { packed_float2 position; packed_float3 color; } Vertex; typedef struct { float4 position [[ position ]]; half4 color; } Varyings;

Vertex Function vertex Varyings vertexfunction( device Vertex* vertex_array [[ buffer(0) ]], constant Uniforms& uniforms [[ buffer(1) ]], unsigned int vid [[ vertex_id ]] ) { Varyings out; Vertex v = vertex_array[vid]; float4 position = float4(v.position, 0.0, 1.0); out.position = uniforms.transform * position; out.color = half4(half3(v.color), 1.0); } return out;

Fragment Function fragment half4 fragmentfunction( Varyings in [[ stage_in ]] ) { return 1.0 - in.color; }

Kernel Function kernel void kernelfunction( const device float *in0 [[ buffer(0) ]], const device float *in1 [[ buffer(1) ]], device float *out [[ buffer(2) ]], uint id [[ thread_position_in_grid ]] ) { out[id] = in0[id] + in1[id]; }

Demo

TOOLS

GPU Frame Debugger Inspect the state of the pipeline Live edit shaders View buffers and textures Also works for OpenGL ES on ios

Demo

Metal vs OpenGL source flickr.com

OpenGL Problems State validation done once at setup Buffer copies no copies, but programmer is responsible for synchronizing Runtime shader compilation can be done at compile time Black screen random effects

When to use Metal Indirectly when using a 3D engine Unity, Unreal, (SpriteKit, SceneKit) Custom high-performance 3D engine Parallel computation No OpenCL, only alternative is misusing OpenGL

source rebelsmarket.com