SlideShare a Scribd company logo
Mark Kilgard, January 19, 2016
Migrating from OpenGL to Vulkan
2
About the Speaker
Mark Kilgard
Principal Graphics Software Engineer in Austin, Texas
Long-time OpenGL driver developer at NVIDIA
Author and implementer of many OpenGL extensions
Collaborated on the development of Cg
First commercial GPU shading language
Recently working on GPU-accelerated vector graphics
(Yes, and wrote GLUT in ages past)
Who is this guy?
3
Motivation for Talk
What kinds of apps benefit from Vulkan?
How to prepare your OpenGL code base to transition to Vulkan
How various common OpenGL usage scenarios are re-thought in Vulkan
Re-thinking your application structure for Vulkan
Coming from OpenGL, Preparing for Vulkan
4
Analogy
Different Valid Approaches
5
Analogy
Fixed-function OpenGL
Pre-assembled toy car
fun out of the box,
not much room for customization
6
Analogy
Modern AZDO OpenGL with Programmable Shaders
LEGO Kit
you build it yourself,
comes with plenty of useful, pre-shaped pieces
AZDO = Approaching Zero Driver Overhead
7
Analogy
Vulkan
Pine Wood Derby Kit
you build it yourself to race from raw materials
power tools used to assemble, adult supervision highly recommended
8
Analogy
Different Valid Approaches
Fixed-function OpenGL
Modern AZDO OpenGL with
Programmable Shaders
Vulkan
9
Beneficial Vulkan Scenarios
Is your graphics work
CPU bound?
Can your graphics
work creation be
parallelized?
start
yes
Vulkan
friendly
yes
Has Parallelizable CPU-bound Graphics Work
10
Beneficial Vulkan Scenarios
Your graphics
platform is fixed
You’ll
do whatever
it takes to squeeze
out max
perf.
start
yes
Vulkan
friendly
yes
Maximizing a Graphics Platform Budget
11
Beneficial Vulkan Scenarios
You put
a premium on
avoiding
hitches
You can
manage your
graphics resource
allocations
start
yes
Vulkan
friendly
yes
Managing Predictable Performance, Free of Hitching
12
Unlikely to Benefit
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 direct 3D graphics API dependencies
• Consider using an engine targeting Vulkan, instead of dealing with Vulkan yourself
Scenarios to Reconsider Coding to Vulkan
13
First Steps Migrating to Vulkan
Eliminate fixed-function
Source all geometry from vertex buffer objects (VBOs) with vertex arrays
Use all programmable GLSL shaders with layout() qualifiers
Consider using samplers
Do all rendering into framebuffer objects (FBOs)
Stay within the non-deprecated subset (e.g. no GL_QUADS, for now…)
Think about better batching & classify all your render states
Avoid depending on OpenGL context state
All pretty sensible advice, even if you stick with OpenGL
Modernize Your OpenGL
14
Next Steps Migrating to Vulkan
Think about how your application would handle losing the GPU
Similar to ARB_robustness
Profile your application, understand what portions are CPU and GPU bound
Vulkan most benefits apps bottlenecked on graphics work creation and driver validation
Is that your app?
Adopt common features available in both OpenGL and Vulkan first in OpenGL
Proving out tessellation or multi-draw-indirect probably easier first in your stable
OpenGL code base
Again all pretty sensible advice, even if you stick with OpenGL
Modernize Your OpenGL
15
Thinking about Vulkan vs. OpenGL
OpenGL, largely understood in terms of
Its API, functions for commands and queries
And how that API interacts with the OpenGL
state machine
OpenGL has lots of implicit synchronization
Errors handled largely by ignoring command
OpenGL API manages various objects
But allocation of underlying device resources
largely handled by driver
+
OpenGL Has a Well-established Approach
Originally client-server
16
Thinking about Vulkan vs. OpenGL
Vulkan constructs and submits work for a
graphics device
Idealized graphics + compute device
Requires application to maintain valid Vulkan
usage for proper operation
Not expected to behave correctly in face of
errors, justified for performance
Instead of updating state machine, Vulkan is
about establishing working relationships
between objects
Pre-validates operation of actions
Vulkan Plays By Different Rules
Explicit API
Explicit memory management
Explicit synchronization
Explicit queuing of work
Explicit management of buffer state with
render passes
Not client-server, explicitly depends on
shared resources and memory
17
Truly Transitioning to Vulkan
Much more graphics resource responsibility for the application
You need to allocate from large device memory chunk
You become responsible for proper explicit synchronization
Fences, Barriers, Semaphores
Barriers are probably the hardest to appreciate
Everything has to be structured as pipeline state objects (PSOs)
Understand render passes
Think how parallel command buffer generation would operate
You become responsible for multi-threaded exclusion of your Vulkan objects
Vulkan Done Right Rethinks Entire Graphics Rendering
18
Common Graphics Tasks
Loading a mipmapped texture
Loading a vertex buffer object for rendering
Choosing a shader representation and loading shaders
Initiating compute work
Managing a memory sub-allocator
Thinking about render passes
Managing Predictable Performance, Free of Hitching
19
Loading a Texture
Well traveled path via OpenGL 3.0
glBindTexture(GL_TEXTURE_2D, texture_name);
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, width, height,
/*border*/0, GL_UNSIGNED_BYTE, GL_RGBA, pixels);
glGenerateMipmap(GL_TEXTURE_2D);
Does more than you think it does
The OpenGL View
20
Logical Operations to Load a Texture
1. Create host driver objects corresponding to texture/sampler/image/view/layout
2. Copy call’s image to staging memory accessible to host+device
3. (OpenGL might do a format conversion and pixel transfer)
4. Allocates device memory for texture image for texturing
5. Copy image from host+device memory to device memory for texturing
6. Allocate device resources for sampler
7. Generate mipmap levels
21
Various Vulkan Objects “inside” a Texture
No such thing as “VkTexture”
Instead
Texture state in Vulkan = VkDescriptorImageInfo
Combines: VkSampler + VkImageView + VkImageLayout
Sampling state + Image state + Current image layout
Texture binding in Vulkan = part of VkDescriptorSetLayoutBinding
Contained with VkDescriptorSetLayout
Building Up the OpenGL Texture Object from Vulkan Concepts
OpenGL textures are opaque,
so lacks an exposed image layout
22
Allocating Image Memory for Texture
VkImage
VkDevice
vkGetImageMemoryRequirements
VkMemoryRequirements
vkBindImageMemory
VkDeviceMemory
vkAllocateMemory
vkCreateImage
With VulkanNaïve approach!
vkAllocateMemory is expensive.
Demos may do this, but real apps
should sub-allocate from large
VkDeviceMemory allocations
See next slide…
23
Sub-allocating Image Memory for Texture
VkImage
VkDevice
vkGetImageMemoryRequirements
VkMemoryRequirements
vkBindImageMemory
VkDeviceMemory
vkAllocateMemory
vkCreateImage
One large device memory allocation, assigned to multiple images
Proper approach!
vkAllocateMemory makes a
large allocation with and then
sub-allocates enough
memory for the image
So who writes
the sub-allocator?
You do!
24
Binding Descriptor Sets for a Texture
VkDescriptorImageInfoVkSampler
VkImageView
VkImageLayout
VkDescriptorSetLayout
VkWriteDescriptorSet vkAllocateDescriptorSets
VkPipelineLayout
vkCmdBindDescriptorSets
VkCommandBuffer
vkCreateDescriptorSetLayout
vkCreatePipelineLayout
vkUpdateDescriptorSets
So Pipeline Sees Texture
25
Establishing Sampler Bindings for a Pipeline Object
VkDescriptorSetLayoutBinding for a sampler binding
vkCreateDescriptorSetLayout
VkDescriptorSetLayout
vkCreatePipelineLayout
VkPipelineLayout
vkCreateGraphicsPipelines
VkPipeline
vkCmdBindPipeline
Vulkan Pipelines Need to Know How They Will Bind Samplers
VkCommandBuffer
26
Base Level Specification + Mipmap Generation
VkCommandBuffer
vkCmdBlitImage
vkCmdPipelineBarrier
vkCmdBlitImage
for each upper mipmap level
vkCmdPipelineBarrier
Vulkan Command Buffer Orchestrates Blit + Downsamples
staging
image
texture
base
image
texture
base
image
level 1
mipmap
level i
mipmap
level i+1
mipmap
27
Binding to a Vertex Array Object (VAO) and
Rendering from Vertex Arrays
Well traveled path via OpenGL 3.0
glBindVertexArray(vertex_array_object);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, indices);
Again, does more than you think it does
The OpenGL View
28
Allocating Buffer Memory for VBO
VkBuffer
VkDevice
vkGetBufferMemoryRequirements
VkMemoryRequirements
vkBindBufferMemory
VkDeviceMemory
vkAllocateMemory
VkCreateBuffer
With VulkanNaïve approach!
vkAllocateMemory is expensive.
Demos may do this, but real apps
should sub-allocate from large
VkDeviceMemory allocations
29
Binding Vertex State To A Pipeline
VkVertexInputBindingDescription
VkVertexInputAttributeDescription
VkPipelineVertexInputStateCreateInfo
VkGraphicsPipelineCreateInfo
VkGraphicsPipeline
vkCreateGraphicsPipelines
So Pipeline Sees Vertex Input State
VkPipelineShaderStageCreateInfo
VkPipelineInputAssemblyStateCreateInfo
VkPipelineViewportStateCreateInfo
VkPipelineRasterizationStateCreateInfo
VkPipelineMultisampleStateCreateInfo
VkPipelineDepthStencilStateCreateInfo
VkPipelineColorBlendStateCreateInfo
VkPipelineDynamicStateCreateInfo
30
Binding Vertex Buffer For Drawing
For the vertex input statevkCmdBindPipeline
Buffer Binding Is Performed With The Command Queue
vkCmdBindVertexBuffers For the vertex data
vkCmdBeginRenderPass
vkCmdBindDescriptorSets
vkCmdDrawIndexed
vkCmdEndRenderPass
Begin Drawing
Draw
End Drawing
31
Big Features, Ready for Vulkan
Compute shaders
Tessellation shaders
Geometry shaders
Sparse resources (textures and buffers)
In OpenGL 4.5 today
Vulkan has the same big features, just different API
Mostly the same GLSL can be used in either case
Prototype in OpenGL now, Enable in Vulkan next
32
Thinking about render passes
OpenGL just has commands to draw primitives
No explicit notion of a render pass in OpenGL API
Vulkan does have render passes, VkRenderPass
Includes notion of sub-passes
Designed to facilitate tiling architectures
Bounds the lifetime of intermediate framebuffer results
Allows iterating over subpasses (chunking) within a render pass on a per tile basis
In most cases, you won’t need to deal with subpasses
How do rendering operations affect the retained framebuffer?
33
Render Pass
Describes the list attachments the render pass involves
Each attachment can specify
How the attachment state is initialized (loaded, cleared, dont-care)
How the attach state is stored (store, or dont-care)
Don’t-care allows framebuffer intermediates to be discarded
E.g. depth buffer not needed after the render pass
How the layout of attachment could change
Sub-pass dependencies indicate involvement of attachments within a subpass
What does a Vulkan Render Pass Provide?
34
Render Passes
vkCmdBeginRenderPass
vkCmdEndRenderPass
vkCmdBeginRenderPass
vkCmdNextSubpass
vkCmdNextSubpass
vkCmdEndRenderPass
Monolithic or Could Have Sub-passes
Each subpass
has its own
description and
dependencies
Simple render pass,
no subpasses
Complex render pass,
with multiple subpasses
35
Vulkan Application Structure
Parallel Command Buffer Generation
Traditional single-threaded
OpenGL app structure
update scene
draw scene
single thread
Possible multi-threaded
Vulkan app structure
collect secondary
command buffers
distribute
update scene work
submit
command
buffer
build secondary
command buffer
work thread
build secondary
command buffer
work thread
build secondary
command buffer
work thread
build secondary
command buffer
work thread
36
Conclusions
Vulkan is a radical departure from OpenGL
Modernizing your OpenGL code base is definitely good for moving to Vulkan
But it will take more work than that!
Vulkan’s explicitness makes simple operations like a texture bind quite involved
Think about multi-threaded command buffer creation
Get Ready for Vulkan
Migrating from OpenGL to Vulkan

