@ludicon/spark.js 0.0.2 → 0.0.3
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/LICENSE +0 -0
- package/README.md +2 -2
- package/dist/index.esm.js +20 -17
- package/dist/spark_astc_rgb-ylbf30mQ.js +0 -0
- package/dist/spark_astc_rgba-C4NuyfHw.js +0 -0
- package/dist/spark_bc1_rgb-CRQwJRCp.js +0 -0
- package/dist/spark_bc3_rgba-CyRcvC8t.js +0 -0
- package/dist/spark_bc4_r-BSB9VB_w.js +0 -0
- package/dist/spark_bc5_rg-NX_OBH9I.js +0 -0
- package/dist/spark_bc7_rgb-CYdL55pE.js +0 -0
- package/dist/spark_bc7_rgba-BFgOyqos.js +0 -0
- package/dist/spark_eac_r-BFwH430b.js +0 -0
- package/dist/spark_eac_rg--Gm5Gzmk.js +0 -0
- package/dist/spark_etc2_rgb-CWjBHhHQ.js +0 -0
- package/dist/spark_etc2_rgba-BRX5DwNI.js +0 -0
- package/dist/utils-BybjJ-PV.js +0 -0
- package/package.json +1 -1
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -21,14 +21,14 @@ npm install @ludicon/spark.js
|
|
|
21
21
|
## Usage Example
|
|
22
22
|
|
|
23
23
|
```js
|
|
24
|
-
import { Spark } from "spark.js"
|
|
24
|
+
import { Spark } from "@ludicon/spark.js"
|
|
25
25
|
|
|
26
26
|
// Initialize a WebGPU device with required features
|
|
27
27
|
const adapter = await navigator.gpu.requestAdapter()
|
|
28
28
|
const requiredFeatures = Spark.getRequiredFeatures(adapter)
|
|
29
29
|
const device = await adapter.requestDevice({ requiredFeatures })
|
|
30
30
|
|
|
31
|
-
// Create
|
|
31
|
+
// Create spark instance for the WebGPU device
|
|
32
32
|
const spark = await Spark.create(device)
|
|
33
33
|
|
|
34
34
|
// Load and encode an image into a GPU texture
|
package/dist/index.esm.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
const modules = /* @__PURE__ */ Object.assign({ "./spark_astc_rgb.wgsl": () => import("./spark_astc_rgb-ylbf30mQ.js"), "./spark_astc_rgba.wgsl": () => import("./spark_astc_rgba-C4NuyfHw.js"), "./spark_bc1_rgb.wgsl": () => import("./spark_bc1_rgb-CRQwJRCp.js"), "./spark_bc3_rgba.wgsl": () => import("./spark_bc3_rgba-CyRcvC8t.js"), "./spark_bc4_r.wgsl": () => import("./spark_bc4_r-BSB9VB_w.js"), "./spark_bc5_rg.wgsl": () => import("./spark_bc5_rg-NX_OBH9I.js"), "./spark_bc7_rgb.wgsl": () => import("./spark_bc7_rgb-CYdL55pE.js"), "./spark_bc7_rgba.wgsl": () => import("./spark_bc7_rgba-BFgOyqos.js"), "./spark_eac_r.wgsl": () => import("./spark_eac_r-BFwH430b.js"), "./spark_eac_rg.wgsl": () => import("./spark_eac_rg--Gm5Gzmk.js"), "./spark_etc2_rgb.wgsl": () => import("./spark_etc2_rgb-CWjBHhHQ.js"), "./spark_etc2_rgba.wgsl": () => import("./spark_etc2_rgba-BRX5DwNI.js"), "./utils.wgsl": () => import("./utils-BybjJ-PV.js") });
|
|
2
2
|
const shaders = Object.fromEntries(
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
)
|
|
3
|
+
Object.entries(modules).map(([path, module]) => {
|
|
4
|
+
const name = path.replace("./", "");
|
|
5
|
+
const fn = async () => (await module()).default;
|
|
6
|
+
return [name, fn];
|
|
7
|
+
})
|
|
9
8
|
);
|
|
10
9
|
const SparkFormat = {
|
|
11
10
|
ASTC_4x4_RGB: 0,
|
|
@@ -461,11 +460,13 @@ class Spark {
|
|
|
461
460
|
/**
|
|
462
461
|
* Initialize the encoder by detecting available compression formats.
|
|
463
462
|
* @param {GPUDevice} device - WebGPU device.
|
|
463
|
+
* @param {Object} options - Encoder options.
|
|
464
|
+
* @param {boolean} options.preload - Whether to preload all encoder pipelines (false by default).
|
|
464
465
|
* @returns {Promise<void>} Resolves when initialization is complete.
|
|
465
466
|
*/
|
|
466
|
-
static async create(device) {
|
|
467
|
+
static async create(device, options = {}) {
|
|
467
468
|
const instance = new Spark();
|
|
468
|
-
await instance.#init(device);
|
|
469
|
+
await instance.#init(device, options.preload ?? false);
|
|
469
470
|
return instance;
|
|
470
471
|
}
|
|
471
472
|
/**
|
|
@@ -757,7 +758,7 @@ class Spark {
|
|
|
757
758
|
}
|
|
758
759
|
const commandEncoder = this.#device.createCommandEncoder();
|
|
759
760
|
commandEncoder.resolveQuerySet(this.#querySet, 0, 2, this.#queryBuffer, 0);
|
|
760
|
-
commandEncoder.copyBufferToBuffer(this.#queryBuffer, this.#queryReadbackBuffer, 16);
|
|
761
|
+
commandEncoder.copyBufferToBuffer(this.#queryBuffer, 0, this.#queryReadbackBuffer, 0, 16);
|
|
761
762
|
this.#device.queue.submit([commandEncoder.finish()]);
|
|
762
763
|
await this.#device.queue.onSubmittedWorkDone();
|
|
763
764
|
await this.#queryReadbackBuffer.mapAsync(GPUMapMode.READ);
|
|
@@ -770,7 +771,7 @@ class Spark {
|
|
|
770
771
|
const elapsedMilliseconds = elapsedNanoseconds / 1e6;
|
|
771
772
|
return elapsedMilliseconds;
|
|
772
773
|
}
|
|
773
|
-
async #init(device) {
|
|
774
|
+
async #init(device, preload) {
|
|
774
775
|
assert(device, "device is required");
|
|
775
776
|
assert(isWebGPU(device), "device is not a WebGPU device");
|
|
776
777
|
this.#device = device;
|
|
@@ -808,17 +809,19 @@ class Spark {
|
|
|
808
809
|
}
|
|
809
810
|
this.#supportsFloat16 = this.#device.features.has("shader-f16");
|
|
810
811
|
await this.#loadUtilPipelines();
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
this.#
|
|
814
|
-
|
|
815
|
-
|
|
812
|
+
if (preload) {
|
|
813
|
+
for (const format of this.#supportedFormats) {
|
|
814
|
+
if (!this.#pipelines[format]) {
|
|
815
|
+
this.#loadPipeline(format).catch((err) => {
|
|
816
|
+
console.error(`Failed to preload pipeline for format ${format}:`, err);
|
|
817
|
+
});
|
|
818
|
+
}
|
|
816
819
|
}
|
|
817
820
|
}
|
|
818
821
|
}
|
|
819
822
|
async #loadUtilPipelines() {
|
|
820
823
|
const shaderModule = this.#device.createShaderModule({
|
|
821
|
-
code: shaders["utils.wgsl"],
|
|
824
|
+
code: await shaders["utils.wgsl"](),
|
|
822
825
|
label: "utils"
|
|
823
826
|
});
|
|
824
827
|
if (typeof shaderModule.compilationInfo == "function") {
|
|
@@ -867,7 +870,7 @@ class Spark {
|
|
|
867
870
|
const pipelinePromise = (async () => {
|
|
868
871
|
const shaderFile = SparkShaderFiles[format];
|
|
869
872
|
assert(shaderFile, `No shader available for format ${SparkFormatName[format]}`);
|
|
870
|
-
let shaderCode = shaders[shaderFile];
|
|
873
|
+
let shaderCode = await shaders[shaderFile]();
|
|
871
874
|
if (!this.#supportsFloat16) {
|
|
872
875
|
shaderCode = shaderCode.replace(/^enable f16;\s*/m, "").replace(/\bf16\b/g, "f32").replace(/\bvec([234])h\b/g, "vec$1f").replace(/\bmat([234]x[234])h/g, "mat$1f").replace(/\b(\d*\.\d+|\d+\.)h\b/g, "$1");
|
|
873
876
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/dist/utils-BybjJ-PV.js
CHANGED
|
File without changes
|