28 torch::Tensor faces) {
29 OptixAccelBuildOptions buildOptions = {};
30 OptixBuildInput buildInput = {};
33 size_t tempBufferSizeInBytes, outputBufferSizeInBytes;
35 buildOptions.buildFlags = OPTIX_BUILD_FLAG_NONE |
36 OPTIX_BUILD_FLAG_ALLOW_COMPACTION |
37 OPTIX_BUILD_FLAG_ALLOW_RANDOM_VERTEX_ACCESS;
38 buildOptions.operation = OPTIX_BUILD_OPERATION_BUILD;
39 buildOptions.motionOptions.numKeys = 0;
41 CUdeviceptr pVert = (CUdeviceptr)vertices.data_ptr();
42 CUdeviceptr pFace = (CUdeviceptr)faces.data_ptr();
44 buildInput.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES;
45 buildInput.triangleArray.vertexBuffers = &pVert;
46 buildInput.triangleArray.numVertices = vertices.size(0);
47 buildInput.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3;
48 buildInput.triangleArray.vertexStrideInBytes =
sizeof(
vec3f);
49 buildInput.triangleArray.indexBuffer = pFace;
50 buildInput.triangleArray.numIndexTriplets = faces.size(0);
51 buildInput.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3;
52 buildInput.triangleArray.indexStrideInBytes =
sizeof(
vec3i);
53 buildInput.triangleArray.preTransform = 0;
55 buildInput.triangleArray.numSbtRecords = 1;
56 buildInput.triangleArray.sbtIndexOffsetBuffer = 0;
57 buildInput.triangleArray.sbtIndexOffsetSizeInBytes = 0;
58 buildInput.triangleArray.sbtIndexOffsetStrideInBytes = 0;
60 uint32_t triangleBuildFlags =
61 OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
62 buildInput.triangleArray.flags = &triangleBuildFlags;
64 OptixAccelBufferSizes bufferSizes = {};
66 &buildInput, 1, &bufferSizes));
70 accelStructureBuffer.
alloc(bufferSizes.outputSizeInBytes);
71 tempBuffer.
alloc(bufferSizes.tempSizeInBytes);
74 compactedSizeBuffer.
alloc(
sizeof(uint64_t));
75 OptixAccelEmitDesc emitDesc;
76 emitDesc.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE;
77 emitDesc.result = compactedSizeBuffer.
d_pointer();
82 (CUdeviceptr)accelStructureBuffer.
d_ptr,
87 uint64_t compactedSize;
88 compactedSizeBuffer.
download(&compactedSize, 1);
97 compactedSizeBuffer.
free();
99 accelStructureBuffer.
free();
162 const torch::Tensor &origins,
163 const torch::Tensor &directions) {
168 torch::TensorOptions().dtype(torch::kBool).device(torch::kCUDA);
170 auto nray =
prod(resultSize);
171 auto result = torch::empty(resultSize, options);
192 const torch::Tensor &origins,
193 const torch::Tensor &directions) {
198 torch::TensorOptions().dtype(torch::kInt).device(torch::kCUDA);
200 auto nray =
prod(resultSize);
201 auto result = torch::empty(resultSize, options);
231std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor,
234 torch::Tensor directions) {
240 torch::TensorOptions().dtype(torch::kBool).device(torch::kCUDA);
242 auto hitbuf = torch::empty(hitbufSize, hitbufOptions);
244 auto frontbuf = torch::empty(hitbufSize, hitbufOptions);
247 torch::TensorOptions().dtype(torch::kInt).device(torch::kCUDA);
249 auto tibuf = torch::empty(tibufSize, tibufOptions);
252 torch::TensorOptions().dtype(torch::kFloat).device(torch::kCUDA);
254 auto locbuf = torch::empty(locbufSize, locbufOptions);
257 torch::TensorOptions().dtype(torch::kFloat).device(torch::kCUDA);
259 auto uvbuf = torch::empty(uvbufSize, uvbufOptions);
260 auto nray =
prod(hitbufSize);
288 return {hitbuf, frontbuf, tibuf, locbuf, uvbuf};
292 torch::Tensor origins, torch::Tensor directions) {
297 auto hitCountBufOptions =
298 torch::TensorOptions().dtype(torch::kInt).device(torch::kCUDA);
300 torch::zeros(hitCountBufSize, hitCountBufOptions).contiguous();
301 auto nray =
prod(hitCountBufSize);
326 torch::Tensor directions) {
333 hitCountBuf = hitCountBuf.flatten();
334 hitCountBuf = torch::where(hitCountBuf <=
MAX_ANYHIT_SIZE, hitCountBuf,
336 auto globalIdxBuf = hitCountBuf.cumsum(0);
337 auto globalIdxBufOptions =
338 torch::TensorOptions().dtype(torch::kInt).device(torch::kCUDA);
339 auto nhits = globalIdxBuf[-1].item<
int>();
340 globalIdxBuf = torch::cat({torch::zeros({1}, globalIdxBufOptions),
341 torch::slice(hitCountBuf, 0, 0, -1)});
344 torch::TensorOptions().dtype(torch::kFloat).device(torch::kCUDA);
345 auto locbuf = torch::empty({nhits, 3}, locbufOptions);
347 torch::TensorOptions().dtype(torch::kInt).device(torch::kCUDA);
348 auto tibuf = torch::empty({nhits}, idxbufOptions);
349 auto ribuf = torch::empty({nhits}, idxbufOptions);
376 return {locbuf, ribuf, tibuf};
std::tuple< torch::Tensor, torch::Tensor, torch::Tensor > intersectsLocation(OptixAccelStructureWrapperCPP as, torch::Tensor origins, torch::Tensor directions)
std::tuple< torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor > intersectsClosest(OptixAccelStructureWrapperCPP as, torch::Tensor origins, torch::Tensor directions)
Find if ray hits any triangle and return ray index, triangle index, hit location and uv.