aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/texture/block.rs
blob: 7735f5c2958865601af32d0caad16b7cdf2e14fb (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
use super::{loader::BlockRef, repo::BLOCK_SIZE};
use crate::types::*;

use arrayvec::ArrayVec;
use hal::prelude::*;
use rendy_memory::{Allocator, Block};
use std::{iter::once, mem::ManuallyDrop};

pub struct TexturesBlock<B: Block<back::Backend>> {
    pub id: BlockRef,
    pub descriptor_set: ManuallyDrop<RDescriptorSet>,
    pub imgs: ArrayVec<[LoadedImage<B>; BLOCK_SIZE]>,
}

impl<B: Block<back::Backend>> TexturesBlock<B> {
    pub fn deactivate<T: Allocator<back::Backend, Block = B>>(
        mut self,
        device: &mut Device,
        tex_alloc: &mut T,
        desc_alloc: &mut DescriptorAllocator,
    ) {
        unsafe {
            use std::ptr::read;

            // Descriptor set
            desc_alloc.free(once(read(&*self.descriptor_set)));

            // Images
            self.imgs
                .drain(..)
                .map(|x| x.deactivate(device, tex_alloc))
                .for_each(|_| {});
        }
    }
}

pub struct LoadedImage<B: Block<back::Backend>> {
    pub mem: ManuallyDrop<B>,
    pub img: ManuallyDrop<Image>,
    pub img_view: ManuallyDrop<ImageView>,
    pub sampler: ManuallyDrop<Sampler>,
    pub row_size: usize,
    pub height: u32,
    pub width: u32,
}

impl<B: Block<back::Backend>> LoadedImage<B> {
    pub fn deactivate<T: Allocator<back::Backend, Block = B>>(
        self,
        device: &mut Device,
        alloc: &mut T,
    ) {
        unsafe {
            use std::ptr::read;

            device.destroy_image_view(read(&*self.img_view));
            device.destroy_image(read(&*self.img));
            device.destroy_sampler(read(&*self.sampler));

            alloc.free(device, read(&*self.mem));
        }
    }
}