|
| 1 | +//===- AccelerationStructure.h - RT Acceleration Structure Types ----------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef OFFLOADTEST_API_ACCELERATIONSTRUCTURE_H |
| 10 | +#define OFFLOADTEST_API_ACCELERATIONSTRUCTURE_H |
| 11 | + |
| 12 | +#include "API/API.h" |
| 13 | +#include "API/Buffer.h" |
| 14 | +#include "API/Resources.h" |
| 15 | + |
| 16 | +#include "llvm/ADT/ArrayRef.h" |
| 17 | +#include "llvm/ADT/BitmaskEnum.h" |
| 18 | +#include "llvm/ADT/SmallVector.h" |
| 19 | +#include "llvm/Support/Error.h" |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | + |
| 23 | +namespace offloadtest { |
| 24 | +LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 25 | + |
| 26 | +enum AccelerationStructureBuildFlags : uint32_t { |
| 27 | + BuildFlagNone = 0, |
| 28 | + AllowUpdate = 1 << 0, |
| 29 | + PreferFastTrace = 1 << 1, |
| 30 | + PreferFastBuild = 1 << 2, |
| 31 | + // NOTE: keep LargestValue in LLVM_DECLARE_ENUM_AS_BITMASK at the end of this |
| 32 | + // header in sync when adding new flags. |
| 33 | +}; |
| 34 | + |
| 35 | +struct AccelerationStructureSizes { |
| 36 | + uint64_t ResultDataMaxSizeInBytes = 0; |
| 37 | + uint64_t ScratchDataSizeInBytes = 0; |
| 38 | + uint64_t UpdateScratchDataSizeInBytes = 0; |
| 39 | +}; |
| 40 | + |
| 41 | +struct TriangleGeometryDesc { |
| 42 | + Buffer *VertexBuffer = nullptr; |
| 43 | + uint64_t VertexBufferOffset = 0; |
| 44 | + uint32_t VertexCount = 0; |
| 45 | + uint32_t VertexStride = 0; |
| 46 | + Format VertexFormat = Format::RGB32Float; |
| 47 | + Buffer *IndexBuffer = nullptr; |
| 48 | + uint64_t IndexBufferOffset = 0; |
| 49 | + uint32_t IndexCount = 0; |
| 50 | + IndexFormat IdxFormat = IndexFormat::Uint32; |
| 51 | + bool Opaque = true; |
| 52 | +}; |
| 53 | + |
| 54 | +struct AABBGeometryDesc { |
| 55 | + Buffer *AABBBuffer = nullptr; |
| 56 | + uint64_t AABBBufferOffset = 0; |
| 57 | + uint32_t AABBCount = 0; |
| 58 | + uint32_t AABBStride = 24; |
| 59 | + bool Opaque = true; |
| 60 | +}; |
| 61 | + |
| 62 | +class AccelerationStructure; |
| 63 | + |
| 64 | +struct AccelerationStructureInstance { |
| 65 | + float Transform[3][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}}; |
| 66 | + uint32_t InstanceID = 0; |
| 67 | + uint8_t InstanceMask = 0xFF; |
| 68 | + AccelerationStructure *BLAS = nullptr; |
| 69 | +}; |
| 70 | + |
| 71 | +struct BLASBuildRequest { |
| 72 | + llvm::SmallVector<TriangleGeometryDesc> Triangles; |
| 73 | + llvm::SmallVector<AABBGeometryDesc> AABBs; |
| 74 | + AccelerationStructureBuildFlags Flags = BuildFlagNone; |
| 75 | + AccelerationStructureSizes Sizes; |
| 76 | +}; |
| 77 | + |
| 78 | +struct TLASBuildRequest { |
| 79 | + llvm::SmallVector<AccelerationStructureInstance> Instances; |
| 80 | + AccelerationStructureBuildFlags Flags = BuildFlagNone; |
| 81 | + AccelerationStructureSizes Sizes; |
| 82 | +}; |
| 83 | + |
| 84 | +inline llvm::Error validateTriangleGeometryDesc(const TriangleGeometryDesc &D) { |
| 85 | + if (!D.VertexBuffer) |
| 86 | + return llvm::createStringError( |
| 87 | + std::errc::invalid_argument, |
| 88 | + "TriangleGeometryDesc: VertexBuffer is null."); |
| 89 | + if (!isPositionCompatible(D.VertexFormat)) |
| 90 | + return llvm::createStringError( |
| 91 | + std::errc::invalid_argument, |
| 92 | + "TriangleGeometryDesc: VertexFormat '%s' is not position-compatible.", |
| 93 | + getFormatName(D.VertexFormat).data()); |
| 94 | + if (D.VertexStride < getFormatSizeInBytes(D.VertexFormat)) |
| 95 | + return llvm::createStringError( |
| 96 | + std::errc::invalid_argument, |
| 97 | + "TriangleGeometryDesc: VertexStride (%u) must be >= format size (%u).", |
| 98 | + D.VertexStride, getFormatSizeInBytes(D.VertexFormat)); |
| 99 | + if (D.VertexCount == 0) |
| 100 | + return llvm::createStringError(std::errc::invalid_argument, |
| 101 | + "TriangleGeometryDesc: VertexCount is 0."); |
| 102 | + if (D.IndexBuffer && D.IndexCount == 0) |
| 103 | + return llvm::createStringError( |
| 104 | + std::errc::invalid_argument, |
| 105 | + "TriangleGeometryDesc: IndexBuffer is set but IndexCount is 0."); |
| 106 | + if (!D.IndexBuffer && D.IndexCount != 0) |
| 107 | + return llvm::createStringError( |
| 108 | + std::errc::invalid_argument, |
| 109 | + "TriangleGeometryDesc: IndexCount is set but IndexBuffer is null."); |
| 110 | + if (D.IndexBuffer && D.IndexCount % 3 != 0) |
| 111 | + return llvm::createStringError( |
| 112 | + std::errc::invalid_argument, |
| 113 | + "TriangleGeometryDesc: IndexCount (%u) must be a multiple of 3.", |
| 114 | + D.IndexCount); |
| 115 | + if (!D.IndexBuffer && D.VertexCount % 3 != 0) |
| 116 | + return llvm::createStringError( |
| 117 | + std::errc::invalid_argument, |
| 118 | + "TriangleGeometryDesc: VertexCount (%u) must be a multiple of 3 when " |
| 119 | + "no index buffer is provided.", |
| 120 | + D.VertexCount); |
| 121 | + return llvm::Error::success(); |
| 122 | +} |
| 123 | + |
| 124 | +inline llvm::Error validateAABBGeometryDesc(const AABBGeometryDesc &D) { |
| 125 | + if (!D.AABBBuffer) |
| 126 | + return llvm::createStringError(std::errc::invalid_argument, |
| 127 | + "AABBGeometryDesc: AABBBuffer is null."); |
| 128 | + if (D.AABBCount == 0) |
| 129 | + return llvm::createStringError(std::errc::invalid_argument, |
| 130 | + "AABBGeometryDesc: AABBCount is 0."); |
| 131 | + if (D.AABBStride < 24) |
| 132 | + return llvm::createStringError( |
| 133 | + std::errc::invalid_argument, |
| 134 | + "AABBGeometryDesc: AABBStride (%u) must be >= 24.", D.AABBStride); |
| 135 | + return llvm::Error::success(); |
| 136 | +} |
| 137 | + |
| 138 | +inline llvm::Error validateBLASBuildRequest(const BLASBuildRequest &Req) { |
| 139 | + if (Req.Triangles.empty() && Req.AABBs.empty()) |
| 140 | + return llvm::createStringError( |
| 141 | + std::errc::invalid_argument, |
| 142 | + "BLASBuildRequest: Must have at least one geometry descriptor."); |
| 143 | + // DXR forbids mixing triangle and AABB geometry in a single BLAS, but D3D12 |
| 144 | + // won't catch the violation at build time — only Vulkan and Metal do. Reject |
| 145 | + // up-front for consistent behaviour across backends. |
| 146 | + if (!Req.Triangles.empty() && !Req.AABBs.empty()) |
| 147 | + return llvm::createStringError( |
| 148 | + std::errc::invalid_argument, |
| 149 | + "BLASBuildRequest: Cannot mix triangle and AABB geometry in a " |
| 150 | + "single BLAS."); |
| 151 | + for (const auto &T : Req.Triangles) |
| 152 | + if (auto Err = validateTriangleGeometryDesc(T)) |
| 153 | + return Err; |
| 154 | + for (const auto &A : Req.AABBs) |
| 155 | + if (auto Err = validateAABBGeometryDesc(A)) |
| 156 | + return Err; |
| 157 | + return llvm::Error::success(); |
| 158 | +} |
| 159 | + |
| 160 | +inline llvm::Error validateTLASBuildRequest(const TLASBuildRequest &Req) { |
| 161 | + if (Req.Instances.empty()) |
| 162 | + return llvm::createStringError( |
| 163 | + std::errc::invalid_argument, |
| 164 | + "TLASBuildRequest: Must have at least one instance."); |
| 165 | + for (size_t I = 0; I < Req.Instances.size(); ++I) |
| 166 | + if (!Req.Instances[I].BLAS) |
| 167 | + return llvm::createStringError( |
| 168 | + std::errc::invalid_argument, |
| 169 | + "TLASBuildRequest: Instance %zu has a null BLAS pointer.", I); |
| 170 | + return llvm::Error::success(); |
| 171 | +} |
| 172 | + |
| 173 | +class AccelerationStructure { |
| 174 | + GPUAPI API; |
| 175 | + |
| 176 | +public: |
| 177 | + virtual ~AccelerationStructure(); |
| 178 | + AccelerationStructure(const AccelerationStructure &) = delete; |
| 179 | + AccelerationStructure &operator=(const AccelerationStructure &) = delete; |
| 180 | + |
| 181 | + GPUAPI getAPI() const { return API; } |
| 182 | + |
| 183 | +protected: |
| 184 | + explicit AccelerationStructure(GPUAPI API) : API(API) {} |
| 185 | +}; |
| 186 | + |
| 187 | +} // namespace offloadtest |
| 188 | + |
| 189 | +namespace llvm { |
| 190 | +LLVM_DECLARE_ENUM_AS_BITMASK(::offloadtest::AccelerationStructureBuildFlags, |
| 191 | + ::offloadtest::PreferFastBuild); |
| 192 | +} // namespace llvm |
| 193 | + |
| 194 | +#endif // OFFLOADTEST_API_ACCELERATIONSTRUCTURE_H |
0 commit comments