Class: Vulkan::LogicalDevice

Inherits:
Object
  • Object
show all
Includes:
Checks, Conversions, Finalizer
Defined in:
lib/vulkan/logical_device.rb

Constant Summary

Constants included from Conversions

Conversions::ACCESS_MASK_BITS, Conversions::BORDER_COLORS, Conversions::BUFFER_USAGE_BITS, Conversions::COMPARE_OPS, Conversions::DEPENDENCY_FLAG_BITS, Conversions::DESCRIPTOR_TYPES, Conversions::DYNAMIC_STATES, Conversions::FILTERS, Conversions::FORMAT_FEATURE_BITS, Conversions::IMAGE_ASPECT_BITS, Conversions::IMAGE_CREATE_BITS, Conversions::IMAGE_FORMATS, Conversions::IMAGE_TILING, Conversions::IMAGE_TYPES, Conversions::IMAGE_USAGE_BITS, Conversions::MEMORY_PROPERTIES, Conversions::PIPELINE_STAGE_BITS, Conversions::PRESENT_MODES, Conversions::SAMPLER_ADDRESS_MODES, Conversions::SAMPLER_MIPMAP_MODES, Conversions::SHADER_STAGE_BITS, Conversions::SHARING_MODES, Conversions::SURFACE_TRANSFORMS, Conversions::VERTEX_INPUT_RATES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Finalizer

#finalize_with, included

Methods included from Conversions

#array_of_pointers, #array_of_structures, #array_of_uint32s, #bool_to_vk, #buffer_usage_flags_to_syms, #const_to_symbol, #cstr_to_rbstr, #flags_to_symbols, #flags_to_syms, #num_to_samples, #present_mode_to_sym, #queue_family_to_index, #struct_to_hash, #sym_to_blend_factor, #sym_to_blend_op, #sym_to_border_color, #sym_to_color_component_bit, #sym_to_command_buffer_level, #sym_to_command_buffer_usage, #sym_to_compare_op, #sym_to_cull_mode, #sym_to_descriptor_type, #sym_to_dynamic_state, #sym_to_filter, #sym_to_front_face, #sym_to_image_format, #sym_to_image_layout, #sym_to_image_tiling, #sym_to_image_type, #sym_to_index_type, #sym_to_load_op, #sym_to_pipeline_bind_point, #sym_to_polygon_mode, #sym_to_present_mode, #sym_to_sampler_address_mode, #sym_to_sampler_mipmap_mode, #sym_to_samples, #sym_to_sharing_mode, #sym_to_store_op, #sym_to_subpass_contents, #sym_to_topology, #sym_to_val, #sym_to_vertex_input_rate, #syms_to_access_mask, #syms_to_buffer_usage_flags, #syms_to_dependency_flags, #syms_to_descriptor_set_layout_type_flags, #syms_to_flags, #syms_to_format_feature_flags, #syms_to_image_aspect_flags, #syms_to_image_create_flags, #syms_to_image_usage_flags, #syms_to_memory_properties, #syms_to_pipeline_stage_flags, #syms_to_shader_stage_flags, #syms_to_surface_transforms, #vk_make_version, #vk_parse_version

Methods included from Checks

#check_result

Constructor Details

#initialize(instance, physical_device, queues:, extensions:, features: physical_device.supported_features) ⇒ LogicalDevice

Returns a new instance of LogicalDevice.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
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
58
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
92
# File 'lib/vulkan/logical_device.rb', line 11

def initialize(instance, physical_device, queues:, extensions:, features: physical_device.supported_features)
  raise ArgumentError, "instance can't be nil" unless instance
  raise ArgumentError, "physical_device can't be nil" unless physical_device
  @instance = instance
  @physical_device = physical_device

  extensions.concat ENV['DEVICE_EXTENSIONS'].split(/\:\s/) if ENV['DEVICE_EXTENSIONS']
  extensions << 'VK_KHR_portability_subset' if physical_device.extension_names.include?('VK_KHR_portability_subset')

  if queues.size == 0
    # take the first available queue, to satisfy the spec (must request a queue)
    queues = [{ family: physical_device.queue_families.first, priorities: [1.0] }]
  end

  queues_p = queues.each_with_index.map do |queue_info, index|
    family_index = queue_family_to_index(queue_info[:family])
    priorities   = queue_info[:priorities]   || raise(ArgumentError, 'queue :priorities (array of floats) is required')

    priorities = [1.0] if priorities.size == 0
    queue_priorities_p = Fiddle::Pointer.malloc(Fiddle::SIZEOF_FLOAT * priorities.size)
    priorities.each_with_index do |priority, i|
      queue_priorities_p[i * Fiddle::SIZEOF_FLOAT, Fiddle::SIZEOF_FLOAT] = [priority].pack(Fiddle::PackInfo::PACK_MAP[Fiddle::TYPE_FLOAT])
    end

    device_queue_info = VkDeviceQueueCreateInfo.malloc
    device_queue_info.sType                   = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO
    device_queue_info.pNext                   = nil
    device_queue_info.flags                   = 0
    device_queue_info.queueFamilyIndex        = family_index
    device_queue_info.queueCount              = priorities.size
    device_queue_info.pQueuePriorities        = queue_priorities_p
    device_queue_info
  end
  queue_infos_p = array_of_structures(queues_p)

  extensions_p = Vulkan.struct("names[#{extensions.size}]" => ['char *name']).malloc
  extensions.each_with_index do |ext, i|
    extname = ext.kind_of?(String) ? ext : ext[:extension_name]
    extensions_p.names[i].name = Fiddle::Pointer[extname.b + "\x00"]
  end

  enabled_features = VkPhysicalDeviceFeatures.malloc
  enabled_features.to_ptr.copy_from physical_device.features.to_ptr
  enabled_features.class.members.each do |member|
    member_name = member.gsub(/[0-9A-Z]+/) { |x| "_#{x.downcase}" }.to_sym
    if features.include?(member_name)
      if enabled_features[member] == VK_FALSE
        raise Error::UnsupportedFeature.new(member_name, member)
      end
    else
      enabled_features[member] = VK_FALSE
    end
  end
  @enabled_features = struct_to_hash(enabled_features).reject! { |name, enabled| enabled != VK_TRUE }.keys

  device_create_info = VkDeviceCreateInfo.malloc
  device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
  device_create_info.pNext = nil
  device_create_info.flags = 0
  device_create_info.queueCreateInfoCount = queues.size
  device_create_info.pQueueCreateInfos = queue_infos_p
  device_create_info.enabledLayerCount = 0 # deprecated
  device_create_info.ppEnabledLayerNames = nil
  device_create_info.enabledExtensionCount = extensions.size
  device_create_info.ppEnabledExtensionNames = extensions_p
  device_create_info.pEnabledFeatures = enabled_features

  device_wrapper = Vulkan.create_value("void *", nil)
  check_result Vulkan[instance, nil].vkCreateDevice(physical_device.to_ptr, device_create_info, nil, device_wrapper)
  @handle = device_wrapper.value
  @vk = Vulkan[instance, self]
  finalize_with Vulkan[instance, nil], :vkDestroyDevice, @handle, nil

  queue_handle_wrapper = Vulkan.create_value("void *", nil)
  @queue_families = queues.map do |queue|
    queues = queue[:priorities].each_with_index.map do |priority, index|
      @vk.vkGetDeviceQueue(@handle, queue_family_to_index(queue[:family]), index, queue_handle_wrapper)
      Vulkan::Queue.new(@vk, queue_handle_wrapper.value, index: index, priority: priority)
    end
    queue[:family].merge queues: queues
  end
end

Instance Attribute Details

#enabled_featuresObject (readonly)

Returns the value of attribute enabled_features.



9
10
11
# File 'lib/vulkan/logical_device.rb', line 9

def enabled_features
  @enabled_features
end

#physical_deviceObject (readonly)

Returns the value of attribute physical_device.



7
8
9
# File 'lib/vulkan/logical_device.rb', line 7

def physical_device
  @physical_device
end

#queue_familiesObject (readonly)

Returns the value of attribute queue_families.



8
9
10
# File 'lib/vulkan/logical_device.rb', line 8

def queue_families
  @queue_families
end

Instance Method Details

#create_buffer(**args) ⇒ Object



110
111
112
# File 'lib/vulkan/logical_device.rb', line 110

def create_buffer(**args)
  Vulkan::Buffer.new(@vk, physical_device, **args)
end

#create_buffer_memory_barrier(**args) ⇒ Object



158
159
160
# File 'lib/vulkan/logical_device.rb', line 158

def create_buffer_memory_barrier(**args)
  Vulkan::BufferMemoryBarrier.new(**args)
end

#create_command_pool(**args) ⇒ Object



138
139
140
# File 'lib/vulkan/logical_device.rb', line 138

def create_command_pool(**args)
  Vulkan::CommandPool.new(@vk, **args)
end

#create_descriptor_set_pool(**args) ⇒ Object



142
143
144
# File 'lib/vulkan/logical_device.rb', line 142

def create_descriptor_set_pool(**args)
  Vulkan::DescriptorPool.new(@vk, **args)
end

#create_fence(**args) ⇒ Object



118
119
120
# File 'lib/vulkan/logical_device.rb', line 118

def create_fence(**args)
  Vulkan::Fence.new(@vk, **args)
end

#create_framebuffer(**args) ⇒ Object



154
155
156
# File 'lib/vulkan/logical_device.rb', line 154

def create_framebuffer(**args)
  Vulkan::Framebuffer.new(@vk, **args)
end

#create_image(**args) ⇒ Object



146
147
148
# File 'lib/vulkan/logical_device.rb', line 146

def create_image(**args)
  Vulkan::Image.new(@vk, physical_device, **args)
end

#create_memory(**args) ⇒ Object



162
163
164
# File 'lib/vulkan/logical_device.rb', line 162

def create_memory(**args)
  Vulkan::Memory.new(@vk, physical_device, **args)
end

#create_memory_barrier(**args) ⇒ Object



166
167
168
# File 'lib/vulkan/logical_device.rb', line 166

def create_memory_barrier(**args)
  Vulkan::MemoryBarrier.new(**args)
end

#create_pipeline(**args) ⇒ Object



126
127
128
# File 'lib/vulkan/logical_device.rb', line 126

def create_pipeline(**args)
  Vulkan::Pipeline.new(@vk, **args)
end

#create_renderpassObject



134
135
136
# File 'lib/vulkan/logical_device.rb', line 134

def create_renderpass
  Vulkan::RenderPass.new(@vk)
end

#create_sampler(**args) ⇒ Object



150
151
152
# File 'lib/vulkan/logical_device.rb', line 150

def create_sampler(**args)
  Vulkan::Sampler.new(@vk, self, **args)
end

#create_semaphoreObject



122
123
124
# File 'lib/vulkan/logical_device.rb', line 122

def create_semaphore
  Vulkan::Semaphore.new(@vk)
end

#create_shader_stage(**args) ⇒ Object



114
115
116
# File 'lib/vulkan/logical_device.rb', line 114

def create_shader_stage(**args)
  Vulkan::ShaderStage.new(@vk, **args)
end

#create_swapchain(**args) ⇒ Object



130
131
132
# File 'lib/vulkan/logical_device.rb', line 130

def create_swapchain(**args)
  Vulkan::Swapchain.new(@instance, self, **args)
end

#feature_enabled?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/vulkan/logical_device.rb', line 106

def feature_enabled?(feature_name)
  @enabled_features.include?(feature_name)
end

#hexaddrObject



170
171
172
# File 'lib/vulkan/logical_device.rb', line 170

def hexaddr
  to_ptr.to_i.to_s(16)
end

#max_color_samplesObject



98
99
100
# File 'lib/vulkan/logical_device.rb', line 98

def max_color_samples
  physical_device.max_color_samples
end

#max_depth_samplesObject



102
103
104
# File 'lib/vulkan/logical_device.rb', line 102

def max_depth_samples
  physical_device.max_depth_samples
end

#max_samplesObject



94
95
96
# File 'lib/vulkan/logical_device.rb', line 94

def max_samples
  physical_device.max_samples
end

#to_ptrObject



174
175
176
# File 'lib/vulkan/logical_device.rb', line 174

def to_ptr
  @handle
end