<commit_before>//==============================================================================
/// Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file   vktTimestampedCmdBuf.cpp
/// \brief  A wrapper for internal command buffer which holds 1 timestamp.
//==============================================================================

#include "vktTimestampedCmdBuf.h"

//-----------------------------------------------------------------------------
/// Static method that instantiates a VktTimestampedCmdBuf.
/// \param config A configuration structure.
/// \returns A new VktTimestampedCmdBuf instance.
//-----------------------------------------------------------------------------
VktTimestampedCmdBuf* VktTimestampedCmdBuf::Create(const TimestampedCmdBufConfig& config)
{
    VktTimestampedCmdBuf* pOut = new VktTimestampedCmdBuf();

    if (pOut != nullptr)
    {
        if (pOut->Init(config) != VK_SUCCESS)
        {
            delete pOut;
            pOut = nullptr;
        }
    }

    return pOut;
}
//-----------------------------------------------------------------------------
/// Constructor.
//-----------------------------------------------------------------------------
VktTimestampedCmdBuf::VktTimestampedCmdBuf() :
    m_pInstanceDT(nullptr),
    m_pDeviceDT(nullptr),
    m_cmdPool(VK_NULL_HANDLE),
    m_cmdBuf(VK_NULL_HANDLE)
{
}

//-----------------------------------------------------------------------------
/// Destructor.
//-----------------------------------------------------------------------------
VktTimestampedCmdBuf::~VktTimestampedCmdBuf()
{
    if (m_gpuRes.timestampQueryPool != NULL)
    {
        m_pDeviceDT->DestroyQueryPool(m_config.device, m_gpuRes.timestampQueryPool, nullptr);
        m_gpuRes.timestampQueryPool = NULL;
    }

    if (m_gpuRes.timestampBuffer != NULL)
    {
        m_pDeviceDT->DestroyBuffer(m_config.device, m_gpuRes.timestampBuffer, nullptr);
        m_gpuRes.timestampBuffer = NULL;
    }

    if (m_gpuRes.timestampMem != NULL)
    {
        m_pDeviceDT->FreeMemory(m_config.device, m_gpuRes.timestampMem, nullptr);
        m_gpuRes.timestampMem = NULL;
    }

    if (m_cmdBuf != VK_NULL_HANDLE)
    {
        m_pDeviceDT->FreeCommandBuffers(m_config.device, m_cmdPool, 1, &m_cmdBuf);
        m_cmdBuf = VK_NULL_HANDLE;
    }
}

