Skip to content

Commit f1f0c5b

Browse files
author
devsh
committed
Merge remote-tracking branch 'remotes/origin/mesh_loaders_kevin'
2 parents 48bdaca + 30a5a19 commit f1f0c5b

File tree

9 files changed

+362
-329
lines changed

9 files changed

+362
-329
lines changed

67_RayQueryGeometry/app_resources/common.hlsl

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55

66
NBL_CONSTEXPR uint32_t WorkgroupSize = 16;
77

8+
enum NormalType : uint32_t
9+
{
10+
NT_R8G8B8A8_SNORM,
11+
NT_R32G32B32_SFLOAT,
12+
};
13+
814
// we need bitfield support in NBL_HLSL_DECLARE_STRUCT it seems
915
struct SGeomInfo
1016
{
1117
uint64_t vertexBufferAddress;
1218
uint64_t indexBufferAddress;
19+
uint64_t normalBufferAddress;
1320

14-
uint32_t vertexStride : 29;
15-
uint32_t indexType : 2; // 16 bit, 32 bit or none
16-
uint32_t smoothNormals : 1; // flat for cube, rectangle, disk
17-
uint32_t padding;
21+
uint32_t normalType : 1;
22+
uint32_t indexType : 1; // 16 bit, 32 bit
1823
};
1924

2025
struct SPushConstants
@@ -28,20 +33,4 @@ struct SPushConstants
2833
float32_t2 offsetNDC;
2934
};
3035

31-
#ifdef __HLSL_VERSION
32-
enum ObjectType : uint32_t // matches c++
33-
{
34-
OT_CUBE = 0,
35-
OT_SPHERE,
36-
OT_CYLINDER,
37-
OT_RECTANGLE,
38-
OT_DISK,
39-
OT_ARROW,
40-
OT_CONE,
41-
OT_ICOSPHERE,
42-
43-
OT_COUNT
44-
};
45-
#endif
46-
4736
#endif // RQG_COMMON_HLSL

67_RayQueryGeometry/app_resources/render.comp.hlsl

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,69 +25,64 @@ float3 unpackNormals3x10(uint32_t v)
2525
return clamp(float3(pn) / 511.0, -1.0, 1.0);
2626
}
2727

28-
float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bary)
28+
float3 calculateNormals(int primID, SGeomInfo geom, float2 bary)
2929
{
3030
const uint indexType = geom.indexType;
31-
const uint vertexStride = geom.vertexStride;
31+
const uint normalType = geom.normalType;
3232

3333
const uint64_t vertexBufferAddress = geom.vertexBufferAddress;
3434
const uint64_t indexBufferAddress = geom.indexBufferAddress;
35+
const uint64_t normalBufferAddress = geom.normalBufferAddress;
3536

3637
uint32_t3 indices;
37-
switch (indexType)
38+
if (indexBufferAddress == 0)
3839
{
39-
case 0: // EIT_16BIT
40-
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint16_t3>::create(indexBufferAddress)+primID).deref().load());
41-
break;
42-
case 1: // EIT_32BIT
43-
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint32_t3>::create(indexBufferAddress)+primID).deref().load());
44-
break;
45-
default: // EIT_NONE
40+
indices[0] = primID * 3;
41+
indices[1] = indices[0] + 1;
42+
indices[2] = indices[0] + 2;
43+
}
44+
else {
45+
switch (indexType)
4646
{
47-
indices[0] = primID * 3;
48-
indices[1] = indices[0] + 1;
49-
indices[2] = indices[0] + 2;
47+
case 0: // EIT_16BIT
48+
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint16_t3>::create(indexBufferAddress)+primID).deref().load());
49+
break;
50+
case 1: // EIT_32BIT
51+
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint32_t3>::create(indexBufferAddress)+primID).deref().load());
52+
break;
5053
}
5154
}
5255

