Vulkan: A Hand Waving Guided Tour - Khronos

Transcription

Vulkan: A Hand Waving GuidedTourHai Nguyen / GoogleDec 2018 The Khronos Group Inc. 2018 - Page 1

About Me My name Hai Nguyen (pronounced Hi Win) Been at Google for 4 years Currently Working on DXC/SPIR-V Screenshot of my first Vulkan program- Circa Christmas 2015 The Khronos Group Inc. 2018 - Page 2

We’re Hiring!!!Please ContactKazuki Harakazhara@google.com The Khronos Group Inc. 2018 - Page 3

Agenda Getting Started HLSL in Vulkan Vulkan Best Practices The Khronos Group Inc. 2018 - Page 4

Getting Started With Vulkan The Khronos Group Inc. 2018 - Page 5

Getting Started With VulkanGetting Started With Vulkan Taking a slightly different approach with this talk- Instead of walking through code, this talk will go though fundamental Vulkan objects andtopics- Will end with some pseudo code that outlines a simple Vulkan program What’s covered in this talk- Basic outline of Vulkan graphics program- Conceptual details about Vulkan objects in a small example What’s not covered in this talk- Code walk through of a Vulkan program- Many online tutorials and blog posts available on line with code examples- Painful details about every Vulkan object type- Create info structs- Vulkan spec is best source for understanding these There will be a bit of handwaving- Some Vulkan objects, like shader module, are pretty straight forward- Going into full detail would require at least a few hours! The Khronos Group Inc. 2018 - Page 6

Vulkan: What Is It?Getting Started With Vulkan Vulkan is a cross platform computing API- ‘Computing’ because - Vulkan Vulkan Vulkan Vulkancancancancanbebebebeusedusedusedusedon both GPUs and CPUsfor graphics applicationsfor compute applicationsfor graphics compute applications- Cross platform exception- Surfaces for graphics application Vulkan Platforms- Android, iOS, Linux, macOS, Switch, Windows Vulkan Ecosystem- Almost all Vulkan tools are open source- IHV specific tools being the exception- Vulkan Validation Layers are open source and available on Github- Vulkan Loader is open source and available on Github- Open source Vulkan drivers available for AMD and Intel GPUs The Khronos Group Inc. 2018 - Page 7

Getting Started With VulkanGetting Started With Vulkan Writing Vulkan code has gotten easier over the past year!- Thank you Vulkan community for providing feedback to push Vulkan forward!- Thank you Khronos Vulkan WG for working tirelessly to improve the Vulkan experience! How has it gotten easier?- Vulkan 1.1 was released bringing with it new features- Many useful extensions promoted into the Core API- Helper libraries helped simplify complex topics in Vulkan- AMD’s Vulkan Memory Allocator (VMA)- Tobias Hector’s Simple Vulkan Synchronization- Features and quality of Vulkan tools continuously improving- RenderDoc’s feature richness keeps growing- Improved shader compiler support for GLSL and HLSL- Binaries available for DXC so you don’t have to build it yourself- Continuously refined spec language- Vulkan specification continues to improve clarity and provides greater detail- More Vulkan literature and tutorials available- Numerous resources are now available online to help get started with Vulkan The Khronos Group Inc. 2018 - Page 8

Getting Started With VulkanGetting Started With Vulkan Essentials- PC running Linux or Windows equipped with a GPU that supports Vulkan- Vulkan SDK from LunarG- C/C Compiler- Visual Studio on Windows- Visual Studio 2015, Visual Studio 2017, and Visual Studio Code all work- clang or gcc on Linux- Visual Studio Code is also available on Linux- Your favorite Linux IDE- VIM or EMacs Supplementary (all available on Github)- GLFW- Simple cross platform windowing library- Vulkan Memory Allocator- Easy to use memory allocation library for Vulkan- Simple Vulkan Synchronization- Reduces the headache of trying to understand Vulkan synchronization The Khronos Group Inc. 2018 - Page 9

Vulkan Application OverviewGetting Started With Vulkan Create Vulkan instance Create Vulkan device Create surface and swapchain Create some or all of the following- Buffers, images, descriptor sets, shaders, pipelines, command buffers, semaphores, fences Render loop-Update resourcesUpdate descriptors with references to resourcesBuild command buffersSubmit command buffersPresent When exiting application - destroy all the objects created for application The Khronos Group Inc. 2018 - Page 10

