aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/texture/chunk.rs
blob: 0cb6737e4b81acd5536f2b3feec40b80623e7df6 (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
/*
 * 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/>.
 */

//! A chunk of textures is an array of textures, the size of which is known at compile time.
//! This reduces the number of times we need to re-bind our descriptor sets

use crate::draw::texture::image::LoadableImage;
use hal::prelude::*;
use image::{Rgba, RgbaImage};

use core::mem::replace;
use std::ops::Deref;

use crate::{error, types::*};

use super::image::SampledImage;
use super::resolver::TextureResolver;
use log::debug;
use std::iter::Iterator;
use stockton_levels::traits::textures::Texture;

/// The size of a chunk. Needs to match up with the fragment shader
pub const CHUNK_SIZE: usize = 8;

/// An array of textures
pub struct TextureChunk {
    pub(crate) descriptor_set: DescriptorSet,
    sampled_images: Vec<SampledImage>,
}

impl TextureChunk {
    /// Create a new empty texture chunk
    pub fn new_empty(
        device: &mut Device,
        adapter: &mut Adapter,
        allocator: &mut DynamicAllocator,
        command_queue: &mut CommandQueue,
        command_pool: &mut CommandPool,
        descriptor_set: DescriptorSet,
    ) -> Result<TextureChunk, error::CreationError> {
        let mut store = TextureChunk {
            descriptor_set,
            sampled_images: Vec::with_capacity(CHUNK_SIZE),
        };

        for i in 0..CHUNK_SIZE {
            debug!("Putting a placeholder in slot {}", i);
            store
                .put_texture(
                    RgbaImage::from_pixel(1, 1, Rgba([0, 0, 0, 1])),
                    i,
                    device,
                    adapter,
                    allocator,
                    command_queue,
                    command_pool,
                )
                .unwrap();
        }

        Ok(store)
    }

    /// Create a new texture chunk and load in the textures specified by `range` from `file` using `resolver`
    /// Can error if the descriptor pool is too small or if a texture isn't found
    pub fn new<'a, I, R: TextureResolver<T>, T: LoadableImage>(
        device: &mut Device,
        adapter: &mut Adapter,
        allocator: &mut DynamicAllocator,
        command_queue: &mut CommandQueue,
        command_pool: &mut CommandPool,
        descriptor_set: DescriptorSet,
        textures: I,
        resolver: &mut R,
    ) -> Result<TextureChunk, error::CreationError>
    where
        I: 'a + Iterator<Item = &'a Texture>,
    {
        let mut store = TextureChunk {
            descriptor_set,
            sampled_images: Vec::with_capacity(CHUNK_SIZE),
        };

        let mut local_idx = 0;

        debug!("Created descriptor set");
        for tex in textures {
            if let Some(img) = resolver.resolve(tex) {
                store
                    .put_texture(
                        img,
                        local_idx,
                        device,
                        adapter,
                        allocator,
                        command_queue,
                        command_pool,
                    )
                    .unwrap();
            } else {
                // Texture not found. For now, tear everything down.
                store.deactivate(device, allocator);

                return Err(error::CreationError::BadDataError);
            }

            local_idx += 1;
        }

        // Pad out the end if needed
        while local_idx < CHUNK_SIZE {
            debug!("Putting a placeholder in slot {}", local_idx);
            store
                .put_texture(
                    RgbaImage::from_pixel(1, 1, Rgba([0, 0, 0, 1])),
                    local_idx,
                    device,
                    adapter,
                    allocator,
                    command_queue,
                    command_pool,
                )
                .unwrap();

            local_idx += 1;
        }

        Ok(store)
    }

    pub fn put_texture<T: LoadableImage>(
        &mut self,
        image: T,
        idx: usize,
        device: &mut Device,
        adapter: &mut Adapter,
        allocator: &mut DynamicAllocator,
        command_queue: &mut CommandQueue,
        command_pool: &mut CommandPool,
    ) -> Result<(), &'static str> {
        // Load the image
        let texture = SampledImage::load_into_new(
            image,
            device,
            adapter,
            allocator,
            command_queue,
            command_pool,
            hal::format::Format::Rgba8Srgb, // TODO
            hal::image::Usage::empty(),
        )?;

        // Write it to the descriptor set
        unsafe {
            use hal::image::Layout;
            use hal::pso::{Descriptor, DescriptorSetWrite};

            device.write_descriptor_sets(vec![
                DescriptorSetWrite {
                    set: &self.descriptor_set,
                    binding: 0,
                    array_offset: idx,
                    descriptors: Some(Descriptor::Image(
                        texture.image.image_view.deref(),
                        Layout::ShaderReadOnlyOptimal,
                    )),
                },
                DescriptorSetWrite {
                    set: &self.descriptor_set,
                    binding: 1,
                    array_offset: idx,
                    descriptors: Some(Descriptor::Sampler(texture.sampler.deref())),
                },
            ]);
        };

        // Store it so we can safely deactivate it when we need to
        // Deactivate the old image if we need to
        if idx < self.sampled_images.len() {
            replace(&mut self.sampled_images[idx], texture).deactivate(device, allocator);
        } else {
            self.sampled_images.push(texture);
        }

        Ok(())
    }

    pub fn deactivate(mut self, device: &mut Device, allocator: &mut DynamicAllocator) {
        for img in self.sampled_images.drain(..) {
            img.deactivate(device, allocator);
        }
    }
}