Skip to content

Commit 25f1cd4

Browse files
author
kevyuu
committed
Fix example 71
1 parent 1bbfd09 commit 25f1cd4

File tree

4 files changed

+71
-86
lines changed

4 files changed

+71
-86
lines changed

71_RayTracingPipeline/app_resources/common.hlsl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ struct SProceduralGeomInfo
8686
float32_t radius;
8787
};
8888

89+
enum NormalType : uint32_t
90+
{
91+
NT_R8G8B8A8_SNORM,
92+
NT_R32G32B32_SFLOAT,
93+
NT_UNKNOWN
94+
};
8995

9096
struct STriangleGeomInfo
9197
{
@@ -94,10 +100,8 @@ struct STriangleGeomInfo
94100
uint64_t indexBufferAddress;
95101
uint64_t normalBufferAddress;
96102

97-
uint32_t vertexStride : 26;
98-
uint32_t objType: 3;
99-
uint32_t indexType : 2; // 16 bit, 32 bit or none
100-
uint32_t smoothNormals : 1; // flat for cube, rectangle, disk
103+
uint32_t normalType : 2;
104+
uint32_t indexType : 1; // 16 bit, 32 bit
101105

102106
};
103107

71_RayTracingPipeline/app_resources/raytrace.rchit.hlsl

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,87 @@
11
#include "common.hlsl"
22

3+
#include "nbl/builtin/hlsl/bda/__ptr.hlsl"
4+
35
[[vk::push_constant]] SPushConstants pc;
46

5-
float32_t3 fetchVertexNormal(int instID, int primID, STriangleGeomInfo geom, float2 bary)
7+
float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
68
{
7-
uint idxOffset = primID * 3;
8-
99
const uint indexType = geom.indexType;
10-
const uint vertexStride = geom.vertexStride;
11-
12-
const uint32_t objType = geom.objType;
10+
const uint normalType = geom.normalType;
11+
12+
const uint64_t vertexBufferAddress = geom.vertexBufferAddress;
1313
const uint64_t indexBufferAddress = geom.indexBufferAddress;
14-
15-
uint i0, i1, i2;
16-
switch (indexType)
14+
const uint64_t normalBufferAddress = geom.normalBufferAddress;
15+
16+
uint32_t3 indices;
17+
if (indexBufferAddress == 0)
1718
{
18-
case 0: // EIT_16BIT
19-
{
20-
i0 = uint32_t(vk::RawBufferLoad < uint16_t > (indexBufferAddress + (idxOffset + 0) * sizeof(uint16_t), 2u));
21-
i1 = uint32_t(vk::RawBufferLoad < uint16_t > (indexBufferAddress + (idxOffset + 1) * sizeof(uint16_t), 2u));
22-
i2 = uint32_t(vk::RawBufferLoad < uint16_t > (indexBufferAddress + (idxOffset + 2) * sizeof(uint16_t), 2u));
23-
}
24-
break;
25-
case 1: // EIT_32BIT
26-
{
27-
i0 = vk::RawBufferLoad < uint32_t > (indexBufferAddress + (idxOffset + 0) * sizeof(uint32_t));
28-
i1 = vk::RawBufferLoad < uint32_t > (indexBufferAddress + (idxOffset + 1) * sizeof(uint32_t));
29-
i2 = vk::RawBufferLoad < uint32_t > (indexBufferAddress + (idxOffset + 2) * sizeof(uint32_t));
30-
}
31-
break;
32-
default: // EIT_NONE
19+
indices[0] = primID * 3;
20+
indices[1] = indices[0] + 1;
21+
indices[2] = indices[0] + 2;
22+
}
23+
else {
24+
switch (indexType)
3325
{
34-
i0 = idxOffset;
35-
i1 = idxOffset + 1;
36-
i2 = idxOffset + 2;
26+
case 0: // EIT_16BIT
27+
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint16_t3>::create(indexBufferAddress)+primID).deref().load());
28+
break;
29+
case 1: // EIT_32BIT
30+
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint32_t3>::create(indexBufferAddress)+primID).deref().load());
31+
break;
3732
}
3833
}
3934

40-
const uint64_t normalVertexBufferAddress = geom.normalBufferAddress;
41-
float3 n0, n1, n2;
35+
if (normalBufferAddress == 0 || normalType == NT_UNKNOWN)
36+
{
37+
float3 v0 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * 12);
38+
float3 v1 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * 12);
39+
float3 v2 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * 12);
40+
41+
return normalize(cross(v2 - v0, v1 - v0));
42+
}
4243

4344
float3 n0, n1, n2;
44-
switch (objType)
45+
switch (normalType)
4546
{
46-
case OT_CUBE:
47-
case OT_SPHERE:
48-
case OT_RECTANGLE:
49-
case OT_CYLINDER:
50-
//case OT_ARROW:
51-
case OT_CONE:
47+
case NT_R8G8B8A8_SNORM:
5248
{
53-
// TODO: document why the alignment is 2 here and nowhere else? isnt the `vertexStride` aligned to more than 2 anyway?
54-
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + i0 * 4);
55-
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + i1 * 4);
56-
uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + i2 * 4);
49+
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4);
50+
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4);
51+
uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[2] * 4);
5752

