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
use std::error;
use std::fmt;
use std::ptr;
use std::sync::Arc;
use smallvec::SmallVec;
use command_buffer::CommandAddError;
use command_buffer::cb::AddCommand;
use command_buffer::cb::UnsafeCommandBufferBuilder;
use command_buffer::pool::CommandPool;
use descriptor::descriptor_set::DescriptorSetsCollection;
use descriptor::pipeline_layout::PipelineLayoutAbstract;
use descriptor::pipeline_layout::PipelineLayoutSetsCompatible;
use device::Device;
use device::DeviceOwned;
use VulkanObject;
use VulkanPointers;
use vk;
pub struct CmdBindDescriptorSets<S, P> {
pipeline_ty: vk::PipelineBindPoint,
raw_pipeline_layout: vk::PipelineLayout,
raw_sets: SmallVec<[(u32, SmallVec<[vk::DescriptorSet; 8]>); 4]>,
device: Arc<Device>,
sets: S,
pipeline_layout: P,
}
impl<S, P> CmdBindDescriptorSets<S, P>
where P: PipelineLayoutAbstract, S: DescriptorSetsCollection
{
#[inline]
pub fn new(graphics: bool, pipeline_layout: P, sets: S)
-> Result<CmdBindDescriptorSets<S, P>, CmdBindDescriptorSetsError>
{
if !PipelineLayoutSetsCompatible::is_compatible(&pipeline_layout, &sets) {
return Err(CmdBindDescriptorSetsError::IncompatibleSets);
}
let raw_pipeline_layout = pipeline_layout.sys().internal_object();
let device = pipeline_layout.device().clone();
let raw_sets = {
let mut raw_sets: SmallVec<[(u32, SmallVec<[_; 8]>); 4]> = SmallVec::new();
let mut add_new = true;
for set_num in 0 .. sets.num_sets() {
let set = match sets.descriptor_set(set_num) {
Some(set) => set.internal_object(),
None => { add_new = true; continue; },
};
if add_new {
let mut v = SmallVec::new(); v.push(set);
raw_sets.push((set_num as u32, v));
add_new = false;
} else {
raw_sets.last_mut().unwrap().1.push(set);
}
}
raw_sets
};
Ok(CmdBindDescriptorSets {
raw_pipeline_layout: raw_pipeline_layout,
raw_sets: raw_sets,
pipeline_ty: if graphics { vk::PIPELINE_BIND_POINT_GRAPHICS }
else { vk::PIPELINE_BIND_POINT_COMPUTE },
device: device,
sets: sets,
pipeline_layout: pipeline_layout,
})
}
}
impl<S, P> CmdBindDescriptorSets<S, P> {
#[inline]
pub fn is_graphics(&self) -> bool {
self.pipeline_ty == vk::PIPELINE_BIND_POINT_GRAPHICS
}
}
unsafe impl<S, Pl> DeviceOwned for CmdBindDescriptorSets<S, Pl>
where Pl: DeviceOwned
{
#[inline]
fn device(&self) -> &Arc<Device> {
self.pipeline_layout.device()
}
}
unsafe impl<'a, P, Pl, S> AddCommand<&'a CmdBindDescriptorSets<S, Pl>> for UnsafeCommandBufferBuilder<P>
where P: CommandPool
{
type Out = UnsafeCommandBufferBuilder<P>;
#[inline]
fn add(self, command: &'a CmdBindDescriptorSets<S, Pl>) -> Result<Self::Out, CommandAddError> {
unsafe {
let vk = self.device().pointers();
let cmd = self.internal_object();
for &(first_set, ref sets) in command.raw_sets.iter() {
vk.CmdBindDescriptorSets(cmd, command.pipeline_ty, command.raw_pipeline_layout,
first_set, sets.len() as u32, sets.as_ptr(),
0, ptr::null());
}
}
Ok(self)
}
}
#[derive(Debug, Copy, Clone)]
pub enum CmdBindDescriptorSetsError {
IncompatibleSets,
}
impl error::Error for CmdBindDescriptorSetsError {
#[inline]
fn description(&self) -> &str {
match *self {
CmdBindDescriptorSetsError::IncompatibleSets => {
"the sets are not compatible with the pipeline layout"
},
}
}
}
impl fmt::Display for CmdBindDescriptorSetsError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}