Skip to content

Commit e1e8dd6

Browse files
committed
Replaced vk::RawBufferLoad with vk::PointerBuffer in example 71
1 parent eb1e29f commit e1e8dd6

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

71_RayTracingPipeline/app_resources/common.hlsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
55
#include "nbl/builtin/hlsl/cpp_compat/basic.h"
66
#include "nbl/builtin/hlsl/random/pcg.hlsl"
7+
#include "nbl/builtin/hlsl/type_traits.hlsl"
78

89
NBL_CONSTEXPR uint32_t WorkgroupSize = 16;
910
NBL_CONSTEXPR uint32_t MAX_UNORM_10 = 1023;
@@ -78,6 +79,9 @@ struct MaterialPacked
7879
return (xi>>22) > alpha;
7980
}
8081
};
82+
#ifdef __HLSL_VERSION
83+
NBL_REGISTER_OBJ_TYPE(MaterialPacked, 4)
84+
#endif
8185

8286
struct SProceduralGeomInfo
8387
{
@@ -103,6 +107,9 @@ struct STriangleGeomInfo
103107
uint32_t indexType : 1; // 16 bit, 32 bit
104108

105109
};
110+
#ifdef __HLSL_VERSION
111+
NBL_REGISTER_OBJ_TYPE(STriangleGeomInfo, 8)
112+
#endif
106113

107114
enum E_GEOM_TYPE : uint16_t
108115
{

71_RayTracingPipeline/app_resources/raytrace.rahit.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ using namespace nbl::hlsl;
1010
void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
1111
{
1212
const int instID = spirv::InstanceCustomIndexKHR;
13-
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo), 8);
13+
const static uint64_t STriangleGeomInfoAlignment = nbl::hlsl::alignment_of_v<STriangleGeomInfo>;
14+
const STriangleGeomInfo geom = vk::BufferPointer<STriangleGeomInfo, STriangleGeomInfoAlignment>(pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo)).Get();
1415

1516
const uint32_t bitpattern = payload.pcg();
1617
// Cannot use spirv::ignoreIntersectionKHR and spirv::terminateRayKHR due to https://github.com/microsoft/DirectXShaderCompiler/issues/7279

71_RayTracingPipeline/app_resources/raytrace.rchit.hlsl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
3838

3939
if (normalBufferAddress == 0)
4040
{
41-
float3 v0 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * 12, 8);
42-
float3 v1 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * 12, 8);
43-
float3 v2 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * 12, 8);
41+
float3 v0 = (nbl::hlsl::bda::__ptr<float3>::create(vertexBufferAddress) + indices[0]).deref().load();
42+
float3 v1 = (nbl::hlsl::bda::__ptr<float3>::create(vertexBufferAddress) + indices[1]).deref().load();
43+
float3 v2 = (nbl::hlsl::bda::__ptr<float3>::create(vertexBufferAddress) + indices[2]).deref().load();
4444

4545
return normalize(cross(v2 - v0, v1 - v0));
4646
}
@@ -50,9 +50,9 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
5050
{
5151
case NT_R8G8B8A8_SNORM:
5252
{
53-
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4, 8);
54-
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4, 8);
55-
uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[2] * 4, 8);
53+
uint32_t v0 = (nbl::hlsl::bda::__ptr<uint32_t>::create(normalBufferAddress) + indices[0]).deref().load();
54+
uint32_t v1 = (nbl::hlsl::bda::__ptr<uint32_t>::create(normalBufferAddress) + indices[1]).deref().load();
55+
uint32_t v2 = (nbl::hlsl::bda::__ptr<uint32_t>::create(normalBufferAddress) + indices[2]).deref().load();
5656