Fundamental Vulkan ObjectsGetting Started With Vulkan Any Vulkan application will require creation / retrieval of these objects-VkInstanceVkPhysicalDeviceVkDeviceVkQueue Creation Sequence-VkInstance object must be the first Vulkan object createdVkPhysicalDevice object(s) are enumerated from the VkInstance objectVkDevice object is created from a VkPhysicalDevice objectVkQueue object(s) are retrieved from VkDevice object The Khronos Group Inc. 2018 - Page 11

VkInstanceGetting Started With Vulkan Stores per application states for Vulkan Created with vkCreateInstance Destroyed with vkDestroyInstance Instance layers and extensions- Instance level layers must be specified in create info- Instance level extensions must be specified in create info Single instance can support multiple Vulkan implementations- TL;DR; Vulkan instance knows about all the registered GPUs that support Vulkan on thesystem Does the Vulkan instance need to remain alive throughout an application’slifetime?- Yes!- Destroy the instance object will lead to undefined behavior or most likely a crash! The Khronos Group Inc. 2018 - Page 12

VkPhysicalDeviceGetting Started With Vulkan Represents a single complete implementation of Vulkan available to theapplication- Conceptually you can think of it as the GPU Enumerated using vkEnumeratePhysicalDevices Once enumerated physical devices can be queried for device properties- vkGetPhysicalDeviceProperties- Driver version, vendor, device type, device name, etc. Things applications should pay attention to because they will vary- Device features – vkGetPhysicalDeviceFeatures- Lets the application know what the Vulkan implementation is capable of- Device limits – vkGetPhysicalDeviceProperties- Lets the application know the limits of the Vulkan implementation are- Max buffer sizes, max image dimensions, max memory allocations, etc.- Queue family properties – vkGetPhysicalDeviceQueueFamilyProperties- Lets the application know what queues are available- Helps determine if the Vulkan implementation can run your application The Khronos Group Inc. 2018 - Page 13

VkDeviceGetting Started With Vulkan Logical Device Provides connection between application and physical device Multiple logical devices an be created from a single physical device- In practice you probably won’t do this, special case Created with vkCreateDevice Destroyed with vkDestroyDevice Device layers and extensions- WARNING: Device level layers are deprecated and will be ignored- Device level extensions must be specified in create info Queues- Must tell the [logical] device creation what queues application plans to use- Queue types and counts can be queried from physical device Make your life easier!- When starting with Vulkan just use a single graphics queue The Khronos Group Inc. 2018 - Page 14

VkQueueGetting Started With Vulkan Retrieved using vkGetDeviceQueue Queues belong to queue families, which determines the type of commands thequeue can execute- Queue families can support a combination of the following command types- Graphics, Compute, and Transfer- Graphics queues also support Compute and Transfer commands- Compute queues also support Transfer commands Queue configurations will vary from GPU to GPU Over Simplification:- AMD - Single graphics, multiple compute queues, multiple transfer queues- Intel - One queue to rule them all- NVIDIA - Multiple graphics queues, multiple compute queues, multiple transfer queues Applications should be aware of different queue configurations- Rigid queue requirements may prevent applications running correctly or at all Make your life easier!- When starting with Vulkan just use a single graphics queue The Khronos Group Inc. 2018 - Page 15

Getting Started With VulkanFundamental Vulkan Objects (RECAP) Any Vulkan application will require creation / retrieval of these objects-VkInstanceVkPhysicalDeviceVkDeviceVkQueue Creation Sequence-VkInstance object must be the first Vulkan object createdVkPhysicalDevice object(s) are enumerated from the VkInstance objectVkDevice object is created from a VkPhysicalDevice objectVkQueue object(s) are retrieved from VkDevice object The Khronos Group Inc. 2018 - Page 16

Getting Started With VulkanFundamental Vulkan Graphics Objects For Vulkan graphics applications, two more fundamental objects are needed- VkSurfaceKHR- VkSwapchainKHR Both are WSI objects- Window System Integration- WSI is what connects the application to the platforms windowing system- This what allows the applications to present what it renders (aka page swap or page flip) Creation sequence- VkSurfaceKHR object is created from VkInstance object- NOTE: VkSurfaceKHR is an instance level object- VkSwapchainKHR is created from vkDevice object using VkSurfaceKHR object- NOTE: VkSwapchainKHR is a device level object The Khronos Group Inc. 2018 - Page 17

