|
| 1 | +#ifndef _NBL_ASSET_I_RAY_TRACING_PIPELINE_H_INCLUDED_ |
| 2 | +#define _NBL_ASSET_I_RAY_TRACING_PIPELINE_H_INCLUDED_ |
| 3 | + |
| 4 | +#include "nbl/asset/IShader.h" |
| 5 | +#include "nbl/asset/IPipeline.h" |
| 6 | + |
| 7 | +#include <span> |
| 8 | +#include <bit> |
| 9 | +#include <type_traits> |
| 10 | + |
| 11 | +namespace nbl::asset |
| 12 | +{ |
| 13 | + |
| 14 | +class IRayTracingPipelineBase : public virtual core::IReferenceCounted |
| 15 | +{ |
| 16 | + public: |
| 17 | + struct SShaderGroupsParams |
| 18 | + { |
| 19 | + struct SIndex |
| 20 | + { |
| 21 | + constexpr static inline uint32_t Unused = 0xffFFffFFu; |
| 22 | + uint32_t index = Unused; |
| 23 | + }; |
| 24 | + |
| 25 | + struct SHitGroup |
| 26 | + { |
| 27 | + uint32_t closestHit = SIndex::Unused; |
| 28 | + uint32_t anyHit = SIndex::Unused; |
| 29 | + uint32_t intersection = SIndex::Unused; |
| 30 | + }; |
| 31 | + |
| 32 | + SIndex raygen; |
| 33 | + std::span<SIndex> misses; |
| 34 | + std::span<SHitGroup> hits; |
| 35 | + std::span<SIndex> callables; |
| 36 | + |
| 37 | + inline uint32_t getShaderGroupCount() const |
| 38 | + { |
| 39 | + return 1 + hits.size() + misses.size() + callables.size(); |
| 40 | + } |
| 41 | + |
| 42 | + }; |
| 43 | + using SGeneralShaderGroup = SShaderGroupsParams::SIndex; |
| 44 | + using SHitShaderGroup = SShaderGroupsParams::SHitGroup; |
| 45 | + |
| 46 | + struct SCachedCreationParams final |
| 47 | + { |
| 48 | + uint32_t maxRecursionDepth : 6 = 0; |
| 49 | + uint32_t dynamicStackSize : 1 = false; |
| 50 | + }; |
| 51 | +}; |
| 52 | + |
| 53 | +template<typename PipelineLayoutType, typename ShaderType> |
| 54 | +class IRayTracingPipeline : public IPipeline<PipelineLayoutType>, public IRayTracingPipelineBase |
| 55 | +{ |
| 56 | + using base_creation_params_t = IPipeline<PipelineLayoutType>::SCreationParams; |
| 57 | + public: |
| 58 | + |
| 59 | + using SGeneralShaderGroupContainer = core::smart_refctd_dynamic_array<SGeneralShaderGroup>; |
| 60 | + using SHitShaderGroupContainer = core::smart_refctd_dynamic_array<SHitShaderGroup>; |
| 61 | + |
| 62 | + struct SCreationParams : base_creation_params_t |
| 63 | + { |
| 64 | + public: |
| 65 | + #define base_flag(F) static_cast<uint64_t>(base_creation_params_t::FLAGS::F) |
| 66 | + enum class FLAGS : uint64_t |
| 67 | + { |
| 68 | + NONE = base_flag(NONE), |
| 69 | + DISABLE_OPTIMIZATIONS = base_flag(DISABLE_OPTIMIZATIONS), |
| 70 | + ALLOW_DERIVATIVES = base_flag(ALLOW_DERIVATIVES), |
| 71 | + FAIL_ON_PIPELINE_COMPILE_REQUIRED = base_flag(FAIL_ON_PIPELINE_COMPILE_REQUIRED), |
| 72 | + EARLY_RETURN_ON_FAILURE = base_flag(EARLY_RETURN_ON_FAILURE), |
| 73 | + SKIP_BUILT_IN_PRIMITIVES = 1<<12, |
| 74 | + SKIP_AABBS = 1<<13, |
| 75 | + NO_NULL_ANY_HIT_SHADERS = 1<<14, |
| 76 | + NO_NULL_CLOSEST_HIT_SHADERS = 1<<15, |
| 77 | + NO_NULL_MISS_SHADERS = 1<<16, |
| 78 | + NO_NULL_INTERSECTION_SHADERS = 1<<17, |
| 79 | + ALLOW_MOTION = 1<<20, |
| 80 | + }; |
| 81 | + #undef base_flag |
| 82 | + |
| 83 | + protected: |
| 84 | + using SpecInfo = ShaderType::SSpecInfo; |
| 85 | + template<typename ExtraLambda> |
| 86 | + inline bool impl_valid(ExtraLambda&& extra) const |
| 87 | + { |
| 88 | + if (!IPipeline<PipelineLayoutType>::SCreationParams::layout) |
| 89 | + return false; |
| 90 | + |
| 91 | + for (const auto info : shaders) |
| 92 | + { |
| 93 | + if (info.shader) |
| 94 | + { |
| 95 | + if (!extra(info)) |
| 96 | + return false; |
| 97 | + const auto stage = info.shader->getStage(); |
| 98 | + if ((stage & ~ICPUShader::E_SHADER_STAGE::ESS_ALL_RAY_TRACING) != 0) |
| 99 | + return false; |
| 100 | + if (!std::has_single_bit<std::underlying_type_t<ICPUShader::E_SHADER_STAGE>>(stage)) |
| 101 | + return false; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + // every shader must not be null. use SIndex::Unused to represent unused shader. |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + auto getShaderStage = [this](size_t index) -> ICPUShader::E_SHADER_STAGE |
| 111 | + { |
| 112 | + return shaders[index].shader->getStage(); |
| 113 | + }; |
| 114 | + |
| 115 | + auto isValidShaderIndex = [this, getShaderStage](size_t index, ICPUShader::E_SHADER_STAGE expectedStage, bool is_unused_shader_forbidden) -> bool |
| 116 | + { |
| 117 | + if (index == SShaderGroupsParams::SIndex::Unused) |
| 118 | + return !is_unused_shader_forbidden; |
| 119 | + if (index >= shaders.size()) |
| 120 | + return false; |
| 121 | + if (getShaderStage(index) != expectedStage) |
| 122 | + return false; |
| 123 | + return true; |
| 124 | + }; |
| 125 | + |
| 126 | + if (!isValidShaderIndex(shaderGroups.raygen.index, ICPUShader::E_SHADER_STAGE::ESS_RAYGEN, true)) |
| 127 | + { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + for (const auto& shaderGroup : shaderGroups.hits) |
| 132 | + { |
| 133 | + // https://docs.vulkan.org/spec/latest/chapters/pipelines.html#VUID-VkRayTracingPipelineCreateInfoKHR-flags-03470 |
| 134 | + if (!isValidShaderIndex(shaderGroup.anyHit, |
| 135 | + ICPUShader::E_SHADER_STAGE::ESS_ANY_HIT, |
| 136 | + bool(flags & FLAGS::NO_NULL_ANY_HIT_SHADERS))) |
| 137 | + return false; |
| 138 | + |
| 139 | + // https://docs.vulkan.org/spec/latest/chapters/pipelines.html#VUID-VkRayTracingPipelineCreateInfoKHR-flags-03471 |
| 140 | + if (!isValidShaderIndex(shaderGroup.closestHit, |
| 141 | + ICPUShader::E_SHADER_STAGE::ESS_CLOSEST_HIT, |
| 142 | + bool(flags & FLAGS::NO_NULL_CLOSEST_HIT_SHADERS))) |
| 143 | + return false; |
| 144 | + |
| 145 | + if (!isValidShaderIndex(shaderGroup.intersection, |
| 146 | + ICPUShader::E_SHADER_STAGE::ESS_INTERSECTION, |
| 147 | + false)) |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + for (const auto& shaderGroup : shaderGroups.misses) |
| 152 | + { |
| 153 | + if (!isValidShaderIndex(shaderGroup.index, |
| 154 | + ICPUShader::E_SHADER_STAGE::ESS_MISS, |
| 155 | + false)) |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + for (const auto& shaderGroup : shaderGroups.callables) |
| 160 | + { |
| 161 | + if (!isValidShaderIndex(shaderGroup.index, ICPUShader::E_SHADER_STAGE::ESS_CALLABLE, false)) |
| 162 | + return false; |
| 163 | + } |
| 164 | + return true; |
| 165 | + } |
| 166 | + |
| 167 | + public: |
| 168 | + inline bool valid() const |
| 169 | + { |
| 170 | + return impl_valid([](const SpecInfo& info)->bool |
| 171 | + { |
| 172 | + if (!info.valid()) |
| 173 | + return false; |
| 174 | + return false; |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + std::span<const SpecInfo> shaders = {}; |
| 179 | + SShaderGroupsParams shaderGroups; |
| 180 | + SCachedCreationParams cached = {}; |
| 181 | + // TODO: Could guess the required flags from SPIR-V introspection of declared caps |
| 182 | + core::bitflag<FLAGS> flags = FLAGS::NONE; |
| 183 | + }; |
| 184 | + |
| 185 | + inline const SCachedCreationParams& getCachedCreationParams() const { return m_params; } |
| 186 | + |
| 187 | + protected: |
| 188 | + explicit IRayTracingPipeline(const SCreationParams& _params) : |
| 189 | + IPipeline<PipelineLayoutType>(core::smart_refctd_ptr<const PipelineLayoutType>(_params.layout)), |
| 190 | + m_params(_params.cached), |
| 191 | + m_raygenShaderGroup(_params.shaderGroups.raygen), |
| 192 | + m_missShaderGroups(core::make_refctd_dynamic_array<SGeneralShaderGroupContainer>(_params.shaderGroups.misses)), |
| 193 | + m_hitShaderGroups(core::make_refctd_dynamic_array<SHitShaderGroupContainer>(_params.shaderGroups.hits)), |
| 194 | + m_callableShaderGroups(core::make_refctd_dynamic_array<SGeneralShaderGroupContainer>(_params.shaderGroups.callables)) |
| 195 | + {} |
| 196 | + |
| 197 | + SCachedCreationParams m_params; |
| 198 | + SGeneralShaderGroup m_raygenShaderGroup; |
| 199 | + SGeneralShaderGroupContainer m_missShaderGroups; |
| 200 | + SHitShaderGroupContainer m_hitShaderGroups; |
| 201 | + SGeneralShaderGroupContainer m_callableShaderGroups; |
| 202 | + |
| 203 | +}; |
| 204 | + |
| 205 | +} |
| 206 | + |
| 207 | +#endif |
0 commit comments