0% found this document useful (0 votes)
215 views210 pages

1 Vulkan Tutorial - English

The document provides an overview of Vulkan initialization and rendering a triangle. It discusses creating an instance, selecting a physical device, creating a logical device and queues. It also covers creating a window surface, swapchain and using command buffers to begin and end a render pass for drawing geometry. The goal is to explain the basic Vulkan setup and rendering process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
215 views210 pages

1 Vulkan Tutorial - English

The document provides an overview of Vulkan initialization and rendering a triangle. It discusses creating an instance, selecting a physical device, creating a logical device and queues. It also covers creating a window surface, swapchain and using command buffers to begin and end a render pass for drawing geometry. The goal is to explain the basic Vulkan setup and rendering process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 210

Vulkan Tutorial

2016 Khronos Seoul DevU


SAMSUNG Electronics

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

• Rotation and 3D Projection

• Texture Mapping

• Standard Validation Layer

Samsung Electronics
Introduction
Vulkan

- Low CPU overhead, cross-platform 3D graphics and compute API

Samsung Electronics
Vulkan Features
Low CPU Overhead

- Low level API reducing CPU overhead

GDC 2015 Khronos Vulkan Session

Samsung Electronics
Vulkan Features
Multi-Core Efficiency

- Multi-threaded command submission to queue

Samsung Electronics
Vulkan Features
Layer Structure

- User can enable / add layers


(Vulkan API does not do basic error detection or dependency tracking for low-
overhead)

Samsung Electronics
Vulkan Features

SPIR-V

- Standard Portable Intermediate Representation


- Binary shading language
- Use pre-compiled shader (No need to compile shaders at run-time)

Samsung Electronics
Development Environment

• Install Development Tools

• Build Vulkan SDK

• Visual Studio Configuration

Samsung Electronics
Install Development Tools

• Installation

- Vulkan SDK (https://vulkan.lunarg.com/app/download )

- Cmake (https://cmake.org/download/ )

- Python 3 (https://www.python.org/downloads/ )

- GLM library (http://glm.g-truc.net/0.9.8/index.html )

- Vulkan Graphic Driver


: Nvdia (https://developer.nvidia.com/vulkan-driver )
: AMD (http://www.amd.com/en-us/innovations/software-
technologies/technologies-gaming/vulkan )

Samsung Electronics
Build Vulkan SDK

• Build Vulkan SDK (based on Visual Studio 2015, 64-bit computer)

- 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

• Device (Physical Device / Logical Device)

• Queue / Queue Family

Samsung Electronics
API Naming Convention
Standard Prefixes

VK : Define / Vk : Type structure / vk : Function


p / PFN / pfn : Pointer, function pointer
vkCmd : Commands that will be stored in the command buffer

Extension

1) Type structure / Function


VkSurfaceFormatKHR / vkDestorySurfaceKHR( )

2) Define
VK_KHR_mirror_clamp_to_edge / VK_EXT_debug_marker

Samsung Electronics
Instance
Instance

- Connection between vulkan and the application


- Including simple application information, instance layers and instance
extensions

Creating an Instance

1) Enable an instance layer / extension


- Check instance layer support
- Check instance extension support
2) Create an instance
- Set application information
- Set instance layer and extension information

Samsung Electronics
Instance
Create Destroy

Samsung Electronics
createInstance()

Samsung Electronics
checkError()

Samsung Electronics
destroyInstance()

Samsung Electronics
Device
Physical Device

- Select physical devices through the instance


- “Physical device” means GPU in the system
- Multiple GPUs can be used in Vulkan

Logical Device

- Create logical devices through physical devices


- Logical connection between a Vulkan program and GPU
(Main handle when using the Vulkan API)
- Multiple logical devices can be created through physical device

Samsung Electronics
Queue / Queue Family
Queue

- Most operations (drawing, texturing, memory transfer, etc.) are


encapsulated in a command buffer, which is submitted to a queue

Queue Family

- Different queue families for different combinations of queue capabilities


- Each queue family allows specific types of operation
e.g.) Queue family 0 for drawing commands and compute commands,
queue family 1 for memory transfer

Samsung Electronics
Physical Device
Selecting Physical Device

- Pick a GPU
- Check a support of queue family for graphics commands

1) Enumerate physical devices (GPUs) available in the system


