Class: Engine::Metal::Device

Inherits:
Object
  • Object
show all
Includes:
Fiddle
Defined in:
lib/engine/metal/device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDevice

Returns a new instance of Device.



16
17
18
19
20
21
22
# File 'lib/engine/metal/device.rb', line 16

def initialize
  @device = MetalFramework.create_system_default_device
  raise "Failed to create Metal device" if @device.null?

  @command_queue = ObjC.msg(@device, 'newCommandQueue')
  raise "Failed to create command queue" if @command_queue.null?
end

Instance Attribute Details

#command_queueObject (readonly)

Returns the value of attribute command_queue.



10
11
12
# File 'lib/engine/metal/device.rb', line 10

def command_queue
  @command_queue
end

#deviceObject (readonly)

Returns the value of attribute device.



10
11
12
# File 'lib/engine/metal/device.rb', line 10

def device
  @device
end

Class Method Details

.instanceObject



12
13
14
# File 'lib/engine/metal/device.rb', line 12

def self.instance
  @instance ||= new
end

Instance Method Details

#new_command_bufferObject



93
94
95
# File 'lib/engine/metal/device.rb', line 93

def new_command_buffer
  ObjC.msg(@command_queue, 'commandBuffer')
end

#new_compute_pipeline(library, function_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/engine/metal/device.rb', line 59

def new_compute_pipeline(library, function_name)
  # Get function from library
  func_name_nsstring = ObjC.msg(
    ObjC.cls('NSString'),
    'stringWithUTF8String:',
    function_name
  )
  function = ObjC.msg(library, 'newFunctionWithName:', func_name_nsstring)
  raise "Failed to find function '#{function_name}'" if function.null?

  # Create compute pipeline
  error_ptr = Pointer.malloc(8)
  error_ptr[0, 8] = [0].pack('Q')

  pipeline = ObjC.msg(
    @device,
    'newComputePipelineStateWithFunction:error:',
    function,
    error_ptr
  )

  if pipeline.null?
    error = Pointer.new(error_ptr[0, 8].unpack('Q')[0])
    unless error.null?
      desc = ObjC.msg(error, 'localizedDescription')
      utf8 = ObjC.msg(desc, 'UTF8String')
      raise "Failed to create compute pipeline: #{utf8.to_s}"
    end
    raise "Failed to create compute pipeline with unknown error"
  end

  pipeline
end

#new_library_with_source(source) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/engine/metal/device.rb', line 24

def new_library_with_source(source)
  # Create NSString from source
  ns_string_class = ObjC.cls('NSString')
  source_nsstring = ObjC.msg(
    ns_string_class,
    'stringWithUTF8String:',
    source
  )

  # Create error pointer
  error_ptr = Pointer.malloc(8)
  error_ptr[0, 8] = [0].pack('Q')

  # Compile the library
  library = ObjC.msg(
    @device,
    'newLibraryWithSource:options:error:',
    source_nsstring,
    nil,
    error_ptr
  )

  if library.null?
    error = Pointer.new(error_ptr[0, 8].unpack('Q')[0])
    unless error.null?
      desc = ObjC.msg(error, 'localizedDescription')
      utf8 = ObjC.msg(desc, 'UTF8String')
      raise "Metal shader compilation failed: #{utf8.to_s}"
    end
    raise "Metal shader compilation failed with unknown error"
  end

  library
end