|
| 1 | +// Copyright (C) 2018-2025 - 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 | + |
| 5 | +#ifndef _NBL_BUILTIN_HLSL_SAMPLING_QUANTIZED_SEQUENCE_INCLUDED_ |
| 6 | +#define _NBL_BUILTIN_HLSL_SAMPLING_QUANTIZED_SEQUENCE_INCLUDED_ |
| 7 | + |
| 8 | +#include "nbl/builtin/hlsl/concepts/vector.hlsl" |
| 9 | +#include "nbl/builtin/hlsl/vector_utils/vector_traits.hlsl" |
| 10 | + |
| 11 | +namespace nbl |
| 12 | +{ |
| 13 | +namespace hlsl |
| 14 | +{ |
| 15 | +namespace sampling |
| 16 | +{ |
| 17 | + |
| 18 | +template<uint16_t BytesLog2, int32_t Dim NBL_STRUCT_CONSTRAINABLE> |
| 19 | +struct QuantizedSequence; |
| 20 | + |
| 21 | +// byteslog2 = 1,2; dim = 1 |
| 22 | +template<uint16_t BytesLog2> NBL_PARTIAL_REQ_TOP(BytesLog2 > 0 && BytesLog2 < 3) |
| 23 | +struct QuantizedSequence<BytesLog2, 1 NBL_PARTIAL_REQ_BOT(BytesLog2 > 0 && BytesLog2 < 3) > |
| 24 | +{ |
| 25 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_store_bytes = uint16_t(1u) << BytesLog2; |
| 26 | + using base_store_type = typename unsigned_integer_of_size<base_store_bytes>::type; |
| 27 | + |
| 28 | + base_store_type getX() { return data; } |
| 29 | + void setX(const base_store_type value) { data = value; } |
| 30 | + |
| 31 | + base_store_type data; |
| 32 | +}; |
| 33 | + |
| 34 | +// byteslog2 = 3,4; dim = 1 |
| 35 | +template<uint16_t BytesLog2> NBL_PARTIAL_REQ_TOP(BytesLog2 > 2 && BytesLog2 < 5) |
| 36 | +struct QuantizedSequence<BytesLog2, 1 NBL_PARTIAL_REQ_BOT(BytesLog2 > 2 && BytesLog2 < 5) > |
| 37 | +{ |
| 38 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_bytes_log2 = uint16_t(2u); |
| 39 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_store_bytes = uint16_t(1u) << base_bytes_log2; |
| 40 | + using base_store_type = typename unsigned_integer_of_size<base_store_bytes>::type; |
| 41 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t num_components = uint16_t(1u) << (BytesLog2 - base_bytes_log2); |
| 42 | + using store_type = vector<base_store_type, num_components>; |
| 43 | + |
| 44 | + store_type getX() { return data; } |
| 45 | + void setX(const store_type value) { data = value; } |
| 46 | + |
| 47 | + store_type data; |
| 48 | +}; |
| 49 | + |
| 50 | +// byteslog2 = 2,3; dim = 2 |
| 51 | +template<uint16_t BytesLog2> NBL_PARTIAL_REQ_TOP(BytesLog2 > 1 && BytesLog2 < 4) |
| 52 | +struct QuantizedSequence<BytesLog2, 2 NBL_PARTIAL_REQ_BOT(BytesLog2 > 2 && BytesLog2 < 5) > |
| 53 | +{ |
| 54 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_bytes_log2 = BytesLog2 - uint16_t(1u); |
| 55 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_store_bytes = uint16_t(1u) << base_bytes_log2; |
| 56 | + using base_store_type = typename unsigned_integer_of_size<base_store_bytes>::type; |
| 57 | + using store_type = vector<base_store_type, 2>; |
| 58 | + |
| 59 | + base_store_type getX() { return data[0]; } |
| 60 | + base_store_type getY() { return data[1]; } |
| 61 | + void setX(const base_store_type value) { data[0] = value; } |
| 62 | + void setY(const base_store_type value) { data[1] = value; } |
| 63 | + |
| 64 | + store_type data; |
| 65 | +}; |
| 66 | + |
| 67 | +// byteslog2 = 1; dim = 2,3,4 |
| 68 | +template<uint16_t Dim> NBL_PARTIAL_REQ_TOP(Dim > 1 && Dim < 5) |
| 69 | +struct QuantizedSequence<1, Dim NBL_PARTIAL_REQ_BOT(Dim > 1 && Dim < 5) > |
| 70 | +{ |
| 71 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_store_bytes = uint16_t(1u) << uint16_t(1u); |
| 72 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t store_bits = uint16_t(8u) * base_store_bytes; |
| 73 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t bits_per_component = store_bits / Dim; |
| 74 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t MASK = (uint16_t(1u) << bits_per_component) - uint16_t(1u); |
| 75 | + using base_store_type = uint16_t; |
| 76 | + |
| 77 | + base_store_type getX() { return data & MASK; } |
| 78 | + base_store_type getY() { return (data >> bits_per_component) & MASK; } |
| 79 | + template<typename C=bool_constant<2 < Dim> NBL_FUNC_REQUIRES(C::value && 2 < Dim) |
| 80 | + base_store_type getZ() { return (data >> (bits_per_component * uint16_t(2u))) & MASK; } |
| 81 | + template<typename C=bool_constant<3 < Dim> NBL_FUNC_REQUIRES(C::value && 3 < Dim) |
| 82 | + base_store_type getW() { return (data >> (bits_per_component * uint16_t(3u))) & MASK; } |
| 83 | + |
| 84 | + void setX(const base_store_type value) |
| 85 | + { |
| 86 | + data &= ~MASK; |
| 87 | + data |= value & MASK; |
| 88 | + } |
| 89 | + void setY(const base_store_type value) |
| 90 | + { |
| 91 | + const uint16_t mask = MASK << bits_per_component; |
| 92 | + data &= ~mask; |
| 93 | + data |= (value & MASK) << bits_per_component; |
| 94 | + } |
| 95 | + template<typename C=bool_constant<2 < Dim> NBL_FUNC_REQUIRES(C::value && 2 < Dim) |
| 96 | + void setZ(const base_store_type value) |
| 97 | + { |
| 98 | + const uint16_t bits = (bits_per_component * uint16_t(2u)); |
| 99 | + const uint16_t mask = MASK << bits; |
| 100 | + data &= ~mask; |
| 101 | + data |= (value & MASK) << bits; |
| 102 | + } |
| 103 | + template<typename C=bool_constant<3 < Dim> NBL_FUNC_REQUIRES(C::value && 3 < Dim) |
| 104 | + void setW(const base_store_type value) |
| 105 | + { |
| 106 | + const uint16_t bits = (bits_per_component * uint16_t(3u)); |
| 107 | + const uint16_t mask = MASK << bits; |
| 108 | + data &= ~mask; |
| 109 | + data |= (value & MASK) << bits; |
| 110 | + } |
| 111 | + |
| 112 | + base_store_type data; |
| 113 | +}; |
| 114 | + |
| 115 | +// byteslog2 = 2,3; dim = 3 |
| 116 | +template<uint16_t BytesLog2> NBL_PARTIAL_REQ_TOP(BytesLog2 > 1 && BytesLog2 < 4) |
| 117 | +struct QuantizedSequence<BytesLog2, 3 NBL_PARTIAL_REQ_BOT(BytesLog2 > 2 && BytesLog2 < 5) > |
| 118 | +{ |
| 119 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_bytes_log2 = BytesLog2 - uint16_t(1u); |
| 120 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t base_store_bytes = uint16_t(1u) << base_bytes_log2; |
| 121 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t store_bits = uint16_t(8u) * base_store_bytes; |
| 122 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t bits_per_component = store_bits / uint16_t(3u); |
| 123 | + NBL_CONSTEXPR_STATIC_INLINE uint16_t MASK = (uint16_t(1u) << bits_per_component) - uint16_t(1u); |
| 124 | + using base_store_type = typename unsigned_integer_of_size<base_store_bytes>::type; |
| 125 | + using store_type = vector<base_store_type, 2>; |
| 126 | + |
| 127 | + base_store_type getX() { return data[0] & MASK; } |
| 128 | + base_store_type getY() |
| 129 | + { |
| 130 | + base_store_type y = data[0] >> bits_per_component; |
| 131 | + y |= (data[1] >> bits_per_component) << (store_bits-bits_per_component); |
| 132 | + return y; |
| 133 | + } |
| 134 | + base_store_type getZ() { return data[1] & MASK; } |
| 135 | + |
| 136 | + void setX(base_store_type x) |
| 137 | + { |
| 138 | + data[0] &= ~MASK; |
| 139 | + data[0] |= x & MASK; |
| 140 | + } |
| 141 | + void setY(base_store_type y) |
| 142 | + { |
| 143 | + const uint16_t ybits = store_bits-bits_per_component; |
| 144 | + const uint16_t ymask = uint16_t(1u) << ybits; |
| 145 | + data[0] &= MASK; |
| 146 | + data[1] &= MASK; |
| 147 | + data[0] |= (y & ymask) << bits_per_component; |
| 148 | + data[1] |= (y >> (ybits) & ymask) << bits_per_component; |
| 149 | + } |
| 150 | + void setZ(base_store_type z) |
| 151 | + { |
| 152 | + data[1] &= ~MASK; |
| 153 | + data[1] |= z & MASK; |
| 154 | + } |
| 155 | + |
| 156 | + store_type data; |
| 157 | +}; |
| 158 | + |
| 159 | +// not complete because we're changing the template params next commit |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +} |
| 164 | +} |
| 165 | + |
| 166 | +#endif |
0 commit comments