Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions include/vsg/app/CommandGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ namespace vsg
uint32_t maxSlot = 2;
int submitOrder = 0;

inline ref_ptr<RecordTraversal> getOrCreateRecordTraversal()
{
if (!recordTraversal) recordTraversal = RecordTraversal::create(maxSlot);
return recordTraversal;
}
ref_ptr<RecordTraversal> getOrCreateRecordTraversal();

ref_ptr<RecordTraversal> recordTraversal;

virtual VkCommandBufferLevel level() const;
virtual void reset();
virtual void record(ref_ptr<RecordedCommandBuffers> recordedCommandBuffers, ref_ptr<FrameStamp> frameStamp = {}, ref_ptr<DatabasePager> databasePager = {});

/// hook for assigning Instrumentation to enable profiling of record traversal.
ref_ptr<Instrumentation> instrumentation;

protected:
virtual ~CommandGraph();

Expand Down
3 changes: 3 additions & 0 deletions include/vsg/app/RecordAndSubmitTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace vsg

ref_ptr<DatabasePager> databasePager;

/// hook for assigning Instrumentation to enable profiling of record traversal.
ref_ptr<Instrumentation> instrumentation;

protected:
size_t _currentFrameIndex;
std::vector<size_t> _indices;
Expand Down
4 changes: 4 additions & 0 deletions include/vsg/app/RecordTraversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace vsg
class SpotLight;
class CommandGraph;
class RecordedCommandBuffers;
class Instrumentation;

VSG_type_name(vsg::RecordTraversal);

Expand Down Expand Up @@ -128,6 +129,9 @@ namespace vsg
// clear the bins to record a new frame.
void clearBins();

/// hook for assigning Instrumentation to enable profiling of record traversal.
ref_ptr<Instrumentation> instrumentation;

protected:
virtual ~RecordTraversal();

Expand Down
3 changes: 3 additions & 0 deletions include/vsg/app/Viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ namespace vsg
/// Call vkDeviceWaitIdle on all the devices associated with this Viewer
virtual void deviceWaitIdle() const;

/// hook for assigning Instrumentation to enable profiling of record traversal.
ref_ptr<Instrumentation> instrumentation;

protected:
virtual ~Viewer();

Expand Down
92 changes: 92 additions & 0 deletions include/vsg/utils/Instrumentation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once

/* <editor-fold desc="MIT License">

Copyright(c) 2023 Robert Osfield

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

</editor-fold> */

#include <vsg/core/Inherit.h>
#include <vsg/io/Logger.h>
#include <vsg/maths/vec4.h>

