Skip to content

Commit 1bbfd09

Browse files
author
kevyuu
committed
Fix example 67
1 parent 5287dab commit 1bbfd09

File tree

4 files changed

+160
-133
lines changed

4 files changed

+160
-133
lines changed

67_RayQueryGeometry/app_resources/common.hlsl

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55

66
NBL_CONSTEXPR uint32_t WorkgroupSize = 16;
77

8+
enum NormalType : uint32_t
9+
{
10+
NT_R8G8B8A8_SNORM,
11+
NT_R32G32B32_SFLOAT,
12+
NT_UNKNOWN
13+
};
14+
815
// we need bitfield support in NBL_HLSL_DECLARE_STRUCT it seems
916
struct SGeomInfo
1017
{
1118
uint64_t vertexBufferAddress;
1219
uint64_t indexBufferAddress;
1320
uint64_t normalBufferAddress;
1421

15-
uint32_t objType : 29;
16-
uint32_t indexType : 2; // 16 bit, 32 bit or none
17-
uint32_t smoothNormals : 1; // flat for cube, rectangle, disk
18-
uint32_t padding;
22+
uint32_t normalType : 2;
23+
uint32_t indexType : 1; // 16 bit, 32 bit
1924
};
2025

2126
struct SPushConstants
@@ -29,18 +34,4 @@ struct SPushConstants
2934
float32_t2 offsetNDC;
3035
};
3136

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

67_RayQueryGeometry/app_resources/render.comp.hlsl

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,47 @@ 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 objType = geom.objType;
31+
const uint normalType = geom.normalType;
3232

3333
const uint64_t vertexBufferAddress = geom.vertexBufferAddress;
3434
const uint64_t indexBufferAddress = geom.indexBufferAddress;
3535
const uint64_t normalBufferAddress = geom.normalBufferAddress;
3636

3737
uint32_t3 indices;
38-
switch (indexType)
38+
if (indexBufferAddress == 0)
3939
{
40-
case 0: // EIT_16BIT
41-
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint16_t3>::create(indexBufferAddress)+primID).deref().load());
42-
break;
43-
case 1: // EIT_32BIT
44-
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint32_t3>::create(indexBufferAddress)+primID).deref().load());
45-
break;
46-
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)
4746
{
48-
indices[0] = primID * 3;
49-
indices[1] = indices[0] + 1;
50-
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;
5153
}
5254
}
5355

56+
if (normalBufferAddress == 0 || normalType == NT_UNKNOWN)
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+
5465
float3 n0, n1, n2;
55-
switch (objType)
66+
switch (normalType)
5667
{
57-
case OT_CUBE:
58-
case OT_SPHERE:
59-
case OT_RECTANGLE:
60-
case OT_CYLINDER:
61-
//case OT_ARROW:
62-
case OT_CONE:
68+
case NT_R8G8B8A8_SNORM:
6369
{
6470
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4);
6571
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4);
@@ -70,13 +76,13 @@ float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bar
7076
n2 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v2).xyz);
7177
}
7278
break;
73-
case OT_ICOSPHERE:
74-
default:
79+
case NT_R32G32B32_SFLOAT:
7580
{
7681
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12));
7782
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12));
7883
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12));
7984
}
85+
break;
8086
}
8187

8288
float3 barycentrics = float3(0.0, bary);
@@ -113,15 +119,16 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
113119

114120
if (spirv::rayQueryGetIntersectionTypeKHR(query, true) == spv::RayQueryCommittedIntersectionTypeRayQueryCommittedIntersectionTriangleKHR)
115121
{
116-
const int instID = spirv::rayQueryGetIntersectionInstanceIdKHR(query, true);
122+
const int instanceCustomIndex = spirv::rayQueryGetIntersectionInstanceCustomIndexKHR(query, true);
123+
const int geometryIndex = spirv::rayQueryGetIntersectionGeometryIndexKHR(query, true);
117124
const int primID = spirv::rayQueryGetIntersectionPrimitiveIndexKHR(query, true);
118125

119126
// TODO: candidate for `bda::__ptr<SGeomInfo>`
120-
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);
121128

122129
float3 normals;
123130
float2 barycentrics = spirv::rayQueryGetIntersectionBarycentricsKHR(query, true);
124-
normals = calculateSmoothNormals(instID, primID, geom, barycentrics);
131+
normals = calculateNormals(primID, geom, barycentrics);
125132

126133
normals = normalize(normals) * 0.5 + 0.5;
127134
color = float4(normals, 1.0);

67_RayQueryGeometry/include/common.hpp

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,67 +17,17 @@ using namespace nbl::examples;
1717

1818
namespace nbl::scene
1919
{
20-
enum ObjectType : uint8_t
21-
{
22-
OT_CUBE,
23-
OT_SPHERE,
24-
OT_CYLINDER,
25-
OT_RECTANGLE,
26-
OT_CONE,
27-
OT_ICOSPHERE,
28-
29-
OT_COUNT,
30-
OT_UNKNOWN = std::numeric_limits<uint8_t>::max()
31-
};
32-
33-
static constexpr uint32_t s_smoothNormals[OT_COUNT] = { 0, 1, 1, 0, 1, 1 };
34-
35-
struct ObjectMeta
36-
{
37-
ObjectType type = OT_UNKNOWN;
38-
std::string_view name = "Unknown";
39-
};
40-
41-
struct ObjectDrawHookCpu
42-
{
43-
nbl::core::matrix3x4SIMD model;
44-
ObjectMeta meta;
45-
};
46-
47-
enum GeometryShader
48-
{
49-
GP_BASIC = 0,
50-
GP_CONE,
51-
GP_ICO,
52-
53-
GP_COUNT
54-
};
5520

21+
using PolygonGeometryData = core::smart_refctd_ptr<ICPUPolygonGeometry>;
22+
using GeometryCollectionData = core::smart_refctd_ptr<ICPUGeometryCollection>;
23+
using GeometryData = std::variant<PolygonGeometryData, GeometryCollectionData>;
5624
struct ReferenceObjectCpu
5725
{
58-
ObjectMeta meta;
5926
core::matrix3x4SIMD transform;
60-
core::smart_refctd_ptr<ICPUPolygonGeometry> data;
27+
GeometryData data;
28+
uint32_t instanceID;
6129
};
6230

63-
struct ReferenceObjectGpu
64-
{
65-
struct Bindings
66-
{
67-
nbl::asset::SBufferBinding<IGPUBuffer> vertex, index;
68-
};
69-
70-
ObjectMeta meta;
71-
Bindings bindings;
72-
uint32_t vertexStride;
73-
nbl::asset::E_INDEX_TYPE indexType = nbl::asset::E_INDEX_TYPE::EIT_UNKNOWN;
74-
uint32_t indexCount = {};
75-
76-
const bool useIndex() const
77-
{
78-
return bindings.index.buffer && (indexType != E_INDEX_TYPE::EIT_UNKNOWN);
79-
}
80-
};
8131
}
8232

8333

0 commit comments

Comments
 (0)