aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/context.rs
blob: b569ced90603f55bb015d233a255adb7159b0fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

//! Deals with all the Vulkan/HAL details.
//! In the end, this takes in a depth-sorted list of faces and a map file and renders them.
//! You'll need something else to actually find/sort the faces though.

use std::{
    borrow::Borrow,
    convert::TryInto,
    mem::{size_of, ManuallyDrop},
    ops::Deref,
};

use arrayvec::ArrayVec;
use hal::{pool::CommandPoolCreateFlags, prelude::*};
use log::debug;
use na::Mat4;
use winit::window::Window;

use stockton_levels::prelude::*;
use stockton_levels::traits::faces::FaceType;
use stockton_types::{Vector2, Vector3};

use super::{
    buffer::ModifiableBuffer,
    draw_buffers::{DrawBuffers, INITIAL_INDEX_SIZE, INITIAL_VERT_SIZE},
    target::{SwapchainProperties, TargetChain},
    texture::TextureStore,
};
use crate::{error, types::*};

/// Entry point name for shaders
const ENTRY_NAME: &str = "main";

/// Source for vertex shader. TODO
const VERTEX_SOURCE: &str = include_str!("./data/stockton.vert");

/// Source for fragment shader. TODO
const FRAGMENT_SOURCE: &str = include_str!("./data/stockton.frag");

/// Represents a point of a triangle, including UV and texture information.
#[derive(Debug, Clone, Copy)]
pub struct UVPoint(pub Vector3, pub i32, pub Vector2);

/// Contains all the hal related stuff.
/// In the end, this takes in a depth-sorted list of faces and a map file and renders them.
// TODO: Settings for clear colour, buffer sizes, etc
pub struct RenderingContext<'a> {
    // Parents for most of these things
    /// Vulkan Instance
    instance: ManuallyDrop<back::Instance>,

    /// Device we're using
    device: ManuallyDrop<Device>,

    /// Adapter we're using
    adapter: Adapter,

    // Render destination
    /// Surface to draw to
    surface: ManuallyDrop<Surface>,

    /// Swapchain and stuff
    pub(crate) target_chain: ManuallyDrop<TargetChain>,

    // Pipeline
    /// Our main render pass
    renderpass: ManuallyDrop<RenderPass>,

    /// The layout of our main graphics pipeline
    pipeline_layout: ManuallyDrop<PipelineLayout>,

    /// Our main graphics pipeline
    pipeline: ManuallyDrop<GraphicsPipeline>,

    // Command pool and buffers
    /// The command pool used for our buffers
    cmd_pool: ManuallyDrop<CommandPool>,

    /// The queue group our buffers belong to
    queue_group: QueueGroup,

    /// Texture store
    texture_store: ManuallyDrop<TextureStore>,

    /// Buffers used for drawing
    draw_buffers: ManuallyDrop<DrawBuffers<'a>>,

    /// View projection matrix
    pub(crate) vp_matrix: Mat4,

    /// The vertex shader module
    vs_module: ManuallyDrop<ShaderModule>,

    /// The fragment shader module
    fs_module: ManuallyDrop<ShaderModule>,
}