namespace vsg
{

#if defined ( __clang__ ) || defined ( __GNUC__ )
# define VsgFunctionName __PRETTY_FUNCTION__
#elif defined ( _MSC_VER )
# define VsgFunctionName __FUNCSIG__
#endif

class CommandBuffer;

/// SourceLocation structs mark the location in a source file when instrumentation is placed.
/// Memory layout was chosen to be compatible to Tracy's SourceLocationData object.
struct SourceLocation
{
const char* name;
const char* function;
const char* file;
uint32_t line;
ubvec4 color;
};

/// base class for Instrumentation implentations
class VSG_DECLSPEC Instrumentation : public Inherit<Object, Instrumentation>
{
public:

Instrumentation();

virtual void enter(const SourceLocation* sl, uint64_t& reference) const= 0;
virtual void leave(const SourceLocation* sl, uint64_t& reference) const = 0;

ref_ptr<vsg::CommandBuffer> commandBuffer;

protected:

virtual ~Instrumentation();

};
VSG_type_name(vsg::Instrumentation);

/// Concrete Implementation that uses VK_debug_utils to emit annotation to scene graph traversal.
/// Provides tools like RenderDoc a way to report the source location associated with Vulkan calls.
class VSG_DECLSPEC VulkanAnnotation : public Inherit<Instrumentation, VulkanAnnotation>
{
public:

VulkanAnnotation();

virtual void enter(const SourceLocation* sl, uint64_t& reference) const;
virtual void leave(const SourceLocation* sl, uint64_t& reference) const;

protected:

virtual ~VulkanAnnotation();

};
VSG_type_name(vsg::VulkanAnnotation);

struct ScopedInstrumentation
{
const Instrumentation* instr;
const SourceLocation* sl;
uint64_t reference;
inline ScopedInstrumentation(const Instrumentation* in_instr, const SourceLocation* in_sl) : instr(in_instr), sl(in_sl) { if (instr) instr->enter(sl, reference); }
inline ~ScopedInstrumentation() { if (instr) instr->leave(sl, reference); }
};

#define SCOPED_INSTRUMENTASTION(instrumentation) static constexpr SourceLocation s_source_location_##__LINE__ { nullptr, VsgFunctionName, __FILE__, __LINE__, ubvec4(255, 255, 255, 255) }; ScopedInstrumentation __scoped_instrumentation(instrumentation, &(s_source_location_##__LINE__));
#define SCOPED_INSTRUMENTASTION_N(instrumentation, name) static constexpr SourceLocation s_source_location_##__LINE__ { name, VsgFunctionName, __FILE__, __LINE__, ubvec4(255, 255, 255, 255) }; ScopedInstrumentation __scoped_instrumentation(instrumentation, &(s_source_location_##__LINE__));
#define SCOPED_INSTRUMENTASTION_C(instrumentation, color) static constexpr SourceLocation s_source_location_##__LINE__ { nullptr, VsgFunctionName, __FILE__, __LINE__, color }; ScopedInstrumentation __scoped_instrumentation(instrumentation, &(s_source_location_##__LINE__));
#define SCOPED_INSTRUMENTASTION_NC(instrumentation, name, color) static constexpr SourceLocation s_source_location_##__LINE__ { name, VsgFunctionName, __FILE__, __LINE__, color }; ScopedInstrumentation __scoped_instrumentation(instrumentation, &(s_source_location_##__LINE__));

} // namespace vsg
1 change: 1 addition & 0 deletions src/vsg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ set(SOURCES
utils/ShaderCompiler.cpp
utils/ComputeBounds.cpp
utils/Intersector.cpp
utils/Instrumentation.cpp
utils/LineSegmentIntersector.cpp
utils/LoadPagedLOD.cpp
)
Expand Down
39 changes: 39 additions & 0 deletions src/vsg/app/CommandGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/state/ViewDependentState.h>
#include <vsg/ui/ApplicationEvent.h>
#include <vsg/vk/State.h>
#include <vsg/utils/Instrumentation.h>

using namespace vsg;

CommandGraph::CommandGraph()
{
SCOPED_INSTRUMENTASTION(instrumentation);
}

CommandGraph::CommandGraph(ref_ptr<Device> in_device, int family) :
device(in_device),
queueFamily(family),
presentFamily(-1)
{
SCOPED_INSTRUMENTASTION(instrumentation);
}

