|
1 | 1 | #include "common.hlsl"
|
2 | 2 |
|
| 3 | +#include "nbl/builtin/hlsl/bda/__ptr.hlsl" |
| 4 | + |
3 | 5 | [[vk::push_constant]] SPushConstants pc;
|
4 | 6 |
|
5 |
| -float32_t3 fetchVertexNormal(int instID, int primID, STriangleGeomInfo geom, float2 bary) |
| 7 | +float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary) |
6 | 8 | {
|
7 |
| - uint idxOffset = primID * 3; |
8 |
| - |
9 | 9 | const uint indexType = geom.indexType;
|
10 |
| - const uint vertexStride = geom.vertexStride; |
11 |
| - |
12 |
| - const uint32_t objType = geom.objType; |
| 10 | + const uint normalType = geom.normalType; |
| 11 | + |
| 12 | + const uint64_t vertexBufferAddress = geom.vertexBufferAddress; |
13 | 13 | const uint64_t indexBufferAddress = geom.indexBufferAddress;
|
14 |
| - |
15 |
| - uint i0, i1, i2; |
16 |
| - switch (indexType) |
| 14 | + const uint64_t normalBufferAddress = geom.normalBufferAddress; |
| 15 | + |
| 16 | + uint32_t3 indices; |
| 17 | + if (indexBufferAddress == 0) |
17 | 18 | {
|
18 |
| - case 0: // EIT_16BIT |
19 |
| - { |
20 |
| - i0 = uint32_t(vk::RawBufferLoad < uint16_t > (indexBufferAddress + (idxOffset + 0) * sizeof(uint16_t), 2u)); |
21 |
| - i1 = uint32_t(vk::RawBufferLoad < uint16_t > (indexBufferAddress + (idxOffset + 1) * sizeof(uint16_t), 2u)); |
22 |
| - i2 = uint32_t(vk::RawBufferLoad < uint16_t > (indexBufferAddress + (idxOffset + 2) * sizeof(uint16_t), 2u)); |
23 |
| - } |
24 |
| - break; |
25 |
| - case 1: // EIT_32BIT |
26 |
| - { |
27 |
| - i0 = vk::RawBufferLoad < uint32_t > (indexBufferAddress + (idxOffset + 0) * sizeof(uint32_t)); |
28 |
| - i1 = vk::RawBufferLoad < uint32_t > (indexBufferAddress + (idxOffset + 1) * sizeof(uint32_t)); |
29 |
| - i2 = vk::RawBufferLoad < uint32_t > (indexBufferAddress + (idxOffset + 2) * sizeof(uint32_t)); |
30 |
| - } |
31 |
| - break; |
32 |
| - default: // EIT_NONE |
| 19 | + indices[0] = primID * 3; |
| 20 | + indices[1] = indices[0] + 1; |
| 21 | + indices[2] = indices[0] + 2; |
| 22 | + } |
| 23 | + else { |
| 24 | + switch (indexType) |
33 | 25 | {
|
34 |
| - i0 = idxOffset; |
35 |
| - i1 = idxOffset + 1; |
36 |
| - i2 = idxOffset + 2; |
| 26 | + case 0: // EIT_16BIT |
| 27 | + indices = uint32_t3((nbl::hlsl::bda::__ptr<uint16_t3>::create(indexBufferAddress)+primID).deref().load()); |
| 28 | + break; |
| 29 | + case 1: // EIT_32BIT |
| 30 | + indices = uint32_t3((nbl::hlsl::bda::__ptr<uint32_t3>::create(indexBufferAddress)+primID).deref().load()); |
| 31 | + break; |
37 | 32 | }
|
38 | 33 | }
|
39 | 34 |
|
40 |
| - const uint64_t normalVertexBufferAddress = geom.normalBufferAddress; |
41 |
| - float3 n0, n1, n2; |
| 35 | + if (normalBufferAddress == 0 || normalType == NT_UNKNOWN) |
| 36 | + { |
| 37 | + float3 v0 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * 12); |
| 38 | + float3 v1 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * 12); |
| 39 | + float3 v2 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * 12); |
| 40 | + |
| 41 | + return normalize(cross(v2 - v0, v1 - v0)); |
| 42 | + } |
42 | 43 |
|
43 | 44 | float3 n0, n1, n2;
|
44 |
| - switch (objType) |
| 45 | + switch (normalType) |
45 | 46 | {
|
46 |
| - case OT_CUBE: |
47 |
| - case OT_SPHERE: |
48 |
| - case OT_RECTANGLE: |
49 |
| - case OT_CYLINDER: |
50 |
| - //case OT_ARROW: |
51 |
| - case OT_CONE: |
| 47 | + case NT_R8G8B8A8_SNORM: |
52 | 48 | {
|
53 |
| - // TODO: document why the alignment is 2 here and nowhere else? isnt the `vertexStride` aligned to more than 2 anyway? |
54 |
| - uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + i0 * 4); |
55 |
| - uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + i1 * 4); |
56 |
| - uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + i2 * 4); |
| 49 | + uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4); |
| 50 | + uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4); |
| 51 | + uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[2] * 4); |
57 | 52 |
|
58 | 53 | n0 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v0).xyz);
|
59 | 54 | n1 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v1).xyz);
|
60 | 55 | n2 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v2).xyz);
|
61 | 56 | }
|
62 | 57 | break;
|
63 |
| - case OT_ICOSPHERE: |
64 |
| - default: |
| 58 | + case NT_R32G32B32_SFLOAT: |
65 | 59 | {
|
66 |
| - n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + i0 * 12)); |
67 |
| - n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + i1 * 12)); |
68 |
| - n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + i2 * 12)); |
| 60 | + n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12)); |
| 61 | + n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12)); |
| 62 | + n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12)); |
69 | 63 | }
|
| 64 | + break; |
70 | 65 | }
|
71 | 66 |
|
72 | 67 | float3 barycentrics = float3(0.0, bary);
|
73 |
| - barycentrics.x = 1.0 - barycentrics.y - barycentrics.z; |
74 |
| - return normalize(barycentrics.x * n0 + barycentrics.y * n1 + barycentrics.z * n2); |
| 68 | + barycentrics.x = 1.0 - barycentrics.y - barycentrics.z; |
| 69 | + |
| 70 | + return barycentrics.x * n0 + barycentrics.y * n1 + barycentrics.z * n2; |
75 | 71 | }
|
76 | 72 |
|
| 73 | + |
77 | 74 | [shader("closesthit")]
|
78 | 75 | void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
|
79 | 76 | {
|
80 |
| - const int instID = InstanceID(); |
81 | 77 | const int primID = PrimitiveIndex();
|
82 |
| - const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo)); |
83 |
| - const float32_t3 vertexNormal = fetchVertexNormal(instID, primID, geom, attribs.barycentrics); |
| 78 | + const int instanceCustomIndex = InstanceIndex(); |
| 79 | + const int geometryIndex = GeometryIndex(); |
| 80 | + const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo)); |
| 81 | + const float32_t3 vertexNormal = calculateNormals(primID, geom, attribs.barycentrics); |
84 | 82 | const float32_t3 worldNormal = normalize(mul(vertexNormal, WorldToObject3x4()).xyz);
|
85 | 83 |
|
86 |
| - payload.materialId = MaterialId::createTriangle(instID); |
| 84 | + payload.materialId = MaterialId::createTriangle(instanceCustomIndex); |
87 | 85 |
|
88 | 86 | payload.worldNormal = worldNormal;
|
89 | 87 | payload.rayDistance = RayTCurrent();
|
|
0 commit comments