Siggraph 2016 - Vulkan and nvidia : the essentials

73
Siggraph 2016 Tristan Lorach Manager of Developer Technology Group, NVIDIA US 7/25/2016 VULKAN AND NVIDIA: THE ESSENTIALS

Transcript of Siggraph 2016 - Vulkan and nvidia : the essentials

Page 1: Siggraph 2016 - Vulkan and nvidia : the essentials

Siggraph 2016

Tristan Lorach Manager of Developer Technology Group, NVIDIA US

7/25/2016

VULKAN AND NVIDIA:THE ESSENTIALS

Page 2: Siggraph 2016 - Vulkan and nvidia : the essentials

2

ANALOGY ON GRAPHIC APIS(getting ready for my 7 years old son’s questions on my job…)

Lego Kit Derby KitCar Toy

Page 3: Siggraph 2016 - Vulkan and nvidia : the essentials

3

AnalogyDifferent Valid Approaches

Fixed-function OpenGL Modern AZDO OpenGL withProgrammable Shaders

Vulkan

(adult supervision required!)(booring…) (cool... Messes-up the bedroom)

Page 4: Siggraph 2016 - Vulkan and nvidia : the essentials

4

WHAT IS VULKAN ?…It’s a modern API• Designed and maintained by Khronos Group

• Designed for high performance on rendering and compute

• [Extremely] low level : no more “baby-sitting” from our driver

• Manage yourself memory, resource updates; batching; scheduling…

• [Extremely] verbose : Lots of structures to fill with parameters

• close to DX12 design…

• Opposite of OpenGL: Multi-threading friendly : Vulkan will especially shine if multi-threading used

• But still generic enough to work on many HW vendors & platforms

Page 5: Siggraph 2016 - Vulkan and nvidia : the essentials

5

Beneficial Vulkan Scenarios

Is your graphicswork CPU bound?

Can your graphicscreation be parallelized?

start

yes

Vulkanfriendly

yes

Your graphicsplatform is fixed

You’lldo whatever

it takes to squeeze outMax perf.

You put a premium on

avoidinghitches

You canmanage your

graphics resourceallocations

yes

yes

yes

yes

Page 6: Siggraph 2016 - Vulkan and nvidia : the essentials

6

Beneficial Vulkan Scenarios

Is your graphicswork CPU bound?

Can your graphicscreation be parallelized?

start

yes

Vulkanfriendly

yes

Your graphicsplatform is fixed

You’lldo whatever

it takes to squeeze outMax perf.

You put a premium on

avoidinghitches

You canmanage your

graphics resourceallocations

yes

yes

yes

yes

Tired with OpenGL(state-machine)or even D3D ?

Want to learn new stuff ?Spend lots of time coding ?

No sleep ?

Kinda…

(it’s a Yes)

Alright…

(Yes)

Page 7: Siggraph 2016 - Vulkan and nvidia : the essentials

7

Unlikely to BenefitScenarios to Reconsider Coding to Vulkan

1. Need for compatibility to pre-Vulkan platforms

2. Heavily GPU-bound application

3. Heavily CPU-bound application due to non-graphics work

4. Single-threaded application, unlikely to change

5. App can target middle-ware engine, avoiding 3D graphics API dependencies

• Consider using an engine targeting Vulkan, instead of dealing with Vulkan yourself

Good News in any case: NVIDIA OpenGL driver is great and will always be there !

Page 8: Siggraph 2016 - Vulkan and nvidia : the essentials

8

memory GPU

BIG PICTURE –OPENGL CASE

Vertex Puller (IA)

Vertex ShaderTCS

(Tessellation)TES

(Tessellation)

Tessellator

Geometry ShaderTransform

FeedbackRasterization

Fragment ShaderPer-Fragment

OpsFramebuffer

Tr. Feedback buffer

Uniform Block

Texture Fetch

Image Load/Store

Atomic Counter

Shader Storage

Element buffer (EBO)

Draw Indirect Buffer

Vertex Buffer (VBO)

Front-End(decoder)

OpenGLDriver

Application

