diff --git a/src/device/draw.rs b/src/device/draw.rs index 8348e8bc8ee..39e6a8f6477 100644 --- a/src/device/draw.rs +++ b/src/device/draw.rs @@ -76,7 +76,7 @@ impl DataBuffer { /// An interface of the abstract command buffer. It collects commands in an /// efficient API-specific manner, to be ready for execution on the device. -pub trait CommandBuffer { +pub trait CommandBuffer { /// An empty constructor fn new() -> Self; /// Clear the command buffer contents, retain the allocated storage @@ -86,9 +86,9 @@ pub trait CommandBuffer { /// Bind an array buffer object fn bind_array_buffer(&mut self, back::ArrayBuffer); /// Bind a vertex attribute - fn bind_attribute(&mut self, ::AttributeSlot, back::Buffer, attrib::Format); + fn bind_attribute(&mut self, ::AttributeSlot, B, attrib::Format); /// Bind an index buffer - fn bind_index(&mut self, back::Buffer); + fn bind_index(&mut self, B); /// Bind a frame buffer object fn bind_frame_buffer(&mut self, target::Access, back::FrameBuffer); /// Unbind any surface from the specified target slot @@ -100,7 +100,7 @@ pub trait CommandBuffer { target::Level, Option); /// Bind a uniform block fn bind_uniform_block(&mut self, back::Program, ::UniformBufferSlot, - ::UniformBlockIndex, back::Buffer); + ::UniformBlockIndex, B); /// Bind a single uniform in the default block fn bind_uniform(&mut self, shade::Location, shade::UniformValue); /// Bind a texture @@ -124,7 +124,7 @@ pub trait CommandBuffer { /// Set output color mask for all targets fn set_color_mask(&mut self, ::state::ColorMask); /// Update a vertex/index/uniform buffer - fn update_buffer(&mut self, back::Buffer, DataPointer, usize); + fn update_buffer(&mut self, B, DataPointer, usize); /// Update a texture region fn update_texture(&mut self, tex::TextureKind, back::Texture, tex::ImageInfo, DataPointer); diff --git a/src/device/gl_device/draw.rs b/src/device/gl_device/draw.rs index c489d456d71..1dd02fa926c 100644 --- a/src/device/gl_device/draw.rs +++ b/src/device/gl_device/draw.rs @@ -17,8 +17,10 @@ use Command; use std::slice; +type Buffer = ::Buffer; + pub struct GlCommandBuffer { - buf: Vec<::Command>, + buf: Vec<::Command>, } impl GlCommandBuffer { @@ -27,7 +29,7 @@ impl GlCommandBuffer { } } -impl ::draw::CommandBuffer for GlCommandBuffer { +impl ::draw::CommandBuffer for GlCommandBuffer { fn new() -> GlCommandBuffer { GlCommandBuffer { buf: Vec::new(), @@ -46,12 +48,12 @@ impl ::draw::CommandBuffer for GlCommandBuffer { self.buf.push(Command::BindArrayBuffer(vao)); } - fn bind_attribute(&mut self, slot: ::AttributeSlot, buf: super::Buffer, + fn bind_attribute(&mut self, slot: ::AttributeSlot, buf: Buffer, format: ::attrib::Format) { self.buf.push(Command::BindAttribute(slot, buf, format)); } - fn bind_index(&mut self, buf: super::Buffer) { + fn bind_index(&mut self, buf: Buffer) { self.buf.push(Command::BindIndex(buf)); } @@ -75,7 +77,7 @@ impl ::draw::CommandBuffer for GlCommandBuffer { } fn bind_uniform_block(&mut self, prog: super::Program, slot: ::UniformBufferSlot, - index: ::UniformBlockIndex, buf: super::Buffer) { + index: ::UniformBlockIndex, buf: Buffer) { self.buf.push(Command::BindUniformBlock(prog, slot, index, buf)); } @@ -120,7 +122,7 @@ impl ::draw::CommandBuffer for GlCommandBuffer { self.buf.push(Command::SetColorMask(mask)); } - fn update_buffer(&mut self, buf: super::Buffer, data: ::draw::DataPointer, + fn update_buffer(&mut self, buf: Buffer, data: ::draw::DataPointer, offset_bytes: usize) { self.buf.push(Command::UpdateBuffer(buf, data, offset_bytes)); } diff --git a/src/device/gl_device/lib.rs b/src/device/gl_device/lib.rs index 33049245347..9231b8cebc2 100644 --- a/src/device/gl_device/lib.rs +++ b/src/device/gl_device/lib.rs @@ -48,7 +48,6 @@ pub struct RawMapping { target: gl::types::GLenum, } -pub type Buffer = gl::types::GLuint; pub type ArrayBuffer = gl::types::GLuint; pub type Shader = gl::types::GLuint; pub type Program = gl::types::GLuint; @@ -567,6 +566,7 @@ impl GlDevice { } impl Device for GlDevice { + type Buffer = gl::types::GLuint; type CommandBuffer = GlCommandBuffer; fn get_capabilities<'a>(&'a self) -> &'a ::Capabilities { @@ -587,7 +587,8 @@ impl Device for GlDevice { } } - fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> ::BufferHandle<()> { + fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) + -> ::BufferHandle<::Buffer, ()> { let name = self.create_buffer_internal(); let info = ::BufferInfo { usage: usage, diff --git a/src/device/lib.rs b/src/device/lib.rs index 99eb719245e..c60448df8ea 100644 --- a/src/device/lib.rs +++ b/src/device/lib.rs @@ -159,25 +159,25 @@ impl Handle { /// Type-safe buffer handle #[derive(Copy, Debug, PartialEq, Clone)] -pub struct BufferHandle { - raw: RawBufferHandle, +pub struct BufferHandle { + raw: RawBufferHandle, } -impl BufferHandle { +impl BufferHandle { /// Create a type-safe BufferHandle from a RawBufferHandle - pub fn from_raw(handle: RawBufferHandle) -> BufferHandle { + pub fn from_raw(handle: RawBufferHandle) -> BufferHandle { BufferHandle { raw: handle, } } /// Cast the type this BufferHandle references - pub fn cast(self) -> BufferHandle { + pub fn cast(self) -> BufferHandle { BufferHandle::from_raw(self.raw) } /// Get the underlying GL name for this BufferHandle - pub fn get_name(&self) -> back::Buffer { + pub fn get_name(&self) -> B { self.raw.get_name() } @@ -187,7 +187,7 @@ impl BufferHandle { } /// Get the underlying raw Handle - pub fn raw(&self) -> RawBufferHandle { + pub fn raw(&self) -> RawBufferHandle { self.raw } @@ -200,8 +200,8 @@ impl BufferHandle { } } -/// Raw (untyped) Buffer Handle -pub type RawBufferHandle = Handle; +/// Raw (untyped) buffer handle +pub type RawBufferHandle = Handle; /// Array Buffer Handle pub type ArrayBufferHandle = Handle; /// Shader Handle @@ -321,11 +321,11 @@ pub struct BufferInfo { /// such as OpenGL (prior to GLNG) and DirectX (prior to DX12) #[allow(missing_docs)] #[derive(Copy, Debug)] -pub enum Command { +pub enum Command { BindProgram(back::Program), BindArrayBuffer(back::ArrayBuffer), - BindAttribute(AttributeSlot, back::Buffer, attrib::Format), - BindIndex(back::Buffer), + BindAttribute(AttributeSlot, B, attrib::Format), + BindIndex(B), BindFrameBuffer(target::Access, back::FrameBuffer), /// Unbind any surface from the specified target slot UnbindTarget(target::Access, target::Target), @@ -334,7 +334,7 @@ pub enum Command { /// Bind a level of the texture to the specified target slot BindTargetTexture(target::Access, target::Target, back::Texture, target::Level, Option), - BindUniformBlock(back::Program, UniformBufferSlot, UniformBlockIndex, back::Buffer), + BindUniformBlock(back::Program, UniformBufferSlot, UniformBlockIndex, B), BindUniform(shade::Location, shade::UniformValue), BindTexture(TextureSlot, tex::TextureKind, back::Texture, Option), SetDrawColorBuffers(usize), @@ -345,7 +345,7 @@ pub enum Command { SetDepthStencilState(Option, Option, state::CullMode), SetBlendState(Option), SetColorMask(state::ColorMask), - UpdateBuffer(back::Buffer, draw::DataPointer, usize), + UpdateBuffer(B, draw::DataPointer, usize), UpdateTexture(tex::TextureKind, back::Texture, tex::ImageInfo, draw::DataPointer), // drawing Clear(target::ClearData, target::Mask), @@ -357,8 +357,10 @@ pub enum Command { /// An interface for performing draw calls using a specific graphics API #[allow(missing_docs)] pub trait Device { - - type CommandBuffer: draw::CommandBuffer; + /// Raw buffer + type Buffer; + /// Command buffer to collect calls and data + type CommandBuffer: draw::CommandBuffer; /// Returns the capabilities available to the specific API implementation fn get_capabilities<'a>(&'a self) -> &'a Capabilities; @@ -368,12 +370,16 @@ pub trait Device { fn submit(&mut self, buffer: (&Self::CommandBuffer, &draw::DataBuffer)); // resource creation - fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) -> BufferHandle<()>; - fn create_buffer(&mut self, num: usize, usage: BufferUsage) -> BufferHandle { + fn create_buffer_raw(&mut self, size: usize, usage: BufferUsage) + -> BufferHandle; + fn create_buffer(&mut self, num: usize, usage: BufferUsage) + -> BufferHandle { self.create_buffer_raw(num * mem::size_of::(), usage).cast() } - fn create_buffer_static_raw(&mut self, data: &[u8]) -> BufferHandle<()>; - fn create_buffer_static(&mut self, data: &[T]) -> BufferHandle { + fn create_buffer_static_raw(&mut self, data: &[u8]) + -> BufferHandle; + fn create_buffer_static(&mut self, data: &[T]) + -> BufferHandle { self.create_buffer_static_raw(as_byte_slice(data)).cast() } fn create_array_buffer(&mut self) -> Result; @@ -386,8 +392,8 @@ pub trait Device { fn create_sampler(&mut self, info: tex::SamplerInfo) -> SamplerHandle; // resource deletion - fn delete_buffer_raw(&mut self, buf: BufferHandle<()>); - fn delete_buffer(&mut self, buf: BufferHandle) { + fn delete_buffer_raw(&mut self, buf: BufferHandle); + fn delete_buffer(&mut self, buf: BufferHandle) { self.delete_buffer_raw(buf.cast()); } fn delete_shader(&mut self, ShaderHandle); @@ -397,17 +403,27 @@ pub trait Device { fn delete_sampler(&mut self, SamplerHandle); /// Update the information stored in a specific buffer - fn update_buffer_raw(&mut self, buf: BufferHandle<()>, data: &[u8], + fn update_buffer_raw(&mut self, buf: BufferHandle, data: &[u8], offset_bytes: usize); - fn update_buffer(&mut self, buf: BufferHandle, data: &[T], - offset_elements: usize) { - self.update_buffer_raw(buf.cast(), as_byte_slice(data), mem::size_of::() * offset_elements) + fn update_buffer(&mut self, buf: BufferHandle, + data: &[T], offset_elements: usize) { + self.update_buffer_raw( + buf.cast(), + as_byte_slice(data), + mem::size_of::() * offset_elements + ) } - fn map_buffer_raw(&mut self, buf: BufferHandle<()>, access: MapAccess) -> back::RawMapping; + 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, @@ -426,7 +442,8 @@ mod test { use super::{BufferHandle, Handle}; use super::{BufferInfo, BufferUsage}; - fn mock_buffer(usage: BufferUsage, len: usize) -> BufferHandle { + fn mock_buffer(usage: BufferUsage, len: usize) + -> BufferHandle { BufferHandle { raw: Handle( 0,