VkSurfaceKHRGetting Started With Vulkan Creation of VkSurfaceKHR is platform specific!- Windows- vkCreateWin32SurfaceKHR- Linux- vkCreateXcbSurfaceKHR / vkCreateXlibSurfaceKHR- Android- vkCreateAndroidSurfaceKHR Each vkCreate*SurfaceKHR function takes specific platform details- See Vk*SurfaceCreateInfo structs for platform details- GLFW has functions to access Windows and Linux details- GLFW can also take care of creating the surface for you Vulkan surfaces capabilities are very straight forward- Surface capabilities tells you ever you need to know about the surface- Min/max image dimensions, min/max image count, pixel format, color spaces, etc.- Use vkGetPhysicalDeviceSurfaceCapabilitiesKHR to get capabilities- No crazy guessing games or creating dummy windows for pixel formats- Easier than OpenGL! The Khronos Group Inc. 2018 - Page 18

VkSwapchainKHRGetting Started With Vulkan Back to platform independence! Created with vkCreateSwapchainKHR Destroyed with vkDestroySwapchainKHR Created from VkDevice object Use surface capabilities to help decide values for swapchain parameters- Parameters will vary from implementation to implementation- Example parameters:-Minimum / maximum image countMinimum / maximum image size (aka extents)Image format (pixel format)Color spacesPresentation mode Once the swapchain is created, VkImage objects can be retrieved from theswapchain and used for rendering- vkGetSwapchainImagesKHR returns a list of available images The Khronos Group Inc. 2018 - Page 19

Getting Started With VulkanFundamental Vulkan Graphics Objects (RECAP) Any Vulkan application will require creation / retrieval of these kSurfaceKHRVkSwapchainKHR Creation Sequence-VkInstance object must be the first Vulkan object createdVkPhysicalDevice object(s) are enumerated from the VkInstance objectVkDevice object is created from a VkPhysicalDevice objectVkQueue object(s) are retrieved from VkDevice objectVkSurfaceKHR object is created from VkInstance objectVkSwapchainKHR object is created from VkDevice object using VkSurfaceKHR object The Khronos Group Inc. 2018 - Page 20

Getting Started With VulkanWriting a Vulkan Graphics Application We just need a few more objects for a Vulkan graphics derPassVkFramebufferVkCommandPoolVkCommandBuffer Don’t worry! We’re not going to go through all these! The Khronos Group Inc. 2018 - Page 21

Getting Started With VulkanWriting a Vulkan Graphics Application Writing a simple Vulkan application that draws a spinning textured triangle- VkBuffer object VkDeviceMemory object for vertex and texture coordinates- We’ll skip using an index buffer to keep things simple-VkBuffer object VkDeviceMemory object for uniform (aka constant) dataVkImage object VkDeviceMemory object for texture dataVkImageView object to make VkImage object visible to shader via a descriptorVkSampler object to describe texture samplingVkDescriptorSet object to for descriptor data- Will also need VkDescriptorSetLayout and VkDescriptorPool objects as well- VkRenderPass object VkFramebuffer object for image attachments- VkShaderModule objects for vertex and fragment shaders- aka VS and FS (or PS in HLSL parlance)- VkPipeline object for render state, shader linkage, pipeline layout- VkPipelineLayout specifies the descriptor types that the pipeline will use- VkCommandBuffer- Will also need VkCommandPool Lets shorten this! The Khronos Group Inc. 2018 - Page 22

Getting Started With VulkanWriting a Vulkan Graphics Application Writing a simple Vulkan application that draws a spinning textured triangle-VkBuffer object VkDeviceMemory Vertex BufferVkBuffer object VkDeviceMemory Uniform Buffer / Constant BufferVkImage object VkDeviceMemory TextureVkImageView object Image ViewVkSampler object SamplerVkDescriptorSet object Descriptor SetVkRenderPass object VkFramebuffer object Render PassVkShaderModule object(s) Shader ModulesVkPipeline object Pipeline (aka Graphics Pipeline)VkCommandBuffer object Command Buffer Lets categorize these objects into functional topics! The Khronos Group Inc. 2018 - Page 23

