diff options
| author | Theresa Foley <10618364+tangent-vector@users.noreply.github.com> | 2025-01-27 18:31:04 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-27 18:31:04 -0800 |
| commit | 31bb5eaa094821145fe235f9a13817b0b3b2bdee (patch) | |
| tree | 8d25b49516dfd74deb80e113ce2af8180a106e08 /examples/reflection-parameter-blocks/vulkan-api.h | |
| parent | f030a5ab62235152055fe8a616dd6d0d7ba1c275 (diff) | |
Add an example for reflection of parameter blocks (#6161)
* Add an example for reflection of parameter blocks
The example loads a shader program, compiles it for Vulkan, and then uses reflection information to set up descriptor set layouts and a pipeline layout.
It then validates that the pipeline layout that is created is compatible with the compiled code, by using them together to make a pipeline state object with the validation layer enabled.
The basic flow of the application follows the presentation in the companion article, and references its sections.
This is example could be expanded in a few ways:
* A D3D12 path could be added, to show the comparable operations for creating a root signature from reflection data
* The existing shader could be modified to touch/use more of its parameter data, to help ensure that any errors would be caught by the validation layer
* The set of shader files/modules that get loaded automatically could be expanded, to test more cases
* The code could be expanded to handle more than just compute shaders, by allowing for multiple-entry-point files to be loaded for rasterization or ray-tracing pipelines
* format code
* fixup: build errors
* format code
* fixups for build
* fixup
* fixup
---------
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'examples/reflection-parameter-blocks/vulkan-api.h')
| -rw-r--r-- | examples/reflection-parameter-blocks/vulkan-api.h | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/examples/reflection-parameter-blocks/vulkan-api.h b/examples/reflection-parameter-blocks/vulkan-api.h new file mode 100644 index 000000000..05a84db5d --- /dev/null +++ b/examples/reflection-parameter-blocks/vulkan-api.h @@ -0,0 +1,128 @@ +#pragma once + +#include "slang-gfx.h" + +#define VK_NO_PROTOTYPES +#include "vulkan/vulkan.h" + +// This file provides basic loading and helper functions for using +// the Vulkan API. + +// The Vulkan function pointers we will use in this example. +// clang-format off +#define VK_API_GLOBAL_PROCS(x) \ + x(vkGetInstanceProcAddr) \ + x(vkCreateInstance) \ + x(vkEnumerateInstanceLayerProperties) \ + x(vkDestroyInstance) \ + /* */ + +#define VK_API_INSTANCE_PROCS_OPT(x) \ + x(vkGetPhysicalDeviceFeatures2) \ + x(vkGetPhysicalDeviceProperties2) \ + x(vkCreateDebugReportCallbackEXT) \ + x(vkDestroyDebugReportCallbackEXT) \ + x(vkDebugReportMessageEXT) \ + /* */ + +#define VK_API_INSTANCE_PROCS(x) \ + x(vkCreateDevice) \ + x(vkDestroyDevice) \ + x(vkEnumeratePhysicalDevices) \ + x(vkGetPhysicalDeviceProperties) \ + x(vkGetPhysicalDeviceFeatures) \ + x(vkGetPhysicalDeviceMemoryProperties) \ + x(vkGetPhysicalDeviceQueueFamilyProperties) \ + x(vkGetPhysicalDeviceFormatProperties) \ + x(vkGetDeviceProcAddr) \ + /* */ + +#define VK_API_DEVICE_PROCS(x) \ + x(vkCreateDescriptorPool) \ + x(vkDestroyDescriptorPool) \ + x(vkGetDeviceQueue) \ + x(vkQueueSubmit) \ + x(vkQueueWaitIdle) \ + x(vkCreateBuffer) \ + x(vkAllocateMemory) \ + x(vkMapMemory) \ + x(vkUnmapMemory) \ + x(vkCmdCopyBuffer) \ + x(vkDestroyBuffer) \ + x(vkFreeMemory) \ + x(vkCreateDescriptorSetLayout) \ + x(vkDestroyDescriptorSetLayout) \ + x(vkAllocateDescriptorSets) \ + x(vkUpdateDescriptorSets) \ + x(vkCreatePipelineLayout) \ + x(vkDestroyPipelineLayout) \ + x(vkCreateComputePipelines) \ + x(vkDestroyPipeline) \ + x(vkCreateShaderModule) \ + x(vkDestroyShaderModule) \ + x(vkCreateCommandPool) \ + x(vkDestroyCommandPool) \ + \ + x(vkGetBufferMemoryRequirements) \ + \ + x(vkCmdBindPipeline) \ + x(vkCmdBindDescriptorSets) \ + x(vkCmdDispatch) \ + \ + x(vkFreeCommandBuffers) \ + x(vkAllocateCommandBuffers) \ + x(vkBeginCommandBuffer) \ + x(vkEndCommandBuffer) \ + x(vkBindBufferMemory) \ + /* */ + +#define VK_API_ALL_GLOBAL_PROCS(x) \ + VK_API_GLOBAL_PROCS(x) + +#define VK_API_ALL_INSTANCE_PROCS(x) \ + VK_API_INSTANCE_PROCS(x) \ + +#define VK_API_ALL_PROCS(x) \ + VK_API_ALL_GLOBAL_PROCS(x) \ + VK_API_ALL_INSTANCE_PROCS(x) \ + VK_API_DEVICE_PROCS(x) \ + VK_API_INSTANCE_PROCS_OPT(x) \ + /* */ + +#define VK_API_DECLARE_PROC(NAME) PFN_##NAME NAME = nullptr; +// clang-format on + +struct VulkanAPI +{ + gfx::Result initFromGFX(gfx::IDevice* gfxDevice); + + VkInstance instance = VK_NULL_HANDLE; + VkDevice device = VK_NULL_HANDLE; + VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; + + VK_API_ALL_PROCS(VK_API_DECLARE_PROC) + + gfx::Result initGlobalProcs(); + + /// Initialize the instance functions + gfx::Result initInstanceProcs(); + + /// Initialize the device functions + gfx::Result initDeviceProcs(); + + /// Clean up + ~VulkanAPI(); +}; + +#define RETURN_ON_FAIL(x) \ + { \ + auto _res = x; \ + if (_res != 0) \ + { \ + return -1; \ + } \ + } + +// Loads Vulkan library and creates a VkDevice. +// Returns 0 if successful. +int initializeVulkanDevice(VulkanAPI& api); |
