diff --git a/src/conv.rs b/src/conv.rs new file mode 100644 index 000000000..f19f0b24c --- /dev/null +++ b/src/conv.rs @@ -0,0 +1,40 @@ +use hal; + +use resource; + + +pub(crate) fn map_buffer_usage(usage: resource::BufferUsageFlags) -> (hal::buffer::Usage, hal::memory::Properties) { + use resource::BufferUsageFlags as W; + use hal::buffer::Usage as U; + use hal::memory::Properties as P; + + let mut hal_memory = P::empty(); + if usage.contains(W::MAP_READ) { + hal_memory |= P::CPU_VISIBLE | P::CPU_CACHED; + } + if usage.contains(W::MAP_WRITE) { + hal_memory |= P::CPU_VISIBLE; + } + + let mut hal_usage = U::empty(); + if usage.contains(W::TRANSFER_SRC) { + hal_usage |= U::TRANSFER_SRC; + } + if usage.contains(W::TRANSFER_DST) { + hal_usage |= U::TRANSFER_DST; + } + if usage.contains(W::INDEX) { + hal_usage |= U::INDEX; + } + if usage.contains(W::VERTEX) { + hal_usage |= U::VERTEX; + } + if usage.contains(W::UNIFORM) { + hal_usage |= U::UNIFORM; + } + if usage.contains(W::STORAGE) { + hal_usage |= U::STORAGE; + } + + (hal_usage, hal_memory) +} \ No newline at end of file diff --git a/src/device.rs b/src/device.rs index 4a3928550..bb4c310af 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,17 +1,9 @@ use hal::{self, Device as _Device, QueueGroup}; -use {memory, pipeline}; +use {conv, memory, pipeline, resource}; use {BufferHandle, CommandBufferHandle, DeviceHandle, ShaderModuleHandle}; -pub type BufferUsage = hal::buffer::Usage; - -#[repr(C)] -pub struct BufferDescriptor { - pub size: u64, - pub usage: BufferUsage, -} - #[repr(C)] pub struct CommandBufferDescriptor { } @@ -36,17 +28,15 @@ impl Device { } } -pub struct Buffer { - pub raw: B::UnboundBuffer, -} - pub extern "C" fn device_create_buffer( - device: DeviceHandle, desc: BufferDescriptor + device: DeviceHandle, desc: resource::BufferDescriptor ) -> BufferHandle { - let buffer = device.device.create_buffer(desc.size, desc.usage).unwrap(); - BufferHandle::new(Buffer { + let (usage, memory_properties) = conv::map_buffer_usage(desc.usage); + let buffer = device.device.create_buffer(desc.size as u64, usage).unwrap(); + BufferHandle::new(resource::Buffer { raw: buffer, + memory_properties, }) } diff --git a/src/lib.rs b/src/lib.rs index 558ef747f..3772d2fd8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ extern crate gfx_hal as hal; extern crate gfx_memory as memory; mod binding_model; +mod conv; mod command; mod device; mod handle; @@ -30,6 +31,28 @@ pub use self::resource::*; use back::Backend as B; use handle::Handle; +#[repr(C)] +pub struct Color { + pub r: f32, + pub g: f32, + pub b: f32, + pub a: f32, +} + +#[repr(C)] +pub struct Origin3d { + pub x: f32, + pub y: f32, + pub z: f32, +} + +#[repr(C)] +pub struct Extent3d { + pub width: f32, + pub height: f32, + pub depth: f32, +} + pub type InstanceHandle = Handle; pub type AdapterHandle = Handle>; pub type DeviceHandle = Handle>; diff --git a/src/pipeline.rs b/src/pipeline.rs index 8cedf24d5..976c93f22 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -33,12 +33,12 @@ pub enum BlendOperation { bitflags! { #[repr(transparent)] pub struct ColorWriteFlags: u32 { - const NONE = 0x0; - const RED = 0x1; - const GREEN = 0x2; - const BLUE = 0x4; - const ALPHA = 0x8; - const ALL = 0x15; + const NONE = 0; + const RED = 1; + const GREEN = 2; + const BLUE = 4; + const ALPHA = 8; + const ALL = 15; } } diff --git a/src/resource.rs b/src/resource.rs index 0a9e80e62..defc23431 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -1,3 +1,46 @@ +use hal; + +use {Extent3d}; + + +bitflags! { + #[repr(transparent)] + pub struct BufferUsageFlags: u32 { + const NONE = 0; + const MAP_READ = 1; + const MAP_WRITE = 2; + const TRANSFER_SRC = 4; + const TRANSFER_DST = 8; + const INDEX = 16; + const VERTEX = 32; + const UNIFORM = 64; + const STORAGE = 128; + } +} + +#[repr(C)] +pub struct BufferDescriptor { + pub size: u32, + pub usage: BufferUsageFlags, +} + +pub struct Buffer { + pub(crate) raw: B::UnboundBuffer, + pub(crate) memory_properties: hal::memory::Properties, + // TODO: mapping, unmap() +} + +pub struct TextureView { + // TODO +} + +#[repr(C)] +pub enum TextureDimension { + D1, + D2, + D3, +} + #[repr(C)] pub enum TextureFormat { R8g8b8a8Unorm = 0, @@ -6,6 +49,46 @@ pub enum TextureFormat { D32FloatS8Uint = 3, } +bitflags! { + #[repr(transparent)] + pub struct TextureUsageFlags: u32 { + const NONE = 0; + const TRANSFER_SRC = 1; + const TRANSFER_DST = 2; + const SAMPLED = 4; + const STORAGE = 8; + const OUTPUT_ATTACHMENT = 16; + const PRESENT = 32; + } +} + +#[repr(C)] +pub struct TextureDescriptor { + pub size: Extent3d, + pub array_size: u32, + pub dimension: TextureDimension, + pub format: TextureFormat, + pub usage: TextureUsageFlags, +} + +pub struct Texture { + // TODO: create_texture_view() +} + +#[repr(C)] +pub enum AddressMode { + ClampToEdge = 0, + Repeat = 1, + MirrorRepeat = 2, + ClampToBorderColor = 3, +} + +#[repr(C)] +pub enum FilterMode { + Nearest = 0, + Linear = 1, +} + #[repr(C)] pub enum CompareFunction { Never = 0, @@ -17,3 +100,29 @@ pub enum CompareFunction { GreaterEqual = 6, Always = 7, } + +#[repr(C)] +pub enum BorderColor { + TransparentBlack = 0, + OpaqueBlack = 1, + OpaqueWhite = 2, +} + +#[repr(C)] +pub struct SamplerDescriptor { + pub r_address_mode: AddressMode, + pub s_address_mode: AddressMode, + pub t_address_mode: AddressMode, + pub mag_filter: FilterMode, + pub min_filter: FilterMode, + pub mipmap_filer: FilterMode, + pub lod_min_clamp: f32, + pub lod_max_clamp: f32, + pub max_anisotropy: u32, + pub compare_function: CompareFunction, + pub border_color: BorderColor, +} + +pub struct Sampler { + // TODO +}