From 3eee65740887ef2966b4b91fb07eaf9f8ea6dc57 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 14 Feb 2015 19:35:31 +1100 Subject: [PATCH 1/3] Parameterise Renderer over Device --- src/gfx/lib.rs | 2 +- src/render/device_ext.rs | 4 ++-- src/render/lib.rs | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/gfx/lib.rs b/src/gfx/lib.rs index 70e6cf764c8..4d58c55616f 100644 --- a/src/gfx/lib.rs +++ b/src/gfx/lib.rs @@ -55,7 +55,7 @@ pub struct Graphics { /// Graphics device. pub device: D, /// Renderer front-end. - pub renderer: Renderer<::CommandBuffer>, + pub renderer: Renderer, /// Hidden batch context. context: batch::Context, } diff --git a/src/render/device_ext.rs b/src/render/device_ext.rs index 3af3ab9266e..5737a187e0c 100644 --- a/src/render/device_ext.rs +++ b/src/render/device_ext.rs @@ -59,7 +59,7 @@ impl<'a> ShaderSource<'a> { /// Backend extension trait for convenience methods pub trait DeviceExt: device::Device { /// Create a new renderer - fn create_renderer(&mut self) -> ::Renderer<::CommandBuffer>; + fn create_renderer(&mut self) -> ::Renderer; /// 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; @@ -73,7 +73,7 @@ pub trait DeviceExt: device::Device { } impl DeviceExt for D { - fn create_renderer(&mut self) -> ::Renderer { + fn create_renderer(&mut self) -> ::Renderer { ::Renderer { command_buffer: device::draw::CommandBuffer::new(), data_buffer: device::draw::DataBuffer::new(), diff --git a/src/render/lib.rs b/src/render/lib.rs index 3bdcdccb374..43a671d4243 100644 --- a/src/render/lib.rs +++ b/src/render/lib.rs @@ -22,6 +22,7 @@ extern crate "gfx_device_gl" as device; use std::mem; +use device::Device; use device::attrib; use device::attrib::IntSize; use device::draw::CommandBuffer; @@ -141,8 +142,8 @@ pub enum DrawError { } /// Renderer front-end -pub struct Renderer { - command_buffer: C, +pub struct Renderer { + command_buffer: D::CommandBuffer, data_buffer: device::draw::DataBuffer, common_array_buffer: Result, draw_frame_buffer: device::FrameBufferHandle, @@ -152,7 +153,7 @@ pub struct Renderer { parameters: ParamStorage, } -impl Renderer { +impl Renderer { /// Reset all commands for the command buffer re-usal. pub fn reset(&mut self) { self.command_buffer.clear(); @@ -161,12 +162,12 @@ impl Renderer { } /// Get command and data buffers to be submitted to the device. - pub fn as_buffer(&self) -> (&C, &device::draw::DataBuffer) { + pub fn as_buffer(&self) -> (&D::CommandBuffer, &device::draw::DataBuffer) { (&self.command_buffer, &self.data_buffer) } /// Clone the renderer shared data but ignore the commands. - pub fn clone_empty(&self) -> Renderer { + pub fn clone_empty(&self) -> Renderer { Renderer { command_buffer: CommandBuffer::new(), data_buffer: device::draw::DataBuffer::new(), From 9978a0751b1efe932359f8788aeae680414988ff Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 14 Feb 2015 19:38:34 +1100 Subject: [PATCH 2/3] Rename CommandBufferHelper->CommandBufferExt --- src/render/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/render/lib.rs b/src/render/lib.rs index 43a671d4243..d63f40c3f56 100644 --- a/src/render/lib.rs +++ b/src/render/lib.rs @@ -105,14 +105,14 @@ impl ParamStorage{ } } -/// Helper routines for the command buffer +/// Extension methods for the command buffer. /// Useful when Renderer is borrowed, and we need to issue commands. -trait CommandBufferHelper { +trait CommandBufferExt { /// Bind a plane to some target fn bind_target(&mut self, Access, Target, Option<&target::Plane>); } -impl CommandBufferHelper for C { +impl CommandBufferExt for C { fn bind_target(&mut self, access: Access, to: Target, plane: Option<&target::Plane>) { match plane { From dfb71e3844ff77b3386bb3a65632a98faa7a0749 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 14 Feb 2015 20:16:48 +1100 Subject: [PATCH 3/3] Move Command into gl module --- src/device/gl_device/draw.rs | 94 ++++++++++++++++++++++++------------ src/device/gl_device/lib.rs | 6 +-- src/device/lib.rs | 39 --------------- 3 files changed, 67 insertions(+), 72 deletions(-) diff --git a/src/device/gl_device/draw.rs b/src/device/gl_device/draw.rs index c489d456d71..733681401f9 100644 --- a/src/device/gl_device/draw.rs +++ b/src/device/gl_device/draw.rs @@ -14,15 +14,49 @@ //! OpenGL implementation of the Command Buffer -use Command; use std::slice; +use {attrib, draw, target, tex, shade, state}; +use {AttributeSlot, IndexType, InstanceCount, PrimitiveType, TextureSlot, UniformBlockIndex, UniformBufferSlot, VertexCount}; +use super::{ArrayBuffer, Buffer, FrameBuffer, Program, Surface, Texture}; + +/// Serialized device command. +#[derive(Copy, Debug)] +pub enum Command { + BindProgram(Program), + BindArrayBuffer(ArrayBuffer), + BindAttribute(AttributeSlot, Buffer, attrib::Format), + BindIndex(Buffer), + BindFrameBuffer(target::Access, FrameBuffer), + UnbindTarget(target::Access, target::Target), + BindTargetSurface(target::Access, target::Target, Surface), + BindTargetTexture(target::Access, target::Target, Texture, target::Level, Option), + BindUniformBlock(Program, UniformBufferSlot, UniformBlockIndex, Buffer), + BindUniform(shade::Location, shade::UniformValue), + BindTexture(TextureSlot, tex::TextureKind, Texture, Option<::SamplerHandle>), + SetDrawColorBuffers(usize), + SetPrimitiveState(state::Primitive), + SetViewport(target::Rect), + SetMultiSampleState(Option), + SetScissor(Option), + SetDepthStencilState(Option, Option, state::CullMode), + SetBlendState(Option), + SetColorMask(state::ColorMask), + UpdateBuffer(Buffer, draw::DataPointer, usize), + UpdateTexture(tex::TextureKind, Texture, tex::ImageInfo, draw::DataPointer), + // drawing + Clear(target::ClearData, target::Mask), + Draw(PrimitiveType, VertexCount, VertexCount, Option<(InstanceCount, VertexCount)>), + DrawIndexed(PrimitiveType, IndexType, VertexCount, VertexCount, VertexCount, Option<(InstanceCount, VertexCount)>), + Blit(target::Rect, target::Rect, target::Mirror, target::Mask), +} + pub struct GlCommandBuffer { - buf: Vec<::Command>, + buf: Vec, } impl GlCommandBuffer { - pub fn iter<'a>(&'a self) -> slice::Iter<'a, ::Command> { + pub fn iter<'a>(&'a self) -> slice::Iter<'a, Command> { self.buf.iter() } } @@ -38,44 +72,44 @@ impl ::draw::CommandBuffer for GlCommandBuffer { self.buf.clear(); } - fn bind_program(&mut self, prog: super::Program) { + fn bind_program(&mut self, prog: Program) { self.buf.push(Command::BindProgram(prog)); } - fn bind_array_buffer(&mut self, vao: super::ArrayBuffer) { + fn bind_array_buffer(&mut self, vao: ArrayBuffer) { 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)); } - fn bind_frame_buffer(&mut self, access: ::target::Access, fbo: super::FrameBuffer) { + fn bind_frame_buffer(&mut self, access: target::Access, fbo: FrameBuffer) { self.buf.push(Command::BindFrameBuffer(access, fbo)); } - fn unbind_target(&mut self, access: ::target::Access, tar: ::target::Target) { + fn unbind_target(&mut self, access: target::Access, tar: target::Target) { self.buf.push(Command::UnbindTarget(access, tar)); } - fn bind_target_surface(&mut self, access: ::target::Access, - tar: ::target::Target, suf: super::Surface) { + fn bind_target_surface(&mut self, access: target::Access, + tar: target::Target, suf: Surface) { self.buf.push(Command::BindTargetSurface(access, tar, suf)); } - fn bind_target_texture(&mut self, access: ::target::Access, - tar: ::target::Target, tex: super::Texture, - level: ::target::Level, layer: Option<::target::Layer>) { + fn bind_target_texture(&mut self, access: target::Access, + tar: target::Target, tex: Texture, + level: target::Level, layer: Option) { self.buf.push(Command::BindTargetTexture(access, tar, tex, level, layer)); } - fn bind_uniform_block(&mut self, prog: super::Program, slot: ::UniformBufferSlot, - index: ::UniformBlockIndex, buf: super::Buffer) { + fn bind_uniform_block(&mut self, prog: Program, slot: ::UniformBufferSlot, + index: ::UniformBlockIndex, buf: Buffer) { self.buf.push(Command::BindUniformBlock(prog, slot, index, buf)); } @@ -83,7 +117,7 @@ impl ::draw::CommandBuffer for GlCommandBuffer { self.buf.push(Command::BindUniform(loc, value)); } fn bind_texture(&mut self, slot: ::TextureSlot, kind: ::tex::TextureKind, - tex: super::Texture, sampler: Option<::SamplerHandle>) { + tex: Texture, sampler: Option<::SamplerHandle>) { self.buf.push(Command::BindTexture(slot, kind, tex, sampler)); } @@ -91,46 +125,46 @@ impl ::draw::CommandBuffer for GlCommandBuffer { self.buf.push(Command::SetDrawColorBuffers(num)); } - fn set_primitive(&mut self, prim: ::state::Primitive) { + fn set_primitive(&mut self, prim: state::Primitive) { self.buf.push(Command::SetPrimitiveState(prim)); } - fn set_viewport(&mut self, view: ::target::Rect) { + fn set_viewport(&mut self, view: target::Rect) { self.buf.push(Command::SetViewport(view)); } - fn set_multi_sample(&mut self, ms: Option<::state::MultiSample>) { + fn set_multi_sample(&mut self, ms: Option) { self.buf.push(Command::SetMultiSampleState(ms)); } - fn set_scissor(&mut self, rect: Option<::target::Rect>) { + fn set_scissor(&mut self, rect: Option) { self.buf.push(Command::SetScissor(rect)); } - fn set_depth_stencil(&mut self, depth: Option<::state::Depth>, - stencil: Option<::state::Stencil>, cull: ::state::CullMode) { + fn set_depth_stencil(&mut self, depth: Option, + stencil: Option, cull: state::CullMode) { self.buf.push(Command::SetDepthStencilState(depth, stencil, cull)); } - fn set_blend(&mut self, blend: Option<::state::Blend>) { + fn set_blend(&mut self, blend: Option) { self.buf.push(Command::SetBlendState(blend)); } - fn set_color_mask(&mut self, mask: ::state::ColorMask) { + fn set_color_mask(&mut self, mask: state::ColorMask) { 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)); } - fn update_texture(&mut self, kind: ::tex::TextureKind, tex: super::Texture, + fn update_texture(&mut self, kind: ::tex::TextureKind, tex: Texture, info: ::tex::ImageInfo, data: ::draw::DataPointer) { self.buf.push(Command::UpdateTexture(kind, tex, info, data)); } - fn call_clear(&mut self, data: ::target::ClearData, mask: ::target::Mask) { + fn call_clear(&mut self, data: target::ClearData, mask: target::Mask) { self.buf.push(Command::Clear(data, mask)); } @@ -145,8 +179,8 @@ impl ::draw::CommandBuffer for GlCommandBuffer { self.buf.push(Command::DrawIndexed(ptype, itype, start, count, base, instances)); } - fn call_blit(&mut self, s_rect: ::target::Rect, d_rect: ::target::Rect, - mirror: ::target::Mirror, mask: ::target::Mask) { + fn call_blit(&mut self, s_rect: target::Rect, d_rect: target::Rect, + mirror: target::Mirror, mask: target::Mask) { self.buf.push(Command::Blit(s_rect, d_rect, mirror, mask)); } } diff --git a/src/device/gl_device/lib.rs b/src/device/gl_device/lib.rs index 33049245347..da33e4c0e6d 100644 --- a/src/device/gl_device/lib.rs +++ b/src/device/gl_device/lib.rs @@ -28,9 +28,9 @@ use state::{CullMode, RasterMethod, WindingOrder}; use target::{Access, Target}; use BufferUsage; -use Command; use Device; use {MapAccess, ReadableMapping, WritableMapping, RWMapping, BufferHandle, PrimitiveType}; +use self::draw::Command; pub use self::draw::GlCommandBuffer; pub use self::info::{Info, PlatformName, Version}; @@ -170,7 +170,7 @@ impl GlDevice { } /// Fails during a debug build if the implementation's error flag was set. - fn check(&mut self, cmd: &::Command) { + fn check(&mut self, cmd: &Command) { if cfg!(not(ndebug)) { let err = GlError::from_error_code(unsafe { self.gl.GetError() }); if err != GlError::NoError { @@ -221,7 +221,7 @@ impl GlDevice { } } - fn process(&mut self, cmd: &::Command, data_buf: &::draw::DataBuffer) { + fn process(&mut self, cmd: &Command, data_buf: &::draw::DataBuffer) { match *cmd { Command::Clear(ref data, mask) => { let mut flags = 0; diff --git a/src/device/lib.rs b/src/device/lib.rs index 99eb719245e..e3bb80de0fe 100644 --- a/src/device/lib.rs +++ b/src/device/lib.rs @@ -315,45 +315,6 @@ pub struct BufferInfo { pub size: usize, } -/// Serialized device command. -/// While this is supposed to be an internal detail of a device, -/// this particular representation may be used by different backends, -/// such as OpenGL (prior to GLNG) and DirectX (prior to DX12) -#[allow(missing_docs)] -#[derive(Copy, Debug)] -pub enum Command { - BindProgram(back::Program), - BindArrayBuffer(back::ArrayBuffer), - BindAttribute(AttributeSlot, back::Buffer, attrib::Format), - BindIndex(back::Buffer), - BindFrameBuffer(target::Access, back::FrameBuffer), - /// Unbind any surface from the specified target slot - UnbindTarget(target::Access, target::Target), - /// Bind a surface to the specified target slot - BindTargetSurface(target::Access, target::Target, back::Surface), - /// 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), - BindUniform(shade::Location, shade::UniformValue), - BindTexture(TextureSlot, tex::TextureKind, back::Texture, Option), - SetDrawColorBuffers(usize), - SetPrimitiveState(state::Primitive), - SetViewport(target::Rect), - SetMultiSampleState(Option), - SetScissor(Option), - SetDepthStencilState(Option, Option, state::CullMode), - SetBlendState(Option), - SetColorMask(state::ColorMask), - UpdateBuffer(back::Buffer, draw::DataPointer, usize), - UpdateTexture(tex::TextureKind, back::Texture, tex::ImageInfo, draw::DataPointer), - // drawing - Clear(target::ClearData, target::Mask), - Draw(PrimitiveType, VertexCount, VertexCount, Option<(InstanceCount, VertexCount)>), - DrawIndexed(PrimitiveType, IndexType, VertexCount, VertexCount, VertexCount, Option<(InstanceCount, VertexCount)>), - Blit(target::Rect, target::Rect, target::Mirror, target::Mask), -} - /// An interface for performing draw calls using a specific graphics API #[allow(missing_docs)] pub trait Device {