Basic buffer creation

This commit is contained in:
Dzmitry Malyshau 2019-01-21 06:51:30 -05:00
parent 4f939bc32f
commit 32f7ae4f28
4 changed files with 103 additions and 21 deletions

View File

@ -107,10 +107,6 @@ impl<B: hal::Backend> CommandAllocator<B> {
self.inner.lock().pending.push(cmd_buf);
}
pub fn recycle(&self, cmd_buf: CommandBuffer<B>) {
self.inner.lock().recycle(cmd_buf);
}
pub fn maintain(&self, last_done: SubmissionIndex) {
let mut inner = self.inner.lock();
for i in (0..inner.pending.len()).rev() {

View File

@ -155,7 +155,6 @@ pub struct Device<B: hal::Backend> {
pub(crate) render_passes: Mutex<HashMap<RenderPassKey, B::RenderPass>>,
pub(crate) framebuffers: Mutex<HashMap<FramebufferKey, B::Framebuffer>>,
desc_pool: Mutex<B::DescriptorPool>,
last_submission_index: SubmissionIndex,
destroyed: Mutex<DestroyedResources<B>>,
}
@ -227,7 +226,6 @@ impl<B: hal::Backend> Device<B> {
render_passes: Mutex::new(HashMap::new()),
framebuffers: Mutex::new(HashMap::new()),
desc_pool,
last_submission_index: 0,
destroyed: Mutex::new(DestroyedResources {
referenced: Vec::new(),
active: Vec::new(),
@ -241,6 +239,70 @@ pub(crate) struct ShaderModule<B: hal::Backend> {
pub raw: B::ShaderModule,
}
#[no_mangle]
pub extern "C" fn wgpu_device_create_buffer(
device_id: DeviceId,
desc: &resource::BufferDescriptor,
) -> BufferId {
let device_guard = HUB.devices.read();
let device = &device_guard.get(device_id);
let (usage, _) = conv::map_buffer_usage(desc.usage);
let mut buffer = unsafe {
device.raw.create_buffer(desc.size as u64, usage).unwrap()
};
let requirements = unsafe {
device.raw.get_buffer_requirements(&buffer)
};
let device_type = device
.mem_props
.memory_types
.iter()
.enumerate()
.position(|(id, memory_type)| {
// TODO
requirements.type_mask & (1 << id) != 0
&& memory_type
.properties
.contains(hal::memory::Properties::DEVICE_LOCAL)
})
.unwrap()
.into();
// TODO: allocate with rendy
let memory = unsafe {
device.raw
.allocate_memory(device_type, requirements.size)
.unwrap()
};
unsafe {
device.raw
.bind_buffer_memory(&memory, 0, &mut buffer)
.unwrap()
};
let life_guard = LifeGuard::new();
let ref_count = life_guard.ref_count.clone();
let id = HUB.buffers
.write()
.register(resource::Buffer {
raw: buffer,
device_id: Stored {
value: device_id,
ref_count: device.life_guard.ref_count.clone(),
},
life_guard,
});
let query = device.buffer_tracker
.lock()
.query(
&Stored { value: id, ref_count },
resource::BufferUsageFlags::WRITE_ALL,
);
assert!(query.initialized);
id
}
#[no_mangle]
pub extern "C" fn wgpu_device_create_texture(
device_id: DeviceId,
@ -252,7 +314,8 @@ pub extern "C" fn wgpu_device_create_texture(
let usage = conv::map_texture_usage(desc.usage, aspects);
let device_guard = HUB.devices.read();
let device = &device_guard.get(device_id);
let mut image_unbound = unsafe {
let mut image = unsafe {
device.raw.create_image(
kind,
1, // TODO: mips
@ -263,7 +326,9 @@ pub extern "C" fn wgpu_device_create_texture(
)
}
.unwrap();
let image_req = unsafe { device.raw.get_image_requirements(&image_unbound) };
let requirements = unsafe {
device.raw.get_image_requirements(&image)
};
let device_type = device
.mem_props
.memory_types
@ -271,7 +336,7 @@ pub extern "C" fn wgpu_device_create_texture(
.enumerate()
.position(|(id, memory_type)| {
// TODO
image_req.type_mask & (1 << id) != 0
requirements.type_mask & (1 << id) != 0
&& memory_type
.properties
.contains(hal::memory::Properties::DEVICE_LOCAL)
@ -279,14 +344,16 @@ pub extern "C" fn wgpu_device_create_texture(
.unwrap()
.into();
// TODO: allocate with rendy
let image_memory = unsafe { device.raw.allocate_memory(device_type, image_req.size) }.unwrap();
let memory = unsafe {
device.raw
.allocate_memory(device_type, requirements.size)
.unwrap()
};
unsafe {
device
.raw
.bind_image_memory(&image_memory, 0, &mut image_unbound)
}
.unwrap();
let bound_image = image_unbound; //TODO: Maybe call this image the same way in the first place
device.raw
.bind_image_memory(&memory, 0, &mut image)
.unwrap()
};
let full_range = hal::image::SubresourceRange {
aspects,
@ -299,7 +366,7 @@ pub extern "C" fn wgpu_device_create_texture(
let id = HUB.textures
.write()
.register(resource::Texture {
raw: bound_image,
raw: image,
device_id: Stored {
value: device_id,
ref_count: device.life_guard.ref_count.clone(),

View File

@ -35,7 +35,8 @@ pub struct BufferDescriptor {
pub(crate) struct Buffer<B: hal::Backend> {
pub raw: B::Buffer,
pub memory_properties: hal::memory::Properties,
pub device_id: Stored<DeviceId>,
//pub memory_properties: hal::memory::Properties,
pub life_guard: LifeGuard,
// TODO: mapping, unmap()
}

View File

@ -14,7 +14,7 @@ pub use wgn::{
RenderPassColorAttachmentDescriptor, RenderPassDepthStencilAttachmentDescriptor,
ShaderModuleDescriptor, ShaderStage, ShaderStageFlags, StoreOp,
TextureDescriptor, TextureDimension, TextureFormat, TextureUsageFlags, TextureViewDescriptor,
SwapChainDescriptor,
BufferDescriptor, SwapChainDescriptor,
};
pub struct Instance {
@ -29,6 +29,10 @@ pub struct Device {
id: wgn::DeviceId,
}
pub struct Buffer {
id: wgn::BufferId,
}
pub struct Texture {
id: wgn::TextureId,
}
@ -273,9 +277,15 @@ impl Device {
}
}
pub fn create_buffer(&self, desc: &BufferDescriptor) -> Buffer {
Buffer {
id: wgn::wgpu_device_create_buffer(self.id, desc),
}
}
pub fn create_texture(&self, desc: &TextureDescriptor) -> Texture {
Texture {
id: wgn::wgpu_device_create_texture(self.id, &desc),
id: wgn::wgpu_device_create_texture(self.id, desc),
}
}
@ -289,7 +299,7 @@ impl Device {
impl Texture {
pub fn create_texture_view(&self, desc: &TextureViewDescriptor) -> TextureView {
TextureView {
id: wgn::wgpu_texture_create_texture_view(self.id, &desc),
id: wgn::wgpu_texture_create_texture_view(self.id, desc),
}
}
@ -355,6 +365,14 @@ impl<'a> RenderPass<'a> {
self.parent
}
pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup) {
wgn::wgpu_render_pass_set_bind_group(self.id, index, bind_group.id);
}
pub fn set_pipeline(&mut self, pipeline: &RenderPipeline) {
wgn::wgpu_render_pass_set_pipeline(self.id, pipeline.id);
}
pub fn draw(
&self, vertices: Range<u32>, instances: Range<u32>
) {