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

particle_allocator.cc « intern « simulation « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91ca667a239e6b8e66a375c3fb9827e83aabd983 (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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "particle_allocator.hh"

#include "BLI_rand.hh"

namespace blender::sim {

AttributesAllocator::~AttributesAllocator()
{
  for (std::unique_ptr<AttributesBlock> &block : allocated_blocks_) {
    for (int i : attributes_info_.index_range()) {
      const fn::CPPType &type = attributes_info_.type_of(i);
      type.destruct_n(block->buffers[i], block->size);
      MEM_freeN(block->buffers[i]);
    }
  }
}

fn::MutableAttributesRef AttributesAllocator::allocate_uninitialized(int size)
{
  std::unique_ptr<AttributesBlock> block = std::make_unique<AttributesBlock>();
  block->buffers = Array<void *>(attributes_info_.size(), nullptr);
  block->size = size;

  for (int i : attributes_info_.index_range()) {
    const fn::CPPType &type = attributes_info_.type_of(i);
    void *buffer = MEM_mallocN_aligned(size * type.size(), type.alignment(), AT);
    block->buffers[i] = buffer;
  }

  fn::MutableAttributesRef attributes{attributes_info_, block->buffers, size};

  {
    std::lock_guard lock{mutex_};
    allocated_blocks_.append(std::move(block));
    allocated_attributes_.append(attributes);
    total_allocated_ += size;
  }

  return attributes;
}

fn::MutableAttributesRef ParticleAllocator::allocate(int size)
{
  const fn::AttributesInfo &info = attributes_allocator_.attributes_info();
  fn::MutableAttributesRef attributes = attributes_allocator_.allocate_uninitialized(size);
  for (int i : info.index_range()) {
    const fn::CPPType &type = info.type_of(i);
    StringRef name = info.name_of(i);
    if (name == "ID") {
      int start_id = next_id_.fetch_add(size);
      MutableSpan<int> ids = attributes.get<int>("ID");
      for (int pindex : IndexRange(size)) {
        ids[pindex] = start_id + pindex;
      }
    }
    else if (name == "Hash") {
      MutableSpan<int> hashes = attributes.get<int>("Hash");
      RandomNumberGenerator rng(hash_seed_ ^ static_cast<uint32_t>(next_id_));
      for (int pindex : IndexRange(size)) {
        hashes[pindex] = static_cast<int>(rng.get_uint32());
      }
    }
    else {
      type.fill_uninitialized(info.default_of(i), attributes.get(i).data(), size);
    }
  }
  return attributes;
}

}  // namespace blender::sim