gputex 0.4.0 → 0.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/three.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { TextureHint, PreferredFormat, SvgRasterSize, TextureFormat, Encoder, EncoderImageSource } from './index.js';
2
- export { ASTC4x4Encoder, ASTC4x4WebGLEncoder, BC1Encoder, BC1WebGLEncoder, BC5Encoder, BC5WebGLEncoder, BC7Encoder, BC7WebGLEncoder, Capabilities, EncodeBytesResult, EncodeCallOptions, EncoderConstructor, EncoderOptions, ExtensionProvider, FeatureProvider, FormatSelection, FormatVariant, MipLevel, RasterizeSvgOptions, RawPixelSource, SelectFormatOptions, WebGLBlockEncoder, WebGLCapabilities, WebGLEncodeBytesResult, WebGLEncoderConstructor, WebGLEncoderImageSource, WebGLEncoderOptions, WebGLFormatSelection, WebGPUFeature, createWebGLContext, detectCapabilities, detectWebGLCapabilities, generateMipChain, getSharedWebGLContext, isWebGLAvailable, padToBlockMultiple, rasterizeSvg, selectFormat, selectWebGLFormat } from './index.js';
1
+ import { TextureHint, PreferredFormat, FormatQuality, SvgRasterSize, TextureFormat, Encoder, EncoderImageSource } from './index.js';
2
+ export { ASTC4x4Encoder, ASTC4x4WebGLEncoder, BC1Encoder, BC1WebGLEncoder, BC5Encoder, BC5WebGLEncoder, BC7Encoder, BC7EncoderOptions, BC7WebGLEncoder, Capabilities, ETC2Encoder, EncodeBytesResult, EncodeCallOptions, EncodeMipChainResult, EncodedLevelBytes, EncoderConstructor, EncoderOptions, ExtensionProvider, FeatureProvider, FormatSelection, FormatVariant, MipLevel, RasterizeSvgOptions, RawPixelSource, SelectFormatOptions, WebGLBlockEncoder, WebGLCapabilities, WebGLEncodeBytesResult, WebGLEncoderConstructor, WebGLEncoderImageSource, WebGLEncoderOptions, WebGLFormatSelection, WebGPUFeature, createWebGLContext, detectCapabilities, detectWebGLCapabilities, generateGpuMipChain, generateMipChain, getSharedWebGLContext, gpuMipLevelCount, isWebGLAvailable, padToBlockMultiple, rasterizeSvg, selectFormat, selectWebGLFormat } from './index.js';
3
3
  import { Texture, CompressedTexture, Loader, CompressedPixelFormat } from 'three';
4
4
 
5
5
  /**
@@ -18,12 +18,22 @@ interface CompressOptions {
18
18
  hint?: TextureHint;
19
19
  /**
20
20
  * Prefer a specific format over the default choice when the device
21
- * supports it; falls back to the normal selection (BC7 → ASTC → RGBA8)
22
- * when it doesn't. Currently only 'bc1': half the memory of BC7 for
23
- * opaque colour textures, at lower quality. Only honoured with
21
+ * supports it; falls back to the normal selection (BC7 → ASTC → ETC2 →
22
+ * RGBA8) when it doesn't. Currently only 'bc1': half the memory of BC7
23
+ * for opaque colour textures, at lower quality. Only honoured with
24
24
  * `hint: 'color'` — BC1 can't carry real alpha or normal maps.
25
25
  */
26
26
  preferredFormat?: PreferredFormat;
27
+ /**
28
+ * Memory/fidelity trade-off for opaque colour textures. Default 'high'
29
+ * (BC7 / ASTC 4×4, 1 byte/pixel). 'low' picks the 4-bpp formats when the
30
+ * device has one — BC1 on desktop-class GPUs, ETC2 RGB8 on mobile-class
31
+ * ones — halving GPU memory at visibly lower quality on smooth content.
32
+ * Ignored for `hint: 'colorWithAlpha'` and `hint: 'normal'` (the 4-bpp
33
+ * formats can't carry them). On the WebGL fallback tier only BC1 is
34
+ * available at 'low'.
35
+ */
36
+ quality?: FormatQuality;
27
37
  /** Pick the sRGB or linear variant of the chosen format. Default 'srgb'. */
28
38
  colorSpace?: 'srgb' | 'linear';
29
39
  /**
@@ -43,6 +53,26 @@ interface CompressOptions {
43
53
  * never destroys it. */
44
54
  device?: GPUDevice;
45
55
  adapter?: GPUAdapter;
56
+ /**
57
+ * Keep the compressed bytes in a session-scoped in-memory LRU and reuse
58
+ * them on repeat calls, skipping BOTH the image decode and the encode —
59
+ * the dominant costs. Re-loading a texture later in the session (e.g.
60
+ * two worlds sharing an atlas) becomes a few ms. Keyed by source
61
+ * identity + selected format + encode options; capped at 256 MiB of
62
+ * compressed bytes by default (`setTranscodeCacheLimit()` to tune) and
63
+ * never touches persistent storage. Default false.
64
+ *
65
+ * URL and Blob/File sources get an identity automatically (URL string or
66
+ * content hash). Pixel sources (ImageBitmap, canvas, ImageData) are only
67
+ * cached when `cacheKey` is provided.
68
+ */
69
+ cache?: boolean;
70
+ /**
71
+ * Explicit cache identity for the source, overriding the derived one.
72
+ * Use when you already know a stable name (e.g. an asset path) and want
73
+ * to skip content hashing, or to make pixel sources cacheable.
74
+ */
75
+ cacheKey?: string;
46
76
  }
