Class: Vulkan::SwapchainBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vulkan/swapchain_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(swapchain_surface_info, config = {}) ⇒ SwapchainBuilder

Returns a new instance of SwapchainBuilder.



6
7
8
9
10
# File 'lib/vulkan/swapchain_builder.rb', line 6

def initialize(swapchain_surface_info, config = {})
  @surface_info = swapchain_surface_info
  @config = config
  raise 'swapchain not supported' unless usage_supported?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/vulkan/swapchain_builder.rb', line 4

def config
  @config
end

#surface_infoObject (readonly)

Returns the value of attribute surface_info.



3
4
5
# File 'lib/vulkan/swapchain_builder.rb', line 3

def surface_info
  @surface_info
end

Instance Method Details

#dimensions(desired_width = nil, desired_height = nil) ⇒ Object



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

def dimensions(desired_width = nil, desired_height = nil)
  [width(desired_width), height(desired_height)]
end

#formatObject

Chooses and returns a swapchain image format from those supported. An override may be specified in ‘config[:swapchain][:format]` and/or `config[:swapchain][:color_space]`. The defaults are `:r8g8b8a8_unorm` and `:srgb_nonlinear`, respectively.



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
# File 'lib/vulkan/swapchain_builder.rb', line 17

def format
  supported = surface_info.formats

  # By default use the most widely supported R8 G8 B8 A8 format with
  # nonlinear colorspace.
  preferred_format = { format: :r8g8b8a8_unorm, color_space: :srgb_nonlinear }

  # If app config specifies an override, and it is supported, then
  # use it.
  if override = config.dig(:graphics, :swapchain, :image)
    preferred_format[:format] = override[:format] if override.key?(:format)
    preferred_format[:color_space] = override[:color_space] if override.key?(:color_space)
  end

  # If the list contains only one entry with an undefined format then
  # it means that there are no preferred surface formats and any can
  # be chosen.
  return preferred_format if supported.size == 1 && supported[0][:format] == :undefined

  # Check if the preferred format is supported, and use it if possible
  return preferred_format if supported.include?(preferred_format)

  # No match found, try to match the format ignoring colorspace
  supported.each { |sup| return sup if sup[:format] == preferred_format[:format] }

  # No match, return the first format in the list
  return supported.first || raise('no supported swapchain image formats')
end

#height(desired_height) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vulkan/swapchain_builder.rb', line 86

def height(desired_height)
  surface_capabilities = surface_info.capabilities
  desired_height  ||= surface_capabilities[:current_extent][:height]
  if desired_height  == -1
    desired_height  = (config.dig(:graphics, :swapchain, :height))  ||
                     (config.dig(:window, :height)) ||
                     640
  end
  desired_height.clamp(surface_capabilities[:min_image_extent][:height],
                       surface_capabilities[:max_image_extent][:height])
end

#image_countObject



102
103
104
105
106
107
108
109
# File 'lib/vulkan/swapchain_builder.rb', line 102

def image_count
  count = @surface_info.capabilities[:min_image_count] + 1
  if @surface_info.capabilities[:max_image_count] > 0 &&
     count > @surface_info.capabilities[:max_image_count]
    count = @surface_info.capabilities[:max_image_count]
  end
  return count
end

#presentation_modeObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vulkan/swapchain_builder.rb', line 46

def presentation_mode
  # :fifo should always be available;
  # :mailbox is the lowest latency V-Sync enabled mode (something like
  # triple-buffering) so use it if available
  supported = surface_info.presentation_modes
  preferred_mode = config.dig(:graphics, :swapchain, :vsync) == false ? :immediate :
                   config.dig(:graphics, :swapchain, :presentation_mode)&.to_sym
  if preferred_mode && supported.include?(preferred_mode)
    return preferred_mode
  else
    return :mailbox if supported.include?(:mailbox)
    return :fifo if supported.include?(:fifo)
    return :fifo_relaxed if supported.include?(:fifo_relaxed)
    return :immediate if supported.include?(:immediate)
    return raise('no supported presentation modes (available: %s)' % supported.inspect)
  end
end

#transformationObject



64
65
66
67
# File 'lib/vulkan/swapchain_builder.rb', line 64

def transformation
  return :identity if @surface_info.capabilities[:supported_transforms].include?(:identity)
  return @surface_info.capabilities[:current_transform]
end

#usage_supported?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/vulkan/swapchain_builder.rb', line 69

def usage_supported?
  usage = @surface_info.capabilities[:supported_usage]
  usage.include?(:transfer_dst)
end

#width(desired_width) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vulkan/swapchain_builder.rb', line 74

def width(desired_width)
  surface_capabilities = surface_info.capabilities
  desired_width  ||= surface_capabilities[:current_extent][:width]
  if desired_width  == -1
    desired_width  = (config.dig(:graphics, :swapchain, :width))  ||
                     (config.dig(:window, :width)) ||
                     640
  end
  desired_width.clamp(surface_capabilities[:min_image_extent][:width],
                      surface_capabilities[:max_image_extent][:width])
end