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
use crate::renderer::framework::error::FrameworkError;
use crate::{
core::scope_profile,
engine::resource_manager::DEFAULT_RESOURCE_LIFETIME,
renderer::{
cache::CacheEntry,
framework::{
gpu_texture::{Coordinate, GpuTexture, PixelKind},
state::PipelineState,
},
},
resource::texture::{Texture, TextureState},
utils::log::{Log, MessageKind},
};
use fxhash::FxHashMap;
use std::{cell::RefCell, collections::hash_map::Entry, ops::Deref, rc::Rc};
#[derive(Default)]
pub struct TextureCache {
pub(in crate) map: FxHashMap<usize, CacheEntry<Rc<RefCell<GpuTexture>>>>,
}
impl TextureCache {
/// Unconditionally uploads requested texture into GPU memory, previous GPU texture will be automatically
/// destroyed.
pub fn upload(
&mut self,
state: &mut PipelineState,
texture: &Texture,
) -> Result<(), FrameworkError> {
let key = texture.key();
let texture = texture.state();
if let TextureState::Ok(texture) = texture.deref() {
let gpu_texture = GpuTexture::new(
state,
texture.kind().into(),
PixelKind::from(texture.pixel_kind()),
texture.minification_filter().into(),
texture.magnification_filter().into(),
texture.mip_count() as usize,
Some(texture.data()),
)?;
match self.map.entry(key) {
Entry::Occupied(mut e) => {
*e.get_mut().value.borrow_mut() = gpu_texture;
}
Entry::Vacant(e) => {
e.insert(CacheEntry {
value: Rc::new(RefCell::new(gpu_texture)),
time_to_live: DEFAULT_RESOURCE_LIFETIME,
value_hash: texture.data_hash(),
});
}
}
Ok(())
} else {
Err(FrameworkError::Custom(
"Texture is not loaded yet!".to_string(),
))
}
}
pub fn get(
&mut self,
state: &mut PipelineState,
texture: &Texture,
) -> Option<Rc<RefCell<GpuTexture>>> {
scope_profile!();
let key = texture.key();
let texture = texture.state();
if let TextureState::Ok(texture) = texture.deref() {
let entry = match self.map.entry(key) {
Entry::Occupied(e) => {
let entry = e.into_mut();
// Texture won't be destroyed while it used.
entry.time_to_live = DEFAULT_RESOURCE_LIFETIME;
// Check if some value has changed in resource.
// Data might change from last frame, so we have to check it and upload new if so.
let data_hash = texture.data_hash();
if entry.value_hash != data_hash {
let mut tex = entry.borrow_mut();
if let Err(e) = tex.bind_mut(state, 0).set_data(
texture.kind().into(),
texture.pixel_kind().into(),
texture.mip_count() as usize,
Some(texture.data()),
) {
Log::writeln(
MessageKind::Error,
format!(
"Unable to upload new texture data to GPU. Reason: {:?}",
e
),
)
} else {
drop(tex);
// TODO: Is this correct to overwrite hash only if we've succeeded?
entry.value_hash = data_hash;
}
}
let mut tex = entry.borrow_mut();
let new_mag_filter = texture.magnification_filter().into();
if tex.magnification_filter() != new_mag_filter {
tex.bind_mut(state, 0)
.set_magnification_filter(new_mag_filter);
}
let new_min_filter = texture.minification_filter().into();
if tex.minification_filter() != new_min_filter {
tex.bind_mut(state, 0)
.set_minification_filter(new_min_filter);
}
if tex.anisotropy().ne(&texture.anisotropy_level()) {
tex.bind_mut(state, 0)
.set_anisotropy(texture.anisotropy_level());
}
let new_s_wrap_mode = texture.s_wrap_mode().into();
if tex.s_wrap_mode() != new_s_wrap_mode {
tex.bind_mut(state, 0)
.set_wrap(Coordinate::S, new_s_wrap_mode);
}
let new_t_wrap_mode = texture.t_wrap_mode().into();
if tex.t_wrap_mode() != new_t_wrap_mode {
tex.bind_mut(state, 0)
.set_wrap(Coordinate::T, new_t_wrap_mode);
}
std::mem::drop(tex);
entry
}
Entry::Vacant(e) => {
let gpu_texture = match GpuTexture::new(
state,
texture.kind().into(),
PixelKind::from(texture.pixel_kind()),
texture.minification_filter().into(),
texture.magnification_filter().into(),
texture.mip_count() as usize,
Some(texture.data()),
) {
Ok(texture) => texture,
Err(e) => {
Log::writeln(
MessageKind::Error,
format!("Failed to create GPU texture. Reason: {:?}", e),
);
return None;
}
};
e.insert(CacheEntry {
value: Rc::new(RefCell::new(gpu_texture)),
time_to_live: DEFAULT_RESOURCE_LIFETIME,
value_hash: texture.data_hash(),
})
}
};
Some(entry.value.clone())
} else {
None
}
}
pub fn update(&mut self, dt: f32) {
scope_profile!();
for entry in self.map.values_mut() {
entry.time_to_live -= dt;
}
self.map.retain(|_, v| v.time_to_live > 0.0);
}
pub fn clear(&mut self) {
self.map.clear();
}
pub fn unload(&mut self, texture: Texture) {
self.map.remove(&texture.key());
}
}