More Related Content

PDF
OpenGL 4.4 - Scene Rendering Techniques
PDF
OpenGL NVIDIA Command-List: Approaching Zero Driver Overhead
PPT
NVIDIA OpenGL 4.6 in 2017
PPTX
OpenGL 4.5 Update for NVIDIA GPUs
PPTX
Siggraph 2016 - Vulkan and nvidia : the essentials
PPTX
Beyond porting
PPT
OpenGL 3.2 and More
PPTX
Approaching zero driver overhead
OpenGL 4.4 - Scene Rendering Techniques
OpenGL NVIDIA Command-List: Approaching Zero Driver Overhead
NVIDIA OpenGL 4.6 in 2017
OpenGL 4.5 Update for NVIDIA GPUs
Siggraph 2016 - Vulkan and nvidia : the essentials
Beyond porting
OpenGL 3.2 and More
Approaching zero driver overhead

What's hot (20)

PPTX
Optimizing the Graphics Pipeline with Compute, GDC 2016
PPSX
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
PPTX
A Certain Slant of Light - Past, Present and Future Challenges of Global Illu...
PPTX
Hable John Uncharted2 Hdr Lighting
PDF
Graphics Gems from CryENGINE 3 (Siggraph 2013)
PPT
A Bit More Deferred Cry Engine3
PPTX
Physically Based and Unified Volumetric Rendering in Frostbite
PPTX
Moving Frostbite to Physically Based Rendering
PDF
Siggraph2016 - The Devil is in the Details: idTech 666
PPT
NVIDIA OpenGL and Vulkan Support for 2017
PPTX
FrameGraph: Extensible Rendering Architecture in Frostbite
PPSX
Holy smoke! Faster Particle Rendering using Direct Compute by Gareth Thomas
PPT
SIGGRAPH Asia 2008 Modern OpenGL
PDF
Screen Space Reflections in The Surge
PPTX
Triangle Visibility buffer
PPTX
Five Rendering Ideas from Battlefield 3 & Need For Speed: The Run
PPT
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
PPTX
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
PDF
The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)
PDF
Forward+ (EUROGRAPHICS 2012)
Optimizing the Graphics Pipeline with Compute, GDC 2016
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
A Certain Slant of Light - Past, Present and Future Challenges of Global Illu...
Hable John Uncharted2 Hdr Lighting
Graphics Gems from CryENGINE 3 (Siggraph 2013)
A Bit More Deferred Cry Engine3
Physically Based and Unified Volumetric Rendering in Frostbite
Moving Frostbite to Physically Based Rendering
Siggraph2016 - The Devil is in the Details: idTech 666
NVIDIA OpenGL and Vulkan Support for 2017
FrameGraph: Extensible Rendering Architecture in Frostbite
Holy smoke! Faster Particle Rendering using Direct Compute by Gareth Thomas
SIGGRAPH Asia 2008 Modern OpenGL
Screen Space Reflections in The Surge
Triangle Visibility buffer
Five Rendering Ideas from Battlefield 3 & Need For Speed: The Run
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
The Rendering Technology of 'Lords of the Fallen' (Game Connection Europe 2014)
Forward+ (EUROGRAPHICS 2012)
Ad

