Files
test/source/blender/gpu/GPU_texture_pool.hh
Clément Foucault 6138ee64a9 GPU: TexturePool: Add texture pool functionality to the GPU module
This patch adds the texture pool functionality that was previously
only available in the DRW module to the GPU module.

This allows to not rely on global `DST` variable for the managment
of these temporary textures.

Moreover, this can be extended using dedicated GPU backend
specific behavior to reduce the amount of memory needed
to render.

The implementation is mostly copy pasted from the draw implementation
but with more documentation. Also it is simplified since the
`DRW_texture_pool_query` functionality is not needed.

Pull Request: https://projects.blender.org/blender/blender/pulls/134403
2025-02-11 18:58:46 +01:00

65 lines
2.1 KiB
C++

/* SPDX-FileCopyrightText: 2005 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*
* A `gpu::TextureFromPool` is a wrapper around backend specific texture objects whose usage is
* transient and can be shared between parts of an engine or across several parts of blender.
*/
#pragma once
#include "BLI_vector.hh"
#include "GPU_texture.hh"
namespace blender::gpu {
class TexturePool {
private:
/* Defer deallocation enough cycles to avoid interleaved calls to different viewport render
* functions (selection / display) causing constant allocation / deallocation (See #113024). */
static constexpr int max_unused_cycles_ = 8;
struct TextureHandle {
GPUTexture *texture;
/* Counts the number of `reset()` call since the last use.
* The texture memory is deallocated after a certain number of cycles. */
int unused_cycles;
};
/* Pool of texture ready to be reused. */
blender::Vector<TextureHandle> pool_;
/* List of textures that are currently being used. Tracked to check memory leak. */
blender::Vector<GPUTexture *> acquired_;
public:
~TexturePool();
/* Return the texture pool from the active GPUContext.
* Only valid if a context is active. */
static TexturePool &get();
/* Acquire a texture from the pool with the given characteristics. */
GPUTexture *acquire_texture(int width,
int height,
eGPUTextureFormat format,
eGPUTextureUsage usage);
/* Release the texture so that its memory can be reused at some other point. */
void release_texture(GPUTexture *tmp_tex);
/* Transfer ownership of a texture from the pool to the caller. */
void take_texture_ownership(GPUTexture *tex);
/* Transfer back ownership to the pool. The texture will become part of the pool. */
void give_texture_ownership(GPUTexture *tex);
/* Ensure no texture is still acquired and release unused textures.
* If `force_free` is true, free all the texture memory inside the pool.
* Otherwise, only unused textures will be freed. */
void reset(bool force_free = false);
};
} // namespace blender::gpu