Program Listing for File resource-info-registry.h

Return to documentation for file (include\playback\resource-info\detail\resource-info-registry.h)

/******************************************************************************

© Intel Corporation.

This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.

 This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.

******************************************************************************/

#pragma once

#include "playback/resource-info/detail/binding-registry.h"
#include "playback/resource-info/detail/info-object-registry.h"
#include "playback/resource-info/enumerator.h"
#include "playback/resource-info/resource-info-types.h"

#include <map>
#include <string>
#include <unordered_map>

namespace gpa {
namespace playback {
namespace detail {

constexpr ResourceId sShaderIdMagic = 0x5244485300000000;  // SHDR

class BindingMonitor;

class ResourceInfoRegistry final
{
public:
    ResourceInfoRegistry() = default;

    uint64_t GetCurrentCallIndex() const;

    void IncrementCallIndex();

    ResourceInfo const* GetResourceInfo(ResourceId resourceId) const;

    Enumerator<ResourceInfo> GetResourceInfos() const;

    Enumerator<ResourceInfo> GetResourceInfos(ResourceInfo::Usage usage, uint64_t callIndex) const;

    Enumerator<ResourceInfo::Ex> GetResourceInfoExs(ResourceInfo::Usage usage, uint64_t callIndex, ResourceInfo const* pResourceInfo) const;

    std::pair<uint64_t, uint64_t> GetBindingInterval(ResourceInfo::Usage usage, uint64_t callIndex, ResourceInfo const* pResourceInfo) const;

    template<typename DerivedType>
    inline ResourceInfo const* RegisterResourceInfo(DerivedType const& resourceInfo)
    {
        auto result = mResourceInfoRegistry.RegisterObject(resourceInfo);
        if (result.first && result.second) {
            ResourceInfo* pResourceInfo = result.first;
            pResourceInfo->OnRegister();
            pResourceInfo->mRegistry = this;
            mResourceInfos[pResourceInfo->id] = pResourceInfo;
            mResourceInfoExBindingRegistries[pResourceInfo];
        }
        return result.first;
    }

    template<typename DerivedType>
    inline ResourceInfo::Ex const* RegisterResourceInfoEx(DerivedType const& resourceInfoEx)
    {
        auto result = mResourceInfoExRegistry.RegisterObject(resourceInfoEx);
        if (result.first && result.second) {
            ResourceInfo::Ex* pResourceInfoEx = result.first;
            pResourceInfoEx->OnRegister();
        }
        return result.first;
    }

    void BindResourceInfo(
        ResourceInfo::Usage usage,
        ResourceInfo const* pResourceInfo,
        BindingMonitor* pBindingMonitor = nullptr);

    template<typename DerivedExType>
    inline void BindResourceInfoEx(
        ResourceInfo::Usage usage,
        ResourceInfo const* pResourceInfo,
        DerivedExType const& resourceInfoEx,
        BindingMonitor* pBindingMonitor = nullptr)
    {
        auto pResourceInfoEx = RegisterResourceInfoEx(resourceInfoEx);
        BindResourceInfoEx(usage, pResourceInfo, pResourceInfoEx, pBindingMonitor);
    }

    ResourceId GenerateShaderId(
        api_types::ShaderStageFlagBits stage,
        void const* pShaderBytecode,
        size_t bytecodeLength);

private:
    class ShaderIdKey
    {
    public:
        ShaderIdKey(api_types::ShaderStageFlagBits stage, std::string bytecode)
            : mStage(stage)
            , mBytecode(std::move(bytecode))
        {
        }

        api_types::ShaderStageFlagBits GetStage() const noexcept { return mStage; }
        std::string const& GetBytecode() const noexcept { return mBytecode; }
        bool operator<(ShaderIdKey const& other) const;

    private:
        api_types::ShaderStageFlagBits mStage{api_types::ShaderStageFlagBits::GPA_SHADER_STAGE_UNKNOWN};
        std::string mBytecode;
    };

    void BindResourceInfoEx(
        ResourceInfo::Usage usage,
        ResourceInfo const* pResourceInfo,
        ResourceInfo::Ex const* pResourceInfoEx,
        BindingMonitor* pBindingMonitor);

    void OnBindingMonitorReset(BindingMonitor const& bindingMonitor);

    uint64_t mCurrentCallIndex{0};
    InfoObjectRegistry<ResourceInfo> mResourceInfoRegistry;
    InfoObjectRegistry<ResourceInfo::Ex> mResourceInfoExRegistry;
    std::unordered_map<ResourceId, ResourceInfo const*> mResourceInfos;
    detail::BindingRegistry<ResourceInfo> mResourceInfoBindingRegistries;
    std::unordered_map<ResourceInfo const*, detail::BindingRegistry<ResourceInfo::Ex>> mResourceInfoExBindingRegistries;
    std::map<ShaderIdKey, ResourceId> mShaderIds;

    ResourceInfoRegistry(ResourceInfoRegistry const&) = delete;
    ResourceInfoRegistry& operator=(ResourceInfoRegistry const&) = delete;
    friend class BindingMonitor;
};

}  // namespace detail
}  // namespace playback
}  // namespace gpa