Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jul 18, 2024
1 parent 06bfc06 commit c11f2d7
Show file tree
Hide file tree
Showing 127 changed files with 12,222 additions and 5,181 deletions.
10 changes: 4 additions & 6 deletions crates/canvas-c/src/webgpu/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{

use wgpu_core::binding_model::{BindGroupEntry, BufferBinding};
use wgpu_types::{AddressMode, BindGroupLayoutEntry, BufferBindingType, CompareFunction, Features, FilterMode, QueryType, SamplerBindingType, StorageTextureAccess, TextureSampleType};
use wgpu_types::BindingType::Sampler;

use crate::webgpu::gpu_buffer::CanvasGPUBuffer;
use crate::webgpu::gpu_sampler::CanvasGPUSampler;
Expand Down Expand Up @@ -600,12 +599,10 @@ impl Into<wgpu_types::TextureFormat> for CanvasGPUTextureFormat {
CanvasGPUTextureFormat::Rg16Unorm => wgpu_types::TextureFormat::Rg16Unorm,
CanvasGPUTextureFormat::Rg16Snorm => wgpu_types::TextureFormat::Rg16Snorm,
CanvasGPUTextureFormat::Rg16Float => wgpu_types::TextureFormat::Rg16Float,
CanvasGPUTextureFormat::Rgba8Unorm => wgpu_types::TextureFormat::Rgba8Unorm,
CanvasGPUTextureFormat::Rgba8UnormSrgb => wgpu_types::TextureFormat::Rgba8UnormSrgb,
CanvasGPUTextureFormat::Rgba8Snorm => wgpu_types::TextureFormat::Rgba8Snorm,
CanvasGPUTextureFormat::Rgba8Uint => wgpu_types::TextureFormat::Rgba8Uint,
CanvasGPUTextureFormat::Rgba8Sint => wgpu_types::TextureFormat::Rgba8Sint,
CanvasGPUTextureFormat::Bgra8Unorm => wgpu_types::TextureFormat::Bgra8Unorm,
CanvasGPUTextureFormat::Bgra8UnormSrgb => wgpu_types::TextureFormat::Bgra8UnormSrgb,
CanvasGPUTextureFormat::Rgb9e5Ufloat => wgpu_types::TextureFormat::Rgb9e5Ufloat,
CanvasGPUTextureFormat::Rgb10a2Uint => wgpu_types::TextureFormat::Rgb10a2Uint,
Expand Down Expand Up @@ -820,7 +817,7 @@ pub enum CanvasOptionalGPUTextureFormat {
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_string_to_gpu_texture_enum(
pub unsafe extern "C" fn canvas_native_webgpu_enum_string_to_gpu_texture(
format: *const c_char,
) -> CanvasOptionalGPUTextureFormat {
let format = CStr::from_ptr(format);
Expand Down Expand Up @@ -2029,8 +2026,8 @@ impl Into<BindGroupLayoutEntry> for CanvasBindGroupLayoutEntry {
.map(|value: u64| NonZeroU64::new(value))
.ok()
.flatten()


,
}
}
Expand Down Expand Up @@ -2059,6 +2056,7 @@ impl Into<BindGroupLayoutEntry> for CanvasBindGroupLayoutEntry {


#[repr(C)]
#[derive(Copy, Clone, PartialOrd, PartialEq)]
pub enum CanvasQueryType {
Occlusion,
Timestamp,
Expand Down
10 changes: 9 additions & 1 deletion crates/canvas-c/src/webgpu/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ pub(crate) fn handle_error(
label: wgpu_core::Label,
string: &'static str,
) {
let cause_error = cause.to_string();
let error = wgpu_core::error::ContextError {
string,
cause: Box::new(cause),
label: label.unwrap_or_default().to_string(),
label_key,
};


let mut sink = sink_mutex.lock();
let mut source_opt: Option<&(dyn Error + 'static)> = Some(&error);
while let Some(source) = source_opt {
Expand All @@ -52,7 +55,7 @@ pub(crate) fn handle_error(
}

// Otherwise, it is a validation error
sink.handle_error(CanvasGPUError::Validation(error.to_string()));
sink.handle_error(CanvasGPUError::Validation(cause_error));
}

pub(crate) fn handle_error_fatal(
Expand All @@ -64,6 +67,11 @@ pub(crate) fn handle_error_fatal(
// "Error in {operation}: {f}",
// f = format_error(context, &cause)
// );
let error = cause.to_string();

// log::error!("Error in {operation}: {f}",
// f = error);

log::error!("Error in {operation}: {f}",
f = format_error(global, &cause))
}
Expand Down
42 changes: 38 additions & 4 deletions crates/canvas-c/src/webgpu/gpu_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ pub struct CanvasGPUAdapter {
pub(crate) limits: wgpu_types::Limits,
}

impl Drop for CanvasGPUAdapter {
fn drop(&mut self) {
if !std::thread::panicking() {
let global = self.instance.global();
gfx_select!(self.id => global.adapter_drop(self.adapter));
}
}
}


#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_adapter_reference(
adapter: *const CanvasGPUAdapter
) {
if adapter.is_null() {
return;
}

Arc::increment_strong_count(adapter);
}


#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_adapter_release(
adapter: *const CanvasGPUAdapter
) {
if adapter.is_null() {
return;
}

Arc::decrement_strong_count(adapter);
}


#[no_mangle]
pub extern "C" fn canvas_native_webgpu_adapter_get_features(
adapter: *const CanvasGPUAdapter,
Expand Down Expand Up @@ -56,13 +90,13 @@ pub extern "C" fn canvas_native_webgpu_adapter_get_limits(
}

#[no_mangle]
pub extern "C" fn canvas_native_webgpu_request_adapter_info(
adapter: *mut CanvasGPUAdapter,
pub extern "C" fn canvas_native_webgpu_adapter_request_adapter_info(
adapter: *const CanvasGPUAdapter,
) -> *const CanvasGPUAdapterInfo {
if adapter.is_null() {
return std::ptr::null_mut();
}
let adapter = unsafe { &mut *adapter };
let adapter = unsafe { &*adapter };

let adapter_id = adapter.adapter;
let global = adapter.instance.global();
Expand All @@ -74,7 +108,7 @@ pub extern "C" fn canvas_native_webgpu_request_adapter_info(
}

#[no_mangle]
pub extern "C" fn canvas_native_webgpu_request_device(
pub extern "C" fn canvas_native_webgpu_adapter_request_device(
adapter: *const CanvasGPUAdapter,
label: *const c_char,
required_features: *const *const c_char,
Expand Down
2 changes: 1 addition & 1 deletion crates/canvas-c/src/webgpu/gpu_bind_group.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::sync::Arc;
use crate::webgpu::gpu_bind_group_layout::CanvasGPUBindGroupLayout;

use super::gpu::CanvasWebGPUInstance;

pub struct CanvasGPUBindGroup {
Expand Down
48 changes: 39 additions & 9 deletions crates/canvas-c/src/webgpu/gpu_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ pub struct CanvasGPUBuffer {
pub(crate) error_sink: super::gpu_device::ErrorSink,
}

impl Drop for CanvasGPUBuffer {
fn drop(&mut self) {
if !std::thread::panicking() {
let global = self.instance.global();
gfx_select!(self.id => global.buffer_drop(self.buffer, false));
}
}
}


impl CanvasGPUBuffer {
pub fn size(&self) -> u64 {
self.size
Expand All @@ -65,6 +75,28 @@ impl CanvasGPUBuffer {
}
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_buffer_reference(
buffer: *const CanvasGPUBuffer
) {
if buffer.is_null() {
return;
}

Arc::increment_strong_count(buffer);
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_buffer_release(
buffer: *const CanvasGPUBuffer
) {
if buffer.is_null() {
return;
}

Arc::decrement_strong_count(buffer);
}

#[no_mangle]
pub extern "C" fn canvas_native_webgpu_buffer_usage(buffer: *const CanvasGPUBuffer) -> u32 {
if buffer.is_null() {
Expand All @@ -88,11 +120,9 @@ pub unsafe extern "C" fn canvas_native_webgpu_buffer_get_mapped_range(
buffer: *const CanvasGPUBuffer,
offset: i64,
size: i64,
dst: *mut u8,
dst_size: usize,
) {
) -> *mut c_void {
if buffer.is_null() {
return;
return std::ptr::null_mut();
}
let buffer = unsafe { &*buffer };
let offset: u64 = offset.try_into().ok().unwrap_or_default();
Expand All @@ -103,14 +133,14 @@ pub unsafe extern "C" fn canvas_native_webgpu_buffer_get_mapped_range(

let range = gfx_select!( buffer_id => global.buffer_get_mapped_range(buffer_id, offset, size));


match range {
Ok((buf, len)) => {
let src = std::slice::from_raw_parts(buf, len as usize);
let dst = std::slice::from_raw_parts_mut(dst, dst_size);
dst.copy_from_slice(src);
Ok((buf, _)) => {
buf as *mut c_void
}
Err(err) => {
handle_error_fatal(global, err, "canvas_native_webgpu_buffer_get_mapped_range")
handle_error_fatal(global, err, "canvas_native_webgpu_buffer_get_mapped_range");
std::ptr::null_mut()
}
}
}
Expand Down
Loading

0 comments on commit c11f2d7

Please sign in to comment.