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
use crate::graphics::{
ext::Texture2dFactory,
texture_atlas::{
AtlasVersion, SamplerHandle, TextureAtlas, TextureHandle,
MAX_SUPPORTED_TEXTURES,
},
vulkan::{buffer::CpuBuffer, texture::TextureImage, Device},
};
use anyhow::Result;
use ash::{version::DeviceV1_0, vk};
use std::sync::Arc;
struct Binding {
texture: TextureImage,
sampler_handle: SamplerHandle,
}
pub struct GpuAtlas {
textures: Vec<Option<Binding>>,
samplers: Vec<vk::Sampler>,
version: AtlasVersion,
device: Arc<Device>,
}
impl GpuAtlas {
pub fn new(device: Arc<Device>) -> Result<Self> {
let sampler = unsafe {
use crate::graphics::ext::SamplerFactory;
device.create_sampler(
"default sampler",
vk::SamplerCreateInfo {
mag_filter: vk::Filter::LINEAR,
min_filter: vk::Filter::LINEAR,
address_mode_u: vk::SamplerAddressMode::REPEAT,
address_mode_v: vk::SamplerAddressMode::REPEAT,
address_mode_w: vk::SamplerAddressMode::REPEAT,
anisotropy_enable: 0,
border_color: vk::BorderColor::INT_OPAQUE_BLACK,
unnormalized_coordinates: 0,
compare_enable: 0,
compare_op: vk::CompareOp::ALWAYS,
mipmap_mode: vk::SamplerMipmapMode::LINEAR,
mip_lod_bias: 0.0,
min_lod: 0.0,
max_lod: vk::LOD_CLAMP_NONE,
..Default::default()
},
)?
};
let default_texture = unsafe {
let mut transfer_buffer = CpuBuffer::new(
device.clone(),
vk::BufferUsageFlags::TRANSFER_SRC,
)?;
let white_pixel: [u8; 4] = [255, 255, 255, 255];
transfer_buffer.write_data(&white_pixel)?;
let mut tex = device.create_empty_2d_texture("default", 1, 1, 1)?;
tex.upload_from_buffer(&transfer_buffer)?;
tex
};
let mut bindings = vec![];
bindings.reserve(MAX_SUPPORTED_TEXTURES);
bindings.push(Some(Binding {
texture: default_texture,
sampler_handle: SamplerHandle::default(),
}));
for _ in 1..MAX_SUPPORTED_TEXTURES {
bindings.push(None);
}
Ok(Self {
textures: bindings,
version: AtlasVersion::new_out_of_date().increment(),
samplers: vec![sampler],
device,
})
}
}
impl TextureAtlas for GpuAtlas {
fn version(&self) -> AtlasVersion {
self.version
}
fn add_sampler(&mut self, sampler: vk::Sampler) -> Result<SamplerHandle> {
self.samplers.push(sampler);
let index = self.samplers.len() - 1;
Ok(SamplerHandle::new(index as u32))
}
fn bind_sampler_to_texture(
&mut self,
sampler_handle: SamplerHandle,
texture_handle: TextureHandle,
) -> Result<()> {
if let Some(binding) =
&mut self.textures[texture_handle.texture_index() as usize]
{
binding.sampler_handle = sampler_handle;
Ok(())
} else {
anyhow::bail!("the provide texture handle does not match an existing texture!");
}
}
fn add_texture(&mut self, texture: TextureImage) -> Result<TextureHandle> {
use anyhow::Context;
let free_slot_index = self
.textures
.iter()
.enumerate()
.find(|(_i, entry)| entry.is_none())
.with_context(|| "unable to find a free texture slot!")?
.0;
self.textures[free_slot_index] = Some(Binding {
texture,
sampler_handle: SamplerHandle::default(),
});
self.version = self.version.increment();
Ok(TextureHandle::new(free_slot_index as u32))
}
unsafe fn take_texture(
&mut self,
texture_handle: TextureHandle,
) -> Result<TextureImage> {
use anyhow::Context;
let texture = self.textures[texture_handle.texture_index() as usize]
.take()
.context("no texture bound with that texture handle!")?
.texture;
self.version = self.version.increment();
Ok(texture)
}
fn build_descriptor_image_info(&self) -> Vec<vk::DescriptorImageInfo> {
let default_view =
unsafe { self.textures[0].as_ref().unwrap().texture.raw_view() };
self.textures
.iter()
.map(|binding_option| match binding_option {
Some(binding) => vk::DescriptorImageInfo {
image_layout: vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
image_view: unsafe { binding.texture.raw_view() },
sampler: self.samplers
[binding.sampler_handle.index() as usize],
},
None => vk::DescriptorImageInfo {
image_layout: vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
image_view: default_view,
sampler: self.samplers[0],
},
})
.collect()
}
}
impl Drop for GpuAtlas {
fn drop(&mut self) {
unsafe {
for sampler in self.samplers.drain(0..) {
self.device.logical_device.destroy_sampler(sampler, None);
}
}
}
}