forked from gfx-rs/gfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative.rs
More file actions
242 lines (208 loc) · 6.79 KB
/
native.rs
File metadata and controls
242 lines (208 loc) · 6.79 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::{Backend, RawDevice, ROUGH_MAX_ATTACHMENT_COUNT};
use hal::{
device::OutOfMemory,
image::{Extent, SubresourceRange},
pso,
};
use ash::vk;
use inplace_it::inplace_or_alloc_from_iter;
use parking_lot::Mutex;
use smallvec::SmallVec;
use std::{collections::HashMap, sync::Arc};
#[derive(Debug, Hash)]
pub struct Semaphore(pub vk::Semaphore);
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct Fence(pub vk::Fence);
#[derive(Debug, Hash)]
pub struct Event(pub vk::Event);
#[derive(Debug, Hash)]
pub struct GraphicsPipeline(pub vk::Pipeline);
#[derive(Debug, Hash)]
pub struct ComputePipeline(pub vk::Pipeline);
#[derive(Debug, Hash)]
pub struct Memory {
pub(crate) raw: vk::DeviceMemory,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Buffer {
pub(crate) raw: vk::Buffer,
}
unsafe impl Sync for Buffer {}
unsafe impl Send for Buffer {}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BufferView {
pub(crate) raw: vk::BufferView,
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct Image {
pub(crate) raw: vk::Image,
pub(crate) ty: vk::ImageType,
pub(crate) flags: vk::ImageCreateFlags,
pub(crate) extent: vk::Extent3D,
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct ImageView {
pub(crate) image: vk::Image,
pub(crate) raw: vk::ImageView,
pub(crate) range: SubresourceRange,
}
#[derive(Debug, Hash)]
pub struct Sampler(pub vk::Sampler);
#[derive(Debug, Hash)]
pub struct RenderPass {
pub raw: vk::RenderPass,
pub attachment_count: usize,
}
pub type FramebufferKey = SmallVec<[vk::ImageView; ROUGH_MAX_ATTACHMENT_COUNT]>;
#[derive(Debug)]
pub enum Framebuffer {
ImageLess(vk::Framebuffer),
Legacy {
name: String,
map: Mutex<HashMap<FramebufferKey, vk::Framebuffer>>,
extent: Extent,
},
}
pub(crate) type SortedBindings = Arc<Vec<pso::DescriptorSetLayoutBinding>>;
#[derive(Debug)]
pub struct DescriptorSetLayout {
pub(crate) raw: vk::DescriptorSetLayout,
pub(crate) bindings: SortedBindings,
}
#[derive(Debug)]
pub struct DescriptorSet {
pub(crate) raw: vk::DescriptorSet,
pub(crate) bindings: SortedBindings,
}
#[derive(Debug, Hash)]
pub struct PipelineLayout {
pub(crate) raw: vk::PipelineLayout,
}
#[derive(Debug)]
pub struct PipelineCache {
pub(crate) raw: vk::PipelineCache,
}
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct ShaderModule {
pub(crate) raw: vk::ShaderModule,
}
#[derive(Debug)]
pub struct DescriptorPool {
raw: vk::DescriptorPool,
device: Arc<RawDevice>,
/// This vec only exists to re-use allocations when `DescriptorSet`s are freed.
temp_raw_sets: Vec<vk::DescriptorSet>,
/// This vec only exists for collecting the layouts when allocating new sets.
temp_raw_layouts: Vec<vk::DescriptorSetLayout>,
/// This vec only exists for collecting the bindings when allocating new sets.
temp_layout_bindings: Vec<SortedBindings>,
}
impl DescriptorPool {
pub(crate) fn new(raw: vk::DescriptorPool, device: &Arc<RawDevice>) -> Self {
DescriptorPool {
raw,
device: Arc::clone(device),
temp_raw_sets: Vec::new(),
temp_raw_layouts: Vec::new(),
temp_layout_bindings: Vec::new(),
}
}
pub(crate) fn finish(self) -> vk::DescriptorPool {
self.raw
}
}
impl pso::DescriptorPool<Backend> for DescriptorPool {
unsafe fn allocate_one(
&mut self,
layout: &DescriptorSetLayout,
) -> Result<DescriptorSet, pso::AllocationError> {
let raw_layouts = [layout.raw];
let info = vk::DescriptorSetAllocateInfo::builder()
.descriptor_pool(self.raw)
.set_layouts(&raw_layouts);
self.device
.raw
.allocate_descriptor_sets(&info)
//Note: https://github.com/MaikKlein/ash/issues/358
.map(|mut sets| DescriptorSet {
raw: sets.pop().unwrap(),
bindings: Arc::clone(&layout.bindings),
})
.map_err(|err| match err {
vk::Result::ERROR_OUT_OF_HOST_MEMORY => {
pso::AllocationError::OutOfMemory(OutOfMemory::Host)
}
vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => {
pso::AllocationError::OutOfMemory(OutOfMemory::Device)
}
vk::Result::ERROR_OUT_OF_POOL_MEMORY => pso::AllocationError::OutOfPoolMemory,
_ => pso::AllocationError::FragmentedPool,
})
}
unsafe fn allocate<'a, I, E>(
&mut self,
layouts: I,
list: &mut E,
) -> Result<(), pso::AllocationError>
where
I: Iterator<Item = &'a DescriptorSetLayout>,
E: Extend<DescriptorSet>,
{
self.temp_raw_layouts.clear();
self.temp_layout_bindings.clear();
for layout in layouts {
self.temp_raw_layouts.push(layout.raw);
self.temp_layout_bindings.push(Arc::clone(&layout.bindings));
}
let info = vk::DescriptorSetAllocateInfo::builder()
.descriptor_pool(self.raw)
.set_layouts(&self.temp_raw_layouts);
self.device
.raw
.allocate_descriptor_sets(&info)
.map(|sets| {
list.extend(
sets.into_iter()
.zip(self.temp_layout_bindings.drain(..))
.map(|(raw, bindings)| DescriptorSet { raw, bindings }),
)
})
.map_err(|err| match err {
vk::Result::ERROR_OUT_OF_HOST_MEMORY => {
pso::AllocationError::OutOfMemory(OutOfMemory::Host)
}
vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => {
pso::AllocationError::OutOfMemory(OutOfMemory::Device)
}
vk::Result::ERROR_OUT_OF_POOL_MEMORY => pso::AllocationError::OutOfPoolMemory,
_ => pso::AllocationError::FragmentedPool,
})
}
unsafe fn free<I>(&mut self, descriptor_sets: I)
where
I: Iterator<Item = DescriptorSet>,
{
let sets_iter = descriptor_sets.map(|d| d.raw);
inplace_or_alloc_from_iter(sets_iter, |sets| {
if !sets.is_empty() {
if let Err(e) = self.device.raw.free_descriptor_sets(self.raw, sets) {
error!("free_descriptor_sets error {}", e);
}
}
})
}
unsafe fn reset(&mut self) {
assert_eq!(
Ok(()),
self.device
.raw
.reset_descriptor_pool(self.raw, vk::DescriptorPoolResetFlags::empty())
);
}
}
#[derive(Debug, Hash)]
pub struct QueryPool(pub vk::QueryPool);
#[derive(Debug, Hash)]
pub struct Display(pub vk::DisplayKHR);
#[derive(Debug, Hash)]
pub struct DisplayMode(pub vk::DisplayModeKHR);