Skip to content

Commit

Permalink
rendervulkan: add facilities for vulkan profiling w/ Tracy profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkautarch committed Jul 12, 2024
1 parent 235b257 commit 9acbbad
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ libdecor_dep = dependency('libdecor-0')
tracy_public_deps = []
tracy_public_args = []
if get_option('tracy_enable')
tracy_public_args += ['-DTRACY_ENABLE']
tracy_public_args += ['-DTRACY_ENABLE', '-DTRACY_VK_USE_SYMBOL_TABLE']

if get_option('tracy_on_demand')
tracy_public_args += ['-DTRACY_ON_DEMAND']
Expand Down Expand Up @@ -73,7 +73,7 @@ if get_option('tracy_enable')
endif

tracy_source = ['../thirdparty/tracy/public/TracyClient.cpp']
tracy_include = include_directories(['../thirdparty/tracy/public'])
tracy_include = include_directories(['../thirdparty/tracy/public', '../thirdparty/tracy/public/tracy'])

tracy_dep = declare_dependency(
compile_args: ['-Wno-sign-compare'] + tracy_public_args,
Expand Down
42 changes: 33 additions & 9 deletions src/rendervulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,9 +1196,26 @@ int32_t CVulkanDevice::findMemoryType( VkMemoryPropertyFlags properties, uint32_
return -1;
}

std::unique_ptr<CVulkanCmdBuffer> CVulkanDevice::commandBuffer()
inline std::unique_ptr<CVulkanCmdBuffer> __attribute__((hot,visibility("internal"))) CVulkanDevice::commandBuffer([[maybe_unused]] const std::source_location& loc)
{
std::unique_ptr<CVulkanCmdBuffer> cmdBuffer;
auto finalize_buf = [this, &loc]<bool bIsRecycled>(std::unique_ptr<CVulkanCmdBuffer> cmdBuf) {
//using this lambda allows for Named Return Value Optimization (NRVO) w/o duplicating code
if constexpr (bIsRecycled)
m_unusedCmdBufs.pop_back();

cmdBuf->begin();

#ifdef TRACY_ENABLE
assert( !cmdBuf->gpuZoneHolder() );
auto source_data = tracy::SourceLocationData(loc.function_name(), loc.function_name(), loc.file_name(), loc.line());

//we don't need to worry about dangling pointer to source_data,
//the gpu zone object held in gpuZoneHolder will only touch the pointer during its construction w/ emplace()
cmdBuf->gpuZoneHolder().emplace(cmdBuf->tracyCtx(), &source_data, cmdBuf->rawBuffer(), true);
#endif
return cmdBuf;
};

if (m_unusedCmdBufs.empty())
{
VkCommandBuffer rawCmdBuffer;
Expand All @@ -1216,16 +1233,12 @@ std::unique_ptr<CVulkanCmdBuffer> CVulkanDevice::commandBuffer()
return nullptr;
}

cmdBuffer = std::make_unique<CVulkanCmdBuffer>(this, rawCmdBuffer, queue(), queueFamily());
return finalize_buf.operator()<false>(std::make_unique<CVulkanCmdBuffer>(this, rawCmdBuffer, queue(), queueFamily()));
}
else
{
cmdBuffer = std::move(m_unusedCmdBufs.back());
m_unusedCmdBufs.pop_back();
return finalize_buf.operator()<true>(std::move(m_unusedCmdBufs.back()));
}

cmdBuffer->begin();
return cmdBuffer;
}

uint64_t CVulkanDevice::submitInternal( CVulkanCmdBuffer* cmdBuffer )
Expand Down Expand Up @@ -1318,13 +1331,22 @@ void CVulkanDevice::resetCmdBuffers(uint64_t sequence)
m_pendingCmdBufs.erase(m_pendingCmdBufs.begin(), ++last);
}



CVulkanCmdBuffer::CVulkanCmdBuffer(CVulkanDevice *parent, VkCommandBuffer cmdBuffer, VkQueue queue, uint32_t queueFamily)
: m_cmdBuffer(cmdBuffer), m_device(parent), m_queue(queue), m_queueFamily(queueFamily)
#ifdef TRACY_ENABLE
, m_tracyCtx{tracy::CreateVkContext(parent->instance(), parent->physDev(), parent->device(), queue, cmdBuffer, g_pfn_vkGetInstanceProcAddr, parent->vk.GetDeviceProcAddr)}
#endif
{
}