Viewers also liked (18)

PPT
NVIDIA OpenGL in 2016
PDF
Modern OpenGL Usage: Using Vertex Buffer Objects Well
PPT
EXT_window_rectangles
PPT
GTC 2012: NVIDIA OpenGL in 2012
PPT
NVIDIA's OpenGL Functionality
PPT
NV_path rendering Functional Improvements
PDF
Advanced Scenegraph Rendering Pipeline
PPTX
Killzone Shadow Fall: Threading the Entity Update on PS4
PDF
OpenGL L06-Performance
PDF
Understaing Android EGL
PDF
NVIDIA Investor's Day 2014 Presentation by Rob Csongor: Tomorrow's Car Today
PPT
Вулкани и земјотреси
PPSX
Introduction to Direct 3D 12 by Ivan Nevraev
PPT
вулкани и земјотреси
PDF
Woden 2: Developing a modern 3D graphics engine in Smalltalk
PDF
Pitfalls of Object Oriented Programming by SONY
DOC
Milestone Highway Construction
PDF
Global Power Systems Pvt. Ltd., Nagpur, Generators Set & Controller
NVIDIA OpenGL in 2016
Modern OpenGL Usage: Using Vertex Buffer Objects Well
EXT_window_rectangles
GTC 2012: NVIDIA OpenGL in 2012
NVIDIA's OpenGL Functionality
NV_path rendering Functional Improvements
Advanced Scenegraph Rendering Pipeline
Killzone Shadow Fall: Threading the Entity Update on PS4
OpenGL L06-Performance
Understaing Android EGL
NVIDIA Investor's Day 2014 Presentation by Rob Csongor: Tomorrow's Car Today
Вулкани и земјотреси
Introduction to Direct 3D 12 by Ivan Nevraev
вулкани и земјотреси
Woden 2: Developing a modern 3D graphics engine in Smalltalk
Pitfalls of Object Oriented Programming by SONY
Milestone Highway Construction
Global Power Systems Pvt. Ltd., Nagpur, Generators Set & Controller
Ad

