Class: Vulkan::Image

Inherits:
Object
  • Object
show all
Includes:
Checks, Conversions, Finalizer
Defined in:
lib/vulkan/image.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, #hexaddr, included, #to_ptr

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(vk, physical_device, dimensions:, width:, height:, depth:, mip_levels: 1, array_layers: 1, format: :r8g8b8a8_unorm, tiling: :optimal, initial_layout: :undefined, usage: [:transfer_dst, :sampled], sharing: :exclusive, samples: 1, flags: [], properties: :device_local) ⇒ Image

Returns a new instance of Image.



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

def initialize(vk, physical_device,
                   dimensions: ,
                   width: ,
                   height: ,
                   depth: ,
                   mip_levels:     1,
                   array_layers:   1,
                   format:         :r8g8b8a8_unorm,
                   tiling:         :optimal,
                   initial_layout: :undefined,
                   usage:          [:transfer_dst, :sampled],
                   sharing:        :exclusive,
                   samples:        1,
                   flags:          [],
                   properties:     :device_local)
  @vk             = vk
  @dimensions     = dimensions
  @width          = width
  @height         = height
  @depth          = depth
  @mip_levels     = mip_levels
  @array_layers   = array_layers
  @format         = format
  @tiling         = tiling
  @initial_layout = @layout = initial_layout
  @usage          = usage
  @sharing        = sharing
  @samples        = samples
  image_info = VkImageCreateInfo.malloc
  image_info.sType         = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
  image_info.imageType     = sym_to_image_type((1..3).include?(dimensions) ? :"#{dimensions}d" : dimensions)
  image_info.extent.width  = width
  image_info.extent.height = height
  image_info.extent.depth  = depth
  image_info.mipLevels     = mip_levels
  image_info.arrayLayers   = array_layers
  image_info.format        = sym_to_image_format(format)
  image_info.tiling        = sym_to_image_tiling(tiling)
  image_info.initialLayout = sym_to_image_layout(initial_layout)
  image_info.usage         = syms_to_image_usage_flags(usage)
  image_info.sharingMode   = sym_to_sharing_mode(sharing)
  image_info.samples       = sym_to_samples(samples)
  image_info.flags         = syms_to_image_create_flags(flags)
  handle_p = Vulkan.create_value('VkImage')
  check_result @vk.vkCreateImage(@vk.device, image_info, nil, handle_p)
  @handle = handle_p.value
  @memory = Vulkan::ImageMemory.new(@vk, physical_device, owner: self, properties: properties)
  finalize_with @vk, :vkDestroyImage, @vk.device, @handle, nil
end

Instance Attribute Details

#array_layersObject (readonly)

Returns the value of attribute array_layers.



12
13
14
# File 'lib/vulkan/image.rb', line 12

def array_layers
  @array_layers
end

#depthObject (readonly)

Returns the value of attribute depth.



10
11
12
# File 'lib/vulkan/image.rb', line 10

def depth
  @depth
end

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



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

def dimensions
  @dimensions
end

#formatObject (readonly)

Returns the value of attribute format.



13
14
15
# File 'lib/vulkan/image.rb', line 13

def format
  @format
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#initial_layoutObject (readonly)

Returns the value of attribute initial_layout.



15
16
17
# File 'lib/vulkan/image.rb', line 15

def initial_layout
  @initial_layout
end

#layoutObject

Returns the value of attribute layout.



16
17
18
# File 'lib/vulkan/image.rb', line 16

def layout
  @layout
end

#memoryObject (readonly)

Returns the value of attribute memory.



20
21
22
# File 'lib/vulkan/image.rb', line 20

def memory
  @memory
end

#mip_levelsObject (readonly)

Returns the value of attribute mip_levels.



11
12
13
# File 'lib/vulkan/image.rb', line 11

def mip_levels
  @mip_levels
end

#samplesObject (readonly)

Returns the value of attribute samples.



19
20
21
# File 'lib/vulkan/image.rb', line 19

def samples
  @samples
end

#sharingObject (readonly)

Returns the value of attribute sharing.



