gsdf_impl_common.h#

Fully qualified name: src/device/vpu_runtime/include/cupva_device/impl/gsdf_impl_common.h

File members: src/device/vpu_runtime/include/cupva_device/impl/gsdf_impl_common.h

/*
 * Copyright (c) 2023 NVIDIA Corporation.  All rights reserved.
 *
 * NVIDIA Corporation and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA Corporation is strictly prohibited.
 */

#ifndef GSDF_IMPL_COMMON_H
#define GSDF_IMPL_COMMON_H

#include "dma_common.h"

#include <cupva_types.h>

namespace cupva { namespace device { namespace priv {
constexpr uint32_t gsdfNumVpucBuffers           = 2U;
constexpr int32_t gsdfNumVpucBuffersSigned      = 2;
constexpr uint32_t gsdfPlanarGeometryStructSize = sizeof(PlanarGeometry);
}}} // namespace cupva::device::priv

#define VMEM_GSDF_HANDLER_MACRO(bank, name, numTiles, isUniform, macro, ...) macro

#define GSDF_DESCRCNTL_SLOT 0U
#define GSDF_LPCNTL_SLOT 1U
#define GSDF_VMEMADDR_SLOT 2U
#define GSDF_TRANSCNTL_SLOT 3U
#define GSDF_TILECNTL_SLOT 4U
#define GSDF_EXTADDR_SLOT 5U

namespace cupva { namespace device { namespace priv {
constexpr uint32_t gsdfBlocklinearXAlignment = 32U;
constexpr uint32_t gsdfBlocklinearYAlignment = 2U;
constexpr uint32_t gsdfBlocklinearYOffset    = 1U;

template<typename T>
inline constexpr uint32_t gsdfBlocklinearXOffset()
{
    return gsdfBlocklinearXAlignment / sizeof(T);
}
}}} // namespace cupva::device::priv

#define GSDF_VPUC_MAGIC 0xDEADC0DEU

namespace cupva { namespace device { namespace priv {
#if (CUPVA_PVA_GEN_NUMBER > 2)
constexpr uint32_t gsdfNumModes = 2U;
#else
constexpr uint32_t gsdfNumModes = 1U;
#endif
}}} // namespace cupva::device::priv

#define GSDF_VPUC_TBL_OFFSET 0U

// Worst case scenario for buffer sizing is where we need a new VPUC table for each channel
namespace cupva { namespace device { namespace priv {
inline constexpr uint32_t gsdfSwseqMaxNodesPerChannel(uint32_t numTiles)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    return (numTiles + (GSDF_SWSEQ_MAX_CHANNEL_COUNT - 1U)) / GSDF_SWSEQ_MAX_CHANNEL_COUNT;
}
// Again the worst case number of channels is where each new tile needs its own channel
inline constexpr uint32_t gsdfSwseqMaxChannelsForTiles(uint32_t numTiles)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    return numTiles > GSDF_SWSEQ_MAX_CHANNEL_COUNT ? GSDF_SWSEQ_MAX_CHANNEL_COUNT : numTiles;
}

inline constexpr uint32_t gsdfSwseqVpucTablePayloadMaxEntriesPerChannel(uint32_t numTiles)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    return GSDF_NUM_ENTRY_PER_DESC * gsdfSwseqMaxNodesPerChannel(numTiles);
}

inline constexpr uint32_t gsdfSwseqVpucTableMaxSizePerChannel(uint32_t numTiles)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    return (gsdfSwseqVpucTablePayloadMaxEntriesPerChannel(numTiles) + 1U) * sizeof(GSDFVPUConfigPair);
}

inline constexpr uint32_t gsdfSwseqVpucTableSize(uint32_t numTiles)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    return gsdfSwseqMaxChannelsForTiles(numTiles) * gsdfSwseqVpucTableMaxSizePerChannel(numTiles);
}