//-----------------------------------------------------------------------------
/// Perform all initialization.
/// \param config A pointer to a profiler configuration structure.
/// \returns The result code for initialization.
//-----------------------------------------------------------------------------
VkResult VktTimestampedCmdBuf::Init(const TimestampedCmdBufConfig& config)   ///< [in] Pointer to profiler configuration
{
    VkResult result = VK_INCOMPLETE;

    memcpy(&m_config, &config, sizeof(m_config));

    if ((m_config.physicalDevice != VK_NULL_HANDLE) && (m_config.device != VK_NULL_HANDLE))
    {
        m_pInstanceDT = instance_dispatch_table(config.physicalDevice);
        m_pDeviceDT = device_dispatch_table(config.device);

        m_pInstanceDT->GetPhysicalDeviceMemoryProperties(m_config.physicalDevice, &m_memProps);

        // Create command pool
        VkCommandPoolCreateInfo cmdPoolCreateInfo = {};
        cmdPoolCreateInfo.sType            = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
        cmdPoolCreateInfo.pNext            = nullptr;
        cmdPoolCreateInfo.queueFamilyIndex = m_config.queueFamilyIndex;
        cmdPoolCreateInfo.flags            = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
        result = m_pDeviceDT->CreateCommandPool(m_config.device, &cmdPoolCreateInfo, nullptr, &m_cmdPool);

        // Create command buffer
        if (result == VK_SUCCESS)
        {
            VkCommandBufferAllocateInfo cmdBufAllocInfo = {};
            cmdBufAllocInfo.sType              = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
            cmdBufAllocInfo.pNext              = nullptr;
            cmdBufAllocInfo.commandPool        = m_cmdPool;
            cmdBufAllocInfo.level              = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
            cmdBufAllocInfo.commandBufferCount = 1;
            result = m_pDeviceDT->AllocateCommandBuffers(m_config.device, &cmdBufAllocInfo, &m_cmdBuf);
        }

        // Create query pool
        if (result == VK_SUCCESS)
        {
            VkQueryPoolCreateInfo queryPoolCreateInfo = {};
            queryPoolCreateInfo.sType      = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
            queryPoolCreateInfo.pNext      = nullptr;
            queryPoolCreateInfo.queryType  = VK_QUERY_TYPE_TIMESTAMP;
            queryPoolCreateInfo.queryCount = 1;
            result = m_pDeviceDT->CreateQueryPool(m_config.device, &queryPoolCreateInfo, nullptr, &m_gpuRes.timestampQueryPool);
        }

        // Create timestamp buffer
        if (result == VK_SUCCESS)
        {
            VkBufferCreateInfo bufferCreateInfo = {};
            bufferCreateInfo.sType                 = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
            bufferCreateInfo.pNext                 = nullptr;
            bufferCreateInfo.usage                 = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
            bufferCreateInfo.size                  = sizeof(UINT64);
            bufferCreateInfo.queueFamilyIndexCount = 0;
            bufferCreateInfo.pQueueFamilyIndices   = nullptr;
            bufferCreateInfo.sharingMode           = VK_SHARING_MODE_EXCLUSIVE;
            bufferCreateInfo.flags                 = 0;
            result = m_pDeviceDT->CreateBuffer(m_config.device, &bufferCreateInfo, nullptr, &m_gpuRes.timestampBuffer);

            if (result == VK_SUCCESS)
            {
                VkMemoryRequirements memReqs = {};
                m_pDeviceDT->GetBufferMemoryRequirements(m_config.device, m_gpuRes.timestampBuffer, &memReqs);

                VkMemoryAllocateInfo allocInfo = {};
                allocInfo.sType          = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
                allocInfo.pNext          = nullptr;
                allocInfo.allocationSize = memReqs.size;

                result = MemTypeFromProps(
                    memReqs.memoryTypeBits,
                    VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
                    &allocInfo.memoryTypeIndex);

                if (result == VK_SUCCESS)
                {
                    result = m_pDeviceDT->AllocateMemory(m_config.device, &allocInfo, nullptr, &m_gpuRes.timestampMem);

                    if (result == VK_SUCCESS)
                    {
                        result = m_pDeviceDT->BindBufferMemory(m_config.device, m_gpuRes.timestampBuffer, m_gpuRes.timestampMem, 0);

                        if (result == VK_SUCCESS)
                        {
                            void* pMappedMem = nullptr;

                            result = m_pDeviceDT->MapMemory(
                                m_config.device,
                                m_gpuRes.timestampMem,
                                0,
                                VK_WHOLE_SIZE,
                                0,
                                &pMappedMem);

                            if (result == VK_SUCCESS)
                            {
                                memset(pMappedMem, 0, memReqs.size);<commit_msg>Implement Vulkan timestamp calibration for GPU/CPU sync.<commit_after>//==============================================================================
/// Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file   vktTimestampedCmdBuf.cpp
/// \brief  A wrapper for internal command buffer which holds 1 timestamp.
//==============================================================================

#include "vktTimestampedCmdBuf.h"

//-----------------------------------------------------------------------------
/// Static method that instantiates a VktTimestampedCmdBuf.
/// \param config A configuration structure.
/// \returns A new VktTimestampedCmdBuf instance.
//-----------------------------------------------------------------------------
VktTimestampedCmdBuf* VktTimestampedCmdBuf::Create(const TimestampedCmdBufConfig& config)
{
    VktTimestampedCmdBuf* pOut = new VktTimestampedCmdBuf();

    if (pOut != nullptr)
    {
        if (pOut->Init(config) != VK_SUCCESS)
        {
            delete pOut;
            pOut = nullptr;
        }
    }

    return pOut;
}
//-----------------------------------------------------------------------------
/// Constructor.
//-----------------------------------------------------------------------------
VktTimestampedCmdBuf::VktTimestampedCmdBuf() :
    m_pInstanceDT(nullptr),
    m_pDeviceDT(nullptr),
    m_cmdPool(VK_NULL_HANDLE),
    m_cmdBuf(VK_NULL_HANDLE)
{
}

//-----------------------------------------------------------------------------
/// Destructor.
//-----------------------------------------------------------------------------
VktTimestampedCmdBuf::~VktTimestampedCmdBuf()
{
    if (m_gpuRes.timestampQueryPool != NULL)
    {
        m_pDeviceDT->DestroyQueryPool(m_config.device, m_gpuRes.timestampQueryPool, nullptr);
        m_gpuRes.timestampQueryPool = NULL;
    }

    if (m_gpuRes.timestampBuffer != NULL)
    {
        m_pDeviceDT->DestroyBuffer(m_config.device, m_gpuRes.timestampBuffer, nullptr);
        m_gpuRes.timestampBuffer = NULL;
    }

    if (m_gpuRes.timestampMem != NULL)
    {
        m_pDeviceDT->FreeMemory(m_config.device, m_gpuRes.timestampMem, nullptr);
        m_gpuRes.timestampMem = NULL;
    }

    if (m_cmdBuf != VK_NULL_HANDLE)
    {
        m_pDeviceDT->FreeCommandBuffers(m_config.device, m_cmdPool, 1, &m_cmdBuf);
        m_cmdBuf = VK_NULL_HANDLE;
    }
}

//-----------------------------------------------------------------------------
/// Perform all initialization.
/// \param config A pointer to a profiler configuration structure.
/// \returns The result code for initialization.
//-----------------------------------------------------------------------------
VkResult VktTimestampedCmdBuf::Init(const TimestampedCmdBufConfig& config)   ///< [in] Pointer to profiler configuration
{
    VkResult result = VK_INCOMPLETE;

    memcpy(&m_config, &config, sizeof(m_config));

    if ((m_config.physicalDevice != VK_NULL_HANDLE) && (m_config.device != VK_NULL_HANDLE))
    {
        m_pInstanceDT = instance_dispatch_table(config.physicalDevice);
        m_pDeviceDT = device_dispatch_table(config.device);

        m_pInstanceDT->GetPhysicalDeviceMemoryProperties(m_config.physicalDevice, &m_memProps);

        // Create command pool
        VkCommandPoolCreateInfo cmdPoolCreateInfo = {};
        cmdPoolCreateInfo.sType            = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
        cmdPoolCreateInfo.pNext            = nullptr;
        cmdPoolCreateInfo.queueFamilyIndex = m_config.queueFamilyIndex;
        cmdPoolCreateInfo.flags            = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
        result = m_pDeviceDT->CreateCommandPool(m_config.device, &cmdPoolCreateInfo, nullptr, &m_cmdPool);

        // Create command buffer
        if (result == VK_SUCCESS)
        {
            VkCommandBufferAllocateInfo cmdBufAllocInfo = {};
            cmdBufAllocInfo.sType              = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
            cmdBufAllocInfo.pNext              = nullptr;
            cmdBufAllocInfo.commandPool        = m_cmdPool;
            cmdBufAllocInfo.level              = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
            cmdBufAllocInfo.commandBufferCount = 1;
            result = m_pDeviceDT->AllocateCommandBuffers(m_config.device, &cmdBufAllocInfo, &m_cmdBuf);
        }

        // Create query pool
        if (result == VK_SUCCESS)
        {
            VkQueryPoolCreateInfo queryPoolCreateInfo = {};
            queryPoolCreateInfo.sType      = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
            queryPoolCreateInfo.pNext      = nullptr;
            queryPoolCreateInfo.queryType  = VK_QUERY_TYPE_TIMESTAMP;
            queryPoolCreateInfo.queryCount = 1;
            result = m_pDeviceDT->CreateQueryPool(m_config.device, &queryPoolCreateInfo, nullptr, &m_gpuRes.timestampQueryPool);
        }

        // Create timestamp buffer
        if (result == VK_SUCCESS)
        {
            VkBufferCreateInfo bufferCreateInfo = {};
            bufferCreateInfo.sType                 = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
            bufferCreateInfo.pNext                 = nullptr;
            bufferCreateInfo.usage                 = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
            bufferCreateInfo.size                  = sizeof(UINT64);
            bufferCreateInfo.queueFamilyIndexCount = 0;
            bufferCreateInfo.pQueueFamilyIndices   = nullptr;
            bufferCreateInfo.sharingMode           = VK_SHARING_MODE_EXCLUSIVE;
            bufferCreateInfo.flags                 = 0;
            result = m_pDeviceDT->CreateBuffer(m_config.device, &bufferCreateInfo, nullptr, &m_gpuRes.timestampBuffer);

            if (result == VK_SUCCESS)
            {
                VkMemoryRequirements memReqs = {};
                m_pDeviceDT->GetBufferMemoryRequirements(m_config.device, m_gpuRes.timestampBuffer, &memReqs);

                VkMemoryAllocateInfo allocInfo = {};
                allocInfo.sType          = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
                allocInfo.pNext          = nullptr;
                allocInfo.allocationSize = memReqs.size;

                result = MemTypeFromProps(
                    memReqs.memoryTypeBits,
                    VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
                    &allocInfo.memoryTypeIndex);

                if (result == VK_SUCCESS)
                {
                    result = m_pDeviceDT->AllocateMemory(m_config.device, &allocInfo, nullptr, &m_gpuRes.timestampMem);

                    if (result == VK_SUCCESS)
                    {
                        result = m_pDeviceDT->BindBufferMemory(m_config.device, m_gpuRes.timestampBuffer, m_gpuRes.timestampMem, 0);

                        if (result == VK_SUCCESS)
                        {
                            void* pMappedMem = nullptr;

                            result = m_pDeviceDT->MapMemory(
                                m_config.device,
                                m_gpuRes.timestampMem,
                                0,
                                VK_WHOLE_SIZE,
                                0,
                                &pMappedMem);

                            if (result == VK_SUCCESS)
                            {
                                memset(pMappedMem, 0, (size_t)memReqs.size);
{'fraction_non_alphanumeric': 0.17849623038782858, 'fraction_numerical': 0.0021734700808259187, 'mean_word_length': 1.6372917786136485, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773835', 'n_tokens_mistral': 3885, 'n_tokens_neox': 3761, 'n_words': 603}