18
19
20
# File 'lib/vulkan/image.rb', line 18

def sharing
  @sharing
end

#tilingObject (readonly)

Returns the value of attribute tiling.



14
15
16
# File 'lib/vulkan/image.rb', line 14

def tiling
  @tiling
end

#usageObject (readonly)

Returns the value of attribute usage.



17
18
19
# File 'lib/vulkan/image.rb', line 17

def usage
  @usage
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#blit_from(command_pool, queue, src:, src_layout: :transfer_src_optimal, dst_layout: :transfer_dst_optimal, regions:, filter: :nearest, wait_until_idle: false) ⇒ Object

See CommandBuffer#blit_image for details. If ‘:wait_until_idle` is true, this method won’t return until the blit operation has completed and its queue is idle.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/vulkan/image.rb', line 227

def blit_from(command_pool, queue,
              src:,
              src_layout: :transfer_src_optimal,
              dst_layout: :transfer_dst_optimal,
              regions:,
              filter: :nearest,
              wait_until_idle: false)
  command_buffer = command_pool.create_command_buffer(usage: :one_time_submit) do |cmd|
    cmd.blit_image src_image: src,
                   src_image_layout: src_layout,
                   dst_image: self,
                   dst_image_layout: dst_layout,
                   regions: regions,
                   filter: filter
  end

  queue.submit([command_buffer])
  queue.wait_until_idle if wait_until_idle
end

#copy_from_buffer(command_pool, queue, buffer:, buffer_offset: 0, buffer_row_length: 0, buffer_image_height: 0, x: 0, y: 0, z: 0, width: self.width - x, height: self.height - y, depth: self.depth - z, aspects: :color, mip_level: 0, base_array_layer: 0, layer_count: array_layers - base_array_layer, wait_until_idle: true) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/vulkan/image.rb', line 186

def copy_from_buffer(command_pool, queue,
                     buffer:,
                     buffer_offset: 0,
                     buffer_row_length: 0,
                     buffer_image_height: 0,
                     x: 0,
                     y: 0,
                     z: 0,
                     width: self.width - x,
                     height: self.height - y,
                     depth: self.depth - z,
                     aspects: :color,
                     mip_level: 0,
                     base_array_layer: 0,
                     layer_count: array_layers - base_array_layer,
                     wait_until_idle: true)
  command_buffer = command_pool.create_command_buffer(usage: :one_time_submit) do |cmd|
    cmd.copy_buffer_to_image buffer, self, [
                               buffer_offset:       buffer_offset,
                               buffer_row_length:   buffer_row_length,
                               buffer_image_height: buffer_image_height,
                               x:                   x,
                               y:                   y,
                               z:                   z,
                               width:               width,
                               height:              height,
                               depth:               depth,
                               aspects:             aspects,
                               mip_level:           mip_level,
                               base_array_layer:    base_array_layer,
                               layer_count:         layer_count
                             ]
  end

  queue.submit([command_buffer])
  queue.wait_until_idle if wait_until_idle
end

#create_view(format: self.format, base_mip_level: 0, level_count: mip_levels - base_mip_level, base_array_layer: 0, layer_count: array_layers - base_array_layer, aspects: :color, **other_image_view_args) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vulkan/image.rb', line 77

def create_view(format: self.format,
                base_mip_level: 0,
                level_count: mip_levels - base_mip_level,
                base_array_layer: 0,
                layer_count: array_layers - base_array_layer,
                aspects: :color,
                **other_image_view_args)
  ImageView.new(@vk, self, format,
                base_mip_level:   base_mip_level,
                level_count:      level_count,
                base_array_layer: base_array_layer,
                layer_count:      layer_count,
                aspects:          aspects,
                **other_image_view_args)
end

#dataObject



259
260
261
# File 'lib/vulkan/image.rb', line 259

def data
  memory.data
end

#detect_transition_access_and_stage_flags(from, to) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/vulkan/image.rb', line 93

