Module: WGPU::DataTypes
- Defined in:
- lib/wgpu/data_types.rb,
sig/wgpu.rbs
Constant Summary collapse
- FORMATS =
{ f32: ["e*", 4], f64: ["E*", 8], u32: ["L<*", 4], i32: ["l<*", 4], u16: ["S<*", 2], u8: ["C*", 1] }.freeze
Class Method Summary collapse
-
.byte_size(type) ⇒ Integer
Returns the byte width of one typed value.
-
.pack(values, type: :f32) ⇒ String
Encodes Ruby values in little-endian native transfer format.
-
.to_pointer(data, type: :f32) ⇒ Array(FFI::Pointer, Integer)
Converts data to an FFI pointer and reports its byte length.
-
.unpack(bytes, type: :f32) ⇒ Array
Decodes little-endian transfer bytes into Ruby values.
-
.validate_alignment!(value, alignment, name:) ⇒ Integer
Validates a non-negative aligned byte value.
Class Method Details
.byte_size(type) ⇒ Integer
Returns the byte width of one typed value.
43 44 45 |
# File 'lib/wgpu/data_types.rb', line 43 def byte_size(type) format_for(type).last end |
.pack(values, type: :f32) ⇒ String
Encodes Ruby values in little-endian native transfer format.
20 21 22 23 24 25 |
# File 'lib/wgpu/data_types.rb', line 20 def pack(values, type: :f32) format, = format_for(type) Array(values).pack(format) rescue RangeError => e raise ArgumentError, "value is out of range for #{type}: #{e.}" end |
.to_pointer(data, type: :f32) ⇒ Array(FFI::Pointer, Integer)
Converts data to an FFI pointer and reports its byte length.
49 50 51 52 53 54 55 56 |
# File 'lib/wgpu/data_types.rb', line 49 def to_pointer(data, type: :f32) return [data, pointer_size(data)] if data.is_a?(FFI::Pointer) bytes = data.is_a?(String) ? data : pack(data, type:) pointer = FFI::MemoryPointer.new(:char, bytes.bytesize) pointer.put_bytes(0, bytes) [pointer, bytes.bytesize] end |
.unpack(bytes, type: :f32) ⇒ Array
Decodes little-endian transfer bytes into Ruby values.
31 32 33 34 35 36 37 38 |
# File 'lib/wgpu/data_types.rb', line 31 def unpack(bytes, type: :f32) format, byte_size = format_for(type) unless (bytes.bytesize % byte_size).zero? raise ArgumentError, "#{type} data size must be a multiple of #{byte_size} bytes" end bytes.unpack(format) end |
.validate_alignment!(value, alignment, name:) ⇒ Integer
Validates a non-negative aligned byte value.
61 62 63 64 65 66 67 |
# File 'lib/wgpu/data_types.rb', line 61 def validate_alignment!(value, alignment, name:) integer = Integer(value) raise ArgumentError, "#{name} must be non-negative" if integer.negative? return integer if (integer % alignment).zero? raise ArgumentError, "#{name} must be aligned to #{alignment} bytes (got #{integer})" end |