2) Check the graphics queue family support
- Check VK_QUEUE_GRAPHICS_BIT flag
3) Check GPU properties and features

Samsung Electronics
Physical Device
Create Destroy

※ Destroy operation is not needed for physical


device selection

Samsung Electronics
selectPhysicalDevice()

Check the graphics queue family support

Samsung Electronics
isDeviceSuitable()

Check the graphics queue family support

Samsung Electronics
findQueueFamilies() #1 See also : Present Queue Family extension version

Check the VK_QUEUE_GRAPHICS_BIT flag

Samsung Electronics
Logical Device
Creating a Logical Device

- Logical connection between a Vulkan program and GPU


(Main handle when using the Vulkan API)

- Creating a logical device


1) Specify queues to use (create queue from queue family)
2) Specify device extensions to use
3) Specify device features to use
4) Create a logical device

Samsung Electronics
Logical Device
Create Destroy

Samsung Electronics
createLogicalDevice()

- Queues to use
- Device extensions to use
- Device features to use

Get the handle of the queue


created during logical device
creation

Samsung Electronics
destroyLogicalDevice()

Samsung Electronics
Drawing a Triangle

• Window System / Surface

• 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

Submit Submit Submit

CMD Buffer CMD Buffer CMD Buffer

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

Device Extension for Presentation

- 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

- We need to create a VkSurfaceKHR surface


- Each platform needs a different function call to create a VkSurfaceKHR:
Win32 : vkCreateWin32SurfaceKHR( )
Android : vkCreateAndroidSurfaceKHR( )
Linux : vkCreateXcbSurfaceKHR( )

Samsung Electronics
Creating the Surface
Create Destroy

Samsung Electronics
Present Queue

VkSurface (WSI)

0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image

Submit Submit Submit

CMD Buffer CMD Buffer CMD Buffer

BeginCommandBuffer

Begin RenderPass

Bind GraphicsPipeline

Draw

End RenderPass

EndCommandBuffer

Samsung Electronics
Present Queue
Present Queue

- Vulkan API uses a ‘Present queue’ to present a rendered screen to a Surface


- To present a rendered image to surface, we should submit to present queue

Present Queue Family

- 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

Submit Submit Submit

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

- Collection of images that can be presented to the presentation engine

- Synchronize rendered image with the refresh rate of the screen

- Render to the image running drawing operation in graphics queue, and submit
it to present queue

Samsung Electronics
Swapchain
Swapchain Image

- Image resource obtained from the swapchain

Swapchain Image View

- Additional information for swapchain


e.g.) RGBA component, view type(2D/3D), surface format, mipmap, image
array

Samsung Electronics
Querying for Swapchain Support
Querying for Swapchain Support

- 3 additional information are needed to create swapchain

1. Surface capabilities

2. Surface format

3. Presentation Mode

Samsung Electronics
Choosing Swapchain Support 1/3
Surface Capabilities (VkSurfaceCapabilitiesKHR)

- Use extent items in capabilities


- Example of surface capability

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

- Setting timing to send present queue


1) VK_PRESENT_MODE_IMMEDIATE_KHR
2) VK_PRESENT_MODE_FIFO_KHR (wait when queue full)
3) VK_PRESENT_MODE_FIFO_RELAXED_KHR (no wait when queue empty)
4) VK_PRESENT_MODE_MAILBOX_KHR (no wait when queue full)

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

Submit Submit Submit

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

Submit Submit Submit

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

- Swapchain image view (color, depth, stencil image view)


- Render pass object that declared the framebuffer attachment type
- Number of attachments and attachment objects, extent information

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

Submit Submit Submit

CMD Buffer CMD Buffer CMD Buffer

BeginCommandBuffer

Begin RenderPass

Bind GraphicsPipeline

Draw

End RenderPass

EndCommandBuffer

Samsung Electronics
Command Buffer
Command Buffers

- Vulkan commands are submitted to queues for execution


- Command buffer can be executed in multi-threaded command jobs
- Command buffers can be reused

Command Pools

- Manage memory for command buffer allocation


- Command buffers are allocated memory from a command pool

Samsung Electronics
Command Pool
Create Destroy

Samsung Electronics
createCommandPool() /
destroyCommandPool()

Samsung Electronics
Command Buffer
Create Destroy

※ Command buffers will automatically


terminated upon command pool destruction

Samsung Electronics
createCommandBuffers()

Samsung Electronics
Render Pass