impl<'a> RenderingContext<'a> {
    /// Create a new RenderingContext for the given window.
    pub fn new<T: HasTextures>(window: &Window, file: &T) -> Result<Self, error::CreationError> {
        // Create surface
        let (instance, mut surface, mut adapters) = unsafe {
            use hal::Instance;

            let instance = back::Instance::create("stockton", 1)
                .map_err(|_| error::CreationError::WindowError)?;
            let surface = instance
                .create_surface(window)
                .map_err(|_| error::CreationError::WindowError)?;
            let adapters = instance.enumerate_adapters();

            (instance, surface, adapters)
        };

        // TODO: Properly figure out which adapter to use
        let mut adapter = adapters.remove(0);

        // Device & Queue group
        let (mut device, mut queue_group) = {
            let family = adapter
                .queue_families
                .iter()
                .find(|family| {
                    surface.supports_queue_family(family) && family.queue_type().supports_graphics()
                })
                .unwrap();

            let mut gpu = unsafe {
                adapter
                    .physical_device
                    .open(&[(family, &[1.0])], hal::Features::empty())
                    .unwrap()
            };

            (gpu.device, gpu.queue_groups.pop().unwrap())
        };

        // Figure out what our swapchain will look like
        let swapchain_properties = SwapchainProperties::find_best(&adapter, &surface)
            .map_err(|_| error::CreationError::BadSurface)?;

        debug!(
            "Detected following swapchain properties: {:?}",
            swapchain_properties
        );

        // Command pool
        let mut cmd_pool = unsafe {
            device.create_command_pool(queue_group.family, CommandPoolCreateFlags::RESET_INDIVIDUAL)
        }
        .map_err(|_| error::CreationError::OutOfMemoryError)?;

        // Renderpass
        let renderpass = {
            use hal::{
                image::{Access, Layout},
                memory::Dependencies,
                pass::*,
                pso::PipelineStage,
            };

            let img_attachment = Attachment {
                format: Some(swapchain_properties.format),
                samples: 1,
                ops: AttachmentOps::new(AttachmentLoadOp::Clear, AttachmentStoreOp::Store),
                stencil_ops: AttachmentOps::new(
                    AttachmentLoadOp::Clear,
                    AttachmentStoreOp::DontCare,
                ),
                layouts: Layout::Undefined..Layout::Present,
            };

            let depth_attachment = Attachment {
                format: Some(swapchain_properties.depth_format),
                samples: 1,
                ops: AttachmentOps::new(AttachmentLoadOp::Clear, AttachmentStoreOp::DontCare),
                stencil_ops: AttachmentOps::new(
                    AttachmentLoadOp::DontCare,
                    AttachmentStoreOp::DontCare,
                ),
                layouts: Layout::Undefined..Layout::DepthStencilAttachmentOptimal,
            };

            let subpass = SubpassDesc {
                colors: &[(0, Layout::ColorAttachmentOptimal)],
                depth_stencil: Some(&(1, Layout::DepthStencilAttachmentOptimal)),
                inputs: &[],
                resolves: &[],
                preserves: &[],
            };

            let in_dependency = SubpassDependency {
                flags: Dependencies::empty(),
                passes: None..Some(0),
                stages: PipelineStage::COLOR_ATTACHMENT_OUTPUT
                    ..(PipelineStage::COLOR_ATTACHMENT_OUTPUT
                        | PipelineStage::EARLY_FRAGMENT_TESTS),
                accesses: Access::empty()
                    ..(Access::COLOR_ATTACHMENT_READ
                        | Access::COLOR_ATTACHMENT_WRITE
                        | Access::DEPTH_STENCIL_ATTACHMENT_READ
                        | Access::DEPTH_STENCIL_ATTACHMENT_WRITE),
            };

            let out_dependency = SubpassDependency {
                flags: Dependencies::empty(),
                passes: Some(0)..None,
                stages: PipelineStage::COLOR_ATTACHMENT_OUTPUT | PipelineStage::EARLY_FRAGMENT_TESTS
                    ..PipelineStage::COLOR_ATTACHMENT_OUTPUT,
                accesses: (Access::COLOR_ATTACHMENT_READ
                    | Access::COLOR_ATTACHMENT_WRITE
                    | Access::DEPTH_STENCIL_ATTACHMENT_READ
                    | Access::DEPTH_STENCIL_ATTACHMENT_WRITE)
                    ..Access::empty(),
            };

            unsafe {
                device.create_render_pass(
                    &[img_attachment, depth_attachment],
                    &[subpass],
                    &[in_dependency, out_dependency],
                )
            }
            .map_err(|_| error::CreationError::OutOfMemoryError)?
        };

        // Subpass
        let subpass = hal::pass::Subpass {
            index: 0,
            main_pass: &renderpass,
        };

        // Vertex and index buffers
        let draw_buffers = DrawBuffers::new(&mut device, &adapter)?;

        // Texture store
        let texture_store = TextureStore::new(
            &mut device,
            &mut adapter,
            &mut queue_group.queues[0],
            &mut cmd_pool,
            file,
        )?;

        let mut descriptor_set_layouts: ArrayVec<[_; 2]> = ArrayVec::new();
        descriptor_set_layouts.push(texture_store.descriptor_set_layout.deref());

        // Graphics pipeline
        let (pipeline_layout, pipeline, vs_module, fs_module) = Self::create_pipeline(
            &mut device,
            swapchain_properties.extent,
            &subpass,
            descriptor_set_layouts,
        )?;

        // Swapchain and associated resources
        let target_chain = TargetChain::new(
            &mut device,
            &adapter,
            &mut surface,
            &renderpass,
            &mut cmd_pool,
            swapchain_properties,
            None,
        )
        .map_err(error::CreationError::TargetChainCreationError)?;

        Ok(RenderingContext {
            instance: ManuallyDrop::new(instance),
            surface: ManuallyDrop::new(surface),

            device: ManuallyDrop::new(device),
            adapter,
            queue_group,

            renderpass: ManuallyDrop::new(renderpass),
            target_chain: ManuallyDrop::new(target_chain),
            cmd_pool: ManuallyDrop::new(cmd_pool),

            pipeline_layout: ManuallyDrop::new(pipeline_layout),
            pipeline: ManuallyDrop::new(pipeline),

            texture_store: ManuallyDrop::new(texture_store),

            draw_buffers: ManuallyDrop::new(draw_buffers),

            vs_module: ManuallyDrop::new(vs_module),
            fs_module: ManuallyDrop::new(fs_module),

            vp_matrix: Mat4::identity(),
        })
    }

