Skip to content

Commit 5287dab

Browse files
author
kevyuu
committed
Add arrow geometry to example 67
1 parent 822af13 commit 5287dab

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

67_RayQueryGeometry/app_resources/common.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct SGeomInfo
1212
uint64_t indexBufferAddress;
1313
uint64_t normalBufferAddress;
1414

15-
uint32_t vertexStride : 29;
15+
uint32_t objType : 29;
1616
uint32_t indexType : 2; // 16 bit, 32 bit or none
1717
uint32_t smoothNormals : 1; // flat for cube, rectangle, disk
1818
uint32_t padding;

67_RayQueryGeometry/app_resources/render.comp.hlsl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ float3 unpackNormals3x10(uint32_t v)
2828
float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bary)
2929
{
3030
const uint indexType = geom.indexType;
31-
const uint vertexStride = geom.vertexStride;
32-
const uint objType = instID;
31+
const uint objType = geom.objType;
3332

3433
const uint64_t vertexBufferAddress = geom.vertexBufferAddress;
3534
const uint64_t indexBufferAddress = geom.indexBufferAddress;
@@ -62,7 +61,6 @@ float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bar
6261
//case OT_ARROW:
6362
case OT_CONE:
6463
{
65-
// TODO: document why the alignment is 2 here and nowhere else? isnt the `vertexStride` aligned to more than 2 anyway?
6664
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4);
6765
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4);
6866
uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[2] * 4);

67_RayQueryGeometry/include/common.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum GeometryShader
5656
struct ReferenceObjectCpu
5757
{
5858
ObjectMeta meta;
59-
GeometryShader shadersType;
59+
core::matrix3x4SIMD transform;
6060
core::smart_refctd_ptr<ICPUPolygonGeometry> data;
6161
};
6262

67_RayQueryGeometry/main.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -491,20 +491,32 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built
491491
// triangles geometries
492492
auto gc = make_smart_refctd_ptr<CGeometryCreator>();
493493

494-
std::array<ReferenceObjectCpu, OT_COUNT> cpuObjects;
495-
cpuObjects[OT_CUBE] = ReferenceObjectCpu{ .meta = {.type = OT_CUBE, .name = "Cube Mesh" }, .shadersType = GP_BASIC, .data = gc->createCube({1.f, 1.f, 1.f}) };
496-
cpuObjects[OT_SPHERE] = ReferenceObjectCpu{ .meta = {.type = OT_SPHERE, .name = "Sphere Mesh" }, .shadersType = GP_BASIC, .data = gc->createSphere(2, 16, 16) };
497-
cpuObjects[OT_CYLINDER] = ReferenceObjectCpu{ .meta = {.type = OT_CYLINDER, .name = "Cylinder Mesh" }, .shadersType = GP_BASIC, .data = gc->createCylinder(2, 2, 20) };
498-
cpuObjects[OT_RECTANGLE] = ReferenceObjectCpu{ .meta = {.type = OT_RECTANGLE, .name = "Rectangle Mesh" }, .shadersType = GP_BASIC, .data = gc->createRectangle({1.5, 3}) };
499-
cpuObjects[OT_CONE] = ReferenceObjectCpu{ .meta = {.type = OT_CONE, .name = "Cone Mesh" }, .shadersType = GP_CONE, .data = gc->createCone(2, 3, 10) };
500-
cpuObjects[OT_ICOSPHERE] = ReferenceObjectCpu{ .meta = {.type = OT_ICOSPHERE, .name = "Icosphere Mesh" }, .shadersType = GP_ICO, .data = gc->createIcoSphere(1, 3, true) };
494+
auto transform_i = 0;
495+
auto nextTransform = [&transform_i]()
496+
{
497+
core::matrix3x4SIMD transform;
498+
transform.setTranslation(nbl::core::vectorSIMDf(5.f * transform_i, 0, 0, 0));
499+
transform_i++;
500+
return transform;
501+
};
501502

502-
auto geomInfoBuffer = ICPUBuffer::create({ OT_COUNT * sizeof(SGeomInfo) });
503+
std::vector<ReferenceObjectCpu> cpuObjects;
504+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_CUBE, .name = "Cube Mesh" }, .transform = nextTransform(), .data = gc->createCube({1.f, 1.f, 1.f})});
505+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_SPHERE, .name = "Sphere Mesh" }, .transform = nextTransform(), .data = gc->createSphere(2, 16, 16)});
506+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_CYLINDER, .name = "Cylinder Mesh" }, .transform = nextTransform(), .data = gc->createCylinder(2, 2, 20)});
507+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_RECTANGLE, .name = "Rectangle Mesh" }, .transform = nextTransform(), .data = gc->createRectangle({1.5, 3})});
508+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_CONE, .name = "Cone Mesh" }, .transform = nextTransform(), .data = gc->createCone(2, 3, 10)});
509+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_ICOSPHERE, .name = "Icosphere Mesh" }, .transform = nextTransform(), .data = gc->createIcoSphere(1, 3, true)});
510+
const auto arrowPolygons = gc->createArrow();
511+
const auto arrowTransform = nextTransform();
512+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_CYLINDER, .name = "Arrow Mesh" }, .transform = arrowTransform, .data = arrowPolygons[0]});
513+
cpuObjects.push_back(ReferenceObjectCpu{ .meta = {.type = OT_CONE, .name = "Arrow Mesh" }, .transform = arrowTransform, .data = arrowPolygons[1]});
514+
auto geomInfoBuffer = ICPUBuffer::create({ cpuObjects.size() * sizeof(SGeomInfo) });
503515