Similar to Migrating from OpenGL to Vulkan (20)

PPT
Advanced Graphics Workshop - GFX2011
PDF
Taste Vulkan Rainbow - Vulkan API for Graphics and Compute - Why It's Importa...
PDF
OpenGL ES and Mobile GPU
PDF
Computer Graphics - Lecture 01 - 3D Programming I
PDF
High Fidelity Games: Real Examples, Best Practices ... | Oleksii Vasylenko
PDF
Introduction of openGL
PDF
Open gl
PDF
Дмитрий Вовк - Learn iOS Game Optimization. Ultimate Guide
PPTX
Opengl presentation
PDF
Get the most out of mobile with Vulkan in Unity
PDF
OpenGL L05-Texturing
PDF
Texture Mapping
PPTX
Porting the Source Engine to Linux: Valve's Lessons Learned
PPTX
Sig13 ce future_gfx
PPT
OpenGL ES based UI Development on TI Platforms
PPTX
Getting started with open gl es 2
PDF
PDF
VkRunner: a Vulkan shader test tool (FOSDEM 2019)
PPT
PPTX
Optimizing Games for Mobiles
Advanced Graphics Workshop - GFX2011
Taste Vulkan Rainbow - Vulkan API for Graphics and Compute - Why It's Importa...
OpenGL ES and Mobile GPU
Computer Graphics - Lecture 01 - 3D Programming I
High Fidelity Games: Real Examples, Best Practices ... | Oleksii Vasylenko
Introduction of openGL
Open gl
Дмитрий Вовк - Learn iOS Game Optimization. Ultimate Guide
Opengl presentation
Get the most out of mobile with Vulkan in Unity
OpenGL L05-Texturing
Texture Mapping
Porting the Source Engine to Linux: Valve's Lessons Learned
Sig13 ce future_gfx
OpenGL ES based UI Development on TI Platforms
Getting started with open gl es 2
VkRunner: a Vulkan shader test tool (FOSDEM 2019)
Optimizing Games for Mobiles

