Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two...

43
Open Source Graphics 101: Getting Started Boris Brezillon ELCE 2019

Transcript of Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two...

Page 1: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

Open Source Graphics 101:Getting Started

Boris BrezillonELCE 2019

Page 2: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

2

Disclaimer

● I am not (yet) an experienced Graphics developer– Take my words with a grain of salt

– Please correct me if I’m wrong

Source: https://me.me/i/every-master-was-once-a-beginner-success-foundation-well-said-16284942

Page 3: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

3

What is this talk about?● This presentation is about

– Explaining what GPUs are and how they work

– Providing a brief overview of the Linux Open Source Graphics stack

● This presentation is not about– Teaching you how to develop a GPU driver

– Teaching you how to use Graphics APIs (OpenGL/Vulkan/D3D)

Page 4: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

4

The Graphics Pipeline

Page 5: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

5

The Graphics Pipeline

GPU

Vertices

Textures

...Transformation

Page 6: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

6

The Graphics PipelineVertices

TexturesTransformation

Graphics Pipeline

Geometry Stage

Rasterizer Stage

Viewport Lighting

...Clipping

Page 7: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

7

The Geometry StageModel

Vertices

Vertex Shader

Model, View, Projection Transform

Viewport Transform

... other kind of vertex/normal manipulation

Geometry/Tessalation Shaders

Primitive Assemby

Clipping Culling

Page 8: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

8

The Rasterizer Stage

Geometry stage

Rasterizer stage

Textures

Triangle setup

Fragment shader

Merging stage

Alpha blending Late depth testing

Page 9: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

9

GPU Internals

Page 10: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

10

GPU Internals

Generic Shader

Core

Texture Units

Generic Shader

Core

Generic Shader

Core

Generic Shader

Core

Triangle Setup Units

Rasterizers

Blending Units

...

GPU

Caches

Scheduler

ALUALU

ALU ALU

Load/Store Unit

Load/Store Unit

Page 11: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

11

Let’s go massively parallel!● Why?

– Vertices, normals, fragments can be processed independently

– We have a lot of them (complex scene, complex models, high resolution)

– We want real-time rendering

● How?– SIMD (Single Instruction Multiple Data)

– Shared dedicated units for complex/specialized tasks

– No fancy CPU stuff like out-of-order control logic, smart pre-fetcher, branch

predictors, ...

Page 12: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

12

Parallization, how hard can it be?

SIMD + lot of cores: we’re done, right?

Page 13: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

13

Parallization, how hard can it be?

Source: http://devhumor.com/media/multithreaded-programming

Page 14: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

14

Parallization, how hard can it be?

● Stalls caused by memory access– Add caches

– Multi-threading

● SIMD: try to get all ALUs busy– Avoid conditional branches

– Try to pack similar operation together

Page 15: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

15

Interaction with your GPU

Page 16: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

16

CPU: Hey GPU, listen/talk to me please!

● The CPU is in charge of all apps running on a machine, including graphics apps

● The CPU needs a way to send requests to/get results from the GPU

● Huge amount of data needs to be exchanged (vertices, framebuffers, textures, …)

Page 17: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

17

CPU: Hey GPU, listen/talk to me please!

● How?– Put everything in memory

– Set of operations to execute is also stored in memory (frequently called

command stream)

– Once everything is in memory, ask the GPU to execute what we prepared

– Let the GPU inform us when it’s done

Page 18: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

18

GPU Command Stream

Vextex Job1

Vertices for Model 1

Fragment Job1

Fragment Shader Binary

for Model 1

Vextex Job2 Fragment Job2

Textures for Model 2

Command Stream

Ancillary Data

Ancillary Data

......

Fragment Shader Binary

for Model 2

Textures for Model 1

Vertex Shader Binary

for Model 2

Vertex Shader Binary

for Model 1

Vertices for Model 2

Page 19: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

19

The Linux Graphics Stack

Page 20: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

