Blender V5.0
gpu_texture_pool.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_global.hh"
10#include "BLI_string.h"
11
12#include "GPU_texture_pool.hh"
13
15
16namespace blender::gpu {
17
19{
20 for (blender::gpu::Texture *tex : acquired_) {
22 }
23 for (TextureHandle &tex : pool_) {
24 GPU_texture_free(tex.texture);
25 }
26}
27
29 int height,
31 eGPUTextureUsage usage)
32{
33 int64_t match_index = -1;
34 /* Search released texture first. */
35 for (auto i : pool_.index_range()) {
36 blender::gpu::Texture *tex = pool_[i].texture;
37 /* TODO(@fclem): We could reuse texture using texture views if the formats are compatible. */
38 if ((GPU_texture_format(tex) == format) && (GPU_texture_width(tex) == width) &&
39 (GPU_texture_height(tex) == height) && (GPU_texture_usage(tex) == usage))
40 {
41 match_index = i;
42 break;
43 }
44 }
45
46 if (match_index != -1) {
47 blender::gpu::Texture *tex = pool_[match_index].texture;
48 pool_.remove_and_reorder(match_index);
49 acquired_.append(tex);
50 return tex;
51 }
52
53 /* Create a new texture in last resort. */
54 /* TODO(@fclem): Rename each allocation using texture views. */
55 char name[16] = "TexFromPool";
56 if (G.debug & G_DEBUG_GPU) {
57 int texture_id = pool_.size();
58 SNPRINTF(name, "TexFromPool_%d", texture_id);
59 }
61 name, width, height, 1, format, usage, nullptr);
62 acquired_.append(tex);
63 return tex;
64}
65
67{
68 acquired_.remove_first_occurrence_and_reorder(tex);
69 pool_.append({tex, 0});
70}
71
73{
74 acquired_.remove_first_occurrence_and_reorder(tex);
75}
76
78{
79 acquired_.append(tex);
80}
81
82void TexturePool::reset(bool force_free)
83{
84 BLI_assert_msg(acquired_.is_empty(),
85 "Missing texture release. Either TextureFromPool.release() or "
86 "TexturePool.release_texture()");
87
88 /* Reverse iteration to make sure we only reorder with known good handles. */
89 for (int i = pool_.size() - 1; i >= 0; i--) {
90 TextureHandle &tex = pool_[i];
91 if (tex.unused_cycles >= max_unused_cycles_ || force_free) {
92 GPU_texture_free(tex.texture);
93 pool_.remove_and_reorder(i);
94 }
95 else {
96 tex.unused_cycles++;
97 }
98 }
99}
100
106
107} // namespace blender::gpu
@ G_DEBUG_GPU
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:604
GPUContext * GPU_context_active_get()
int GPU_texture_height(const blender::gpu::Texture *texture)
blender::gpu::TextureFormat GPU_texture_format(const blender::gpu::Texture *texture)
int GPU_texture_width(const blender::gpu::Texture *texture)
eGPUTextureUsage
eGPUTextureUsage GPU_texture_usage(const blender::gpu::Texture *texture)
blender::gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, blender::gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_free(blender::gpu::Texture *texture)
long long int int64_t
void reset(bool force_free=false)
blender::gpu::Texture * acquire_texture(int width, int height, blender::gpu::TextureFormat format, eGPUTextureUsage usage)
void give_texture_ownership(blender::gpu::Texture *tex)
void take_texture_ownership(blender::gpu::Texture *tex)
static TexturePool & get()
void release_texture(blender::gpu::Texture *tmp_tex)
format
#define G(x, y, z)
static Context * unwrap(GPUContext *ctx)
const char * name
i
Definition text_draw.cc:230