GraphicsTimer
Package: Libraries.Game.Graphics.Timing · Inherits from: Libraries.Language.Object
The GraphicsTimer measures how long the GPU spends on named phases of each frame, using Vulkan timestamp queries: the GPU writes its own clock at the start and end of each phase, and the timer reads the times back a frame or two later, without waiting. Turn it on with Game:SetGraphicsTiming(true). The 3D and 2D painters then time their own phases: "Shadows" (with "Shadows: directional", "Shadows: point" and "Shadows: spot" inside it), "G-buffer", "Lighting and forward" (the lighting pass and forward programs such as water), "Tone mapping", "Bloom" and "2D". Any code that records Vulkan commands can time its own phases with StartPhase and EndPhase, and phases may nest. A phase timed more than once in a frame is added up. Each frame's result is a GraphicsTimingFrame (GetLatestFrame). Give the timer a GraphicsTimingLog (SetLog) to keep frames, summarize them, chart them or write them as JSON. Limits: on Apple GPUs time is measured between render passes, not single draws; phases can overlap on the GPU, so they may not add up exactly to the frame; and the GPU changes its clock speed with load. Timing needs Vulkan, and isn't supported on iOS and Android yet (IsSupported).
Example Code
use Libraries.Game.Game
use Libraries.Game.Graphics.Timing.GraphicsTimer
use Libraries.Game.Graphics.Timing.GraphicsTimingFrame
class Main is Game
action Main
UseVulkan(true)
StartGame()
end
action CreateGame
SetGraphicsTiming(true)
end
action Update(number seconds)
GraphicsTimingFrame frame = GetGraphicsTimer():GetLatestFrame()
if frame not= undefined
output "G-buffer: " + frame:GetTime("G-buffer") + " ms"
end
end
end
Actions
BeginFrame(Libraries.Game.Graphics.Vulkan.VulkanCommandBuffer buffer, integer slot, integer frame)
This action starts timing a frame: it resets the slot's stamps and stamps the frame's start. VulkanGraphics calls it just after the frame's command buffer begins recording, outside any render pass.
Parameters
bufferLibraries.Game.Graphics.Vulkan.VulkanCommandBuffer: The frame's command bufferslotinteger: The frame's slot, from 0, one per frame in flightframeinteger: The frame's number
Collect(integer slot)
This action reads a slot's frame, once the GPU has finished it, into a GraphicsTimingFrame, and adds it to the log. VulkanGraphics calls it after waiting for the slot's fence, before the slot is used again.
Parameters
slotinteger: The slot, from 0
Dispose()
This action frees the timer's pools. VulkanGraphics calls it when Vulkan is shut down.
EndFrame(Libraries.Game.Graphics.Vulkan.VulkanCommandBuffer buffer)
This action stamps the frame's end. VulkanGraphics calls it just before the frame's command buffer stops recording.
Parameters
bufferLibraries.Game.Graphics.Vulkan.VulkanCommandBuffer: The frame's command buffer
EndPhase(Libraries.Game.Graphics.Vulkan.VulkanCommandBuffer buffer, text name)
This action stamps the end of a phase started with StartPhase.
Parameters
bufferLibraries.Game.Graphics.Vulkan.VulkanCommandBuffer: The command buffer being recordednametext: The phase's name
GetLatestFrame()
This action returns the last frame read, a frame or two behind the one being drawn, or undefined before the first.
Returns
Libraries.Game.Graphics.Timing.GraphicsTimingFrame: The frame
GetLog()
This action gives the timer a log that keeps each frame as it is read.
Returns
GetMaximumPhases()
This action sets how many phases a frame may time, counting each time a phase is timed (32 by default). Stamps past it are left out. It takes effect for pools made after it, so set it before turning timing on.
Returns
integer
HasLabels()
This action sets whether each phase is also a debug label, so tools such as Xcode's GPU frame capture show the phases by name (labels need Vulkan's debug extension, loaded with validation layers).
Returns
boolean
IsActive()
This action returns true if frames are being timed: timing is on and supported.
Returns
boolean: True if timing
IsEnabled()
This action returns true if timing is on.
Returns
boolean: True if on
IsSplittingForwardPass()
This action returns true if the 3D painter splits its forward programs into their own pass, while timing.
Returns
boolean: True if split
IsSupported()
This action returns true if the device can time frames: it is Vulkan, on the desktop for now, and its graphics queue can write timestamps.
Returns
boolean: True if supported
SetEnabled(boolean value)
This action turns timing on or off. Off, nothing is recorded, so it costs nothing.
Parameters
valueboolean: True to time frames
SetLabels(boolean value)
This action sets whether each phase is also a debug label, so tools such as Xcode's GPU frame capture show the phases by name (labels need Vulkan's debug extension, loaded with validation layers).
Parameters
valueboolean: True to label phases
SetLog(Libraries.Game.Graphics.Timing.GraphicsTimingLog value)
This action gives the timer a log that keeps each frame as it is read.
Parameters
valueLibraries.Game.Graphics.Timing.GraphicsTimingLog: The log, or undefined for none
SetMaximumPhases(integer count)
This action sets how many phases a frame may time, counting each time a phase is timed (32 by default). Stamps past it are left out. It takes effect for pools made after it, so set it before turning timing on.
Parameters
countinteger: The most phases
SetSplitForwardPass(boolean value)
This action sets whether the 3D painter draws its forward programs, such as water, in a render pass of their own, so "Lighting" and "Forward" are timed apart, even on Apple GPUs. It costs a little memory traffic, so it is off unless asked for.
Parameters
valueboolean: True to split them
Setup(Libraries.Game.Graphics.Vulkan.VulkanDevice device, number period, integer bits)
This action tells the timer which device it times and how its clock works. VulkanGraphics calls it once Vulkan is ready.
Parameters
deviceLibraries.Game.Graphics.Vulkan.VulkanDevice: The deviceperiodnumber: Nanoseconds per GPU clock tickbitsinteger: How many bits of a timestamp are valid, or 0 if the graphics queue can't write them
StartPhase(Libraries.Game.Graphics.Vulkan.VulkanCommandBuffer buffer, text name)
This action stamps the start of a phase. Call EndPhase with the same name to end it. Phases may nest, and a name may be used more than once a frame.
Parameters
bufferLibraries.Game.Graphics.Vulkan.VulkanCommandBuffer: The command buffer being recordednametext: The phase's name