feat(debug): added debug fps stats

This commit is contained in:
maelstrom 2025-06-30 21:59:35 +02:00
parent 4188e2d9e1
commit f2a7662b08
7 changed files with 74 additions and 9 deletions

View file

@ -4,10 +4,10 @@
out vec4 FragColor; out vec4 FragColor;
uniform vec3 aColor; uniform vec4 aColor;
// Main // Main
void main() { void main() {
FragColor = vec4(aColor, 1); FragColor = aColor;
} }

View file

@ -93,8 +93,11 @@ void ScriptContext::PushThreadSleep(lua_State* thread, float delay) {
lua_pop(state, 1); // pop sleepingThreads lua_pop(state, 1); // pop sleepingThreads
} }
tu_time_t schedTime;
void ScriptContext::RunSleepingThreads() { void ScriptContext::RunSleepingThreads() {
for (size_t i = 0; i < sleepingThreads.size();) { tu_time_t startTime = tu_clock_micros();
size_t i;
for (i = 0; i < sleepingThreads.size();) {
bool deleted = false; bool deleted = false;
SleepingThread sleep = sleepingThreads[i]; SleepingThread sleep = sleepingThreads[i];
@ -127,6 +130,8 @@ void ScriptContext::RunSleepingThreads() {
if (!deleted) if (!deleted)
i++; i++;
} }
if (i > 0)
schedTime = tu_clock_micros() - startTime;
} }
std::string ScriptContext::RegisterScriptSource(std::shared_ptr<Script> script) { std::string ScriptContext::RegisterScriptSource(std::shared_ptr<Script> script) {

View file

@ -7,6 +7,7 @@
#include "objects/joint/jointinstance.h" #include "objects/joint/jointinstance.h"
#include "objects/datamodel.h" #include "objects/datamodel.h"
#include "physics/util.h" #include "physics/util.h"
#include "timeutil.h"
#include <memory> #include <memory>
#include <reactphysics3d/collision/CollisionCallback.h> #include <reactphysics3d/collision/CollisionCallback.h>
#include <reactphysics3d/collision/OverlapCallback.h> #include <reactphysics3d/collision/OverlapCallback.h>
@ -144,7 +145,9 @@ void Workspace::SyncPartPhysics(std::shared_ptr<Part> part) {
part->rigidBody->setUserData(&*part); part->rigidBody->setUserData(&*part);
} }
tu_time_t physTime;
void Workspace::PhysicsStep(float deltaTime) { void Workspace::PhysicsStep(float deltaTime) {
tu_time_t startTime = tu_clock_micros();
// Step the simulation a few steps // Step the simulation a few steps
physicsWorld->update(std::min(deltaTime / 2, (1/60.f))); physicsWorld->update(std::min(deltaTime / 2, (1/60.f)));
@ -180,6 +183,8 @@ void Workspace::PhysicsStep(float deltaTime) {
parent.value()->Destroy(); parent.value()->Destroy();
} }
} }
physTime = tu_clock_micros() - startTime;
} }

View file