VkSurface (WSI)

0
Swapchain Swapchain 1 Swapchain N
PresentQueue
Image Image Image

Submit Submit Submit

CMD Buffer CMD Buffer CMD Buffer

BeginCommandBuffer

Begin RenderPass

Bind GraphicsPipeline

Draw

End RenderPass

EndCommandBuffer

Samsung Electronics
Render Pass
Render Pass

• Specify framebuffer attachment type using for rendering


: Framebuffer attachment information (color buffer, depth buffer,
multisampling, etc.)
: Subpass information (consecutive rendering)

Render Pass Generation

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

Submit Submit Submit

CMD Buffer CMD Buffer CMD Buffer

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

Framebuffer Color Blending

Samsung Electronics
Shader / SPIR-V
Vertex shader / Fragment shader

- Converting binary type of SPIR-V based on GLSL (glslang compiler)

SPIR-V

- Pre-compiled bytecode format


- Intermediate language for parallel compute and graphics
- GLSL can be compiled to SPIR-V using the Khronos GLSL open source
compiler based on the GL_KHR_vulkan_glsl extension

Samsung Electronics
Vertex Shader (shader.vert) See also : Vertex buffer version

Vertex attributes
- coordinate
- color

Samsung Electronics
Fragment Shader (shader.frag)

Shader Compile

C:/VulkanSDK/1.0.17.0/Bin32/glslangValidator.exe –V shader.vert shader.frag


=> vert.spv / frag.spv

Samsung Electronics
createShaderModule()

Samsung Electronics
Graphics Pipeline
Create Destroy

Samsung Electronics
createGraphicsPipeline() #1

Samsung Electronics
Graphics Pipeline
Index , vertex
Input assembler
Buffer

Vertex Shader Done

Tessellation Skip

Geometry Shader Skip

ViewPort / Scissor

Rasterization

Fragment Shader Done

Framebuffer Color Blending

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

Submit Submit Submit

CMD Buffer CMD Buffer CMD Buffer


3
2
1
vkAcquireNextImageKHR
1st Semaphore

vkQueueSubmit
2nd Semaphore

vkQueuePresentKHR

Samsung Electronics
Semaphore
Synchronization

- Semaphore : Synchronize with each queue and command buffer sync


- Fence : Waiting for GPU ready on the CPU side (Fences are mainly designed to
synchronize your application itself with rendering operation and can be used by the
host to determine completion of execution of queue operations without GPU
involvement)

Semaphore

- Usually using two type of semaphore for drawing


1) Getting swapchain images (waiting for rendering)
2) Returning signal when rendering finished

Samsung Electronics
Semaphore
Create Destroy

Samsung Electronics
createSemaphores() / destroySemaphores()

Samsung Electronics
drawFrame()

Samsung Electronics
Swapchain Recreation
Swapchain Recreation

- If window surface size is changed, swapchain needs to be recreated


- Display rotation, pause/resume, scaling can also require 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

- Window/surface resolution changed > Updating buffer


objects
(framebuffer/depth buffer/command buffer)

- Viewport, scissor changed > Updating graphics pipeline

Samsung Electronics
reInitSwapchain()

Samsung Electronics
createSwapchain()

Samsung Electronics
Swapchain Recreation
Create Destroy Recreate

Samsung Electronics
Drawing a Triangle

• Window System / Surface

• 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

- Vertex shader get vertex attribute input using vertex buffer


(coordinates, color, texture coordinates, etc. )

Sequence using Vertex Buffer

- Modify the vertex shader


- Define a vertex data
- Set the vertex binding description
- Set the vertex attribute description
- Set the vertex binding / attribute description in vulkan graphics pipeline
- Create the vertex buffer
- Draw using the 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.)

※ GLM supports C++ compatible vector types (vec2, vec3, etc.)

Samsung Electronics
Define the Vertex data

Samsung Electronics
Vertex Binding/Attribute Description

Vertex Binding Description

- One unit of information in the data array


(Instance unit in the case of instance rendering)
- VkVertexInputBindingDescription

Vertex Attribute Description

- Specify binding index, location, format, offset information of vertex data


- VkVertexInputAttributeDescription

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

- Can store any data, GPU memory area


- User needs to allocate memory explicitly

Vertex Buffer Creation

1) Create the vertex buffer


