-
I have this function that takes in a list of nalgebra fn render_shape(&mut self, points: &[Point3<f32>]) {
let texture_size = 256u32;
let texture_desc = wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: texture_size,
height: texture_size,
depth_or_array_layers: 1
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::RENDER_ATTACHMENT,
label: Some("Current frame descriptor"),
view_formats: &[wgpu::TextureFormat::Rgba8UnormSrgb]
};
let current_frame = self.device.create_texture(&texture_desc);
let current_frame_view = current_frame.create_view(&Default::default());
let output_buffer_size = (std::mem::size_of::<u32>() as u32 * texture_size * texture_size) as wgpu::BufferAddress;
let output_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Output buffer"),
size: output_buffer_size,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false
});
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Command encoder")
});
let vertex_buffer = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex buffer"),
contents: bytemuck::cast_slice(points),
usage: wgpu::BufferUsages::VERTEX
});
{
let render_pass_desc = wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[
Some(wgpu::RenderPassColorAttachment {
view: ¤t_frame_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0
}),
store: wgpu::StoreOp::Store
}
})
],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None
};
let mut render_pass = encoder.begin_render_pass(&render_pass_desc);
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_vertex_buffer(0, vertex_buffer.slice(..));
render_pass.draw(0..points.len() as u32, 0..1);
}
encoder.copy_texture_to_buffer(wgpu::ImageCopyTexture {
texture: ¤t_frame,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All
}, wgpu::ImageCopyBuffer {
buffer: &output_buffer,
layout: wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(std::mem::size_of::<u32>() as u32 * texture_size),
rows_per_image: Some(texture_size)
}
}, texture_desc.size);
self.queue.submit([encoder.finish()]);
let output_buffer_slice = output_buffer.slice(..);
output_buffer_slice.map_async(wgpu::MapMode::Read, |_| {});
self.device.poll(wgpu::Maintain::Wait);
let data = output_buffer_slice.get_mapped_range();
use image::{ImageBuffer, Rgba};
let buffer =
ImageBuffer::<Rgba<u8>, _>::from_raw(texture_size, texture_size, data).unwrap();
buffer.save("image.png").unwrap();
output_buffer.unmap();
} The function works just fine. I give it any set of points, it draws the shape and saves it to a file. However, the programs exit code is What exactly is going on? It doesn't seem to affect the output in any way but I know this will come back to haunt me later on... |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
It's not readily apparent whether the exit code actually corresponds to an actual stack buffer overrun, unfortunately. It's unlikely, since that exit code is the same code that Rust uses for Panics also abort the current process if your build profile contains |
Beta Was this translation helpful? Give feedback.
-
This issue in |
Beta Was this translation helpful? Give feedback.
-
After a bit of messing around with the code I discovered that the issue comes from the last line, {
let output_buffer_slice = output_buffer.slice(..);
output_buffer_slice.map_async(wgpu::MapMode::Read, |_| {});
self.device.poll(wgpu::Maintain::Wait);
let data = output_buffer_slice.get_mapped_range();
use image::{ImageBuffer, Rgba};
let buffer =
ImageBuffer::<Rgba<u8>, _>::from_raw(texture_size, texture_size, data).unwrap();
buffer.save("image.png").unwrap();output_buffer_slice = output_buffer.slice(..);
output_buffer_slice.map_async(wgpu::MapMode::Read, |_| {});
self.device.poll(wgpu::Maintain::Wait);
let data = output_buffer_slice.get_mapped_range();
use image::{ImageBuffer, Rgba};
let buffer =
ImageBuffer::<Rgba<u8>, _>::from_raw(texture_size, texture_size, data).unwrap();
buffer.save("image.png").unwrap();
}
output_buffer.unmap(); |
Beta Was this translation helpful? Give feedback.
After a bit of messing around with the code I discovered that the issue comes from the last line,
output_buffer.unmap()
. It raised the errorYou cannot unmap a buffer that still has accessible mapped views
but since I was usingcargo test
to execute it it didn't show outputs. As it turns out there can't be anyBufferView
s alive whenunmap()
is called so the solution was to put this code in its own scope.