| /* Copyright (c) 2019-2026 The Khronos Group Inc. | |
| * Copyright (c) 2019-2026 Valve Corporation | |
| * Copyright (c) 2019-2026 LunarG, Inc. | |
| * Copyright (C) 2019-2026 Google Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| * | |
| * John Zulauf <jzulauf@lunarg.com> | |
| * | |
| */ | |
| #pragma once | |
| #include <cassert> | |
| #include <cstddef> | |
| #include "containers/range.h" | |
| #include "vulkan/vulkan.h" | |
| namespace vvl { | |
| class SubresourceEncoder; | |
| // Interface for aspect specific traits objects (now isolated in the cpp file) | |
| class AspectParameters { | |
| public: | |
| virtual ~AspectParameters() {} | |
| static const AspectParameters* Get(VkImageAspectFlags); | |
| virtual VkImageAspectFlags AspectMask() const = 0; | |
| virtual uint32_t AspectCount() const = 0; | |
| virtual const VkImageAspectFlagBits* AspectBits() const = 0; | |
| }; | |
| struct Subresource : public VkImageSubresource { | |
| uint32_t aspect_index; | |
| Subresource() : VkImageSubresource({0, 0, 0}), aspect_index(0) {} | |
| Subresource(const Subresource& from) = default; | |
| Subresource(const SubresourceEncoder& encoder, const VkImageSubresource& subres); | |
| Subresource(VkImageAspectFlags aspect_mask_, uint32_t mip_level_, uint32_t array_layer_, uint32_t aspect_index_) | |
| : VkImageSubresource({aspect_mask_, mip_level_, array_layer_}), aspect_index(aspect_index_) {} | |
| Subresource(VkImageAspectFlagBits aspect_, uint32_t mip_level_, uint32_t array_layer_, uint32_t aspect_index_) | |
| : Subresource(static_cast<VkImageAspectFlags>(aspect_), mip_level_, array_layer_, aspect_index_) {} | |
| Subresource& operator=(const Subresource&) = default; | |
| }; | |
| // Subresource is encoded in (from slowest varying to fastest) | |
| // aspect_index | |
| // mip_level_index | |
| // array_layer_index | |
| // into continuous index ranges | |
| class SubresourceEncoder { | |
| public: | |
| using IndexType = uint32_t; | |
| using IndexRange = vvl::range<IndexType>; | |
| static constexpr uint32_t kMaxSupportedAspect = 4; | |
| // The default constructor for default iterators | |
| SubresourceEncoder() | |
| : limits_(), | |
| full_range_(), | |
| mip_size_(0), | |
| aspect_size_(0), | |
| aspect_bits_(nullptr), | |
| encode_function_(nullptr), | |
| decode_function_(nullptr), | |
| lower_bound_function_(nullptr), | |
| lower_bound_with_start_function_(nullptr), | |
| aspect_base_{0, 0, 0} {} | |
| // Create the encoder suitable to the full range (aspect mask *must* be canonical) | |
| explicit SubresourceEncoder(const VkImageSubresourceRange& full_range) | |
| : SubresourceEncoder(full_range, AspectParameters::Get(full_range.aspectMask)) {} | |
| SubresourceEncoder(const SubresourceEncoder& from) = default; | |
| inline bool InRange(const VkImageSubresourceRange& range) const { | |
| return (range.baseMipLevel < limits_.mipLevel) && ((range.baseMipLevel + range.levelCount) <= limits_.mipLevel) && | |
| (range.baseArrayLayer < limits_.arrayLayer) && ((range.baseArrayLayer + range.layerCount) <= limits_.arrayLayer) && | |
| (range.aspectMask & limits_.aspectMask); | |
| } | |
| inline IndexType Encode(const Subresource& pos) const { return (this->*(encode_function_))(pos); } | |
| inline IndexType Encode(const VkImageSubresource& subres) const { return Encode(Subresource(*this, subres)); } | |
| Subresource Decode(const IndexType& index) const { return (this->*decode_function_)(index); } | |
| inline Subresource BeginSubresource(const VkImageSubresourceRange& range) const { | |
| if (!InRange(range)) { | |
| return limits_; | |
| } | |
| const auto aspect_index = LowerBoundFromMask(range.aspectMask); | |
| Subresource begin(aspect_bits_[aspect_index], range.baseMipLevel, range.baseArrayLayer, aspect_index); | |
| return begin; | |
| } | |
| inline Subresource Begin() const { | |
| Subresource begin(aspect_bits_[0], 0, 0, 0); | |
| return begin; | |
| } | |
| // This version assumes the mask must have at least one bit matching limits_.aspectMask | |
| // Suitable for getting a starting value from a range | |
| inline uint32_t LowerBoundFromMask(VkImageAspectFlags mask) const { | |
| assert(mask & limits_.aspectMask); | |
| return (this->*(lower_bound_function_))(mask); | |
| } | |
| // This version allows for a mask that can (starting at start) not have any bits set matching limits_.aspectMask | |
| // Suitable for seeking the *next* value for a range | |
| inline uint32_t LowerBoundFromMask(VkImageAspectFlags mask, uint32_t start) const { | |
| if (start < limits_.aspect_index) { | |
| return (this->*(lower_bound_with_start_function_))(mask, start); | |
| } | |
| return limits_.aspect_index; | |
| } | |
| inline IndexType AspectSize() const { return aspect_size_; } | |
| inline IndexType MipSize() const { return mip_size_; } | |
| inline const Subresource& Limits() const { return limits_; } | |
| inline const VkImageSubresourceRange& FullRange() const { return full_range_; } | |
| inline IndexType SubresourceCount() const { return AspectSize() * Limits().aspect_index; } | |
| inline VkImageAspectFlags AspectMask() const { return limits_.aspectMask; } | |
| inline VkImageAspectFlagBits AspectBit(uint32_t aspect_index) const { | |
| assert(aspect_index < limits_.aspect_index); | |
| return aspect_bits_[aspect_index]; | |
| } | |
| inline IndexType AspectBase(uint32_t aspect_index) const { | |
| assert(aspect_index < limits_.aspect_index); | |
| return aspect_base_[aspect_index]; | |
| } | |
| inline VkImageSubresource MakeVkSubresource(const Subresource& subres) const { | |
| VkImageSubresource vk_subres = {static_cast<VkImageAspectFlags>(aspect_bits_[subres.aspect_index]), subres.mipLevel, | |
| subres.arrayLayer}; | |
| return vk_subres; | |
| } | |
| inline VkImageSubresource IndexToVkSubresource(const IndexType& index) const { return MakeVkSubresource(Decode(index)); } | |
| protected: | |
| SubresourceEncoder(const VkImageSubresourceRange& full_range, const AspectParameters* param); | |
| void PopulateFunctionPointers(); | |
| IndexType Encode1AspectArrayOnly(const Subresource& pos) const; | |
| IndexType Encode1AspectMipArray(const Subresource& pos) const; | |
| IndexType Encode1AspectMipOnly(const Subresource& pos) const; | |
| IndexType EncodeAspectArrayOnly(const Subresource& pos) const; | |
| IndexType EncodeAspectMipArray(const Subresource& pos) const; | |
| IndexType EncodeAspectMipOnly(const Subresource& pos) const; | |
| // Use compiler to create the aspect count variants... | |
| // For ranges that only have a single mip level... | |
| template <uint32_t N> | |
| Subresource DecodeAspectArrayOnly(const IndexType& index) const { | |
| if constexpr (N > 2) { | |
| if (index >= aspect_base_[2]) { | |
| return Subresource(aspect_bits_[2], 0, static_cast<uint32_t>(index - aspect_base_[2]), 2); | |
| } | |
| } else if constexpr (N > 1) { | |
| if (index >= aspect_base_[1]) { | |
| return Subresource(aspect_bits_[1], 0, static_cast<uint32_t>(index - aspect_base_[1]), 1); | |
| } | |
| } | |
| // NOTE: aspect_base_[0] is always 0... here and below | |
| return Subresource(aspect_bits_[0], 0, static_cast<uint32_t>(index), 0); | |
| } | |
| // For ranges that only have a single array layer... | |
| template <uint32_t N> | |
| Subresource DecodeAspectMipOnly(const IndexType& index) const { | |
| if constexpr (N > 2) { | |
| if (index >= aspect_base_[2]) { | |
| return Subresource(aspect_bits_[2], static_cast<uint32_t>(index - aspect_base_[2]), 0, 2); | |
| } | |
| } else if constexpr (N > 1) { | |
| if (index >= aspect_base_[1]) { | |
| return Subresource(aspect_bits_[1], static_cast<uint32_t>(index - aspect_base_[1]), 0, 1); | |
| } | |
| } | |
| return Subresource(aspect_bits_[0], static_cast<uint32_t>(index), 0, 0); | |
| } | |
| // For ranges that only have both > 1 layer and level | |
| template <uint32_t N> | |
| Subresource DecodeAspectMipArray(const IndexType& index) const { | |
| assert(limits_.aspect_index <= N); | |
| uint32_t aspect_index = 0; | |
| if constexpr (N > 2) { | |
| if (index >= aspect_base_[2]) { | |
| aspect_index = 2; | |
| } | |
| } else if constexpr (N > 1) { | |
| if (index >= aspect_base_[1]) { | |
| aspect_index = 1; | |
| } | |
| } | |
| // aspect_base_[0] is always zero, so use the template to cheat | |
| const IndexType base_index = index - ((N == 1) ? 0 : aspect_base_[aspect_index]); | |
| const IndexType mip_level = base_index / mip_size_; | |
| const IndexType mip_start = mip_level * mip_size_; | |
| const IndexType array_offset = base_index - mip_start; | |
| return Subresource(aspect_bits_[aspect_index], static_cast<uint32_t>(mip_level), static_cast<uint32_t>(array_offset), | |
| aspect_index); | |
| } | |
| uint32_t LowerBoundImpl1(VkImageAspectFlags aspect_mask) const; | |
| uint32_t LowerBoundImpl2(VkImageAspectFlags aspect_mask) const; | |
| uint32_t LowerBoundImpl3(VkImageAspectFlags aspect_mask) const; | |
| uint32_t LowerBoundWithStartImpl1(VkImageAspectFlags aspect_mask, uint32_t start) const; | |
| uint32_t LowerBoundWithStartImpl2(VkImageAspectFlags aspect_mask, uint32_t start) const; | |
| uint32_t LowerBoundWithStartImpl3(VkImageAspectFlags aspect_mask, uint32_t start) const; | |
| Subresource limits_; | |
| private: | |
| VkImageSubresourceRange full_range_; | |
| const IndexType mip_size_; | |
| const IndexType aspect_size_; | |
| const VkImageAspectFlagBits* const aspect_bits_; | |
| IndexType (SubresourceEncoder::*encode_function_)(const Subresource&) const; | |
| Subresource (SubresourceEncoder::*decode_function_)(const IndexType&) const; | |
| uint32_t (SubresourceEncoder::*lower_bound_function_)(VkImageAspectFlags aspect_mask) const; | |
| uint32_t (SubresourceEncoder::*lower_bound_with_start_function_)(VkImageAspectFlags aspect_mask, uint32_t start) const; | |
| IndexType aspect_base_[kMaxSupportedAspect]; | |
| }; | |
| class SubresourceGenerator : public Subresource { | |
| public: | |
| SubresourceGenerator() : Subresource(), encoder_(nullptr), limits_() {}; | |
| SubresourceGenerator(const SubresourceEncoder& encoder, const VkImageSubresourceRange& range) | |
| : Subresource(encoder.BeginSubresource(range)), encoder_(&encoder), limits_(range) {} | |
| explicit SubresourceGenerator(const SubresourceEncoder& encoder) | |
| : Subresource(encoder.Begin()), encoder_(&encoder), limits_(encoder.FullRange()) {} | |
| const VkImageSubresourceRange& Limits() const { return limits_; } | |
| // Seek functions are used by generators to force synchronization, as callers may have altered the position | |
| // to iterater between calls to the generator increment or Seek functions | |
| void SeekAspect(uint32_t seek_index) { | |
| arrayLayer = limits_.baseArrayLayer; | |
| mipLevel = limits_.baseMipLevel; | |
| const auto aspect_index_limit = encoder_->Limits().aspect_index; | |
| if (seek_index < aspect_index_limit) { | |
| aspect_index = seek_index; | |
| // Seeking to bit outside of the limit will set a "empty" subresource | |
| aspectMask = encoder_->AspectBit(aspect_index) & limits_.aspectMask; | |
| } else { | |
| // This is an "end" tombstone | |
| aspect_index = aspect_index_limit; | |
| aspectMask = 0; | |
| } | |
| } | |
| void SeekMip(uint32_t mip_level) { | |
| arrayLayer = limits_.baseArrayLayer; | |
| mipLevel = mip_level; | |
| } | |
| // Next and and ++ functions are for iteration from a base with the bounds, this may be additionally | |
| // controlled/updated by an owning generator (like SubresourceRangeGenerator using Seek functions) | |
| inline void NextAspect() { SeekAspect(encoder_->LowerBoundFromMask(limits_.aspectMask, aspect_index + 1)); } | |
| void NextMip() { | |
| arrayLayer = limits_.baseArrayLayer; | |
| mipLevel++; | |
| if (mipLevel >= (limits_.baseMipLevel + limits_.levelCount)) { | |
| NextAspect(); | |
| } | |
| } | |
| SubresourceGenerator& operator++() { | |
| arrayLayer++; | |
| if (arrayLayer >= (limits_.baseArrayLayer + limits_.layerCount)) { | |
| NextMip(); | |
| } | |
| return *this; | |
| } | |
| // General purpose and slow, when we have no other information to update the generator | |
| void Seek(SubresourceEncoder::IndexType index) { | |
| // skip forward past discontinuities | |
| *static_cast<Subresource*>(this) = encoder_->Decode(index); | |
| } | |
| const VkImageSubresource& operator*() const { return *this; } | |
| const VkImageSubresource* operator->() const { return this; } | |
| private: | |
| const SubresourceEncoder* encoder_; | |
| const VkImageSubresourceRange limits_; | |
| }; | |
| // Like an iterator for ranges... | |
| class SubresourceRangeGenerator { | |
| public: | |
| using IndexRange = SubresourceEncoder::IndexRange; | |
| SubresourceRangeGenerator() : encoder_(nullptr), isr_pos_(), pos_(), aspect_base_() {} | |
| bool operator!=(const SubresourceRangeGenerator& rhs) { return (pos_ != rhs.pos_) || (&encoder_ != &rhs.encoder_); } | |
| explicit SubresourceRangeGenerator(const SubresourceEncoder& encoder) | |
| : SubresourceRangeGenerator(encoder, encoder.FullRange()) {} | |
| SubresourceRangeGenerator(const SubresourceEncoder& encoder, const VkImageSubresourceRange& subres_range); | |
| const IndexRange& operator*() const { return pos_; } | |
| const IndexRange* operator->() const { return &pos_; } | |
| SubresourceRangeGenerator& operator++(); | |
| private: | |
| const SubresourceEncoder* encoder_; | |
| SubresourceGenerator isr_pos_; | |
| IndexRange pos_; | |
| IndexRange aspect_base_; | |
| uint32_t mip_count_ = 0; | |
| uint32_t mip_index_ = 0; | |
| uint32_t aspect_count_ = 0; | |
| uint32_t aspect_index_ = 0; | |
| }; | |
| } // namespace vvl |