    /// If this function fails the whole context is probably dead
    /// # Safety
    /// The context must not be used while this is being called
    pub unsafe fn handle_surface_change(&mut self) -> Result<(), error::CreationError> {
        self.device.wait_idle().unwrap();

        let properties = SwapchainProperties::find_best(&self.adapter, &self.surface)
            .map_err(|_| error::CreationError::BadSurface)?;

        use core::ptr::read;

        // Graphics pipeline
        self.device
            .destroy_graphics_pipeline(ManuallyDrop::into_inner(read(&self.pipeline)));

        self.device
            .destroy_pipeline_layout(ManuallyDrop::into_inner(read(&self.pipeline_layout)));

        self.device
            .destroy_shader_module(ManuallyDrop::into_inner(read(&self.vs_module)));
        self.device
            .destroy_shader_module(ManuallyDrop::into_inner(read(&self.fs_module)));

        let (pipeline_layout, pipeline, vs_module, fs_module) = {
            let mut descriptor_set_layouts: ArrayVec<[_; 2]> = ArrayVec::new();
            descriptor_set_layouts.push(self.texture_store.descriptor_set_layout.deref());

            let subpass = hal::pass::Subpass {
                index: 0,
                main_pass: &(*self.renderpass),
            };

            Self::create_pipeline(
                &mut self.device,
                properties.extent,
                &subpass,
                descriptor_set_layouts,
            )?
        };

        self.pipeline_layout = ManuallyDrop::new(pipeline_layout);
        self.pipeline = ManuallyDrop::new(pipeline);

        self.vs_module = ManuallyDrop::new(vs_module);
        self.fs_module = ManuallyDrop::new(fs_module);

        let old_swapchain = ManuallyDrop::into_inner(read(&self.target_chain))
            .deactivate_with_recyling(&mut self.device, &mut self.cmd_pool);
        self.target_chain = ManuallyDrop::new(
            TargetChain::new(
                &mut self.device,
                &self.adapter,
                &mut self.surface,
                &self.renderpass,
                &mut self.cmd_pool,
                properties,
                Some(old_swapchain),
            )
            .map_err(error::CreationError::TargetChainCreationError)?,
        );

        Ok(())
    }