47
77
  interface CompressResult {
48
78
  /** CompressedTexture on a compressed path; Texture on RGBA8 fallback. */
@@ -67,12 +97,43 @@ interface CompressResult {
67
97
  mipLevels: number;
68
98
  /** Wall-clock time of GPU encoding, summed across mip levels. */
69
99
  encodeMs: number;
70
- /** Release the encoder's internal GPU resources. No-op if `device` was
71
- * passed in by the caller. */
100
+ /**
101
+ * Wall-clock time to turn the source into decoded RGBA pixels: fetch /
102
+ * base64 decode, image decode, SVG rasterisation. Usually the dominant
103
+ * cost for large images — when a load feels slower than `encodeMs`
104
+ * suggests, this is where the time went.
105
+ */
106
+ decodeMs: number;
107
+ /** Wall-clock time of the whole `compressTexture()` call: decode + CPU
108
+ * mip generation + encode + texture assembly. */
109
+ totalMs: number;
110
+ /** True when the result came from the in-memory transcode cache (the
111
+ * `cache` option) — no decode or encode ran; decodeMs/encodeMs are 0. */
112
+ cacheHit: boolean;
113
+ /** Dispose the texture and release GPU resources owned by this call.
114
+ * On the default path (no `device`/`adapter` option) the encoder and
115
+ * device are shared across `compressTexture()` calls and survive this —
116
+ * release those with `releaseSharedGpuResources()`. */
72
117
  destroy(): void;
73
118
  }
119
+ /**
120
+ * Destroy the WebGPU device and encoders that `compressTexture()` shares
121
+ * across calls (created lazily when neither the `device` nor the `adapter`
122
+ * option is passed). Safe to call at any time — in-flight encodes on the
123
+ * shared device will fail, and the next `compressTexture()` call recreates
124
+ * everything. No-op when nothing is cached.
125
+ */
126
+ declare function releaseSharedGpuResources(): void;
74
127
  declare function compressTexture(source: CompressTextureSource, options?: CompressOptions): Promise<CompressResult>;
75
128
 
129
+ /**
130
+ * Cap the cache's total compressed payload in bytes (default 256 MiB).
131
+ * Lower it to evict immediately; 0 disables caching entirely.
132
+ */
133
+ declare function setTranscodeCacheLimit(bytes: number): void;
134
+ /** Drop every cached transcode. Textures already built from entries are unaffected. */
135
+ declare function clearTranscodeCache(): void;
136
+
76
137
  declare class GputexLoader extends Loader<Texture> {
77
138
  /** Format-selection hint. Default 'color'. */
78
139
  hint: TextureHint;
@@ -82,6 +143,12 @@ declare class GputexLoader extends Loader<Texture> {
82
143
  * `CompressOptions.preferredFormat`.
83
144
  */
84
145
  preferredFormat?: PreferredFormat;
146
+ /**
147
+ * Memory/fidelity trade-off: 'high' (default, BC7 / ASTC) or 'low'
148
+ * (BC1 / ETC2 RGB8 at half the memory, opaque colour only). See
149
+ * `CompressOptions.quality`.
150
+ */
151
+ quality: FormatQuality;
85
152
  /** Pick the sRGB or linear variant of the chosen format. Default 'srgb'. */
86
153
  colorSpace: 'srgb' | 'linear';
87
154
  /**
@@ -94,6 +161,10 @@ declare class GputexLoader extends Loader<Texture> {
94
161
  flipY: boolean;
95
162
  /** Generate + encode a full mip chain. Default false. */
96
163
  mipmaps: boolean;
164
+ /** Reuse compressed bytes from the session's in-memory transcode cache,
165
+ * skipping decode + encode on repeat loads. See `CompressOptions.cache`.
166
+ * Default false. */
167
+ cache: boolean;
97
168
  /**
98
169
  * Optional pre-existing WebGPU device. Reusing the renderer's device
99
170
  * avoids spinning up a second WebGPU context for encoding.
@@ -159,4 +230,4 @@ interface EncodeToTextureOptions {
159
230
  */
160
231
  declare function encodeToTexture(encoder: Encoder, source: EncoderImageSource, { colorSpace, flipY }?: EncodeToTextureOptions): Promise<EncodeResult>;
161
232
 
162
- export { type CompressOptions, type CompressResult, type CompressTextureSource, type EncodeResult, type EncodeToTextureOptions, Encoder, EncoderImageSource, GputexLoader, PreferredFormat, SvgRasterSize, TextureFormat, TextureHint, buildCompressedTexture, compressTexture, encodeToTexture, threeFormatFor };
233
+ export { type CompressOptions, type CompressResult, type CompressTextureSource, type EncodeResult, type EncodeToTextureOptions, Encoder, EncoderImageSource, FormatQuality, GputexLoader, PreferredFormat, SvgRasterSize, TextureFormat, TextureHint, buildCompressedTexture, clearTranscodeCache, compressTexture, encodeToTexture, releaseSharedGpuResources, setTranscodeCacheLimit, threeFormatFor };