5757
n0 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v0).xyz);
5858
n1 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v1).xyz);
@@ -61,9 +61,13 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
6161
break;
6262
case NT_R32G32B32_SFLOAT:
6363
{
64-
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12, 8));
65-
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12, 8));
66-
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12, 8));
64+
float3 v0 = (nbl::hlsl::bda::__ptr<float3>::create(normalBufferAddress) + indices[0]).deref().load();
65+
float3 v1 = (nbl::hlsl::bda::__ptr<float3>::create(normalBufferAddress) + indices[1]).deref().load();
66+
float3 v2 = (nbl::hlsl::bda::__ptr<float3>::create(normalBufferAddress) + indices[2]).deref().load();
67+
68+
n0 = normalize(v0);
69+
n1 = normalize(v1);
70+
n2 = normalize(v2);
6771
}
6872
break;
6973
}
@@ -81,7 +85,8 @@ void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes
8185
const int primID = spirv::PrimitiveId;
8286
const int instanceCustomIndex = spirv::InstanceCustomIndexKHR;
8387
const int geometryIndex = spirv::RayGeometryIndexKHR;
84-
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo), 8);
88+
const static uint64_t STriangleGeomInfoAlignment = nbl::hlsl::alignment_of_v<STriangleGeomInfo>;
89+
const STriangleGeomInfo geom = vk::BufferPointer<STriangleGeomInfo, STriangleGeomInfoAlignment>(pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo)).Get();
8590
const float32_t3 vertexNormal = calculateNormals(primID, geom, attribs.barycentrics);
8691
const float32_t3 worldNormal = normalize(mul(vertexNormal, transpose(spirv::WorldToObjectKHR)).xyz);
8792

71_RayTracingPipeline/app_resources/raytrace.rgen.hlsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,16 @@ void main()
7979

8080
Material material;
8181
MaterialId materialId = payload.materialId;
82+
const static uint64_t MaterialPackedAlignment = nbl::hlsl::alignment_of_v<MaterialPacked>;
8283
// we use negative index to indicate that this is a procedural geometry
8384
if (materialId.isHitProceduralGeom())
8485
{
85-
const MaterialPacked materialPacked = vk::RawBufferLoad<MaterialPacked>(pc.proceduralGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(SProceduralGeomInfo));
86+
const MaterialPacked materialPacked = vk::BufferPointer<MaterialPacked, MaterialPackedAlignment>(pc.proceduralGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(SProceduralGeomInfo)).Get();
8687
material = nbl::hlsl::_static_cast<Material>(materialPacked);
8788
}
8889
else
8990
{
90-
const MaterialPacked materialPacked = vk::RawBufferLoad<MaterialPacked>(pc.triangleGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(STriangleGeomInfo));
91+
const MaterialPacked materialPacked = vk::BufferPointer<MaterialPacked, MaterialPackedAlignment>(pc.triangleGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(STriangleGeomInfo)).Get();
9192
material = nbl::hlsl::_static_cast<Material>(materialPacked);
9293
}
9394

71_RayTracingPipeline/app_resources/raytrace.rint.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ void main()
3636

3737
const int primID = spirv::PrimitiveId;
3838

39+
const static uint64_t SProceduralGeomInfoAlignment = nbl::hlsl::alignment_of_v<STriangleGeomInfo>;
3940
// Sphere data
40-
SProceduralGeomInfo sphere = vk::RawBufferLoad<SProceduralGeomInfo>(pc.proceduralGeomInfoBuffer + primID * sizeof(SProceduralGeomInfo));
41+
SProceduralGeomInfo sphere = vk::BufferPointer<SProceduralGeomInfo, SProceduralGeomInfoAlignment>(pc.proceduralGeomInfoBuffer + primID * sizeof(SProceduralGeomInfo)).Get();
4142

4243
const float32_t tHit = hitSphere(sphere, ray);
4344

71_RayTracingPipeline/app_resources/raytrace_shadow.rahit.hlsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "common.hlsl"
22
#include "nbl/builtin/hlsl/spirv_intrinsics/raytracing.hlsl"
33
#include "nbl/builtin/hlsl/spirv_intrinsics/core.hlsl"
4+
#include "nbl/builtin/hlsl/type_traits.hlsl"
45

56
using namespace nbl::hlsl;
67

@@ -10,7 +11,8 @@ using namespace nbl::hlsl;
1011
void main(inout OcclusionPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
1112
{
1213
const int instID = spirv::InstanceCustomIndexKHR;
13-
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo), 8);
14+
const static uint64_t STriangleGeomInfoAlignment = nbl::hlsl::alignment_of_v<STriangleGeomInfo>;
15+
const STriangleGeomInfo geom = vk::BufferPointer<STriangleGeomInfo, STriangleGeomInfoAlignment>(pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo)).Get();
1416
const Material material = nbl::hlsl::_static_cast<Material>(geom.material);
1517

1618
const float attenuation = (1.f-material.alpha) * payload.attenuation;

0 commit comments

Comments
 (0)