Skip to content

Commit ac0519c

Browse files
committed
use custom integer iterator instead of std::vector + iota
1 parent b948b6f commit ac0519c

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

src/nbl/asset/ICPUImage.cpp

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,56 @@ class CFlattenRegionsStreamHashImageFilter : public CMatchedSizeInOutImageFilter
8181

8282
using state_type = CState;
8383

84+
// A wrapper for an additive, in[de]crementable value, so you can use values as iterators
85+
template <typename Type, typename DiffType = std::ptrdiff_t>
86+
struct ValueIterator {
87+
88+
// Iterator type traits
89+
using iterator_category = std::random_access_iterator_tag;
90+
using difference_type = DiffType; //must be a signed integer-like type
91+
using value_type = Type;
92+
using reference = Type;
93+
using pointer = Type;
94+
95+
inline explicit ValueIterator(value_type value) : m_value(value) {};
96+
inline ValueIterator() : m_value{} {};
97+
ValueIterator(const ValueIterator& other) = default;
98+
ValueIterator(ValueIterator&& other) = default;
99+
100+
ValueIterator& operator=(const ValueIterator& other) = default;
101+
ValueIterator& operator=(ValueIterator&& other) = default;
102+
103+
// Iterator traits
104+
inline reference operator*() const { return m_value; }
105+
inline ValueIterator& operator++() { m_value++; return *this; }
106+
inline ValueIterator operator++(int) { ValueIterator tmp = *this; ++(*this); return tmp; }
107+
108+
// InputIterator traits
109+
inline bool operator==(const ValueIterator& other) const { return m_value == other.operator*(); }
110+
inline bool operator!=(const ValueIterator& other) const { return !operator==(other); }
111+
inline pointer operator->() const { return m_value; }
112+
113+
// BidirectionalIterator traits
114+
inline ValueIterator& operator--() { m_value--; return *this; }
115+
inline ValueIterator operator--(int) { ValueIterator tmp = *this; --(*this); return tmp; }
116+
117+
//RandomAccessIterator traits
118+
inline ValueIterator& operator+=(difference_type advance) { m_value += advance; return *this; }
119+
inline ValueIterator operator+(difference_type advance) const { return ValueIterator(m_value + advance); }
120+
friend inline ValueIterator operator+(difference_type advance, const ValueIterator& other) { return valueIterator(advance + *other); }
121+
inline ValueIterator& operator-=(difference_type advance) { m_value -= advance; return *this; }
122+
inline ValueIterator operator-(difference_type advance) const { return ValueIterator(m_value - advance); }
123+
inline value_type operator[] (int index) const { return m_value + index; }
124+
inline difference_type operator-(const ValueIterator& other) const { return m_value - *other; }
125+
inline bool operator< (const ValueIterator& other) const { return m_value < *other; }
126+
inline bool operator> (const ValueIterator& other) const { return m_value > *other; }
127+
inline bool operator>= (const ValueIterator& other) const { return m_value >= *other; }
128+
inline bool operator<= (const ValueIterator& other) const { return m_value <= *other; }
129+
130+
private:
131+
value_type m_value;
132+
};
133+
84134
static inline bool validate(state_type* state)
85135
{
86136
if (!state)
@@ -203,11 +253,13 @@ class CFlattenRegionsStreamHashImageFilter : public CMatchedSizeInOutImageFilter
203253
const auto texelOrBlockByteSize = asset::getTexelOrBlockBytesize(parameters.format);
204254
const uint8_t* inData = reinterpret_cast<const uint8_t*>(image->getBuffer()->getPointer());
205255

206-
std::vector<uint32_t> layers(parameters.arrayLayers);
207-
std::iota(layers.begin(), layers.end(), 0);
256+
struct range {
257+
ValueIterator<uint32_t> begin;
258+
ValueIterator<uint32_t> end;
259+
};
208260

209-
std::vector<uint32_t> levels(parameters.mipLevels);
210-
std::iota(levels.begin(), levels.end(), 0);
261+
range layers = { .begin{0}, .end{parameters.arrayLayers} };
262+
range levels = { .begin{0}, .end{parameters.mipLevels} };
211263

212264
/*
213265
we stream-hash texels per given mip level & layer
@@ -246,10 +298,10 @@ class CFlattenRegionsStreamHashImageFilter : public CMatchedSizeInOutImageFilter
246298
blake3_hasher_finalize(hasher, reinterpret_cast<uint8_t*>(hash), sizeof(CState::hash_t)); // finalize hash for layer + put it to heap for given mip level
247299
};
248300

249-
std::for_each(policy, layers.begin(), layers.end(), executePerLayer); // fire per layer for given given mip level with specified execution policy, yes you can use parallel policy here if you want at it will work
301+
std::for_each(policy, layers.begin, layers.end, executePerLayer); // fire per layer for given given mip level with specified execution policy, yes you can use parallel policy here if you want at it will work
250302
};
251303

252-
std::for_each(policy, levels.begin(), levels.end(), executePerMipLevel); // fire per block of layers for given mip level with specified execution policy, yes you can use parallel policy here if you want at it will work
304+
std::for_each(policy, levels.begin, levels.end, executePerMipLevel); // fire per block of layers for given mip level with specified execution policy, yes you can use parallel policy here if you want at it will work
253305

254306
/*
255307
scratch's heap is filled with all hashes,

0 commit comments

Comments
 (0)