Skip to content

Commit

Permalink
chore: webgpu.11
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Aug 16, 2024
1 parent 42b94ea commit 44bfb6b
Show file tree
Hide file tree
Showing 151 changed files with 7,471 additions and 5,616 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# . "$(dirname "$0")/_/husky.sh"

npx lint-staged --allow-empty
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ strip = true

[workspace.dependencies.wgt]
package = "wgpu-types"
version = "22.0.0"
git = "https://github.com/gfx-rs/wgpu"
rev = "c72bc7b84b094f70cc47dd8a1aa8175e3f3dc5b6"

[workspace.dependencies]
wgpu-types = "22.0.0"
parking_lot = "0.12.3"
raw-window-handle = "0.6.2"
canvas-2d = { path = "./crates/canvas-2d"}
canvas-2d = { path = "./crates/canvas-2d" }
canvas-core = { path = "./crates/canvas-core" }
canvas-webgl = { path = "./crates/canvas-webgl" }
gl-bindings = { path = "./crates/gl-bindings" }
canvas-c = { path = "./crates/canvas-c" }
skia-safe = { version = "0.73.0", features = ["textlayout"] }
itertools = "0.13.0"
wgpu-core = { version = "22.1.0", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
wgpu-core = { git = "https://github.com/gfx-rs/wgpu", rev = "c72bc7b84b094f70cc47dd8a1aa8175e3f3dc5b6", features = ["wgsl", "vulkan", "metal", "raw-window-handle"] }
wgpu-types = { git = "https://github.com/gfx-rs/wgpu", rev = "c72bc7b84b094f70cc47dd8a1aa8175e3f3dc5b6" }
13 changes: 13 additions & 0 deletions crates/canvas-c/src/webgpu/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ use crate::webgpu::gpu_buffer::CanvasGPUBuffer;
use crate::webgpu::gpu_sampler::CanvasGPUSampler;
use crate::webgpu::gpu_texture_view::CanvasGPUTextureView;


#[repr(C)]
#[derive(Copy, Clone, Debug,Eq, PartialEq)]
pub enum SurfaceGetCurrentTextureStatus {
Success = 0x00000000,
Timeout = 0x00000001,
Outdated = 0x00000002,
Lost = 0x00000003,
OutOfMemory = 0x00000004,
DeviceLost = 0x00000005,
Force32 = 0x7FFFFFFF
}

#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum CanvasTextureDimension {
Expand Down
8 changes: 5 additions & 3 deletions crates/canvas-c/src/webgpu/gpu_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::{
};
use std::sync::Arc;

//use wgpu_core::gfx_select;

use crate::buffers::StringBuffer;
use crate::webgpu::gpu_device::{DEFAULT_DEVICE_LOST_HANDLER, ErrorSinkRaw};
use crate::webgpu::gpu_queue::QueueId;
Expand All @@ -15,6 +13,8 @@ use super::{
gpu_queue::CanvasGPUQueue, gpu_supported_limits::CanvasGPUSupportedLimits, prelude::*,
};

//use wgpu_core::gfx_select;

pub struct CanvasGPUAdapter {
pub(crate) instance: Arc<CanvasWebGPUInstance>,
pub(crate) adapter: wgpu_core::id::AdapterId,
Expand Down Expand Up @@ -134,7 +134,7 @@ pub extern "C" fn canvas_native_webgpu_adapter_request_device(
let instance = Arc::clone(&adapter.instance);
std::thread::spawn(move || {
let descriptor = wgt::DeviceDescriptor {
label,
label: label.clone(),
required_features: features,
required_limits: limits,
memory_hints: Default::default(),
Expand Down Expand Up @@ -169,6 +169,7 @@ pub extern "C" fn canvas_native_webgpu_adapter_request_device(
)));

let queue = Arc::new(CanvasGPUQueue {
label: descriptor.label,
queue: Arc::new(QueueId {
id: queue,
instance: Arc::clone(&instance_copy),
Expand All @@ -177,6 +178,7 @@ pub extern "C" fn canvas_native_webgpu_adapter_request_device(
});

let ret = Arc::into_raw(Arc::new(CanvasGPUDevice {
label,
device,
queue,
user_data: std::ptr::null_mut(),
Expand Down
15 changes: 15 additions & 0 deletions crates/canvas-c/src/webgpu/gpu_bind_group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::borrow::Cow;
use std::os::raw::c_char;
use std::sync::Arc;

use crate::webgpu::prelude::label_to_ptr;

//use wgpu_core::gfx_select;
use super::gpu::CanvasWebGPUInstance;

Expand All @@ -17,6 +21,17 @@ impl Drop for CanvasGPUBindGroup {
}
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_bind_group_get_label(
bind_group: *const CanvasGPUBindGroup,
) -> *mut c_char {
if bind_group.is_null() {
return std::ptr::null_mut();
}
let bind_group = &*bind_group;
label_to_ptr(bind_group.label.clone())
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_bind_group_reference(
bind_group: *const CanvasGPUBindGroup,
Expand Down
17 changes: 17 additions & 0 deletions crates/canvas-c/src/webgpu/gpu_bind_group_layout.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use std::borrow::Cow;
use std::os::raw::c_char;
use std::sync::Arc;

use crate::webgpu::prelude::label_to_ptr;

//use wgpu_core::gfx_select;
use super::gpu::CanvasWebGPUInstance;

pub struct CanvasGPUBindGroupLayout {
pub(crate) label: Option<Cow<'static, str>>,
pub(crate) instance: Arc<CanvasWebGPUInstance>,
pub(crate) group_layout: wgpu_core::id::BindGroupLayoutId,
}
Expand All @@ -15,6 +21,17 @@ impl Drop for CanvasGPUBindGroupLayout {
}
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_bind_group_layout_get_label(
bind_group_layout: *const CanvasGPUBindGroupLayout,
) -> *mut c_char {
if bind_group_layout.is_null() {
return std::ptr::null_mut();
}
let bind_group_layout = &*bind_group_layout;
label_to_ptr(bind_group_layout.label.clone())
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_bind_group_layout_reference(
bind_group_layout: *const CanvasGPUBindGroupLayout,
Expand Down
26 changes: 20 additions & 6 deletions crates/canvas-c/src/webgpu/gpu_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::Arc;
use std::{
borrow::Cow,
ffi::CString,
os::raw::{c_char, c_void},
};
//use wgpu_core::gfx_select;
use crate::webgpu::error::{handle_error_fatal, CanvasGPUError, CanvasGPUErrorType};
use std::sync::Arc;

//use wgpu_core::gfx_select;
use crate::webgpu::error::{CanvasGPUError, CanvasGPUErrorType, handle_error_fatal};
use crate::webgpu::prelude::label_to_ptr;
use super::gpu::CanvasWebGPUInstance;

#[repr(C)]
Expand Down Expand Up @@ -36,7 +37,7 @@ impl From<wgpu_core::device::HostMap> for GPUMapMode {

pub struct CanvasGPUBuffer {
pub(crate) instance: Arc<CanvasWebGPUInstance>,
pub(crate) label: Cow<'static, str>,
pub(crate) label: Option<Cow<'static, str>>,
pub(crate) buffer: wgpu_core::id::BufferId,
pub(crate) size: u64,
pub(crate) usage: u32,
Expand All @@ -47,12 +48,15 @@ impl Drop for CanvasGPUBuffer {
fn drop(&mut self) {
if !std::thread::panicking() {
let global = self.instance.global();
gfx_select!(id => global.buffer_drop(self.buffer, false));
gfx_select!(id => global.buffer_drop(self.buffer));
}
}
}

impl CanvasGPUBuffer {
pub fn label(&self) -> Option<Cow<'static, str>> {
return self.label.clone();
}
pub fn size(&self) -> u64 {
self.size
}
Expand All @@ -74,6 +78,16 @@ impl CanvasGPUBuffer {
}
}


#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_buffer_get_label(buffer: *const CanvasGPUBuffer) -> *mut c_char {
if buffer.is_null() {
return std::ptr::null_mut();
}
let buffer = &*buffer;
label_to_ptr(buffer.label.clone())
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_buffer_reference(buffer: *const CanvasGPUBuffer) {
if buffer.is_null() {
Expand Down Expand Up @@ -161,7 +175,7 @@ pub extern "C" fn canvas_native_webgpu_buffer_map_async(
mode: GPUMapMode,
offset: i64,
size: i64,
callback: extern "C" fn(*mut c_char, *mut c_void),
callback: extern "C" fn(CanvasGPUErrorType, *mut c_char, *mut c_void),
callback_data: *mut c_void,
) {
if buffer.is_null() {
Expand Down
68 changes: 41 additions & 27 deletions crates/canvas-c/src/webgpu/gpu_canvas_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::sync::Arc;

use parking_lot::lock_api::Mutex;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
//use wgpu_core::gfx_select;
use wgt::SurfaceStatus;

use crate::webgpu::enums::CanvasOptionalGPUTextureFormat;
use crate::webgpu::enums::{CanvasOptionalGPUTextureFormat, SurfaceGetCurrentTextureStatus};
use crate::webgpu::error::handle_error_fatal;
use crate::webgpu::gpu_adapter::CanvasGPUAdapter;
use crate::webgpu::gpu_device::ErrorSink;
Expand All @@ -16,6 +16,8 @@ use super::{
gpu_texture::CanvasGPUTexture,
};

//use wgpu_core::gfx_select;

#[derive(Copy, Clone)]
struct TextureData {
usage: wgt::TextureUsages,
Expand Down Expand Up @@ -433,31 +435,43 @@ pub extern "C" fn canvas_native_webgpu_context_get_current_texture(
let result = gfx_select!(surface_id => global.surface_get_current_texture(surface_id, None));

match result {
Ok(texture) => match texture.status {
wgt::SurfaceStatus::Good | wgt::SurfaceStatus::Suboptimal => {
context
.has_surface_presented
.store(false, std::sync::atomic::Ordering::SeqCst);

Arc::into_raw(Arc::new(CanvasGPUTexture {
instance: context.instance.clone(),
texture: texture.texture_id.unwrap(),
surface_id: Some(surface_id),
owned: false,
depth_or_array_layers: 1,
dimension: super::enums::CanvasTextureDimension::D2,
format: surface_data.texture_data.format.into(),
mipLevelCount: 1,
sampleCount: 1,
width: surface_data.texture_data.size.width,
height: surface_data.texture_data.size.height,
usage: surface_data.texture_data.usage.bits(),
error_sink: surface_data.error_sink.clone(),
has_surface_presented: context.has_surface_presented.clone(),
}))
}
_ => std::ptr::null_mut(),
},
Ok(texture) => {
let mut suboptimal = false;
let status = match texture.status {
SurfaceStatus::Good => SurfaceGetCurrentTextureStatus::Success,
SurfaceStatus::Suboptimal => {
suboptimal = true;
SurfaceGetCurrentTextureStatus::Success
}
SurfaceStatus::Timeout => SurfaceGetCurrentTextureStatus::Timeout,
SurfaceStatus::Outdated => SurfaceGetCurrentTextureStatus::Outdated,
SurfaceStatus::Lost => SurfaceGetCurrentTextureStatus::Lost,
};

context
.has_surface_presented
.store(false, std::sync::atomic::Ordering::SeqCst);

Arc::into_raw(Arc::new(CanvasGPUTexture {
label: None,
instance: context.instance.clone(),
texture: texture.texture_id.unwrap(),
surface_id: Some(surface_id),
owned: false,
depth_or_array_layers: 1,
dimension: super::enums::CanvasTextureDimension::D2,
format: surface_data.texture_data.format.into(),
mipLevelCount: 1,
sampleCount: 1,
width: surface_data.texture_data.size.width,
height: surface_data.texture_data.size.height,
usage: surface_data.texture_data.usage.bits(),
error_sink: surface_data.error_sink.clone(),
suboptimal,
status,
has_surface_presented: context.has_surface_presented.clone(),
}))
}
Err(cause) => {
handle_error_fatal(
global,
Expand Down
18 changes: 18 additions & 0 deletions crates/canvas-c/src/webgpu/gpu_command_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use std::borrow::Cow;
use std::os::raw::c_char;
use std::sync::Arc;

use crate::webgpu::prelude::label_to_ptr;

//use wgpu_core::gfx_select;
use super::gpu::CanvasWebGPUInstance;

pub struct CanvasGPUCommandBuffer {
pub(crate) label: Option<Cow<'static, str>>,
pub(crate) instance: Arc<CanvasWebGPUInstance>,
pub(crate) command_buffer: wgpu_core::id::CommandBufferId,
pub(crate) open: std::sync::atomic::AtomicBool,
Expand All @@ -22,6 +28,18 @@ impl Drop for CanvasGPUCommandBuffer {
}
}

#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_command_buffer_get_label(
command_buffer: *const CanvasGPUCommandBuffer,
) -> *mut c_char {
if command_buffer.is_null() {
return std::ptr::null_mut();
}
let command_buffer = &*command_buffer;
label_to_ptr(command_buffer.label.clone())
}


#[no_mangle]
pub unsafe extern "C" fn canvas_native_webgpu_command_buffer_reference(
command_buffer: *const CanvasGPUCommandBuffer,
Expand Down
Loading

0 comments on commit 44bfb6b

Please sign in to comment.