FBO resources(Textures / RB)

OpenGLCommands

Cmd bundles

Push-Buffer

(FIF

O)

cmds

OpenGLresources

Resources

Graphics pipelineStates

DependenciesHe

ap

..

Page 9: Siggraph 2016 - Vulkan and nvidia : the essentials

9

Application

memory GPU

BIG PICTURE – VULKAN

Vertex Puller (IA)

Vertex ShaderTCS

(Tessellation)TES

(Tessellation)

Tessellator

Geometry ShaderTransform

FeedbackRasterization

Fragment ShaderPer-Fragment

OpsFramebuffer

Tr. Feedback buffer

Uniform Block

Texture Fetch

Image Load/Store

Atomic Counter

Shader Storage

Element buffer (EBO)

Draw Indirect Buffer

Vertex Buffer (VBO)

Front-End(decoder)

OpenGLDriver

FBO resources(Textures / RB)

Cmd bundles

Push-Buffer

(FIF

O)

cmds

Minimal memorymanagement

Fewer translation,Validation checksAnd internal mgt

Resources

PipelineStates

Cmd-buffers / queues

Dependencies

Heap

RenderPasses

Descriptor

Sets

Page 10: Siggraph 2016 - Vulkan and nvidia : the essentials

10

Instance

VULKAN COMPONENTS

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Page 11: Siggraph 2016 - Vulkan and nvidia : the essentials

11

Instance

Device

VULKAN COMPONENTS

Device(s)

Queue(s)

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-buffer

Page 12: Siggraph 2016 - Vulkan and nvidia : the essentials

12

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferVULKAN OBJECTS: DEVICE

Instance

Device(s)

Instance ~~ OpenGL Context

Instance-Layers• Intercepting API calls for misc.

purposes• Many layers available (api-dump;

core/std/parms validation; screenshot…)

Instance-Specific Extensions• KHR_Surface (for Swap-chains)• EXT_debug_report• …

Exposes some Devices…

Page 13: Siggraph 2016 - Vulkan and nvidia : the essentials

13

HOW DOES IT LOOK ?Instance creation

Layer properties

Extension properties

Page 14: Siggraph 2016 - Vulkan and nvidia : the essentials

14

HOW DOES IT LOOK ?Instance creation

etc

Get Instance-Extension’s functions

Page 15: Siggraph 2016 - Vulkan and nvidia : the essentials

15

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-buffer

Can have many …

VULKAN OBJECTS: DEVICE

Device(s)

VkPhysicalDevice• Capabilities• Memory Management• Queues• Objects

• Buffers• Images• Sync Primitives

Page 16: Siggraph 2016 - Vulkan and nvidia : the essentials

16

NVIDIA’S VULKAN CAPABILITIES

Properties listed from Physical DeviceNVIDIA is almost full featured

Top to bottom: from GeForce, Quadro down to Tegra

Check http://vulkan.gpuinfo.org/listreports.php

Page 17: Siggraph 2016 - Vulkan and nvidia : the essentials

17

NVIDIA’S VULKAN CAPABILITIESGeForce GTX 980

Tegra X1 & K1

Page 18: Siggraph 2016 - Vulkan and nvidia : the essentials

18

HOW DOES IT LOOK ?Device creation – enumerate physical devices; gather properties

Page 19: Siggraph 2016 - Vulkan and nvidia : the essentials

20

HOW DOES IT LOOK ?Device creation – Create the device (!)

Page 20: Siggraph 2016 - Vulkan and nvidia : the essentials

21

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Queue

VULKAN COMPONENTS

Page 21: Siggraph 2016 - Vulkan and nvidia : the essentials

22

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferQUEUESCommand queue was hidden in OpenGL Context… now explitly declared

Multiple threads can submit work to a queue (or queues)!Queues accept GPU work via CommandBuffer submissions

few operations available around Queues:, “submit work” and “wait for idle”Queue submissions can include sync primitives for the queue to:Wait upon before processing the submitted workSignal when the work in this submission is completed

Queue “families” can accept different types of work, e.g.NVIDIA exposes 2 families: 1+16 Queues