Getting Started With VulkanWriting a Vulkan Graphics Application Writing a simple Vulkan application that draws a spinning textured triangle- Objects that require Memory Allocations- VkBuffer object VkDeviceMemory Vertex Buffer- VkBuffer object VkDeviceMemory Uniform Buffer / Constant Buffer- VkImage object VkDeviceMemory Texture- Objects related to Descriptor Sets- VkImageView object Image View- VkSampler object Sampler- VkDescriptorSet object Descriptor Set- Objects related to Render Passes- VkRenderPass object VkFramebuffer object Render Pass- Objects related to Graphics Pipelines- VkShaderModule object(s) Shader Modules- VkPipeline object Pipeline (aka Graphics Pipeline)- Building the Command Buffer- VkCommandBuffer object Command Buffer Lets take a brief look at these topics! The Khronos Group Inc. 2018 - Page 24

Memory AllocationsGetting Started With Vulkan Images and buffers require memory allocation- These are the only two Vulkan objects that have explicit memory allocation Allocated memory must be freed when no longer used Over Simplification:- Device Local means GPU memory (fast)- Host Visible means CPU memory (slow)- On UMA systems these are the same thing Vulkan has different memory types that come from different heaps- Memory type and heaps will vary from implementation to implementation- It takes some time to fully understand memory in Vulkan Make your life easier! Use Vulkan Memory Allocator (aka VMA)!- MemoryAllocator- Apart of AMD’s GPUOpen initiative- Author is Adam Sawicki- Google “vulkan memory allocator” Ash’s talk will go in depth about Vulkan memory usage! The Khronos Group Inc. 2018 - Page 25

Descriptor SetsGetting Started With Vulkan Over Simplification:- Descriptor sets are containers for descriptor bindings that are described usingVkDescriptorSetLayout objects- Descriptor bindings connect resources to shaders- Descriptor bindings are sometimes referred to as descriptors- Buffers, images, and samplers are all resources- A descriptor set is referred to by its ‘set number’ from a shader Descriptor sets must be allocated from descriptor pools- Descriptor pools maintains a pool available descriptors- Descriptor pools are created with fixed sizes for each descriptor type (VkDescriptorType)- Descriptor pool sizes example:-20 descriptors for Sampled Images (Textures)10 descriptors for Uniform Buffers (Constant Buffers)15 descriptors for Storage Buffers (Structured Buffers)5 descriptors for Samplers- Allocation will fail when a pool runs out of available descriptors The Khronos Group Inc. 2018 - Page 26

Descriptor SetsGetting Started With Vulkan Before a descriptor set can be used it must be updated- References to resources are written to the descriptor set Example descriptor set- 1 Uniform Buffer- 1 Sampled Image- 1 Sampler Updating descriptor set- Update descriptor for Uniform Buffer to refer to a VkBuffer object- Update descriptor for Sampled Image to refer to a VkImage object- Actually a VkImageView- Update descriptor for Sampler to refer to a VkSampler object Once updated, descriptor sets can be bound for use The Khronos Group Inc. 2018 - Page 27

Render PassesGetting Started With Vulkan Over Simplification:- Stores a list of attachments (images) an application renders to- Each attachment has pixel format and sample count associated with it In Vulkan everything that gets drawn must happen within a render pass- Conceptually looks like this:- Begin a render pass- Draw geometry- End render pass- Render passes cannot be nested- A render pass that has begun must be ‘ended’ before another can begin Why is there a framebuffer associated with a render pass? (TL;DR;)- Framebuffers stores the actual references to the attachments (images) you’ll render to- Framebuffers stores dimensions (Width x Height) used for rendering What about subpasses?- If you’re just getting started, just use one subpass- If you’re not planning to target mobile, just use one subpass The Khronos Group Inc. 2018 - Page 28

Graphics PipelineGetting Started With Vulkan Over Simplification:- Graphics pipeline stores vertex data description, shaders, render states, pipeline layout, etc.- Biggest create info struct in Vulkan, please see spec- Describes what type of geometry (primitive topology) is rendered and how it’s rendered- Similar, in concept, to graphics pipelines of every other graphics API Shaders in a graphics pipeline are per shader stages- Shaders for each shader stage is referenced using VkShaderModule objects- Valid combinations-VSVS / PSVS / GS / PSVS / HS / DS / PSVS / HS / DS / GS / PSHS (Hull Shader aka Tessellation Control Shader), DS (Domain Shader aka Tessellation Evaluation Shader) Pipeline Layout- Conceptually a program interface between the Vulkan API and shaders in a pipeline- Takes in descriptor set layouts as apart of its creation- Remember that a descriptor set layout is what a descriptor set looks like not the actual references to resources- Tells Vulkan what types of resources the shaders will use- This means that there must be agreement between what the shader expects and what’s in the pipeline layout The Khronos Group Inc. 2018 - Page 29

