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

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

#pragma once

#include <cstdint>

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

#include "GPU_texture.h"

namespace blender::realtime_compositor {

/* ------------------------------------------------------------------------------------------------
 * Texture Pool Key
 *
 * A key used to identify a texture specification in a texture pool. Defines a hash and an equality
 * operator for use in a hash map. */
class TexturePoolKey {
 public:
  int2 size;
  eGPUTextureFormat format;

  /* Construct a key from the given texture size and format. */
  TexturePoolKey(int2 size, eGPUTextureFormat format);

  /* Construct a key from the size and format of the given texture. */
  TexturePoolKey(const GPUTexture *texture);

  uint64_t hash() const;
};

bool operator==(const TexturePoolKey &a, const TexturePoolKey &b);

/* ------------------------------------------------------------------------------------------------
 * Texture Pool
 *
 * A texture pool allows the allocation and reuse of textures throughout the execution of the
 * compositor to avoid memory fragmentation and texture allocation overheads. The texture pool
 * delegates the actual texture allocation to an allocate_texture method that should be implemented
 * by the caller of the compositor evaluator, allowing a more agnostic and flexible execution that
 * can be controlled by the caller. If the compositor is expected to execute frequently, like on
 * every redraw, then the allocation method should use a persistent texture pool to allow
 * cross-evaluation texture pooling, for instance, by using the DRWTexturePool. But if the
 * evaluator is expected to execute infrequently, the allocated textures can just be freed when the
 * evaluator is done, that is, when the pool is destructed. */
class TexturePool {
 private:
  /* The set of textures in the pool that are available to acquire for each distinct texture
   * specification. */
  Map<TexturePoolKey, Vector<GPUTexture *>> textures_;

 public:
  /* Check if there is an available texture with the given specification in the pool, if such
   * texture exists, return it, otherwise, return a newly allocated texture. Expect the texture to
   * be uncleared and possibly contains garbage data. */
  GPUTexture *acquire(int2 size, eGPUTextureFormat format);

  /* Shorthand for acquire with GPU_RGBA16F format. */
  GPUTexture *acquire_color(int2 size);

  /* Shorthand for acquire with GPU_RGBA16F format. Identical to acquire_color because vectors
   * are stored in RGBA textures, due to the limited support for RGB textures. */
  GPUTexture *acquire_vector(int2 size);

  /* Shorthand for acquire with GPU_R16F format. */
  GPUTexture *acquire_float(int2 size);

  /* Put the texture back into the pool, potentially to be acquired later by another user. Expects
   * the texture to be one that was acquired using the same texture pool. */
  void release(GPUTexture *texture);

  /* Reset the texture pool by clearing all available textures without freeing the textures. If the
   * textures will no longer be needed, they should be freed in the destructor. This should be
   * called after the compositor is done evaluating. */
  void reset();

 private:
  /* Returns a newly allocated texture with the given specification. This method should be
   * implemented by the caller of the compositor evaluator. See the class description for more
   * information. */
  virtual GPUTexture *allocate_texture(int2 size, eGPUTextureFormat format) = 0;
};

}  // namespace blender::realtime_compositor