Class: Stagecraft::Renderer::GPUContext

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/renderer/gpu_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window: nil, width:, height:, device: nil, queue: nil, surface: nil) ⇒ GPUContext

Returns a new instance of GPUContext.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stagecraft/renderer/gpu_context.rb', line 8

def initialize(window: nil, width:, height:, device: nil, queue: nil, surface: nil)
  WGPUCompatibility.install!
  @window = window
  @width = Integer(width)
  @height = Integer(height)
  @owns_device = device.nil?
  @disposed = false
  if device
    @device = device
    @queue = queue || device.queue
    @surface = surface
    @adapter = device.respond_to?(:adapter) ? device.adapter : nil
  else
    initialize_wgpu
  end
  @surface_format = choose_surface_format
  configure_surface if @surface
rescue StandardError
  dispose_after_failed_initialization
  raise
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def adapter
  @adapter
end

#deviceObject (readonly)

Returns the value of attribute device.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def device
  @device
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def height
  @height
end

#instanceObject (readonly)

Returns the value of attribute instance.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def instance
  @instance
end

#queueObject (readonly)

Returns the value of attribute queue.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def queue
  @queue
end

#surfaceObject (readonly)

Returns the value of attribute surface.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def surface
  @surface
end

#surface_formatObject (readonly)

Returns the value of attribute surface_format.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def surface_format
  @surface_format
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/stagecraft/renderer/gpu_context.rb', line 6

def width
  @width
end

Instance Method Details

#current_targetObject

Raises:



41
42
43
44
45
46
# File 'lib/stagecraft/renderer/gpu_context.rb', line 41

def current_target
  raise Error, "renderer has no surface" unless surface

  texture = surface.current_texture
  [texture, texture.create_view]
end

#disposeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/stagecraft/renderer/gpu_context.rb', line 52

def dispose
  return self if @disposed

  @disposed = true
  cleanups = [
    -> { surface&.unconfigure },
    -> { surface&.release }
  ]
  if @owns_device
    cleanups.concat(
      [
        -> { device.destroy if device.respond_to?(:destroy) },
        -> { device.release if device.respond_to?(:release) },
        -> { adapter.release if adapter&.respond_to?(:release) },
        -> { instance.release if instance&.respond_to?(:release) }
      ]
    )
  end
  first_error = nil
  cleanups.each do |cleanup|
    cleanup.call
  rescue StandardError => error
    first_error ||= error
  end
  raise first_error if first_error

  self
end

#presentObject



48
49
50
# File 'lib/stagecraft/renderer/gpu_context.rb', line 48

def present
  surface&.present
end

#resize(width, height) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/stagecraft/renderer/gpu_context.rb', line 30

def resize(width, height)
  next_width = [Integer(width), 1].max
  next_height = [Integer(height), 1].max
  return false if next_width == @width && next_height == @height

  @width = next_width
  @height = next_height
  configure_surface if surface
  true
end