Skip to content

Commit 75a27e6

Browse files
committed
refactor closures code, fix NSMethodSignature crash, add webgpu example, add Symbols framework to metadata generator script, interop.handleof implementation
1 parent 64c1060 commit 75a27e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+7301
-5013
lines changed

examples/webgpu.ts

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import "@nativescript/macos-node-api";
2+
3+
const adapter = await navigator.gpu.requestAdapter();
4+
const device = await adapter!.requestDevice();
5+
6+
@NativeClass
7+
export class ApplicationDelegate
8+
extends NSObject
9+
implements NSApplicationDelegate
10+
{
11+
static ObjCProtocols = [NSApplicationDelegate];
12+
13+
running = true;
14+
window: Window | null = null;
15+
16+
applicationDidFinishLaunching(_notification: NSNotification) {
17+
this.window = Window.new();
18+
}
19+
20+
applicationWillTerminate(_notification: NSNotification) {
21+
this.running = false;
22+
}
23+
}
24+
25+
@NativeClass
26+
export class Window extends NSWindow implements NSWindowDelegate {
27+
static ObjCProtocols = [NSWindowDelegate];
28+
29+
surface!: Deno.UnsafeWindowSurface;
30+
context!: GPUCanvasContext;
31+
renderPipeline!: GPURenderPipeline;
32+
33+
init() {
34+
const menu = NSMenu.new();
35+
NSApp.mainMenu = menu;
36+
37+
const appMenuItem = NSMenuItem.new();
38+
menu.addItem(appMenuItem);
39+
40+
const appMenu = NSMenu.new();
41+
appMenuItem.submenu = appMenu;
42+
43+
appMenu.addItemWithTitleActionKeyEquivalent("Quit", "terminate:", "q");
44+
45+
super.initWithContentRectStyleMaskBackingDefer(
46+
{ origin: { x: 0, y: 0 }, size: { width: 500, height: 500 } },
47+
NSWindowStyleMask.Titled |
48+
NSWindowStyleMask.Closable |
49+
NSWindowStyleMask.Miniaturizable |
50+
NSWindowStyleMask.Resizable,
51+
2,
52+
false
53+
);
54+
55+
this.title = "NativeScript for macOS";
56+
this.delegate = this;
57+
58+
this.isReleasedWhenClosed = false;
59+
60+
this.surface = new Deno.UnsafeWindowSurface(
61+
"cocoa",
62+
Deno.UnsafePointer.create(BigInt(interop.handleof(this).toNumber())),
63+
Deno.UnsafePointer.create(
64+
BigInt(interop.handleof(this.contentView).toNumber())
65+
)
66+
);
67+
68+
const { width, height } = this.convertRectToBacking(this.frame).size;
69+
70+
this.context = this.surface.getContext("webgpu");
71+
72+
this.context.configure({
73+
device,
74+
format: "bgra8unorm",
75+
width,
76+
height,
77+
});
78+
79+
const shaderCode = `
80+
@vertex
81+
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
82+
let x = f32(i32(in_vertex_index) - 1);
83+
let y = f32(i32(in_vertex_index & 1u) * 2 - 1);
84+
return vec4<f32>(x, y, 0.0, 1.0);
85+
}
86+
87+
@fragment
88+
fn fs_main() -> @location(0) vec4<f32> {
89+
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
90+
}
91+
`;
92+
93+
const shaderModule = device.createShaderModule({
94+
code: shaderCode,
95+
});
96+
97+
const pipelineLayout = device.createPipelineLayout({
98+
bindGroupLayouts: [],
99+
});
100+
101+
this.renderPipeline = device.createRenderPipeline({
102+
layout: pipelineLayout,
103+
vertex: {
104+
module: shaderModule,
105+
entryPoint: "vs_main",
106+
},
107+
fragment: {
108+
module: shaderModule,
109+
entryPoint: "fs_main",
110+
targets: [
111+
{
112+
format: "bgra8unorm",
113+
},
114+
],
115+
},
116+
});
117+
118+
NSApp.activateIgnoringOtherApps(true);
119+
120+
this.makeKeyAndOrderFront(NSApp);
121+
122+
this.center();
123+
124+
return this;
125+
}
126+
127+
render() {
128+
const encoder = device.createCommandEncoder();
129+
const texture = this.context.getCurrentTexture().createView();
130+
const renderPass = encoder.beginRenderPass({
131+
colorAttachments: [
132+
{
133+
view: texture,
134+
storeOp: "store",
135+
loadOp: "clear",
136+
clearValue: { r: 0, g: 1, b: 0, a: 1.0 },
137+
},
138+
],
139+
});
140+
renderPass.setPipeline(this.renderPipeline);
141+
renderPass.draw(3, 1);
142+
renderPass.end();
143+
device.queue.submit([encoder.finish()]);
144+
this.surface.present();
145+
}
146+
147+
windowWillClose(_notification: NSNotification) {
148+
NSApp.terminate(this);
149+
}
150+
}
151+
152+
const NSApp = NSApplication.sharedApplication;
153+
154+
NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular);
155+
156+
const delegate = ApplicationDelegate.new();
157+
158+
NSApp.delegate = delegate;
159+
160+
NSApp.finishLaunching();
161+
162+
while (true) {
163+
const event = NSApp.nextEventMatchingMaskUntilDateInModeDequeue(
164+
NSEventMask.Any,
165+
null,
166+
"kCFRunLoopDefaultMode",
167+
true
168+
);
169+
170+
if (event != null) {
171+
NSApp.sendEvent(event);
172+
}
173+
174+
if (!delegate.running) {
175+
break;
176+
}
177+
178+
delegate.window?.render();
179+
}

metadata/metadata.macos.nsmd

34.2 KB
Binary file not shown.

packages/macos/types/AVFAudio.d.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,6 @@ declare class AVAudioTime extends NSObject {
221221
readonly audioTimeStamp: AudioTimeStamp;
222222
}
223223

224-
declare class AVAudioEnvironmentReverbParameters extends NSObject {
225-
enable: boolean;
226-
227-
level: number;
228-
229-
readonly filterParameters: AVAudioUnitEQFilterParameters;
230-
231-
loadFactoryReverbPreset(preset: interop.Enum<typeof AVAudioUnitReverbPreset>): void;
232-
}
233-
234224
declare class AVAudioPCMBuffer extends AVAudioBuffer {
235225
initWithPCMFormatFrameCapacity(format: AVAudioFormat, frameCapacity: number): this;
236226

@@ -452,6 +442,16 @@ declare class AVAudioFormat extends NSObject implements NSSecureCoding {
452442
initWithCoder(coder: NSCoder): this;
453443
}
454444

445+
declare class AVAudioEnvironmentReverbParameters extends NSObject {
446+
enable: boolean;
447+
448+
level: number;
449+
450+
readonly filterParameters: AVAudioUnitEQFilterParameters;
451+
452+
loadFactoryReverbPreset(preset: interop.Enum<typeof AVAudioUnitReverbPreset>): void;
453+
}
454+
455455
declare class AVAudioIONode extends AVAudioNode {
456456
readonly presentationLatency: number;
457457

@@ -668,6 +668,8 @@ declare class AVAudioEnvironmentNode extends AVAudioNode implements AVAudioMixin
668668

669669
readonly applicableRenderingAlgorithms: NSArray;
670670

671+
isListenerHeadTrackingEnabled: boolean;
672+
671673
destinationForMixerBus(mixer: AVAudioNode, bus: number): AVAudioMixingDestination;
672674

673675
volume: number;

0 commit comments

Comments
 (0)