vsg: include/vsg/utils/Instrumentation.h Source File

vsg 1.1.8 VulkanSceneGraph library

Loading...
Searching...
No Matches
Instrumentation.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2023 Robert Osfield
6
7Permission 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:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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.
12
13</editor-fold> */
14
15#include <vsg/core/Inherit.h>
16#include <vsg/core/Version.h>
17#include <vsg/io/Logger.h>
18#include <vsg/maths/vec4.h>
19
20namespace vsg
21{
22
23 // forward declare
24 class CommandBuffer;
25 class FrameStamp;
26
28 struct uint_color
29 {
30 // BGRA order required to map to Tracy's uint32_t color value
31 uint8_t b = 255, g = 255, r = 255, a = 255;
32 constexpr uint_color() = default;
33 constexpr uint_color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) :
34 b(blue), g(green), r(red), a(alpha) {}
35 };
36
39 struct SourceLocation
40 {
41 const char* name;
42 const char* function;
43 const char* file;
44 uint32_t line;
45 uint_color color;
46 uint32_t level;
47 };
48
50 class VSG_DECLSPEC Instrumentation : public Inherit<Object, Instrumentation>
51 {
52 public:
53 Instrumentation();
54
55 virtual ref_ptr<Instrumentation> shareOrDuplicateForThreadSafety() { return ref_ptr<Instrumentation>(this); }
56
57 virtual void setThreadName(const std::string& /*name*/) const {};
58
59 virtual void enterFrame(const SourceLocation* /*sl*/, uint64_t& /*reference*/, FrameStamp& /*frameStamp*/) const {};
60 virtual void leaveFrame(const SourceLocation* /*sl*/, uint64_t& /*reference*/, FrameStamp& /*frameStamp*/) const {};
61
62 virtual void enter(const SourceLocation* /*sl*/, uint64_t& /*reference*/, const Object* /*object*/ = nullptr) const {};
63 virtual void leave(const SourceLocation* /*sl*/, uint64_t& /*reference*/, const Object* /*object*/ = nullptr) const {};
64
65 virtual void enterCommandBuffer(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/) const {};
66 virtual void leaveCommandBuffer(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/) const {};
67
68 virtual void enter(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/, const Object* /*object*/ = nullptr) const {};
69 virtual void leave(const SourceLocation* /*sl*/, uint64_t& /*reference*/, CommandBuffer& /*commandBuffer*/, const Object* /*object*/ = nullptr) const {};
70
71 virtual void finish() const {};
72
73 protected:
74 virtual ~Instrumentation();
75 };
76 VSG_type_name(vsg::Instrumentation);
77
79 extern ref_ptr<Instrumentation> shareOrDuplicateForThreadSafety(ref_ptr<Instrumentation> instrumentation);
80
81 struct CpuInstrumentation
82 {
83 const Instrumentation* instr;
84 const SourceLocation* sl;
85 uint64_t reference;
86 const Object* object;
87
88 inline CpuInstrumentation(const Instrumentation* in_instr, const SourceLocation* in_sl, const Object* in_object) :
89 instr(in_instr), sl(in_sl), reference(0), object(in_object)
90 {
91 if (instr) instr->enter(sl, reference, object);
92 }
93 inline ~CpuInstrumentation()
94 {
95 if (instr) instr->leave(sl, reference, object);
96 }
97 };
98
99 struct GpuInstrumentation
100 {
101 const Instrumentation* instr;
102 const SourceLocation* sl;
103 uint64_t reference;
104 CommandBuffer& commandBuffer;
105 const Object* object;
106
107 inline GpuInstrumentation(const Instrumentation* in_instr, const SourceLocation* in_sl, CommandBuffer& in_commandBuffer, const Object* in_object) :
108 instr(in_instr), sl(in_sl), reference(0), commandBuffer(in_commandBuffer), object(in_object)
109 {
110 if (instr) instr->enter(sl, reference, commandBuffer, object);
111 }
112 inline ~GpuInstrumentation()
113 {
114 if (instr) instr->leave(sl, reference, commandBuffer, object);
115 }
116 };
117
118 struct CommandBufferInstrumentation
119 {
120 const Instrumentation* instr;
121 const SourceLocation* sl;
122 uint64_t reference;
123 CommandBuffer& commandBuffer;
124
125 inline CommandBufferInstrumentation(const Instrumentation* in_instr, const SourceLocation* in_sl, CommandBuffer& in_commandBuffer) :
126 instr(in_instr), sl(in_sl), reference(0), commandBuffer(in_commandBuffer)
127 {
128 if (instr) instr->enterCommandBuffer(sl, reference, commandBuffer);
129 }
130 inline ~CommandBufferInstrumentation()
131 {
132 if (instr) instr->leaveCommandBuffer(sl, reference, commandBuffer);
133 }
134 };
135
136// standard colours specified in {r, g, b, a} ordering
137#define COLOR_DEFAULT vsg::uint_color(255, 255, 255, 255)
138#define COLOR_VIEWER vsg::uint_color(127, 240, 240, 255)
139#define COLOR_UPDATE vsg::uint_color(0, 255, 0, 255)
140#define COLOR_GPU vsg::uint_color(255, 127, 0, 255)
141#define COLOR_RECORD_L1 vsg::uint_color(140, 247, 0, 255)
142#define COLOR_RECORD_L2 vsg::uint_color(176, 176, 0, 255)
143#define COLOR_RECORD_L3 COLOR_GPU
144#define COLOR_RECORD COLOR_RECORD_L1
145#define COLOR_COMPILE vsg::uint_color(255, 249, 64, 255)
146#define COLOR_PAGER vsg::uint_color(240, 255, 64, 255)
147#define COLOR_READ vsg::uint_color(0, 255, 128, 255)
148#define COLOR_WRITE vsg::uint_color(0, 128, 255, 255)
149
150#if defined(__clang__) || defined(__GNUC__)
151# define VsgFunctionName __PRETTY_FUNCTION__
152#elif defined(_MSC_VER)
153# define VsgFunctionName __FUNCSIG__
154#endif
155
156#define __CPU_INSTRUMENTATION(level, instrumentation, name, color, object) \
157 static constexpr vsg::SourceLocation s_cpu_source_location_##__LINE__{name, VsgFunctionName, __FILE__, __LINE__, color, level}; \
158 vsg::CpuInstrumentation __cpu_scoped_instrumentation_##__LINE__(instrumentation, &(s_cpu_source_location_##__LINE__), object);
159
160#define __GPU_INSTRUMENTATION(level, instrumentation, cg, name, color, object) \
161 static constexpr vsg::SourceLocation s_gpu_source_location_##__LINE__{name, VsgFunctionName, __FILE__, __LINE__, color, level}; \
162 vsg::GpuInstrumentation __gpu_scoped_instrumentation_##__LINE__(instrumentation, &(s_gpu_source_location_##__LINE__), cg, object);
163
164#define __COMMAND_BUFFER_INSTRUMENTATION(level, instrumentation, cg, name, color) \
165 static constexpr vsg::SourceLocation s_cg_source_location_##__LINE__{name, VsgFunctionName, __FILE__, __LINE__, color, level}; \
166 vsg::CommandBufferInstrumentation __cg_scoped_instrumentation_##__LINE__(instrumentation, &(s_cg_source_location_##__LINE__), cg);
167
168#if VSG_MAX_INSTRUMENTATION_LEVEL >= 1
169
170# define CPU_INSTRUMENTATION_L1(instrumentation) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, COLOR_DEFAULT, nullptr)
171# define CPU_INSTRUMENTATION_L1_N(instrumentation, name) __CPU_INSTRUMENTATION(1, instrumentation, name, COLOR_DEFAULT, nullptr)
172# define CPU_INSTRUMENTATION_L1_C(instrumentation, color) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, color, nullptr)
173# define CPU_INSTRUMENTATION_L1_NC(instrumentation, name, color) __CPU_INSTRUMENTATION(1, instrumentation, name, color, nullptr)
174
175# define GPU_INSTRUMENTATION_L1(instrumentation, cg) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, COLOR_DEFAULT, nullptr)
176# define GPU_INSTRUMENTATION_L1_N(instrumentation, cg, name) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, COLOR_DEFAULT, nullptr)
177# define GPU_INSTRUMENTATION_L1_C(instrumentation, cg, color) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, color, nullptr)
178# define GPU_INSTRUMENTATION_L1_NC(instrumentation, cg, name, color) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, color, nullptr)
179
180# define CPU_INSTRUMENTATION_L1_O(instrumentation, object) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, COLOR_DEFAULT, object)
181# define CPU_INSTRUMENTATION_L1_NO(instrumentation, name, object) __CPU_INSTRUMENTATION(1, instrumentation, name, COLOR_DEFAULT, object)
182# define CPU_INSTRUMENTATION_L1_CO(instrumentation, color, object) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, color, object)
183# define CPU_INSTRUMENTATION_L1_NCO(instrumentation, name, color, object) __CPU_INSTRUMENTATION(1, instrumentation, name, color, object)
184
185# define GPU_INSTRUMENTATION_L1_O(instrumentation, cg, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, COLOR_DEFAULT, object)
186# define GPU_INSTRUMENTATION_L1_NO(instrumentation, cg, name, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, COLOR_DEFAULT, object)
187# define GPU_INSTRUMENTATION_L1_CO(instrumentation, cg, color, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, color, object)
188# define GPU_INSTRUMENTATION_L1_NCO(instrumentation, cg, name, color, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, color, object)
189
190# define COMMAND_BUFFER_INSTRUMENTATION(instrumentation, cg, name, color) __COMMAND_BUFFER_INSTRUMENTATION(1, instrumentation, cg, name, color)
191
192#else
193
194# define CPU_INSTRUMENTATION_L1(instrumentation)
195# define CPU_INSTRUMENTATION_L1_N(instrumentation, name)
196# define CPU_INSTRUMENTATION_L1_C(instrumentation, color)
197# define CPU_INSTRUMENTATION_L1_NC(instrumentation, name, color)
198
199# define GPU_INSTRUMENTATION_L1(instrumentation, cg)
200# define GPU_INSTRUMENTATION_L1_N(instrumentation, cg, name)
201# define GPU_INSTRUMENTATION_L1_C(instrumentation, cg, color)
202# define GPU_INSTRUMENTATION_L1_NC(instrumentation, cg, name, color)
203
204# define CPU_INSTRUMENTATION_L1_O(instrumentation, object)
205# define CPU_INSTRUMENTATION_L1_NO(instrumentation, name, object)
206# define CPU_INSTRUMENTATION_L1_CO(instrumentation, color, object)
207# define CPU_INSTRUMENTATION_L1_NCO(instrumentation, name, color, object)
208
209# define GPU_INSTRUMENTATION_L1_O(instrumentation, cg, object)
210# define GPU_INSTRUMENTATION_L1_NO(instrumentation, cg, name, object)
211# define GPU_INSTRUMENTATION_L1_CO(instrumentation, cg, color, object)
212# define GPU_INSTRUMENTATION_L1_NCO(instrumentation, cg, name, color, object)
213
214# define COMMAND_BUFFER_INSTRUMENTATION(instrumentation, cg, name, color)
215
216#endif
217
218#if VSG_MAX_INSTRUMENTATION_LEVEL >= 2
219
220# define CPU_INSTRUMENTATION_L2(instrumentation) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, COLOR_DEFAULT, nullptr)
221# define CPU_INSTRUMENTATION_L2_N(instrumentation, name) __CPU_INSTRUMENTATION(2, instrumentation, name, COLOR_DEFAULT, nullptr)
222# define CPU_INSTRUMENTATION_L2_C(instrumentation, color) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, color, nullptr)
223# define CPU_INSTRUMENTATION_L2_NC(instrumentation, name, color) __CPU_INSTRUMENTATION(2, instrumentation, name, color, nullptr)
224
225# define GPU_INSTRUMENTATION_L2(instrumentation, cg) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, COLOR_DEFAULT, nullptr)
226# define GPU_INSTRUMENTATION_L2_N(instrumentation, cg, name) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, COLOR_DEFAULT, nullptr)
227# define GPU_INSTRUMENTATION_L2_C(instrumentation, cg, color) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, color, nullptr)
228# define GPU_INSTRUMENTATION_L2_NC(instrumentation, cg, name, color) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, color, nullptr)
229
230# define CPU_INSTRUMENTATION_L2_O(instrumentation, object) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, COLOR_DEFAULT, object)
231# define CPU_INSTRUMENTATION_L2_NO(instrumentation, name, object) __CPU_INSTRUMENTATION(2, instrumentation, name, COLOR_DEFAULT, object)
232# define CPU_INSTRUMENTATION_L2_CO(instrumentation, color, object) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, color, object)
233# define CPU_INSTRUMENTATION_L2_NCO(instrumentation, name, color, object) __CPU_INSTRUMENTATION(2, instrumentation, name, color, object)
234
235# define GPU_INSTRUMENTATION_L2_O(instrumentation, cg, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, COLOR_DEFAULT, object)
236# define GPU_INSTRUMENTATION_L2_NO(instrumentation, cg, name, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, COLOR_DEFAULT, object)
237# define GPU_INSTRUMENTATION_L2_CO(instrumentation, cg, color, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, color, object)
238# define GPU_INSTRUMENTATION_L2_NCO(instrumentation, cg, name, color, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, color, object)
239
240#else
241
242# define CPU_INSTRUMENTATION_L2(instrumentation)
243# define CPU_INSTRUMENTATION_L2_N(instrumentation, name)
244# define CPU_INSTRUMENTATION_L2_C(instrumentation, color)
245# define CPU_INSTRUMENTATION_L2_NC(instrumentation, name, color)
246
247# define GPU_INSTRUMENTATION_L2(instrumentation, cg)
248# define GPU_INSTRUMENTATION_L2_N(instrumentation, cg, name)
249# define GPU_INSTRUMENTATION_L2_C(instrumentation, cg, color)
250# define GPU_INSTRUMENTATION_L2_NC(instrumentation, cg, name, color)
251
252# define CPU_INSTRUMENTATION_L2_O(instrumentation, object)
253# define CPU_INSTRUMENTATION_L2_NO(instrumentation, name, object)
254# define CPU_INSTRUMENTATION_L2_CO(instrumentation, color, object)
255# define CPU_INSTRUMENTATION_L2_NCO(instrumentation, name, color, object)
256
257# define GPU_INSTRUMENTATION_L2_O(instrumentation, cg, object)
258# define GPU_INSTRUMENTATION_L2_NO(instrumentation, cg, name, object)
259# define GPU_INSTRUMENTATION_L2_CO(instrumentation, cg, color, object)
260# define GPU_INSTRUMENTATION_L2_NCO(instrumentation, cg, name, color, object)
261
262#endif
263
264#if VSG_MAX_INSTRUMENTATION_LEVEL >= 3
265
266# define CPU_INSTRUMENTATION_L3(instrumentation) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, COLOR_DEFAULT, nullptr)
267# define CPU_INSTRUMENTATION_L3_N(instrumentation, name) __CPU_INSTRUMENTATION(3, instrumentation, name, COLOR_DEFAULT, nullptr)
268# define CPU_INSTRUMENTATION_L3_C(instrumentation, color) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, color, nullptr)
269# define CPU_INSTRUMENTATION_L3_NC(instrumentation, name, color) __CPU_INSTRUMENTATION(3, instrumentation, name, color, nullptr)
270
271# define GPU_INSTRUMENTATION_L3(instrumentation, cg) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, COLOR_DEFAULT, nullptr)
272# define GPU_INSTRUMENTATION_L3_N(instrumentation, cg, name) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, COLOR_DEFAULT, nullptr)
273# define GPU_INSTRUMENTATION_L3_C(instrumentation, cg, color) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, color, nullptr)
274# define GPU_INSTRUMENTATION_L3_NC(instrumentation, cg, name, color) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, color, nullptr)
275
276# define CPU_INSTRUMENTATION_L3_O(instrumentation, object) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, COLOR_DEFAULT, object)
277# define CPU_INSTRUMENTATION_L3_NO(instrumentation, name, object) __CPU_INSTRUMENTATION(3, instrumentation, name, COLOR_DEFAULT, object)
278# define CPU_INSTRUMENTATION_L3_CO(instrumentation, color, object) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, color, object)
279# define CPU_INSTRUMENTATION_L3_NCO(instrumentation, name, color, object) __CPU_INSTRUMENTATION(3, instrumentation, name, color, object)
280
281# define GPU_INSTRUMENTATION_L3_O(instrumentation, cg, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, COLOR_DEFAULT, object)
282# define GPU_INSTRUMENTATION_L3_NO(instrumentation, cg, name, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, COLOR_DEFAULT, object)
283# define GPU_INSTRUMENTATION_L3_CO(instrumentation, cg, color, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, color, object)
284# define GPU_INSTRUMENTATION_L3_NCO(instrumentation, cg, name, color, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, color, object)
285
286#else
287
288# define CPU_INSTRUMENTATION_L3(instrumentation)
289# define CPU_INSTRUMENTATION_L3_N(instrumentation, name)
290# define CPU_INSTRUMENTATION_L3_C(instrumentation, color)
291# define CPU_INSTRUMENTATION_L3_NC(instrumentation, name, color)
292
293# define GPU_INSTRUMENTATION_L3(instrumentation, cg)
294# define GPU_INSTRUMENTATION_L3_N(instrumentation, cg, name)
295# define GPU_INSTRUMENTATION_L3_C(instrumentation, cg, color)
296# define GPU_INSTRUMENTATION_L3_NC(instrumentation, cg, name, color)
297
298# define CPU_INSTRUMENTATION_L3_O(instrumentation, object)
299# define CPU_INSTRUMENTATION_L3_NO(instrumentation, name, object)
300# define CPU_INSTRUMENTATION_L3_CO(instrumentation, color, object)
301# define CPU_INSTRUMENTATION_L3_NCO(instrumentation, name, color, object)
302
303# define GPU_INSTRUMENTATION_L3_O(instrumentation, cg, object)
304# define GPU_INSTRUMENTATION_L3_NO(instrumentation, cg, name, object)
305# define GPU_INSTRUMENTATION_L3_CO(instrumentation, cg, color, object)
306# define GPU_INSTRUMENTATION_L3_NCO(instrumentation, cg, name, color, object)
307
308#endif
309
310} // namespace vsg
vsg::CommandBuffer
CommandBuffer encapsulates VkCommandBuffer.
Definition CommandBuffer.h:27
vsg::FrameStamp
FrameStamp represents the time and frame count of a specific frame.
Definition FrameStamp.h:22
vsg::Inherit
Definition Inherit.h:28
vsg::Instrumentation
base class for Instrumentation implentations
Definition Instrumentation.h:51
vsg::Object
Definition Object.h:60
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::CommandBufferInstrumentation
Definition Instrumentation.h:119
vsg::CpuInstrumentation
Definition Instrumentation.h:82
vsg::GpuInstrumentation
Definition Instrumentation.h:100
vsg::SourceLocation
Definition Instrumentation.h:40
vsg::uint_color
uint_color struct used to provide a {r, g, b, a} interface a colors assigned as uint32_t
Definition Instrumentation.h:29