Skip to content

Commit

Permalink
Make CommandBuffer and associated type on Device
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Jan 25, 2015
1 parent b21a766 commit 02a89d7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
35 changes: 18 additions & 17 deletions src/device/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ pub enum MapAccess {
}

/// A handle to a readable map, which can be sliced.
pub struct ReadableMapping<'a, T: Copy, C: draw::CommandBuffer, D: 'a + Device<C>> {
pub struct ReadableMapping<'a, T: Copy, D: 'a + Device> {
raw: back::RawMapping,
len: usize,
device: &'a mut D,
}

impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> Deref for ReadableMapping<'a, T, C, D> {
impl<'a, T: Copy, D: Device> Deref for ReadableMapping<'a, T, D> {
type Target = [T];

fn deref(&self) -> &[T] {
Expand All @@ -83,20 +83,20 @@ impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> Deref for ReadableMappin
}

#[unsafe_destructor]
impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> Drop for ReadableMapping<'a, T, C, D> {
impl<'a, T: Copy, D: Device> Drop for ReadableMapping<'a, T, D> {
fn drop(&mut self) {
self.device.unmap_buffer_raw(self.raw)
}
}

/// A handle to a writable map, which only allows setting elements.
pub struct WritableMapping<'a, T: Copy, C: draw::CommandBuffer, D: 'a + Device<C>> {
pub struct WritableMapping<'a, T: Copy, D: 'a + Device> {
raw: back::RawMapping,
len: usize,
device: &'a mut D,
}

impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> WritableMapping<'a, T, C, D> {
impl<'a, T: Copy, D: Device> WritableMapping<'a, T, D> {
/// Set a value in the buffer
pub fn set(&mut self, idx: usize, val: T) {
if idx >= self.len {
Expand All @@ -107,35 +107,35 @@ impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> WritableMapping<'a, T, C
}

#[unsafe_destructor]
impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> Drop for WritableMapping<'a, T, C, D> {
impl<'a, T: Copy, D: Device> Drop for WritableMapping<'a, T, D> {
fn drop(&mut self) {
self.device.unmap_buffer_raw(self.raw)
}
}

/// A handle to a complete readable/writable map, which can be sliced both ways.
pub struct RWMapping<'a, T: Copy, C: draw::CommandBuffer, D: 'a + Device<C>> {
pub struct RWMapping<'a, T: Copy, D: 'a + Device> {
raw: back::RawMapping,
len: usize,
device: &'a mut D,
}

impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> Deref for RWMapping<'a, T, C, D> {
impl<'a, T: Copy, D: Device> Deref for RWMapping<'a, T, D> {
type Target = [T];

fn deref(&self) -> &[T] {
unsafe { mem::transmute(slice::from_raw_buf(&(self.raw.pointer as *const T), self.len)) }
}
}

impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> DerefMut for RWMapping<'a, T, C, D> {
impl<'a, T: Copy, D: Device> DerefMut for RWMapping<'a, T, D> {
fn deref_mut(&mut self) -> &mut [T] {
unsafe { mem::transmute(slice::from_raw_mut_buf(&self.raw.pointer, self.len)) }
}
}

#[unsafe_destructor]
impl<'a, T: Copy, C: draw::CommandBuffer, D: Device<C>> Drop for RWMapping<'a, T, C, D> {
impl<'a, T: Copy, D: Device> Drop for RWMapping<'a, T, D> {
fn drop(&mut self) {
self.device.unmap_buffer_raw(self.raw)
}
Expand Down Expand Up @@ -356,17 +356,18 @@ pub enum Command {
Blit(target::Rect, target::Rect, target::Mask),
}

// CommandBuffer is really an associated type, so will look much better when
// Rust supports this natively.
/// An interface for performing draw calls using a specific graphics API
#[allow(missing_docs)]
pub trait Device<C: draw::CommandBuffer> {
pub trait Device {

type CommandBuffer: draw::CommandBuffer;

/// Returns the capabilities available to the specific API implementation
fn get_capabilities<'a>(&'a self) -> &'a Capabilities;
/// Reset all the states to disabled/default
fn reset_state(&mut self);
/// Submit a command buffer for execution
fn submit(&mut self, buffer: (&C, &draw::DataBuffer));
fn submit(&mut self, buffer: (&Self::CommandBuffer, &draw::DataBuffer));

// resource creation
fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> BufferHandle<()>;
Expand Down Expand Up @@ -407,9 +408,9 @@ pub trait Device<C: draw::CommandBuffer> {
}
fn map_buffer_raw(&mut self, buf: BufferHandle<()>, access: MapAccess) -> back::RawMapping;
fn unmap_buffer_raw(&mut self, map: back::RawMapping);
fn map_buffer_readable<T: Copy>(&mut self, buf: BufferHandle<T>) -> ReadableMapping<T, C, Self>;
fn map_buffer_writable<T: Copy>(&mut self, buf: BufferHandle<T>) -> WritableMapping<T, C, Self>;
fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<T>) -> RWMapping<T, C, Self>;
fn map_buffer_readable<T: Copy>(&mut self, buf: BufferHandle<T>) -> ReadableMapping<T, Self>;
fn map_buffer_writable<T: Copy>(&mut self, buf: BufferHandle<T>) -> WritableMapping<T, Self>;
fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<T>) -> RWMapping<T, Self>;

/// Update the information stored in a texture
fn update_texture_raw(&mut self, tex: &TextureHandle, img: &tex::ImageInfo,
Expand Down
8 changes: 4 additions & 4 deletions src/gfx/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ pub use device::target::{COLOR, DEPTH, STENCIL};
pub use device::gl_device::{GlDevice, GlCommandBuffer};

/// A convenient wrapper suitable for single-threaded operation.
pub struct Graphics<D, C: device::draw::CommandBuffer> {
pub struct Graphics<D: device::Device> {
/// Graphics device.
pub device: D,
/// Renderer front-end.
pub renderer: Renderer<C>,
pub renderer: Renderer<<D as device::Device>::CommandBuffer>,
/// Hidden batch context.
context: batch::Context,
}

impl<D: device::Device<C>, C: device::draw::CommandBuffer> Graphics<D, C> {
impl<D: device::Device> Graphics<D> {
/// Create a new graphics wrapper.
pub fn new(mut device: D) -> Graphics<D, C> {
pub fn new(mut device: D) -> Graphics<D> {
let rend = device.create_renderer();
Graphics {
device: device,
Expand Down
10 changes: 6 additions & 4 deletions src/gl_device/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,9 @@ impl GlDevice {
}
}

impl Device<GlCommandBuffer> for GlDevice {
impl Device for GlDevice {
type CommandBuffer = GlCommandBuffer;

fn get_capabilities<'a>(&'a self) -> &'a ::Capabilities {
&self.caps
}
Expand Down Expand Up @@ -753,7 +755,7 @@ impl Device<GlCommandBuffer> for GlDevice {
unsafe { self.gl.UnmapBuffer(map.target) };
}

fn map_buffer_readable<T: Copy>(&mut self, buf: BufferHandle<T>) -> ReadableMapping<T, GlCommandBuffer, GlDevice> {
fn map_buffer_readable<T: Copy>(&mut self, buf: BufferHandle<T>) -> ReadableMapping<T, GlDevice> {
let map = self.map_buffer_raw(buf.cast(), MapAccess::Readable);
ReadableMapping {
raw: map,
Expand All @@ -762,7 +764,7 @@ impl Device<GlCommandBuffer> for GlDevice {
}
}

fn map_buffer_writable<T: Copy>(&mut self, buf: BufferHandle<T>) -> WritableMapping<T, GlCommandBuffer, GlDevice> {
fn map_buffer_writable<T: Copy>(&mut self, buf: BufferHandle<T>) -> WritableMapping<T, GlDevice> {
let map = self.map_buffer_raw(buf.cast(), MapAccess::Writable);
WritableMapping {
raw: map,
Expand All @@ -771,7 +773,7 @@ impl Device<GlCommandBuffer> for GlDevice {
}
}

fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<T>) -> RWMapping<T, GlCommandBuffer, GlDevice> {
fn map_buffer_rw<T: Copy>(&mut self, buf: BufferHandle<T>) -> RWMapping<T, GlDevice> {
let map = self.map_buffer_raw(buf.cast(), MapAccess::RW);
RWMapping {
raw: map,
Expand Down
8 changes: 4 additions & 4 deletions src/render/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ impl<C: CommandBuffer> Renderer<C> {
}

/// Backend extension trait for convenience methods
pub trait DeviceHelper<C: CommandBuffer> {
pub trait DeviceHelper: device::Device {
/// Create a new renderer
fn create_renderer(&mut self) -> Renderer<C>;
fn create_renderer(&mut self) -> Renderer<<Self as device::Device>::CommandBuffer>;
/// Create a new mesh from the given vertex data.
/// Convenience function around `create_buffer` and `Mesh::from_format`.
fn create_mesh<T: mesh::VertexFormat + Copy>(&mut self, data: &[T]) -> mesh::Mesh;
Expand All @@ -460,8 +460,8 @@ pub trait DeviceHelper<C: CommandBuffer> {
-> Result<device::ProgramHandle, ProgramError>;
}

impl<D: device::Device<C>, C: CommandBuffer> DeviceHelper<C> for D {
fn create_renderer(&mut self) -> Renderer<C> {
impl<D: device::Device> DeviceHelper for D {
fn create_renderer(&mut self) -> Renderer<<D as device::Device>::CommandBuffer> {
Renderer {
command_buffer: CommandBuffer::new(),
data_buffer: device::draw::DataBuffer::new(),
Expand Down

0 comments on commit 02a89d7

Please sign in to comment.