2) Check the memory requirement
3) Allocate the memory (CPU accessible)
4) Binding the memory to the vertex buffer
5) Copy the vertex data to the vertex 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

Drawing with the Vulkan Buffer

- Bind the vertex buffer before running the drawing command

Samsung Electronics
Drawing using the Vertex Buffer
Create Destroy

Samsung Electronics
recordCommandBuffers()

Samsung Electronics
Staging Buffer
Memory Optimization

- CPU-accessible vertex buffer is not an optimized memory type


- Optimized memory type need to have
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT flag (potentially not CPU accessible)

Use the Staging Buffer

1) Create the staging buffer (GPU memory, CPU accessible)


2) Copy vertex data to staging buffer
3) Copy staging buffer to final vertex buffer (GPU memory, CPU not accessible)

Samsung Electronics
createBuffer() / destroyBuffer()

Samsung Electronics
copyBuffer()

Create a one time command


buffer

Copy the buffer

Submit a command buffer/


Free the command buffer

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

• Descriptor Set / Descriptor Set Layout

• Uniform Buffer Object (UBO)

Samsung Electronics
Resource Descriptor
Resource Descriptor

- Specify the resource in vulkan


- Shader can access resources(buffer, image, etc.) using resource descriptor

Typical Resource Descriptor

- UBO (Uniform Buffer Object)


: Update values in rendering time without modifying the shader
e.g.) transformation matrix(vertex shader)
- Texture image

Samsung Electronics
Descriptor Set / Descriptor Set Layout
Descriptor Set Layout

- Specify resource type to access in pipeline


(binding number, pipeline stage information, etc.)

Descriptor Pool

- Have the number of descriptor


- Descriptor sets are created from a descriptor pool

Descriptor Set

- Specify the actual resources bound to the resource descriptor


- Descriptor sets are created from a descriptor set layout and descriptor pool

Samsung Electronics
Descriptor Set / Descriptor Set Layout
Using Descriptor

1) Before graphics pipeline creation : Create the descriptor set layout


2) Graphics pipeline creation time : Specify the descriptor set layout
3) After graphics pipeline creation : Create the uniform buffer object , descriptor
pool and descriptor set
4) Rendering time : Descriptor binding

Samsung Electronics
Resource Descriptor
Create

Create the descriptor set layout

Specify the descriptor set layout

Create the Uniform Buffer Object


Create the Descriptor pool/descriptor
set

Bind the descriptor set

Samsung Electronics
Update the Vertex Shader

Vertex Shader

- Add uniform in vertex shader (MVP matrix)


- Apply MVP transformation in gl_Position
(Model-View-Projection : Rotation and 3D projection)

Samsung Electronics
Vertex Shader (shader.vert)

Uniform (MVP matrix)

(binding = 0) is used as index in


descriptor set layout

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)

Descriptor Set Layout Creation

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)

- Uniform information (MVP transformation) can be updated every frame


- Use the Vulkan buffer (VkBuffer) to deliver uniform information from CPU to GPU
- Uniform buffer creation process is similar to vertex buffer creation

▶ 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

- Create the descriptor pool prior to creating the descriptor set


- Descriptor pools are created for each resource type with some information
(descriptor type, the number of descriptors, etc.)

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

※ Descriptor sets are destroyed when


destroying the descriptor pool

Samsung Electronics
createDescriptorSet()

Samsung Electronics
Descriptor Set Binding
Descriptor Set Binding

- Bind the descriptor set at rendering time


- Submit binding command in the command buffer, before submitting drawing
commands

Samsung Electronics
Bind the Descriptor Set

Create Destroy

Samsung Electronics
recordCommandBuffers()

Samsung Electronics
Rotation and 3D Projection

• Resource Descriptor

• Descriptor Set / Descriptor Set Layout

• Uniform Buffer Object (UBO)

Samsung Electronics
Texture Mapping

• Texture Image

• Sampler

• Texture Descriptor Set

Samsung Electronics
Update the Fragment Shader

Fragment Shader

- Fragment shader gets per-fragment data from the program


- Deliver sampler and texture coordinates to shader

Samsung Electronics
Fragment Shader (shader.frag)

Samsung Electronics
Define the Texture Data

Vertex Data

- Add texture coordinates on vertex data

Samsung Electronics
Define the Texture data

Samsung Electronics
Texture Image
Create the Texture Image

- Texture image process is similar with vertex buffer creation


