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

texture_pool.cc « intern « realtime_compositor « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6bf2041e6bac06e951e4bccc7429c8c23b817785 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <cstdint>

#include "BLI_hash.hh"
#include "BLI_map.hh"
#include "BLI_math_vec_types.hh"
#include "BLI_vector.hh"

#include "GPU_texture.h"

#include "COM_texture_pool.hh"

namespace blender::realtime_compositor {

/* -------------------------------------------------------------------- */
/** \name Texture Pool Key
 * \{ */

TexturePoolKey::TexturePoolKey(int2 size, eGPUTextureFormat format) : size(size), format(format)
{
}

TexturePoolKey::TexturePoolKey(const GPUTexture *texture)
{
  size = int2(GPU_texture_width(texture), GPU_texture_height(texture));
  format = GPU_texture_format(texture);
}

uint64_t TexturePoolKey::hash() const
{
  return get_default_hash_3(size.x, size.y, format);
}

bool operator==(const TexturePoolKey &a, const TexturePoolKey &b)
{
  return a.size == b.size && a.format == b.format;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Texture Pool
 * \{ */

GPUTexture *TexturePool::acquire(int2 size, eGPUTextureFormat format)
{
  /* Check if there is an available texture with the required specification, and if one exists,
   * return it. */
  const TexturePoolKey key = TexturePoolKey(size, format);
  Vector<GPUTexture *> &available_textures = textures_.lookup_or_add_default(key);
  if (!available_textures.is_empty()) {
    return available_textures.pop_last();
  }

  /* Otherwise, allocate a new texture. */
  return allocate_texture(size, format);
}

GPUTexture *TexturePool::acquire_color(int2 size)
{
  return acquire(size, GPU_RGBA16F);
}

GPUTexture *TexturePool::acquire_vector(int2 size)
{
  /* Vectors are stored in RGBA textures because RGB textures have limited support. */
  return acquire(size, GPU_RGBA16F);
}

GPUTexture *TexturePool::acquire_float(int2 size)
{
  return acquire(size, GPU_R16F);
}

void TexturePool::release(GPUTexture *texture)
{
  textures_.lookup(TexturePoolKey(texture)).append(texture);
}

void TexturePool::reset()
{
  textures_.clear();
}

/** \} */

}  // namespace blender::realtime_compositor