gputex 0.1.2 → 0.3.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,141 @@
1
+ import { TextureHint, EncodeQuality, 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, RawPixelSource, SelectFormatOptions, WebGLBlockEncoder, WebGLCapabilities, WebGLEncodeBytesResult, WebGLEncoderConstructor, WebGLEncoderImageSource, WebGLEncoderOptions, WebGLFormatSelection, WebGPUFeature, createWebGLContext, detectCapabilities, detectWebGLCapabilities, generateMipChain, getSharedWebGLContext, isWebGLAvailable, padToBlockMultiple, selectFormat, selectWebGLFormat } from './index.js';
3
+ import { Texture, CompressedTexture, Loader, CompressedPixelFormat } from 'three';
4
+
5
+ /**
6
+ * Everything `compressTexture()` can take as an image source. A superset
7
+ * of `EncoderImageSource` (see Encoder.ts) that also accepts URL strings
8
+ * and Blob / File objects — the common cases in a web app.
9
+ */
10
+ type CompressTextureSource = string | Blob | File | ImageBitmap | HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageData;
11
+ interface CompressOptions {
12
+ /** How the texture will be used. Drives format selection. Default 'color'. */
13
+ hint?: TextureHint;
14
+ /** Pick the sRGB or linear variant of the chosen format. Default 'srgb'. */
15
+ colorSpace?: 'srgb' | 'linear';
16
+ /** Flip the image vertically before encoding. Default true (matches Three.js convention). */
17
+ flipY?: boolean;
18
+ /** Generate a full mip chain down to 1×1 on the CPU, encode every level. */
19
+ mipmaps?: boolean;
20
+ /**
21
+ * Encode quality / speed trade-off. 'fast' (default) is ~2–4× faster for a
22
+ * ≤0.36 dB PSNR cost; 'high' runs the exhaustive search (output identical to
23
+ * the CPU reference encoders; for BC1, a principal-axis seed + iterative
24
+ * refit). No effect on the WebGL fallback (which always uses the fast
25
+ * encoders).
26
+ */
27
+ quality?: EncodeQuality;
28
+ /** Reuse an existing device (e.g. Three.js's renderer device) instead
29
+ * of creating a new one. WebGPU path only. When provided, the encoder
30
+ * never destroys it. */
31
+ device?: GPUDevice;
32
+ adapter?: GPUAdapter;
33
+ }
34
+ interface CompressResult {
35
+ /** CompressedTexture on a compressed path; Texture on RGBA8 fallback. */
36
+ texture: Texture | CompressedTexture;
37
+ /** The compressed format selected, or null when we fell back to RGBA8. */
38
+ format: TextureFormat | null;
39
+ /** True iff we returned an uncompressed Texture because no encoder fit. */
40
+ fallbackUncompressed: boolean;
41
+ /**
42
+ * Which backend produced the result. 'webgpu' = compute path, 'webgl' =
43
+ * fragment-shader fallback, 'none' = uncompressed RGBA8.
44
+ */
45
+ backend: 'webgpu' | 'webgl' | 'none';
46
+ /**
47
+ * True iff the chosen format is ASTC and the hint was 'normal'. The
48
+ * caller must apply the (R, W) → (x, y) swizzle in the material — ASTC
49
+ * has no 2-channel mode, so normal maps ride the RGBA path.
50
+ */
51
+ astcNormalRemap: boolean;
52
+ width: number;
53
+ height: number;
54
+ mipLevels: number;
55
+ /** Wall-clock time of GPU encoding, summed across mip levels. */
56
+ encodeMs: number;
57
+ /** Release the encoder's internal GPU resources. No-op if `device` was
58
+ * passed in by the caller. */
59
+ destroy(): void;
60
+ }
61
+ declare function compressTexture(source: CompressTextureSource, options?: CompressOptions): Promise<CompressResult>;
62
+
63
+ declare class GputexLoader extends Loader<Texture> {
64
+ /** Format-selection hint. Default 'color'. */
65
+ hint: TextureHint;
66
+ /** Pick the sRGB or linear variant of the chosen format. Default 'srgb'. */
67
+ colorSpace: 'srgb' | 'linear';
68
+ /** Flip the image vertically before encoding. Default true (matches Three.js convention). */
69
+ flipY: boolean;
70
+ /** Generate + encode a full mip chain. Default false. */
71
+ mipmaps: boolean;
72
+ /** Encode quality / speed trade-off. Default 'fast' (~2–4× faster, ≤0.36 dB). */
73
+ quality: EncodeQuality;
74
+ /**
75
+ * Optional pre-existing WebGPU device. Reusing the renderer's device
76
+ * avoids spinning up a second WebGPU context for encoding.
77
+ */
78
+ device?: GPUDevice;
79
+ adapter?: GPUAdapter;
80
+ /**
81
+ * Most recent full encode result. Useful when the caller wants format
82
+ * / mipLevels / astcNormalRemap metadata without threading a separate
83
+ * callback through `load()`. Cleared when a new load starts.
84
+ */
85
+ lastResult: CompressResult | null;
86
+ /**
87
+ * THREE.Loader contract: returns void, drives callbacks. `loadAsync`
88
+ * (inherited from the base class) wraps this with Promise semantics.
89
+ * Errors routed through `manager.itemError` so the LoadingManager's
90
+ * aggregate state stays accurate.
91
+ */
92
+ load(url: string, onLoad?: (texture: Texture) => void, _onProgress?: (event: ProgressEvent) => void, onError?: (err: unknown) => void): void;
93
+ }
94
+
95
+ /** One encoded mip level. The fields both encoder backends already produce. */
96
+ interface EncodedLevel {
97
+ /** Logical (pre-padding) dimensions, surfaced on the texture's userData. */
98
+ width: number;
99
+ height: number;
100
+ /** Block-aligned dimensions the compressed `data` actually covers. */
101
+ paddedWidth: number;
102
+ paddedHeight: number;
103
+ data: Uint8Array;
104
+ }
105
+
106
+ /** The Three.js `CompressedPixelFormat` constant for a logical `TextureFormat`. */
107
+ declare function threeFormatFor(format: TextureFormat): CompressedPixelFormat;
108
+ /**
109
+ * Wrap pre-encoded mip levels into a `CompressedTexture` for the given logical
110
+ * format. `levels[0]` is the base level. The texture's colour space is taken
111
+ * from the format variant (sRGB families tag sRGB, everything else linear), so
112
+ * no separate colour-space argument is needed — pick the format variant you
113
+ * want (e.g. `BC7_SRGB` vs `BC7`).
114
+ */
115
+ declare function buildCompressedTexture(levels: readonly EncodedLevel[], format: TextureFormat): CompressedTexture;
116
+ interface EncodeResult {
117
+ width: number;
118
+ height: number;
119
+ paddedWidth: number;
120
+ paddedHeight: number;
121
+ data: Uint8Array;
122
+ texture: CompressedTexture;
123
+ encodeMs: number;
124
+ }
125
+ interface EncodeToTextureOptions {
126
+ /** Pick the sRGB or linear variant of the encoder's format. Default 'srgb'. */
127
+ colorSpace?: 'srgb' | 'linear';
128
+ /** Encode quality / speed trade-off. Default 'fast'. */
129
+ quality?: EncodeQuality;
130
+ /** Flip the image vertically before encoding. Default false. */
131
+ flipY?: boolean;
132
+ }
133
+ /**
134
+ * One-shot: encode a single image with a WebGPU `Encoder` straight to a
135
+ * `CompressedTexture`, plus the raw byte metadata. Convenience wrapper over
136
+ * `encoder.encodeToBytes()` + `buildCompressedTexture()` for callers that want
137
+ * a ready-to-use Three.js texture from one call.
138
+ */
139
+ declare function encodeToTexture(encoder: Encoder, source: EncoderImageSource, { colorSpace, quality, flipY }?: EncodeToTextureOptions): Promise<EncodeResult>;
140
+
141
+ export { type CompressOptions, type CompressResult, type CompressTextureSource, EncodeQuality, type EncodeResult, type EncodeToTextureOptions, Encoder, EncoderImageSource, GputexLoader, TextureFormat, TextureHint, buildCompressedTexture, compressTexture, encodeToTexture, threeFormatFor };