More from Mark Kilgard (20)

PDF
D11: a high-performance, protocol-optional, transport-optional, window system...
PPT
Computers, Graphics, Engineering, Math, and Video Games for High School Students
PPT
Virtual Reality Features of NVIDIA GPUs
PPT
OpenGL for 2015
PPT
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
PDF
Accelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
PPT
SIGGRAPH Asia 2012: GPU-accelerated Path Rendering
PPT
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
PDF
Programming with NV_path_rendering: An Annex to the SIGGRAPH Asia 2012 paper...
PPT
GPU accelerated path rendering fastforward
PDF
GPU-accelerated Path Rendering
PPT
SIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
PPT
SIGGRAPH 2012: NVIDIA OpenGL for 2012
PPT
GTC 2012: GPU-Accelerated Path Rendering
PPT
CS 354 Final Exam Review
PPT
CS 354 Surfaces, Programmable Tessellation, and NPR Graphics
PPT
CS 354 Performance Analysis
PPT
CS 354 Acceleration Structures
PPT
CS 354 Global Illumination
PPT
CS 354 Ray Casting & Tracing
D11: a high-performance, protocol-optional, transport-optional, window system...
Computers, Graphics, Engineering, Math, and Video Games for High School Students
Virtual Reality Features of NVIDIA GPUs
OpenGL for 2015
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
Accelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
SIGGRAPH Asia 2012: GPU-accelerated Path Rendering
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
Programming with NV_path_rendering: An Annex to the SIGGRAPH Asia 2012 paper...
GPU accelerated path rendering fastforward
GPU-accelerated Path Rendering
SIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
SIGGRAPH 2012: NVIDIA OpenGL for 2012
GTC 2012: GPU-Accelerated Path Rendering
CS 354 Final Exam Review
CS 354 Surfaces, Programmable Tessellation, and NPR Graphics
CS 354 Performance Analysis
CS 354 Acceleration Structures
CS 354 Global Illumination
CS 354 Ray Casting & Tracing

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Encapsulation theory and applications.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
A Presentation on Artificial Intelligence
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Modernizing your data center with Dell and AMD
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Encapsulation theory and applications.pdf
Review of recent advances in non-invasive hemoglobin estimation
A Presentation on Artificial Intelligence
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Modernizing your data center with Dell and AMD
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Migrating from OpenGL to Vulkan

  • 1. Mark Kilgard, January 19, 2016 Migrating from OpenGL to Vulkan
  • 2. 2 About the Speaker Mark Kilgard Principal Graphics Software Engineer in Austin, Texas Long-time OpenGL driver developer at NVIDIA Author and implementer of many OpenGL extensions Collaborated on the development of Cg First commercial GPU shading language Recently working on GPU-accelerated vector graphics (Yes, and wrote GLUT in ages past) Who is this guy?
  • 3. 3 Motivation for Talk What kinds of apps benefit from Vulkan? How to prepare your OpenGL code base to transition to Vulkan How various common OpenGL usage scenarios are re-thought in Vulkan Re-thinking your application structure for Vulkan Coming from OpenGL, Preparing for Vulkan
  • 5. 5 Analogy Fixed-function OpenGL Pre-assembled toy car fun out of the box, not much room for customization
  • 6. 6 Analogy Modern AZDO OpenGL with Programmable Shaders LEGO Kit you build it yourself, comes with plenty of useful, pre-shaped pieces AZDO = Approaching Zero Driver Overhead
  • 7. 7 Analogy Vulkan Pine Wood Derby Kit you build it yourself to race from raw materials power tools used to assemble, adult supervision highly recommended
  • 8. 8 Analogy Different Valid Approaches Fixed-function OpenGL Modern AZDO OpenGL with Programmable Shaders Vulkan
  • 9. 9 Beneficial Vulkan Scenarios Is your graphics work CPU bound? Can your graphics work creation be parallelized? start yes Vulkan friendly yes Has Parallelizable CPU-bound Graphics Work
  • 10. 10 Beneficial Vulkan Scenarios Your graphics platform is fixed You’ll do whatever it takes to squeeze out max perf. start yes Vulkan friendly yes Maximizing a Graphics Platform Budget
  • 11. 11 Beneficial Vulkan Scenarios You put a premium on avoiding hitches You can manage your graphics resource allocations start yes Vulkan friendly yes Managing Predictable Performance, Free of Hitching
  • 12. 12 Unlikely to Benefit 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 direct 3D graphics API dependencies • Consider using an engine targeting Vulkan, instead of dealing with Vulkan yourself Scenarios to Reconsider Coding to Vulkan
  • 13. 13 First Steps Migrating to Vulkan Eliminate fixed-function Source all geometry from vertex buffer objects (VBOs) with vertex arrays Use all programmable GLSL shaders with layout() qualifiers Consider using samplers Do all rendering into framebuffer objects (FBOs) Stay within the non-deprecated subset (e.g. no GL_QUADS, for now…) Think about better batching & classify all your render states Avoid depending on OpenGL context state All pretty sensible advice, even if you stick with OpenGL Modernize Your OpenGL
  • 14. 14 Next Steps Migrating to Vulkan Think about how your application would handle losing the GPU Similar to ARB_robustness Profile your application, understand what portions are CPU and GPU bound Vulkan most benefits apps bottlenecked on graphics work creation and driver validation Is that your app? Adopt common features available in both OpenGL and Vulkan first in OpenGL Proving out tessellation or multi-draw-indirect probably easier first in your stable OpenGL code base Again all pretty sensible advice, even if you stick with OpenGL Modernize Your OpenGL
  • 15. 15 Thinking about Vulkan vs. OpenGL OpenGL, largely understood in terms of Its API, functions for commands and queries And how that API interacts with the OpenGL state machine OpenGL has lots of implicit synchronization Errors handled largely by ignoring command OpenGL API manages various objects But allocation of underlying device resources largely handled by driver + OpenGL Has a Well-established Approach Originally client-server
  • 16. 16 Thinking about Vulkan vs. OpenGL Vulkan constructs and submits work for a graphics device Idealized graphics + compute device Requires application to maintain valid Vulkan usage for proper operation Not expected to behave correctly in face of errors, justified for performance Instead of updating state machine, Vulkan is about establishing working relationships between objects Pre-validates operation of actions Vulkan Plays By Different Rules Explicit API Explicit memory management Explicit synchronization Explicit queuing of work Explicit management of buffer state with render passes Not client-server, explicitly depends on shared resources and memory
  • 17. 17 Truly Transitioning to Vulkan Much more graphics resource responsibility for the application You need to allocate from large device memory chunk You become responsible for proper explicit synchronization Fences, Barriers, Semaphores Barriers are probably the hardest to appreciate Everything has to be structured as pipeline state objects (PSOs) Understand render passes Think how parallel command buffer generation would operate You become responsible for multi-threaded exclusion of your Vulkan objects Vulkan Done Right Rethinks Entire Graphics Rendering
  • 18. 18 Common Graphics Tasks Loading a mipmapped texture Loading a vertex buffer object for rendering Choosing a shader representation and loading shaders Initiating compute work Managing a memory sub-allocator Thinking about render passes Managing Predictable Performance, Free of Hitching
  • 19. 19 Loading a Texture Well traveled path via OpenGL 3.0 glBindTexture(GL_TEXTURE_2D, texture_name); glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, width, height, /*border*/0, GL_UNSIGNED_BYTE, GL_RGBA, pixels); glGenerateMipmap(GL_TEXTURE_2D); Does more than you think it does The OpenGL View
  • 20. 20 Logical Operations to Load a Texture 1. Create host driver objects corresponding to texture/sampler/image/view/layout 2. Copy call’s image to staging memory accessible to host+device 3. (OpenGL might do a format conversion and pixel transfer) 4. Allocates device memory for texture image for texturing 5. Copy image from host+device memory to device memory for texturing 6. Allocate device resources for sampler 7. Generate mipmap levels
  • 21. 21 Various Vulkan Objects “inside” a Texture No such thing as “VkTexture” Instead Texture state in Vulkan = VkDescriptorImageInfo Combines: VkSampler + VkImageView + VkImageLayout Sampling state + Image state + Current image layout Texture binding in Vulkan = part of VkDescriptorSetLayoutBinding Contained with VkDescriptorSetLayout Building Up the OpenGL Texture Object from Vulkan Concepts OpenGL textures are opaque, so lacks an exposed image layout
  • 22. 22 Allocating Image Memory for Texture VkImage VkDevice vkGetImageMemoryRequirements VkMemoryRequirements vkBindImageMemory VkDeviceMemory vkAllocateMemory vkCreateImage With VulkanNaïve approach! vkAllocateMemory is expensive. Demos may do this, but real apps should sub-allocate from large VkDeviceMemory allocations See next slide…
  • 23. 23 Sub-allocating Image Memory for Texture VkImage VkDevice vkGetImageMemoryRequirements VkMemoryRequirements vkBindImageMemory VkDeviceMemory vkAllocateMemory vkCreateImage One large device memory allocation, assigned to multiple images Proper approach! vkAllocateMemory makes a large allocation with and then sub-allocates enough memory for the image So who writes the sub-allocator? You do!
  • 24. 24 Binding Descriptor Sets for a Texture VkDescriptorImageInfoVkSampler VkImageView VkImageLayout VkDescriptorSetLayout VkWriteDescriptorSet vkAllocateDescriptorSets VkPipelineLayout vkCmdBindDescriptorSets VkCommandBuffer vkCreateDescriptorSetLayout vkCreatePipelineLayout vkUpdateDescriptorSets So Pipeline Sees Texture
  • 25. 25 Establishing Sampler Bindings for a Pipeline Object VkDescriptorSetLayoutBinding for a sampler binding vkCreateDescriptorSetLayout VkDescriptorSetLayout vkCreatePipelineLayout VkPipelineLayout vkCreateGraphicsPipelines VkPipeline vkCmdBindPipeline Vulkan Pipelines Need to Know How They Will Bind Samplers VkCommandBuffer
  • 26. 26 Base Level Specification + Mipmap Generation VkCommandBuffer vkCmdBlitImage vkCmdPipelineBarrier vkCmdBlitImage for each upper mipmap level vkCmdPipelineBarrier Vulkan Command Buffer Orchestrates Blit + Downsamples staging image texture base image texture base image level 1 mipmap level i mipmap level i+1 mipmap
  • 27. 27 Binding to a Vertex Array Object (VAO) and Rendering from Vertex Arrays Well traveled path via OpenGL 3.0 glBindVertexArray(vertex_array_object); glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, indices); Again, does more than you think it does The OpenGL View
  • 28. 28 Allocating Buffer Memory for VBO VkBuffer VkDevice vkGetBufferMemoryRequirements VkMemoryRequirements vkBindBufferMemory VkDeviceMemory vkAllocateMemory VkCreateBuffer With VulkanNaïve approach! vkAllocateMemory is expensive. Demos may do this, but real apps should sub-allocate from large VkDeviceMemory allocations
  • 29. 29 Binding Vertex State To A Pipeline VkVertexInputBindingDescription VkVertexInputAttributeDescription VkPipelineVertexInputStateCreateInfo VkGraphicsPipelineCreateInfo VkGraphicsPipeline vkCreateGraphicsPipelines So Pipeline Sees Vertex Input State VkPipelineShaderStageCreateInfo VkPipelineInputAssemblyStateCreateInfo VkPipelineViewportStateCreateInfo VkPipelineRasterizationStateCreateInfo VkPipelineMultisampleStateCreateInfo VkPipelineDepthStencilStateCreateInfo VkPipelineColorBlendStateCreateInfo VkPipelineDynamicStateCreateInfo
  • 30. 30 Binding Vertex Buffer For Drawing For the vertex input statevkCmdBindPipeline Buffer Binding Is Performed With The Command Queue vkCmdBindVertexBuffers For the vertex data vkCmdBeginRenderPass vkCmdBindDescriptorSets vkCmdDrawIndexed vkCmdEndRenderPass Begin Drawing Draw End Drawing
  • 31. 31 Big Features, Ready for Vulkan Compute shaders Tessellation shaders Geometry shaders Sparse resources (textures and buffers) In OpenGL 4.5 today Vulkan has the same big features, just different API Mostly the same GLSL can be used in either case Prototype in OpenGL now, Enable in Vulkan next
  • 32. 32 Thinking about render passes OpenGL just has commands to draw primitives No explicit notion of a render pass in OpenGL API Vulkan does have render passes, VkRenderPass Includes notion of sub-passes Designed to facilitate tiling architectures Bounds the lifetime of intermediate framebuffer results Allows iterating over subpasses (chunking) within a render pass on a per tile basis In most cases, you won’t need to deal with subpasses How do rendering operations affect the retained framebuffer?
  • 33. 33 Render Pass Describes the list attachments the render pass involves Each attachment can specify How the attachment state is initialized (loaded, cleared, dont-care) How the attach state is stored (store, or dont-care) Don’t-care allows framebuffer intermediates to be discarded E.g. depth buffer not needed after the render pass How the layout of attachment could change Sub-pass dependencies indicate involvement of attachments within a subpass What does a Vulkan Render Pass Provide?
  • 34. 34 Render Passes vkCmdBeginRenderPass vkCmdEndRenderPass vkCmdBeginRenderPass vkCmdNextSubpass vkCmdNextSubpass vkCmdEndRenderPass Monolithic or Could Have Sub-passes Each subpass has its own description and dependencies Simple render pass, no subpasses Complex render pass, with multiple subpasses
  • 35. 35 Vulkan Application Structure Parallel Command Buffer Generation Traditional single-threaded OpenGL app structure update scene draw scene single thread Possible multi-threaded Vulkan app structure collect secondary command buffers distribute update scene work submit command buffer build secondary command buffer work thread build secondary command buffer work thread build secondary command buffer work thread build secondary command buffer work thread
  • 36. 36 Conclusions Vulkan is a radical departure from OpenGL Modernizing your OpenGL code base is definitely good for moving to Vulkan But it will take more work than that! Vulkan’s explicitness makes simple operations like a texture bind quite involved Think about multi-threaded command buffer creation Get Ready for Vulkan