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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use std::error;
use std::fmt;
use std::mem;
use std::ptr;
use std::sync::Arc;
use smallvec::SmallVec;
use device::Device;
use framebuffer::RenderPass;
use framebuffer::RenderPassAttachmentsList;
use framebuffer::RenderPassCompatible;
use framebuffer::UnsafeRenderPass;
use image::Layout as ImageLayout;
use image::traits::Image;
use image::traits::ImageView;
use Error;
use OomError;
use VulkanObject;
use VulkanPointers;
use check_errors;
use vk;
pub struct Framebuffer<L> {
device: Arc<Device>,
render_pass: Arc<L>,
framebuffer: vk::Framebuffer,
dimensions: [u32; 3],
resources: SmallVec<[(Arc<ImageView>, Arc<Image>, ImageLayout, ImageLayout); 8]>,
}
impl<L> Framebuffer<L> {
pub fn new<A>(render_pass: &Arc<L>, dimensions: [u32; 3],
attachments: A) -> Result<Arc<Framebuffer<L>>, FramebufferCreationError>
where L: RenderPass + RenderPassAttachmentsList<A>
{
let vk = render_pass.inner().device().pointers();
let device = render_pass.inner().device().clone();
let attachments = try!(render_pass.convert_attachments_list(attachments))
.collect::<SmallVec<[_; 8]>>();
{
let limits = render_pass.inner().device().physical_device().limits();
let limits = [limits.max_framebuffer_width(), limits.max_framebuffer_height(),
limits.max_framebuffer_layers()];
if dimensions[0] > limits[0] || dimensions[1] > limits[1] ||
dimensions[2] > limits[2]
{
return Err(FramebufferCreationError::DimensionsTooLarge);
}
}
let ids = {
let mut ids = SmallVec::<[_; 8]>::new();
for &(ref a, _, _, _) in attachments.iter() {
debug_assert!(a.identity_swizzle());
let atch_dims = a.parent().dimensions();
if atch_dims.width() < dimensions[0] || atch_dims.height() < dimensions[1] ||
atch_dims.array_layers() < dimensions[2]
{
return Err(FramebufferCreationError::AttachmentTooSmall);
}
ids.push(a.inner().internal_object());
}
ids
};
let framebuffer = unsafe {
let infos = vk::FramebufferCreateInfo {
sType: vk::STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
pNext: ptr::null(),
flags: 0,
renderPass: render_pass.inner().internal_object(),
attachmentCount: ids.len() as u32,
pAttachments: ids.as_ptr(),
width: dimensions[0],
height: dimensions[1],
layers: dimensions[2],
};
let mut output = mem::uninitialized();
try!(check_errors(vk.CreateFramebuffer(device.internal_object(), &infos,
ptr::null(), &mut output)));
output
};
Ok(Arc::new(Framebuffer {
device: device,
render_pass: render_pass.clone(),
framebuffer: framebuffer,
dimensions: dimensions,
resources: attachments,
}))
}
#[inline]
pub fn is_compatible_with<R>(&self, render_pass: &Arc<R>) -> bool
where R: RenderPass,
L: RenderPass + RenderPassCompatible<R>
{
(&*self.render_pass.inner() as *const UnsafeRenderPass as usize ==
&*render_pass.inner() as *const UnsafeRenderPass as usize) ||
self.render_pass.is_compatible_with(render_pass)
}
#[inline]
pub fn dimensions(&self) -> [u32; 3] {
self.dimensions
}
#[inline]
pub fn width(&self) -> u32 {
self.dimensions[0]
}
#[inline]
pub fn height(&self) -> u32 {
self.dimensions[1]
}
#[inline]
pub fn layers(&self) -> u32 {
self.dimensions[2]
}
#[inline]
pub fn device(&self) -> &Arc<Device> {
&self.device
}
#[inline]
pub fn render_pass(&self) -> &Arc<L> {
&self.render_pass
}
#[inline]
pub fn attachments(&self) -> &[(Arc<ImageView>, Arc<Image>, ImageLayout, ImageLayout)] {
&self.resources
}
}
unsafe impl<L> VulkanObject for Framebuffer<L> {
type Object = vk::Framebuffer;
#[inline]
fn internal_object(&self) -> vk::Framebuffer {
self.framebuffer
}
}
impl<L> Drop for Framebuffer<L> {
#[inline]
fn drop(&mut self) {
unsafe {
let vk = self.device.pointers();
vk.DestroyFramebuffer(self.device.internal_object(), self.framebuffer, ptr::null());
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum FramebufferCreationError {
OomError(OomError),
DimensionsTooLarge,
AttachmentNotIdentitySwizzled,
AttachmentTooSmall,
}
impl From<OomError> for FramebufferCreationError {
#[inline]
fn from(err: OomError) -> FramebufferCreationError {
FramebufferCreationError::OomError(err)
}
}
impl error::Error for FramebufferCreationError {
#[inline]
fn description(&self) -> &str {
match *self {
FramebufferCreationError::OomError(_) => "no memory available",
FramebufferCreationError::DimensionsTooLarge => "the dimensions of the framebuffer \
are too large",
FramebufferCreationError::AttachmentNotIdentitySwizzled => {
"one of the attachments has a component swizzle that is different from identity"
},
FramebufferCreationError::AttachmentTooSmall => {
"one of the attachments is too small compared to the requested framebuffer \
dimensions"
},
}
}
#[inline]
fn cause(&self) -> Option<&error::Error> {
match *self {
FramebufferCreationError::OomError(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for FramebufferCreationError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}
impl From<Error> for FramebufferCreationError {
#[inline]
fn from(err: Error) -> FramebufferCreationError {
FramebufferCreationError::from(OomError::from(err))
}
}
#[cfg(test)]
mod tests {
use format::R8G8B8A8Unorm;
use framebuffer::Framebuffer;
use framebuffer::FramebufferCreationError;
use image::attachment::AttachmentImage;
mod example {
use format::R8G8B8A8Unorm;
single_pass_renderpass! {
attachments: {
color: {
load: Clear,
store: DontCare,
format: R8G8B8A8Unorm,
}
},
pass: {
color: [color],
depth_stencil: {}
}
}
}
#[test]
fn simple_create() {
let (device, _) = gfx_dev_and_queue!();
let render_pass = example::CustomRenderPass::new(&device, &example::Formats {
color: (R8G8B8A8Unorm, 1)
}).unwrap();
let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap();
let _ = Framebuffer::new(&render_pass, [1024, 768, 1], example::AList {
color: &image
}).unwrap();
}
#[test]
fn framebuffer_too_large() {
let (device, _) = gfx_dev_and_queue!();
let render_pass = example::CustomRenderPass::new(&device, &example::Formats {
color: (R8G8B8A8Unorm, 1)
}).unwrap();
let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap();
let alist = example::AList { color: &image };
match Framebuffer::new(&render_pass, [0xffffffff, 0xffffffff, 0xffffffff], alist) {
Err(FramebufferCreationError::DimensionsTooLarge) => (),
_ => panic!()
}
}
#[test]
fn attachment_too_small() {
let (device, _) = gfx_dev_and_queue!();
let render_pass = example::CustomRenderPass::new(&device, &example::Formats {
color: (R8G8B8A8Unorm, 1)
}).unwrap();
let image = AttachmentImage::new(&device, [512, 512], R8G8B8A8Unorm).unwrap();
let alist = example::AList { color: &image };
match Framebuffer::new(&render_pass, [600, 600, 1], alist) {
Err(FramebufferCreationError::AttachmentTooSmall) => (),
_ => panic!()
}
}
}