@@ -25,69 +25,64 @@ float3 unpackNormals3x10(uint32_t v)
25
25
return clamp (float3 (pn) / 511.0 , -1.0 , 1.0 );
26
26
}
27
27
28
- float3 calculateSmoothNormals ( int instID, int primID, SGeomInfo geom, float2 bary)
28
+ float3 calculateNormals ( int primID, SGeomInfo geom, float2 bary)
29
29
{
30
30
const uint indexType = geom.indexType;
31
- const uint vertexStride = geom.vertexStride ;
31
+ const uint normalType = geom.normalType ;
32
32
33
33
const uint64_t vertexBufferAddress = geom.vertexBufferAddress;
34
34
const uint64_t indexBufferAddress = geom.indexBufferAddress;
35
+ const uint64_t normalBufferAddress = geom.normalBufferAddress;
35
36
36
37
uint32_t3 indices;
37
- switch (indexType )
38
+ if (indexBufferAddress == 0 )
38
39
{
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)
46
46
{
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 ;
50
53
}
51
54
}
52
55
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
+
53
65
float3 n0, n1, n2;
54
- switch (instID )
66
+ switch (normalType )
55
67
{
56
- case OT_CUBE :
68
+ case NT_R8G8B8A8_SNORM :
57
69
{
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 );
62
73
63
74
n0 = normalize (nbl::hlsl::spirv::unpackSnorm4x8 (v0).xyz);
64
75
n1 = normalize (nbl::hlsl::spirv::unpackSnorm4x8 (v1).xyz);
65
76
n2 = normalize (nbl::hlsl::spirv::unpackSnorm4x8 (v2).xyz);
66
77
}
67
78
break ;
68
- case OT_SPHERE:
69
- case OT_CYLINDER:
70
- case OT_ARROW:
71
- case OT_CONE:
79
+ case NT_R32G32B32_SFLOAT:
72
80
{
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 ));
80
84
}
81
85
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
- }
91
86
}
92
87
93
88
float3 barycentrics = float3 (0.0 , bary);
@@ -124,15 +119,16 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
124
119
125
120
if (spirv::rayQueryGetIntersectionTypeKHR (query, true ) == spv::RayQueryCommittedIntersectionTypeRayQueryCommittedIntersectionTriangleKHR)
126
121
{
127
- const int instID = spirv::rayQueryGetIntersectionInstanceIdKHR (query, true );
122
+ const int instanceCustomIndex = spirv::rayQueryGetIntersectionInstanceCustomIndexKHR (query, true );
123
+ const int geometryIndex = spirv::rayQueryGetIntersectionGeometryIndexKHR (query, true );
128
124
const int primID = spirv::rayQueryGetIntersectionPrimitiveIndexKHR (query, true );
129
125
130
126
// 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 );
132
128
133
129
float3 normals;
134
130
float2 barycentrics = spirv::rayQueryGetIntersectionBarycentricsKHR (query, true );
135
- normals = calculateSmoothNormals (instID, primID, geom, barycentrics);
131
+ normals = calculateNormals ( primID, geom, barycentrics);
136
132
137
133
normals = normalize (normals) * 0.5 + 0.5 ;
138
134
color = float4 (normals, 1.0 );
0 commit comments