Class: Stagecraft::Renderer::Frame

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(texture:, width:, height:, device:, queue:, readable:) ⇒ Frame

Returns a new instance of Frame.



8
9
10
11
12
13
14
15
# File 'lib/stagecraft/renderer/frame.rb', line 8

def initialize(texture:, width:, height:, device:, queue:, readable:)
  @texture = texture
  @width = width
  @height = height
  @device = device
  @queue = queue
  @readable = readable
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#textureObject (readonly)

Returns the value of attribute texture.



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

def texture
  @texture
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#read_pixelsObject

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stagecraft/renderer/frame.rb', line 17

def read_pixels
  raise Error, "surface frames cannot be read after presentation; use Renderer.offscreen" unless @readable

  require "texel"
  bytes_per_row = align(width * 4, 256)
  padded = @queue.read_texture(
    source: { texture: },
    data_layout: { bytes_per_row:, rows_per_image: height },
    size: { width:, height:, depth_or_array_layers: 1 },
    device: @device
  )
  pixels = if bytes_per_row == width * 4
             padded
           else
             height.times.map { |row| padded.byteslice(row * bytes_per_row, width * 4) }.join
           end
  Texel::Image.new(
    width:, height:, channels: 4, dtype: :u8, color_space: :srgb, data: pixels
  )
end