Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-render

Source of release 0.1.3

0.1.3 / tools / sobol_table.py

#!/usr/bin/env python3
"""Write shaders/sobol_table.glsl: direction numbers of the first four Sobol
dimensions (Joe and Kuo's new-joe-kuo-6.21201 parameters), 32 bits each, for
Burley's Owen-scrambled Sobol sampler in shaders/sampler.glsl.

    python3 tools/sobol_table.py
"""
from pathlib import Path

# (s, a, m) per dimension after the first (van der Corput).
PARAMETERS = [(1, 0, [1]), (2, 1, [1, 3]), (3, 1, [1, 3, 1])]

def directions(s, a, m):
    v = [0] * 33
    for k in range(1, 33):
        if k <= s:
            v[k] = m[k - 1] << (32 - k)
        else:
            value = v[k - s] ^ (v[k - s] >> s)
            for i in range(1, s):
                if (a >> (s - 1 - i)) & 1:
                    value ^= v[k - i]
            v[k] = value
    return [x & 0xffffffff for x in v[1:]]

rows = [[1 << (31 - bit) for bit in range(32)]] + [directions(*p) for p in PARAMETERS]
lines = ['// Generated by tools/sobol_table.py; do not edit.',
         '// Direction numbers of Sobol dimensions 0..3, bit b of the index at [d * 32 + b].',
         'const uint sobol_directions[128] = uint[128](']
body = []
for row in rows:
    body.append('    ' + ', '.join(f'0x{x:08x}u' for x in row))
lines.append(',\n'.join(body))
lines.append(');')
(Path(__file__).resolve().parent.parent / 'shaders' / 'sobol_table.glsl').write_text('\n'.join(lines) + '\n')