Building the Command BufferGetting Started With Vulkan Writing a simple Vulkan application that draws a spinning textured triangle-VkBuffer object VkDeviceMemory Vertex BufferVkBuffer object VkDeviceMemory Uniform Buffer / Constant BufferVkImage object VkDeviceMemory TextureVkImageView object Image ViewVkSampler object SamplerVkDescriptorSet object Descriptor SetVkRenderPass object VkFramebuffer object Render PassVkShaderModule object(s) Shader ModulesVkPipeline object Pipeline (aka Graphics Pipeline)VkCommandBuffer object Command Buffer Lets rearrange it a bit! The Khronos Group Inc. 2018 - Page 30

Building the Command BufferGetting Started With Vulkan External to building the command buffer-VkBuffer object VkDeviceMemory Uniform Buffer / Constant BufferVkImage object VkDeviceMemory TextureVkImageView object Image ViewVkSampler object SamplerVkDescriptorSet object Descriptor Set Building the command buffer-VkBuffer object VkDeviceMemory Vertex BufferVkDescriptorSet object Descriptor SetVkRenderPass object VkFramebuffer object Render PassVkPipeline object Pipeline (aka Graphics Pipeline) Lets clean it up! The Khronos Group Inc. 2018 - Page 31

Building the Command BufferGetting Started With Vulkan External to building the command buffer-Uniform Buffer / Constant BufferImage View (references Texture)SamplerDescriptor Set (needs updating to reference Uniform Buffer, Image View, and Sampler) Building the command buffer-Vertex BufferDescriptor SetRender PassGraphics Pipeline Lets map this to Vulkan calls! The Khronos Group Inc. 2018 - Page 32

Building the Command BufferGetting Started With Vulkan External to command buffer building-Uniform Buffer / Constant BufferImage View (references Texture)SamplerDescriptor Set (call vkUpdateDescriptorSets to write uniform buffer, image view, andsampler) Command buffer building-Call vkBeginCommandBufferBind Vertex Buffer (call vkCmdBindVertexBuffers)Bind Descriptor Set (call vkCmdBindDescriptorSets)Begin Render Pass (call vkCmdBeginRenderPass)Bind Graphics Pipeline (call vkCmdBindPipeline)Draw Triangle (call vkCmdDraw)End Render Pass (call vkCmdEndRenderPass)Call vkEndCommandBuffer Lets see the pseudo code! The Khronos Group Inc. 2018 - Page 33

Simple Render LoopGetting Started With Vulkan// Update transforms, etc.// Update uniform buffersvkUpdateDescriptorSets( ); // if Buffers( vertexBuffer );vkCmdBindDescriptorSets( descriptorSet );vkCmdBeginRenderPass( renderPass );vkCmdBindPipeline( graphicsPipeline );vkCmdDraw( );vkEndCommadnBuffer(cmd);vkQueueSubmit( cmd );vkQueuePresent( ); The Khronos Group Inc. 2018 - Page 34

Getting Started With VulkanDescriptor Sets and Pipeline Layout typedef struct VkPipelineLayoutCreateInfo {VkStructureTypesType;const void*pNext;VkPipelineLayoutCreateFlagsflags;uint32 tsetLayoutCount;const VkDescriptorSetLayout*pSetLayouts;uint32 tpushConstantRangeCount;const VkPushConstantRange*pPushConstantRanges;} VkPipelineLayoutCreateInfo;void Layoutlayout,uint32 tfirstSet,uint32 tdescriptorSetCount,const VkDescriptorSet* pDescriptorSets,uint32 tdynamicOffsetCount,const uint32 t*pDynamicOffsets); Parameter firstSet has two meanings- Is the set number of the first descriptor set to be bound- For example firstSet 3 means that pDescriptorSets[0] is at set number 3, pDescriptorSets[1] is at set number 4,pDescriptorSets[2] is at set number 5, and so on- It also serves as an index that maps into VkPipelineLayoutCreateInfo::pSetLayouts array- So for firstSet 3 the descriptor set layout of pDescriptorSets[0 ] must match what was specified toVkPipelineLayoutCreateInfo::pSetLayouts[3 ] when the layout (VkPipelineLayout object) was created- Validation layers will catch this! Otherwise, undefined behavior! The Khronos Group Inc. 2018 - Page 35

