yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Jay KwakCheck the available VK extensions before using CoopVec APIs in GFX (#6849)591affaf7

master
5.2 KiB156 linesraw
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
51    static VkFormat getVkFormat(Format format);
52
53    static 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.
57    static Slang::Result handleFail(VkResult res);
58    /// Called when a failure has occurred with SLANG_VK_CHECK - will typically assert.
59    static void checkFail(VkResult res);
60
61    /// Get the VkPrimitiveTopology for the given topology.
62    /// Returns VK_PRIMITIVE_TOPOLOGY_MAX_ENUM on failure
63    static VkPrimitiveTopology getVkPrimitiveTopology(PrimitiveTopology topology);
64
65    static VkImageLayout mapResourceStateToLayout(ResourceState state);
66
67    /// Returns Slang::Result equivalent of a VkResult
68    static Slang::Result toSlangResult(VkResult res);
69
70    static VkShaderStageFlags getShaderStage(SlangStage stage);
71
72    static VkImageLayout getImageLayoutFromState(ResourceState state);
73
74    /// Calculate size taking into account alignment. Alignment must be a power of 2
75    static UInt calcAligned(UInt size, UInt alignment)
76    {
77        return (size + alignment - 1) & ~(alignment - 1);
78    }
79
80    static inline bool isDepthFormat(VkFormat format)
81    {
82        switch (format)
83        {
84        case VK_FORMAT_D16_UNORM:
85        case VK_FORMAT_D24_UNORM_S8_UINT:
86        case VK_FORMAT_X8_D24_UNORM_PACK32:
87        case VK_FORMAT_D32_SFLOAT:
88        case VK_FORMAT_D32_SFLOAT_S8_UINT:
89            return true;
90        }
91        return false;
92    }
93
94    static inline bool isStencilFormat(VkFormat format)
95    {
96        switch (format)
97        {
98        case VK_FORMAT_S8_UINT:
99        case VK_FORMAT_D24_UNORM_S8_UINT:
100        case VK_FORMAT_D32_SFLOAT_S8_UINT:
101            return true;
102        }
103        return false;
104    }
105
106    static VkSampleCountFlagBits translateSampleCount(uint32_t sampleCount);
107
108    static VkCullModeFlags translateCullMode(CullMode cullMode);
109
110    static VkFrontFace translateFrontFaceMode(FrontFaceMode frontFaceMode);
111
112    static VkPolygonMode translateFillMode(FillMode fillMode);
113
114    static VkBlendFactor translateBlendFactor(BlendFactor blendFactor);
115
116    static VkBlendOp translateBlendOp(BlendOp op);
117
118    static VkPrimitiveTopology translatePrimitiveTypeToListTopology(PrimitiveType primitiveType);
119
120    static VkStencilOp translateStencilOp(StencilOp op);
121
122    static VkFilter translateFilterMode(TextureFilteringMode mode);
123
124    static VkSamplerMipmapMode translateMipFilterMode(TextureFilteringMode mode);
125
126    static VkSamplerAddressMode translateAddressingMode(TextureAddressingMode mode);
127
128    static VkCompareOp translateComparisonFunc(ComparisonFunc func);
129
130    static VkStencilOpState translateStencilState(DepthStencilOpDesc desc);
131
132    static VkSamplerReductionMode translateReductionOp(TextureReductionOp op);
133
134    static CooperativeVectorComponentType translateCooperativeVectorComponentType(
135        VkComponentTypeKHR type);
136};
137
138struct AccelerationStructureBuildGeometryInfoBuilder
139{
140public:
141    VkAccelerationStructureBuildGeometryInfoKHR buildInfo = {
142        VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR};
143    Slang::List<uint32_t> primitiveCounts;
144
145    Slang::Result build(
146        const IAccelerationStructure::BuildInputs& buildInputs,
147        IDebugCallback* debugCallback);
148
149private:
150    Slang::List<VkAccelerationStructureGeometryKHR> m_geometryInfos;
151    VkAccelerationStructureGeometryKHR m_vkInstanceInfo = {
152        VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR};
153};
154
155
156} // namespace gfx