Optick C++ Profiler

repository·master·Indexed 25 days ago

https://github.com/bombomby/optick

A lightweight C++ profiler designed for games, formerly known as Brofiler. It provides instrumentation, sampling, switch-context analysis, and GPU counter support for D3D12 and Vulkan. The library includes dedicated plugins for Unreal Engine 4 and 5 and can be configured via macros such as USE_OPTICK, OPTICK_ENABLE_TRACING, and OPTICK_ENABLE_GPU.

Tokens
1.2K
Snippets
2
Records
7
Agent score
85%

What's inside Optick

  1. Integrate Optick into a C++ project

    master

    To perform basic instrumentation in your game, follow these steps:

    1. Copy Source: Copy the src folder from the Optick repository or latest release into your game project.
    2. Include Header: Add #include "optick.h" to your source files.
    3. Frame Instrumentation: Add the OPTICK_FRAME("Name"); macro inside your main loop to mark the start of a frame.
    4. Function Instrumentation: Use the OPTICK_EVENT(); macro inside functions you want to profile.
    5. Thread Instrumentation: Use the OPTICK_THREAD("Name"); macro at the beginning of new threads to declare them in Optick.
    6. Configuration: Edit optick.config.h to enable or disable specific features or to disable Optick entirely in final production builds.

    Important for Dynamic Linking: If your game uses dynamic linking and you intend to use Optick from multiple DLLs within the same executable, you must:

    • Add Optick's code to a common Dynamic Library.
    • Compile that library with the OPTICK_EXPORT define.

    Alternatively, you can use the precompiled OptickCore.dll provided in the release:

    • Add the include folder to your project's extra include directories.
    • Add lib/x64/debug and lib/x64/release to your extra library directories.
    • Copy OptickCore.dll from the respective folders to your project's debug/release output folders.
    #include "optick.h"
    
    // In your main loop
    while( true )
    {
    	OPTICK_FRAME("MainThread");
    	engine.Update();
    }
    
    // To instrument a function
    void SlowFunction()
    {
    	OPTICK_EVENT();
    	...
    }
    
    // To declare a new thread
    void WorkerThread(...)
    {
    	OPTICK_THREAD("Worker");
    	while (isRunning)
    	{
    		...
    	}
    }
  2. Upgrade from Brofiler to Optick

    master

    If you are migrating an existing project from Brofiler to Optick (v1.2.0+), use the following macro mappings:

    Old Brofiler MacroNew Optick Macro
    BROFILER_FRAME("Name");OPTICK_FRAME("Name");
    BROFILER_THREAD("Name");OPTICK_THREAD("Name");
    BROFILER_CATEGORY("Name", Color);OPTICK_CATEGORY("Name", Optick::Category::Name);
    BROFILER_EVENT(NAME);OPTICK_EVENT(NAME);
    PROFILE;OPTICK_EVENT();
  3. Run Optick Samples

    master

    To explore Optick integration via samples:

    1. Generate Projects:
      • For GPU samples (requires VulkanSDK): Run tools/GenerateProjects_gpu.bat.
      • For minimal ConsoleApp samples: Run tools/GenerateProjects.bat.
    2. Open Solution: Open the generated solution at build\vs2017\Optick.sln.

    Available samples include:

    • WindowsD3D12: DirectX12 multithreading sample.
    • WindowsVulkan: Vulkan multithreading sample.
    • ConsoleApp: Basic ConsoleApp integration (Windows, Linux, MacOS).
  4. Configure Optick global settings via macros

    master

    Optick is configured using preprocessor macros. You can control the profiler's presence and its feature set (tracing and GPU support) by defining these macros before including Optick headers.

    Master Switch

    • USE_OPTICK: The primary switch to enable or disable the profiler. Set to (0) to disable the profiler entirely in final builds.

    Feature Toggles

    • OPTICK_ENABLE_TRACING: Enables low-level platform-specific tracing, such as switch contexts and autosampling.
    • OPTICK_ENABLE_GPU: Enables GPU counter support.

    GPU API Specifics

    If OPTICK_ENABLE_GPU is enabled, you can further specify the API:

    • OPTICK_ENABLE_GPU_D3D12: Enables D3D12 GPU support.
    • OPTICK_ENABLE_GPU_VULKAN: Enables Vulkan GPU support.
  5. Reference: Optick configuration macros

    master

    The following macros are used to configure the Optick profiler behavior. These should be defined in your build system or at the top of your configuration header.

    // Master Switch
    USE_OPTICK
    
    // Tracing
    OPTICK_ENABLE_TRACING
    
    // GPU Support
    OPTICK_ENABLE_GPU
    OPTICK_ENABLE_GPU_D3D12
    OPTICK_ENABLE_GPU_VULKAN