def detect_transition_access_and_stage_flags(from, to)
  from, to = sym_to_image_layout(from), sym_to_image_layout(to)
  if from == VK_IMAGE_LAYOUT_UNDEFINED &&
     to   == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
    return {
      src_access: 0,
      dst_access: :transfer_write,
      src_stages: :top_of_pipe,
      dst_stages: :transfer,
      dependencies: 0
    }
  elsif from == VK_IMAGE_LAYOUT_UNDEFINED &&
        to   == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
    return {
      src_access:   0,
      dst_access:   :shader_read,
      src_stages:   :color_attachment_output,
      dst_stages:   :fragment_shader,
      dependencies: 0
    }
  elsif from == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL &&
        to   == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
    return {
      src_access:   :transfer_write,
      dst_access:   :shader_read,
      src_stages:   :transfer,
      dst_stages:   :fragment_shader,
      dependencies: 0
    }
  elsif from == VK_IMAGE_LAYOUT_UNDEFINED &&
        to   == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
    return {
      src_access: 0,
      dst_access: [ :depth_stencil_attachment_read, :depth_stencil_attachment_write ],
      src_stages: :top_of_pipe,
      dst_stages: :early_fragment_tests,
      dependencies: 0
    }
  elsif from == VK_IMAGE_LAYOUT_UNDEFINED &&
        to   == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
    return {
      src_access: 0,
      dst_access: [ :color_attachment_read, :color_attachment_write ],
      src_stages: :top_of_pipe,
      dst_stages: :color_attachment_output,
      dependencies: 0
    }
  else
    raise ArgumentError, "unsupported layout transition (%s to %s)!" % [from.inspect, to.inspect]
  end
end

#map(*args, &block) ⇒ Object



247
248
249
# File 'lib/vulkan/image.rb', line 247

def map(*args, &block)
  memory.map(*args, &block)
end

#mapped?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/vulkan/image.rb', line 251

def mapped?
  memory.mapped?
end

#transition_layout(command_pool, queue, wait_until_idle: true, **other_args) ⇒ Object

Transitions the layout for this image, using the given command pool to allocate a command buffer and using the given queue to submit it. If ‘wait_until_idle` is true, this method will block until the queue is idle. Otherwise, it will return as soon as the command buffer is submitted. See #transition_layout_with for other arguments.



178
179
180
181
182
183
184
# File 'lib/vulkan/image.rb', line 178

def transition_layout(command_pool, queue, wait_until_idle: true, **other_args)
  command_buffer = command_pool.create_command_buffer(usage: :one_time_submit) do |cmd|
    transition_layout_using(cmd, **other_args)
  end
  queue.submit([command_buffer])
  queue.wait_until_idle if wait_until_idle
end

#transition_layout_using(cmd, from: self.layout, to:, src_queue_family: nil, dst_queue_family: nil, base_mip_level: 0, level_count: mip_levels - base_mip_level, base_array_layer: 0, layer_count: array_layers - base_array_layer, aspects: :color) ⇒ Object

Transitions the layout for this image using the given command buffer. Does not submit the command buffer. Use #transition_layout if you want to be sure the image has fully transitioned before continuing.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/vulkan/image.rb', line 148

def transition_layout_using(cmd,
                            from: self.layout,
                            to:,
                            src_queue_family: nil,
                            dst_queue_family: nil,
                            base_mip_level: 0,
                            level_count: mip_levels - base_mip_level,
                            base_array_layer: 0,
                            layer_count: array_layers - base_array_layer,
                            aspects: :color)
  access_opts = detect_transition_access_and_stage_flags(from, to)
  cmd.pipeline_image_barrier from_layout:      from,
                             to_layout:        to,
                             src_queue_family: src_queue_family,
                             dst_queue_family: dst_queue_family,
                             image:            self,
                             aspects:          aspects,
                             base_mip_level:   base_mip_level,
                             level_count:      level_count,
                             base_array_layer: base_array_layer,
                             layer_count:      layer_count,
                             **access_opts
  @layout = to
end

#unmapObject



255
256
257
# File 'lib/vulkan/image.rb', line 255

def unmap
  memory.unmap
end