First bits of the API with extern C functions

This commit is contained in:
Dzmitry Malyshau 2018-09-13 15:19:16 -04:00
parent 0597949ff5
commit 53aabad8b4
7 changed files with 283 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target
**/*.rs.bk
Cargo.lock

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "gpu"
version = "0.1.0"
authors = ["Dzmitry Malyshau <kvark@mozilla.com>"]
[features]
default = ["gfx-backend-vulkan"]
[dependencies]
gfx-hal = { git = "https://github.com/gfx-rs/gfx" }
gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", optional = true }

13
src/command.rs Normal file
View File

@ -0,0 +1,13 @@
use hal;
pub struct CommandBuffer<B: hal::Backend> {
raw: B::CommandBuffer,
}
pub struct RenderPass<B: hal::Backend> {
raw: B::CommandBuffer,
}
pub struct ComputePass<B: hal::Backend> {
raw: B::CommandBuffer,
}

22
src/device.rs Normal file
View File

@ -0,0 +1,22 @@
use hal;
pub type BufferUsage = hal::buffer::Usage;
#[repr(C)]
pub struct BufferDescriptor {
pub size: u64,
pub usage: BufferUsage,
}
#[repr(C)]
pub struct CommandBufferDescriptor {
}
pub struct Device<B: hal::Backend> {
pub raw: B::Device,
}
pub struct Buffer<B: hal::Backend> {
pub raw: B::Buffer,
}

113
src/handle.rs Normal file
View File

@ -0,0 +1,113 @@
use std::{borrow, cmp, fmt, ops, ptr};
#[cfg(feature = "nightly")]
use std::sync::{Arc, Mutex};
#[cfg(feature = "nightly")]
use hal::backend::FastHashMap;
#[cfg(feature = "nightly")]
lazy_static! {
static ref REGISTRY: Arc<Mutex<FastHashMap<usize, &'static str>>> = Arc::new(Mutex::new(FastHashMap::default()));
}
#[repr(C)]
pub struct Handle<T>(*mut T);
#[cfg(feature = "nightly")]
impl Handle<()> {
pub fn report_leaks() {
println!("Leaked handles:");
let mut map = REGISTRY.lock().unwrap();
for (_, type_id) in map.drain() {
println!("\t{:?}", type_id);
}
}
}
impl<T: 'static> Handle<T> {
pub fn new(value: T) -> Self {
let ptr = Box::into_raw(Box::new(value));
#[cfg(feature = "nightly")]
{
use std::intrinsics::type_name;
let name = unsafe { type_name::<T>() };
REGISTRY.lock().unwrap().insert(ptr as _, name);
}
Handle(ptr)
}
pub fn null() -> Self {
Handle(ptr::null_mut())
}
pub fn unbox(self) -> Option<T> {
if self.0.is_null() {
None
} else {
#[cfg(feature = "nightly")]
{
REGISTRY.lock().unwrap().remove(&(self.0 as _)).unwrap();
}
Some(*unsafe { Box::from_raw(self.0) })
}
}
pub fn as_ref(&self) -> Option<&T> {
unsafe { self.0.as_ref() }
}
}
impl<T> Handle<T> {
#[cfg(feature = "nightly")]
#[inline]
fn check(&self) {
assert!(REGISTRY.lock().unwrap().contains_key(&(self.0 as _)));
}
#[cfg(not(feature = "nightly"))]
#[inline]
fn check(&self) {
debug_assert!(!self.0.is_null());
}
}
impl<T> Clone for Handle<T> {
fn clone(&self) -> Self {
Handle(self.0)
}
}
impl<T> Copy for Handle<T> {}
impl<T> ops::Deref for Handle<T> {
type Target = T;
fn deref(&self) -> &T {
self.check();
unsafe { &*self.0 }
}
}
impl<T> ops::DerefMut for Handle<T> {
fn deref_mut(&mut self) -> &mut T {
self.check();
unsafe { &mut *self.0 }
}
}
impl<T> borrow::Borrow<T> for Handle<T> {
fn borrow(&self) -> &T {
self.check();
unsafe { &*self.0 }
}
}
impl<T> cmp::PartialEq for Handle<T> {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<T> fmt::Debug for Handle<T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Handle({:p})", self.0)
}
}

23
src/instance.rs Normal file
View File

@ -0,0 +1,23 @@
//use hal;
#[repr(C)]
pub enum PowerPreference {
Default = 0,
LowPower = 1,
HighPerformance = 2,
}
#[repr(C)]
pub struct AdapterDescriptor {
pub power_preference: PowerPreference,
}
#[repr(C)]
pub struct Extensions {
anisotropicFiltering: bool,
}
#[repr(C)]
pub struct DeviceDescriptor {
pub extension: Extensions,
}

97
src/lib.rs Normal file
View File

@ -0,0 +1,97 @@
extern crate gfx_hal as hal;
#[cfg(feature = "gfx-backend-vulkan")]
extern crate gfx_backend_vulkan as back;
mod command;
mod device;
mod handle;
mod instance;
use back::Backend as B;
use hal::Device;
use handle::Handle;
pub type InstanceHandle = Handle<back::Instance>;
pub type AdapterHandle = Handle<hal::Adapter<B>>;
pub type DeviceHandle = Handle<device::Device<B>>;
pub type BufferHandle = Handle<device::Buffer<B>>;
pub type CommandBufferHandle = Handle<command::CommandBuffer<B>>;
pub type RenderPassHandle = Handle<command::RenderPass<B>>;
pub type ComputePassHandle = Handle<command::ComputePass<B>>;
// Instance logic
pub extern "C"
fn create_instance() -> InstanceHandle {
unimplemented!()
}
pub extern "C"
fn instance_get_adapter(
instance: InstanceHandle, desc: instance::AdapterDescriptor
) -> AdapterHandle {
unimplemented!()
}
pub extern "C"
fn adapter_create_device(
adapter: AdapterHandle, desc: instance::DeviceDescriptor
) -> DeviceHandle {
unimplemented!()
}
// Device logic
pub extern "C"
fn device_create_buffer(
device: DeviceHandle, desc: device::BufferDescriptor
) -> BufferHandle {
//let unbound = device.raw.create_buffer(desc.size, desc.usage).unwrap();
//let reqs = device.raw.get_buffer_requirements(&unbound);
unimplemented!()
}
pub extern "C"
fn device_create_command_buffer(
device: DeviceHandle, desc: device::CommandBufferDescriptor
) -> CommandBufferHandle {
unimplemented!()
}
// Command Buffer logic
pub extern "C"
fn command_buffer_begin_render_pass(
command_buffer: CommandBufferHandle
) -> RenderPassHandle {
unimplemented!()
}
pub extern "C"
fn command_buffer_begin_compute_pass(
) -> ComputePassHandle {
unimplemented!()
}
// Render Pass logic
pub extern "C"
fn render_pass_draw(
pass: RenderPassHandle, vertex_count: u32, instance_count: u32, first_vertex: u32, first_instance: u32
) {
unimplemented!()
}
pub extern "C"
fn render_pass_draw_indexed(
pass: RenderPassHandle, index_count: u32, instance_count: u32, first_index: u32, vertex_offset: i32, first_instance: u32
) {
unimplemented!()
}
pub extern "C"
fn render_pass_end(pass: RenderPassHandle) -> CommandBufferHandle {
unimplemented!()
}