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
use std::error::Error as StdError;
use std::fmt;
use std::sync::Arc;
use vulkano::buffer::{BufferAccess, CpuAccessibleBuffer};
use vulkano::buffer::cpu_access::{
ReadLock,
WriteLock,
ReadLockError,
WriteLockError
};
use framing::Image;
#[derive(Clone, Debug)]
pub struct Buffer<T> {
inner: Arc<CpuAccessibleBuffer<[T]>>,
width: usize,
height: usize
}
impl<T> Buffer<T> where T: Send + Sync + 'static {
pub fn new(
inner: Arc<CpuAccessibleBuffer<[T]>>,
width: usize,
height: usize
) -> Result<Self, BadLength> {
let expected_len = width * height;
let actual_len = inner.len();
if expected_len == actual_len {
Ok(Self {
inner,
width,
height
})
} else {
Err(BadLength {
expected_len,
actual_len
})
}
}
}
impl<T: 'static> Buffer<T> {
pub fn read(&self) -> Result<Reader<T>, ReadLockError> {
let inner = self.inner.read()?;
let (width, height) = (self.width, self.height);
Ok(Reader { inner, width, height })
}
pub fn write(&self) -> Result<Writer<T>, WriteLockError> {
let inner = self.inner.write()?;
let (width, height) = (self.width, self.height);
Ok(Writer { inner, width, height })
}
}
impl<T> Buffer<T> {
pub fn buffer(&self) -> &Arc<CpuAccessibleBuffer<[T]>> {
&self.inner
}
pub fn width(&self) -> usize { self.width }
pub fn height(&self) -> usize { self.height }
}
pub struct Reader<'a, T: 'a> {
inner: ReadLock<'a, [T]>,
width: usize,
height: usize
}
impl<'a, T: 'a> Image for Reader<'a, T> where T: Clone {
type Pixel = T;
fn width(&self) -> usize { self.width }
fn height(&self) -> usize { self.height }
unsafe fn pixel(&self, x: usize, y: usize) -> Self::Pixel {
self.inner.get_unchecked(y * self.width + x).clone()
}
}
impl<'a, T: 'a> AsRef<[T]> for Reader<'a, T> {
fn as_ref(&self) -> &[T] {
&self.inner
}
}
pub struct Writer<'a, T: 'a> {
inner: WriteLock<'a, [T]>,
width: usize,
height: usize
}
impl<'a, T: 'a> Image for Writer<'a, T> where T: Clone {
type Pixel = T;
fn width(&self) -> usize { self.width }
fn height(&self) -> usize { self.height }
unsafe fn pixel(&self, x: usize, y: usize) -> Self::Pixel {
self.inner.get_unchecked(y * self.width + x).clone()
}
}
impl<'a, T: 'a> AsRef<[T]> for Writer<'a, T> {
fn as_ref(&self) -> &[T] {
&self.inner
}
}
impl<'a, T: 'a> AsMut<[T]> for Writer<'a, T> {
fn as_mut(&mut self) -> &mut [T] {
&mut self.inner
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BadLength {
expected_len: usize,
actual_len: usize
}
impl fmt::Display for BadLength {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"the buffer had to have {} pixels, but had {} pixels",
self.expected_len,
self.actual_len
)
}
}
impl StdError for BadLength {
fn description(&self) -> &str {
"incorrect buffer length"
}
}