CVulkanCmdBuffer::~CVulkanCmdBuffer()
{
#ifdef TRACY_ENABLE
TracyVkDestroy(m_tracyCtx);
#endif

m_device->vk.FreeCommandBuffers(m_device->device(), m_device->commandPool(), 1, &m_cmdBuffer);
}

Expand All @@ -1333,6 +1355,9 @@ void CVulkanCmdBuffer::reset()
vk_check( m_device->vk.ResetCommandBuffer(m_cmdBuffer, 0) );
m_textureRefs.clear();
m_textureState.clear();
#ifdef TRACY_ENABLE
m_gpuZoneHolder.reset();
#endif
}

void CVulkanCmdBuffer::begin()
Expand Down Expand Up @@ -3349,7 +3374,6 @@ gamescope::OwningRc<CVulkanTexture> vulkan_create_texture_from_bits( uint32_t wi
memcpy( g_device.uploadBufferData(size), bits, size );

auto cmdBuffer = g_device.commandBuffer();

cmdBuffer->copyBufferToImage(g_device.uploadBuffer(), 0, 0, pTex.get());
// TODO: Sync this copyBufferToImage.

Expand Down
21 changes: 19 additions & 2 deletions src/rendervulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <bitset>
#include <mutex>
#include <optional>
#include <source_location>

#include "main.hpp"
#include "color_helpers.h"
Expand All @@ -18,6 +19,11 @@

#include "shaders/descriptor_set_constants.h"

namespace tracy { //forward declaration
class VkCtx;
class VkCtxScope;
}

class CVulkanCmdBuffer;

// 1: Fade Plane (Fade outs between switching focus)
Expand Down Expand Up @@ -720,7 +726,7 @@ class CVulkanDevice
VkSampler sampler(SamplerState key);
VkPipeline pipeline(ShaderType type, uint32_t layerCount = 1, uint32_t ycbcrMask = 0, uint32_t blur_layers = 0, uint32_t colorspace_mask = 0, uint32_t output_eotf = EOTF_Gamma22, bool itm_enable = false);
int32_t findMemoryType( VkMemoryPropertyFlags properties, uint32_t requiredTypeBits );
std::unique_ptr<CVulkanCmdBuffer> commandBuffer();
inline std::unique_ptr<CVulkanCmdBuffer> __attribute__((hot,visibility("internal"))) commandBuffer([[maybe_unused]] const std::source_location& loc = std::source_location::current());
uint64_t submit( std::unique_ptr<CVulkanCmdBuffer> cmdBuf);
uint64_t submitInternal( CVulkanCmdBuffer* cmdBuf );
void wait(uint64_t sequence, bool reset = true);
Expand Down Expand Up @@ -895,7 +901,14 @@ class CVulkanCmdBuffer

VkQueue queue() { return m_queue; }
uint32_t queueFamily() { return m_queueFamily; }


protected:
friend class CVulkanDevice;
#ifdef TRACY_ENABLE
inline std::optional<tracy::VkCtxScope>& gpuZoneHolder() {return m_gpuZoneHolder;}
inline tracy::VkCtx* tracyCtx() {return m_tracyCtx;}
#endif

private:
VkCommandBuffer m_cmdBuffer;
CVulkanDevice *m_device;
Expand All @@ -917,6 +930,10 @@ class CVulkanCmdBuffer
std::array<CVulkanTexture *, VKR_LUT3D_COUNT> m_lut3D;

uint32_t m_renderBufferOffset = 0;
#ifdef TRACY_ENABLE
tracy::VkCtx* m_tracyCtx;
std::optional<tracy::VkCtxScope> m_gpuZoneHolder;
#endif
};

uint32_t VulkanFormatToDRM( VkFormat vkFormat );
Expand Down
1 change: 1 addition & 0 deletions src/vulkan_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

#define VK_NO_PROTOTYPES
#include <vulkan/vulkan_core.h>
#include "tracy/TracyVulkan.hpp"

0 comments on commit 9acbbad

Please sign in to comment.