Skip to content

Commit

Permalink
chore: webgpu.11 (#126)
Browse files Browse the repository at this point in the history
* chore: refactor

* chore: updates

* chore: updates

* chore: render direct

* chore: updates

* chore: webgpu.2

* chore: config

* chore: webgpu.4

* chore: ci

* chore: ci targets

* chore: ci

* chore: ci

* fix: ci

* fix: ci

* chore: cii

* chore: ci

* chore: ci

* chore: ci

* chore: ci

* chore: ci

* chore: ci

* fix: ci

* chore: ci

* chore: ci

* chore: ci

* chore: ci

* chore: ci

* chore: webgpu.5

* chore: cleanup with TS 5.5

* chore: webgpu.6

* chore: webgpu.7

* chore: webgpu.8

* chore: webgpu.9

* chore: webgpu.11

---------

Co-authored-by: Nathan Walker <[email protected]>
  • Loading branch information
triniwiz and NathanWalker authored Aug 16, 2024
1 parent 99f8ab8 commit 9d730b4
Show file tree
Hide file tree
Showing 201 changed files with 10,500 additions and 6,566 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" }
2 changes: 1 addition & 1 deletion apps/demo/src/plugin-demos/canvas.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ loaded="{{ rectLoaded }}"
<!-- <WebView rowSpan="2" colSpan="2" height="100%" width="100%" loaded="loaded"/>" -->

<GridLayout verticalAlignment="top" rowSpan="2" colSpan="2" height="100%" width="100%">
<canvas:Canvas ready="{{ canvasLoaded }}" style="width:100%; height:auto;"/>
<canvas:Canvas ready="{{ canvasLoaded }}" style="width:100%; height:100%;"/>
</GridLayout>

<!-- <Button height="40" text="Draw" tap="{{ draw }}"/> -->
Expand Down
5 changes: 3 additions & 2 deletions crates/canvas-2d/src/context/drawing_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,11 @@ impl Context {
} else {
1.0
};

let current = canvas.local_to_device_as_3x3();
let current_y = current.scale_y();
canvas.save();
if need_scale {
canvas.scale((ratio, 1.0));
canvas.scale((ratio, current_y));
}
let paint_y = y + baseline_offset;

Expand Down
3 changes: 1 addition & 2 deletions crates/canvas-2d/src/context/surface_gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ impl Context {
let interface = Interface::new_native();
let mut ctx = gpu::direct_contexts::make_gl(interface.unwrap(), None).unwrap();

ctx.reset(None);

let mut frame_buffer = gpu::gl::FramebufferInfo::from_fboid(buffer_id as u32);
if alpha {
frame_buffer.format = GR_GL_RGBA8;
Expand Down Expand Up @@ -164,6 +162,7 @@ impl Context {
return;
}
let mut ctx = ctx.unwrap();
// ctx.reset(None);

let mut frame_buffer = gpu::gl::FramebufferInfo::from_fboid(buffer_id as u32);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,13 @@ pub extern "system" fn nativeUpdateGLSurface(
unsafe {
if let Some(window) = NativeWindow::from_surface(env.get_native_interface(), surface) {
let handle = to_raw_window_handler(&window);

context.gl_context.set_window_surface(
&mut context.contextAttributes,
window.width(),
window.height(),
handle,
);
context.gl_context.make_current();
context.android_window = Some(window);
drop(env);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use jni::JNIEnv;
use jni::objects::{JClass, JFloatArray, JIntArray, JObject, JString};
use jni::sys::{jboolean, jfloat, jint, jlong, JNI_FALSE, JNI_TRUE};
use jni::JNIEnv;
use skia_safe::Color;

use canvas_2d::context::compositing::composite_operation_type::CompositeOperationType;
use canvas_2d::context::fill_and_stroke_styles::pattern::Repetition;
use canvas_2d::utils::image::from_image_slice;
use canvas_c::{CanvasRenderingContext2D,PaintStyle};
use canvas_c::{ImageAsset};
use canvas_c::{CanvasRenderingContext2D, PaintStyle};
use canvas_c::ImageAsset;

#[no_mangle]
pub extern "system" fn nativeCreatePattern(
Expand Down Expand Up @@ -386,3 +386,23 @@ pub extern "system" fn nativeDrawImageWithAsset(

JNI_TRUE
}


#[no_mangle]
pub extern "system" fn nativeScale(
_env: JNIEnv,
_: JClass,
context: jlong,
x: jfloat,
y: jfloat,
) {
if context == 0 {
return;
}

let context = context as *mut CanvasRenderingContext2D;

canvas_c::canvas_native_context_scale(
context, x, y,
);
}
31 changes: 10 additions & 21 deletions crates/canvas-android/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,20 @@ extern crate core;
extern crate log;

use std::os::raw::c_void;
use std::sync::OnceLock;

use ::jni::signature::JavaType;
use ::jni::sys::jint;
use ::jni::JavaVM;
use android_logger::Config;
use itertools::izip;
use jni::NativeMethod;
use jni::sys::jlong;
use jni::{ NativeMethod};
use log::LevelFilter;
use std::sync::OnceLock;

use crate::jni_compat::org_nativescript_canvas_NSCCanvas::{
nativeContext2DPathTest, nativeContext2DPathTestNormal, nativeContext2DRender,
nativeContext2DTest, nativeContext2DTestNormal, nativeCreate2DContext,
nativeCreate2DContextNormal, nativeCustomWithBitmapFlush, nativeGLPointerRefCount,
nativeGLPointerRefCountNormal, nativeGetGLPointer, nativeGetGLPointerNormal, nativeInitGL,
nativeInitGLNoSurface, nativeInitWebGPU, nativeMakeGLCurrent, nativeMakeGLCurrentNormal,
nativeReleaseGL, nativeReleaseGLNormal, nativeReleaseGLPointer, nativeReleaseGLPointerNormal,
nativeUpdate2DSurface, nativeUpdate2DSurfaceNoSurface, nativeUpdate2DSurfaceNoSurfaceNormal,
nativeUpdateGLNoSurface, nativeUpdateGLNoSurfaceNormal, nativeUpdateGLSurface,
nativeWebGLC2DRender, nativeWriteCurrentGLContextToBitmap,
};
use crate::jni_compat::org_nativescript_canvas_NSCCanvasRenderingContext2D::{
nativeCreatePattern, nativeDrawAtlasWithBitmap, nativeDrawImageDxDyDwDhWithAsset,
nativeDrawImageDxDyDwDhWithBitmap, nativeDrawImageDxDyWithAsset, nativeDrawImageDxDyWithBitmap,
nativeDrawImageWithAsset, nativeDrawImageWithBitmap,
};
use crate::jni_compat::org_nativescript_canvas_NSCCanvas::{nativeContext2DPathTest, nativeContext2DPathTestNormal, nativeContext2DRender, nativeContext2DTest, nativeContext2DTestNormal, nativeCreate2DContext, nativeCreate2DContextNormal, nativeCustomWithBitmapFlush, nativeGetGLPointer, nativeGetGLPointerNormal, nativeGLPointerRefCount, nativeGLPointerRefCountNormal, nativeInitGL, nativeInitGLNoSurface, nativeInitWebGPU, nativeMakeGLCurrent, nativeMakeGLCurrentNormal, nativeReleaseGL, nativeReleaseGLNormal, nativeReleaseGLPointer, nativeReleaseGLPointerNormal, nativeUpdate2DSurface, nativeUpdate2DSurfaceNoSurface, nativeUpdate2DSurfaceNoSurfaceNormal, nativeUpdateGLNoSurface, nativeUpdateGLNoSurfaceNormal, nativeUpdateGLSurface, nativeWebGLC2DRender, nativeWriteCurrentGLContextToBitmap};
use crate::jni_compat::org_nativescript_canvas_NSCCanvasRenderingContext2D::{nativeCreatePattern, nativeDrawAtlasWithBitmap, nativeDrawImageDxDyDwDhWithAsset, nativeDrawImageDxDyDwDhWithBitmap, nativeDrawImageDxDyWithAsset, nativeDrawImageDxDyWithBitmap, nativeDrawImageWithAsset, nativeDrawImageWithBitmap, nativeScale};
use crate::jni_compat::org_nativescript_canvas_NSCImageAsset::{
nativeCreateImageAsset, nativeDestroyImageAsset, nativeGetDimensions, nativeGetError,
nativeLoadFromBitmap, nativeLoadFromBuffer, nativeLoadFromBytes, nativeLoadFromPath,
Expand All @@ -40,8 +27,6 @@ use crate::jni_compat::org_nativescript_canvas_NSCImageAsset::{
use crate::jni_compat::org_nativescript_canvas_NSCWebGLRenderingContext::{
nativeTexImage2D, nativeTexSubImage2D,
};
use crate::utils::gl::st::{SurfaceTexture, SURFACE_TEXTURE};
use crate::utils::gl::texture_render::nativeDrawFrame;
use crate::utils::{
nativeInitContextWithCustomSurface, nativeInitContextWithCustomSurfaceNormal,
nativeResizeCustomSurface, nativeResizeCustomSurfaceNormal,
Expand Down Expand Up @@ -128,7 +113,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"(J)V",
"(J)V",
"(JJII)V",
"(JLandroid/view/Surface;II)J",
"(JLandroid/view/Surface;II)J"
]
} else {
[
Expand All @@ -152,7 +137,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"!(J)V",
"!(J)V",
"!(JJII)V",
"!(JLandroid/view/Surface;II)J",
"!(JLandroid/view/Surface;II)J"
]
};

Expand Down Expand Up @@ -249,6 +234,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"nativeDrawImageDxDyWithAsset",
"nativeDrawImageDxDyDwDhWithAsset",
"nativeDrawImageWithAsset",
"nativeScale"
];

let canvas_rendering_context_2d_signatures = if ret >= ANDROID_O {
Expand All @@ -261,6 +247,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"(JJFF)Z",
"(JJFFFF)Z",
"(JJFFFFFFFF)Z",
"(JFF)V",
]
} else {
[
Expand All @@ -272,6 +259,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
"!(JJFF)Z",
"!(JJFFFF)Z",
"!(JJFFFFFFFF)Z",
"!(JFF)V",
]
};

Expand All @@ -284,6 +272,7 @@ pub extern "system" fn JNI_OnLoad(vm: JavaVM, _reserved: *const c_void) -> jint
nativeDrawImageDxDyWithAsset as *mut c_void,
nativeDrawImageDxDyDwDhWithAsset as *mut c_void,
nativeDrawImageWithAsset as *mut c_void,
nativeScale as *mut c_void,
];

let canvas_rendering_context_2d_native_methods: Vec<NativeMethod> = izip!(
Expand Down
10 changes: 8 additions & 2 deletions crates/canvas-c/src/c2d/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ pub fn resize_gl(context: &mut CanvasRenderingContext2D, width: f32, height: f32
}

let mut fb = [0];

context.clear_rect(0., 0., width, height);
context.flush_and_render_to_surface();

Expand Down Expand Up @@ -110,6 +109,11 @@ pub fn resize(context: &mut CanvasRenderingContext2D, width: f32, height: f32) {
#[cfg(feature = "gl")]
{
if context.engine == Engine::GL {
// #[cfg(target_os = "android")]
// {
// context.make_current();
// context.gl_context.resize_window_surface(width.floor() as i32, height.floor() as i32);
// }
resize_gl(context, width, height);
return;
}
Expand Down Expand Up @@ -181,7 +185,9 @@ impl CanvasRenderingContext2D {
}

pub fn render(&mut self) {
self.gl_context.make_current();
if self.engine == Engine::GL {
self.gl_context.make_current();
}

{
self.context.flush_and_render_to_surface();
Expand Down
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
Loading

0 comments on commit 9d730b4

Please sign in to comment.