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
use crate::command_buffer::synced::CommandBufferState;
use crate::pipeline::graphics::vertex_input::VertexInputRate;
use crate::pipeline::GraphicsPipeline;
use crate::DeviceSize;
use std::error;
use std::fmt;
pub(in super::super) fn check_vertex_buffers(
current_state: CommandBufferState,
pipeline: &GraphicsPipeline,
vertices: Option<(u32, u32)>,
instances: Option<(u32, u32)>,
) -> Result<(), CheckVertexBufferError> {
let vertex_input = pipeline.vertex_input_state();
let mut max_vertex_count: Option<u32> = None;
let mut max_instance_count: Option<u32> = None;
for (&binding_num, binding_desc) in &vertex_input.bindings {
let vertex_buffer = match current_state.vertex_buffer(binding_num) {
Some(x) => x,
None => return Err(CheckVertexBufferError::BufferNotBound { binding_num }),
};
let mut num_elements = (vertex_buffer.size() / binding_desc.stride as DeviceSize)
.try_into()
.unwrap_or(u32::MAX);
match binding_desc.input_rate {
VertexInputRate::Vertex => {
max_vertex_count = Some(if let Some(x) = max_vertex_count {
x.min(num_elements)
} else {
num_elements
});
}
VertexInputRate::Instance { divisor } => {
if divisor == 0 {
if num_elements != 0 {
num_elements = u32::MAX;
}
} else {
num_elements = num_elements.saturating_mul(divisor);
}
max_instance_count = Some(if let Some(x) = max_instance_count {
x.min(num_elements)
} else {
num_elements
});
}
};
}
if let Some((first_vertex, vertex_count)) = vertices {
if let Some(max_vertex_count) = max_vertex_count {
if first_vertex + vertex_count > max_vertex_count {
return Err(CheckVertexBufferError::TooManyVertices {
vertex_count,
max_vertex_count,
});
}
}
}
if let Some((first_instance, instance_count)) = instances {
if let Some(max_instance_count) = max_instance_count {
if first_instance + instance_count > max_instance_count {
return Err(CheckVertexBufferError::TooManyInstances {
instance_count,
max_instance_count,
}
.into());
}
}
if pipeline.subpass().render_pass().views_used() != 0 {
let max_instance_index = pipeline
.device()
.physical_device()
.properties()
.max_multiview_instance_index
.unwrap_or(0);
let out_of_range = first_instance > max_instance_index
|| (instance_count > 0 && instance_count - 1 > max_instance_index - first_instance);
if out_of_range {
return Err(CheckVertexBufferError::TooManyInstances {
instance_count,
max_instance_count: max_instance_index + 1,
}
.into());
}
}
}
Ok(())
}
#[derive(Debug, Copy, Clone)]
pub enum CheckVertexBufferError {
BufferNotBound { binding_num: u32 },
BufferMissingUsage {
binding_num: u32,
},
TooManyVertices {
vertex_count: u32,
max_vertex_count: u32,
},
TooManyInstances {
instance_count: u32,
max_instance_count: u32,
},
}
impl error::Error for CheckVertexBufferError {}
impl fmt::Display for CheckVertexBufferError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
CheckVertexBufferError::BufferNotBound { .. } => {
"no buffer was bound to a binding slot needed by the pipeline"
}
CheckVertexBufferError::BufferMissingUsage { .. } => {
"the vertex buffer usage is missing on a vertex buffer"
}
CheckVertexBufferError::TooManyVertices { .. } => {
"the draw command requested too many vertices"
}
CheckVertexBufferError::TooManyInstances { .. } => {
"the draw command requested too many instances"
}
}
)
}
}