Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement some new functions & some minor fixes #265

Merged
merged 4 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 108 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct WGPUBufferImpl {
context: Arc<Context>,
id: id::BufferId,
error_sink: ErrorSink,
descriptor: native::WGPUBufferDescriptor,
}

pub struct WGPUCommandEncoderImpl {
Expand All @@ -103,6 +104,7 @@ pub struct WGPUTextureImpl {
context: Arc<Context>,
id: id::TextureId,
error_sink: ErrorSink,
descriptor: native::WGPUTextureDescriptor,
}

pub struct WGPURenderPassEncoderImpl {
Expand Down Expand Up @@ -415,14 +417,13 @@ pub unsafe extern "C" fn wgpuAdapterGetProperties(
let adapter = adapter.as_mut().expect("invalid adapter");
let properties = properties.expect("invalid return pointer \"properties\"");
let context = &adapter.context;
let id = adapter.id;
let adapter_id = adapter.id;

let maybe_props = gfx_select!(id => context.adapter_get_info(id));
match maybe_props {
match gfx_select!(adapter_id => context.adapter_get_info(adapter_id)) {
Ok(props) => {
adapter.name = CString::new((&props.name) as &str).unwrap();
let driver_desc = format!("{} {}", props.driver, props.driver_info);
adapter.driver_desc = CString::new(driver_desc.trim()).unwrap();
adapter.name = CString::new(props.name).unwrap();
adapter.vendor_name = CString::new(props.driver).unwrap();
adapter.driver_desc = CString::new(props.driver_info).unwrap();

properties.vendorID = props.vendor as u32;
properties.vendorName = adapter.vendor_name.as_ptr();
Expand All @@ -444,7 +445,7 @@ pub unsafe extern "C" fn wgpuAdapterGetProperties(
wgt::Backend::Dx12 => native::WGPUBackendType_D3D12,
wgt::Backend::Dx11 => native::WGPUBackendType_D3D11,
wgt::Backend::Gl => native::WGPUBackendType_OpenGL,
wgt::Backend::BrowserWebGpu => native::WGPUBackendType_OpenGLES, // close enough?
wgt::Backend::BrowserWebGpu => native::WGPUBackendType_WebGPU,
};
}
Err(err) => handle_error_fatal(context, err, "wgpuAdapterGetProperties"),
Expand Down Expand Up @@ -587,6 +588,18 @@ pub unsafe extern "C" fn wgpuBufferGetMappedRange(
buf as *mut ::std::os::raw::c_void
}

#[no_mangle]
pub unsafe extern "C" fn wgpuBufferGetSize(buffer: native::WGPUBuffer) -> u64 {
let descriptor = buffer.as_ref().expect("invalid buffer").descriptor;
descriptor.size
}

#[no_mangle]
pub unsafe extern "C" fn wgpuBufferGetUsage(buffer: native::WGPUBuffer) -> native::WGPUBufferUsage {
let descriptor = buffer.as_ref().expect("invalid buffer").descriptor;
descriptor.usage as _
}

#[no_mangle]
pub unsafe extern "C" fn wgpuBufferMapAsync(
buffer: native::WGPUBuffer,
Expand Down Expand Up @@ -1545,6 +1558,7 @@ pub unsafe extern "C" fn wgpuDeviceCreateBuffer(
context: context.clone(),
id: buffer_id,
error_sink: error_sink.clone(),
descriptor: *descriptor,
}))
}

Expand Down Expand Up @@ -2104,12 +2118,13 @@ pub unsafe extern "C" fn wgpuDeviceCreateTexture(
context: context.clone(),
id: texture_id,
error_sink: error_sink.clone(),
descriptor: *descriptor,
}))
}

#[no_mangle]
pub extern "C" fn wgpuDeviceDestroy(_device: native::WGPUDevice) {
// Empty implementation, maybe call drop?
//TODO: empty implementation, wait till wgpu-core implements a way.
}

#[no_mangle]
Expand Down Expand Up @@ -2377,8 +2392,39 @@ pub unsafe extern "C" fn wgpuInstanceRequestAdapter(
};
}

// QuerySet methods

#[no_mangle]
pub unsafe extern "C" fn wgpuQuerySetDestroy(_query_set: native::WGPUQuerySet) {
//TODO: empty implementation, wait till wgpu-core implements a way.
}

// Queue methods

#[no_mangle]
pub unsafe extern "C" fn wgpuQueueOnSubmittedWorkDone(
queue: native::WGPUQueue,
callback: native::WGPUQueueWorkDoneCallback,
userdata: *mut ::std::os::raw::c_void,
) {
let (queue_id, context) = {
let queue = queue.as_ref().expect("invalid queue");
(queue.id, &queue.context)
};
let callback = callback.expect("invalid callback");
let userdata = utils::Userdata::new(userdata);

let closure = wgc::device::queue::SubmittedWorkDoneClosure::from_rust(Box::new(move || {
callback(native::WGPUQueueWorkDoneStatus_Success, userdata.as_ptr());
}));

if let Err(cause) =
gfx_select!(queue_id => context.queue_on_submitted_work_done(queue_id, closure))
{
handle_error_fatal(context, cause, "wgpuQueueOnSubmittedWorkDone");
};
}

