@nemigo/webgpu 1.5.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.
- package/dist/index.d.ts +33 -0
- package/dist/index.js +53 -0
- package/package.json +34 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Cortege } from "@nemigo/helpers/types";
|
|
2
|
+
export interface ProxyFloatBuffer<L extends number, V extends Cortege<L> = Cortege<L>> {
|
|
3
|
+
readonly buffer: GPUBuffer;
|
|
4
|
+
value: V;
|
|
5
|
+
patch(part: number[], offset?: number): void;
|
|
6
|
+
}
|
|
7
|
+
export interface CreateProxyFloatBufferOptions {
|
|
8
|
+
usage?: number;
|
|
9
|
+
label?: string;
|
|
10
|
+
init?: boolean;
|
|
11
|
+
mappedAtCreation?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface IWebGPU {
|
|
14
|
+
adapter: GPUAdapter;
|
|
15
|
+
device: GPUDevice;
|
|
16
|
+
format: GPUTextureFormat;
|
|
17
|
+
init(): Promise<void>;
|
|
18
|
+
createFloatBuffer<L extends number, V extends Cortege<L> = Cortege<L>>(length: L, init: V, options?: CreateProxyFloatBufferOptions): ProxyFloatBuffer<L, V>;
|
|
19
|
+
}
|
|
20
|
+
export declare class WebGPU implements IWebGPU {
|
|
21
|
+
adapter: GPUAdapter;
|
|
22
|
+
device: GPUDevice;
|
|
23
|
+
format: GPUTextureFormat;
|
|
24
|
+
init: () => Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* @param length - количество байтов
|
|
27
|
+
* @param init - начальное значение
|
|
28
|
+
* @param [options] - опции создания
|
|
29
|
+
* @param [options.usage=GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST] - флаги поведения
|
|
30
|
+
* @param [options.init=true] - записать init-значение
|
|
31
|
+
*/
|
|
32
|
+
createFloatBuffer<const L extends number, V extends Cortege<L> = Cortege<L>>(length: L, init: V, options?: CreateProxyFloatBufferOptions): ProxyFloatBuffer<L, V>;
|
|
33
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { profish } from "@nemigo/helpers/fish";
|
|
2
|
+
//...
|
|
3
|
+
export class WebGPU {
|
|
4
|
+
adapter;
|
|
5
|
+
device;
|
|
6
|
+
format;
|
|
7
|
+
init = profish(async () => {
|
|
8
|
+
const adapter = await navigator.gpu.requestAdapter();
|
|
9
|
+
if (!adapter)
|
|
10
|
+
throw new Error("No GPU adapter found");
|
|
11
|
+
this.adapter = adapter;
|
|
12
|
+
this.device = await this.adapter.requestDevice();
|
|
13
|
+
this.format = navigator.gpu.getPreferredCanvasFormat();
|
|
14
|
+
});
|
|
15
|
+
//...
|
|
16
|
+
/**
|
|
17
|
+
* @param length - количество байтов
|
|
18
|
+
* @param init - начальное значение
|
|
19
|
+
* @param [options] - опции создания
|
|
20
|
+
* @param [options.usage=GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST] - флаги поведения
|
|
21
|
+
* @param [options.init=true] - записать init-значение
|
|
22
|
+
*/
|
|
23
|
+
createFloatBuffer(length, init, options = {}) {
|
|
24
|
+
const device = this.device;
|
|
25
|
+
const buffer = device.createBuffer({
|
|
26
|
+
...options,
|
|
27
|
+
size: length * 4,
|
|
28
|
+
usage: options.usage ?? GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
29
|
+
});
|
|
30
|
+
let state = init;
|
|
31
|
+
const proxy = {
|
|
32
|
+
get buffer() {
|
|
33
|
+
return buffer;
|
|
34
|
+
},
|
|
35
|
+
get value() {
|
|
36
|
+
return state;
|
|
37
|
+
},
|
|
38
|
+
set value(value) {
|
|
39
|
+
state = value;
|
|
40
|
+
device.queue.writeBuffer(buffer, 0, new Float32Array(state));
|
|
41
|
+
},
|
|
42
|
+
patch: (partial, offset = 0) => {
|
|
43
|
+
for (let i = 0; i < partial.length; i++) {
|
|
44
|
+
state[offset + i] = partial[i];
|
|
45
|
+
}
|
|
46
|
+
device.queue.writeBuffer(buffer, offset * 4, new Float32Array(partial));
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
if (options.init !== false)
|
|
50
|
+
proxy.value = state;
|
|
51
|
+
return proxy;
|
|
52
|
+
}
|
|
53
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nemigo/webgpu",
|
|
3
|
+
"version": "1.5.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Vlad Logvin",
|
|
7
|
+
"email": "vlad.logvin84@gmail.com"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=22",
|
|
12
|
+
"pnpm": ">=10.9.0"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "svelte-package && rimraf .svelte-kit",
|
|
16
|
+
"check": "tsc --noemit",
|
|
17
|
+
"lint": "eslint ./",
|
|
18
|
+
"format": "prettier --write ./"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@nemigo/helpers": ">=1.5.1",
|
|
28
|
+
"@webgpu/types": ">=0.1.50"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@nemigo/configs": "workspace:*",
|
|
32
|
+
"@nemigo/helpers": "workspace:*"
|
|
33
|
+
}
|
|
34
|
+
}
|