Reputation: 1014
I create a wgpu::TextureView
within a render
method as below:
let mut encoder = self.device.create_command_encoder(...);
let texture_view = self
.surface
.get_current_frame()?
.output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut render_pass = encoder.begin_render_pas(&wgpu::RenderPassDescriptor {
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &texture_view,
...
}],
...
})
render_pass.set_/* pipeline, bind_group, vertex_buffer, index_buffer */(...);
render_pass.draw_indexed(...);
self.queue.submit(std::iter::once(encoder.finish()));
But when I run the program, it panics:
thread 'main' panicked at 'Texture[1] does not exist', /home/doliphin/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-core-0.10.0/src/hub.rs:129:32
Upvotes: 1
Views: 516
Reputation: 1014
This can be solved by forcing the SurfaceTexture
to be dropped after the TextureView
.
let mut encoder = self.device.create_command_encoder(...);
let surface_texture = self.surface.get_current_frame()?.output; // SurfaceTexture
{
let texture_view = surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default()); // TextureView
let mut render_pass = encoder.begin_render_pas(&wgpu::RenderPassDescriptor {
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &texture_view,
...
}],
...
})
render_pass.set_/* pipeline, bind_group, vertex_buffer, index_buffer */(...);
render_pass.draw_indexed(...);
}
// drop(render_pass);
// drop(texture_view);
self.queue.submit(std::iter::once(encoder.finish()));
// drop(surface_texture)
// drop(encoder)
Upvotes: 1