Commit: 1bc006f0f5decc9f55c168fd0a912bbcb12f190b Author: Omar Emara Date: Fri Apr 8 14:56:29 2022 +0200 Branches: temp-viewport-compositor-compiler https://developer.blender.org/rB1bc006f0f5decc9f55c168fd0a912bbcb12f190b
Viewport Compositor: Separate texture pool into its own file =================================================================== M source/blender/viewport_compositor/CMakeLists.txt M source/blender/viewport_compositor/VPC_compositor_execute.hh A source/blender/viewport_compositor/VPC_texture_pool.hh M source/blender/viewport_compositor/intern/compositor_execute.cc A source/blender/viewport_compositor/intern/texture_pool.cc =================================================================== diff --git a/source/blender/viewport_compositor/CMakeLists.txt b/source/blender/viewport_compositor/CMakeLists.txt index 46ae1293208..c2f7f6f6e60 100644 --- a/source/blender/viewport_compositor/CMakeLists.txt +++ b/source/blender/viewport_compositor/CMakeLists.txt @@ -18,10 +18,12 @@ set(INC set(SRC intern/compositor_execute.cc intern/scheduler.cc + intern/texture_pool.cc intern/utils.cc VPC_compositor_execute.hh VPC_scheduler.hh + VPC_texture_pool.hh VPC_utils.hh ) diff --git a/source/blender/viewport_compositor/VPC_compositor_execute.hh b/source/blender/viewport_compositor/VPC_compositor_execute.hh index 20806548e71..7b204cd9c9e 100644 --- a/source/blender/viewport_compositor/VPC_compositor_execute.hh +++ b/source/blender/viewport_compositor/VPC_compositor_execute.hh @@ -22,66 +22,10 @@ #include "NOD_derived_node_tree.hh" #include "VPC_scheduler.hh" +#include "VPC_texture_pool.hh" namespace blender::viewport_compositor { -/* -------------------------------------------------------------------- - * Texture Pool. - */ - -/* A key structure 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; - - TexturePoolKey(int2 size, eGPUTextureFormat format); - TexturePoolKey(const GPUTexture *texture); - - uint64_t hash() const; -}; - -/* A pool of textures that can be allocated and reused transparently throughout the evaluation of - * the compositor. This texture pool only pools textures throughout a single evaluation of the - * compositor and will reset after evaluation without freeing any textures. Cross-evaluation - * pooling and freeing of unused textures is the responsibility of the back-end texture pool used - * by the allocate_texture method. In the case of the viewport compositor engine, this would be the - * global DRWTexturePool of the draw manager. */ -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 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 vector - * textures are and should internally be stored in RGBA 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); - - private: - /* Returns a newly allocated texture with the given specification. This method should be - * implemented by the compositor engine and should use a global texture pool that is persistent - * across evaluations and capable of freeing unused textures. In the case of the viewport - * compositor engine, this would be the global DRWTexturePool of the draw manager. */ - virtual GPUTexture *allocate_texture(int2 size, eGPUTextureFormat format) = 0; -}; - /* -------------------------------------------------------------------- * Context. */ diff --git a/source/blender/viewport_compositor/VPC_texture_pool.hh b/source/blender/viewport_compositor/VPC_texture_pool.hh new file mode 100644 index 00000000000..3bf5bf2ed09 --- /dev/null +++ b/source/blender/viewport_compositor/VPC_texture_pool.hh @@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Copyright 2022 Blender Foundation. All rights reserved. */ + +#pragma once + +#include <cstdint> + +#include "BLI_map.hh" +#include "BLI_math_vec_types.hh" +#include "BLI_vector.hh" + +#include "GPU_texture.h" + +namespace blender::viewport_compositor { + +/* A key structure 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; + + TexturePoolKey(int2 size, eGPUTextureFormat format); + TexturePoolKey(const GPUTexture *texture); + + uint64_t hash() const; +}; + +/* A pool of textures that can be used to allocate textures and reused transparently throughout the + * evaluation of the compositor. This texture pool only pools textures throughout a single + * evaluation of the compositor and will reset after the evaluation without freeing any textures. + * Cross-evaluation pooling and freeing of unused textures is the responsibility of the back-end + * texture pool used by the allocate_texture method. In the case of the viewport compositor engine, + * this would be the global DRWTexturePool of the draw manager. */ +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 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 vector + * textures are and should internally be stored in RGBA 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); + + private: + /* Returns a newly allocated texture with the given specification. This method should be + * implemented by the compositor engine and should use a global texture pool that is persistent + * across evaluations and capable of freeing unused textures. In the case of the viewport + * compositor engine, this would be the global DRWTexturePool of the draw manager. */ + virtual GPUTexture *allocate_texture(int2 size, eGPUTextureFormat format) = 0; +}; + +} // namespace blender::viewport_compositor diff --git a/source/blender/viewport_compositor/intern/compositor_execute.cc b/source/blender/viewport_compositor/intern/compositor_execute.cc index 10f4bb8db15..dfab0d0b956 100644 --- a/source/blender/viewport_compositor/intern/compositor_execute.cc +++ b/source/blender/viewport_compositor/intern/compositor_execute.cc @@ -39,65 +39,11 @@ #include "VPC_compositor_execute.hh" #include "VPC_scheduler.hh" +#include "VPC_texture_pool.hh" #include "VPC_utils.hh" namespace blender::viewport_compositor { -/* -------------------------------------------------------------------- - * Texture Pool. - */ - -TexturePoolKey::TexturePoolKey(int2 size, eGPUTextureFormat format) : size(size), format(format) -{ -} - -TexturePoolKey::TexturePoolKey(const GPUTexture *texture) -{ - size = int2{GPU_texture_width(texture), GPU_texture_height(texture)}; - format = GPU_texture_format(texture); -} - -uint64_t TexturePoolKey::hash() const -{ - return get_default_hash_3(size.x, size.y, format); -} - -bool operator==(const TexturePoolKey &a, const TexturePoolKey &b) -{ - return a.size == b.size && a.format == b.format; -} - -GPUTexture *TexturePool::acquire(int2 size, eGPUTextureFormat format) -{ - const TexturePoolKey key = TexturePoolKey(size, format); - Vector<GPUTexture *> &available_textures = textures_.lookup_or_add_default(key); - if (available_textures.is_empty()) { - return allocate_texture(size, format); - } - return available_textures.pop_last(); -} - -GPUTexture *TexturePool::acquire_color(int2 size) -{ - return acquire(size, GPU_RGBA16F); -} - -/* Vectors are and should be stored in RGBA textures. */ -GPUTexture *TexturePool::acquire_vector(int2 size) -{ - return acquire(size, GPU_RGBA16F); -} - -GPUTexture *TexturePool::acquire_float(int2 size) -{ - return acquire(size, GPU_R16F); -} - -void TexturePool::release(GPUTexture *texture) -{ - textures_.lookup(TexturePoolKey(texture)).append(texture); -} - /* -------------------------------------------------------------------- * Context. */ diff --git a/source/blender/viewport_compositor/intern/texture_pool.cc b/source/blender/viewport_compositor/intern/texture_pool.cc new file mode 100644 index 00000000000..aad0f177516 --- /dev/null +++ b/source/blender/viewport_compositor/intern/texture_pool.cc @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + * Copyright 2022 Blender Foundation. All rights reserved. */ + +#include <cstdint> + +#include "BLI_hash.hh" +#include "BLI_map.hh" +#include "BLI_math_vec_types.hh" +#include "BLI_vector.hh" + +#include "GPU_texture.h" + +#include "VPC_texture_pool.hh" + +namespace blender::viewport_compositor { + +TexturePoolKey::TexturePoolKey(int2 size, eGPUTextureFormat format) : size(size), format(format) +{ +} + +TexturePoolKey::TexturePoolKey(const GPUTexture *texture) +{ + size = int2(GPU_texture_width(texture), GPU_texture_height(texture)); + format = GPU_texture_format(texture); +} + +uint64_t TexturePoolKey::hash() const +{ + return get_default_hash_3(size.x, size.y, format); +} + +bool operator==(const TexturePoolKey &a, const TexturePoolKey &b) +{ + return a.size == b.size && a.format == b.format; +} + +GPUTexture *TexturePool::acquire(int2 size, eGPUTextureFormat format) +{ + /* Check if there is an available texture with the required specification, and one exists, return + * it. */ + const TexturePoolKey @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs