Skip to content

Commit fcae0b4

Browse files
author
kevyuu
committed
Initial commit for example 71 to use ICPUPolygonGeometry
1 parent 62f1a26 commit fcae0b4

File tree

2 files changed

+101
-106
lines changed

2 files changed

+101
-106
lines changed

71_RayTracingPipeline/include/common.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,66 @@ using namespace nbl::examples;
1919

2020
#include "app_resources/common.hlsl"
2121

22+
namespace nbl::scene
23+
{
24+
25+
enum ObjectType : uint8_t
26+
{
27+
OT_CUBE,
28+
OT_SPHERE,
29+
OT_CYLINDER,
30+
OT_RECTANGLE,
31+
OT_DISK,
32+
OT_ARROW,
33+
OT_CONE,
34+
OT_ICOSPHERE,
35+
36+
OT_COUNT,
37+
OT_UNKNOWN = std::numeric_limits<uint8_t>::max()
38+
};
39+
40+
static constexpr uint32_t s_smoothNormals[OT_COUNT] = { 0, 1, 1, 0, 0, 1, 1, 1 };
41+
42+
struct ObjectMeta
43+
{
44+
ObjectType type = OT_UNKNOWN;
45+
std::string_view name = "Unknown";
46+
};
47+
48+
struct ObjectDrawHookCpu
49+
{
50+
nbl::core::matrix3x4SIMD model;
51+
ObjectMeta meta;
52+
};
53+
54+
struct ReferenceObjectCpu
55+
{
56+
ObjectMeta meta;
57+
core::smart_refctd_ptr<ICPUPolygonGeometry> data;
58+
Material material;
59+
core::matrix3x4SIMD transform;
60+
};
61+
62+
struct ReferenceObjectGpu
63+
{
64+
struct Bindings
65+
{
66+
nbl::asset::SBufferBinding<IGPUBuffer> vertex, index;
67+
};
68+
69+
ObjectMeta meta;
70+
Bindings bindings;
71+
uint32_t vertexStride;
72+
nbl::asset::E_INDEX_TYPE indexType = nbl::asset::E_INDEX_TYPE::EIT_UNKNOWN;
73+
uint32_t indexCount = {};
74+
MaterialPacked material;
75+
core::matrix3x4SIMD transform;
76+
77+
const bool useIndex() const
78+
{
79+
return bindings.index.buffer && (indexType != E_INDEX_TYPE::EIT_UNKNOWN);
80+
}
81+
};
82+
}
83+
2284
#endif // __NBL_THIS_EXAMPLE_COMMON_H_INCLUDED__

71_RayTracingPipeline/main.cpp

Lines changed: 39 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,25 +1109,23 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
11091109

