Skip to content

Commit 5b83328

Browse files
committed
fix bitfieldInsert usage, truncate data
1 parent cd67d94 commit 5b83328

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

include/nbl/builtin/hlsl/sampling/quantized_sequence.hlsl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#ifndef _NBL_BUILTIN_HLSL_SAMPLING_QUANTIZED_SEQUENCE_INCLUDED_
66
#define _NBL_BUILTIN_HLSL_SAMPLING_QUANTIZED_SEQUENCE_INCLUDED_
77

8+
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
9+
#include "nbl/builtin/hlsl/bit.hlsl"
10+
#include "nbl/builtin/hlsl/glsl_compat/core.hlsl"
811
#include "nbl/builtin/hlsl/concepts/vector.hlsl"
912
#include "nbl/builtin/hlsl/vector_utils/vector_traits.hlsl"
10-
#include "nbl/builtin/hlsl/random/pcg.hlsl"
1113

1214
namespace nbl
1315
{
@@ -160,6 +162,7 @@ struct QuantizedSequence<T, Dim NBL_PARTIAL_REQ_BOT(impl::SequenceSpecialization
160162
using scalar_type = store_type;
161163
NBL_CONSTEXPR_STATIC_INLINE uint16_t StoreBits = uint16_t(8u) * size_of_v<store_type>;
162164
NBL_CONSTEXPR_STATIC_INLINE uint16_t BitsPerComponent = StoreBits / Dim;
165+
NBL_CONSTEXPR_STATIC_INLINE uint16_t DiscardBits = (uint16_t(8u) * size_of_v<scalar_type>) - BitsPerComponent;
163166
NBL_CONSTEXPR_STATIC_INLINE uint16_t Dimension = Dim;
164167

165168
static this_t create(const vector<store_type, Dimension> value)
@@ -180,7 +183,7 @@ struct QuantizedSequence<T, Dim NBL_PARTIAL_REQ_BOT(impl::SequenceSpecialization
180183
void set(const uint16_t idx, const store_type value)
181184
{
182185
assert(idx > 0 && idx < Dim);
183-
glsl::bitfieldInsert(data, value, BitsPerComponent * idx, BitsPerComponent);
186+
data = glsl::bitfieldInsert(data, value >> DiscardBits, BitsPerComponent * idx, BitsPerComponent);
184187
}
185188

186189
template<typename F>
@@ -290,15 +293,16 @@ struct QuantizedSequence<T, Dim NBL_PARTIAL_REQ_BOT(is_same_v<T,uint32_t2> && Di
290293
void set(const uint16_t idx, const scalar_type value)
291294
{
292295
assert(idx >= 0 && idx < 3);
296+
const scalar_type trunc_val = value >> DiscardBits;
293297
if (idx == 0) // x
294-
glsl::bitfieldInsert(data[0], value, 0u, BitsPerComponent);
298+
data[0] = glsl::bitfieldInsert(data[0], trunc_val, 0u, BitsPerComponent);
295299
else if (idx == 1) // y
296300
{
297-
glsl::bitfieldInsert(data[0], value, BitsPerComponent, DiscardBits);
298-
glsl::bitfieldInsert(data[1], value >> DiscardBits, 0u, DiscardBits - 1u);
301+
data[0] = glsl::bitfieldInsert(data[0], trunc_val, BitsPerComponent, DiscardBits);
302+
data[1] = glsl::bitfieldInsert(data[1], trunc_val >> DiscardBits, 0u, DiscardBits - 1u);
299303
}
300304
else // z
301-
glsl::bitfieldInsert(data[1], value, DiscardBits - 1u, BitsPerComponent);
305+
data[1] = glsl::bitfieldInsert(data[1], trunc_val, DiscardBits - 1u, BitsPerComponent);
302306
}
303307

304308
template<typename F>
@@ -334,6 +338,7 @@ struct QuantizedSequence<T, Dim NBL_PARTIAL_REQ_BOT(is_same_v<T,uint16_t2> && Di
334338
using scalar_type = typename vector_traits<T>::scalar_type;
335339
NBL_CONSTEXPR_STATIC_INLINE uint16_t StoreBits = uint16_t(8u) * size_of_v<store_type>;
336340
NBL_CONSTEXPR_STATIC_INLINE uint16_t BitsPerComponent = StoreBits / Dim;
341+
NBL_CONSTEXPR_STATIC_INLINE uint16_t DiscardBits = (uint16_t(8u) * size_of_v<scalar_type>) - BitsPerComponent;
337342
NBL_CONSTEXPR_STATIC_INLINE uint16_t Dimension = Dim;
338343

339344
static this_t create(const vector<scalar_type, Dimension> value)
@@ -361,13 +366,14 @@ struct QuantizedSequence<T, Dim NBL_PARTIAL_REQ_BOT(is_same_v<T,uint16_t2> && Di
361366
void set(const uint16_t idx, const scalar_type value)
362367
{
363368
assert(idx >= 0 && idx < 4);
369+
const scalar_type trunc_val = value >> DiscardBits;
364370
if (idx < 2) // x y
365371
{
366-
glsl::bitfieldInsert(data[0], value, BitsPerComponent * idx, BitsPerComponent);
372+
data[0] = glsl::bitfieldInsert(data[0], trunc_val, BitsPerComponent * idx, BitsPerComponent);
367373
}
368374
else // z w
369375
{
370-
glsl::bitfieldInsert(data[1], value, BitsPerComponent * (idx & uint16_t(1u)), BitsPerComponent);
376+
data[1] = glsl::bitfieldInsert(data[1], trunc_val, BitsPerComponent * (idx & uint16_t(1u)), BitsPerComponent);
371377
}
372378
}
373379

0 commit comments

Comments
 (0)