20

The Big PictureApplication

Graphics API

Kernel

OpenGL VulkanDirect3D

DRM

freedreno panfrost etnaviv ...

Mesa3D

EGLGLX

Userspace Drivers

i915

OpenGL Ecosystem

WSI

Vulkan Ecosystem

Page 21: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

21

The Graphics API: What are they?● Entry points for Graphics Apps/Libs● Abstract the GPU pipeline configuration/manipulation● You might have the choice

– OpenGL/OpenGLES: Well established, well supported and widely used

– Vulkan: Modern API, this is the future, but not everyone uses/supports it yet

– Direct3D: Windows Graphics API (version 12 of the API resembles the

Vulkan API)

Page 22: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

22

The Graphics API: Shaders

● Part of the pipeline is programmable– Separate Programming Language: GLSL or HLSL

– Programs are passed as part of the pipeline configuration...

– ... and compiled by drivers to generate hardware-specific bytecode

Page 23: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

23

The Graphics API: OpenGL(ES) vs Vulkan● Two philosophies:

– OpenGL tries to hide as much as possible the GPU internals

– Vulkan provides fine grained control

– Vulkan provides a way to record operations and replay them

– More work for the developer, less work for the CPU

● Vulkan applications are more verbose, but– Vulkan verbosity can be leveraged by higher-level APIs

– Drivers are simpler

– Improved perfs on CPU-bound workloads

Page 24: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

24

The Kernel/Userspace Driver Separation

● GPUs are complex beasts → drivers are complex too:– We don’t want to put all the complexity kernel side

– Not all code needs to run in a privileged context

– Debugging in userspace is much easier

– Licensing issues (for closed source drivers)

Page 25: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

25

Kernel Drivers● Kernel drivers deal with

– Memory

– Command Stream submission/scheduling

– Interrupts and Signaling

● Kernel drivers interfaces with open-source userspace drivers live

in Linus’ tree: drivers/gpu/drm/

● Kernel drivers interfacing with closed-source userspace drivers

are out-of-tree

Page 26: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

26

Userspace Driver: Roles● Exposing one or several Graphics API

– Maintaining the API specific state machine (if any)

– Managing off-screen rendering contexts (if any)

– Compiling shaders into hardware specific bytecode

– Creating, populating and submitting command streams

● Interacting with the Windowing System– Managing on-screen rendering contexts

– Binding/unbinding render buffers

– Synchronizing on render operations

Page 27: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

27

Mesa: Open Source Userspace Drivers● 2 Graphics APIs 2 different approaches:● GL:

– Mesa provides a frontend for GL APIs (libGL(ES))

– GL Drivers implement the DRI driver interface

– Drivers are shared libs loaded on demand● Vulkan:

– Khronos has its own driver loader (Open Source)

– Mesa just provides Vulkan drivers

– No abstraction for Vulkan drivers, code sharing through libs

Page 28: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

28

Mesa State Tracking: Pre-GalliumApplication

Graphics API

Kernel

OpenGL

DRM

nouveau

Mesa3D

EGLGLX

DRI

i915...nouveau

i915

OpenGL Ecosystem

...

DRI DRI DRIGL Interface

GL Iface

GL Iface

GL dispatcher

Page 29: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

29

Mesa State Tracking: GalliumApplication

Graphics API

Kernel

OpenGL Direct3D

DRM

msmpanfrost etnaviv ...

Mesa3D

EGLGLX

DRI

Gallium3D

State Trackers

Driver Interface

panfrost etnaviv ... freedreno

OpenGL Ecosystem

ninedd_function_table/DRI

GL Dispatcher

Page 30: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

30

Mesa State Tracking: VulkanApplication

Graphics API

Kernel

OpenGL Direct3D

DRM

msmpanfrost etnaviv ...

Mesa3D

EGLGLX

DRI

Gallium3D

State Trackers

Driver Interface

panfrost etnaviv ... freedreno

