From 0f08453ea9216c3cd957c17278a7158c98525219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Fri, 25 Feb 2022 01:06:09 +0100 Subject: DRW: Add simple texture view wrappers to draw::Texture --- source/blender/draw/intern/DRW_gpu_wrapper.hh | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/source/blender/draw/intern/DRW_gpu_wrapper.hh b/source/blender/draw/intern/DRW_gpu_wrapper.hh index 947ad6edc38..a69df8ac297 100644 --- a/source/blender/draw/intern/DRW_gpu_wrapper.hh +++ b/source/blender/draw/intern/DRW_gpu_wrapper.hh @@ -358,6 +358,8 @@ class StorageBuffer : public T, public detail::StorageCommon class Texture : NonCopyable { protected: GPUTexture *tx_ = nullptr; + Vector mip_views_; + Vector layer_views_; const char *name_; public: @@ -507,6 +509,56 @@ class Texture : NonCopyable { return ensure_impl(extent, extent, layers, mips, format, data, false, true); } + /** + * Ensure the availability of mipmap views. + * Mip view covers all layers of array textures. + */ + bool ensure_mip_views() + { + int mip_len = GPU_texture_mip_count(tx_); + if (mip_views_.size() != mip_len) { + for (GPUTexture *&view : mip_views_) { + GPU_TEXTURE_FREE_SAFE(view); + } + eGPUTextureFormat format = GPU_texture_format(tx_); + for (auto i : IndexRange(mip_len)) { + mip_views_.append(GPU_texture_create_view(name_, tx_, format, i, 1, 0, 9999)); + } + return true; + } + return false; + } + + GPUTexture *mip_view(int miplvl) + { + return mip_views_[miplvl]; + } + + /** + * Ensure the availability of mipmap views. + * Layer views covers all layers of array textures. + */ + bool ensure_layer_views() + { + int layer_len = GPU_texture_layer_count(tx_); + if (layer_views_.size() != layer_len) { + for (GPUTexture *&view : layer_views_) { + GPU_TEXTURE_FREE_SAFE(view); + } + eGPUTextureFormat format = GPU_texture_format(tx_); + for (auto i : IndexRange(layer_len)) { + layer_views_.append(GPU_texture_create_view(name_, tx_, format, 0, 9999, i, 1)); + } + return true; + } + return false; + } + + GPUTexture *layer_view(int layer) + { + return layer_views_[layer]; + } + /** * Returns true if the texture has been allocated or acquired from the pool. */ @@ -601,6 +653,13 @@ class Texture : NonCopyable { void free() { GPU_TEXTURE_FREE_SAFE(tx_); + for (GPUTexture *&view : mip_views_) { + GPU_TEXTURE_FREE_SAFE(view); + } + for (GPUTexture *&view : layer_views_) { + GPU_TEXTURE_FREE_SAFE(view); + } + mip_views_.clear(); } /** -- cgit v1.2.3