    #[allow(clippy::type_complexity)]
    fn create_pipeline<T>(
        device: &mut Device,
        extent: hal::image::Extent,
        subpass: &hal::pass::Subpass<back::Backend>,
        set_layouts: T,
    ) -> Result<(PipelineLayout, GraphicsPipeline, ShaderModule, ShaderModule), error::CreationError>
    where
        T: IntoIterator,
        T::Item: Borrow<DescriptorSetLayout>,
    {
        use hal::format::Format;
        use hal::pso::*;

        // Shader modules
        let (vs_module, fs_module) = {
            let mut compiler = shaderc::Compiler::new().ok_or(error::CreationError::NoShaderC)?;

            let vertex_compile_artifact = compiler
                .compile_into_spirv(
                    VERTEX_SOURCE,
                    shaderc::ShaderKind::Vertex,
                    "vertex.vert",
                    ENTRY_NAME,
                    None,
                )
                .map_err(error::CreationError::ShaderCError)?;

            let fragment_compile_artifact = compiler
                .compile_into_spirv(
                    FRAGMENT_SOURCE,
                    shaderc::ShaderKind::Fragment,
                    "fragment.frag",
                    ENTRY_NAME,
                    None,
                )
                .map_err(error::CreationError::ShaderCError)?;

            // Make into shader module
            unsafe {
                (
                    device
                        .create_shader_module(vertex_compile_artifact.as_binary())
                        .map_err(error::CreationError::ShaderModuleFailed)?,
                    device
                        .create_shader_module(fragment_compile_artifact.as_binary())
                        .map_err(error::CreationError::ShaderModuleFailed)?,
                )
            }
        };

        // Shader entry points (ShaderStage)
        let (vs_entry, fs_entry) = (
            EntryPoint::<back::Backend> {
                entry: ENTRY_NAME,
                module: &vs_module,
                specialization: Specialization::default(),
            },
            EntryPoint::<back::Backend> {
                entry: ENTRY_NAME,
                module: &fs_module,
                specialization: Specialization::default(),
            },
        );

        // Shader set
        let shaders = GraphicsShaderSet {
            vertex: vs_entry,
            fragment: Some(fs_entry),
            hull: None,
            domain: None,
            geometry: None,
        };

        // Vertex buffers
        let vertex_buffers: Vec<VertexBufferDesc> = vec![VertexBufferDesc {
            binding: 0,
            stride: (size_of::<f32>() * 6) as u32,
            rate: VertexInputRate::Vertex,
        }];

        let attributes: Vec<AttributeDesc> = pipeline_vb_attributes!(0,
            size_of::<f32>() * 3; Rgb32Sfloat,
            size_of::<u32>(); R32Sint,
            size_of::<f32>() * 2; Rg32Sfloat
        );

        // Rasterizer
        let rasterizer = Rasterizer {
            polygon_mode: PolygonMode::Fill,
            cull_face: Face::BACK,
            front_face: FrontFace::CounterClockwise,
            depth_clamping: false,
            depth_bias: None,
            conservative: true,
            line_width: hal::pso::State::Static(1.0),
        };

        // Depth stencil
        let depth_stencil = DepthStencilDesc {
            depth: Some(DepthTest {
                fun: Comparison::Less,
                write: true,
            }),
            depth_bounds: false,
            stencil: None,
        };

        // Pipeline layout
        let layout = unsafe {
            device.create_pipeline_layout(
                set_layouts,
                // vp matrix, 4x4 f32
                &[(ShaderStageFlags::VERTEX, 0..64)],
            )
        }
        .map_err(|_| error::CreationError::OutOfMemoryError)?;

        // Colour blending
        let blender = {
            let blend_state = BlendState {
                color: BlendOp::Add {
                    src: Factor::One,
                    dst: Factor::Zero,
                },
                alpha: BlendOp::Add {
                    src: Factor::One,
                    dst: Factor::Zero,
                },
            };

            BlendDesc {
                logic_op: Some(LogicOp::Copy),
                targets: vec![ColorBlendDesc {
                    mask: ColorMask::ALL,
                    blend: Some(blend_state),
                }],
            }
        };

        // Baked states
        let baked_states = BakedStates {
            viewport: Some(Viewport {
                rect: extent.rect(),
                depth: (0.0..1.0),
            }),
            scissor: Some(extent.rect()),
            blend_color: None,
            depth_bounds: None,
        };

        // Input assembler
        let input_assembler = InputAssemblerDesc::new(Primitive::TriangleList);

        // Pipeline description
        let pipeline_desc = GraphicsPipelineDesc {
            shaders,
            rasterizer,
            vertex_buffers,
            blender,
            depth_stencil,
            multisampling: None,
            baked_states,
            layout: &layout,
            subpass: *subpass,
            flags: PipelineCreationFlags::empty(),
            parent: BasePipeline::None,
            input_assembler,
            attributes,
        };

        // Pipeline
        let pipeline = unsafe { device.create_graphics_pipeline(&pipeline_desc, None) }
            .map_err(error::CreationError::PipelineError)?;

        Ok((layout, pipeline, vs_module, fs_module))
    }

    /// Draw all vertices in the buffer
    pub fn draw_vertices<M: MinBSPFeatures<VulkanSystem>>(
        &mut self,
        file: &M,
        faces: &[u32],
    ) -> Result<(), &'static str> {
        // Prepare command buffer
        let cmd_buffer = self.target_chain.prep_next_target(
            &mut self.device,
            &mut self.draw_buffers,
            &self.renderpass,
            &self.pipeline,
            &self.pipeline_layout,
            &self.vp_matrix,
        )?;

        // Iterate over faces, copying them in and drawing groups that use the same texture chunk all at once.
        let mut current_chunk = file.get_face(0).texture_idx as usize / 8;
        let mut chunk_start = 0;

        let mut curr_vert_idx: usize = 0;
        let mut curr_idx_idx: usize = 0;

