mirror of
https://github.com/wolfpld/tracy.git
synced 2026-06-11 02:03:48 +00:00
Compare commits
5 Commits
slomp/gl-m
...
slomp/gl-e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
debda1df55 | ||
|
|
d98608b022 | ||
|
|
eb88c6eba0 | ||
|
|
e83429c926 | ||
|
|
1b207d3e2a |
84
examples/opengl/triangle/CMakeLists.txt
Normal file
84
examples/opengl/triangle/CMakeLists.txt
Normal file
@@ -0,0 +1,84 @@
|
||||
# CMakeLists.txt — OpenGL spinning triangle demo
|
||||
#
|
||||
# macOS:
|
||||
# cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -B build/ninja .
|
||||
# cmake --build build/ninja
|
||||
#
|
||||
# Linux (requires libx11-dev libgl1-mesa-dev libglew-dev):
|
||||
# cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -B build/ninja .
|
||||
# cmake --build build/ninja
|
||||
#
|
||||
# Windows (MSVC, requires GLEW):
|
||||
# cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DGLEW_ROOT=<path> -B build/ninja .
|
||||
# cmake --build build/ninja
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(gl_spinning_triangle LANGUAGES C CXX)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tracy root — defaults to three directories above this CMakeLists.txt.
|
||||
# ---------------------------------------------------------------------------
|
||||
set(TRACY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..")
|
||||
option(TRACY_ENABLE "Enable Tracy profiling" ON)
|
||||
option(TRACY_OPENGL_AUTO_CALIBRATION "Enable periodic GPU/CPU recalibration" ON)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Platform — RGFW (cross-platform windowing, fetched automatically)
|
||||
# ---------------------------------------------------------------------------
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(rgfw
|
||||
GIT_REPOSITORY https://github.com/ColleagueRiley/RGFW.git
|
||||
GIT_TAG main # pin to a specific commit for reproducible builds
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(rgfw)
|
||||
|
||||
set(PLATFORM_SOURCES platform/platform_rgfw.cpp)
|
||||
set(PLATFORM_INCLUDES ${rgfw_SOURCE_DIR})
|
||||
|
||||
if(APPLE)
|
||||
set(PLATFORM_LIBS "-framework Cocoa" "-framework OpenGL"
|
||||
"-framework CoreVideo" "-framework IOKit")
|
||||
elseif(WIN32)
|
||||
find_package(GLEW REQUIRED)
|
||||
set(PLATFORM_LIBS opengl32 user32 gdi32 GLEW::GLEW)
|
||||
else()
|
||||
find_package(GLEW REQUIRED)
|
||||
find_package(X11 REQUIRED)
|
||||
set(PLATFORM_LIBS X11::X11 GL GLEW::GLEW)
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Target
|
||||
# ---------------------------------------------------------------------------
|
||||
add_executable(gl_spinning_triangle
|
||||
spinning_triangle.cpp
|
||||
"${TRACY_DIR}/public/TracyClient.cpp"
|
||||
${PLATFORM_SOURCES}
|
||||
)
|
||||
|
||||
# Suppress upstream warnings from TracyClient.cpp
|
||||
if(MSVC)
|
||||
set_source_files_properties("${TRACY_DIR}/public/TracyClient.cpp"
|
||||
PROPERTIES COMPILE_FLAGS "/w"
|
||||
)
|
||||
else()
|
||||
set_source_files_properties("${TRACY_DIR}/public/TracyClient.cpp"
|
||||
PROPERTIES COMPILE_FLAGS "-w"
|
||||
)
|
||||
endif()
|
||||
|
||||
target_compile_features(gl_spinning_triangle PRIVATE cxx_std_17)
|
||||
|
||||
if(TRACY_ENABLE)
|
||||
target_compile_definitions(gl_spinning_triangle PRIVATE TRACY_ENABLE)
|
||||
endif()
|
||||
if(TRACY_OPENGL_AUTO_CALIBRATION)
|
||||
target_compile_definitions(gl_spinning_triangle PRIVATE TRACY_OPENGL_AUTO_CALIBRATION)
|
||||
endif()
|
||||
|
||||
target_include_directories(gl_spinning_triangle PRIVATE
|
||||
"${TRACY_DIR}/public"
|
||||
${PLATFORM_INCLUDES}
|
||||
)
|
||||
target_link_libraries(gl_spinning_triangle PRIVATE ${PLATFORM_LIBS})
|
||||
41
examples/opengl/triangle/platform/platform.h
Normal file
41
examples/opengl/triangle/platform/platform.h
Normal file
@@ -0,0 +1,41 @@
|
||||
// platform.h — interface between platform-agnostic code and platform backends
|
||||
//
|
||||
// Each platform_*.mm / platform_*.cpp file implements these four functions.
|
||||
// Exactly one backend must be linked into the final binary.
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __APPLE__
|
||||
// OpenGL is only available on MacOS (no iOS support)
|
||||
// Anything from gl3.h will spew deprecation warnings when used,
|
||||
// unless GL_SILENCE_DEPRECATION has been defined beforehand
|
||||
//# define GL_SILENCE_DEPRECATION
|
||||
# include <OpenGL/gl3.h>
|
||||
#else
|
||||
# include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
// Initialize the windowing system, create a window, and make an OpenGL 3.3
|
||||
// Core Profile context current on the calling thread.
|
||||
// Returns true on success.
|
||||
bool platformInit(int width, int height, const char* title);
|
||||
|
||||
// Load OpenGL function pointers (no-op on macOS where the framework exports them directly).
|
||||
// Must be called after platformInit() while the GL context is current.
|
||||
// Returns true on success.
|
||||
bool platformInitGL();
|
||||
|
||||
// Elapsed wall-clock time in seconds since platformInit().
|
||||
double platformGetTime();
|
||||
|
||||
// Swap front and back buffers (present the rendered frame).
|
||||
void platformSwapBuffers();
|
||||
|
||||
// Pixel scaling factor relative to the logical window size (1.0 on non-HiDPI displays).
|
||||
// Must be called after platformInit().
|
||||
void platformGetPixelDensityScale(float* x, float* y);
|
||||
|
||||
// Enter the platform event/render loop.
|
||||
// Calls render() each frame at ~60 fps.
|
||||
// Calls shutdown() exactly once before returning.
|
||||
void platformRunLoop(void (*render)(), void (*shutdown)());
|
||||
73
examples/opengl/triangle/platform/platform_rgfw.cpp
Normal file
73
examples/opengl/triangle/platform/platform_rgfw.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
// platform_rgfw.cpp — RGFW windowing backend (cross-platform)
|
||||
// https://github.com/ColleagueRiley/RGFW
|
||||
|
||||
#include "platform.h" // GL headers first (gl3.h / glew.h) so RGFW sees guards set
|
||||
|
||||
#define RGFW_OPENGL
|
||||
#define RGFW_IMPLEMENTATION
|
||||
#include <RGFW.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
|
||||
static RGFW_window* sWin = nullptr;
|
||||
static std::chrono::steady_clock::time_point sStartTime;
|
||||
|
||||
bool platformInit(int width, int height, const char* title) {
|
||||
RGFW_glHints* hints = RGFW_getGlobalHints_OpenGL();
|
||||
hints->major = 3;
|
||||
hints->minor = 3;
|
||||
RGFW_setGlobalHints_OpenGL(hints);
|
||||
|
||||
sWin = RGFW_createWindow(title, 0, 0, width, height,
|
||||
RGFW_windowCenter | RGFW_windowOpenGL);
|
||||
if (!sWin) {
|
||||
fprintf(stderr, "RGFW: failed to create window\n");
|
||||
return false;
|
||||
}
|
||||
RGFW_window_makeCurrentContext_OpenGL(sWin);
|
||||
RGFW_window_swapInterval_OpenGL(sWin, 1);
|
||||
RGFW_window_setExitKey(sWin, RGFW_keyEscape);
|
||||
|
||||
sStartTime = std::chrono::steady_clock::now();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool platformInitGL() {
|
||||
#ifndef __APPLE__
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK) {
|
||||
fprintf(stderr, "Failed to initialize GLEW\n");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
double platformGetTime() {
|
||||
return std::chrono::duration<double>(
|
||||
std::chrono::steady_clock::now() - sStartTime).count();
|
||||
}
|
||||
|
||||
void platformSwapBuffers() { RGFW_window_swapBuffers_OpenGL(sWin); }
|
||||
|
||||
void platformGetPixelDensityScale(float* x, float* y) {
|
||||
i32 pw, ph;
|
||||
RGFW_window_getSizeInPixels(sWin, &pw, &ph);
|
||||
*x = (float)pw / (float)sWin->w;
|
||||
*y = (float)ph / (float)sWin->h;
|
||||
}
|
||||
|
||||
void platformRunLoop(void (*render)(), void (*shutdown)()) {
|
||||
while (RGFW_window_shouldClose(sWin) == RGFW_FALSE) {
|
||||
RGFW_event event;
|
||||
while (RGFW_window_checkEvent(sWin, &event)) {
|
||||
if (event.type == RGFW_windowClose) goto done;
|
||||
}
|
||||
render();
|
||||
}
|
||||
done:
|
||||
shutdown();
|
||||
RGFW_window_close(sWin);
|
||||
sWin = nullptr;
|
||||
}
|
||||
136
examples/opengl/triangle/spinning_triangle.cpp
Normal file
136
examples/opengl/triangle/spinning_triangle.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
// spinning_triangle.cpp — OpenGL spinning triangle demo with Tracy GPU profiling.
|
||||
//
|
||||
// Tracy GPU zones are active on non-Apple platforms when TRACY_ENABLE is defined.
|
||||
// TRACY_OPENGL_AUTO_CALIBRATION (enabled by default in CMakeLists.txt) enables
|
||||
// periodic GPU/CPU drift correction via glGetInteger64v(GL_TIMESTAMP).
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
|
||||
#include <tracy/Tracy.hpp>
|
||||
#include <tracy/TracyOpenGL.hpp>
|
||||
|
||||
static const int kWidth = 800;
|
||||
static const int kHeight = 600;
|
||||
|
||||
static GLuint gProgram = 0;
|
||||
static GLuint gVao = 0;
|
||||
static GLint gAngleLoc = -1;
|
||||
|
||||
// Vertex colors and positions are baked in; rotation is driven by a uniform.
|
||||
static const char* kVertSrc = R"(
|
||||
#version 150 core
|
||||
uniform float uAngle;
|
||||
const vec2 kPos[3] = vec2[3](
|
||||
vec2( 0.0, 0.5 ),
|
||||
vec2(-0.433, -0.25 ),
|
||||
vec2( 0.433, -0.25 )
|
||||
);
|
||||
const vec3 kCol[3] = vec3[3](
|
||||
vec3(1.0, 0.0, 0.0),
|
||||
vec3(0.0, 1.0, 0.0),
|
||||
vec3(0.0, 0.0, 1.0)
|
||||
);
|
||||
out vec3 vColor;
|
||||
void main() {
|
||||
float c = cos(uAngle);
|
||||
float s = sin(uAngle);
|
||||
vec2 p = kPos[gl_VertexID];
|
||||
gl_Position = vec4(p.x*c - p.y*s, p.x*s + p.y*c, 0.0, 1.0);
|
||||
vColor = kCol[gl_VertexID];
|
||||
}
|
||||
)";
|
||||
|
||||
static const char* kFragSrc = R"(
|
||||
#version 150 core
|
||||
in vec3 vColor;
|
||||
out vec4 fragColor;
|
||||
void main() { fragColor = vec4(vColor, 1.0); }
|
||||
)";
|
||||
|
||||
static GLuint compileShader(GLenum type, const char* src) {
|
||||
GLuint s = glCreateShader(type);
|
||||
glShaderSource(s, 1, &src, nullptr);
|
||||
glCompileShader(s);
|
||||
GLint ok = 0;
|
||||
glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
|
||||
if (!ok) {
|
||||
char log[512];
|
||||
glGetShaderInfoLog(s, sizeof(log), nullptr, log);
|
||||
fprintf(stderr, "Shader compile error: %s\n", log);
|
||||
glDeleteShader(s);
|
||||
return 0;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static int initGL() {
|
||||
if (!platformInitGL()) return 1;
|
||||
|
||||
TracyGpuContext;
|
||||
TracyGpuContextName("OpenGL", 6);
|
||||
|
||||
GLuint vert = compileShader(GL_VERTEX_SHADER, kVertSrc);
|
||||
GLuint frag = compileShader(GL_FRAGMENT_SHADER, kFragSrc);
|
||||
if (!vert || !frag) return 1;
|
||||
|
||||
gProgram = glCreateProgram();
|
||||
glAttachShader(gProgram, vert);
|
||||
glAttachShader(gProgram, frag);
|
||||
glLinkProgram(gProgram);
|
||||
glDeleteShader(vert);
|
||||
glDeleteShader(frag);
|
||||
|
||||
GLint ok = 0;
|
||||
glGetProgramiv(gProgram, GL_LINK_STATUS, &ok);
|
||||
if (!ok) {
|
||||
char log[512];
|
||||
glGetProgramInfoLog(gProgram, sizeof(log), nullptr, log);
|
||||
fprintf(stderr, "Program link error: %s\n", log);
|
||||
return 1;
|
||||
}
|
||||
|
||||
gAngleLoc = glGetUniformLocation(gProgram, "uAngle");
|
||||
|
||||
// Core profile requires a bound VAO even with no vertex attributes.
|
||||
glGenVertexArrays(1, &gVao);
|
||||
glBindVertexArray(gVao);
|
||||
|
||||
glClearColor(0.05f, 0.05f, 0.08f, 1.0f);
|
||||
float scaleX, scaleY;
|
||||
platformGetPixelDensityScale(&scaleX, &scaleY);
|
||||
glViewport(0, 0, (int)(kWidth * scaleX), (int)(kHeight * scaleY));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void renderFrame() {
|
||||
ZoneScoped;
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glUseProgram(gProgram);
|
||||
|
||||
{
|
||||
TracyGpuZone("triangle draw");
|
||||
glUniform1f(gAngleLoc, (float)platformGetTime());
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
}
|
||||
|
||||
platformSwapBuffers();
|
||||
TracyGpuCollect;
|
||||
}
|
||||
|
||||
static void shutdown() {
|
||||
fprintf(stderr, "application is shutting down...\n");
|
||||
glDeleteVertexArrays(1, &gVao);
|
||||
glDeleteProgram(gProgram);
|
||||
}
|
||||
|
||||
int main() {
|
||||
if (!platformInit(kWidth, kHeight, "OpenGL Spinning Triangle"))
|
||||
return 1;
|
||||
if (initGL() != 0)
|
||||
return 2;
|
||||
platformRunLoop(renderFrame, shutdown);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
#ifndef __TRACYOPENGL_HPP__
|
||||
#define __TRACYOPENGL_HPP__
|
||||
|
||||
#if !defined TRACY_ENABLE || defined __APPLE__
|
||||
#ifdef __APPLE__
|
||||
#define TRACY_OPENGL_DISABLE
|
||||
#warning "OpenGL support on Apple devices is deprecated or unavailable."
|
||||
#endif
|
||||
|
||||
#if !defined TRACY_ENABLE || defined TRACY_OPENGL_DISABLE
|
||||
|
||||
#define TracyGpuContext
|
||||
#define TracyGpuContextName(x,y)
|
||||
@@ -98,17 +103,25 @@ public:
|
||||
, m_head( 0 )
|
||||
, m_tail( 0 )
|
||||
{
|
||||
ZoneScopedC( Color::Red4 );
|
||||
|
||||
assert( m_context != 255 );
|
||||
|
||||
glGenQueries( QueryCount, m_query );
|
||||
GLint bits;
|
||||
glGetQueryiv( GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, &bits );
|
||||
if( bits == 0 )
|
||||
{
|
||||
// all timestamp queries would resolve to 0 (and produce 0ns GPU zones).
|
||||
// (this is the case for many TBDR GPUs, including Apple Silicon)
|
||||
Profiler::LogString( MessageSourceType::Tracy, MessageSeverity::Warning, Color::Tomato, 0,
|
||||
"OpenGL driver does not implement GL_TIMESTAMP precision." );
|
||||
}
|
||||
assert( bits > 0 );
|
||||
|
||||
int64_t tgpu;
|
||||
glGetInteger64v( GL_TIMESTAMP, &tgpu );
|
||||
int64_t tcpu = Profiler::GetTime();
|
||||
|
||||
GLint bits;
|
||||
glGetQueryiv( GL_TIMESTAMP, GL_QUERY_COUNTER_BITS, &bits );
|
||||
|
||||
#ifdef TRACY_OPENGL_AUTO_CALIBRATION
|
||||
// The anchor above is never refreshed; advertise calibration and emit periodic
|
||||
// GpuCalibration events to correct CPU/GPU drift (see Recalibrate). Opt-in,
|
||||
@@ -117,6 +130,8 @@ public:
|
||||
m_prevCalibration = GetHostTimeNs();
|
||||
#endif
|
||||
|
||||
glGenQueries( QueryCount, m_query );
|
||||
|
||||
const float period = 1.f;
|
||||
const auto thread = GetThreadHandle();
|
||||
TracyLfqPrepare( QueueType::GpuNewContext );
|
||||
|
||||
Reference in New Issue
Block a user