inline constexpr uint32_t gsdfSwseqTransposeTableOffset(uint32_t numTiles)
{
    return GSDF_VPUC_TBL_OFFSET + gsdfSwseqVpucTableSize(numTiles);
}
}}} // namespace cupva::device::priv
namespace cupva { namespace device { namespace priv {
constexpr uint32_t gsdfSwseqTransposeTableLp = GSDF_MAX_NUM_TILES + 1U;
constexpr uint32_t gsdfSwseqTransposeTableSizePerTable =
    GSDF_NUM_ENTRY_PER_DESC * gsdfSwseqTransposeTableLp * sizeof(uint32_t);
constexpr uint32_t gsdfSwseqTransposeTableSize = gsdfNumVpucBuffers * gsdfSwseqTransposeTableSizePerTable;

constexpr uint32_t gsdfHwseqMode0            = 0xDEADU;
constexpr uint32_t gsdfHwseqMode2            = 0xC0DAU;
constexpr uint32_t gsdfHwseqNumEntryAtClose  = 7U;
constexpr uint32_t gsdfHwseqHeader           = 12U;
constexpr uint32_t gsdfHwseqNumcolsShift     = 24U;
constexpr uint32_t gsdfHwseqEntrySize        = 8U;
constexpr int32_t gsdfHwseqEntrySizeSigned   = 8;
constexpr uint32_t gsdfHwseqNumPreambleEntry = 4U;
constexpr uint32_t gsdfHwseqMultiEntryOffset = 3U;

inline constexpr uint32_t gsdfHwseqVpucTablePayloadEntries(uint32_t numTiles, bool isUniform)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    return isUniform ? (GSDF_NUM_ENTRY_PER_DESC + numTiles) - 1U
                     : (GSDF_NUM_ENTRY_PER_DESC - gsdfHwseqMultiEntryOffset) * numTiles + gsdfHwseqMultiEntryOffset;
}

inline constexpr uint32_t gsdfHwseqVpucTableEntries(uint32_t numTiles, bool isUniform)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    return gsdfHwseqNumPreambleEntry + gsdfHwseqVpucTablePayloadEntries(numTiles, isUniform);
}

inline constexpr uint32_t gsdfHwseqVpucTableSize(uint32_t numTiles, bool isUniform)
{
    cupva::device::priv::vpuAssert((numTiles <= GSDF_MAX_NUM_TILES));
    uint32_t const aligned = alignUp(gsdfHwseqVpucTableEntries(numTiles, isUniform) + 1U, 16U);
    cupva::device::priv::vpuAssert((aligned <= (UINT32_MAX / gsdfHwseqEntrySize)));
    return aligned * gsdfHwseqEntrySize;
}
}}} // namespace cupva::device::priv
#define VMEM_GSDF_HANDLER_IMPL(bank, name, numTiles)                                                                \
    VMEM_1D(bank, uint8_t, _##name, cupva::device::priv::gsdfHandlerTotalSize(numTiles, false));                    \
    CUPVA_EXPORT(reinterpret_cast<GSDFHandler *>(_##name)->params, _M##name);                                       \
    CUPVA_EXPORT_IMPL(_##name[cupva::device::priv::gsdfStaticHeaderSize + GSDF_VPUC_TBL_OFFSET], _V##name##_export, \
                      cupva::device::priv::gsdfSwseqVpucTableSize(numTiles), VMEM_TYPE_VPUC_TABLE);                 \
    CUPVA_EXPORT(reinterpret_cast<GSDFHandler *>(_##name)->priv.swseq.configBuffers[0].extPointer, _P##name,        \
                 VMEM_TYPE_POINTER_EX);                                                                             \
    GSDFHandler *const name = reinterpret_cast<GSDFHandler *>(_##name);

#define VMEM_GSDF_HANDLER_UNIFORM_IMPL(bank, name, numTiles, isUniform)                                             \
    VMEM_1D(bank, uint8_t, _##name, cupva::device::priv::gsdfHandlerTotalSize(numTiles, isUniform));                \
    CUPVA_EXPORT(reinterpret_cast<GSDFHandlerUniform *>(_##name)->params, _M##name);                                \
    CUPVA_EXPORT_IMPL(_##name[cupva::device::priv::gsdfStaticHeaderSize + GSDF_VPUC_TBL_OFFSET], _U##name##_export, \
                      cupva::device::priv::gsdfSwseqVpucTableSize(numTiles), VMEM_TYPE_VPUC_TABLE);                 \
    CUPVA_EXPORT(reinterpret_cast<GSDFHandlerUniform *>(_##name)->priv.swseq.configBuffers[0].extPointer, _P##name, \
                 VMEM_TYPE_POINTER_EX);                                                                             \
    GSDFHandlerUniform *const name = reinterpret_cast<GSDFHandlerUniform *>(_##name);

// Forward declaration
struct GSDFHandler;
struct GSDFHandlerUniform;

#define GSDF_ENABLE_HWSEQ (CUPVA_PVA_GEN_NUMBER > 2)

namespace cupva { namespace device { namespace priv {
template<typename Handler, typename... Args>
inline bool gsdfHwseqEnabled(Handler const *handler, Args const &...)
{
    return handler->params.useHwseq == 1U;
}
}}} // namespace cupva::device::priv

#if GSDF_ENABLE_HWSEQ
#    define GSDF_IMPL_FUNC(_a, ...)                             \
        if (cupva::device::priv::gsdfHwseqEnabled(__VA_ARGS__)) \
        {                                                       \
            cupvaGSDFHwseq##_a##Priv(__VA_ARGS__);              \
        }                                                       \
        else                                                    \
        {                                                       \
            cupvaGSDFSwseq##_a##Priv(__VA_ARGS__);              \
        }

#    define GSDF_IMPL_FUNC_RET(_a, _b, ...)                         \
        {                                                           \
            if (cupva::device::priv::gsdfHwseqEnabled(__VA_ARGS__)) \
            {                                                       \
                _b = cupvaGSDFHwseq##_a##Priv(__VA_ARGS__);         \
            }                                                       \
            else                                                    \
            {                                                       \
                _b = cupvaGSDFSwseq##_a##Priv(__VA_ARGS__);         \
            }                                                       \
        }
#else
#    define GSDF_IMPL_FUNC(_a, ...) cupvaGSDFSwseq##_a##Priv(__VA_ARGS__)
#    define GSDF_IMPL_FUNC_RET(_a, _b, ...)             \
        {                                               \
            _b = cupvaGSDFSwseq##_a##Priv(__VA_ARGS__); \
        }
#endif

struct GSDFBufferMetadata
{
    uint32_t vmemStride;
    uint16_t vmemLinePitch;
    uint8_t extFormat;
    uint8_t extIsRaw;
    uint8_t extTransMode;
};

#if CUPVA_BUILD_MODE == CUPVA_NATIVE
// In GSDF, vector load is used to read the offsets. The ALIGNED_COPY macro is needed to prevent out of
// bound errors when ASAN is on in native mode.
#    define ALIGNED_COPY(_x, _type, _size)                                         \
        _type _x##_aligned[GSDF_MAX_NUM_TILES];                                    \
        if (_x != NULL)                                                            \
        {                                                                          \
            (void)memcpy((void *)_x##_aligned, (void *)_x, _size * sizeof(_type)); \
            _x = _x##_aligned;                                                     \
        }
#else
#    define ALIGNED_COPY(_x, _type, _size)
#endif

#endif // CUPVA_GSDF_IMPL_COMMON_H