use std::error::Error;
use sdl3::gpu::{Device, TextureCreateInfo, TextureFormat, TextureType, TextureUsage, *};
pub fn create_buffer_with_data<T: Copy>(
device: &Device,
transfer_buffer: &TransferBuffer,
copy_pass: &CopyPass,
usage: BufferUsageFlags,
data: &[T],
) -> Result<Buffer, sdl3::Error> {
let len_bytes = std::mem::size_of_val(data);
let buffer = device
.create_buffer()
.with_size(len_bytes as u32)
.with_usage(usage)
.build()?;
let mut map = transfer_buffer.map::<T>(device, true);
let mem = map.mem_mut();
for (index, &value) in data.iter().enumerate() {
mem[index] = value;
}
map.unmap();
copy_pass.upload_to_gpu_buffer(
TransferBufferLocation::new()
.with_offset(0)
.with_transfer_buffer(transfer_buffer),
BufferRegion::new()
.with_offset(0)
.with_size(len_bytes as u32)
.with_buffer(&buffer),
true,
);
Ok(buffer)
}
pub fn create_texture(
device: &Device,
copy_pass: &CopyPass,
image_data: &[u8],
width: u32,
height: u32,
) -> Result<Texture<'static>, Box<dyn Error>> {
let size_bytes = width * height * 4;
let texture = device.create_texture(
TextureCreateInfo::new()
.with_format(TextureFormat::R8g8b8a8Unorm)
.with_type(TextureType::_2D)
.with_width(width)
.with_height(height)
.with_layer_count_or_depth(1)
.with_num_levels(1)
.with_usage(TextureUsage::SAMPLER),
)?;
let transfer_buffer = device
.create_transfer_buffer()
.with_size(size_bytes)
.with_usage(TransferBufferUsage::UPLOAD)
.build()?;
let mut buffer_mem = transfer_buffer.map::<u8>(device, false);
buffer_mem.mem_mut().copy_from_slice(image_data);
buffer_mem.unmap();
copy_pass.upload_to_gpu_texture(
TextureTransferInfo::new()
.with_transfer_buffer(&transfer_buffer)
.with_offset(0),
TextureRegion::new()
.with_texture(&texture)
.with_layer(0)
.with_width(width)
.with_height(height)
.with_depth(1),
false,
);
Ok(texture)
}