|
| 1 | +# Nabla HLSL Builtin Library |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Shared shader code for Nabla. Many headers compile both as HLSL on the GPU and |
| 6 | +as C++ on the host. The two paths are split by the `__HLSL_VERSION` macro: the |
| 7 | +host branch often forwards to the C++ standard library, while the device branch |
| 8 | +provides the GPU equivalent, often backed by SPIR-V intrinsics. `bit.hlsl` is a |
| 9 | +small example of both branches in one file. |
| 10 | + |
| 11 | +All code lives in the `nbl::hlsl` namespace. |
| 12 | + |
| 13 | +## Single-source HLSL/C++ |
| 14 | + |
| 15 | +This folder is Nabla's shader-side support library: reusable HLSL headers for |
| 16 | +math, types, intrinsics, algorithms, compatibility helpers, and graphics code. |
| 17 | +The same header can be compiled by the shader compiler for GPU use and by the |
| 18 | +C++ compiler for host-side tests, tools, and shared data definitions. |
| 19 | + |
| 20 | +The compatibility layer is what makes that possible. `cpp_compat.hlsl` and the |
| 21 | +`cpp_compat/` folder provide the C++ side of HLSL types and intrinsics, while |
| 22 | +other headers use `__HLSL_VERSION` to choose between host implementations |
| 23 | +(`std`, C++ helpers, or test code) and device implementations (HLSL builtins, |
| 24 | +GLSL-style compatibility wrappers, or SPIR-V intrinsics). |
| 25 | + |
| 26 | +This lets Nabla write shader abstractions in HLSL while keeping them close |
| 27 | +enough to C++ to share structures, validate behavior on the host, and build |
| 28 | +shader utilities similar to parts of the C++ standard library. |
| 29 | + |
| 30 | +For more context, see the DevSH presentations: |
| 31 | + |
| 32 | +- https://www.youtube.com/watch?v=JCJ35dlZJb4 |
| 33 | +- https://www.devsh.eu/presentations |
| 34 | + |
| 35 | +## Placement rules |
| 36 | + |
| 37 | +Root files that share names with C++ standard library headers are for the |
| 38 | +matching `std::` counterpart only. For example, `bit.hlsl` is the `<bit>` |
| 39 | +counterpart and should stay limited to things like `bit_cast`, `rotl`, `rotr`, |
| 40 | +and `countl_zero`. |
| 41 | + |
| 42 | +Code beyond the standard library goes in a subfolder, even if it is related to a |
| 43 | +root file. The bitfield abstraction is not part of `<bit>`, so it belongs in |
| 44 | +`utils/bitfield.hlsl`, not in `bit.hlsl`. |
| 45 | + |
| 46 | +Prefer folder names that match their namespaces, but do not force it when the |
| 47 | +result reads badly. `utils/bitfield.hlsl` is better than |
| 48 | +`bitfield/bitfield.hlsl` if the second form pushes toward |
| 49 | +`hlsl::bitfield::bitfield`. |
| 50 | + |
| 51 | +## How to extend it |
| 52 | + |
| 53 | +1. If it mirrors a C++ standard library header, put it in the matching root |
| 54 | + file. |
| 55 | +2. If it is a Nabla extension or helper, put it in a subfolder. |
| 56 | +3. Reuse an existing folder when it fits. Add a new one only when needed. |
| 57 | + |
| 58 | +## Structure |
| 59 | + |
| 60 | +### Root files |
| 61 | + |
| 62 | +The "std" column marks files that are the GPU counterpart of a C++ standard |
| 63 | +library header. Those hold only what the standard header provides. |
| 64 | + |
| 65 | +| File | std | Contents and what belongs here | |
| 66 | +| --- | --- | --- | |
| 67 | +| `macros.h` | | Core preprocessor macros, including the `static_assert`/`assert` shims for HLSL. Cross-compilation macros go here. | |
| 68 | +| `cpp_compat.hlsl` | | Umbrella include for the C++ compatibility layer (pulls in `cpp_compat/`). | |
| 69 | +| `algorithm.hlsl` | `<algorithm>` | `swap` and other standard algorithms. | |
| 70 | +| `bit.hlsl` | `<bit>` | `bit_cast`, `rotl`, `rotr`, `countl_zero`. | |
| 71 | +| `complex.hlsl` | `<complex>` | `complex_t`. | |
| 72 | +| `concepts.hlsl` | `<concepts>` | Entry point for the concepts library (includes `concepts/`). | |
| 73 | +| `functional.hlsl` | `<functional>` | `reference_wrapper` and function objects. | |
| 74 | +| `limits.hlsl` | `<limits>` | `numeric_limits`. | |
| 75 | +| `memory.hlsl` | `<memory>` | Pointer and reference helpers, e.g. `pointer_to` for BDA refs. | |
| 76 | +| `numbers.hlsl` | `<numbers>` | Math constants (`e`, `pi`, ...). | |
| 77 | +| `tuple.hlsl` | `<tuple>` | `tuple`. | |
| 78 | +| `type_traits.hlsl` | `<type_traits>` | Type trait structs and aliases. | |
| 79 | +| `utility.hlsl` | `<utility>` | Standard utilities (currently `declval`). | |
| 80 | +| `tgmath.hlsl` | `<tgmath>`/`<cmath>` | Type-generic math functions. | |
| 81 | +| `ieee754.hlsl` | | IEEE-754 float layout traits and bit helpers. | |
| 82 | +| `mpl.hlsl` | | Template metaprogramming helpers (compile-time, boost::mpl style). | |
| 83 | +| `enums.hlsl` | | Engine enums shared host and device (e.g. `ShaderStage`). | |
| 84 | +| `format.hlsl` | | Texel block format enum and format pack/unpack (includes `format/`). | |
| 85 | +| `colorspace.hlsl` | | Colorspace conversions entry point (includes `colorspace/`). | |
| 86 | +| `morton.hlsl` | | Morton / Z-order code encode and decode. | |
| 87 | +| `acceleration_structures.hlsl` | | Ray tracing acceleration structure build structs. | |
| 88 | +| `indirect_commands.hlsl` | | Indirect draw and dispatch command structs. | |
| 89 | +| `binding_info.hlsl` | | Compile-time descriptor binding info structs. | |
| 90 | +| `device_capabilities_traits.hlsl` | | Traits to query device capabilities at compile time. | |
| 91 | +| `array_accessors.hlsl` | | Generic array `get`/`set` accessor structs. | |
| 92 | +| `memory_accessor.hlsl` | | Accessor wrappers over memory with atomic and barrier method detection. | |
| 93 | +| `member_test_macros.hlsl` | | Macros to detect presence of struct members and methods. | |
| 94 | +| `ndarray_addressing.hlsl` | | Multi-dimensional to linear index addressing. | |
| 95 | +| `scanning_append.hlsl` | | Result types for the scan-and-append primitive. | |
| 96 | +| `surface_transform.h` | | Swapchain surface transform flags and helpers. | |
| 97 | + |
| 98 | +### Subfolders |
| 99 | + |
| 100 | +| Folder | Contents and what belongs here | |
| 101 | +| --- | --- | |
| 102 | +| `barycentric/` | Barycentric coordinate utilities. | |
| 103 | +| `bda/` | Buffer Device Address: typed pointers, references, and accessors. | |
| 104 | +| `blit/` | Image blit and normalization compute shaders and their parameters. | |
| 105 | +| `bxdf/` | BxDF models: fresnel, NDF, reflection, transmission. | |
| 106 | +| `colorspace/` | Transfer functions (EOTF/OETF) and CIEXYZ encode/decode. | |
| 107 | +| `concepts/` | Concept definitions (core, vector, matrix, accessors). | |
| 108 | +| `cpp_compat/` | The C++/HLSL compatibility layer: vector, matrix, intrinsics, promote, truncate. | |
| 109 | +| `emulated/` | Software-emulated types (`float64_t`, `int64_t`, emulated vector/matrix) for platforms without native support. | |
| 110 | +| `ext/` | Helpers tied to engine extensions (e.g. FullScreenTriangle). | |
| 111 | +| `fft/` | FFT building blocks. See [`fft/README.md`](fft/README.md). | |
| 112 | +| `format/` | Pixel/texel format pack and unpack (octahedral, shared exponent). | |
| 113 | +| `glsl_compat/` | GLSL builtin equivalents (core, subgroup ops). | |
| 114 | +| `ieee754/` | IEEE-754 implementation details. | |
| 115 | +| `ies/` | IES light profile sampling and textures. | |
| 116 | +| `math/` | Math routines: geometry, linalg, quaternions, equations, quadrature, and more. | |
| 117 | +| `matrix_utils/` | Matrix traits, compile-time and runtime. | |
| 118 | +| `path_tracing/` | Path tracing building blocks: ray gen, accumulators, integrators. | |
| 119 | +| `portable/` | Type aliases that pick native or emulated types per platform. | |
| 120 | +| `prefix_sum_blur/` | Prefix-sum based blur. | |
| 121 | +| `random/` | RNGs (lcg, pcg, tea, xoroshiro) and adaptors. | |
| 122 | +| `rwmc/` | Reweighted Monte Carlo: cascade accumulator, resolve, splatting. | |
| 123 | +| `sampling/` | Sampling distributions and warps (alias table, spherical shapes, mappings). | |
| 124 | +| `scan/` | Device-wide scan (prefix sum) primitives and schedulers. | |
| 125 | +| `shapes/` | Geometric primitives (aabb, obb, line, triangle, beziers, ...). | |
| 126 | +| `sort/` | Sorting primitives (counting sort). | |
| 127 | +| `spirv_intrinsics/` | Raw SPIR-V intrinsic declarations. | |
| 128 | +| `subgroup/` | Subgroup-level collectives (ballot, arithmetic, basic, fft). | |
| 129 | +| `subgroup2/` | Newer subgroup collective API. | |
| 130 | +| `testing/` | Comparison helpers for tests (approx compare, max error). | |
| 131 | +| `text_rendering/` | Text rendering (MSDF). | |
| 132 | +| `tgmath/` | `tgmath` implementation details (isnan, output structs). | |
| 133 | +| `vector_utils/` | Vector traits. | |
| 134 | +| `visualization/` | Visualization helpers (turbo colormap). | |
| 135 | +| `workgroup/` | Workgroup-level collectives (arithmetic, ballot, scan, shuffle, fft). | |
| 136 | +| `workgroup2/` | Newer workgroup collective API. | |
0 commit comments