Lecture 2: Vulkan, Part 1
COMP5822M – High Perf. Graphics
– Practicalstuff – WhyVulkan? – VulkanBasics
Copyright By PowCoder代写 加微信 powcoder
COMP5822M – High Perf. Graphics
Practical, I
– Labsupportsessions,suggestion
– Tuesday afternoon, online, 15.00 to <=17.00
- Friday afternoon, in pers., 14:00 to <=16.00
- (Overlap with other lab. Right now difficult to
make it earlier. Might be able to adjust later.)
- WillpostonTeams,candiscussfurther there.
COMP5822M – High Perf. Graphics
Practical, II
- Vulkan 1.3 released with perfect timing
- Will mention a few new features it brings
- Was considering doing that anyway 😉
COMP5822M – High Perf. Graphics
Practical, III
- Exercises:Vulkan1.1+extensionsfornow?
- Less problematic for Apple-y devices
- One of the reasons for 1.2 was made obsolete
by 1.3 anway...
COMP5822M – High Perf. Graphics
Practical, IV
- Quickcheck–C++knowledge.
COMP5822M – High Perf. Graphics
Practical, IV
- Quickcheck–C++knowledge.
- Constructors & Destructors?
- Exceptions?
- Move semantics / move-only objects? - Templates?
COMP5822M – High Perf. Graphics
Why Vulkan?
COMP5822M – High Perf. Graphics
Why Vulkan?
- Twoquestions,actually
- Why would you (as a programmer) pick
- Why did we pick Vulkan for COMP5822?
COMP5822M – High Perf. Graphics
Why Vulkan?
- Twoquestions,actually
- Why would you (as a programmer) pick
- Why did we pick Vulkan for COMP5822?
- Give you some insight into why Vulkan does things a certain way.
COMP5822M – High Perf. Graphics
A Modern System
COMP5822M – High Perf. Graphics
- GPU is a separate device/chip
- Runs asynchronously with everything else
- Has a lot of internal parallelism
- Different GPUs with different tech/features
- Multiple GPUs
- Which can display stuff on the screen?
COMP5822M – High Perf. Graphics
Issues, II
- Different types of memory
- System RAM and VRAM
- Not all GPUs have VRAM
- Not all system RAM is equal (special regions)
- Not all VRAM is equal (special regions)
COMP5822M – High Perf. Graphics
Issues, III
- Minimize unnecessary processing
- Need to “drive” the GPU from CPU
- But CPU has other tasks too
- “Draw calls”
COMP5822M – High Perf. Graphics
Vulkan Principles
- Designprinciples
- Multi-thread, multi-device
- Low overhead
- Explicit control
- Repeatable & predictable
- Other modern APIs similar - E.g.,DirectXorMetal
COMP5822M – High Perf. Graphics
Contrast: OpenGL?
- Not multi-device
- Badatmulti-threading
- Pain to program
- Global State
- Difficult to write independent code
- (Global State has been reduced lately)
COMP5822M – High Perf. Graphics
Contrast: OpenGL?
- Behindthescenes“magic”
- E.g., shadow buffers, shader recompiles, ...
- Expensivealways-onvalidation
- Required to report errors in some cases
- Thatsaid...
- Modern OpenGL is pretty nice
- Still, lots of difficult-to-diagnose
COMP5822M – High Perf. Graphics
Vulkan vs OpenGL
- Less “magic”
- More explicit & verbose
- Less “magic”
- More explicit & verbose
COMP5822M – High Perf. Graphics
Vulkan vs OpenGL
- No global state, little implicit state
- Not actively hostile to multi-threading - Consistentstyleandconcepts
- Validation on demand
- Validation is standardized (and quite good)
- More expensive than OpenGL, but that’s OK
- Only enable it for development/debugging
COMP5822M – High Perf. Graphics
Other reasons
- Crossplatform
- Desktop: Windows, Linux, ~MacOS (MoltenVK)
- Mobile: Android, ~iOS (MoltenVK)
- Others? (Google Stadia?)
- Needs to deal with different archs
- Traditional (sort-last) vs Tiled (~sort-middle)
- Extra complexity.
COMP5822M – High Perf. Graphics
Additional facts
- Extensions (similar to OpenGL) - Fast support for new features.
- Layersystem
- E.g. used for validation
- Can write your own
- Many existing ones are open source
COMP5822M – High Perf. Graphics
COMP5822M – High Perf. Graphics
- At the core, Vulkan is an API specification that conformant hardware implementations follow.
(https://github.com/KhronosGroup/Vulkan- Guide/blob/master/chapters/what_is_vulkan.md)
- Note: software implementations do exist.
COMP5822M – High Perf. Graphics
- Vulkan specification defines an interface
- We use it to talk to the GPU / implementation
- Interface defines functions relating to
- Graphics
- Ray tracing (extension)
- Video encoding/decoding (extension) -...
- (Resource management)
COMP5822M – High Perf. Graphics
- Specificationdefineswhatthe
implementation should do - You can read it online
- XML file (vk.xml) with definitions/names - “Cinterface”
COMP5822M – High Perf. Graphics
The C Interface
- DefaultAPIforVulkan
- “Lowest level”
- We’ll use it in COMP5822
- Common elsewhere (vulkan-tutorial, ...)
- Easiest to find via Google et al. - Distinct naming scheme
COMP5822M – High Perf. Graphics
Side note: other options
- C++:vulkan.hpp(plusothers?)
- Otherlanguagebindings(Rust,...)
- Typically generated from the same XML
- Rollyourown?
- Parse the XML, output desired API/wrappers
- LoadLibrary() / dlopen() etc
COMP5822M – High Perf. Graphics
Vulkan Stack
Application
COMP5822M – High Perf. Graphics
Vulkan Stack
vkFoo( deviceA, ... )
Application
COMP5822M – High Perf. Graphics
Vulkan Stack
vkFoo( deviceA, ... )
Application
vkFoo( deviceA, ... )
COMP5822M – High Perf. Graphics
Vulkan Stack
vkFoo( deviceA, ... ) Application vkBar( deviceB, ... )
vkFoo( deviceA, ... )
COMP5822M – High Perf. Graphics
vkBar( deviceB, ... )
Vulkan Stack
Application
vkFoo( deviceA, ... ) vkBar( deviceB, ... )
vkFoo( deviceA, ... )
COMP5822M – High Perf. Graphics
vkBar( deviceB, ... )
vkBar( deviceB, ... )
Vulkan Stack
Application
COMP5822M – High Perf. Graphics
- Vulkan allows us to load layers
- LayerscaninterceptVulkanAPIcalls
COMP5822M – High Perf. Graphics
Vulkan Stack
vkFoo( deviceA, ... )
Application
vkFoo( deviceA, ... )
Layer A Device A vkFoo( deviceA, ... )
COMP5822M – High Perf. Graphics
See: https://renderdoc.org/vulkan-layer-guide.html
- Example:VK_KHRONOS_validation - Checks for various errors.
- Whylayersforthis?
- Validation is expensive
- Load only during development
- Skip during “normal” deployment
COMP5822M – High Perf. Graphics
Vulkan Stack
vkFoo( deviceA, ... )
Application
Loader Layer A
vkFoo( deviceA, ... )
vkFoo( deviceA, ... )
COMP5822M – High Perf. Graphics
See: https://renderdoc.org/vulkan-layer-guide.html
vkBar( deviceB, ... )
Vulkan Stack
Application
COMP5822M – High Perf. Graphics
Vulkan Stack
Application Volk Vulkan Device A Loader
“Meta-loader for Vulkan” https://github.com/zeux/volk
COMP5822M – High Perf. Graphics
- Meta-loader, 3rd party
- LoadstheVulkanLoaderatruntime - LoadLibrary() / dlopen()
- Simplifies my life. 🙂
- I can give you Volk (2 sources)
- Don’t need to figure out how to link etc. on
your system.
COMP5822M – High Perf. Graphics
- Volk’s API looks just like the standard API.
- However:
#include
#include
– Calls to volkInitialize(), volkLoadInstance() andoptionally
volkLoadDevice()
COMP5822M – High Perf. Graphics
volkLoadDevice()
volkLoadDevice( deviceB )
Application Volk Vulkan Device A Loader
If you need to work with multiple devices, read Volk’s documentation.
COMP5822M – High Perf. Graphics
volkLoadDevice()
volkLoadDevice( deviceB )
Application Volk
COMP5822M – High Perf. Graphics
Works with layers, just no/less benefit.
– Note: loading device functions is only really useful if you’re bound by CPU performance
– Even then, the gains are likely to be quite small.
COMP5822M – High Perf. Graphics
Vulkan C API
COMP5822M – High Perf. Graphics
Vulkan C API
– Objectoriented.
COMP5822M – High Perf. Graphics
Vulkan C API
– Objectoriented.
– Handles = references to objects
– vkCreateFoo() … vkDestroyFoo()
– vkDoThing( fooHandle, … )
COMP5822M – High Perf. Graphics
Vulkan C API
– We are required to clean up after ourselves.
– I.e., call vkDestroyFoo()
– Cannot do that while object is in use
– Either on GPU or on CPU
– No internal reference counting or similar
COMP5822M – High Perf. Graphics
Vulkan C API – Conventions
– Constants:VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO – Types:VkResult, VkInstance, VkInstanceCreateInfo – Functions:vkCreateInstance()
COMP5822M – High Perf. Graphics
Vulkan C API – Conventions
– Constants:VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO – Types:VkResult, VkInstance, VkInstanceCreateInfo – Functions:vkCreateInstance()
COMP5822M – High Perf. Graphics
Vulkan C API – Conventions
– Constants:VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO – Types:VkResult, VkInstance, VkInstanceCreateInfo – Functions:vkCreateInstance()
Enumeration
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
VkResult vkCreateImage(
VkDevice device, // object handle VkImageCreateInfo const* pInfo, // parameters VkAllocationCallbacks const* pCallbacks, // nullptr (ignored for now) VkImage* pImage // “output”
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
– Returnvalue:VkResult – VK_SUCCESS
– error codes
– “conditional” success conditions
– Typicalreturnvalue
– Thoughsomefunctionsarevoid
– E.g. vkDestroyImage()
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
– VkDevice: handle to device
– Image is created for/on this device
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
– VkImageCreateInfo
– Parameters to image creation
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
– VkAllocationCallbacks
– Enables control over internal memory allocations
– System member
– We ignore this mostly (=set to nullptr)
– System memory allocator is good enough – Difficult to beat in the general case anyway
COMP5822M – High Perf. Graphics
Vulkan C API – General Style
– VkImage: out pointer
– Handle to newly created image is placed here
COMP5822M – High Perf. Graphics
Vulkan C API – Code Example
VkDevice device = …;
VkImageCreateInfo imageInfo{}; // “value initialize”, i.e. zero out struct imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
//imageInfo.pNext = nullptr; // already nullptr due to init with {}
imageInfo.format = VK_FORMAT_R8G8B8A8_SRGB; …
VkImage image = VK_NULL_HANDLE;
auto const res = vkCreateImage( device, &imageInfo, nullptr, &image );
if( VK_SUCCESS != res ) error( … );
COMP5822M – High Perf. Graphics
Vulkan C API – Code Example
VkDevice device = …;
VkImageCreateInfo imageInfo{}; // “value initialize”, i.e. zero out struct imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
//imageInfo.pNext = nullptr; // already nullptr due to init with {}
imageInfo.format = VK_FORMAT_R8G8B8A8_SRGB; …
VkImage image = VK_NULL_HANDLE;
if( auto const res = vkCreateImage( … ); VK_SUCCESS != res ) // C++17 if()
error( … );
COMP5822M – High Perf. Graphics
– Typicallypackparametersintostructs
– Manystructshave.sType and.pNext
– sType needs to be set correctly
– pNext mostly left at nullptr
– Used for extensions (pNext points at additional
structs with more parameters)
– Someexceptions(VkExtent2D,…)
– Although some have been updated to have
them after all.
COMP5822M – High Perf. Graphics
Thanks for your attention.
COMP5822M – High Perf. Graphics
Bonus Slide – Extensions / sType, .pNext
VkPhysicalDeviceVulkan12Features featsV12{};
featsV12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN12_FEATURES; …
VkPhysicalDeviceFeatures2 feats{};
feats.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; feats.pNext = &featsV12;
vkGetPhysicalDeviceFeatures2( physicalDevice, &feats );
COMP5822M – High Perf. Graphics
Bonus Slide – Extensions / sType, .pNext
VkPhysicalDeviceVulkan12Features featsV12{};
featsV12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN12_FEATURES; …
VkPhysicalDeviceFeatures2 feats{};
feats.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; feats.pNext = &featsV12;
vkGetPhysicalDeviceFeatures2( physicalDevice, &feats );
Forms a single-linked list.
COMP5822M – High Perf. Graphics
pNext is a void* pointer. Vulkan needs the sType field to know which type of additional structure is attached!
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com