yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
591affaf7
master
1// vk-util.h 2#pragma once 3 4#include "core/slang-basic.h" 5#include "slang-gfx.h" 6#include "vk-api.h" 7 8// Macros to make testing vulkan return codes simpler 9 10/// SLANG_VK_RETURN_ON_FAIL can be used in a similar way to SLANG_RETURN_ON_FAIL macro, except it 11/// will turn a vulkan failure into Slang::Result in the process Calls handleFail which on debug 12/// builds asserts 13#define SLANG_VK_RETURN_ON_FAIL (x ) \ 14 { \ 15 VkResult _res = x; \ 16 if (_res != VK_SUCCESS) \ 17 { \ 18 return VulkanUtil::handleFail(_res); \ 19 } \ 20 } 21 22#define SLANG_VK_RETURN_NULL_ON_FAIL (x ) \ 23 { \ 24 VkResult _res = x; \ 25 if (_res != VK_SUCCESS) \ 26 { \ 27 VulkanUtil::handleFail(_res); \ 28 return nullptr; \ 29 } \ 30 } 31 32/// Is similar to SLANG_VK_RETURN_ON_FAIL, but does not return. Will call checkFail on failure - 33/// which asserts on debug builds. 34#define SLANG_VK_CHECK (x ) \ 35 { \ 36 VkResult _res = x; \ 37 if (_res != VK_SUCCESS) \ 38 { \ 39 VulkanUtil::checkFail(_res); \ 40 } \ 41 } 42 43namespace gfx 44{ 45 46// Utility functions for Vulkan 47struct VulkanUtil 48{ 49/// Get the equivalent VkFormat from the format 50/// Returns VK_FORMAT_UNDEFINED if a match is not found 51static VkFormat getVkFormat (Format format ); 52 53static VkImageAspectFlags getAspectMask (TextureAspect aspect ,VkFormat format ); 54 55/// Called by SLANG_VK_RETURN_FAIL if a res is a failure. 56/// On debug builds this will cause an assertion on failure. 57static Slang ::Result handleFail (VkResult res ); 58/// Called when a failure has occurred with SLANG_VK_CHECK - will typically assert. 59static void checkFail (VkResult res ); 60 61/// Get the VkPrimitiveTopology for the given topology. 62/// Returns VK_PRIMITIVE_TOPOLOGY_MAX_ENUM on failure 63static VkPrimitiveTopology getVkPrimitiveTopology (PrimitiveTopology topology ); 64 65static VkImageLayout mapResourceStateToLayout (ResourceState state ); 66 67/// Returns Slang::Result equivalent of a VkResult 68static Slang ::Result toSlangResult (VkResult res ); 69 70static VkShaderStageFlags getShaderStage (SlangStage stage ); 71 72static VkImageLayout getImageLayoutFromState (ResourceState state ); 73 74/// Calculate size taking into account alignment. Alignment must be a power of 2 75static UInt calcAligned (UInt size ,UInt alignment ) 76 { 77return (size + alignment - 1 )& ~(alignment - 1 ); 78 } 79 80static inline bool isDepthFormat (VkFormat format ) 81 { 82switch (format ) 83 { 84case VK_FORMAT_D16_UNORM : 85case VK_FORMAT_D24_UNORM_S8_UINT : 86case VK_FORMAT_X8_D24_UNORM_PACK32 : 87case VK_FORMAT_D32_SFLOAT : 88case VK_FORMAT_D32_SFLOAT_S8_UINT : 89return true; 90 } 91return false; 92 } 93 94static inline bool isStencilFormat (VkFormat format ) 95 { 96switch (format ) 97 { 98case VK_FORMAT_S8_UINT : 99case VK_FORMAT_D24_UNORM_S8_UINT : 100case VK_FORMAT_D32_SFLOAT_S8_UINT : 101return true; 102 } 103return false; 104 } 105 106static VkSampleCountFlagBits translateSampleCount (uint32_t sampleCount ); 107 108static VkCullModeFlags translateCullMode (CullMode cullMode ); 109 110static VkFrontFace translateFrontFaceMode (FrontFaceMode frontFaceMode ); 111 112static VkPolygonMode translateFillMode (FillMode fillMode ); 113 114static VkBlendFactor translateBlendFactor (BlendFactor blendFactor ); 115 116static VkBlendOp translateBlendOp (BlendOp op ); 117 118static VkPrimitiveTopology translatePrimitiveTypeToListTopology (PrimitiveType primitiveType ); 119 120static VkStencilOp translateStencilOp (StencilOp op ); 121 122static VkFilter translateFilterMode (TextureFilteringMode mode ); 123 124static VkSamplerMipmapMode translateMipFilterMode (TextureFilteringMode mode ); 125 126static VkSamplerAddressMode translateAddressingMode (TextureAddressingMode mode ); 127 128static VkCompareOp translateComparisonFunc (ComparisonFunc func ); 129 130static VkStencilOpState translateStencilState (DepthStencilOpDesc desc ); 131 132static VkSamplerReductionMode translateReductionOp (TextureReductionOp op ); 133 134static CooperativeVectorComponentType translateCooperativeVectorComponentType ( 135VkComponentTypeKHR type ); 136}; 137 138struct AccelerationStructureBuildGeometryInfoBuilder 139{ 140public : 141VkAccelerationStructureBuildGeometryInfoKHR buildInfo = { 142VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR }; 143Slang ::List < uint32_t > primitiveCounts ; 144 145Slang ::Result build ( 146const IAccelerationStructure ::BuildInputs & buildInputs , 147IDebugCallback * debugCallback ); 148 149private : 150Slang ::List < VkAccelerationStructureGeometryKHR > m_geometryInfos ; 151VkAccelerationStructureGeometryKHR m_vkInstanceInfo = { 152VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR }; 153}; 154 155 156}// namespace gfx