跟着cherno手搓游戏引擎【26】Profile和Profile网页可视化

封装Profile:

Sandbox2D.h:ProfileResult结构体和ProfileResult容器,存储相应的信息

#pragma once
#include "YOTO.h"
class Sandbox2D :public YOTO::Layer
{public:Sandbox2D();virtual ~Sandbox2D() = default;virtual void OnAttach()override;virtual void OnDetach()override;void OnUpdate(YOTO::Timestep ts)override;virtual void OnImGuiRender() override;void OnEvent(YOTO::Event& e)override;
private:YOTO::OrthographicCameraController m_CameraController;YOTO::Ref<YOTO::Shader> m_FlatColorShader;YOTO::Ref<YOTO::VertexArray> m_SquareVA;YOTO::Ref<YOTO::Texture2D>m_CheckerboardTexture;struct ProfileResult {const char* Name;float Time;};std::vector<ProfileResult>m_ProfileResults;glm::vec4 m_SquareColor = { 0.2f,0.3f,0.7f,1.0f };
};

 Sandbox2D.cpp:实现timer并定义PROFILE_SCOPE使用(YT_PROFILE_SCOPE为网页可视化的内容,先放到这了)

#include "Sandbox2D.h"
#include <imgui/imgui.h>
#include <glm/gtc/matrix_transform.hpp>
//#include <Platform/OpenGL/OpenGLShader.h>
#include <glm/gtc/type_ptr.hpp>
#include<vector>
#include<chrono>
template<typename Fn>
class Timer {
public:Timer(const char* name, Fn&&func):m_Name(name),m_Func(func),m_Stopped(false){m_StartTimepoint = std::chrono::high_resolution_clock::now();}~Timer() {if (!m_Stopped) {Stop();}}void Stop() {auto endTimepoint= std::chrono::high_resolution_clock::now();long long start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count();long long end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count();m_Stopped = true;float duration = (end - start)*0.001f;m_Func({m_Name,duration});//std::cout << "Timer:"<< m_Name << "时差:" << duration << "ms" << std::endl;}
private:const char* m_Name;std::chrono::time_point<std::chrono::steady_clock>m_StartTimepoint;bool m_Stopped;Fn m_Func;
};
//未找到匹配的重载:auto的问题,改回原来的类型就好了
#define PROFILE_SCOPE(name) Timer timer##__LINE__(name,[&](ProfileResult profileResult) {m_ProfileResults.push_back(profileResult);})
Sandbox2D::Sandbox2D()
:Layer("Sandbox2D"), m_CameraController(1280.0f / 720.0f, true) 
{
}
void Sandbox2D::OnAttach()
{m_CheckerboardTexture = YOTO::Texture2D::Create("assets/textures/Checkerboard.png");}
void Sandbox2D::OnDetach()
{
}void Sandbox2D::OnUpdate(YOTO::Timestep ts)
{YT_PROFILE_FUNCTION();PROFILE_SCOPE("Sandbox2D::OnUpdate");{YT_PROFILE_SCOPE("CameraController::OnUpdate");PROFILE_SCOPE("CameraController::OnUpdate");//updatem_CameraController.OnUpdate(ts);}{YT_PROFILE_SCOPE("Renderer Prep");PROFILE_SCOPE("Renderer Prep");//RenderYOTO::RenderCommand::SetClearColor({ 0.2f, 0.2f, 0.2f, 1.0f });YOTO::RenderCommand::Clear();}{YT_PROFILE_SCOPE("Renderer Draw");PROFILE_SCOPE("Renderer Draw");YOTO::Renderer2D::BeginScene(m_CameraController.GetCamera());{static glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));glm::vec4  redColor(0.8f, 0.3f, 0.3f, 1.0f);glm::vec4  blueColor(0.2f, 0.3f, 0.8f, 1.0f);/*std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_FlatColorShader)->Bind();std::dynamic_pointer_cast<YOTO::OpenGLShader>(m_FlatColorShader)->UploadUniformFloat4("u_Color", m_SquareColor);YOTO::Renderer::Submit(m_FlatColorShader, m_SquareVA, glm::scale(glm::mat4(1.0f), glm::vec3(1.5f)));*/YOTO::Renderer2D::DrawQuad({ -1.0f,0.0f }, { 0.8f,0.8f }, { 0.8f,0.2f,0.3f,1.0f });YOTO::Renderer2D::DrawQuad({ 0.5f,-0.5f }, { 0.5f,0.75f }, { 0.2f,0.3f,0.8f,1.0f });YOTO::Renderer2D::DrawQuad({ 0.0f,0.0f,-0.1f }, { 10.0f,10.0f }, m_CheckerboardTexture);YOTO::Renderer2D::EndScene();}}}
void Sandbox2D::OnImGuiRender()
{ImGui::Begin("Setting");ImGui::ColorEdit4("Color", glm::value_ptr(m_SquareColor));for (auto& res : m_ProfileResults) {char lable[50];strcpy(lable, "%.3fms  ");strcat(lable, res.Name);ImGui::Text(lable, res.Time);}m_ProfileResults.clear();ImGui::End();
}void Sandbox2D::OnEvent(YOTO::Event& e)
{m_CameraController.OnEvent(e);
}

测试: 

Profile网页可视化:

创建.h文件:

 

instrumentor.h:直接粘贴全部,实现跟封装的profile类似,但是多了生成json文件的代码

#pragma once#include "YOTO/Core/Log.h"#include <algorithm>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <string>
#include <thread>
#include <mutex>
#include <sstream>namespace YOTO {using FloatingPointMicroseconds = std::chrono::duration<double, std::micro>;struct ProfileResult{std::string Name;FloatingPointMicroseconds Start;std::chrono::microseconds ElapsedTime;std::thread::id ThreadID;};struct InstrumentationSession{std::string Name;};class Instrumentor{public:Instrumentor(const Instrumentor&) = delete;Instrumentor(Instrumentor&&) = delete;void BeginSession(const std::string& name, const std::string& filepath = "results.json"){std::lock_guard lock(m_Mutex);if (m_CurrentSession){// If there is already a current session, then close it before beginning new one.// Subsequent profiling output meant for the original session will end up in the// newly opened session instead.  That's better than having badly formatted// profiling output.if (YOTO::Log::GetCoreLogger()) // Edge case: BeginSession() might be before Log::Init(){YT_CORE_ERROR("Instrumentor::BeginSession('{0}') when session '{1}' already open.", name, m_CurrentSession->Name);}InternalEndSession();}m_OutputStream.open(filepath);if (m_OutputStream.is_open()){m_CurrentSession = new InstrumentationSession({ name });WriteHeader();}else{if (YOTO::Log::GetCoreLogger()) // Edge case: BeginSession() might be before Log::Init(){YT_CORE_ERROR("Instrumentor could not open results file '{0}'.", filepath);}}}void EndSession(){std::lock_guard lock(m_Mutex);InternalEndSession();}void WriteProfile(const ProfileResult& result){std::stringstream json;json << std::setprecision(3) << std::fixed;json << ",{";json << "\"cat\":\"function\",";json << "\"dur\":" << (result.ElapsedTime.count()) << ',';json << "\"name\":\"" << result.Name << "\",";json << "\"ph\":\"X\",";json << "\"pid\":0,";json << "\"tid\":" << result.ThreadID << ",";json << "\"ts\":" << result.Start.count();json << "}";std::lock_guard lock(m_Mutex);if (m_CurrentSession){m_OutputStream << json.str();m_OutputStream.flush();}}static Instrumentor& Get(){static Instrumentor instance;return instance;}private:Instrumentor(): m_CurrentSession(nullptr){}~Instrumentor(){EndSession();}void WriteHeader(){m_OutputStream << "{\"otherData\": {},\"traceEvents\":[{}";m_OutputStream.flush();}void WriteFooter(){m_OutputStream << "]}";m_OutputStream.flush();}// Note: you must already own lock on m_Mutex before// calling InternalEndSession()void InternalEndSession(){if (m_CurrentSession){WriteFooter();m_OutputStream.close();delete m_CurrentSession;m_CurrentSession = nullptr;}}private:std::mutex m_Mutex;InstrumentationSession* m_CurrentSession;std::ofstream m_OutputStream;};class InstrumentationTimer{public:InstrumentationTimer(const char* name): m_Name(name), m_Stopped(false){m_StartTimepoint = std::chrono::steady_clock::now();}~InstrumentationTimer(){if (!m_Stopped)Stop();}void Stop(){auto endTimepoint = std::chrono::steady_clock::now();auto highResStart = FloatingPointMicroseconds{ m_StartTimepoint.time_since_epoch() };auto elapsedTime = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch() - std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch();Instrumentor::Get().WriteProfile({ m_Name, highResStart, elapsedTime, std::this_thread::get_id() });m_Stopped = true;}private:const char* m_Name;std::chrono::time_point<std::chrono::steady_clock> m_StartTimepoint;bool m_Stopped;};namespace InstrumentorUtils {template <size_t N>struct ChangeResult{char Data[N];};template <size_t N, size_t K>constexpr auto CleanupOutputString(const char(&expr)[N], const char(&remove)[K]){ChangeResult<N> result = {};size_t srcIndex = 0;size_t dstIndex = 0;while (srcIndex < N){size_t matchIndex = 0;while (matchIndex < K - 1 && srcIndex + matchIndex < N - 1 && expr[srcIndex + matchIndex] == remove[matchIndex])matchIndex++;if (matchIndex == K - 1)srcIndex += matchIndex;result.Data[dstIndex++] = expr[srcIndex] == '"' ? '\'' : expr[srcIndex];srcIndex++;}return result;}}
}#define YT_PROFILE 0
#if YT_PROFILE
// Resolve which function signature macro will be used. Note that this only
// is resolved when the (pre)compiler starts, so the syntax highlighting
// could mark the wrong one in your editor!
#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__)
#define YT_FUNC_SIG __PRETTY_FUNCTION__
#elif defined(__DMC__) && (__DMC__ >= 0x810)
#define YT_FUNC_SIG __PRETTY_FUNCTION__
#elif (defined(__FUNCSIG__) || (_MSC_VER))
#define YT_FUNC_SIG __FUNCSIG__
#elif (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) || (defined(__IBMCPP__) && (__IBMCPP__ >= 500))
#define YT_FUNC_SIG __FUNCTION__
#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)
#define YT_FUNC_SIG __FUNC__
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
#define YT_FUNC_SIG __func__
#elif defined(__cplusplus) && (__cplusplus >= 201103)
#define YT_FUNC_SIG __func__
#else
#define YT_FUNC_SIG "YT_FUNC_SIG unknown!"
#endif#define YT_PROFILE_BEGIN_SESSION(name, filepath) ::YOTO::Instrumentor::Get().BeginSession(name, filepath)
#define YT_PROFILE_END_SESSION() ::YOTO::Instrumentor::Get().EndSession()
#define YT_PROFILE_SCOPE_LINE2(name, line) constexpr auto fixedName##line = ::YOTO::InstrumentorUtils::CleanupOutputString(name, "__cdecl ");\::YOTO::InstrumentationTimer timer##line(fixedName##line.Data)
#define YT_PROFILE_SCOPE_LINE(name, line) YT_PROFILE_SCOPE_LINE2(name, line)
#define YT_PROFILE_SCOPE(name) YT_PROFILE_SCOPE_LINE(name, __LINE__)
#define YT_PROFILE_FUNCTION() YT_PROFILE_SCOPE(YT_FUNC_SIG)
#else
#define YT_PROFILE_BEGIN_SESSION(name, filepath)
#define YT_PROFILE_END_SESSION()
#define YT_PROFILE_SCOPE(name)
#define YT_PROFILE_FUNCTION()
#endif

 EntryPoint.h:使用定义

#pragma once#ifdef YT_PLATFORM_WINDOWS#include "YOTO.h"
void main(int argc,char** argv) {//初始化日志YOTO::Log::Init();//YT_CORE_ERROR("EntryPoint测试警告信息");//int test = 1;//YT_CLIENT_INFO("EntryPoint测试info:test={0}",test);YT_PROFILE_BEGIN_SESSION("Start","YOTOProfile-Startup.json");auto app = YOTO::CreateApplication();YT_PROFILE_END_SESSION();YT_PROFILE_BEGIN_SESSION("Runtime", "YOTOProfile-Runtime.json");app->Run();YT_PROFILE_END_SESSION();YT_PROFILE_BEGIN_SESSION("Shutdown", "YOTOProfile-Shutdown.json");delete app;YT_PROFILE_END_SESSION();
}
#endif

ytpch.h:

#pragma once
#include<iostream>
#include<memory>
#include<utility>
#include<algorithm>
#include<functional>
#include<string>
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<sstream>
#include<array>
#include "YOTO/Core/Log.h"#include "YOTO/Debug/instrumentor.h"
#ifdef YT_PLATFORM_WINDOWS
#include<Windows.h>
#endif // YT_PLATFORM_WINDOWS

测试: 

在谷歌浏览器输入:chrome://tracing

拖入json文件:

cool,虽然看不太懂,但是文件有够大(运行了几秒就2000多k,平时使用还是用自己写的封装的叭) 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2812120.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

python中的数字类型Number

Python 数字(Number) Python 数字数据类型用于存储数值。 数据类型是不允许改变的&#xff0c;这就意味着如果改变数字数据类型的值&#xff0c;将重新分配内存空间。 以下实例在变量赋值时 Number 对象将被创建&#xff1a; var1 1 var2 10Python 支持三种不同的数值类型…

蜣螂优化算法DBO求解不闭合MD-MTSP,可以修改旅行商个数及起点(提供MATLAB代码)

一、蜣螂优化算法&#xff08;Dung beetle optimizer&#xff0c;DBO&#xff09; 蜣螂优化算法&#xff08;Dung beetle optimizer&#xff0c;DBO&#xff09;由Jiankai Xue和Bo Shen于2022年提出&#xff0c;该算法主要受蜣螂的滚球、跳舞、觅食、偷窃和繁殖行为的启发所得…

kubectl使用及源码阅读

目录 概述实践样例yaml 中的必须字段 kubectl 代码原理kubectl 命令行设置pprof 抓取火焰图kubectl 中的 cobra 七大分组命令kubectl createcreateCmd中的builder模式createCmd中的visitor访问者模式外层VisitorFunc分析 结束 概述 k8s 版本 v1.24.16 kubectl的职责 1.主要的…

SQL注入之order by脚本盲注

一、环境 还是用上次搭建的sql-labs靶机环境 搭建sql注入环境 二、什么是order by盲注 在有的时候我们进行sql注入时&#xff0c;源码的查询语句后面为order by&#xff1b; 众所周知&#xff0c;order by 后面接的字段或者数字不一样&#xff0c;那么这个数据表的排序就会…

嵌入式学习第二十一天!(线程)

线程&#xff1a; 1. 基本概念&#xff1a; 线程&#xff1a;线程是一个轻量级的进程&#xff0c;位于进程空间内部&#xff0c;一个进程中可以创建多个线程 2. 线程创建&#xff1a; 线程独占栈空间&#xff0c;文本段、数据段和堆区与进程共享 3. 线程调度&#xff1a; 与进程…

探索比特币现货 ETF 对加密货币价格的潜在影响

撰文&#xff1a;Sean&#xff0c;Techub News 文章来源Techub News&#xff0c;搜Tehub News下载查看更多Web3资讯。 自美国比特币现货交易所交易基金&#xff08;ETF&#xff09;上市以来&#xff0c;比特币现货 ETF 的相关信息无疑成为了影响比特币价格及加密货币市场走向…

Java四大引用详解:强引用、软引用、弱引用、虚引用

在JDK1.2以前的版本中&#xff0c;当一个对象不被任何变量引用&#xff0c;那么程序就无法再使用这个对象。也就是说&#xff0c;只有对象处于可触及状态&#xff0c;程序才能使用它。这就像在商店购买了某样物品后&#xff0c;如果有用就一直保留它&#xff0c;否则就把它扔到…

Spring Boot项目中不使用@RequestMapping相关注解,如何动态发布自定义URL路径

一、前言 在Spring Boot项目开发过程中&#xff0c;对于接口API发布URL访问路径&#xff0c;一般都是在类上标识RestController或者Controller注解&#xff0c;然后在方法上标识RequestMapping相关注解&#xff0c;比如&#xff1a;PostMapping、GetMapping注解&#xff0c;通…

【UnityShader入门精要学习笔记】第六章(1)Unity中的基础光照

本系列为作者学习UnityShader入门精要而作的笔记&#xff0c;内容将包括&#xff1a; 书本中句子照抄 个人批注项目源码一堆新手会犯的错误潜在的太监断更&#xff0c;有始无终 总之适用于同样开始学习Shader的同学们进行有取舍的参考。 文章目录 光照的原理光源吸收和散射着…

LinkedList的使用(Java)

目录 一、介绍二、LinkedList的使用1、LinkedList的构造2、 LinkedList的其他常用方法介绍3、LinkedList的遍历 三、ArrayList和LinkedList的区别 一、介绍 LinkedList的底层是双向链表结构&#xff0c;由于链表没有将元素存储在连续的空间中&#xff0c;元素存储在单独的节点…

开发知识点-.netC#图形用户界面开发之WPF

C#图形用户界面开发 框架简介WinForms(Windows Forms):WPF(Windows Presentation Foundation):UWP(Universal Windows Platform):MAUI(Multi-platform App UI):选择控件参考文章随笔分类 - WPF入门基础教程系列基于C#语言的GUI开发,主要介绍WPF框架

IP对讲终端SV-6002(防水)

SV-6002&#xff08;防水&#xff09;是一款IP对讲终端&#xff0c;具有10/100M以太网接口&#xff0c;其接收网络的音频数据&#xff0c;解码后播放&#xff0c;外部DC12~24V电源供电端子&#xff0c;提供单路2W的音频输出。基于TCP/IP网络通信协议和数字音频技术&#xff0c;…

MATLAB练习题:并行计算的parfor循环

​讲解视频&#xff1a;可以在bilibili搜索《MATLAB教程新手入门篇——数学建模清风主讲》。​ MATLAB教程新手入门篇&#xff08;数学建模清风主讲&#xff0c;适合零基础同学观看&#xff09;_哔哩哔哩_bilibili 在MATLAB中&#xff0c;parfor&#xff08;Parallel for&…

Centos配置SSH并禁止密码登录

CentOS8 配置SSH使用密钥登录并禁止密码登录 一、概念 SSH 为 Secure Shell 的缩写,SSH 为建立在应用层基础上的安全协议。SSH 是较可靠&#xff0c;专为远程登录会话和其他网络服务提供安全性的协议。 SSH提供两个级别的认证&#xff1a; 基于口令的认证 基于密钥的认证 基本使…

【大厂AI课学习笔记NO.55】2.3深度学习开发任务实例(8)模型训练

作者简介&#xff1a;giszz&#xff0c;腾讯云人工智能从业者TCA认证&#xff0c;信息系统项目管理师。 博客地址&#xff1a;https://giszz.blog.csdn.net 声明&#xff1a;本学习笔记来自腾讯云人工智能课程&#xff0c;叠加作者查阅的背景资料、延伸阅读信息&#xff0c;及学…

LockBit官宣复活,表示将更多针对政府部门

仅仅在11国联合执法行动宣布成功查封LockBit基础设施服务器不到一周后&#xff0c;2月24日&#xff0c;LockBit 宣布将恢复勒索软件业务&#xff0c;并威胁会将更多的攻击集中在政府部门。 在一个新上线的数据泄露网站上&#xff0c;LockBit仍然使用其3.0 版本的名称&#xff…

【iOS ARKit】ARWorldMap

ARWorldMap 用于存储 ARSession 检测扫描到的空间信息数据&#xff0c;包括地标&#xff08;Landmark&#xff09;、特征点&#xff08;Feature Point&#xff09;、平面&#xff08;Plane&#xff09;等&#xff0c;以及使用者的操作信息&#xff0c;如使用者添加的 ARAnchor …

Wget: 如何批量下载风云卫星和NASA数据?

01 批量下载风云数据 首先进入风云卫星服务官网: https://satellite.nsmc.org.cn/portalsite/default.aspx 假定我们下载火点数据,如下先找到下载界面: 接着依据数据所对应卫星\传感器\产品名称进行检索: 选定需要的数据加入到购物车中: 接着提交订单等待所有数据回调完成就…

TensorFlow2.x 精选笔记(2)自动求导与概率

学习参考&#xff1a; 动手学深度学习2.0Deep-Learning-with-TensorFlow-bookpytorchlightning ①如有冒犯、请联系侵删。 ②已写完的笔记文章会不定时一直修订修改(删、改、增)&#xff0c;以达到集多方教程的精华于一文的目的。 ③非常推荐上面&#xff08;学习参考&#x…

python爬虫实战:获取电子邮件和联系人信息

引言 在数字时代&#xff0c;电子邮件和联系人信息成为了许多企业和个人重要的资源&#xff0c;在本文中&#xff0c;我们将探讨如何使用Python爬虫从网页中提取电子邮件和联系人信息&#xff0c;并附上示例代码。 目录 引言 二、准备工作 你可以使用以下命令来安装这些库&a…