1 Vulkan Tutorial - English
1 Vulkan Tutorial - English
Hyokuen Lee
Senior Graphics Engineer ([email protected])
Minwook Kim
Senior Graphics Engineer ([email protected])
Contents
• Introduction
• Development Environment
• Initialization
• Drawing a Triangle
• Drawing a Rectangle
• Texture Mapping
Samsung Electronics
Introduction
Vulkan
Samsung Electronics
Vulkan Features
Low CPU Overhead
Samsung Electronics
Vulkan Features
Multi-Core Efficiency
Samsung Electronics
Vulkan Features
Layer Structure
Samsung Electronics
Vulkan Features
SPIR-V
Samsung Electronics
Development Environment
Samsung Electronics
Install Development Tools
• Installation
- Cmake (https://cmake.org/download/ )
- Python 3 (https://www.python.org/downloads/ )
Samsung Electronics
Build Vulkan SDK
- go to C:\VulkanSDK\1.0.17.0\glslang\build
: cmake –G “Visual Studio 14 Win64” ..
: build all Debug/Release x64
- go to C:\VulkanSDK\1.0.17.0\spirv-tools\build
: cmake –G “Visual Studio 14 Win64” .. Create the folder “build” in person
: build all Debug/Release x64
- go to C:\VulkanSDK\1.0.17.0\Samples\build
: cmake –G “Visual Studio 14 Win64” ..
: build all Debug/Release x64
Samsung Electronics
Visual Studio Configuration
• Set the Vulkan / GLM header path
Samsung Electronics
Visual Studio Configuration
• Set the Vulkan library path
Samsung Electronics
Visual Studio Configuration
• Vulkan library
Samsung Electronics
Initialization
• Instance
Samsung Electronics
API Naming Convention
Standard Prefixes
Extension
2) Define
VK_KHR_mirror_clamp_to_edge / VK_EXT_debug_marker
Samsung Electronics
Instance
Instance
Creating an Instance
Samsung Electronics
Instance
Create Destroy
Samsung Electronics
createInstance()
Samsung Electronics
checkError()
Samsung Electronics
destroyInstance()
Samsung Electronics
Device
Physical Device
Logical Device
Samsung Electronics
Queue / Queue Family
Queue
Queue Family
Samsung Electronics
Physical Device
Selecting Physical Device
- Pick a GPU
- Check a support of queue family for graphics commands
Samsung Electronics
Physical Device
Create Destroy
Samsung Electronics
selectPhysicalDevice()
Samsung Electronics
isDeviceSuitable()
Samsung Electronics
findQueueFamilies() #1 See also : Present Queue Family extension version
Samsung Electronics
Logical Device
Creating a Logical Device
Samsung Electronics
Logical Device
Create Destroy
Samsung Electronics
createLogicalDevice()
- Queues to use
- Device extensions to use
- Device features to use
Samsung Electronics
destroyLogicalDevice()
Samsung Electronics
Drawing a Triangle
• Present Queue
• Swapchain / Framebuffer
• Command Buffer
• Render Pass
• Graphics Pipeline
• Shader (SPIR-V)
• Swapchain Recreation
Samsung Electronics
Drawing a Triangle
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
BeginCommandBuffer
Begin RenderPass
Bind GraphicsPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Window System / Surface
Window Surface
- Vulkan is platform agnostic API, each platform can not interface directly
with the window system
- To present rendered screen, we need to use the WSI (Window System
Integration) extension
- Also need to use rendering target surface fit with each platform
- VK_KHR_surface
: Vulkan WSI (Window System Integration)
- VK_KHR_win32_surface
: Extension for using Win32 system platform Surface Window
※ VK_KHR_xcb_surface(Linux) / VK_KHR_android_surface(android)
Samsung Electronics
Window System / Surface
Create Destroy
Samsung Electronics
Creating the Surface
Creating Surface
Samsung Electronics
Creating the Surface
Create Destroy
Samsung Electronics
Present Queue
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
BeginCommandBuffer
Begin RenderPass
Bind GraphicsPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Present Queue
Present Queue
- Present queue families may or may not be the same as graphics queue families
- Check for present queue family using
vkGetPhysicalDeviceSurfaceSupportKHR( )
Samsung Electronics
findQueueFamilies() #2 See also : Graphics Queue Family version
Samsung Electronics
Swapchain
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
CMD Buffer
Swapchain CMD Buffer
Swapchain CMD Buffer
Swapchain
ImageView ImageView ImageView
( VKImageView ) ( VKImageView ) ( VKImageView )
BeginCommandBuffer
Begin RenderPass
Framebuffer Framebuffer Framebuffer
Bind GraphicPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Swapchain
Swapchain
- Render to the image running drawing operation in graphics queue, and submit
it to present queue
Samsung Electronics
Swapchain
Swapchain Image
Samsung Electronics
Querying for Swapchain Support
Querying for Swapchain Support
1. Surface capabilities
2. Surface format
3. Presentation Mode
Samsung Electronics
Choosing Swapchain Support 1/3
Surface Capabilities (VkSurfaceCapabilitiesKHR)
Samsung Electronics
Choosing Swapchain Support 2/3
Surface Format (VkSurfaceFormatKHR)
1) format (VkFormat)
- VK_FORMAT_B8G8R8A8_UNORM
2) colorSpace (VkColorSpaceKHR)
- VK_COLOR_SPACE_SRGB_NONLINEAR_KHR
Samsung Electronics
Choosing Swapchain Support 3/3
Presentation Mode
Samsung Electronics
Swapchain
Create Destroy
Samsung Electronics
createSwapchain() #1
Samsung Electronics
createSwapchain() #2
Samsung Electronics
createSwapchain() #3
destroySwapchain()
Samsung Electronics
Swapchain Image View
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
CMD Buffer
Swapchain CMD Buffer
Swapchain CMD Buffer
Swapchain
ImageView ImageView ImageView
( VKImageView ) ( VKImageView ) ( VKImageView )
BeginCommandBuffer
Begin RenderPass
Framebuffer Framebuffer Framebuffer
Bind GraphicPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Swapchain Image View
Create Destroy
Samsung Electronics
createImageViews()
Samsung Electronics
Framebuffer
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
CMD Buffer
Swapchain CMD Buffer
Swapchain CMD Buffer
Swapchain
ImageView ImageView ImageView
( VKImageView ) ( VKImageView ) ( VKImageView )
BeginCommandBuffer
Begin RenderPass
Framebuffer Framebuffer Framebuffer
Bind GraphicPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Framebuffer
Framebuffer
- Target buffer for color, depth, stencil target
- A frame buffer should be created fitting all swapchain image views
Creating Framebuffer
Samsung Electronics
Framebuffer
Create Destroy
Samsung Electronics
createFramebuffers() / destroyFramebuffers()
Samsung Electronics
Command Buffer
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
BeginCommandBuffer
Begin RenderPass
Bind GraphicsPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Command Buffer
Command Buffers
Command Pools
Samsung Electronics
Command Pool
Create Destroy
Samsung Electronics
createCommandPool() /
destroyCommandPool()
Samsung Electronics
Command Buffer
Create Destroy
Samsung Electronics
createCommandBuffers()
Samsung Electronics
Render Pass
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
BeginCommandBuffer
Begin RenderPass
Bind GraphicsPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Render Pass
Render Pass
1) Attachment description
2) Subpass description / dependency
3) Render pass create info
4) Render pass
Samsung Electronics
Render Pass
Create Destroy
Samsung Electronics
createRenderPass() #1
LOAD_OP_LOAD :
LOAD_OP_CLEAR :
LOAD_OP_DONT_CARE :
STORE_OP_STORE :
STORE_OP_DONT_CARE :
Samsung Electronics
createRenderPass() #2
Samsung Electronics
Graphics Pipeline
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
BeginCommandBuffer
Begin RenderPass
Bind GraphicsPipeline
Draw
End RenderPass
EndCommandBuffer
Samsung Electronics
Graphics Pipeline
Index , vertex
Input assembler
Graphics Pipeline Buffer
Vertex Shader
- Rasterizing 3D object to 2D image
Tessellation
- Vulkan explicitly defines each step of the
graphics pipeline Geometry Shader
ViewPort / Scissor
Rasterization
Fragment Shader
Samsung Electronics
Shader / SPIR-V
Vertex shader / Fragment shader
SPIR-V
Samsung Electronics
Vertex Shader (shader.vert) See also : Vertex buffer version
Vertex attributes
- coordinate
- color
Samsung Electronics
Fragment Shader (shader.frag)
Shader Compile
Samsung Electronics
createShaderModule()
Samsung Electronics
Graphics Pipeline
Create Destroy
Samsung Electronics
createGraphicsPipeline() #1
Samsung Electronics
Graphics Pipeline
Index , vertex
Input assembler
Buffer
Tessellation Skip
ViewPort / Scissor
Rasterization
Samsung Electronics
createGraphicsPipeline() #2
Samsung Electronics
createGraphicsPipeline() #3
Samsung Electronics
createGraphicsPipeline() #4
Samsung Electronics
createGraphicsPipeline() #5
Samsung Electronics
createGraphicsPipeline() #6
Samsung Electronics
Recording Command Buffer
BeginCommandBuffer
Begin RenderPass
Bind GraphicsPipeline
Draw
End RenderPass
EndCommandBuffer
ONE_TIME_SUBMIT_BIT
RENDER_PASS_CONTINUE_BIT
SIMULTANEOUS_USE_BIT
Command buffer can be reused
Samsung Electronics
recordingCommandBuffers()
Samsung Electronics
Recording Command Buffer
Create Destroy
Samsung Electronics
Drawing Frame
VkSurface (WSI)
0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image
vkQueueSubmit
2nd Semaphore
vkQueuePresentKHR
Samsung Electronics
Semaphore
Synchronization
Semaphore
Samsung Electronics
Semaphore
Create Destroy
Samsung Electronics
createSemaphores() / destroySemaphores()
Samsung Electronics
drawFrame()
Samsung Electronics
Swapchain Recreation
Swapchain Recreation
Samsung Electronics
Swapchain Recreation (Resize case)
Swapchain Dependency
Surface Changed Swapchain
Buffers
- Window changed > Surface changed > Updating
swapchain Render Pass
- Surface format changed > Updating render pass
Graphics Pipeline
Samsung Electronics
reInitSwapchain()
Samsung Electronics
createSwapchain()
Samsung Electronics
Swapchain Recreation
Create Destroy Recreate
Samsung Electronics
Drawing a Triangle
• Present Queue
• Swapchain / Framebuffer
• Command Buffer
• Render Pass
• Graphics Pipeline
• Shader (SPIR-V)
• Swapchain Recreation
Samsung Electronics
Drawing a Rectangle
• Vertex Buffer
• Staging Buffer
• Index Buffer
Samsung Electronics
Vertex Buffer
Vertex Buffer
Samsung Electronics
Update the Vertex Shader
Vertex Shader
- Vertex attribute : get per-vertex data from the program (vertex buffer)
- Use the “in” keyword to get attributes from vertex buffer
- Compile shader again after updating vertex shader
Samsung Electronics
Vertex Shader (shader.vert) See also : Fixed version
Vertex attributes
- coordinate
- color
Samsung Electronics
Define the Vertex Data
Vertex Data
- Use GLM library to use linear algebra (vector, matrix type, etc.)
Samsung Electronics
Define the Vertex data
Samsung Electronics
Vertex Binding/Attribute Description
Samsung Electronics
Vertex Binding/Attribute Description
Samsung Electronics
Define the Vertex Data
Create Destroy
Samsung Electronics
createGraphicsPipeline()
Samsung Electronics
Create the Vertex Buffer
Vulkan Buffer
Samsung Electronics
Create the Vertex Buffer
Create Destroy
Samsung Electronics
createVertexBuffer()
Samsung Electronics
findMemoryType()
Samsung Electronics
destroyVertexBuffer()
Samsung Electronics
Drawing using the Vertex Buffer
Samsung Electronics
Drawing using the Vertex Buffer
Create Destroy
Samsung Electronics
recordCommandBuffers()
Samsung Electronics
Staging Buffer
Memory Optimization
Samsung Electronics
createBuffer() / destroyBuffer()
Samsung Electronics
copyBuffer()
Samsung Electronics
createVertexBuffer()
Samsung Electronics
Index Buffer
Index Data
- If drawing with vertex data only, there can be a lot of vertex data duplication
- The solution is to use index data
Samsung Electronics
Define the Index data
Samsung Electronics
Index Buffer
Create Destroy
Samsung Electronics
createIndexBuffer()
Samsung Electronics
destroyIndexBuffer()
Samsung Electronics
Drawing using the Index Buffer
Create Destroy
Samsung Electronics
recordCommandBuffers()
Samsung Electronics
Drawing a Rectangle
• Vertex Buffer
• Staging Buffer
• Index Buffer
Samsung Electronics
Rotation and 3D Projection
• Resource Descriptor
Samsung Electronics
Resource Descriptor
Resource Descriptor
Samsung Electronics
Descriptor Set / Descriptor Set Layout
Descriptor Set Layout
Descriptor Pool
Descriptor Set
Samsung Electronics
Descriptor Set / Descriptor Set Layout
Using Descriptor
Samsung Electronics
Resource Descriptor
Create
Samsung Electronics
Update the Vertex Shader
Vertex Shader
Samsung Electronics
Vertex Shader (shader.vert)
MVP transformation
Samsung Electronics
Define the Uniform Data
Uniform Data
- User GLM library to use uniform data (matrix type) in vertex shader
Samsung Electronics
Use the Uniform data
Samsung Electronics
Create the Descriptor Set Layout
Descriptor Set Layout
- Meta-information of resources
e.g.) binding information, resource type(uniform, texture, etc.), usage stage
(pipeline stage)
1) Set the descriptor set layout binding information for each resource type
(VkDescriptorSetLayoutBinding)
2) Set descriptor set layout create information to bind more than one descriptor set
layout binding information (VkDescriptorSetLayoutCreateInfo)
3) Create the descriptor set layout
4) Set the descriptor set layout information in the graphics pipeline
Samsung Electronics
Create the Descriptor Set Layout
Samsung Electronics
Create the Descriptor Set Layout
Create Destroy
Samsung Electronics
createDescriptorSetLayout()
destroyDescriptorSetLayout()
Samsung Electronics
Specify the Descriptor Set Layout
Create Destroy
Samsung Electronics
createGraphicsPipeline()
Samsung Electronics
Create the Uniform Buffer Object
Uniform Buffer Object (UBO)
▶ In every frame, copy uniform data to the UBO so the shader can access it
Samsung Electronics
Create the Uniform Buffer Object
Create Destroy
Samsung Electronics
createStagingUniformBuffer()
destroyStagingUniformBuffer()
Samsung Electronics
createUniformBuffer()
destroyUniformBuffer()
Samsung Electronics
updateUniformBuffer() Call this every frame before
calling the drawFrame()
Samsung Electronics
createGraphicsPipeline()
Invert Y-coordinate
Samsung Electronics
Create the Descriptor Pool
Descriptor Pool
Samsung Electronics
Create the Descriptor Pool
Create Destroy
Samsung Electronics
createDescriptorPool()
destroyDescriptorPool()
Samsung Electronics
Create the Descriptor Set
Descriptor Set
- The descriptor set is created from the descriptor set layout and descriptor pool
- Allocate the buffer after creating the descriptor set
Samsung Electronics
Create the Descriptor Set
Create Destroy
Samsung Electronics
createDescriptorSet()
Samsung Electronics
Descriptor Set Binding
Descriptor Set Binding
Samsung Electronics
Bind the Descriptor Set
Create Destroy
Samsung Electronics
recordCommandBuffers()
Samsung Electronics
Rotation and 3D Projection
• Resource Descriptor
Samsung Electronics
Texture Mapping
• Texture Image
• Sampler
Samsung Electronics
Update the Fragment Shader
Fragment Shader
Samsung Electronics
Fragment Shader (shader.frag)
Samsung Electronics
Define the Texture Data
Vertex Data
Samsung Electronics
Define the Texture data
Samsung Electronics
Texture Image
Create the Texture Image
Samsung Electronics
Texture Image
Samsung Electronics
createImage()
Samsung Electronics
destroyImage()
Samsung Electronics
copyImage()
Samsung Electronics
beginSingleCommandBuffer() / endSingleCommandBuffer()
Samsung Electronics
Texture Image
Image Layout
Pipeline barrier
Samsung Electronics
transitionImageLayout() #1
Layout transition
Samsung Electronics
transitionImageLayout() #2
Samsung Electronics
Texture Image
Create Destroy
Samsung Electronics
createTextureImage() #1
Samsung Electronics
createTextureImage() #2
Samsung Electronics
createTextureImage() #3
Samsung Electronics
Texture Image View
Image View
Samsung Electronics
Texture Image View
Create Destroy
Samsung Electronics
createTextureImageView()
destroyTextureImageViews()
Samsung Electronics
createImageView()
Samsung Electronics
Sampler
Sampler
- Shader does not sample a texture image directly, but accesses it through a sampler
- Samplers support texture filter, mipmap, wrap mode
Samsung Electronics
Sampler
Create Destroy
Samsung Electronics
createTextureSampler()
destroyTextureSampler()
Samsung Electronics
Texture Descriptor Set
Texture Descriptor Set
- Use descriptor sets to deliver texture image views and samplers to a shader
Using Descriptor
Samsung Electronics
Resource Descriptor
Create
Samsung Electronics
createDescriptorSetLayout()
Samsung Electronics
createGraphicsPipeline()
Samsung Electronics
Create the Descriptor Pool
Descriptor Pool
Samsung Electronics
Create the Descriptor Pool
Create Destroy
Samsung Electronics
createDescriptorPool()
Samsung Electronics
Create the Descriptor Set
Descriptor Set
- Create the
texture type
descriptor set
Samsung Electronics
Create the Descriptor Set
Create Destroy
Samsung Electronics
createDescriptorSet()
Samsung Electronics
Descriptor Set Binding
Descriptor Set Binding
Samsung Electronics
Bind the Descriptor Set
Create Destroy
Samsung Electronics
recordCommandBuffers()
Samsung Electronics
Texture Mapping
• Texture Image
• Sampler
Samsung Electronics
Enable the Standard Validation Layer
Vulkan Design Concepts
Validation Layer
Samsung Electronics
Vulkan API Hooking Example
Samsung Electronics
Standard Validation Layer
Samsung Electronics
Enable the Standard Validation Layer
Create Destroy
Samsung Electronics
setupValidationLayers()
VK_EXT_debug_report
User-defined
callback function
Samsung Electronics
createInstance()
Samsung Electronics
checkInstanceLayerSupport()
Samsung Electronics
checkInstanceExtensionSupport()
Samsung Electronics
Set the Debug Report Callback
Debug Report Callback
- Define an error report callback that will be called when error detected in
validation layer
Samsung Electronics
Create the Debug Report Callback
Create Destroy
Samsung Electronics
createDebugReportCallback() / destroyDebugReportCallback()
Samsung Electronics
VulkanDebugCallback()
Set in VkDebugReportCallbackCreateInfoEXT
Samsung Electronics
Appendix
Samsung Electronics