Needing help writing data into a texture array #3879
-
I have a 2d texture array with space for 4 textures that i want to write into. My code looks like the following let texture_descriptor = wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 4,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
label: None,
view_formats: &[],
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
..texture_descriptor
}); For simplicity I only want to write the first 2 textures. If i do this in one operation, this is no problem and the texture array is written correctly: let green_and_red = &[255 as u8, 0, 0, 255, 0, 255, 0, 255];
queue.write_texture(
texture.as_image_copy(),
green_and_red,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(4),
rows_per_image: Some(1),
},
wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 2,
},
); But when trying to write them one by one, I can't write into the second texture slot. Here is what i tried: let red_texture_data = &[255, 0, 0, 255];
let green_texture_data = &[0, 255, 0, 255];
// works
queue.write_texture(
texture.as_image_copy(),
red_texture_data,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(4),
rows_per_image: Some(1),
},
wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
},
);
// crashes
queue.write_texture(
texture.as_image_copy(),
green_texture_data,
wgpu::ImageDataLayout {
offset: 4,
bytes_per_row: Some(4),
rows_per_image: Some(1),
},
wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
},
); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The issue is that your offset is 4. This means that it's trying to read from 4..8 instead of 0..4 on your new buffer. You should also be using ImageCopyTexture directly as your origin is what should be selecting the array layer. |
Beta Was this translation helpful? Give feedback.
The issue is that your offset is 4. This means that it's trying to read from 4..8 instead of 0..4 on your new buffer. You should also be using ImageCopyTexture directly as your origin is what should be selecting the array layer.