504516
SGeomInfo* geomInfos = reinterpret_cast<SGeomInfo*>(geomInfoBuffer->getPointer());
505517

506518
// get ICPUBuffers into ICPUBottomLevelAccelerationStructures
507-
std::array<smart_refctd_ptr<ICPUBottomLevelAccelerationStructure>, OT_COUNT> cpuBlas;
519+
std::vector<smart_refctd_ptr<ICPUBottomLevelAccelerationStructure>> cpuBlas(cpuObjects.size());
508520
for (uint32_t i = 0; i < cpuBlas.size(); i++)
509521
{
510522
auto triangles = make_refctd_dynamic_array<smart_refctd_dynamic_array<ICPUBottomLevelAccelerationStructure::Triangles<ICPUBuffer>>>(1u);
@@ -530,7 +542,7 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built
530542
}
531543

532544
// get ICPUBottomLevelAccelerationStructure into ICPUTopLevelAccelerationStructure
533-
auto geomInstances = make_refctd_dynamic_array<smart_refctd_dynamic_array<ICPUTopLevelAccelerationStructure::PolymorphicInstance>>(OT_COUNT);
545+
auto geomInstances = make_refctd_dynamic_array<smart_refctd_dynamic_array<ICPUTopLevelAccelerationStructure::PolymorphicInstance>>(cpuObjects.size());
534546
{
535547
uint32_t i = 0;
536548
for (auto instance = geomInstances->begin(); instance != geomInstances->end(); instance++, i++)
@@ -541,11 +553,7 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built
541553
inst.base.instanceCustomIndex = i;
542554
inst.base.instanceShaderBindingTableRecordOffset = 0;
543555
inst.base.mask = 0xFF;
544-
545-
core::matrix3x4SIMD transform;
546-
transform.setTranslation(nbl::core::vectorSIMDf(5.f * i, 0, 0, 0));
547-
inst.transform = transform;
548-
556+
inst.transform = cpuObjects[i].transform;
549557
instance->instance = inst;
550558
}
551559
}
@@ -612,9 +620,9 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built
612620

613621
CAssetConverter::patch_t<ICPUTopLevelAccelerationStructure> tlasPatch = {};
614622
tlasPatch.compactAfterBuild = true;
615-
std::array<CAssetConverter::patch_t<ICPUBottomLevelAccelerationStructure>,OT_COUNT> tmpBLASPatches = {};
616-
std::array<ICPUPolygonGeometry*, std::size(cpuObjects)> tmpGeometries;
617-
std::array<CAssetConverter::patch_t<asset::ICPUPolygonGeometry>, std::size(cpuObjects)> tmpGeometryPatches;
623+
std::vector<CAssetConverter::patch_t<ICPUBottomLevelAccelerationStructure>> tmpBLASPatches(cpuObjects.size());
624+
std::vector<ICPUPolygonGeometry*> tmpGeometries(cpuObjects.size());
625+
std::vector<CAssetConverter::patch_t<asset::ICPUPolygonGeometry>> tmpGeometryPatches(cpuObjects.size());
618626
{
619627
tmpBLASPatches.front().compactAfterBuild = true;
620628
std::fill(tmpBLASPatches.begin(),tmpBLASPatches.end(),tmpBLASPatches.front());
@@ -779,7 +787,7 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built
779787
.vertexBufferAddress = vertexBufferAddress,
780788
.indexBufferAddress = indexBufferBinding.buffer ? indexBufferBinding.buffer->getDeviceAddress() + indexBufferBinding.offset : vertexBufferAddress,
781789
.normalBufferAddress = normalBufferAddress,
782-
.vertexStride = gpuTriangles.vertexStride,
790+
.objType = cpuObject.meta.type,
783791
.indexType = gpuTriangles.indexType,
784792
.smoothNormals = s_smoothNormals[cpuObject.meta.type],
785793
};
@@ -792,7 +800,7 @@ class RayQueryGeometryApp final : public SimpleWindowedApplication, public Built
792800
{
793801
IGPUBuffer::SCreationParams params;
794802
params.usage = IGPUBuffer::EUF_STORAGE_BUFFER_BIT | IGPUBuffer::EUF_TRANSFER_DST_BIT | IGPUBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT;
795-
params.size = OT_COUNT * sizeof(SGeomInfo);
803+
params.size = cpuObjects.size() * sizeof(SGeomInfo);
796804
m_utils->createFilledDeviceLocalBufferOnDedMem(SIntendedSubmitInfo{ .queue = gQueue }, std::move(params), geomInfos).move_into(geometryInfoBuffer);
797805
}
798806

0 commit comments

Comments
 (0)