Skip to content

Commit ec6bdab

Browse files
author
devsh
committed
test the bda::__ptr and __ref in some examples
turns out in many we can't use cause of unspecialized templates and bitfields.
1 parent c6a5b27 commit ec6bdab

File tree

7 files changed

+52
-45
lines changed

7 files changed

+52
-45
lines changed

11_FFT/app_resources/shader.comp.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct Accessor
4343
return accessor;
4444
}
4545

46+
// TODO: can't use our own BDA yet, because it doesn't support the types `workgroup::FFT` will invoke these templates with
4647
template <typename AccessType>
4748
void get(const uint32_t index, NBL_REF_ARG(AccessType) value)
4849
{

67_RayQueryGeometry/app_resources/common.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
NBL_CONSTEXPR uint32_t WorkgroupSize = 16;
77

8+
// we need bitfield support in NBL_HLSL_DECLARE_STRUCT it seems
89
struct SGeomInfo
910
{
1011
uint64_t vertexBufferAddress;

67_RayQueryGeometry/app_resources/render.comp.hlsl

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "nbl/builtin/hlsl/glsl_compat/core.hlsl"
66
#include "nbl/builtin/hlsl/spirv_intrinsics/raytracing.hlsl"
7+
#include "nbl/builtin/hlsl/bda/__ptr.hlsl"
78

89
using namespace nbl::hlsl;
910

@@ -24,36 +25,26 @@ float3 unpackNormals3x10(uint32_t v)
2425

2526
float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bary)
2627
{
27-
uint idxOffset = primID * 3;
28-
2928
const uint indexType = geom.indexType;
3029
const uint vertexStride = geom.vertexStride;
3130

3231
const uint64_t vertexBufferAddress = geom.vertexBufferAddress;
3332
const uint64_t indexBufferAddress = geom.indexBufferAddress;
3433

35-
uint i0, i1, i2;
34+
uint32_t3 indices;
3635
switch (indexType)
3736
{
3837
case 0: // EIT_16BIT
39-
{
40-
i0 = uint32_t(vk::RawBufferLoad<uint16_t>(indexBufferAddress + (idxOffset + 0) * sizeof(uint16_t), 2u));
41-
i1 = uint32_t(vk::RawBufferLoad<uint16_t>(indexBufferAddress + (idxOffset + 1) * sizeof(uint16_t), 2u));
42-
i2 = uint32_t(vk::RawBufferLoad<uint16_t>(indexBufferAddress + (idxOffset + 2) * sizeof(uint16_t), 2u));
43-
}
44-
break;
38+
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint16_t3>::create(indexBufferAddress)+primID).deref().load());
39+
break;
4540
case 1: // EIT_32BIT
46-
{
47-
i0 = vk::RawBufferLoad<uint32_t>(indexBufferAddress + (idxOffset + 0) * sizeof(uint32_t));
48-
i1 = vk::RawBufferLoad<uint32_t>(indexBufferAddress + (idxOffset + 1) * sizeof(uint32_t));
49-
i2 = vk::RawBufferLoad<uint32_t>(indexBufferAddress + (idxOffset + 2) * sizeof(uint32_t));
50-
}
51-
break;
41+
indices = uint32_t3((nbl::hlsl::bda::__ptr<uint32_t3>::create(indexBufferAddress)+primID).deref().load());
42+
break;
5243
default: // EIT_NONE
5344
{
54-
i0 = idxOffset;
55-
i1 = idxOffset + 1;
56-
i2 = idxOffset + 2;
45+
indices[0] = primID * 3;
46+
indices[1] = indices[0] + 1;
47+
indices[2] = indices[0] + 2;
5748
}
5849
}
5950

