yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.1 KiB79 linesraw
1#ifndef SLANG_CORE_PERFORMANCE_PROFILER_H
2#define SLANG_CORE_PERFORMANCE_PROFILER_H
3
4#include "../core/slang-list.h"
5#include "slang-com-helper.h"
6#include "slang-string.h"
7
8#include <chrono>
9#include <vector>
10
11namespace Slang
12{
13
14struct FuncProfileInfo
15{
16    int invocationCount = 0;
17    std::chrono::nanoseconds duration = std::chrono::nanoseconds::zero();
18};
19
20struct FuncProfileContext
21{
22    const char* funcName = nullptr;
23    std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
24};
25
26class PerformanceProfiler
27{
28public:
29    virtual FuncProfileContext enterFunction(const char* funcName) = 0;
30    virtual void exitFunction(FuncProfileContext context) = 0;
31    virtual void getResult(StringBuilder& out) = 0;
32    virtual void clear() = 0;
33    virtual void dispose() = 0;
34
35public:
36    static PerformanceProfiler* getProfiler();
37};
38
39struct PerformanceProfilerFuncRAIIContext
40{
41    FuncProfileContext context;
42    PerformanceProfilerFuncRAIIContext(const char* funcName)
43    {
44        context = PerformanceProfiler::getProfiler()->enterFunction(funcName);
45    }
46    ~PerformanceProfilerFuncRAIIContext()
47    {
48        PerformanceProfiler::getProfiler()->exitFunction(context);
49    }
50};
51
52struct SlangProfiler : public ISlangProfiler, public RefObject
53{
54public:
55    SLANG_REF_OBJECT_IUNKNOWN_ALL
56    struct ProfileInfo
57    {
58        char funcName[256] = {0};
59        int invocationCount = 0;
60        std::chrono::nanoseconds duration = std::chrono::nanoseconds::zero();
61    };
62    SlangProfiler(PerformanceProfiler* profiler);
63    ISlangUnknown* getInterface(const Guid& guid);
64
65    virtual SLANG_NO_THROW size_t SLANG_MCALL getEntryCount() override;
66    virtual SLANG_NO_THROW const char* SLANG_MCALL getEntryName(uint32_t index) override;
67    virtual SLANG_NO_THROW long SLANG_MCALL getEntryTimeMS(uint32_t index) override;
68    virtual SLANG_NO_THROW uint32_t SLANG_MCALL getEntryInvocationTimes(uint32_t index) override;
69
70private:
71    List<ProfileInfo> m_profilEntries;
72};
73
74#define SLANG_PROFILE PerformanceProfilerFuncRAIIContext _profileContext(__func__)
75#define SLANG_PROFILE_SECTION(s) PerformanceProfilerFuncRAIIContext _profileContext##s(#s)
76
77} // namespace Slang
78
79#endif