Getting Started With VulkanGetting Started With Vulkan (RECAP) Writing Vulkan code has gotten easier over the past year! Essentials- PC running Linux or Windows equipped with a GPU that supports Vulkan- Vulkan SDK from LunarG- C/C Compiler- Visual Studio on Windows- Visual Studio 2015, Visual Studio 2017, and Visual Studio Code all work- clang or gcc on Linux- Visual Studio Code is also available on Linux Supplementary (all available on Github)- GLFW- Simple cross platform windowing library- Vulkan Memory Allocator- Easy to use memory allocation library for Vulkan- Simple Vulkan Synchronization- Reduces the headache of trying to understand Vulkan synchronization The Khronos Group Inc. 2018 - Page 36

HLSL in Vulkan The Khronos Group Inc. 2018 - Page 37

HLSL in Vulkan Vulkan Shader Languages HLSL Compilers HLSL Example Registers and Bindings Sets and Spaces Resource MappingsVulkan Shader Languages HLSL Vulkanizations The Khronos Group Inc. 2018 - Page 38

HLSL in VulkanVulkan Shader Languages Vulkan does not have a spec for a high level language for shaders Vulkan implementations must accept shaders in SPIR-V format Vulkan ecosystem tools support two high level languages- GLSL- HLSL Most tutorials and code samples available online use GLSL- But we wanted to let you know that HLSL can be used as well :) The Khronos Group Inc. 2018 - Page 39

HLSL in VulkanHLSL Compilers and Language HLSL Compilers for Vulkan- DirectX Shader Compiler (aka DXC) – Microsoft’s open source HLSL compiler- Google contributed and maintains the SPIR-V backend- glslang - Khronos reference compiler HLSL Language in Vulkan- DXC supports SM6.0- Work on 6.1 has started- glslang supports SM5.1 - Some HLSL 5.1 features not supported- Some HLSL 6.0 features present thanks to community contributions- Source compatible with HLSL in DirectX- Caveat features not available in Vulkan- DXC has stronger source compatibility support Which compiler should I use for HLSL in Vulkan?- DXC is recommended if you care about SM6.0 and up- DXC is the future of HLSL in Vulkan going forward- DXC or glslang is fine if you’re only targeting SM5.1 and below The Khronos Group Inc. 2018 - Page 40

HLSL ExampleHLSL in Vulkan// dxc –spirv –Tvs 5 0 –E VSMain –Fo vs.spv shader.hlsl// dxc –spirv –Tps 5 0 –E PSMain –Fo ps.spv shader.hlslcbuffer UniformBlock0 : register(b0, space0) {float4x4 mvp;};struct VSOutput {float4 Position : SV POSITION;float2 TexCoord : TEXCOORD;};VSOutput VSMain(float4 Position : POSITION, float2 TexCoord : TEXCOORD0) {VSOutput result;result.Position mul(mvp, Position);result.TexCoord TexCoord;return result;}Texture2D uTex0 : register(t1, space0);SamplerState uSampler0 : register(s2, space0);float4 PSMain(VSOutput input) : SV TARGET {return uTex0.Sample(uSampler0, input.TexCoord);} The Khronos Group Inc. 2018 - Page 41

HLSL in VulkanRegisters and Bindings All Vulkan resource types map to a binding number-HLSL’s register(bW)HLSL’s register(sX)HLSL’s register(tY)HLSL’s register(uZ)maps to binding number Wmaps to binding number Xmaps to binding number Ymaps to binding number Z Unlike DirectX, Vulkan does not have namespaces for resource types- register(bN), register(sN), register(tN), and register(uN) all map to binding N- register(b0), register(s0), register(t0), and register(u0) all map to binding 0 Shift flags in DXC and glslang- Helps developers work around the lack of namespace for resource types in Vulkan- Shift flags lets you shift the binding number based on the resource type- For example, to shift all b# resource types in DXC by 16- Use the command line option: -fvk-b-shift 16 0 The Khronos Group Inc. 2018 - Page 42