        for face in faces.iter().map(|idx| file.get_face(*idx)) {
            if current_chunk != face.texture_idx as usize / 8 {
                // Last index was last of group, so draw it all.
                let mut descriptor_sets: ArrayVec<[_; 1]> = ArrayVec::new();
                descriptor_sets.push(self.texture_store.get_chunk_descriptor_set(current_chunk));
                unsafe {
                    cmd_buffer.bind_graphics_descriptor_sets(
                        &self.pipeline_layout,
                        0,
                        descriptor_sets,
                        &[],
                    );
                    cmd_buffer.draw_indexed(
                        chunk_start as u32 * 3..(curr_idx_idx as u32 * 3) + 1,
                        0,
                        0..1,
                    );
                }

                // Next group of same-chunked faces starts here.
                chunk_start = curr_idx_idx;
                current_chunk = face.texture_idx as usize / 8;
            }

            if face.face_type == FaceType::Polygon || face.face_type == FaceType::Mesh {
                // 2 layers of indirection
                let base = face.vertices_idx.start;

                for idx in face.meshverts_idx.clone().step_by(3) {
                    let start_idx: u16 = curr_vert_idx.try_into().unwrap();

                    for idx2 in idx..idx + 3 {
                        let vert = &file.resolve_meshvert(idx2 as u32, base);
                        let uv = Vector2::new(vert.tex.u[0], vert.tex.v[0]);

                        let uvp = UVPoint(vert.position, face.texture_idx.try_into().unwrap(), uv);
                        self.draw_buffers.vertex_buffer[curr_vert_idx] = uvp;

                        curr_vert_idx += 1;
                    }

                    self.draw_buffers.index_buffer[curr_idx_idx] =
                        (start_idx, start_idx + 1, start_idx + 2);

                    curr_idx_idx += 1;

                    if curr_vert_idx >= INITIAL_VERT_SIZE.try_into().unwrap()
                        || curr_idx_idx >= INITIAL_INDEX_SIZE.try_into().unwrap()
                    {
                        println!("out of vertex buffer space!");
                        break;
                    }
                }
            } else {
                // TODO: Other types of faces
            }

            if curr_vert_idx >= INITIAL_VERT_SIZE.try_into().unwrap()
                || curr_idx_idx >= INITIAL_INDEX_SIZE.try_into().unwrap()
            {
                println!("out of vertex buffer space!");
                break;
            }
        }

        // Draw the final group of chunks
        let mut descriptor_sets: ArrayVec<[_; 1]> = ArrayVec::new();
        descriptor_sets.push(self.texture_store.get_chunk_descriptor_set(current_chunk));
        unsafe {
            cmd_buffer.bind_graphics_descriptor_sets(
                &self.pipeline_layout,
                0,
                descriptor_sets,
                &[],
            );
            cmd_buffer.draw_indexed(
                chunk_start as u32 * 3..(curr_idx_idx as u32 * 3) + 1,
                0,
                0..1,
            );
        }

        // Update our buffers before we actually start drawing
        self.draw_buffers.vertex_buffer.commit(
            &self.device,
            &mut self.queue_group.queues[0],
            &mut self.cmd_pool,
        );

        self.draw_buffers.index_buffer.commit(
            &self.device,
            &mut self.queue_group.queues[0],
            &mut self.cmd_pool,
        );

        // Send commands off to GPU
        self.target_chain
            .finish_and_submit_target(&mut self.queue_group.queues[0])?;

        Ok(())
    }
}

impl<'a> core::ops::Drop for RenderingContext<'a> {
    fn drop(&mut self) {
        self.device.wait_idle().unwrap();

        unsafe {
            use core::ptr::read;

            ManuallyDrop::into_inner(read(&self.draw_buffers)).deactivate(&mut self.device);
            ManuallyDrop::into_inner(read(&self.texture_store)).deactivate(&mut self.device);

            ManuallyDrop::into_inner(read(&self.target_chain))
                .deactivate(&mut self.device, &mut self.cmd_pool);

            self.device
                .destroy_command_pool(ManuallyDrop::into_inner(read(&self.cmd_pool)));
            self.device
                .destroy_render_pass(ManuallyDrop::into_inner(read(&self.renderpass)));

            self.device
                .destroy_shader_module(ManuallyDrop::into_inner(read(&self.vs_module)));
            self.device
                .destroy_shader_module(ManuallyDrop::into_inner(read(&self.fs_module)));

            self.device
                .destroy_graphics_pipeline(ManuallyDrop::into_inner(read(&self.pipeline)));

            self.device
                .destroy_pipeline_layout(ManuallyDrop::into_inner(read(&self.pipeline_layout)));

            self.instance
                .destroy_surface(ManuallyDrop::into_inner(read(&self.surface)));

            ManuallyDrop::drop(&mut self.device);
        }
    }
}