@@ -62,9 +53,10 @@ float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bar
6253
{
6354
case OT_CUBE:
6455
{
65-
uint32_t v0 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + i0 * vertexStride, 2u);
66-
uint32_t v1 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + i1 * vertexStride, 2u);
67-
uint32_t v2 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + i2 * vertexStride, 2u);
56+
// TODO: document why the alignment is 2 here and nowhere else? isnt the `vertexStride` aligned to more than 2 anyway?
57+
uint32_t v0 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[0] * vertexStride, 2u);
58+
uint32_t v1 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[1] * vertexStride, 2u);
59+
uint32_t v2 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[2] * vertexStride, 2u);
6860

6961
n0 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v0).xyz);
7062
n1 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v1).xyz);
@@ -76,9 +68,9 @@ float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bar
7668
case OT_ARROW:
7769
case OT_CONE:
7870
{
79-
uint32_t v0 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + i0 * vertexStride);
80-
uint32_t v1 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + i1 * vertexStride);
81-
uint32_t v2 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + i2 * vertexStride);
71+
uint32_t v0 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[0] * vertexStride);
72+
uint32_t v1 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[1] * vertexStride);
73+
uint32_t v2 = vk::RawBufferLoad<uint32_t>(vertexBufferAddress + indices[2] * vertexStride);
8274

8375
n0 = normalize(unpackNormals3x10(v0));
8476
n1 = normalize(unpackNormals3x10(v1));
@@ -90,9 +82,9 @@ float3 calculateSmoothNormals(int instID, int primID, SGeomInfo geom, float2 bar
9082
case OT_ICOSPHERE:
9183
default:
9284
{
93-
n0 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + i0 * vertexStride));
94-
n1 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + i1 * vertexStride));
95-
n2 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + i2 * vertexStride));
85+
n0 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * vertexStride));
86+
n1 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * vertexStride));
87+
n2 = normalize(vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * vertexStride));
9688
}
9789
}
9890

@@ -132,6 +124,7 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
132124
const int instID = spirv::rayQueryGetIntersectionInstanceIdKHR(query, true);
133125
const int primID = spirv::rayQueryGetIntersectionPrimitiveIndexKHR(query, true);
134126

127+
// TODO: candidate for `bda::__ptr<SGeomInfo>`
135128
const SGeomInfo geom = vk::RawBufferLoad<SGeomInfo>(pc.geometryInfoBuffer + instID * sizeof(SGeomInfo));
136129

137130
float3 normals;

70_FLIPFluids/app_resources/compute/advectParticles.comp.hlsl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@ cbuffer GridData
1616
SGridData gridData;
1717
};
1818

19+
1920
[[vk::binding(b_apVelField, s_ap)]] Texture3D<float> velocityField[3];
2021
[[vk::binding(b_apPrevVelField, s_ap)]] Texture3D<float> prevVelocityField[3];
2122
[[vk::binding(b_apVelSampler, s_ap)]] SamplerState velocityFieldSampler;
2223

23-
// TODO: delta time push constant? (but then for CI need a commandline `-fixed-timestep=MS` and `-frames=N` option too)
24+
#include "nbl/builtin/hlsl/bda/__ptr.hlsl"
25+
using namespace nbl::hlsl;
2426

27+
// TODO: delta time push constant? (but then for CI need a commandline `-fixed-timestep=MS` and `-frames=N` option too)
2528
[numthreads(WorkgroupSize, 1, 1)]
2629
void main(uint32_t3 ID : SV_DispatchThreadID)
2730
{
2831
uint32_t pid = ID.x;
2932
Particle p;
3033

31-
int offset = sizeof(float32_t3) * pid;
32-
p.position = vk::RawBufferLoad<float32_t3>(pc.particlePosAddress + offset);
33-
p.velocity = vk::RawBufferLoad<float32_t3>(pc.particleVelAddress + offset);
34+
// use a restrict reference for speed
35+
bda::__ref<float32_t3,4,true> rPosition = (bda::__ptr<float32_t3>::create(pc.particlePosAddress)+pid).deref<4,true>();
36+
bda::__ref<float32_t3,4,true> rVelocity = (bda::__ptr<float32_t3>::create(pc.particleVelAddress)+pid).deref<4,true>();
37+
p.position = rPosition.load();
38+
p.velocity = rVelocity.load();
3439

3540
// advect velocity
3641
float3 gridPrevVel = sampleVelocityAt(p.position, prevVelocityField, velocityFieldSampler, gridData);
@@ -52,6 +57,6 @@ void main(uint32_t3 ID : SV_DispatchThreadID)
5257

5358
p.position = clampPosition(p.position, gridData.worldMin, gridData.worldMax);
5459

55-
vk::RawBufferStore<float32_t3>(pc.particlePosAddress + offset, p.position);
56-
vk::RawBufferStore<float32_t3>(pc.particleVelAddress + offset, p.velocity);
60+
rPosition.store(p.position);
61+
rVelocity.store(p.velocity);
5762
}