16 for all available types of work1 for transfer operations only (Copy Engine)

Queue

Page 22: Siggraph 2016 - Vulkan and nvidia : the essentials

23

HOW DOES IT LOOK ?Queue(s)

Page 23: Siggraph 2016 - Vulkan and nvidia : the essentials

24

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

VULKAN COMPONENTS

Page 24: Siggraph 2016 - Vulkan and nvidia : the essentials

25

SYNCHRONIZATIONevents and barriers

used to synchronize work within a command buffer or sequence of command buffers submitted to a single queue

semaphores used to synchronize work across queues or across coarse-grained submissions to a single queue

fences used to synchronize work between the device and the host. Device

Queue

Cmd-buffer

Host

barrier

event

Queue

Cmd-buffer

Queue

Cmd-buffer

event

Semaphores

Fences

Page 25: Siggraph 2016 - Vulkan and nvidia : the essentials

26

COMMAND-BUFFERSVulkan Rendering Command-BuffersClose to what GPU will get at Front-End (FIFO)

Minor translation & optimization from the Driver prior to sending to the GPU

Each can be created either for one shot or for multiple frames/submissions

Cannot Cmd-Buffers from GPU (command-lists can): API calls to vkCmd…() between Begin & End

Multi-threading friendly (!)Primary Cmd-Buffer can call many many 2ndary Cmd-Buffers

2ndary Command-buffer

2ndary Command-buffer

Primary Cmd-buffer

2ndary Cmd-buffer

……

Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Cmd.Buffer Pool

Page 26: Siggraph 2016 - Vulkan and nvidia : the essentials

27

Thread 1(Busy)

Update Work

Feed Cmd Buffers

cmd. Buffer PoolCreate 2dary Cmd

Buffer

Give out Cmd Buffers

COMMAND-BUFFERS AND MULTI-THREADING

Main thread(Busy)

Game WorkThread

Coordination

Swapping

Collect

Submit to Q

cmd. Buffer PoolCreate 1ary Cmd

Buffer

1ary Cmd calls 2dary ones

Thread 3(Busy)

Update Work

Feed Cmd Buffers

cmd. Buffer PoolCreate 2dary Cmd

Buffer

Give out Cmd Buffers

Thread 4(Busy)

Update Work

Feed Cmd Buffers

cmd. Buffer PoolCreate 2dary Cmd

Buffer

Give out Cmd Buffers

Thread 2(Busy)

Update Work

Feed Cmd Buffers

cmd. Buffer PoolCreate 2dary Cmd

Buffer

Give out Cmd Buffers

Command Buffer Pool local to the threadTo prevent conflicts in concurrent access

Page 27: Siggraph 2016 - Vulkan and nvidia : the essentials

28

Must not recycle a CommandBuffer for rewriting until it is no longer in flight (In flight == GPU still consuming it on its side)

But we can’t flush the queue each frame: would break parallelism !VkFences can be provided with a queue submission to test when a command buffer is ready to be recycled

COMMAND BUFFER THREAD SAFETY

App Submissions to the Queue

GPU Consumes Queue

Fence ACommandBuffe

rCommandBuffe

rCommandBuffe

r

Fence BCommandBuffe

rCommandBuffe

r

Fence A Signaled to App

Rewrite command buffer

CommandBuffer

Page 28: Siggraph 2016 - Vulkan and nvidia : the essentials

29

Frame NFrame N-1

Threads can have more than 1 Command PoolRing-buffer: One Command-Pool per Frame

when the frame is no longer in flight (Using Fences): simply reset the whole Pool

Frame N-2

THREADS AND COMMAND POOLS

Thread 1 CommandPoolCommand

Buffer

Command

Buffer

CommandPoolCommand

Buffer

Command

Buffer

CommandPoolCommand

Buffer

Command

Buffer

Thread 2 CommandPoolCommand

Buffer

Command

Buffer

CommandPoolCommand

Buffer

Command

Buffer

CommandPoolCommand

Buffer

Command

Buffer

