Skip to content

Commit

Permalink
chore: webgpu.1
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jul 23, 2024
1 parent f104df6 commit cdf9826
Show file tree
Hide file tree
Showing 67 changed files with 5,281 additions and 3,995 deletions.
11 changes: 6 additions & 5 deletions crates/canvas-c/src/webgpu/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@ pub(crate) fn handle_error_fatal(
// "Error in {operation}: {f}",
// f = format_error(context, &cause)
// );
let error = cause.to_string();

println!("error ? {:?}", error);

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

log::error!("Error in {operation}: {f}",
f = format_error(global, &cause))
let error = cause.to_string();

log::error!("Error in {operation}: {error}")

// log::error!("Error in {operation}: {f}",
// f = format_error(global, &cause))
}

#[repr(C)]
Expand Down
18 changes: 13 additions & 5 deletions crates/canvas-c/src/webgpu/gpu_command_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;

use crate::webgpu::error::handle_error;
use crate::webgpu::prelude::ptr_into_label;

use crate::webgpu::structs::{CanvasLoadOp, CanvasStoreOp};
use super::{
enums::CanvasTextureAspect,
gpu::CanvasWebGPUInstance,
Expand Down Expand Up @@ -214,15 +214,22 @@ pub unsafe extern "C" fn canvas_native_webgpu_command_encoder_begin_render_pass(
let depth_stencil_attachment = if !depth_stencil_attachment.is_null() {
let depth_stencil_attachment = &*depth_stencil_attachment;
let view = &*depth_stencil_attachment.view;

let depth_load: Option<CanvasLoadOp> = depth_stencil_attachment.depth_load_op.into();
let depth_store: Option<CanvasStoreOp> = depth_stencil_attachment.depth_store_op.into();

let stencil_load: Option<CanvasLoadOp> = depth_stencil_attachment.stencil_load_op.into();
let stencil_store: Option<CanvasStoreOp> = depth_stencil_attachment.stencil_store_op.into();

let depth = wgpu_core::command::PassChannel {
load_op: depth_stencil_attachment.depth_load_op.into(),
store_op: depth_stencil_attachment.depth_store_op.into(),
load_op: depth_load.unwrap_or(CanvasLoadOp::Clear).into(),
store_op: depth_store.unwrap_or(CanvasStoreOp::Store).into(),
clear_value: depth_stencil_attachment.depth_clear_value,
read_only: depth_stencil_attachment.depth_read_only,
};
let stencil = wgpu_core::command::PassChannel {
load_op: depth_stencil_attachment.stencil_load_op.into(),
store_op: depth_stencil_attachment.stencil_store_op.into(),
load_op: stencil_load.unwrap_or(CanvasLoadOp::Clear).into(),
store_op: stencil_store.unwrap_or(CanvasStoreOp::Store).into(),
clear_value: depth_stencil_attachment.stencil_clear_value,
read_only: depth_stencil_attachment.stencil_read_only,
};
Expand All @@ -249,6 +256,7 @@ pub unsafe extern "C" fn canvas_native_webgpu_command_encoder_begin_render_pass(
timestamp_writes: timestamp_writes.as_ref(),
occlusion_query_set,
};

let (pass, err) = global.command_encoder_create_render_pass(command_encoder_id, &desc);

let error_sink = command_encoder.error_sink.as_ref();
Expand Down
1 change: 0 additions & 1 deletion crates/canvas-c/src/webgpu/gpu_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,6 @@ pub unsafe extern "C" fn canvas_native_webgpu_device_create_render_pipeline(
} else {
None
};

let primitive: wgpu_types::PrimitiveState = if !descriptor.primitive.is_null() {
let primitive = *descriptor.primitive;
primitive.into()
Expand Down
56 changes: 46 additions & 10 deletions crates/canvas-c/src/webgpu/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,24 @@ pub enum CanvasOptionalLoadOp {
Some(CanvasLoadOp),
}

impl From<Option<CanvasLoadOp>> for CanvasOptionalLoadOp {
fn from(value: Option<CanvasLoadOp>) -> Self {
match value {
Some(value) => Self::Some(value),
None => Self::None,
}
}
}

impl Into<Option<CanvasLoadOp>> for CanvasOptionalLoadOp {
fn into(self) -> Option<CanvasLoadOp> {
match self {
CanvasOptionalLoadOp::None => None,
CanvasOptionalLoadOp::Some(value) => Some(value),
}
}
}

impl From<Option<wgpu_core::command::LoadOp>> for CanvasOptionalLoadOp {
fn from(value: Option<wgpu_core::command::LoadOp>) -> Self {
match value {
Expand All @@ -772,6 +790,24 @@ pub enum CanvasOptionalStoreOp {
Some(CanvasStoreOp),
}

impl From<Option<CanvasStoreOp>> for CanvasOptionalStoreOp {
fn from(value: Option<CanvasStoreOp>) -> Self {
match value {
Some(value) => Self::Some(value),
None => Self::None,
}
}
}

impl Into<Option<CanvasStoreOp>> for CanvasOptionalStoreOp {
fn into(self) -> Option<CanvasStoreOp> {
match self {
CanvasOptionalStoreOp::None => None,
CanvasOptionalStoreOp::Some(value) => Some(value),
}
}
}

impl From<Option<wgpu_core::command::StoreOp>> for CanvasOptionalStoreOp {
fn from(value: Option<wgpu_core::command::StoreOp>) -> Self {
match value {
Expand All @@ -790,19 +826,19 @@ impl Into<Option<wgpu_core::command::StoreOp>> for CanvasOptionalStoreOp {
}
}

#[repr(C)]
pub struct CanvasRenderPassDepthStencilAttachment {
pub(crate) view: *const CanvasGPUTextureView,
pub(crate) depth_clear_value: f32,
pub(crate) depth_load_op: CanvasLoadOp,
pub(crate) depth_store_op: CanvasStoreOp,
pub(crate) depth_read_only: bool,
pub(crate) stencil_clear_value: u32,
pub(crate) stencil_load_op: CanvasLoadOp,
pub(crate) stencil_store_op: CanvasStoreOp,
pub(crate) stencil_read_only: bool,
pub view: *const CanvasGPUTextureView,
pub depth_clear_value: f32,
pub depth_load_op: CanvasOptionalLoadOp,
pub depth_store_op: CanvasOptionalStoreOp,
pub depth_read_only: bool,
pub stencil_clear_value: u32,
pub stencil_load_op: CanvasOptionalLoadOp,
pub stencil_store_op: CanvasOptionalStoreOp,
pub stencil_read_only: bool,
}


#[repr(C)]
pub struct CanvasSurfaceCapabilities {
pub formats: *const StringBuffer,
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-babylon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-babylon",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-media/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-media",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "Canvas media",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-phaser-ce/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-phaser-ce",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "Tools for using Phaser-ce to build native 2D games in NativeScript 👾",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-phaser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-phaser",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "Build awesome 2D games with Phaser.js and NativeScript",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-pixi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-pixi",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "Plugin for using pixi.js in NativeScript",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-polyfill/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-polyfill",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "Polyfill for making NativeScript compatible with web libs like pixi.js, three.js, phaser.js, babylon.js, etc....",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-svg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-svg",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "Add a plugin description",
"main": "index",
"typings": "index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-three/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nativescript/canvas-three",
"version": "2.0.0-webgpu.0",
"version": "2.0.0-webgpu.1",
"description": "Utilities for using THREE.js on NativeScript",
"main": "index",
"typings": "index.d.ts",
Expand Down
20 changes: 8 additions & 12 deletions packages/canvas/Canvas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,27 +257,23 @@ export class Touch {
stopPropagation() {}
}

export class TouchList {
private _list: Touch[];

export class TouchList extends Array {
static fromList(list: Touch[]): TouchList {
const ret = new TouchList();
ret._list = list;
ret.splice(0, 0, ...list);
return ret;
}

static empty() {
const ret = new TouchList();
ret._list = [];
return ret;
return new TouchList();
}

item(index: number): Touch {
return this._list[index];
return this[index];
}

get length(): number {
return this._list.length;
return this.length;
}
}

Expand Down Expand Up @@ -616,7 +612,7 @@ export abstract class CanvasBase extends View implements ICanvasBase {
}

if (this._touchMoveCallbacks.length > 0) {
const changedTouches = [];
const changedTouches = TouchList.empty();

for (const pointer of pointers) {
changedTouches.push(
Expand Down Expand Up @@ -735,7 +731,7 @@ export abstract class CanvasBase extends View implements ICanvasBase {
if (this._touchEndCallbacks.length > 0) {
const touches = TouchList.fromList(this._touches);

const changedTouches = [
const changedTouches = TouchList.fromList([
new Touch({
identifier: ptrId,
target: null,
Expand All @@ -746,7 +742,7 @@ export abstract class CanvasBase extends View implements ICanvasBase {
pageX: x,
pageY: y,
}),
];
]);

const event = new TouchEvent('touchend', {
touches,
Expand Down
2 changes: 2 additions & 0 deletions packages/canvas/Canvas/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ export class Canvas extends CanvasBase {
this._canvas.initWebGPUContext(long(ptr));

this._gpuContext = new (GPUCanvasContext as any)(this._canvas);

(this._gpuContext as any)._canvas = this;
}

return this._gpuContext;
Expand Down
5 changes: 3 additions & 2 deletions packages/canvas/Canvas/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,10 @@ export class Canvas extends CanvasBase {
const ptr = navigator.gpu.native.__getPointer();
const number = NSNumber.numberWithLong(Number(ptr));
this._canvas.initWebGPUContext(number);

this._gpuContext = new (GPUCanvasContext as any)(this._canvas);


(this._gpuContext as any)._canvas = this;
}

return this._gpuContext;
Expand Down
23 changes: 17 additions & 6 deletions packages/canvas/WebGPU/GPUCanvasContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { GPUAdapter } from './GPUAdapter';
import type { GPUCanvasAlphaMode, GPUCanvasPresentMode, GPUExtent3D, GPUTextureFormat } from './Types';
export class GPUCanvasContext {
_type;
_canvas: any;
static {
Helpers.initialize();
}
Expand Down Expand Up @@ -70,22 +71,32 @@ export class GPUCanvasContext {
this[native_].unconfigure();
}

_currentTexture: GPUTexture;
getCurrentTexture() {
const texture = this[native_].getCurrentTexture();
if (texture) {
return GPUTexture.fromNative(texture);
if (this._currentTexture) {
return this._currentTexture;
}

return null;
const texture = this[native_].getCurrentTexture();
// if (texture) {
// this._currentTexture = GPUTexture.fromNative(texture);
// } else {
// this._currentTexture = null;
// }

// return this._currentTexture;
return GPUTexture.fromNative(texture);
}

presentSurface(texture: GPUTexture) {
if (this._currentTexture === texture) {
this._currentTexture = null;
}
this[native_].presentSurface(texture?.[native_]);
}

getCapabilities(adapter: GPUAdapter): {
format: GPUTextureFormat[];
presentModes: ('autoVsync' | 'autoNoVsync' | 'fifo' | 'fifoRelaxed' | 'immediate' | 'mailbox')[];
presentModes: GPUCanvasPresentMode[];
alphaModes: GPUCanvasAlphaMode;
usages: number;
} {
Expand Down
6 changes: 5 additions & 1 deletion packages/canvas/WebGPU/GPUCommandEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export class GPUCommandEncoder {
if (Array.isArray(attachment.clearValue)) {
attachment.clearValue = { r: attachment.clearValue[0], g: attachment.clearValue[1], b: attachment.clearValue[2], a: attachment.clearValue[3] };
}
attachment.view = attachment.view[native_];
if (attachment.view) {
attachment.view = attachment.view[native_];
} else {
/// ???
}

if (attachment.resolveTarget) {
attachment.resolveTarget = attachment.resolveTarget[native_];
Expand Down
Loading

0 comments on commit cdf9826

Please sign in to comment.