Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions 71_RayTracingPipeline/app_resources/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
#include "nbl/builtin/hlsl/cpp_compat/basic.h"
#include "nbl/builtin/hlsl/random/pcg.hlsl"
#include "nbl/builtin/hlsl/type_traits.hlsl"

NBL_CONSTEXPR uint32_t WorkgroupSize = 16;
NBL_CONSTEXPR uint32_t MAX_UNORM_10 = 1023;
Expand Down Expand Up @@ -78,6 +79,9 @@ struct MaterialPacked
return (xi>>22) > alpha;
}
};
#ifdef __HLSL_VERSION
NBL_REGISTER_OBJ_TYPE(MaterialPacked, 4)
#endif
Comment on lines +82 to +84

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm we could just make NBL_REGISTER_OBJ_TYPE a NOOP for !__HLSL_VERSION

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we don't have to spam this ifdef everywhere


struct SProceduralGeomInfo
{
Expand All @@ -103,6 +107,9 @@ struct STriangleGeomInfo
uint32_t indexType : 1; // 16 bit, 32 bit

};
#ifdef __HLSL_VERSION
NBL_REGISTER_OBJ_TYPE(STriangleGeomInfo, 8)
#endif

enum E_GEOM_TYPE : uint16_t
{
Expand Down
3 changes: 2 additions & 1 deletion 71_RayTracingPipeline/app_resources/raytrace.rahit.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using namespace nbl::hlsl;
void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
{
const int instID = spirv::InstanceCustomIndexKHR;
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo), 8);
const static uint64_t STriangleGeomInfoAlignment = nbl::hlsl::alignment_of_v<STriangleGeomInfo>;
const STriangleGeomInfo geom = vk::BufferPointer<STriangleGeomInfo, STriangleGeomInfoAlignment>(pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo)).Get();

const uint32_t bitpattern = payload.pcg();
// Cannot use spirv::ignoreIntersectionKHR and spirv::terminateRayKHR due to https://github.com/microsoft/DirectXShaderCompiler/issues/7279
Expand Down
25 changes: 15 additions & 10 deletions 71_RayTracingPipeline/app_resources/raytrace.rchit.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)

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

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

n0 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v0).xyz);
n1 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v1).xyz);
Expand All @@ -61,9 +61,13 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
break;
case NT_R32G32B32_SFLOAT:
{
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12, 8));
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12, 8));
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12, 8));
float3 v0 = (nbl::hlsl::bda::__ptr<float3>::create(normalBufferAddress) + indices[0]).deref().load();
float3 v1 = (nbl::hlsl::bda::__ptr<float3>::create(normalBufferAddress) + indices[1]).deref().load();
float3 v2 = (nbl::hlsl::bda::__ptr<float3>::create(normalBufferAddress) + indices[2]).deref().load();

n0 = normalize(v0);
n1 = normalize(v1);
n2 = normalize(v2);
}
break;
}
Expand All @@ -81,7 +85,8 @@ void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes
const int primID = spirv::PrimitiveId;
const int instanceCustomIndex = spirv::InstanceCustomIndexKHR;
const int geometryIndex = spirv::RayGeometryIndexKHR;
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo), 8);
const static uint64_t STriangleGeomInfoAlignment = nbl::hlsl::alignment_of_v<STriangleGeomInfo>;
const STriangleGeomInfo geom = vk::BufferPointer<STriangleGeomInfo, STriangleGeomInfoAlignment>(pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo)).Get();
const float32_t3 vertexNormal = calculateNormals(primID, geom, attribs.barycentrics);
const float32_t3 worldNormal = normalize(mul(vertexNormal, transpose(spirv::WorldToObjectKHR)).xyz);

Expand Down
5 changes: 3 additions & 2 deletions 71_RayTracingPipeline/app_resources/raytrace.rgen.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ void main()

Material material;
MaterialId materialId = payload.materialId;
const static uint64_t MaterialPackedAlignment = nbl::hlsl::alignment_of_v<MaterialPacked>;
// we use negative index to indicate that this is a procedural geometry
if (materialId.isHitProceduralGeom())
{
const MaterialPacked materialPacked = vk::RawBufferLoad<MaterialPacked>(pc.proceduralGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(SProceduralGeomInfo));
const MaterialPacked materialPacked = vk::BufferPointer<MaterialPacked, MaterialPackedAlignment>(pc.proceduralGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(SProceduralGeomInfo)).Get();
material = nbl::hlsl::_static_cast<Material>(materialPacked);
}
else
{
const MaterialPacked materialPacked = vk::RawBufferLoad<MaterialPacked>(pc.triangleGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(STriangleGeomInfo));
const MaterialPacked materialPacked = vk::BufferPointer<MaterialPacked, MaterialPackedAlignment>(pc.triangleGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(STriangleGeomInfo)).Get();
material = nbl::hlsl::_static_cast<Material>(materialPacked);
}

Expand Down
3 changes: 2 additions & 1 deletion 71_RayTracingPipeline/app_resources/raytrace.rint.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ void main()

const int primID = spirv::PrimitiveId;

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

const float32_t tHit = hitSphere(sphere, ray);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common.hlsl"
#include "nbl/builtin/hlsl/spirv_intrinsics/raytracing.hlsl"
#include "nbl/builtin/hlsl/spirv_intrinsics/core.hlsl"
#include "nbl/builtin/hlsl/type_traits.hlsl"

using namespace nbl::hlsl;

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

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