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
|
use std::sync::{Arc, RwLock};
use super::{block::TexturesBlock, repo::BLOCK_SIZE, resolver::TextureResolver, LoadableImage};
use crate::{
buffers::{
image::{ImageSpec, SampledImage, COLOR_RESOURCES},
staging::StagingBuffer,
},
error::LockPoisoned,
mem::{Block, MappableBlock, MemoryPool},
types::*,
};
use anyhow::{Context, Result};
use arrayvec::ArrayVec;
use hal::{
format::{Aspects, Format},
image::{
Filter, SamplerDesc, SubresourceLayers, SubresourceRange, Usage as ImgUsage, WrapMode,
},
};
use thiserror::Error;
/// The format used by the texture repo
// TODO: This should be customisable.
pub const FORMAT: Format = Format::Rgba8Srgb;
/// The resources used by each texture. ie one colour aspect
pub const RESOURCES: SubresourceRange = SubresourceRange {
aspects: Aspects::COLOR,
level_start: 0,
level_count: Some(1),
layer_start: 0,
layer_count: Some(1),
};
/// The layers used by each texture. ie one colour layer
pub const LAYERS: SubresourceLayers = SubresourceLayers {
aspects: Aspects::COLOR,
level: 0,
layers: 0..1,
};
/// Configuration required to load a texture
pub struct TextureLoadConfig<R: TextureResolver> {
/// The resolver to use
pub resolver: R,
/// How to sample the image
pub filter: Filter,
/// How to deal with texture coordinates outside the image.
pub wrap_mode: WrapMode,
}
/// A texture load that has been queued, and is finished when the fence triggers.
pub struct QueuedLoad<TP: MemoryPool, SP: MemoryPool> {
pub fence: FenceT,
pub buf: CommandBufferT,
pub block: TexturesBlock<TP>,
pub staging_bufs: ArrayVec<[StagingBuffer<SP>; BLOCK_SIZE]>,
}
impl<TP: MemoryPool, SP: MemoryPool> QueuedLoad<TP, SP> {
/// Break down into a tuple
pub fn dissolve(
self,
) -> (
(FenceT, CommandBufferT),
ArrayVec<[StagingBuffer<SP>; BLOCK_SIZE]>,
TexturesBlock<TP>,
) {
((self.fence, self.buf), self.staging_bufs, self.block)
}
}
/// Create a SampledImage for the given LoadableImage, and load the image data into a StagingBuffer
/// Note that this doesn't queue up transferring from the buffer to the image.
pub unsafe fn load_image<I, R, SP, TP>(
device: &mut DeviceT,
staging_allocator: &Arc<RwLock<SP>>,
tex_allocator: &Arc<RwLock<TP>>,
obcpa: u32,
img_data: I,
config: &TextureLoadConfig<R>,
) -> Result<(StagingBuffer<SP>, SampledImage<TP>)>
where
I: LoadableImage,
R: TextureResolver,
SP: MemoryPool,
TP: MemoryPool,
SP::Block: MappableBlock,
{
// Create sampled image
let sampled_image = {
let mut tex_allocator = tex_allocator
.write()
.map_err(|_| LockPoisoned::MemoryPool)?;
SampledImage::from_device_allocator(
device,
&mut *tex_allocator,
obcpa as u32,
&ImageSpec {
width: img_data.width(),
height: img_data.height(),
format: FORMAT,
usage: ImgUsage::TRANSFER_DST | ImgUsage::SAMPLED,
resources: COLOR_RESOURCES,
},
&SamplerDesc::new(config.filter, config.wrap_mode),
)?
};
// Create staging buffer
let total_size = sampled_image.bound_image().mem().size();
let mut staging_buffer = {
let mut staging_allocator = staging_allocator
.write()
.map_err(|_| LockPoisoned::MemoryPool)?;
StagingBuffer::from_device_pool(device, &mut *staging_allocator, total_size as u64)
.context("Error creating staging buffer")?
};
// Write to staging buffer
let mapped_memory = staging_buffer
.map(device, 0..total_size)
.context("Error mapping staged memory")?;
img_data.copy_into(mapped_memory, sampled_image.row_size() as usize);
staging_buffer.unmap(device)?;
Ok((staging_buffer, sampled_image))
}
/// Errors that can be encountered when loading a texture.
#[derive(Error, Debug)]
pub enum TextureLoadError {
#[error("No available resources")]
NoResources,
}
|