56+
if (normalBufferAddress == 0)
57+
{
58+
float3 v0 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * 12);
59+
float3 v1 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * 12);
60+
float3 v2 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * 12);
61+
62+
return normalize(cross(v2 - v0, v1 - v0));
63+
}
64+
5365
float3 n0, n1, n2;
54-
switch (instID)
66+
switch (normalType)
5567
{
56-
case OT_CUBE:
68+
case NT_R8G8B8A8_SNORM:
5769
{
58-
// TODO: document why the alignment is 2 here and nowhere else? isnt the `vertexStride` aligned to more than 2 anyway?
59-
uint32_t v0 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[0] * vertexStride, 2u);
60-
uint32_t v1 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[1] * vertexStride, 2u);
61-
uint32_t v2 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[2] * vertexStride, 2u);
70+
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4);
71+
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4);
72+
uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[2] * 4);
6273

6374
n0 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v0).xyz);
6475
n1 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v1).xyz);
6576
n2 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v2).xyz);
6677
}
6778
break;
68-
case OT_SPHERE:
69-
case OT_CYLINDER:
70-
case OT_ARROW:
71-
case OT_CONE:
79+
case NT_R32G32B32_SFLOAT:
7280
{
73-
uint32_t v0 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[0] * vertexStride);
74-
uint32_t v1 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[1] * vertexStride);
75-
uint32_t v2 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[2] * vertexStride);
76-
77-
n0 = normalize(unpackNormals3x10(v0));
78-
n1 = normalize(unpackNormals3x10(v1));
79-
n2 = normalize(unpackNormals3x10(v2));
81+
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12));
82+
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12));
83+
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12));
8084
}
8185
break;
82-
case OT_RECTANGLE:
83-
case OT_DISK:
84-
case OT_ICOSPHERE:
85-
default:
86-
{
87-
n0 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * vertexStride));
88-
n1 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * vertexStride));
89-
n2 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * vertexStride));
90-
}
9186
}
9287

9388
float3 barycentrics = float3(0.0, bary);
@@ -124,15 +119,16 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
124119

125120
if (spirv::rayQueryGetIntersectionTypeKHR(query, true) == spv::RayQueryCommittedIntersectionTypeRayQueryCommittedIntersectionTriangleKHR)
126121
{
127-
const int instID = spirv::rayQueryGetIntersectionInstanceIdKHR(query, true);
122+
const int instanceCustomIndex = spirv::rayQueryGetIntersectionInstanceCustomIndexKHR(query, true);
123+
const int geometryIndex = spirv::rayQueryGetIntersectionGeometryIndexKHR(query, true);
128124
const int primID = spirv::rayQueryGetIntersectionPrimitiveIndexKHR(query, true);
129125

130126
// TODO: candidate for `bda::__ptr<SGeomInfo>`
131-
const SGeomInfo geom = vk::RawBufferLoad<SGeomInfo>(pc.geometryInfoBuffer + instID * sizeof(SGeomInfo),8);
127+
const SGeomInfo geom = vk::RawBufferLoad<SGeomInfo>(pc.geometryInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(SGeomInfo), 8);
132128

133129
float3 normals;
134130
float2 barycentrics = spirv::rayQueryGetIntersectionBarycentricsKHR(query, true);
135-
normals = calculateSmoothNormals(instID, primID, geom, barycentrics);
131+
normals = calculateNormals(primID, geom, barycentrics);
136132

137133
normals = normalize(normals) * 0.5 + 0.5;
138134
color = float4(normals, 1.0);

67_RayQueryGeometry/include/common.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,20 @@ using namespace nbl::examples;
1515

1616
#include "app_resources/common.hlsl"
1717

18+
namespace nbl::scene
19+
{
20+
21+
using PolygonGeometryData = core::smart_refctd_ptr<ICPUPolygonGeometry>;
22+
using GeometryCollectionData = core::smart_refctd_ptr<ICPUGeometryCollection>;
23+
using GeometryData = std::variant<PolygonGeometryData, GeometryCollectionData>;
24+
struct ReferenceObjectCpu
25+
{
26+
core::matrix3x4SIMD transform;
27+
GeometryData data;
28+
uint32_t instanceID;
29+
};
30+
31+
}
32+
33+
1834
#endif // _NBL_THIS_EXAMPLE_COMMON_H_INCLUDED_

0 commit comments

Comments
 (0)