Skip to content

Commit e135db2

Browse files
committed
Added Sobol matrices as builtins
1 parent 0571f7e commit e135db2

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (C) 2018-2026 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_BUILTIN_HLSL_SAMPLING_SOBOL_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_SAMPLING_SOBOL_INCLUDED_
6+
7+
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
8+
#include <nbl/builtin/hlsl/macros.h>
9+
10+
namespace nbl
11+
{
12+
namespace hlsl
13+
{
14+
namespace sampling
15+
{
16+
17+
// without acceleration
18+
struct RowMajorSobolMatrix
19+
{
20+
inline uint16_t operator()(const uint16_t sampleIx)
21+
{
22+
// max number bits set is 16, if want to mask then need to be 5 apart
23+
const uint16_t mask = 0b10000100001u;
24+
uint16_t val = _static_cast<uint16_t>(spirv::bitCount<uint32_t>(rows[0] & sampleIx));
25+
val |= _static_cast<uint16_t>(spirv::bitCount<uint32_t>(rows[5] & sampleIx)) << 5;
26+
val |= _static_cast<uint16_t>(spirv::bitCount<uint32_t>(rows[10] & sampleIx)) << 10;
27+
val |= _static_cast<uint16_t>(spirv::bitCount<uint32_t>(rows[15] & sampleIx)) << 15;
28+
val &= mask;
29+
NBL_UNROLL
30+
for (uint16_t i = 1; i < 5; i++)
31+
{
32+
uint16_t tmp = _static_cast<uint16_t>(spirv::bitCount<uint32_t>(rows[i] & sampleIx));
33+
tmp |= _static_cast<uint16_t>(spirv::bitCount<uint32_t>(rows[5 + i] & sampleIx)) << 5;
34+
tmp |= _static_cast<uint16_t>(spirv::bitCount<uint32_t>(rows[10 + i] & sampleIx)) << 10;
35+
val |= (tmp & mask) << i;
36+
}
37+
return val;
38+
}
39+
40+
uint16_t rows[16];
41+
};
42+
43+
// bitcount is expensive, its full 32 bit and needs shifting and masking anyway
44+
struct ColMajorSobolMatrix
45+
{
46+
inline uint16_t operator()(const uint16_t sampleIx)
47+
{
48+
// abuse 2s completement to get a 0xffffu mask when `sampleIx` bit is on
49+
uint16_t val = cols[0] & ((sampleIx & _static_cast<uint16_t>(0x1u)) - _static_cast<uint16_t>(2));
50+
NBL_UNROLL
51+
for (uint16_t i = 1; i < 16; i++)
52+
{
53+
const uint16_t mask = _static_cast<uint16_t>(0x1u) << i;
54+
val |= cols[i] & ((sampleIx & mask) - _static_cast<uint16_t>(mask + 1));
55+
}
56+
return val;
57+
}
58+
59+
uint16_t cols[16];
60+
};
61+
62+
} // namespace sampling
63+
} // namespace hlsl
64+
} // namespace nbl
65+
66+
#endif

src/nbl/builtin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/concepts.hlsl")
290290
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/uniform_spheres.hlsl")
291291
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/alias_table.hlsl")
292292
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/cumulative_probability.hlsl")
293+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/sobol.hlsl")
293294
#
294295
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/ndarray_addressing.hlsl")
295296
#

0 commit comments

Comments
 (0)