70_FLIPFluids/app_resources/compute/genParticleVertices.comp.hlsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ static const float2 quadUVs[4] = {
5353
float2(1, 1)
5454
};
5555

56+
#include "nbl/builtin/hlsl/bda/__ptr.hlsl"
57+
using namespace nbl::hlsl;
58+
5659
[numthreads(WorkgroupSize, 1, 1)]
5760
void main(uint32_t3 ID : SV_DispatchThreadID)
5861
{
@@ -112,6 +115,6 @@ void main(uint32_t3 ID : SV_DispatchThreadID)
112115

113116
vertex.uv = quadUVs[vertexOrder[i]];
114117

115-
vk::RawBufferStore<VertexInfo>(pc.particleVerticesAddress + sizeof(VertexInfo) * (quadBeginIdx + i), vertex);
118+
(bda::__ptr<VertexInfo>::create(pc.particleVerticesAddress)+(quadBeginIdx+i)).deref_restrict().store(vertex);
116119
}
117120
}

70_FLIPFluids/app_resources/fluidParticles.vertex.hlsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ struct SPushConstants
1010

1111
[[vk::push_constant]] SPushConstants pc;
1212

13+
14+
#include "nbl/builtin/hlsl/bda/__ptr.hlsl"
15+
using namespace nbl::hlsl;
16+
1317
PSInput main(uint vertexID : SV_VertexID)
1418
{
1519
PSInput output;
1620

17-
VertexInfo vertex = vk::RawBufferLoad<VertexInfo>(pc.particleVerticesAddress + sizeof(VertexInfo) * vertexID);
21+
VertexInfo vertex = (bda::__ptr<VertexInfo>::create(pc.particleVerticesAddress)+vertexID).deref_restrict().load();
1822

1923
output.position = vertex.position;
2024
output.vsSpherePos = vertex.vsSpherePos.xyz;

70_FLIPFluids/app_resources/render_common.hlsl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef _FLIP_EXAMPLE_RENDER_COMMON_HLSL
22
#define _FLIP_EXAMPLE_RENDER_COMMON_HLSL
3+
#include "nbl/builtin/hlsl/bda/struct_declare.hlsl"
34

45
struct SParticleRenderParams
56
{
@@ -9,16 +10,15 @@ struct SParticleRenderParams
910
};
1011

1112
// TODO: This struct shouldn't exist if there's no "vertex generation" shader
12-
struct VertexInfo
13-
{
14-
// TODO: don't use 4D vectors for 3D quantities
15-
float32_t4 position;
16-
float32_t4 vsSpherePos;
17-
float32_t radius;
18-
19-
float32_t4 color;
20-
float32_t2 uv;
21-
};
13+
struct VertexInfo;
14+
// TODO: don't use 4D vectors for 3D quantities
15+
NBL_HLSL_DEFINE_STRUCT((VertexInfo),
16+
((position, float32_t4))
17+
((vsSpherePos, float32_t4))
18+
((radius, float32_t))
19+
((color, float32_t4))
20+
((uv, float32_t2))
21+
);
2222

2323
#ifdef __HLSL_VERSION
2424
struct PSInput

0 commit comments

Comments
 (0)