#[no_mangle]
pub unsafe extern "C" fn wgpuQueueSubmit(
queue: native::WGPUQueue,
Expand Down Expand Up @@ -3169,6 +3215,60 @@ pub unsafe extern "C" fn wgpuTextureDestroy(texture: native::WGPUTexture) {
let _ = gfx_select!(texture_id => context.texture_destroy(texture_id));
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetDepthOrArrayLayers(texture: native::WGPUTexture) -> u32 {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.size.depthOrArrayLayers
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetDimension(
texture: native::WGPUTexture,
) -> native::WGPUTextureDimension {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.dimension
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetFormat(
texture: native::WGPUTexture,
) -> native::WGPUTextureFormat {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.format
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetHeight(texture: native::WGPUTexture) -> u32 {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.size.height
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetMipLevelCount(texture: native::WGPUTexture) -> u32 {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.mipLevelCount
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetSampleCount(texture: native::WGPUTexture) -> u32 {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.sampleCount
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetUsage(
texture: native::WGPUTexture,
) -> native::WGPUTextureUsage {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.usage as _
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetWidth(texture: native::WGPUTexture) -> u32 {
let descriptor = texture.as_ref().expect("invalid texture").descriptor;
descriptor.size.width
}

// wgpu.h functions

#[no_mangle]
Expand Down
16 changes: 7 additions & 9 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{map_enum, native};
use log::{Level, LevelFilter, Metadata, Record};
use std::{ffi::CString, sync::Mutex};
use std::{ffi::CString, sync::RwLock};

#[no_mangle]
pub extern "C" fn wgpuGetVersion() -> std::os::raw::c_uint {
Expand Down Expand Up @@ -35,12 +35,9 @@ impl log::Log for Logger {
}

fn log(&self, record: &Record) {
let (callback, userdata) = {
let logger = LOGGER_INFO.lock().unwrap();
(logger.callback, logger.userdata)
};
let logger = LOGGER_INFO.read().unwrap();

if let Some(callback) = callback {
if let Some(callback) = logger.callback {
let msg = record.args().to_string();
let msg_c = CString::new(msg).unwrap();
let level = match record.level() {
Expand All @@ -52,7 +49,7 @@ impl log::Log for Logger {
};

unsafe {
callback(level, msg_c.as_ptr(), userdata);
callback(level, msg_c.as_ptr(), logger.userdata);
}

// We do not use std::mem::forget(msg_c), so Rust will reclaim the memory
Expand All @@ -69,8 +66,9 @@ struct LoggerInfo {
userdata: *mut std::os::raw::c_void,
}
unsafe impl Send for LoggerInfo {}
unsafe impl Sync for LoggerInfo {}

static LOGGER_INFO: Mutex<LoggerInfo> = Mutex::new(LoggerInfo {
static LOGGER_INFO: RwLock<LoggerInfo> = RwLock::new(LoggerInfo {
initialized: false,
callback: None,
userdata: std::ptr::null_mut(),
Expand All @@ -81,7 +79,7 @@ pub extern "C" fn wgpuSetLogCallback(
callback: native::WGPULogCallback,
userdata: *mut std::os::raw::c_void,
) {
let mut logger = LOGGER_INFO.lock().unwrap();
let mut logger = LOGGER_INFO.write().unwrap();
logger.callback = callback;
logger.userdata = userdata;
if !logger.initialized {
Expand Down
66 changes: 0 additions & 66 deletions src/unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ pub extern "C" fn wgpuBufferGetMapState(_buffer: native::WGPUBuffer) -> native::
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuBufferGetSize(_buffer: native::WGPUBuffer) -> u64 {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuBufferGetUsage(_buffer: native::WGPUBuffer) -> native::WGPUBufferUsage {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuBufferSetLabel(
_buffer: native::WGPUBuffer,
Expand Down Expand Up @@ -120,11 +110,6 @@ pub extern "C" fn wgpuPipelineLayoutSetLabel(
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuQuerySetDestroy(_query_set: native::WGPUQuerySet) {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuQuerySetGetCount(_query_set: native::WGPUQuerySet) -> u32 {
unimplemented!();
Expand All @@ -143,15 +128,6 @@ pub extern "C" fn wgpuQuerySetSetLabel(
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuQueueOnSubmittedWorkDone(
_queue: native::WGPUQueue,
_callback: native::WGPUQueueWorkDoneCallback,
_userdata: *mut ::std::os::raw::c_void,
) {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuQueueSetLabel(
_queue: native::WGPUQueue,
Expand Down Expand Up @@ -224,48 +200,6 @@ pub extern "C" fn wgpuShaderModuleSetLabel(
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureGetDepthOrArrayLayers(_texture: native::WGPUTexture) -> u32 {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureGetDimension(
_texture: native::WGPUTexture,
) -> native::WGPUTextureDimension {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureGetFormat(_texture: native::WGPUTexture) -> native::WGPUTextureFormat {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureGetHeight(_texture: native::WGPUTexture) -> u32 {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureGetMipLevelCount(_texture: native::WGPUTexture) -> u32 {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureGetSampleCount(_texture: native::WGPUTexture) -> u32 {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureGetUsage(_texture: native::WGPUTexture) -> native::WGPUTextureUsage {
unimplemented!();
}

#[no_mangle]
pub unsafe extern "C" fn wgpuTextureGetWidth(_texture: native::WGPUTexture) -> u32 {
unimplemented!();
}

#[no_mangle]
pub extern "C" fn wgpuTextureSetLabel(
_texture: native::WGPUTexture,
Expand Down