Page 29: Siggraph 2016 - Vulkan and nvidia : the essentials

30

HOW DOES IT LOOK ?Command-Buffers

Note: helpers (structs wrappers) to use Constructors & functors for compact data declarationSee NVK.h/cpp inhttps://github.com/nvpro-samples

Page 30: Siggraph 2016 - Vulkan and nvidia : the essentials

31

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferVULKAN COMPONENTS

Graphics pipeline

Page 31: Siggraph 2016 - Vulkan and nvidia : the essentials

32

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferDescriptor-set LayoutDescriptor-set Layout

GRAPHICS PIPELINESnapshot of all States

Including Shaders

Pre-compiled & ImmutableIdeally: done at Initialization timeOk at render-time *if* using the Pipeline-Cache

Prevents validation overhead during rendering loop

Some Render-states can be excluded from it: they become “Dynamic” States

Graphics pipeline

Pipeline-layout

Descriptor-set Layout

Shader Stage

Vertex Input

Tess. State

Viewport State

Rasterizations State

Multi-Sample State

Depth & Stencil State

Color Blend State

Optional Dynamic States

Viewport ScissorBlend const Stencil RefDepth

BoundsDepth Bias

Shader Module

Shader Stage

Rasterizations State• depthClipEnable• rasterizerDiscardEnable• fillMode• cullMode• frontFace• depthBiasEnable• depthBias• depthBiasClamp• slopeScaledDepthBias• lineWidth

Pipelinecache

Page 32: Siggraph 2016 - Vulkan and nvidia : the essentials

33

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferDescriptor-set LayoutDescriptor-set Layout

GRAPHICS PIPELINEGraphics Pipeline must be consistent with shaders

No “introspection”, so everything known & prepared in advance

Vertex Input:tells how Attributes: Locations are attached to which Vertex Buffer at which offset

Pipeline Layout:Tells how to map Sets and Bindings for the shaders at each stage (Vtx, Fragment, Geom…)

Graphics pipeline

Pipeline-layout

Descriptor-set Layout

Shader Stage

Vertex Input

Shader Module

GLSL Code

layout(std140, set= 0 , binding= 0) uniform A { ... };layout(std140, set= 0 , binding= 1) uniform B { … };layout(std140, set= 1 , binding= 2) uniform C { … };…

