Blender V4.5
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 (GPUTexture *tex : acquired_) {
22 }
23 for (TextureHandle &tex : pool_) {
24 GPU_texture_free(tex.texture);
25 }
26}
27
28GPUTexture *TexturePool::acquire_texture(int width,
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 GPUTexture *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 GPUTexture *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 }
60 GPUTexture *tex = GPU_texture_create_2d(name, width, height, 1, format, usage, nullptr);
61 acquired_.append(tex);
62 return tex;
63}
64
65void TexturePool::release_texture(GPUTexture *tex)
66{
67 acquired_.remove_first_occurrence_and_reorder(tex);
68 pool_.append({tex, 0});
69}
70
72{
73 acquired_.remove_first_occurrence_and_reorder(tex);
74}
75
77{
78 acquired_.append(tex);
79}
80
81void TexturePool::reset(bool force_free)
82{
83 BLI_assert_msg(acquired_.is_empty(),
84 "Missing texture release. Either TextureFromPool.release() or "
85 "TexturePool.release_texture()");
86
87 /* Reverse iteration to make sure we only reorder with known good handles. */
88 for (int i = pool_.size() - 1; i >= 0; i--) {
89 TextureHandle &tex = pool_[i];
90 if (tex.unused_cycles >= max_unused_cycles_ || force_free) {
91 GPU_texture_free(tex.texture);
92 pool_.remove_and_reorder(i);
93 }
94 else {
95 tex.unused_cycles++;
96 }
97 }
98}
99
105
106} // 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:599
GPUContext * GPU_context_active_get()
int GPU_texture_height(const GPUTexture *texture)
GPUTexture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, eGPUTextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_free(GPUTexture *texture)
int GPU_texture_width(const GPUTexture *texture)
eGPUTextureUsage
eGPUTextureFormat
eGPUTextureUsage GPU_texture_usage(const GPUTexture *texture)
eGPUTextureFormat GPU_texture_format(const GPUTexture *texture)
long long int int64_t
void release_texture(GPUTexture *tmp_tex)
void reset(bool force_free=false)
void give_texture_ownership(GPUTexture *tex)
static TexturePool & get()
GPUTexture * acquire_texture(int width, int height, eGPUTextureFormat format, eGPUTextureUsage usage)
void take_texture_ownership(GPUTexture *tex)
format
#define G(x, y, z)
static Context * unwrap(GPUContext *ctx)
i
Definition text_draw.cc:230