@ -1,13 +1,17 @@
#include "rendering/shader.h" #include "rendering/shader.h"
#include "rendering/texture3d.h" #include "rendering/texture3d.h"
#include "timeutil.h"
#include <GL/glew.h> #include <GL/glew.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <glm/ext/vector_float4.hpp>
#include <string>
extern int viewportWidth, viewportHeight; extern int viewportWidth, viewportHeight;
extern Texture3D* fontTexture; extern Texture3D* fontTexture;
extern Shader* fontShader; extern Shader* fontShader;
extern Shader* identityShader;
void renderChar(char c, int x, int y, float scale=1.f) { void drawChar(char c, int x, int y, float scale=1.f) {
fontShader->use(); fontShader->use();
fontTexture->activate(1); fontTexture->activate(1);
fontShader->set("fontTex", 1); fontShader->set("fontTex", 1);
@ -30,16 +34,57 @@ void renderChar(char c, int x, int y, float scale=1.f) {
glEnd(); glEnd();
} }
void renderString(std::string str, int x, int y, float scale=1.f) { void drawString(std::string str, int x, int y, float scale=1.f) {
for (int i = 0; i < (int)str.length(); i++) { for (int i = 0; i < (int)str.length(); i++) {
char c = str[i]; char c = str[i];
renderChar(c, x+i*8*scale, y, scale); drawChar(c, x+i*8*scale, y, scale);
} }
} }
void drawRect(int x, int y, int w, int h, glm::vec4 color) {
identityShader->use();
identityShader->set("aColor", color);
float x0 = 2*float(x)/viewportWidth-1, y0 = 2*float(y)/viewportHeight-1, x1 = 2*float(x + w)/viewportWidth-1, y1 = 2*float(y + h)/viewportHeight-1;
float tmp;
tmp = -y0, y0 = -y1, y1 = tmp;
glBegin(GL_QUADS);
glVertex3f(x0, y0, 0);
glVertex3f(x1, y0, 0);
glVertex3f(x1, y1, 0);
glVertex3f(x0, y1, 0);
glEnd();
}
static tu_time_t lastTime;
extern tu_time_t renderTime;
extern tu_time_t physTime;
extern tu_time_t schedTime;
// Draws debug info window // Draws debug info window
// Including info about framerates, etc. // Including info about framerates, etc.
void renderDebugInfo() { void renderDebugInfo() {
// renderString("Hello, test!", 50, 50, 2); tu_time_t timePassed = tu_clock_micros() - lastTime;
renderString("Hello, test!", 0, 0, 1); float frames = 1/(((float)timePassed)/1'000'000);
glDisable(GL_DEPTH_TEST);
drawRect(0, 0, 200, 16*8, glm::vec4(0.2f,0.2f,0.2f,0.8f));
drawString("FPS: " + std::to_string((int)frames), 0, 16*0);
drawString(" 1/: " + std::to_string((float)timePassed/1'000'000), 0, 16*1);
frames = 1/(((float)renderTime)/1'000'000);
drawString("RPS: " + std::to_string((int)frames), 0, 16*2);
drawString(" 1/: " + std::to_string((float)renderTime/1'000'000), 0, 16*3);
frames = 1/(((float)physTime)/1'000'000);
drawString("PPS: " + std::to_string((int)frames), 0, 16*4);
drawString(" 1/: " + std::to_string((float)physTime/1'000'000), 0, 16*5);
frames = 1/(((float)schedTime)/1'000'000);
drawString("SPS: " + std::to_string((int)frames), 0, 16*6);
drawString(" 1/: " + std::to_string((float)schedTime/1'000'000), 0, 16*7);
lastTime = tu_clock_micros();
} }

View file

@ -32,6 +32,7 @@
#include "skybox.h" #include "skybox.h"
#include "enum/surface.h" #include "enum/surface.h"
#include "texture3d.h" #include "texture3d.h"
#include "timeutil.h"
#include "renderer.h" #include "renderer.h"
@ -331,7 +332,7 @@ void renderHandles() {
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
identityShader->use(); identityShader->use();
identityShader->set("aColor", glm::vec3(0.f, 1.f, 1.f)); identityShader->set("aColor", glm::vec4(0.f, 1.f, 1.f, 1.f));
for (auto face : HandleFace::Faces) { for (auto face : HandleFace::Faces) {
CFrame cframe = getHandleCFrame(face); CFrame cframe = getHandleCFrame(face);
@ -634,9 +635,12 @@ void addDebugRenderCFrame(CFrame frame, Color3 color) {
DEBUG_CFRAMES.push_back(std::make_pair(frame, color)); DEBUG_CFRAMES.push_back(std::make_pair(frame, color));
} }
tu_time_t renderTime;
void render(GLFWwindow* window) { void render(GLFWwindow* window) {
tu_time_t startTime = tu_clock_micros();
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
renderSkyBox(); renderSkyBox();
renderHandles(); renderHandles();
@ -652,6 +656,7 @@ void render(GLFWwindow* window) {
renderDebugInfo(); renderDebugInfo();
// TODO: Make this a debug flag // TODO: Make this a debug flag
// renderAABB(); // renderAABB();
renderTime = tu_clock_micros() - startTime;
} }
void setViewport(int width, int height) { void setViewport(int width, int height) {

View file

@ -100,6 +100,10 @@ void Shader::set(std::string key, glm::vec3 value) {
glUniform3f(glGetUniformLocation(id, key.c_str()), value.x, value.y, value.z); glUniform3f(glGetUniformLocation(id, key.c_str()), value.x, value.y, value.z);
} }
void Shader::set(std::string key, glm::vec4 value) {
glUniform4f(glGetUniformLocation(id, key.c_str()), value.x, value.y, value.z, value.w);
}
void Shader::set(std::string key, glm::mat3 value) { void Shader::set(std::string key, glm::mat3 value) {
glUniformMatrix3fv(glGetUniformLocation(id, key.c_str()), 1, GL_FALSE, glm::value_ptr(value)); glUniformMatrix3fv(glGetUniformLocation(id, key.c_str()), 1, GL_FALSE, glm::value_ptr(value));
} }

View file

@ -19,6 +19,7 @@ public:
void set(std::string key, DirLight value); void set(std::string key, DirLight value);
void set(std::string key, PointLight value); void set(std::string key, PointLight value);
void set(std::string key, glm::vec3 value); void set(std::string key, glm::vec3 value);
void set(std::string key, glm::vec4 value);
void set(std::string key, glm::mat3 value); void set(std::string key, glm::mat3 value);
void set(std::string key, glm::mat4 value); void set(std::string key, glm::mat4 value);