From 140d25ab975fff508589a9ac12cd368b8cfc8601 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 6 Jul 2015 11:13:19 +0200 Subject: [PATCH] Add some initial code draft --- DESIGN.md | 4 ++ src/buffer.rs | 0 src/command_buffer.rs | 97 +++++++++++++++++++++++++++++++++++++++++++ src/device/mod.rs | 28 +++++++++++++ src/image.rs | 0 src/lib.rs | 8 ++++ 6 files changed, 137 insertions(+) create mode 100644 src/buffer.rs create mode 100644 src/command_buffer.rs create mode 100644 src/device/mod.rs create mode 100644 src/image.rs diff --git a/DESIGN.md b/DESIGN.md index ca4e2b3e..1e68814f 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -130,3 +130,7 @@ Pipelines, however, would still be created manually. ## Memory views **To do** + +## Context loss + +**To do** diff --git a/src/buffer.rs b/src/buffer.rs new file mode 100644 index 00000000..e69de29b diff --git a/src/command_buffer.rs b/src/command_buffer.rs new file mode 100644 index 00000000..89396e2f --- /dev/null +++ b/src/command_buffer.rs @@ -0,0 +1,97 @@ + +/// Represents a prototype of a command buffer. +/// +/// # Usage +/// +/// ```norun +/// let commands_buffer = +/// CommandBufferBuilder::new(&device) +/// .copy_memory(..., ...) +/// .draw(...) +/// .build(); +/// +/// ``` +/// +/// # Implementation +/// +/// Builds a command buffer and starts writing into it. Each function call directly adds a command +/// into the buffer. +/// +/// Resources that are used are held in the `CommandBufferBuilder` and the `CommandBuffer`, +/// ensuring that they don't get destroyed as long as the command buffer is alive. +/// +pub struct CommandBufferBuilder { + device: Arc, + cmd: Option, + memory_refs: Vec, + buffers: Vec>, + images: Vec>, + pipelines: Vec>, +} + +impl CommandBufferBuilder { + /// Builds a new prototype of a command buffer. + pub fn new(device: &Arc) -> CommandBufferBuilder { + let infos = ffi::GR_CMD_BUFFER_CREATE_INFO { + queueType: ffi::GR_QUEUE_UNIVERSAL, + flags: 0, + }; + + let cmd_buffer = unsafe { + let mut cmd = mem::uninitialized(); + error::check_result(ffi::grCreateCommandBuffer(*device.get_id(), + &infos, &mut cmd)).unwrap(); + cmd + }; + + error::check_result(unsafe { ffi::grBeginCommandBuffer(cmd_buffer, 0) }).unwrap(); + + CommandBufferBuilder { + device: device.clone(), + cmd: Some(cmd_buffer), + memory_refs: Vec::new(), + } + } + + /// Builds the command buffer containing all the commands. + pub fn build(mut self) -> Arc { + let cmd_buffer = self.cmd.take().unwrap(); + error::check_result(unsafe { ffi::grEndCommandBuffer(cmd_buffer) }).unwrap(); + + Arc::new(CommandBuffer { + device: self.device.clone(), + cmd: cmd_buffer, + memory_refs: mem::replace(&mut self.memory_refs, Vec::with_capacity(0)), + buffers: mem::replace(&mut self.buffers, Vec::with_capacity(0)), + images: mem::replace(&mut self.images, Vec::with_capacity(0)), + pipelines: mem::replace(&mut self.pipelines, Vec::with_capacity(0)), + }) + } +} + +impl Drop for CommandBufferBuilder { + fn drop(&mut self) { + if let Some(cmd) = self.cmd { + error::check_result(unsafe { ffi::grEndCommandBuffer(cmd) }).unwrap(); + error::check_result(unsafe { ffi::grDestroyObject(cmd) }).unwrap(); + } + } +} + +/// Represents a command buffer. +/// +/// Can be submitted to a queue. +pub struct CommandBuffer { + device: Arc, + cmd: ffi::GR_CMD_BUFFER, + memory_refs: Vec, + buffers: Vec>, + images: Vec>, + pipelines: Vec>, +} + +impl Drop for CommandBuffer { + fn drop(&mut self) { + error::check_result(unsafe { ffi::grDestroyObject(cmd) }).unwrap(); + } +} diff --git a/src/device/mod.rs b/src/device/mod.rs new file mode 100644 index 00000000..a2ce2d53 --- /dev/null +++ b/src/device/mod.rs @@ -0,0 +1,28 @@ + +/// Represents a Vulkan context. +pub struct Device { + device: ffi::GR_DEVICE, + queue: ffi::GR_QUEUE, +} + +impl Device { + /// Builds a new Vulkan context for the given GPU. + pub fn new(gpu: &GpuId) -> Arc { + unimplemented!(); + } + + /// Enumerates the list of universal queues. These queues can do anything. + pub fn universal_queues(&self) -> UniversalQueuesIter { + unimplemented!(); + } + + /// Enumerates the list of compute queues. + pub fn compute_queues(&self) -> ComputeQueuesIter { + unimplemented!(); + } + + /// Enumerates the list of DMA queues. DMA queues can only transfer data. + pub fn dma_queues(&self) -> DmaQueueIter { + unimplemented!(); + } +} diff --git a/src/image.rs b/src/image.rs new file mode 100644 index 00000000..e69de29b diff --git a/src/lib.rs b/src/lib.rs index e69de29b..f97fb1a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1,8 @@ + +mod buffer; +mod command_buffer; +mod device; +mod image; + +pub use self::command_buffer::CommandBufferBuilder; +pub use self::command_buffer::CommandBuffer;