Skip to content

Commit

Permalink
Cleanup exports
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 19, 2019
1 parent 407cf69 commit 3908d2b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 41 deletions.
5 changes: 2 additions & 3 deletions examples/compute/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
#define BINDINGS_LENGTH (1)
#define BIND_GROUP_LAYOUTS_LENGTH (1)

void request_adapter_callback(WGPUAdapterId const *received, void *userdata) {
WGPUAdapterId *id = (WGPUAdapterId*) userdata;
*id = *received;
void request_adapter_callback(WGPUAdapterId received, void *userdata) {
*(WGPUAdapterId*)userdata = received;
}

void read_buffer_map(
Expand Down
5 changes: 2 additions & 3 deletions examples/triangle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
#define RENDER_PASS_ATTACHMENTS_LENGTH (1)
#define BIND_GROUP_LAYOUTS_LENGTH (1)

void request_adapter_callback(WGPUAdapterId const *received, void *userdata) {
WGPUAdapterId *id = (WGPUAdapterId*) userdata;
*id = *received;
void request_adapter_callback(WGPUAdapterId received, void *userdata) {
*(WGPUAdapterId*)userdata = received;
}

int main() {
Expand Down
2 changes: 1 addition & 1 deletion ffi/wgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ typedef struct {

typedef uint32_t WGPUBackendBit;

typedef void (*WGPURequestAdapterCallback)(const WGPUAdapterId *adapter, void *userdata);
typedef void (*WGPURequestAdapterCallback)(WGPUAdapterId id, void *userdata);

typedef struct {
WGPUTextureViewId view_id;
Expand Down
13 changes: 4 additions & 9 deletions wgpu-core/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,11 @@ impl<B: GfxBackend> Device<B> {
}
};
match operation {
resource::BufferMapOperation::Read(_, on_read, userdata) => {
on_read(status, ptr, userdata)
resource::BufferMapOperation::Read(_, on_read) => {
on_read(status, ptr)
}
resource::BufferMapOperation::Write(_, on_write, userdata) => {
on_write(status, ptr, userdata)
resource::BufferMapOperation::Write(_, on_write) => {
on_write(status, ptr)
}
}
}
Expand Down Expand Up @@ -2000,11 +2000,6 @@ impl<F: AllIdentityFilter + IdentityFilter<DeviceId>> Global<F> {
}
}

pub type BufferMapReadCallback =
extern "C" fn(status: resource::BufferMapAsyncStatus, data: *const u8, userdata: *mut u8);
pub type BufferMapWriteCallback =
extern "C" fn(status: resource::BufferMapAsyncStatus, data: *mut u8, userdata: *mut u8);

impl<F> Global<F> {
pub fn buffer_map_async<B: GfxBackend>(
&self,
Expand Down
3 changes: 0 additions & 3 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use serde::{Deserialize, Serialize};
pub use hal::adapter::AdapterInfo;
use hal::{self, adapter::PhysicalDevice as _, queue::QueueFamily as _, Instance as _};

use std::ffi::c_void;

#[derive(Debug)]
pub struct Instance {
Expand Down Expand Up @@ -153,8 +152,6 @@ pub struct DeviceDescriptor {
pub limits: Limits,
}

pub type RequestAdapterCallback = extern "C" fn(adapter: *const AdapterId, userdata: *mut c_void);

bitflags::bitflags! {
#[repr(transparent)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
27 changes: 18 additions & 9 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::{
device::{BufferMapReadCallback, BufferMapWriteCallback},
id::{DeviceId, SwapChainId, TextureId},
track::DUMMY_SELECTOR,
BufferAddress,
Expand All @@ -17,7 +16,7 @@ use hal;
use rendy_memory::MemoryBlock;
use smallvec::SmallVec;

use std::borrow::Borrow;
use std::{borrow::Borrow, fmt};

bitflags::bitflags! {
#[repr(transparent)]
Expand Down Expand Up @@ -62,25 +61,35 @@ pub enum BufferMapAsyncStatus {
ContextLost,
}

#[derive(Clone, Debug)]
pub enum BufferMapOperation {
Read(std::ops::Range<u64>, BufferMapReadCallback, *mut u8),
Write(std::ops::Range<u64>, BufferMapWriteCallback, *mut u8),
Read(std::ops::Range<u64>, Box<dyn FnOnce(BufferMapAsyncStatus, *const u8)>),
Write(std::ops::Range<u64>, Box<dyn FnOnce(BufferMapAsyncStatus, *mut u8)>),
}

//TODO: clarify if/why this is needed here
unsafe impl Send for BufferMapOperation {}
unsafe impl Sync for BufferMapOperation {}

impl fmt::Debug for BufferMapOperation {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let (op, range) = match *self {
BufferMapOperation::Read(ref range, _) => ("read", range),
BufferMapOperation::Write(ref range, _) => ("write", range),
};
write!(fmt, "BufferMapOperation <{}> of range {:?}", op, range)
}
}

impl BufferMapOperation {
pub(crate) fn call_error(self) {
match self {
BufferMapOperation::Read(_, callback, userdata) => {
BufferMapOperation::Read(_, callback) => {
log::error!("wgpu_buffer_map_read_async failed: buffer mapping is pending");
callback(BufferMapAsyncStatus::Error, std::ptr::null_mut(), userdata);
callback(BufferMapAsyncStatus::Error, std::ptr::null());
}
BufferMapOperation::Write(_, callback, userdata) => {
BufferMapOperation::Write(_, callback) => {
log::error!("wgpu_buffer_map_write_async failed: buffer mapping is pending");
callback(BufferMapAsyncStatus::Error, std::ptr::null_mut(), userdata);
callback(BufferMapAsyncStatus::Error, std::ptr::null_mut());
}
}
}
Expand Down
38 changes: 27 additions & 11 deletions wgpu-native/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use core::{gfx_select, hub::Token, id};

use std::{marker::PhantomData, slice};

pub type RequestAdapterCallback =
unsafe extern "C" fn(id: id::AdapterId, userdata: *mut std::ffi::c_void);
pub type BufferMapReadCallback =
unsafe extern "C" fn(status: core::resource::BufferMapAsyncStatus, data: *const u8, userdata: *mut u8);
pub type BufferMapWriteCallback =
unsafe extern "C" fn(status: core::resource::BufferMapAsyncStatus, data: *mut u8, userdata: *mut u8);

pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> id::SurfaceId {
use raw_window_handle::RawWindowHandle as Rwh;
Expand Down Expand Up @@ -116,17 +122,19 @@ pub extern "C" fn wgpu_create_surface_from_windows_hwnd(
pub extern "C" fn wgpu_request_adapter_async(
desc: Option<&core::instance::RequestAdapterOptions>,
mask: core::instance::BackendBit,
callback: core::instance::RequestAdapterCallback,
callback: RequestAdapterCallback,
userdata: *mut std::ffi::c_void,
) {
let id = GLOBAL.pick_adapter(
&desc.cloned().unwrap_or_default(),
core::instance::AdapterInputs::Mask(mask, || PhantomData),
);
callback(
id.as_ref().map_or(&id::AdapterId::ERROR, |x| x as *const _),
userdata,
);
unsafe {
callback(
id.unwrap_or(id::AdapterId::ERROR),
userdata,
)
};
}

#[no_mangle]
Expand Down Expand Up @@ -313,11 +321,15 @@ pub extern "C" fn wgpu_buffer_map_read_async(
buffer_id: id::BufferId,
start: core::BufferAddress,
size: core::BufferAddress,
callback: core::device::BufferMapReadCallback,
callback: BufferMapReadCallback,
userdata: *mut u8,
) {
let operation =
core::resource::BufferMapOperation::Read(start .. start + size, callback, userdata);
let operation = core::resource::BufferMapOperation::Read(
start .. start + size,
Box::new(move |status, data| unsafe {
callback(status, data, userdata)
}),
);
gfx_select!(buffer_id => GLOBAL.buffer_map_async(buffer_id, core::resource::BufferUsage::MAP_READ, operation))
}

Expand All @@ -326,11 +338,15 @@ pub extern "C" fn wgpu_buffer_map_write_async(
buffer_id: id::BufferId,
start: core::BufferAddress,
size: core::BufferAddress,
callback: core::device::BufferMapWriteCallback,
callback: BufferMapWriteCallback,
userdata: *mut u8,
) {
let operation =
core::resource::BufferMapOperation::Write(start .. start + size, callback, userdata);
let operation = core::resource::BufferMapOperation::Write(
start .. start + size,
Box::new(move |status, data| unsafe {
callback(status, data, userdata)
}),
);
gfx_select!(buffer_id => GLOBAL.buffer_map_async(buffer_id, core::resource::BufferUsage::MAP_WRITE, operation))
}

Expand Down
7 changes: 5 additions & 2 deletions wgpu-native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::sync::Arc;

pub mod command;
pub mod device;
mod command;
mod device;

pub use self::command::*;
pub use self::device::*;

type Global = core::hub::Global<parking_lot::Mutex<core::hub::IdentityManager>>;

Expand Down

0 comments on commit 3908d2b

Please sign in to comment.