layout(location=0) in vec3 pos;layout(location=1) in vec3 N;…void main() { …

Spir-

V co

mpi

ledDescriptor-Set(s)Descriptor-Sets for

Page 33: Siggraph 2016 - Vulkan and nvidia : the essentials

34

HOW DOES IT LOOK ?Graphics pipeline - setup

Page 34: Siggraph 2016 - Vulkan and nvidia : the essentials

35

HOW DOES IT LOOK ?Graphics pipeline creation (compact notation)

Page 35: Siggraph 2016 - Vulkan and nvidia : the essentials

36

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-buffer

Buffer

Buffer

VULKAN COMPONENTS

Page 36: Siggraph 2016 - Vulkan and nvidia : the essentials

37

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferBUFFERSHighly Heterogenous. Most often used for:

Index/Vertex BuffersUniform Buffers (Matrices, material parameters…)

Vulkan Object: Must be bound to some Device MemoryCan be CPU accessible memory (mappable)Can be CPU cachedCan be GPU accessible only: need a “Staging Buffer” to write into it

But most Efficient

(More on Device Memory later…)

Page 37: Siggraph 2016 - Vulkan and nvidia : the essentials

38

COMMAND-BUFFERS: UPDATE/PUSH CONSTANTS2 more ways to update constants/uniforms for Shaders from the Command-BufferUpdate-Buffer: prior to Render-Pass: can target any Buffer bound by Descriptor Sets

Push-Constants: targets a dedicated section in GLSL/SpirV

New values appended “in-band”: in the Command-Buffer

Efficient; but good for small amount of values

Primary Cmd-buffer

Begin Render-Pass

Draw…

vkCmdPushConstants

vkCmdUpdateBuffer()

layout(push_constant) uniform objectBuffer { mat4 matrixObject; vec4 diffuse;} object;

layout(set=0 , binding = 2 ) uniform MyBuffer { mat4 mW;…

Page 38: Siggraph 2016 - Vulkan and nvidia : the essentials

39

HOW DOES IT LOOK ?Buffers – copy data in a buffer

• Create Buffer

• Create staging buffer

• Bind objects to some memory

• Command buffer for copy

• Enqueue command buffer

• Note: could be put with other graphic commands for better efficiency

Page 39: Siggraph 2016 - Vulkan and nvidia : the essentials

40

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferVULKAN COMPONENTS

Page 40: Siggraph 2016 - Vulkan and nvidia : the essentials

41

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferIMAGES AND IMAGEVIEWImages represent all kind of ‘pixel-like’ arrays

Textures: Color or Depth-StencilRender targets : Color and Depth-StencilEven Compute dataShader Load/Store (imgLoadStore)

ImageView required to expose Images properly when specific format requiredFor Shaders For Framebuffers

Page 41: Siggraph 2016 - Vulkan and nvidia : the essentials

42

HOW DOES IT LOOK ?

Way more complex than OpenGL !• Load image• Create an Image

(1D/2D/3D/Cube…)• Create an Image-View

• Aggregate layers/mipmap layers info (offsets, sizes) in a structure (VkBufferImageCopy)

• Aggregate layers & mipmap data to contiguous memory

• Create staging buffer + bind memory + copy data in it

• Use command-buffer to copy to the image: layers and mipmaps• Layout transition of image

for copy• vkCmdCopyBufferToImage• Layout transition of image

for use by shader• Enqueue command buffer and

execute

Simple texture creation

Page 42: Siggraph 2016 - Vulkan and nvidia : the essentials

43

HOW DOES IT LOOK ?Simple texture creation

Page 43: Siggraph 2016 - Vulkan and nvidia : the essentials

44

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferVULKAN COMPONENTS

Descriptor-Set

Page 44: Siggraph 2016 - Vulkan and nvidia : the essentials

45

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferDESCRIPTOR-SET

Each DescriptorSet holds references to some resourcesDescriptor-Set-Layout defines how resources must be put together in a DescriptorSet

Command buffers can then efficiently bind any or them

They must match what shaders of each stage expect !

Descriptor-Set

Image

Memory

Image View

Buffer

Sampler

Heap

DescriptorSet Pool

Descriptor-set Layout

GLSL Code

layout(std140, set= 0 , binding= 0) uniform A { ... };layout(std140, set= 0 , binding= 1) uniform B { … };layout(std140, set= 1 , binding= 2) uniform C { … };layout(set=0, binding=3) uniform sampler2D tex;…void main() { …

Page 45: Siggraph 2016 - Vulkan and nvidia : the essentials

46

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferDESCRIPTOR-SET

Descriptor-set Layout A

Uniform Buffer for VtxStorage Buffer for frag.Image View for frag

Descriptor-set Layout C

Uniform Buffer for Vtx

Descriptor-set Layout B

Image View for frag.Sampler for frag. shd

Dset A1

Buffer L Image View M

Buffer K

Dset B1Image View QSampler R

Dset C3Buffer S

Command-buffer

Bind Graphics-pipeline

Bind Descriptor-Set(s)

Dset A2

Buffer O Image View P

Buffer N

?

Memory

Heap

Image M

Buffer K

Image PImage Q

Buffer L Buffer N Buffer O Buffer S Buffer T

Dset C2Buffer T

Dset C1Buffer S

?

Defined forLayout A+B+C

Page 46: Siggraph 2016 - Vulkan and nvidia : the essentials

47

HOW DOES IT LOOK ?Descriptor Set - initialization

Page 47: Siggraph 2016 - Vulkan and nvidia : the essentials

48

HOW DOES IT LOOK ?Descriptor Set – using it

Page 48: Siggraph 2016 - Vulkan and nvidia : the essentials

49

DESCRIPTOR SETS AND MULTI-THREADING

Thread 1

Update Work

Update resources

in Desc. Sets

Descriptor PoolAllocate Desc. Sets

… more Vulkan work …

Main thread

Game WorkThread

Coordination

Swappingsynchronize

Descriptor PoolAllocate Desc. SetsUpdate resources

in Desc. Sets … more Vulkan work …

Thread 2

Update Work

Update resourcesin Desc. Sets

Descriptor PoolAllocate Desc. Sets

… more Vulkan work …

Thread 1

Update Work

Update resources

in Desc. Sets

Descriptor PoolAllocate Desc. Sets

… more Vulkan work …

Thread 2

Update Work

Update resourcesin Desc. Sets

Descriptor PoolAllocate Desc. Sets

… more Vulkan work …

! Descriptor Pool local to the thread !cmd. Buffer Pool

Create 2dary Cmd Buffer

Page 49: Siggraph 2016 - Vulkan and nvidia : the essentials

50

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferVULKAN COMPONENTS

Page 50: Siggraph 2016 - Vulkan and nvidia : the essentials

51

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferVULKAN COMPONENTS

Image

Image ViewImage ViewImage ViewImageView #0

ImageImageImage #0 Attachment Desc.• Image #0: fmt…• Image #1: fmt…• Image #2: fmt…• Image #3: fmt…

Must matchSub-Pass #0 Desc.• Color: Image #0• DST: Image #1• MSAA resolve:Image

#2

Sub-Pass #1 Desc.• Color0: Image #3• Color1: Image #4• DST: Image #1• MSAA resolve:Image

#5

• Framebuffer• Simpler than OpenGL• “Bag” or “Repository” of resource views• No role defined for the resources

• Render-Pass• Really defines the role of Framebuffer resources• Can have more than 1 Sub-Pass• Each Sub-Passes defines which Framebuffer resource to

use• invented for Tilers Arch

Sub-Pass #N Desc.…

DefaultSub-Pass

Can use many if compatibles

Page 51: Siggraph 2016 - Vulkan and nvidia : the essentials

52

HOW DOES IT LOOK ?Renderpass Creation (‘compacted’ notation for Vulkan structures)

Page 52: Siggraph 2016 - Vulkan and nvidia : the essentials

53

HOW DOES IT LOOK ?Framebuffer Creation and use

Page 53: Siggraph 2016 - Vulkan and nvidia : the essentials

54

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferVULKAN COMPONENTS

Page 54: Siggraph 2016 - Vulkan and nvidia : the essentials

55

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferMEMORY VULKAN OBJECTS

Vulkan Objects referring to buffer(s) of data need binding to memoryVertex/Index Buffers; Uniform Buffers; Images/Textures…

Vulkan Device exposes various Memory Heaps - Example:heap 0: size:12,288Mb (Video Memory of my K6000)

heap 1: size:17,911Mb (System Memory of my PC)

And various Memory Types from these Heaps. Example:Mem.Type Heap Flags0 1 (sys.mem) -1 0 (Video) DEVICE_LOCAL2 1 (sys.mem) HOST_VISIBLE | HOST_COHERENT3 1 (sys.mem) HOST_VISIBLE | HOST_COHERENT |

HOST_CACHED

Tegra: Adds one more:HOST_VISIBLE “NON-Coherent”

Page 55: Siggraph 2016 - Vulkan and nvidia : the essentials

56

Device

Queue

Command-buffer

Graphics pipeline

Descriptor-Set

Framebuffer Render-Pass

Image

Memory

2ndary Command-buffer

Buffer

Image View

Buffer

Sampler

Image View Begin Render-Pass

Bind Graphics-pipeline

Bind Vertex/Idx Buffer(s)

Bind Descriptor-Set(s)

Draw…

End Render-Pass

Execute Commands

Update Buffer

Set misc. dynamic states

Barrier synchronization

Heap

Cmd.Buffer Pool

DescriptorSet Pool

Command-bufferMEMORY VULKAN OBJECTS

Image (Tex)

Image View

Buffer (Uniforms)

Image View

Buffer (Vertices)

matrices

Vtx Buf.V1.xyzwV2.xyzw…

Image (RT)

rgba

rgb VtxBuf.

Page 56: Siggraph 2016 - Vulkan and nvidia : the essentials

57

RESOURCE MANAGEMENT

Allocation and Sub allocation

HEAP supporting A,B HEAP supporting B

Allocation Type A Allocation Type B

Image

...

Cube Image Buffer

Allocate memory type from heap

Query Vulkan Object about size, alignment & type requirementsAssign memory subregion to a resource (allows aliasing)

BufferView BufferViewCreate resource views on subranges of a buffer or image (array slices...)

Buffer

Page 57: Siggraph 2016 - Vulkan and nvidia : the essentials

58

RESOURCE MANAGEMENT

#HappyGPU

Memory Allocation

Buffer

UniformVertexIndex

Memory Allocation

Buffer

Index Vertex

Buffer

Uniform

Buffer

Better...

Buffer

Index Vertex

Buffer

Uniform

Buffer

Not. So. Good.

Bind same buffer withOffsets > 0

1 buffer can have many types of data

Page 58: Siggraph 2016 - Vulkan and nvidia : the essentials

59

HOW DOES IT LOOK ?Binding Memory to Objects – Simple use-caseMem properties previously gathered at init time

Page 59: Siggraph 2016 - Vulkan and nvidia : the essentials

60

SHADERSVulkan uses SPIR-V passed directly to the driver

Can be compiled from GLSL Via glslang or LunarG’s glslangValidator; Google ShaderC

theoretically other languages could be compiled to Spir-V…Libraries available to compile GLSL to Spir-V from the application

NVIDIA allows to compile GLSL directly

glslangGLSL

Some other language

NVIDIA VK_NV_glsl_shader: Vulkan reads GLSL directly

Page 60: Siggraph 2016 - Vulkan and nvidia : the essentials

61

SHADERSMultiple entry points can be defined in a single Spir-V shader-module

Prevents redundant code: shader module used by many Graphics-Pipelines

Specialization Constants: early setup of constants for shaders in given Graphics-Pipeline

Allows sharing snippets of code : easier to share common shader code

Modulevtx_main()

frag_pastic(float t)Frag_metal()lighting(

)

rotate(…)

Graphics pipeline AVtx_main() frag_pastic(1.1)

Graphics pipeline BVtx_main() frag_metal()

Graphics pipeline CVtx_main() frag_wood()

frag_wood()

Warning: Current GLSL

Spir-V compilers

Don’t support this feature,

yetBut part of the API & Spir-V

Will happen soon

Graphics pipeline AVtx_main() frag_pastic(3.2)

Page 61: Siggraph 2016 - Vulkan and nvidia : the essentials

62

VULKAN WINDOW SYSTEM INTEGRATION (WSI)WSI manages the ownership of images via a swap chainOne image is presented while the other is rendered toWSI is a Vulkan Extension

WSIDisplay

Swap Chain(images)

Application

Submit image to WSIThe display owns the image

Acquire image from WSIThe application owns the

image

Page 62: Siggraph 2016 - Vulkan and nvidia : the essentials

63

NVIDIA OPENGL VULKAN INTEROPAlternative to WSI: GL_NV_draw_vulkan_imageCreate an OpenGL Context and all the usual thingsCreate Vulkan DeviceRendering Loop involves both OpenGL and Vulkan

Blit the Vulkan image to OpenGL backbuffer: glDrawVkImageNVExtra care on synchronization (Semaphores)

Bonus: Mix OpenGL rendering (UI overlay…) with VulkanAllows smooth transition in projects

OpenGLVulkan

Page 63: Siggraph 2016 - Vulkan and nvidia : the essentials

64

HOW DOES IT LOOK ?GL_NV_draw_vulkan_image Initialize…

Page 64: Siggraph 2016 - Vulkan and nvidia : the essentials

65

HOW DOES IT LOOK ?GL_NV_draw_vulkan_image

Submit commands to Queue with semaphores

Backbuffer Blit

Page 65: Siggraph 2016 - Vulkan and nvidia : the essentials

66

PRE-REQUISITES TO WORK WITH VULKANLunar-G (http://lunarg.com/ )

Vulkan Loader (+Source code)Tools: Spir-V compiler for GLSL code and other librariesLayers: intermediate code invoked by Vulkan API functions to help debug

Vulkan Includes

Drivers:GeForce Experiencehttps://developer.nvidia.com/vulkan-driver

NVIDIA resources: https://developer.nvidia.com/Vulkan

Page 66: Siggraph 2016 - Vulkan and nvidia : the essentials

67

RECAP’ ON NVIDIA-SPECIFIC FEATURESCompatible GPUs for Vulkan: Kepler and Higher; Shield Tablet; Shield Android TV

VK_NV_glsl_shader : GLSL can be directly sent to VulkanVK_NV_dedicated_allocation : more efficient memory usageGL_NV_draw_vulkan_image can replace WSI16 Queues. All available for all kind of use; 1 Queue for Copy-Engine only3 frames (max) in flight with WSIAll Host memories are “Coherent” (except one for Tegra)Layout transitions don’t exist in our HW (VK_IMAGE_LAYOUT_GENERAL)Linear-Tiling only for 2D non-mipmapped textures… please avoid (bad performance)

Shaders never need re-compilation due to states in Graphics-pipeline

Page 67: Siggraph 2016 - Vulkan and nvidia : the essentials

68

NSIGHT FOR VULKAN

Page 68: Siggraph 2016 - Vulkan and nvidia : the essentials

69

RECAP’ ON VULKAN PHILOSOPHY

Validate as much as possible up-front (DescriptorSets; Pipelines…)The driver doesn’t waste time on figuring-out how to set things-up

Reuse existing patterns of Graphics-Pipelines: cached pipelinesKnow your application: Taylor Vulkan design according to itKnow your memory usage: You are in charge of optimal sub-allocationsExplicit multi-threading for graphics: Application’s responsibilityExplicit Resource updates: Either through [non]Coherent buffers; or Queue-Based DMA transfers

Page 69: Siggraph 2016 - Vulkan and nvidia : the essentials

70

FEW WORDS ON VKCPP PROJECTC++11 to the rescue• Open-Source Project of a C++11 overlay for Vulkan: became Khronos-

offcial (!)

• Simplify Vulkan usage by

• reducing risk of errors, i.e. type safety, automatic initialization of sType, …

• Reduce #lines of written code, i.e. constructors, initalizer lists for arrays, …

• Add utility functions for common tasks (suballocators, resource tracking, …)

http://on-demand.gputechconf.com/gtc/2016/events/vulkanday/Vulkan_C++.pdf

https://developer.nvidia.com/open-source-vulkan-c-api

https://www.khronos.org/news/permalink/khronos-introduces-vulkan-hpp-open-source-vulkan-c-api

Page 70: Siggraph 2016 - Vulkan and nvidia : the essentials

71

VKCPP PROJECTTwo C++ based layers

Autogenerated ‚low-level‘ layer using vulkan.xml

• Type safety

• Syntactic sugar

• Lightweight layer; Keeps you closer to the real Vulkan

Hand-coded ‚high level‘ layer• Reduce code complexity• Exception safety, resource lifetime tracking, …• Closer dependency with VkCpp internal implementations

Page 71: Siggraph 2016 - Vulkan and nvidia : the essentials

72

NATIVE VULKAN VS. VKCPP CODE

Native Vulkan: ~750 lines vkCPP: ~200 lines

Page 72: Siggraph 2016 - Vulkan and nvidia : the essentials

73

REFERENCESVulkan info from NVIDIA:

https://developer.nvidia.com/Vulkan https://developer.nvidia.com/vulkan-graphics-api-here

Samples + Source code in OpenGL and Vulkan:https://github.com/nvpro-samples

Other:https://gameworks.nvidia.com https://developer.nvidia.com/designworks http://vulkan.gpuinfo.org/listreports.php https://developer.nvidia.com/open-source-vulkan-c-api