5853
n0 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v0).xyz);
5954
n1 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v1).xyz);
6055
n2 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v2).xyz);
6156
}
6257
break;
63-
case OT_ICOSPHERE:
64-
default:
58+
case NT_R32G32B32_SFLOAT:
6559
{
66-
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + i0 * 12));
67-
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + i1 * 12));
68-
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + i2 * 12));
60+
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12));
61+
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12));
62+
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12));
6963
}
64+
break;
7065
}
7166

7267
float3 barycentrics = float3(0.0, bary);
73-
barycentrics.x = 1.0 - barycentrics.y - barycentrics.z;
74-
return normalize(barycentrics.x * n0 + barycentrics.y * n1 + barycentrics.z * n2);
68+
barycentrics.x = 1.0 - barycentrics.y - barycentrics.z;
69+
70+
return barycentrics.x * n0 + barycentrics.y * n1 + barycentrics.z * n2;
7571
}
7672

73+
7774
[shader("closesthit")]
7875
void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
7976
{
80-
const int instID = InstanceID();
8177
const int primID = PrimitiveIndex();
82-
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo));
83-
const float32_t3 vertexNormal = fetchVertexNormal(instID, primID, geom, attribs.barycentrics);
78+
const int instanceCustomIndex = InstanceIndex();
79+
const int geometryIndex = GeometryIndex();
80+
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo));
81+
const float32_t3 vertexNormal = calculateNormals(primID, geom, attribs.barycentrics);
8482
const float32_t3 worldNormal = normalize(mul(vertexNormal, WorldToObject3x4()).xyz);
8583

86-
payload.materialId = MaterialId::createTriangle(instID);
84+
payload.materialId = MaterialId::createTriangle(instanceCustomIndex);
8785

8886
payload.worldNormal = worldNormal;
8987
payload.rayDistance = RayTCurrent();

71_RayTracingPipeline/include/common.hpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,8 @@ using namespace nbl::examples;
2222
namespace nbl::scene
2323
{
2424

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-
4825
struct ReferenceObjectCpu
4926
{
50-
ObjectMeta meta;
5127
core::smart_refctd_ptr<ICPUPolygonGeometry> data;
5228
Material material;
5329
core::matrix3x4SIMD transform;

71_RayTracingPipeline/main.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,19 +1114,16 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public Bui
11141114

11151115
const auto cpuObjects = std::array{
11161116
scene::ReferenceObjectCpu {
1117-
.meta = {.type = scene::OT_RECTANGLE, .name = "Plane Mesh"},
11181117
.data = geometryCreator->createRectangle({10, 10}),
11191118
.material = defaultMaterial,
11201119
.transform = planeTransform,
11211120
},
11221121
scene::ReferenceObjectCpu {
1123-
.meta = {.type = scene::OT_CUBE, .name = "Cube Mesh"},
11241122
.data = geometryCreator->createCube({1, 1, 1}),
11251123
.material = defaultMaterial,
11261124
.transform = getTranslationMatrix(0, 0.5f, 0),
11271125
},
11281126
scene::ReferenceObjectCpu {
1129-
.meta = {.type = scene::OT_CUBE, .name = "Cube Mesh 2"},
11301127
.data = geometryCreator->createCube({1.5, 1.5, 1.5}),
11311128
.material = Material{
11321129
.ambient = {0.1, 0.1, 0.2},
@@ -1138,7 +1135,6 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public Bui
11381135
.transform = getTranslationMatrix(-5.0f, 1.0f, 0),
11391136
},
11401137
scene::ReferenceObjectCpu {
1141-
.meta = {.type = scene::OT_CUBE, .name = "Transparent Cube Mesh"},
11421138
.data = geometryCreator->createCube({1.5, 1.5, 1.5}),
11431139
.material = Material{
11441140
.ambient = {0.1, 0.2, 0.1},
@@ -1448,6 +1444,19 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public Bui
14481444

14491445
const auto& normalView = gpuPolygon->getNormalView();
14501446
const uint64_t normalBufferAddress = normalView ? normalView.src.buffer->getDeviceAddress() + normalView.src.offset : 0;
1447+
const auto normalType = [&normalView]
1448+
{
1449+
if (!normalView) return NT_UNKNOWN;
1450+
switch (normalView.composed.format)
1451+
{
1452+
case EF_R32G32B32_SFLOAT:
1453+
return NT_R32G32B32_SFLOAT;
1454+
case EF_R8G8B8A8_SNORM:
1455+
return NT_R8G8B8A8_SNORM;
1456+
default:
1457+
return NT_UNKNOWN;
1458+
}
1459+
}();
14511460

14521461
const auto& indexBufferBinding = gpuTriangles.indexData;
14531462
auto& geomInfo = geomInfos[i];
@@ -1456,10 +1465,8 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public Bui
14561465
.vertexBufferAddress = vertexBufferAddress,
14571466
.indexBufferAddress = indexBufferBinding.buffer ? indexBufferBinding.buffer->getDeviceAddress() + indexBufferBinding.offset : vertexBufferAddress,
14581467
.normalBufferAddress = normalBufferAddress,
1459-
.vertexStride = gpuTriangles.vertexStride,
1460-
.objType = cpuObject.meta.type,
1468+
.normalType = normalType,
14611469
.indexType = gpuTriangles.indexType,
1462-
.smoothNormals = scene::s_smoothNormals[cpuObject.meta.type],
14631470
};
14641471

14651472
m_gpuPolygons[i] = gpuPolygon;

0 commit comments

Comments
 (0)