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