11101110
// triangles geometries
11111111
auto geometryCreator = make_smart_refctd_ptr<CGeometryCreator>();
1112-
#if 1
1113-
return false;
1114-
#else
1112+
11151113
const auto cpuObjects = std::array{
1116-
ReferenceObjectCpu {
1117-
.meta = {.type = OT_RECTANGLE, .name = "Plane Mesh"},
1118-
.data = gc->createRectangleMesh(nbl::core::vector2df_SIMD(10, 10)),
1114+
scene::ReferenceObjectCpu {
1115+
.meta = {.type = scene::OT_RECTANGLE, .name = "Plane Mesh"},
1116+
.data = geometryCreator->createRectangle({10, 10}),
11191117
.material = defaultMaterial,
11201118
.transform = planeTransform,
11211119
},
1122-
ReferenceObjectCpu {
1123-
.meta = {.type = OT_CUBE, .name = "Cube Mesh"},
1124-
.data = gc->createCubeMesh(nbl::core::vector3df(1, 1, 1)),
1120+
scene::ReferenceObjectCpu {
1121+
.meta = {.type = scene::OT_CUBE, .name = "Cube Mesh"},
1122+
.data = geometryCreator->createCube({1, 1, 1}),
11251123
.material = defaultMaterial,
11261124
.transform = getTranslationMatrix(0, 0.5f, 0),
11271125
},
1128-
ReferenceObjectCpu {
1129-
.meta = {.type = OT_CUBE, .name = "Cube Mesh 2"},
1130-
.data = gc->createCubeMesh(nbl::core::vector3df(1.5, 1.5, 1.5)),
1126+
scene::ReferenceObjectCpu {
1127+
.meta = {.type = scene::OT_CUBE, .name = "Cube Mesh 2"},
1128+
.data = geometryCreator->createCube({1.5, 1.5, 1.5}),
11311129
.material = Material{
11321130
.ambient = {0.1, 0.1, 0.2},
11331131
.diffuse = {0.2, 0.2, 0.8},
@@ -1137,9 +1135,9 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
11371135
},
11381136
.transform = getTranslationMatrix(-5.0f, 1.0f, 0),
11391137
},
1140-
ReferenceObjectCpu {
1141-
.meta = {.type = OT_CUBE, .name = "Transparent Cube Mesh"},
1142-
.data = gc->createCubeMesh(nbl::core::vector3df(1.5, 1.5, 1.5)),
1138+
scene::ReferenceObjectCpu {
1139+
.meta = {.type = scene::OT_CUBE, .name = "Transparent Cube Mesh"},
1140+
.data = geometryCreator->createCube({1.5, 1.5, 1.5}),
11431141
.material = Material{
11441142
.ambient = {0.1, 0.2, 0.1},
11451143
.diffuse = {0.2, 0.8, 0.2},
@@ -1151,40 +1149,6 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
11511149
},
11521150
};
11531151

1154-
struct CPUTriBufferBindings
1155-
{
1156-
nbl::asset::SBufferBinding<ICPUBuffer> vertex, index;
1157-
};
1158-
std::array<CPUTriBufferBindings, std::size(cpuObjects)> cpuTriBuffers;
1159-
1160-
for (uint32_t i = 0; i < cpuObjects.size(); i++)
1161-
{
1162-
const auto& cpuObject = cpuObjects[i];
1163-
1164-
auto vBuffer = smart_refctd_ptr(cpuObject.data.bindings[0].buffer); // no offset
1165-
auto vUsage = bitflag(IGPUBuffer::EUF_STORAGE_BUFFER_BIT) | IGPUBuffer::EUF_TRANSFER_DST_BIT | IGPUBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF |
1166-
IGPUBuffer::EUF_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT | IGPUBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT;
1167-
vBuffer->addUsageFlags(vUsage);
1168-
vBuffer->setContentHash(vBuffer->computeContentHash());
1169-
1170-
auto iBuffer = smart_refctd_ptr(cpuObject.data.indexBuffer.buffer); // no offset
1171-
auto iUsage = bitflag(IGPUBuffer::EUF_STORAGE_BUFFER_BIT) | IGPUBuffer::EUF_TRANSFER_DST_BIT | IGPUBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF |
1172-
IGPUBuffer::EUF_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT | IGPUBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT;
1173-
1174-
if (cpuObject.data.indexType != EIT_UNKNOWN)
1175-
if (iBuffer)
1176-
{
1177-
iBuffer->addUsageFlags(iUsage);
1178-
iBuffer->setContentHash(iBuffer->computeContentHash());
1179-
}
1180-
1181-
cpuTriBuffers[i] = {
1182-
.vertex = {.offset = 0, .buffer = vBuffer},
1183-
.index = {.offset = 0, .buffer = iBuffer},
1184-
};
1185-
1186-
}
1187-
11881152
// procedural geometries
11891153
using Aabb = IGPUBottomLevelAccelerationStructure::AABB_t;
11901154

@@ -1233,10 +1197,10 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
12331197
const auto blasCount = std::size(cpuObjects) + 1;
12341198
const auto proceduralBlasIdx = std::size(cpuObjects);
12351199

1236-
std::array<smart_refctd_ptr<ICPUBottomLevelAccelerationStructure>, std::size(cpuObjects)+1u> cpuBlas;
1200+
std::array<smart_refctd_ptr<ICPUBottomLevelAccelerationStructure>, std::size(cpuObjects)+1u> cpuBlasList;
12371201
for (uint32_t i = 0; i < blasCount; i++)
12381202
{
1239-
auto& blas = cpuBlas[i];
1203+
auto& blas = cpuBlasList[i];
12401204
blas = make_smart_refctd_ptr<ICPUBottomLevelAccelerationStructure>();
12411205

12421206
if (i == proceduralBlasIdx)
@@ -1256,30 +1220,15 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
12561220
}
12571221
else
12581222
{
1259-
auto triangles = make_refctd_dynamic_array<smart_refctd_dynamic_array<ICPUBottomLevelAccelerationStructure::Triangles<ICPUBuffer>>>(1u);
1223+
auto triangles = make_refctd_dynamic_array<smart_refctd_dynamic_array<ICPUBottomLevelAccelerationStructure::Triangles<ICPUBuffer>>>(cpuObjects[i].data->exportForBLAS());
12601224
auto primitiveCounts = make_refctd_dynamic_array<smart_refctd_dynamic_array<uint32_t>>(1u);
12611225

12621226
auto& tri = triangles->front();
1263-
auto& primCount = primitiveCounts->front();
1264-
const auto& geom = cpuObjects[i];
1265-
const auto& cpuBuf = cpuTriBuffers[i];
12661227

1267-
const bool useIndex = geom.data.indexType != EIT_UNKNOWN;
1268-
const uint32_t vertexStride = geom.data.inputParams.bindings[0].stride;
1269-
const uint32_t numVertices = cpuBuf.vertex.buffer->getSize() / vertexStride;
1228+
auto& primCount = primitiveCounts->front();
1229+
primCount = cpuObjects[i].data->getPrimitiveCount();
12701230

1271-
if (useIndex)
1272-
primCount = geom.data.indexCount / 3;
1273-
else
1274-
primCount = numVertices / 3;
1275-
1276-
tri.vertexData[0] = cpuBuf.vertex;
1277-
tri.indexData = useIndex ? cpuBuf.index : cpuBuf.vertex;
1278-
tri.maxVertex = numVertices - 1;
1279-
tri.vertexStride = vertexStride;
1280-
tri.vertexFormat = EF_R32G32B32_SFLOAT;
1281-
tri.indexType = geom.data.indexType;
1282-
tri.geometryFlags = geom.material.isTransparent() ?
1231+
tri.geometryFlags = cpuObjects[i].material.isTransparent() ?
12831232
IGPUBottomLevelAccelerationStructure::GEOMETRY_FLAGS::NO_DUPLICATE_ANY_HIT_INVOCATION_BIT :
12841233
IGPUBottomLevelAccelerationStructure::GEOMETRY_FLAGS::OPAQUE_BIT;
12851234

@@ -1305,7 +1254,7 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
13051254
{
13061255
const auto isProceduralInstance = i == proceduralBlasIdx;
13071256
ICPUTopLevelAccelerationStructure::StaticInstance inst;
1308-
inst.base.blas = cpuBlas[i];
1257+
inst.base.blas = cpuBlasList[i];
13091258
inst.base.flags = static_cast<uint32_t>(IGPUTopLevelAccelerationStructure::INSTANCE_FLAGS::TRIANGLE_FACING_CULL_DISABLE_BIT);
13101259
inst.base.instanceCustomIndex = i;
13111260
inst.base.instanceShaderBindingTableRecordOffset = isProceduralInstance ? 2 : 0;;
@@ -1356,18 +1305,19 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
13561305
inputs.allocator = &myalloc;
13571306

13581307
std::array<ICPUTopLevelAccelerationStructure*, 1u> tmpTlas;
1359-
std::array<ICPUBuffer*, 2 * std::size(cpuObjects) + 1u> tmpBuffers;
1308+
std::array<ICPUPolygonGeometry*, std::size(cpuObjects)> tmpGeometries;
1309+
std::array<ICPUBuffer*, 1> tmpBuffers;
13601310
{
13611311
tmpTlas[0] = cpuTlas.get();
1312+
tmpBuffers[0] = cpuProcBuffer.get();
13621313
for (uint32_t i = 0; i < cpuObjects.size(); i++)
13631314
{
1364-
tmpBuffers[2 * i + 0] = cpuTriBuffers[i].vertex.buffer.get();
1365-
tmpBuffers[2 * i + 1] = cpuTriBuffers[i].index.buffer.get();
1315+
tmpGeometries[i] = cpuObjects[i].data.get();
13661316
}
1367-
tmpBuffers[2 * proceduralBlasIdx] = cpuProcBuffer.get();
13681317

13691318
std::get<CAssetConverter::SInputs::asset_span_t<ICPUTopLevelAccelerationStructure>>(inputs.assets) = tmpTlas;
13701319
std::get<CAssetConverter::SInputs::asset_span_t<ICPUBuffer>>(inputs.assets) = tmpBuffers;
1320+
std::get<CAssetConverter::SInputs::asset_span_t<ICPUPolygonGeometry>>(inputs.assets) = tmpGeometries;
13711321
}
13721322

13731323
auto reservation = converter->reserve(inputs);
@@ -1475,37 +1425,24 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
14751425
auto&& tlases = reservation.getGPUObjects<ICPUTopLevelAccelerationStructure>();
14761426
m_gpuTlas = tlases[0].value;
14771427
auto&& buffers = reservation.getGPUObjects<ICPUBuffer>();
1478-
for (uint32_t i = 0; i < cpuObjects.size(); i++)
1479-
{
1480-
auto& cpuObject = cpuObjects[i];
1481-
1482-
m_gpuTriangleGeometries.push_back(ReferenceObjectGpu{
1483-
.meta = cpuObject.meta,
1484-
.bindings = {
1485-
.vertex = {.offset = 0, .buffer = buffers[2 * i + 0].value },
1486-
.index = {.offset = 0, .buffer = buffers[2 * i + 1].value },
1487-
},
1488-
.vertexStride = cpuObject.data.inputParams.bindings[0].stride,
1489-
.indexType = cpuObject.data.indexType,
1490-
.indexCount = cpuObject.data.indexCount,
1491-
.material = hlsl::_static_cast<MaterialPacked>(cpuObject.material),
1492-
.transform = cpuObject.transform,
1493-
});
1494-
}
1428+
14951429
m_proceduralAabbBuffer = buffers[2 * proceduralBlasIdx].value;
14961430

1497-
for (uint32_t i = 0; i < m_gpuTriangleGeometries.size(); i++)
1431+
for (uint32_t i = 0; i < cpuObjects.size(); i++)
14981432
{
1499-
const auto& gpuObject = m_gpuTriangleGeometries[i];
1500-
const uint64_t vertexBufferAddress = gpuObject.bindings.vertex.buffer->getDeviceAddress();
1433+
const auto& cpuObject = cpuObjects[i];
1434+
const auto& cpuBlas = cpuBlasList[i];
1435+
const auto& geometry = cpuBlas->getTriangleGeometries()[0];
1436+
const uint64_t vertexBufferAddress = buffers[2 * i].value->getDeviceAddress();
1437+
const uint64_t indexBufferAddress = buffers[(2 * i) + 1].value->getDeviceAddress();
15011438
geomInfos[i] = {
1502-
.material = gpuObject.material,
1439+
.material = hlsl::_static_cast<MaterialPacked>(cpuObject.material),
15031440
.vertexBufferAddress = vertexBufferAddress,
1504-
.indexBufferAddress = gpuObject.useIndex() ? gpuObject.bindings.index.buffer->getDeviceAddress() : vertexBufferAddress,
1505-
.vertexStride = gpuObject.vertexStride,
1506-
.objType = gpuObject.meta.type,
1507-
.indexType = gpuObject.indexType,
1508-
.smoothNormals = s_smoothNormals[gpuObject.meta.type],
1441+
.indexBufferAddress = geometry.indexData.buffer ? indexBufferAddress : vertexBufferAddress,
1442+
.vertexStride = geometry.vertexStride,
1443+
.objType = cpuObject.meta.type,
1444+
.indexType = geometry.indexType,
1445+
.smoothNormals = scene::s_smoothNormals[cpuObject.meta.type],
15091446
};
15101447
}
15111448
}
@@ -1516,12 +1453,10 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
15161453
params.size = geomInfoBuffer->getSize();
15171454
m_utils->createFilledDeviceLocalBufferOnDedMem(SIntendedSubmitInfo{ .queue = queue }, std::move(params), geomInfos).move_into(m_triangleGeomInfoBuffer);
15181455
}
1519-
#endif
1456+
15201457
return true;
15211458
}
15221459

1523-
1524-
15251460
smart_refctd_ptr<IWindow> m_window;
15261461
smart_refctd_ptr<CSimpleResizeSurface<ISimpleManagedSurface::ISwapchainResources>> m_surface;
15271462
smart_refctd_ptr<ISemaphore> m_semaphore;
@@ -1570,8 +1505,6 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public app
15701505
} m_ui;
15711506
core::smart_refctd_ptr<IDescriptorPool> m_guiDescriptorSetPool;
15721507

1573-
// TODO: how much of this do we actually have to keep ?
1574-
// core::vector<ReferenceObjectGpu> m_gpuTriangleGeometries;
15751508
core::vector<SProceduralGeomInfo> m_gpuIntersectionSpheres;
15761509
uint32_t m_intersectionHitGroupIdx;
15771510

0 commit comments

Comments
 (0)