@ludicon/spark.js 0.0.1 → 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 CHANGED
@@ -1,21 +1,31 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Ludicon LLC
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+
2
+ This repository contains both open-source and proprietary components.
3
+
4
+ The use of the *Spark* shaders is covered under the spark.js EULA:
5
+
6
+ https://ludicon.com/sparkjs/eula.html
7
+
8
+
9
+ The JavaScript code is released under MIT license:
10
+
11
+ MIT License
12
+
13
+ Copyright (c) 2025 Ludicon LLC
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in all
23
+ copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # spark.js⚡️
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@ludicon/spark.js.svg)](https://www.npmjs.com/package/@ludicon/spark.js) [![License: MIT](https://img.shields.io/badge/license-MIT-yellow.svg)](./LICENSE) [![WebGPU](https://img.shields.io/badge/WebGPU-supported-green.svg)](https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API)
3
+ [![npm version](https://img.shields.io/npm/v/@ludicon/spark.js.svg)](https://www.npmjs.com/package/@ludicon/spark.js) [![WebGPU](https://img.shields.io/badge/WebGPU-supported-green.svg)](https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API)
4
4
 
5
5
  Real-time texture compression library for the Web.
6
6
 
@@ -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 Spark instance for the WebGPU device
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
@@ -72,7 +72,7 @@ And visit `https://localhost:5174/examples/basic.html`.
72
72
 
73
73
  ### `encodeTexture(source, options)`
74
74
 
75
- Load an image and transcode it to a compressed GPU texture.
75
+ Load an image and encode it to a compressed GPU texture.
76
76
 
77
77
  #### Parameters
78
78
 
@@ -93,7 +93,7 @@ Load an image and transcode it to a compressed GPU texture.
93
93
  Whether to encode the image using an as sRGB format. This also affects mipmap generation. The `srgb` mode can also be inferred from the `format`. Default: `false`.
94
94
 
95
95
  - **`normal`** (`boolean`)
96
- Whether to interpret the image as a normal map. This affects automatic format selection favoring the use of `"bc5"` and `"etc-rg"` formats. Default: `false`.
96
+ Whether to interpret the image as a normal map. This affects automatic format selection favoring the use of `"bc5"` and `"eac-rg"` formats. Default: `false`.
97
97
 
98
98
  - **`flipY`** (`boolean`)
99
99
  Whether to vertically flip the image before encoding. Default: `false`.
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
- await Promise.all(
4
- Object.entries(modules).map(async function([path, module]) {
5
- const { default: shader } = await module(), name = path.replace("./", "");
6
- return [name, shader];
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,15 +809,19 @@ class Spark {
808
809
  }
809
810
  this.#supportsFloat16 = this.#device.features.has("shader-f16");
810
811
  await this.#loadUtilPipelines();
811
- for (const format of this.#supportedFormats) {
812
- if (!this.#pipelines[format]) {
813
- await this.#loadPipeline(format);
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
+ }
814
819
  }
815
820
  }
816
821
  }
817
822
  async #loadUtilPipelines() {
818
823
  const shaderModule = this.#device.createShaderModule({
819
- code: shaders["utils.wgsl"],
824
+ code: await shaders["utils.wgsl"](),
820
825
  label: "utils"
821
826
  });
822
827
  if (typeof shaderModule.compilationInfo == "function") {
@@ -865,7 +870,7 @@ class Spark {
865
870
  const pipelinePromise = (async () => {
866
871
  const shaderFile = SparkShaderFiles[format];
867
872
  assert(shaderFile, `No shader available for format ${SparkFormatName[format]}`);
868
- let shaderCode = shaders[shaderFile];
873
+ let shaderCode = await shaders[shaderFile]();
869
874
  if (!this.#supportsFloat16) {
870
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");
871
876
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ludicon/spark.js",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Real-Time GPU Texture Codecs for the Web",
5
5
  "main": "dist/index.esm.js",
6
6
  "module": "dist/index.esm.js",
@@ -47,7 +47,7 @@
47
47
  "spark"
48
48
  ],
49
49
  "author": "Ignacio Castano <castano@ludicon.com> (https://ludicon.com/)",
50
- "license": "MIT",
50
+ "license": "See LICENSE",
51
51
  "repository": {
52
52
  "type": "git",
53
53
  "url": "git+https://github.com/ludicon/spark.js.git"