Class: Rendering::GpuTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/engine/rendering/gpu_timer.rb

Class Method Summary collapse

Class Method Details

.enableObject



10
11
12
13
14
15
16
# File 'lib/engine/rendering/gpu_timer.rb', line 10

def enable
  @enabled = true
  @frame_count = 0
  @stages = []
  @queries = {}
  puts "GPU Profiler: ON"
end

.enabled?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/engine/rendering/gpu_timer.rb', line 6

def enabled?
  @enabled ||= false
end

.measure(stage) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/engine/rendering/gpu_timer.rb', line 18

def measure(stage)
  unless enabled?
    return yield
  end

  query_id = query_for(stage)
  Engine::GL.BeginQuery(Engine::GL::TIME_ELAPSED, query_id)
  result = yield
  Engine::GL.EndQuery(Engine::GL::TIME_ELAPSED)
  result
end


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/engine/rendering/gpu_timer.rb', line 30

def print_results
  return unless enabled?

  @frame_count += 1
  return unless (@frame_count % 60) == 0

  results = {}
  @stages.each do |stage|
    buf = ' ' * 8
    Engine::GL.GetQueryObjectui64v(@queries[stage], Engine::GL::QUERY_RESULT, buf)
    results[stage] = buf.unpack1('Q') / 1_000_000.0
  end

  total = results.values.sum
  puts "\n=== GPU Timing ==="
  @stages.each do |stage|
    ms = results[stage]
    pct = total > 0 ? (ms / total * 100).round(1) : 0
    bar = "█" * (pct / 5).to_i
    puts format("%-20s %6.2f ms (%5.1f%%) %s", stage, ms, pct, bar)
  end
  puts format("%-20s %6.2f ms", "TOTAL", total)
  puts "===================="
end