From 02a89d78988b9ee4ea742c7977aded251b09ea44 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 25 Jan 2015 14:40:48 +1100 Subject: [PATCH] Make CommandBuffer and associated type on Device --- src/device/lib.rs | 35 ++++++++++++++++++----------------- src/gfx/lib.rs | 8 ++++---- src/gl_device/lib.rs | 10 ++++++---- src/render/lib.rs | 8 ++++---- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/device/lib.rs b/src/device/lib.rs index 11d67a306bc..88e913bd9e4 100644 --- a/src/device/lib.rs +++ b/src/device/lib.rs @@ -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> { +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> 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] { @@ -83,20 +83,20 @@ impl<'a, T: Copy, C: draw::CommandBuffer, D: Device> Deref for ReadableMappin } #[unsafe_destructor] -impl<'a, T: Copy, C: draw::CommandBuffer, D: Device> 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> { +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> 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 { @@ -107,20 +107,20 @@ impl<'a, T: Copy, C: draw::CommandBuffer, D: Device> WritableMapping<'a, T, C } #[unsafe_destructor] -impl<'a, T: Copy, C: draw::CommandBuffer, D: Device> 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> { +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> 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] { @@ -128,14 +128,14 @@ impl<'a, T: Copy, C: draw::CommandBuffer, D: Device> Deref for RWMapping<'a, } } -impl<'a, T: Copy, C: draw::CommandBuffer, D: Device> 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> 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) } @@ -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 { +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<()>; @@ -407,9 +408,9 @@ pub trait Device { } 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(&mut self, buf: BufferHandle) -> ReadableMapping; - fn map_buffer_writable(&mut self, buf: BufferHandle) -> WritableMapping; - fn map_buffer_rw(&mut self, buf: BufferHandle) -> RWMapping; + fn map_buffer_readable(&mut self, buf: BufferHandle) -> ReadableMapping; + fn map_buffer_writable(&mut self, buf: BufferHandle) -> WritableMapping; + fn map_buffer_rw(&mut self, buf: BufferHandle) -> RWMapping; /// Update the information stored in a texture fn update_texture_raw(&mut self, tex: &TextureHandle, img: &tex::ImageInfo, diff --git a/src/gfx/lib.rs b/src/gfx/lib.rs index 266a6b80241..3ec38e746c0 100644 --- a/src/gfx/lib.rs +++ b/src/gfx/lib.rs @@ -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 { +pub struct Graphics { /// Graphics device. pub device: D, /// Renderer front-end. - pub renderer: Renderer, + pub renderer: Renderer<::CommandBuffer>, /// Hidden batch context. context: batch::Context, } -impl, C: device::draw::CommandBuffer> Graphics { +impl Graphics { /// Create a new graphics wrapper. - pub fn new(mut device: D) -> Graphics { + pub fn new(mut device: D) -> Graphics { let rend = device.create_renderer(); Graphics { device: device, diff --git a/src/gl_device/lib.rs b/src/gl_device/lib.rs index 4908f21ecb4..1012c7f86ae 100644 --- a/src/gl_device/lib.rs +++ b/src/gl_device/lib.rs @@ -558,7 +558,9 @@ impl GlDevice { } } -impl Device for GlDevice { +impl Device for GlDevice { + type CommandBuffer = GlCommandBuffer; + fn get_capabilities<'a>(&'a self) -> &'a ::Capabilities { &self.caps } @@ -753,7 +755,7 @@ impl Device for GlDevice { unsafe { self.gl.UnmapBuffer(map.target) }; } - fn map_buffer_readable(&mut self, buf: BufferHandle) -> ReadableMapping { + fn map_buffer_readable(&mut self, buf: BufferHandle) -> ReadableMapping { let map = self.map_buffer_raw(buf.cast(), MapAccess::Readable); ReadableMapping { raw: map, @@ -762,7 +764,7 @@ impl Device for GlDevice { } } - fn map_buffer_writable(&mut self, buf: BufferHandle) -> WritableMapping { + fn map_buffer_writable(&mut self, buf: BufferHandle) -> WritableMapping { let map = self.map_buffer_raw(buf.cast(), MapAccess::Writable); WritableMapping { raw: map, @@ -771,7 +773,7 @@ impl Device for GlDevice { } } - fn map_buffer_rw(&mut self, buf: BufferHandle) -> RWMapping { + fn map_buffer_rw(&mut self, buf: BufferHandle) -> RWMapping { let map = self.map_buffer_raw(buf.cast(), MapAccess::RW); RWMapping { raw: map, diff --git a/src/render/lib.rs b/src/render/lib.rs index c726f8e4299..8c26729283b 100644 --- a/src/render/lib.rs +++ b/src/render/lib.rs @@ -449,9 +449,9 @@ impl Renderer { } /// Backend extension trait for convenience methods -pub trait DeviceHelper { +pub trait DeviceHelper: device::Device { /// Create a new renderer - fn create_renderer(&mut self) -> Renderer; + fn create_renderer(&mut self) -> Renderer<::CommandBuffer>; /// Create a new mesh from the given vertex data. /// Convenience function around `create_buffer` and `Mesh::from_format`. fn create_mesh(&mut self, data: &[T]) -> mesh::Mesh; @@ -460,8 +460,8 @@ pub trait DeviceHelper { -> Result; } -impl, C: CommandBuffer> DeviceHelper for D { - fn create_renderer(&mut self) -> Renderer { +impl DeviceHelper for D { + fn create_renderer(&mut self) -> Renderer<::CommandBuffer> { Renderer { command_buffer: CommandBuffer::new(), data_buffer: device::draw::DataBuffer::new(),