Program Listing for File callable-cache.h

Return to documentation for file (include\playback\callable-cache.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/callable.h"

#include "playback/resource-info/detail/span.h"

#include <cstddef>
#include <vector>
#include <map>
#include <set>
#include <vector>

namespace gpa {
namespace playback {

class Callable;

typedef uint64_t SecondaryCommandBufferKeyT;
typedef uint64_t PrimaryCommandBufferKeyT;

struct SecondaryCommandBufferCallRange
{
    uint64_t rangeStart;
    uint64_t rangeEnd;
    PrimaryCommandBufferKeyT primaryCommandBufferKey;

    SecondaryCommandBufferCallRange(uint64_t startIndex, PrimaryCommandBufferKeyT key)
        : rangeStart(startIndex)
        , rangeEnd(UINT64_MAX)
        , primaryCommandBufferKey(key)
    {
    }
};

struct SecondaryCommandBufferMetadata
{
    std::map<uint64_t, SecondaryCommandBufferCallRange> primaryCommandBufferOverrides;
    void* originalSecondaryCommandBufferHandle;

    PrimaryCommandBufferKeyT GetOverrideForIndex(uint64_t callableIndex) const
    {
        auto itr = primaryCommandBufferOverrides.upper_bound(callableIndex);
        if (itr == primaryCommandBufferOverrides.begin()) {
            // No entries found itr == end == begin
            return 0;
        }
        --itr;
        if (callableIndex > itr->second.rangeEnd) {
            // index beyond last range
            return 0;
        }
        return itr->second.primaryCommandBufferKey;
    }
};

class CallableCache
{
public:
    CallableCache();
    CallableCache(CallableCache&& other);
    CallableCache(CallableCache const& other);
    ~CallableCache();

    CallableCache& operator=(CallableCache const& other);
    Callable* operator[](uint64_t index);
    Callable const* operator[](uint64_t index) const;

    // cache takes ownership of the callable
    void Add(Callable&& callable);

    // employs STL "reserve" method semantics/behavior
    void Reserve(size_t capacity);

    // empty cache contents
    void Clear();

    typedef bool (*SortFunction)(Callable const& lhs, Callable const& rhs);
    void Sort(SortFunction sortFunction);

    typedef void (*EnumerateCallablesCallback)(Callable* callable, void* userData);
    typedef void (*EnumerateCallablesCallbackEx)(Callable* callable, uint64_t callableIndex, void* userData);
    using EnumerateCallablesStdFunCallback = std::function<void(Callable* callable, uint64_t callableIndex)>;
    void EnumerateCallables(EnumerateCallablesCallback callback, void* userData);
    void EnumerateCallables(EnumerateCallablesCallbackEx callback, void* userData);
    void EnumerateCallables(EnumerateCallablesStdFunCallback callback);

    Callable const& PeekFront() const;
    Callable const& PeekBack() const;
    size_t CallableCount() const;

    detail::Span<Callable> GetCallables();

    void SetProcessedInGPUOrder(bool val);
    bool IsProcessedInGPUOrder() const;

    // @brief Return override command buffer key for secondary command buffer at the callableIndex
    PrimaryCommandBufferKeyT GetOverrideCommandBufferKeyForSecondaryBuffer(SecondaryCommandBufferKeyT secondaryCommandBufferKey,
                                                                           uint64_t callableIndex);

    // @brief Returns mObjectMapKeyOverrides
    std::map<SecondaryCommandBufferKeyT, SecondaryCommandBufferMetadata>* ObjectMapKeyOverrides();

    const std::set<uint64_t>& GetSecondaryBufferExecutionIndices();
    const std::set<uint64_t>& GetCommandBufferSubmissionIndices();
    // Must be called if mCallables vector is changed (new members added or deleted, vector sorted)
    void MarkCachedSetsDirty();
    // Recalculate secondary buffer executions for mCallables
    void RecalculateSecondaryBufferExecutionIndices();
    // Recalculate command buffer submissions for mCallables
    void RecalculateCommandBufferSubmissionIndices();

protected:
    bool mIsProcessedInGPUOrder = false;
    std::vector<Callable> mCallables;
    // @brief Map containing pairs of capture key with its override that needs to be applied at given index
    std::map<SecondaryCommandBufferKeyT, SecondaryCommandBufferMetadata> mObjectMapKeyOverrides;

    // @brief set that contains secondary command buffer (Bundle) execution callable indices for entire CallableCache
    std::set<uint64_t> mSecondaryCommandBufferExecutionIndices;
    bool mIsDirtySecondaryCommandBufferExecutionsSet = true;

    // @brief set that contains command buffer (command list) submission callable indices for entire CallableCache
    std::set<uint64_t> mCommandBufferSubmissionIndices;
    bool mIsDirtyCommandBufferSubmissionsSet = true;
};

}  // namespace playback
}  // namespace gpa