t u r n i p

a n v

i915

OpenGL Ecosystem

Vulkan

Vulkan Ecosystem

WSI

ninedd_function_table / dri

GL Dispatcher

Page 31: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

31

Conclusion

Page 32: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

32

Nice overview, but what’s next?● The GPU topic is quite vast

● Start small

– Choose a driver

– Find a feature that’s missing or buggy

– Stick to it until you get it working● Getting a grasp on GPU concepts/implementation takes time● Don’t give up

Page 33: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

33

Useful readings● Understanding how GPUs work is fundamental:

– https://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-

pipeline-2011-index/

– https://www.cs.cmu.edu/afs/cs/academic/class/15462-f11/www/lec_slides/

lec19.pdf

– Search ”how GPUs work” on Google ;-)

● Mesa source tree is sometimes hard to follow, refer to the doc: https://mesa-

docs.readthedocs.io/en/latest/sourcetree.html● And the DRM kernel doc can be useful too: https://01.org/linuxgraphics/gfx-

docs/drm/gpu/index.html

Page 34: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

34

Q & AThank you!

(Psst, we’re hiring!)

Page 35: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

35

Backup Slides

Page 36: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

36

Stalls on Memory Accessesinstruction flow

arithmethic instruction

load

store

execution on a GPU core

...

...

Page 37: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

37

Avoid ld/st stalls: Multi-threading

arithmethic instruction

load

store

T1

executing

t

T2

T1 T2

T1 T2

T1 T2

T1 T2

Page 38: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

38

SIMD & Conditional branches: Ouch!

ALU ALU ALU ALU

Instruction Fetch/Decode

ctx ctx ctx ctx

instruction flowALUs

(a x b) + c;

if (x < 0)

a + c;

else

a - b;

SIMD

Page 39: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

39

Kernel Drivers: Memory Management● Two Frameworks

– GEM: Graphics Execution Manager

– TTM: Translation Table Manager

● GPU drivers using GEM– Should provide an ioctl() to allocate Buffer Objects (BOs)

– Releasing BOs is done through a generic ioctl()

– Might provide a way to do cache maintenance operations on a BO

– Should guarantee that BOs referenced by a submitted Command Stream

are properly mapped GPU-side

Page 40: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

40

Kernel Drivers: Scheduling● Submission != Immediate execution

– Several processes might be using the GPU in parallel

– The GPU might already be busy when the request comes in

● Submission == Queue the cmdstream

● Each driver has its own ioctl() for that

● Userspace driver knows inter-cmdstream dependencies

● Scheduler needs to know about those constraints too

● DRM provides a generic scheduling framework: drm_sched

Page 41: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

41

Userspace/Kernel Driver Synchronization● Userspace driver needs to know when the GPU is done executing

a cmdstream

● Hardware reports that through an interrupt

● Information has to be propagated to userspace

● Here come fences: objects allowing one to wait on job

completion

● Exposed as syncobjs objects to userspace

● fences can also be placed on BOs

Page 42: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

42

Mesa: Shader Compilation● Compilation is a crucial aspect● Compilation usually follows the following steps

– Shader Programming Language -> Generic Intermediate Representation

(IR)

– Optimization in the generic IR space

– Generic IR -> GPU specific IR

– Optimization in the GPU specific IR space

– Byte code generation● Note that you can have several layers of generic IR

Page 43: Open Source Graphics 101: Getting Started · 23 The Graphics API: OpenGL(ES) vs Vulkan Two philosophies: – OpenGL tries to hide as much as possible the GPU internals – Vulkan

43

Mesa: Shader CompilationGLSL HLSL

Shader Programming Languages

Intermediate Representations

SPIR-VGLSL IR

NIRTGSI

Driver Compilers

MIR (Midgard IR)

IR3 (Adreno IR)

......NV50 IR

(Nouveau IR)

NV50 bytecode

MIR bytecode

IR3 bytecode