CommandGraph::CommandGraph(ref_ptr<Window> in_window, ref_ptr<Node> child) :
window(in_window),
device(in_window->getOrCreateDevice())
{
SCOPED_INSTRUMENTASTION(instrumentation);

VkQueueFlags queueFlags = VK_QUEUE_GRAPHICS_BIT;
if (window->traits()) queueFlags = window->traits()->queueFlags;

Expand All @@ -45,6 +50,7 @@ CommandGraph::CommandGraph(ref_ptr<Window> in_window, ref_ptr<Node> child) :

CommandGraph::~CommandGraph()
{
SCOPED_INSTRUMENTASTION(instrumentation);
}

VkCommandBufferLevel CommandGraph::level() const
Expand All @@ -56,8 +62,28 @@ void CommandGraph::reset()
{
}

ref_ptr<RecordTraversal> CommandGraph::getOrCreateRecordTraversal()
{
SCOPED_INSTRUMENTASTION(instrumentation);

if (!recordTraversal)
{
recordTraversal = RecordTraversal::create(maxSlot);
if (!recordTraversal->instrumentation && window && window->traits() && window->traits()->apiDumpLayer)
{
recordTraversal->instrumentation = VulkanAnnotation::create();
}

info("CommandGraph::getOrCreateRecordTraversal() ", recordTraversal);
}
return recordTraversal;
}


void CommandGraph::record(ref_ptr<RecordedCommandBuffers> recordedCommandBuffers, ref_ptr<FrameStamp> frameStamp, ref_ptr<DatabasePager> databasePager)
{
SCOPED_INSTRUMENTASTION(instrumentation);

if (window && !window->visible())
{
return;
Expand Down Expand Up @@ -100,6 +126,7 @@ void CommandGraph::record(ref_ptr<RecordedCommandBuffers> recordedCommandBuffers

recordTraversal->getState()->_commandBuffer = commandBuffer;


// or select index when maps to a dormant CommandBuffer
VkCommandBuffer vk_commandBuffer = *commandBuffer;

Expand All @@ -112,10 +139,22 @@ void CommandGraph::record(ref_ptr<RecordedCommandBuffers> recordedCommandBuffers

vkBeginCommandBuffer(vk_commandBuffer, &beginInfo);

if (recordTraversal->instrumentation)
{
// attach the commandBuffer to instrumentation so it can be recorded to if required.
recordTraversal->instrumentation->commandBuffer = commandBuffer;
}

traverse(*recordTraversal);

vkEndCommandBuffer(vk_commandBuffer);

if (recordTraversal->instrumentation)
{
// disconnect the commandBuffer from instrumentation as it's no longer valid for recording commands to
recordTraversal->instrumentation->commandBuffer = {};
}

recordedCommandBuffers->add(submitOrder, commandBuffer);
}

Expand Down
13 changes: 13 additions & 0 deletions src/vsg/app/RecordAndSubmitTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/io/Logger.h>
#include <vsg/ui/ApplicationEvent.h>
#include <vsg/vk/State.h>
#include <vsg/utils/Instrumentation.h>

using namespace vsg;

RecordAndSubmitTask::RecordAndSubmitTask(Device* in_device, uint32_t numBuffers) :
device(in_device)
{
SCOPED_INSTRUMENTASTION(instrumentation);

_currentFrameIndex = numBuffers; // numBuffers is used to signify unset value
for (uint32_t i = 0; i < numBuffers; ++i)
{
Expand All @@ -42,6 +45,8 @@ RecordAndSubmitTask::RecordAndSubmitTask(Device* in_device, uint32_t numBuffers)

void RecordAndSubmitTask::advance()
{
SCOPED_INSTRUMENTASTION(instrumentation);

if (_currentFrameIndex >= _indices.size())
{
// first frame so set to 0
Expand Down Expand Up @@ -80,6 +85,8 @@ Fence* RecordAndSubmitTask::fence(size_t relativeFrameIndex)

VkResult RecordAndSubmitTask::submit(ref_ptr<FrameStamp> frameStamp)
{
SCOPED_INSTRUMENTASTION(instrumentation);

if (VkResult result = start(); result != VK_SUCCESS) return result;

if (earlyTransferTask)
Expand All @@ -96,6 +103,8 @@ VkResult RecordAndSubmitTask::submit(ref_ptr<FrameStamp> frameStamp)

VkResult RecordAndSubmitTask::start()
{
SCOPED_INSTRUMENTASTION(instrumentation);

if (earlyTransferTask) earlyTransferTask->currentTransferCompletedSemaphore = {};
if (lateTransferTask) lateTransferTask->currentTransferCompletedSemaphore = {};

Expand All @@ -112,6 +121,8 @@ VkResult RecordAndSubmitTask::start()

VkResult RecordAndSubmitTask::record(ref_ptr<RecordedCommandBuffers> recordedCommandBuffers, ref_ptr<FrameStamp> frameStamp)
{
SCOPED_INSTRUMENTASTION(instrumentation);

for (auto& commandGraph : commandGraphs)
{
commandGraph->record(recordedCommandBuffers, frameStamp, databasePager);
Expand All @@ -122,6 +133,8 @@ VkResult RecordAndSubmitTask::record(ref_ptr<RecordedCommandBuffers> recordedCom

VkResult RecordAndSubmitTask::finish(ref_ptr<RecordedCommandBuffers> recordedCommandBuffers)
{
SCOPED_INSTRUMENTASTION(instrumentation);

if (lateTransferTask)
{
if (VkResult result = lateTransferTask->transferDynamicData(); result != VK_SUCCESS) return result;
Expand Down
Loading