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

vsg 1.1.3 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), 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), 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), 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_VIEWER uint_color(127, 240, 240, 255)
138#define COLOR_UPDATE uint_color(0, 255, 0, 255)
139#define COLOR_GPU uint_color(255, 127, 0, 255)
140#define COLOR_RECORD_L1 uint_color(140, 247, 0, 255)
141#define COLOR_RECORD_L2 uint_color(176, 176, 0, 255)
142#define COLOR_RECORD_L3 COLOR_GPU
143#define COLOR_RECORD COLOR_RECORD_L1
144#define COLOR_COMPILE uint_color(255, 249, 64, 255)
145#define COLOR_PAGER uint_color(240, 255, 64, 255)
146#define COLOR_READ uint_color(0, 255, 128, 255)
147#define COLOR_WRITE uint_color(0, 128, 255, 255)
148
149#if defined(__clang__) || defined(__GNUC__)
150# define VsgFunctionName __PRETTY_FUNCTION__
151#elif defined(_MSC_VER)
152# define VsgFunctionName __FUNCSIG__
153#endif
154
155#define __CPU_INSTRUMENTATION(level, instrumentation, name, color, object) \
156 static constexpr SourceLocation s_cpu_source_location_##__LINE__{name, VsgFunctionName, __FILE__, __LINE__, color, level}; \
157 CpuInstrumentation __cpu_scoped_instrumentation_##__LINE__(instrumentation, &(s_cpu_source_location_##__LINE__), object);
158
159#define __GPU_INSTRUMENTATION(level, instrumentation, cg, name, color, object) \
160 static constexpr SourceLocation s_gpu_source_location_##__LINE__{name, VsgFunctionName, __FILE__, __LINE__, color, level}; \
161 GpuInstrumentation __gpu_scoped_instrumentation_##__LINE__(instrumentation, &(s_gpu_source_location_##__LINE__), cg, object);
162
163#define __COMMAND_BUFFER_INSTRUMENTATION(level, instrumentation, cg, name, color) \
164 static constexpr SourceLocation s_cg_source_location_##__LINE__{name, VsgFunctionName, __FILE__, __LINE__, color, level}; \
165 CommandBufferInstrumentation __cg_scoped_instrumentation_##__LINE__(instrumentation, &(s_cg_source_location_##__LINE__), cg);
166
167#if VSG_MAX_INSTRUMENTATION_LEVEL >= 1
168
169# define CPU_INSTRUMENTATION_L1(instrumentation) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, uint_color(255, 255, 255, 255), nullptr)
170# define CPU_INSTRUMENTATION_L1_N(instrumentation, name) __CPU_INSTRUMENTATION(1, instrumentation, name, uint_color(255, 255, 255, 255), nullptr)
171# define CPU_INSTRUMENTATION_L1_C(instrumentation, color) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, color, nullptr)
172# define CPU_INSTRUMENTATION_L1_NC(instrumentation, name, color) __CPU_INSTRUMENTATION(1, instrumentation, name, color, nullptr)
173
174# define GPU_INSTRUMENTATION_L1(instrumentation, cg) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, uint_color(255, 255, 255, 255), nullptr)
175# define GPU_INSTRUMENTATION_L1_N(instrumentation, cg, name) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, uint_color(255, 255, 255, 255), nullptr)
176# define GPU_INSTRUMENTATION_L1_C(instrumentation, cg, color) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, color, nullptr)
177# define GPU_INSTRUMENTATION_L1_NC(instrumentation, cg, name, color) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, color, nullptr)
178
179# define CPU_INSTRUMENTATION_L1_O(instrumentation, object) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, uint_color(255, 255, 255, 255), object)
180# define CPU_INSTRUMENTATION_L1_NO(instrumentation, name, object) __CPU_INSTRUMENTATION(1, instrumentation, name, uint_color(255, 255, 255, 255), object)
181# define CPU_INSTRUMENTATION_L1_CO(instrumentation, color, object) __CPU_INSTRUMENTATION(1, instrumentation, nullptr, color, object)
182# define CPU_INSTRUMENTATION_L1_NCO(instrumentation, name, color, object) __CPU_INSTRUMENTATION(1, instrumentation, name, color, object)
183
184# define GPU_INSTRUMENTATION_L1_O(instrumentation, cg, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, uint_color(255, 255, 255, 255), object)
185# define GPU_INSTRUMENTATION_L1_NO(instrumentation, cg, name, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, uint_color(255, 255, 255, 255), object)
186# define GPU_INSTRUMENTATION_L1_CO(instrumentation, cg, color, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, nullptr, color, object)
187# define GPU_INSTRUMENTATION_L1_NCO(instrumentation, cg, name, color, object) __GPU_INSTRUMENTATION(1, instrumentation, cg, name, color, object)
188
189# define COMMAND_BUFFER_INSTRUMENTATION(instrumentation, cg, name, color) __COMMAND_BUFFER_INSTRUMENTATION(1, instrumentation, cg, name, color)
190
191#else
192
193# define CPU_INSTRUMENTATION_L1(instrumentation)
194# define CPU_INSTRUMENTATION_L1_N(instrumentation, name)
195# define CPU_INSTRUMENTATION_L1_C(instrumentation, color)
196# define CPU_INSTRUMENTATION_L1_NC(instrumentation, name, color)
197
198# define GPU_INSTRUMENTATION_L1(instrumentation, cg)
199# define GPU_INSTRUMENTATION_L1_N(instrumentation, cg, name)
200# define GPU_INSTRUMENTATION_L1_C(instrumentation, cg, color)
201# define GPU_INSTRUMENTATION_L1_NC(instrumentation, cg, name, color)
202
203# define CPU_INSTRUMENTATION_L1_O(instrumentation, object)
204# define CPU_INSTRUMENTATION_L1_NO(instrumentation, name, object)
205# define CPU_INSTRUMENTATION_L1_CO(instrumentation, color, object)
206# define CPU_INSTRUMENTATION_L1_NCO(instrumentation, name, color, object)
207
208# define GPU_INSTRUMENTATION_L1_O(instrumentation, cg, object)
209# define GPU_INSTRUMENTATION_L1_NO(instrumentation, cg, name, object)
210# define GPU_INSTRUMENTATION_L1_CO(instrumentation, cg, color, object)
211# define GPU_INSTRUMENTATION_L1_NCO(instrumentation, cg, name, color, object)
212
213# define COMMAND_BUFFER_INSTRUMENTATION(instrumentation, cg, name, color)
214
215#endif
216
217#if VSG_MAX_INSTRUMENTATION_LEVEL >= 2
218
219# define CPU_INSTRUMENTATION_L2(instrumentation) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, uint_color(255, 255, 255, 255), nullptr)
220# define CPU_INSTRUMENTATION_L2_N(instrumentation, name) __CPU_INSTRUMENTATION(2, instrumentation, name, uint_color(255, 255, 255, 255), nullptr)
221# define CPU_INSTRUMENTATION_L2_C(instrumentation, color) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, color, nullptr)
222# define CPU_INSTRUMENTATION_L2_NC(instrumentation, name, color) __CPU_INSTRUMENTATION(2, instrumentation, name, color, nullptr)
223
224# define GPU_INSTRUMENTATION_L2(instrumentation, cg) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, uint_color(255, 255, 255, 255), nullptr)
225# define GPU_INSTRUMENTATION_L2_N(instrumentation, cg, name) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, uint_color(255, 255, 255, 255), nullptr)
226# define GPU_INSTRUMENTATION_L2_C(instrumentation, cg, color) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, color, nullptr)
227# define GPU_INSTRUMENTATION_L2_NC(instrumentation, cg, name, color) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, color, nullptr)
228
229# define CPU_INSTRUMENTATION_L2_O(instrumentation, object) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, uint_color(255, 255, 255, 255), object)
230# define CPU_INSTRUMENTATION_L2_NO(instrumentation, name, object) __CPU_INSTRUMENTATION(2, instrumentation, name, uint_color(255, 255, 255, 255), object)
231# define CPU_INSTRUMENTATION_L2_CO(instrumentation, color, object) __CPU_INSTRUMENTATION(2, instrumentation, nullptr, color, object)
232# define CPU_INSTRUMENTATION_L2_NCO(instrumentation, name, color, object) __CPU_INSTRUMENTATION(2, instrumentation, name, color, object)
233
234# define GPU_INSTRUMENTATION_L2_O(instrumentation, cg, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, uint_color(255, 255, 255, 255), object)
235# define GPU_INSTRUMENTATION_L2_NO(instrumentation, cg, name, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, uint_color(255, 255, 255, 255), object)
236# define GPU_INSTRUMENTATION_L2_CO(instrumentation, cg, color, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, nullptr, color, object)
237# define GPU_INSTRUMENTATION_L2_NCO(instrumentation, cg, name, color, object) __GPU_INSTRUMENTATION(2, instrumentation, cg, name, color, object)
238
239#else
240
241# define CPU_INSTRUMENTATION_L2(instrumentation)
242# define CPU_INSTRUMENTATION_L2_N(instrumentation, name)
243# define CPU_INSTRUMENTATION_L2_C(instrumentation, color)
244# define CPU_INSTRUMENTATION_L2_NC(instrumentation, name, color)
245
246# define GPU_INSTRUMENTATION_L2(instrumentation, cg)
247# define GPU_INSTRUMENTATION_L2_N(instrumentation, cg, name)
248# define GPU_INSTRUMENTATION_L2_C(instrumentation, cg, color)
249# define GPU_INSTRUMENTATION_L2_NC(instrumentation, cg, name, color)
250
251# define CPU_INSTRUMENTATION_L2_O(instrumentation, object)
252# define CPU_INSTRUMENTATION_L2_NO(instrumentation, name, object)
253# define CPU_INSTRUMENTATION_L2_CO(instrumentation, color, object)
254# define CPU_INSTRUMENTATION_L2_NCO(instrumentation, name, color, object)
255
256# define GPU_INSTRUMENTATION_L2_O(instrumentation, cg, object)
257# define GPU_INSTRUMENTATION_L2_NO(instrumentation, cg, name, object)
258# define GPU_INSTRUMENTATION_L2_CO(instrumentation, cg, color, object)
259# define GPU_INSTRUMENTATION_L2_NCO(instrumentation, cg, name, color, object)
260
261#endif
262
263#if VSG_MAX_INSTRUMENTATION_LEVEL >= 3
264
265# define CPU_INSTRUMENTATION_L3(instrumentation) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, uint_color(255, 255, 255, 255), nullptr)
266# define CPU_INSTRUMENTATION_L3_N(instrumentation, name) __CPU_INSTRUMENTATION(3, instrumentation, name, uint_color(255, 255, 255, 255), nullptr)
267# define CPU_INSTRUMENTATION_L3_C(instrumentation, color) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, color, nullptr)
268# define CPU_INSTRUMENTATION_L3_NC(instrumentation, name, color) __CPU_INSTRUMENTATION(3, instrumentation, name, color, nullptr)
269
270# define GPU_INSTRUMENTATION_L3(instrumentation, cg) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, uint_color(255, 255, 255, 255), nullptr)
271# define GPU_INSTRUMENTATION_L3_N(instrumentation, cg, name) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, uint_color(255, 255, 255, 255), nullptr)
272# define GPU_INSTRUMENTATION_L3_C(instrumentation, cg, color) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, color, nullptr)
273# define GPU_INSTRUMENTATION_L3_NC(instrumentation, cg, name, color) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, color, nullptr)
274
275# define CPU_INSTRUMENTATION_L3_O(instrumentation, object) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, uint_color(255, 255, 255, 255), object)
276# define CPU_INSTRUMENTATION_L3_NO(instrumentation, name, object) __CPU_INSTRUMENTATION(3, instrumentation, name, uint_color(255, 255, 255, 255), object)
277# define CPU_INSTRUMENTATION_L3_CO(instrumentation, color, object) __CPU_INSTRUMENTATION(3, instrumentation, nullptr, color, object)
278# define CPU_INSTRUMENTATION_L3_NCO(instrumentation, name, color, object) __CPU_INSTRUMENTATION(3, instrumentation, name, color, object)
279
280# define GPU_INSTRUMENTATION_L3_O(instrumentation, cg, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, uint_color(255, 255, 255, 255), object)
281# define GPU_INSTRUMENTATION_L3_NO(instrumentation, cg, name, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, uint_color(255, 255, 255, 255), object)
282# define GPU_INSTRUMENTATION_L3_CO(instrumentation, cg, color, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, nullptr, color, object)
283# define GPU_INSTRUMENTATION_L3_NCO(instrumentation, cg, name, color, object) __GPU_INSTRUMENTATION(3, instrumentation, cg, name, color, object)
284
285#else
286
287# define CPU_INSTRUMENTATION_L3(instrumentation)
288# define CPU_INSTRUMENTATION_L3_N(instrumentation, name)
289# define CPU_INSTRUMENTATION_L3_C(instrumentation, color)
290# define CPU_INSTRUMENTATION_L3_NC(instrumentation, name, color)
291
292# define GPU_INSTRUMENTATION_L3(instrumentation, cg)
293# define GPU_INSTRUMENTATION_L3_N(instrumentation, cg, name)
294# define GPU_INSTRUMENTATION_L3_C(instrumentation, cg, color)
295# define GPU_INSTRUMENTATION_L3_NC(instrumentation, cg, name, color)
296
297# define CPU_INSTRUMENTATION_L3_O(instrumentation, object)
298# define CPU_INSTRUMENTATION_L3_NO(instrumentation, name, object)
299# define CPU_INSTRUMENTATION_L3_CO(instrumentation, color, object)
300# define CPU_INSTRUMENTATION_L3_NCO(instrumentation, name, color, object)
301
302# define GPU_INSTRUMENTATION_L3_O(instrumentation, cg, object)
303# define GPU_INSTRUMENTATION_L3_NO(instrumentation, cg, name, object)
304# define GPU_INSTRUMENTATION_L3_CO(instrumentation, cg, color, object)
305# define GPU_INSTRUMENTATION_L3_NCO(instrumentation, cg, name, color, object)
306
307#endif
308
309} // 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