Welcome to mirror list, hosted at ThFree Co, Russian Federation.

eevee_allocator.hh « eevee « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4bbeb4f113d1ca29e5e145f3611bdc69bd7ce36c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2021 Blender Foundation.
 */

/** \file
 * \ingroup eevee
 *
 * Allocators that may be moved to BLI at some point.
 */

#pragma once

#include "BLI_math_bits.h"
#include "BLI_vector.hh"

namespace blender::eevee {

/**
 * Allow allocation and deletion of elements without reordering.
 * Useful to keed valid indices to items inside this allocator.
 * Type T need to implement the free_resources() method.
 */
template<typename T> class IndexedAllocator {
 private:
  Vector<T> items_;
  /** Bitmap of used items. Make search of unused slot faster. */
  Vector<uint32_t> unused_;
  /** First unused batch of items in the vector for fast reallocating. */
  int64_t unused_first_ = LONG_MAX;
  /** Unused item count in the vector for fast reallocating. */
  int64_t unused_count_ = 0;

 public:
  int64_t alloc(T &&value)
  {
    if (unused_count_ > 0) {
      /* Reclaim unused slot. */
      int64_t index = unused_first_;
      items_[index] = std::move(value);
      set_slot_used(index);

      unused_count_ -= 1;
      unused_first_ = first_unused_slot_get();
      return index;
    }
    /* Not enough place, grow the vector. */
    int64_t index = items_.append_and_get_index(value);
    int64_t size_needed = (index + 32) / 32;
    int64_t size_old = unused_.size();
    unused_.resize(size_needed);
    if (size_old < size_needed) {
      for (auto i : IndexRange(size_old, size_needed - size_old)) {
        /* Used by default. */
        unused_[i] = 0;
      }
    }
    set_slot_used(index);
    return index;
  }

  void free(int64_t index)
  {
    unused_count_ += 1;
    if (index < unused_first_) {
      unused_first_ = index;
    }
    set_slot_unused(index);
    is_slot_unused(index);
    items_[index].free_resources();
  }

  /* Pruned unused shadows at the end of the vector. */
  void resize()
  {
    while (items_.size() > 0 && is_slot_unused(items_.size() - 1)) {
      set_slot_used(items_.size() - 1);
      items_.remove_last();
      unused_count_--;
    }
    if (unused_first_ >= items_.size()) {
      /* First unused has been pruned. */
      unused_first_ = first_unused_slot_get();
    }
  }

  int64_t size() const
  {
    return items_.size() - unused_count_;
  }

  class Iterator {
   public:
    using iterator_category = std::forward_iterator_tag;
    using value_type = T;
    using pointer = const T *;
    using reference = const T &;
    using difference_type = std::ptrdiff_t;

   private:
    IndexedAllocator &allocator_;
    int64_t current_;

   public:
    constexpr explicit Iterator(IndexedAllocator &allocator, int64_t current)
        : allocator_(allocator), current_(current)
    {
    }

    constexpr Iterator &operator++()
    {
      current_++;
      while ((current_ < allocator_.items_.size()) && allocator_.is_slot_unused(current_)) {
        current_++;
      }
      return *this;
    }

    constexpr Iterator operator++(int) const
    {
      Iterator iterator = *this;
      ++*this;
      return iterator;
    }

    constexpr friend bool operator!=(const Iterator &a, const Iterator &b)
    {
      return a.current_ != b.current_;
    }

    T &operator*()
    {
      BLI_assert(allocator_.is_slot_unused(current_) == false);
      return allocator_[current_];
    }
  };

  constexpr Iterator begin()
  {
    int64_t first_used = first_used_slot_get();
    if (first_used == LONG_MAX) {
      /* Will produce no iteration. */
      first_used = items_.size();
    }
    return Iterator(*this, first_used);
  }

  constexpr Iterator end()
  {
    return Iterator(*this, items_.size());
  }

  const T &operator[](int64_t index) const
  {
    BLI_assert(is_slot_unused(index) == false);
    return items_[index];
  }

  T &operator[](int64_t index)
  {
    BLI_assert(is_slot_unused(index) == false);
    return items_[index];
  }

 private:
  int64_t first_unused_slot_get(void) const
  {
    if (unused_count_ > 0) {
      for (auto i : IndexRange(unused_.size())) {
        if (unused_[i] != 0) {
          return i * 32 + bitscan_forward_uint(unused_[i]);
        }
      }
    }
    return LONG_MAX;
  }

  int64_t first_used_slot_get(void) const
  {
    if (unused_count_ < items_.size()) {
      for (auto i : IndexRange(unused_.size())) {
        if (~unused_[i] != 0) {
          return i * 32 + bitscan_forward_uint(~unused_[i]);
        }
      }
    }
    return LONG_MAX;
  }

  bool is_slot_unused(int64_t index) const
  {
    return (unused_[index / 32] & (1u << uint32_t(index % 32))) != 0;
  }

  void set_slot_unused(int64_t index)
  {
    SET_FLAG_FROM_TEST(unused_[index / 32], true, (1u << uint32_t(index % 32)));
  }

  void set_slot_used(int64_t index)
  {
    SET_FLAG_FROM_TEST(unused_[index / 32], false, (1u << uint32_t(index % 32)));
  }
};

}  // namespace blender::eevee