@mrigx/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @file WebGPUManager.ts
3
+ * @description Handles the initialization and lifecycle of the WebGPU device and context.
4
+ */
5
+
6
+ export class WebGPUManager {
7
+ public device: GPUDevice | null = null;
8
+ public context: GPUCanvasContext | null = null;
9
+ public format: GPUTextureFormat = 'bgra8unorm';
10
+ public canvas: HTMLCanvasElement | null = null;
11
+
12
+ /**
13
+ * Initializes the WebGPU device and context for the given canvas.
14
+ * @param canvas - The HTMLCanvasElement to render to.
15
+ * @returns A boolean indicating whether initialization was successful.
16
+ */
17
+ public async init(canvas: HTMLCanvasElement): Promise<boolean> {
18
+ this.canvas = canvas;
19
+
20
+ if (!navigator.gpu) {
21
+ console.error('WebGPU is not supported by this browser.');
22
+ return false;
23
+ }
24
+
25
+ const adapter = await navigator.gpu.requestAdapter();
26
+
27
+ if (!adapter) {
28
+ console.error('Failed to request WebGPU adapter.');
29
+ return false;
30
+ }
31
+
32
+ this.device = await adapter.requestDevice({
33
+ requiredLimits: {
34
+ maxTextureDimension2D: adapter.limits.maxTextureDimension2D,
35
+ }
36
+ });
37
+ this.context = canvas.getContext('webgpu') as GPUCanvasContext;
38
+
39
+ if (!this.context) {
40
+ console.error('Failed to get WebGPU canvas context.');
41
+ return false;
42
+ }
43
+
44
+ this.format = navigator.gpu.getPreferredCanvasFormat();
45
+ this.context.configure({
46
+ device: this.device,
47
+ format: this.format,
48
+ alphaMode: 'premultiplied',
49
+ });
50
+
51
+ return true;
52
+ }
53
+
54
+ /**
55
+ * Cleans up WebGPU resources.
56
+ */
57
+ public destroy(): void {
58
+ if (this.device) {
59
+ this.device.destroy();
60
+ this.device = null;
61
+ }
62
+ this.context = null;
63
+ this.canvas = null;
64
+ }
65
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { MrigEngine } from './MrigEngine';
2
+ export { DomRasterSystem } from './DomRasterSystem';
3
+ export { WebGPUManager } from './WebGPUManager';
4
+ export * from './types';
5
+ export * from './utils';