HLSL in VulkanSets and Spaces HLSL’s spaceN keyword is analogous to set N in Vulkan-HLSL’s register(b0,HLSL’s register(t2,HLSL’s register(u4,HLSL’s register(s8,space0)space1)space2)space6)is analogous to binding number 0, set number 0is analogous to binding number 2, set number 1is analogous to binding number 4, set number 2is analogous to binding number 8, set number 6 If a spaceN is not provided, the default is space0 / set 0- All these will be in set 0-ConstantBuffer T Texture2DRWTexture2D float4 );register(s8); The Khronos Group Inc. 2018 - Page 43

Resource MappingsHLSL in Vulkan DirectX’s HLSL resource to Vulkan descriptor type mapping- cbuffer / ConstantBuffer T : CBV (Constant Buffer View)- VK DESCRIPTOR TYPE UNIFORM BUFFER- VK DESCRIPTOR TYPE UNIFORM BUFFER DYNAMIC- Texture[N]D, Buffer T , StructuredBuffer T : SRV (Shader Resource View)- VK DESCRIPTOR TYPE SAMPLED IMAGE- VK DESCRIPTOR TYPE UNIFORM TEXEL BUFFER- VK DESCRIPTOR TYPE STORAGE BUFFER- RWTexture[N]D, RWBuffer T , RWStructuredBuffer T : UAV (Unordered Access View)-VK DESCRIPTOR TYPE STORAGE IMAGEVK DESCRIPTOR TYPE STORAGE TEXEL BUFFERVK DESCRIPTOR TYPE STORAGE BUFFERVK DESCRIPTOR TYPE STORAGE BUFFER DYNAMIC- SamplerState: Sampler- VK DESCRIPTOR TYPE SAMPLER No HLSL mapping for these types- VK DESCRIPTOR TYPE COMBINED IMAGE SAMPLER The Khronos Group Inc. 2018 - Page 44

HLSL in VulkanSV TARGET Outputs SV TARGET is the same as SV TARGET0- Same as it is in DirectX SV TARGET[N] refers to the Nth color attachment in a VkRenderPass Dual source blending- Vulkan does not automatically detect that dual source blending is present- Must tell SPIR-V targets intended for dual source blending using attributes- Example:struct PSOut {[[vk::location(0), vk::index(0)]] float4 a: SV TARGET0;[[vk::location(0), vk::index(1)]] float4 b: SV TARGET1;};PSOut main() { . } The Khronos Group Inc. 2018 - Page 45

HLSL VulkanizationsHLSL in Vulkan// Vulkan binding 1, set 1[[vk::binding(1, 1)]] cbuffer UniformBlock0 : register(b0, space0) {float4x4 mvp;};struct VSOutput {float4 Position : SV POSITION;float2 TexCoord : TEXCOORD;};VSOutput VSMain(float4 Position : POSITION, float2 TexCoord : TEXCOORD0) {VSOutput result;result.Position mul(mvp, Position);result.TexCoord TexCoord;return result;}// Vulkan binding 0, set 2 and binding 1, set 2[[vk::binding(0, 2]] Texture2D uTex0 : register(t1, space0);[[vk::binding(1, 2)]] SamplerState uSampler0 : register(s2, space0);float4 PSMain(VSOutput input) : SV TARGET {return uTex0.Sample(uSampler0, input.TexCoord);} The Khronos Group Inc. 2018 - Page 46

HLSL in Vulkan (RECAP)HLSL in Vulkan HLSL Compilers- DXC - - Google “dxc shader compiler”- glslang - https://github.com/KhronosGroup/glslang- Google “glslang” HLSL Language in Vulkan- DXC supports SM6.0- Work on 6.1 has started- glslang supports SM5.1 - Some HLSL 5.1 features not supported- Some HLSL 6.0 features present thanks to community contributions- Source compatible with HLSL in DirectX- Caveat features not available in Vulkan- DXC has stronger source compatibility support DXC/SPIR-V

-Android, iOS, Linux, macOS, Switch, Windows Vulkan Ecosystem-Almost all Vulkan tools are open source-IHV specific tools being the exception-Vulkan Validation Layers are open source and available on Github-Vulkan Loader is open source and available on Github-Open source Vulkan drivers available for AMD and Intel GPUs Getting Started With Vulkan