16#include "fmt/format.h"
38 return TextureFormat::SFLOAT_32_32_32_32;
40 return TextureFormat::SFLOAT_32_32;
42 return TextureFormat::SFLOAT_32;
44 return TextureFormat::SFLOAT_16;
46 return TextureFormat::UINT_8;
54 for (
const TextureHandle &handle : acquired_) {
57 for (AllocationHandle &handle : pool_) {
76 int mip_len_max = 1 +
floorf(log2f(std::max({extent.x, extent.y, extent.z})));
77 mip_len =
min_ii(mip_len, mip_len_max);
81 for (
uint64_t i : pool_.index_range()) {
82 const AllocationHandle &handle = pool_[i];
83 if (handle.texture->
format_ != compatible_format) {
86 if (
int3(handle.texture->
w_, handle.texture->
h_, handle.texture->
d_) != extent) {
90 if (handle.texture->
mip_count() != mip_len) {
99 TextureHandle texture_handle;
102 if (match_index != -1) {
103 texture_handle.texture = pool_[match_index].texture;
104 pool_.remove_and_reorder(match_index);
111 std::string texture_name_str;
113 texture_name_str = fmt::format(
"TexFromPool_{}", pool_.size());
118 bool texture_result =
false;
123 texture_result =
texture->init_1D(extent.x, extent.y, mip_len, compatible_format);
127 texture_result =
texture->init_2D(
128 extent.x, extent.y, extent.z, mip_len, compatible_format);
131 texture_result =
texture->init_3D(
132 extent.x, extent.y, extent.z, mip_len, compatible_format);
136 texture_result =
texture->init_cubemap(extent.x, extent.y, mip_len, compatible_format);
161 std::string view_name_str;
163 view_name_str = name ? name : texture_handle.texture->
name_;
169 bool view_result =
false;
177 texture_handle.texture, format, type, 0, mip_len, 0, 1,
false,
false);
181 texture_handle.texture, format, type, 0, mip_len, 0, extent.y,
false,
false);
186 texture_handle.texture, format, type, 0, mip_len, 0, extent.z,
false,
false);
203 texture_handle.view =
unwrap(view);
206 current_usage_data_.usage_count++;
207 current_usage_data_.usage_count_max = std::max(current_usage_data_.usage_count,
208 current_usage_data_.usage_count_max);
211 acquired_.add(texture_handle);
212 return wrap(texture_handle.view);
218 "Unacquired texture passed to TexturePool::release_texture()");
219 TextureHandle texture_handle = acquired_.lookup_key({
unwrap(tex), {}, 1});
222 current_usage_data_.usage_count--;
226 AllocationHandle allocation_handle;
227 allocation_handle.texture = texture_handle.texture;
228 pool_.append(allocation_handle);
232 acquired_.remove(texture_handle);
238 "Unacquired texture passed to TexturePool::offset_users_count()");
239 TextureHandle texture_handle = acquired_.lookup_key({
unwrap(tex), {}, 1});
240 texture_handle.users_count += offset;
241 acquired_.add_overwrite(texture_handle);
249 for (
const TextureHandle &tex : acquired_) {
251 "Missing texture release/retain. Likely TextureFromPool::release(), "
252 "TextureFromPool::retain() or TexturePool::release_texture().");
257 for (
int i = pool_.size() - 1; i >= 0; i--) {
258 AllocationHandle &handle = pool_[i];
259 if (handle.unused_cycles_count >= max_unused_cycles_ || force_free) {
261 pool_.remove_and_reorder(i);
264 handle.unused_cycles_count++;
270 if (!(previous_usage_data_ == current_usage_data_)) {
275 previous_usage_data_ = current_usage_data_;
276 current_usage_data_ = {};
277 current_usage_data_.usage_count = acquired_.size();
281void GLTexturePool::log_usage_data()
const
285 "GLTexturePool uses %ld textures (%ld consecutively)",
286 long(total_texture_count),
287 long(current_usage_data_.usage_count_max));
#define CLOG_TRACE(clg_ref,...)
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
Texture * acquire_texture_impl(int3 extent, int mip_len, GPUTextureType type, TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const char *name=nullptr) override
void offset_users_count(Texture *tex, int offset) override
void release_texture(Texture *tex) override
void reset(bool force_free=false) override
virtual Texture * texture_alloc(const char *name)=0
static GPUBackend * get()
GPUSamplerState sampler_state
bool init_view(Texture *src, TextureFormat format, GPUTextureType type, int mip_start, int mip_len, int layer_start, int layer_len, bool cube_as_array, bool use_stencil)
void usage_set(eGPUTextureUsage usage_flags)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
static TextureFormat get_compatible_texture_format(TextureFormat format)
@ GPU_FORMAT_DEPTH_STENCIL
BLI_INLINE MTLTexture * unwrap(Texture *tex)
int to_bytesize(const DataFormat format)
BLI_INLINE Texture * wrap(MTLTexture *texture)
GPUTextureFormatFlag to_format_flag(TextureFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
bool GPU_texture_has_integer_format(const gpu::Texture *texture)
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_ATTACHMENT
void GPU_memory_barrier(GPUBarrier barrier)
@ GPU_BARRIER_FRAMEBUFFER
@ GPU_BARRIER_TEXTURE_FETCH
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
void GPU_texture_free(gpu::Texture *texture)
MINLINE int min_ii(int a, int b)
void set_filtering_flag_from_test(GPUSamplerFiltering filtering_flags, bool test)