: VkImage → Image object handle
: VkDeviceMemory → Memory object that has actual image data

Texture Image Creation Process

1) Read image data from image file


2) Create the texture image object
3) Copy image data to the texture image object

Samsung Electronics
Texture Image

Texture Image vs. Vertex buffer

Samsung Electronics
createImage()

Samsung Electronics
destroyImage()

Samsung Electronics
copyImage()

Samsung Electronics
beginSingleCommandBuffer() / endSingleCommandBuffer()

Samsung Electronics
Texture Image
Image Layout

- User optimal layout for Vulkan image object according to usage


- User barrier object to synchronize image layout transition

Pipeline barrier

- Use the resource read/write synchronization


-Use image layout transition, queue family ownership transfer synchronization in
VK_SHARING_MODE_EXCLUSIVE mode
- Image memory barrier (VkImageMemoryBarrier) : image layout transition
Buffer memory barrier (VkBufferMemoryBarrier) : buffer synchronization

Samsung Electronics
transitionImageLayout() #1

Layout transition

Queue family ownership transfer

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

- Have the texture image object and additional information


(Texture view type, format, mipmap, texture array, etc.)
- Use texture image view as main handle instead of texture image

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

1) Before graphics pipeline creation : Create the descriptor set layout


2) Graphics pipeline creation time : Specify the descriptor set layout
3) After graphics pipeline creation : Create the texture image view, sampler,
descriptor pool and descriptor set
4) Rendering time : Descriptor binding

Samsung Electronics
Resource Descriptor
Create

Create the descriptor set layout

Specify the descriptor set layout

Create the texture image view /


sampler
Create the Descriptor pool / descriptor
set

Bind the descriptor set

Samsung Electronics
createDescriptorSetLayout()

Samsung Electronics
createGraphicsPipeline()

Samsung Electronics
Create the Descriptor Pool
Descriptor Pool

- Add texture image descriptor types to the 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

※ Descriptor sets are destroyed automatically


when destroying the descriptor pool

Samsung Electronics
createDescriptorSet()

Samsung Electronics
Descriptor Set Binding
Descriptor Set Binding

- Bind the descriptor set at rendering time


- Submit binding command in command buffer, before submitting drawing
commands

Samsung Electronics
Bind the Descriptor Set
Create Destroy

Samsung Electronics
recordCommandBuffers()

Samsung Electronics
Texture Mapping

• Texture Image

• Sampler

• Texture Descriptor Set

Samsung Electronics
Enable the Standard Validation Layer
Vulkan Design Concepts

- Minimal driver overhead


- User control most thing explicitly
- Very limited error checking

Validation Layer

- Support various functions and services by hooking Vulkan API


- In general, enable validation layer in debug mode and disable in release mode

Samsung Electronics
Vulkan API Hooking Example

Check error before calling the final vkCreateInstance( )

Check result value after calling the final vkCreateInstance( )

Samsung Electronics
Standard Validation Layer

- The LunarG Vulkan SDK supports standard validation layer


- Users can develop customized validation layers

Samsung Electronics
Enable the Standard Validation Layer
Create Destroy

Set validation layer information before creating


instance
→ This information is used when creating the instance

Samsung Electronics
setupValidationLayers()

Used when creating the instance


- instance layer
- instance extension
- debug report callback information

Device Layer : deprecated (SDK ver. 1.0.13.0)

VK_EXT_debug_report

User-defined
callback function

Samsung Electronics
createInstance()

Set below information at setupValidationLayers( )


- instance layer
- instance extension
- debug report callback information

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()

- Error report callbacks need to be created/destroyed explicitly


- Use vkCreateDebugReportCallbackEXT( ) and vkDestoryDebugReportCallbackEXT( ) functions
- These functions are not loaded automatically because those are extensions. Get function pointer using
vkGetInstanceProcAddr( )

Samsung Electronics
VulkanDebugCallback()

Set in VkDebugReportCallbackCreateInfoEXT

Samsung Electronics
Appendix

• Vulkan API Specification


https://www.khronos.org/registry/vulkan/specs/1.0/xhtml/vkspec.html

• Building sample project in Vulkan SDK


https://www.youtube.com/watch?v=wHt5wcxIPcE (Tutorial 0)

• Vulkan Validation Layers


http://gpuopen.com/using-the-vulkan-validation-layers/

Samsung Electronics

You might also like