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
#![deny(missing_docs)]
use std::{fmt, mem};
use std::hash::Hash;
pub use draw_state::target;
pub use draw_state::state;
pub mod attrib;
pub mod draw;
pub mod handle;
pub mod mapping;
pub mod shade;
pub mod tex;
mod arc;
pub type VertexCount = u32;
pub type InstanceCount = u32;
pub type UniformBlockIndex = u8;
pub type AttributeSlot = u8;
pub type UniformBufferSlot = u8;
pub type TextureSlot = u8;
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct NotSupported;
pub fn as_byte_slice<T>(slice: &[T]) -> &[u8] {
use std::slice;
let len = mem::size_of::<T>() * slice.len();
unsafe {
slice::from_raw_parts(slice.as_ptr() as *const u8, len)
}
}
#[derive(Copy, Clone, Debug)]
#[allow(missing_docs)]
pub struct Capabilities {
pub shader_model: shade::ShaderModel,
pub max_draw_buffers: usize,
pub max_texture_size: usize,
pub max_vertex_attributes: usize,
pub array_buffer_supported: bool,
pub fragment_output_supported: bool,
pub immutable_storage_supported: bool,
pub instance_base_supported: bool,
pub instance_call_supported: bool,
pub instance_rate_supported: bool,
pub render_targets_supported: bool,
pub sampler_objects_supported: bool,
pub srgb_color_supported: bool,
pub uniform_block_supported: bool,
pub vertex_base_supported: bool,
}
#[derive(Copy, Clone)]
pub enum MapAccess {
Readable,
Writable,
RW
}
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(u8)]
pub enum PrimitiveType {
Point,
Line,
LineStrip,
TriangleList,
TriangleStrip,
TriangleFan,
}
pub type IndexType = attrib::IntSize;
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[repr(u8)]
pub enum BufferRole {
Vertex,
Index,
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[repr(u8)]
pub enum BufferUsage {
Static,
Dynamic,
Stream,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct BufferInfo {
pub role: BufferRole,
pub usage: BufferUsage,
pub size: usize,
}
#[allow(missing_docs)]
pub trait Resources: Clone + Hash + fmt::Debug + Eq + PartialEq {
type Buffer: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
type ArrayBuffer: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
type Shader: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
type Program: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
type FrameBuffer: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
type Surface: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
type Texture: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
type Sampler: Copy + Clone + Hash + fmt::Debug + Eq + PartialEq + Send + Sync;
}
#[allow(missing_docs)]
pub trait Factory<R: Resources> {
type Mapper: Clone + mapping::Raw;
fn get_capabilities<'a>(&'a self) -> &'a Capabilities;
fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> handle::RawBuffer<R>;
fn create_buffer<T>(&mut self, num: usize, usage: BufferUsage) -> handle::Buffer<R, T> {
handle::Buffer::from_raw(
self.create_buffer_raw(num * mem::size_of::<T>(), usage))
}
fn create_buffer_static_raw(&mut self, data: &[u8], role: BufferRole) -> handle::RawBuffer<R>;
fn create_buffer_static<T>(&mut self, data: &[T]) -> handle::Buffer<R, T> {
handle::Buffer::from_raw(
self.create_buffer_static_raw(as_byte_slice(data), BufferRole::Vertex))
}
fn create_buffer_index<T>(&mut self, data: &[T]) -> handle::IndexBuffer<R, T> {
handle::IndexBuffer::from_raw(
self.create_buffer_static_raw(as_byte_slice(data), BufferRole::Index))
}
fn create_array_buffer(&mut self) -> Result<handle::ArrayBuffer<R>, NotSupported>;
fn create_shader(&mut self, stage: shade::Stage, code: &[u8]) ->
Result<handle::Shader<R>, shade::CreateShaderError>;
fn create_program(&mut self, shaders: &[handle::Shader<R>], targets: Option<&[&str]>)
-> Result<handle::Program<R>, shade::CreateProgramError>;
fn create_frame_buffer(&mut self) -> Result<handle::FrameBuffer<R>, NotSupported>;
fn create_surface(&mut self, tex::SurfaceInfo) -> Result<handle::Surface<R>, tex::SurfaceError>;
fn create_texture(&mut self, tex::TextureInfo) -> Result<handle::Texture<R>, tex::TextureError>;
fn create_sampler(&mut self, tex::SamplerInfo) -> handle::Sampler<R>;
fn update_buffer_raw(&mut self, buf: &handle::RawBuffer<R>, data: &[u8], offset_bytes: usize);
fn update_buffer<T>(&mut self, buf: &handle::Buffer<R, T>, data: &[T], offset_elements: usize) {
self.update_buffer_raw(buf.raw(), as_byte_slice(data), mem::size_of::<T>() * offset_elements)
}
fn map_buffer_raw(&mut self, &handle::RawBuffer<R>, MapAccess) -> Self::Mapper;
fn unmap_buffer_raw(&mut self, Self::Mapper);
fn map_buffer_readable<T: Copy>(&mut self, &handle::Buffer<R, T>) -> mapping::Readable<T, R, Self>;
fn map_buffer_writable<T: Copy>(&mut self, &handle::Buffer<R, T>) -> mapping::Writable<T, R, Self>;
fn map_buffer_rw<T: Copy>(&mut self, &handle::Buffer<R, T>) -> mapping::RW<T, R, Self>;
fn update_texture_raw(&mut self, tex: &handle::Texture<R>,
img: &tex::ImageInfo, data: &[u8],
kind: Option<tex::TextureKind>) -> Result<(), tex::TextureError>;
fn update_texture<T>(&mut self, tex: &handle::Texture<R>,
img: &tex::ImageInfo, data: &[T],
kind: Option<tex::TextureKind>) -> Result<(), tex::TextureError> {
self.update_texture_raw(tex, img, as_byte_slice(data), kind)
}
fn generate_mipmap(&mut self, &handle::Texture<R>);
fn create_texture_static<T>(&mut self, info: tex::TextureInfo, data: &[T])
-> Result<handle::Texture<R>, tex::TextureError> {
let image_info = info.to_image_info();
match self.create_texture(info) {
Ok(handle) => self.update_texture(&handle, &image_info, data, None)
.map(|_| handle),
Err(e) => Err(e),
}
}
fn cleanup(&mut self);
}
pub type SubmitInfo<'a, D: Device> = (
&'a D::CommandBuffer,
&'a draw::DataBuffer,
&'a handle::Manager<D::Resources>
);
pub trait Device {
type Resources: Resources;
type CommandBuffer: draw::CommandBuffer<Self::Resources>;
fn get_capabilities<'a>(&'a self) -> &'a Capabilities;
fn reset_state(&mut self);
fn submit(&mut self, SubmitInfo<Self>);
fn after_frame(&mut self);
}