@luma.gl/core 9.2.0-alpha.1 → 9.2.0-alpha.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/dist/adapter/canvas-context.d.ts +17 -3
- package/dist/adapter/canvas-context.d.ts.map +1 -1
- package/dist/adapter/canvas-context.js +48 -2
- package/dist/adapter/canvas-context.js.map +1 -1
- package/dist/adapter/device.d.ts +56 -11
- package/dist/adapter/device.d.ts.map +1 -1
- package/dist/adapter/device.js +78 -22
- package/dist/adapter/device.js.map +1 -1
- package/dist/adapter/luma.js +1 -1
- package/dist/adapter/resources/buffer.d.ts +13 -7
- package/dist/adapter/resources/buffer.d.ts.map +1 -1
- package/dist/adapter/resources/buffer.js +1 -5
- package/dist/adapter/resources/buffer.js.map +1 -1
- package/dist/adapter/resources/command-encoder.d.ts +0 -1
- package/dist/adapter/resources/command-encoder.d.ts.map +1 -1
- package/dist/adapter/resources/command-encoder.js.map +1 -1
- package/dist/adapter/resources/framebuffer.d.ts +3 -3
- package/dist/adapter/resources/framebuffer.d.ts.map +1 -1
- package/dist/adapter/resources/pipeline-layout.d.ts +13 -0
- package/dist/adapter/resources/pipeline-layout.d.ts.map +1 -0
- package/dist/adapter/resources/pipeline-layout.js +21 -0
- package/dist/adapter/resources/pipeline-layout.js.map +1 -0
- package/dist/adapter/resources/render-pipeline.d.ts +4 -4
- package/dist/adapter/resources/render-pipeline.d.ts.map +1 -1
- package/dist/adapter/resources/resource.d.ts +1 -0
- package/dist/adapter/resources/resource.d.ts.map +1 -1
- package/dist/adapter/resources/resource.js.map +1 -1
- package/dist/adapter/resources/texture-view.d.ts +1 -1
- package/dist/adapter/resources/texture-view.d.ts.map +1 -1
- package/dist/adapter/resources/texture.d.ts +1 -1
- package/dist/adapter/resources/texture.d.ts.map +1 -1
- package/dist/adapter/resources/vertex-array.js +1 -1
- package/dist/adapter/resources/vertex-array.js.map +1 -1
- package/dist/adapter/types/attachments.d.ts +3 -3
- package/dist/adapter/types/attachments.d.ts.map +1 -1
- package/dist/adapter/types/buffer-layout.d.ts +1 -1
- package/dist/adapter/types/buffer-layout.d.ts.map +1 -1
- package/dist/adapter/types/parameters.d.ts +2 -2
- package/dist/adapter/types/parameters.d.ts.map +1 -1
- package/dist/adapter/types/shader-layout.d.ts +5 -6
- package/dist/adapter/types/shader-layout.d.ts.map +1 -1
- package/dist/adapter-utils/get-attribute-from-layouts.d.ts +3 -3
- package/dist/adapter-utils/get-attribute-from-layouts.d.ts.map +1 -1
- package/dist/adapter-utils/get-attribute-from-layouts.js +2 -2
- package/dist/adapter-utils/get-attribute-from-layouts.js.map +1 -1
- package/dist/dist.dev.js +417 -267
- package/dist/dist.min.js +5 -4
- package/dist/index.cjs +416 -266
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +18 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -7
- package/dist/index.js.map +1 -1
- package/dist/portable/uniform-block.d.ts +1 -1
- package/dist/portable/uniform-block.d.ts.map +1 -1
- package/dist/portable/uniform-buffer-layout.d.ts +2 -2
- package/dist/portable/uniform-buffer-layout.d.ts.map +1 -1
- package/dist/portable/uniform-buffer-layout.js +2 -2
- package/dist/portable/uniform-buffer-layout.js.map +1 -1
- package/dist/portable/uniform-store.d.ts +1 -1
- package/dist/portable/uniform-store.d.ts.map +1 -1
- package/dist/shadertypes/{data-types.d.ts → data-types/data-types.d.ts} +4 -0
- package/dist/shadertypes/data-types/data-types.d.ts.map +1 -0
- package/dist/shadertypes/data-types/data-types.js.map +1 -0
- package/dist/shadertypes/{utils → data-types}/decode-data-types.d.ts +4 -4
- package/dist/shadertypes/data-types/decode-data-types.d.ts.map +1 -0
- package/dist/shadertypes/data-types/decode-data-types.js +74 -0
- package/dist/shadertypes/data-types/decode-data-types.js.map +1 -0
- package/dist/shadertypes/{utils → data-types}/decode-shader-types.d.ts +2 -2
- package/dist/shadertypes/data-types/decode-shader-types.d.ts.map +1 -0
- package/dist/shadertypes/data-types/decode-shader-types.js.map +1 -0
- package/dist/shadertypes/data-types/shader-types.d.ts.map +1 -0
- package/dist/shadertypes/data-types/shader-types.js.map +1 -0
- package/dist/shadertypes/textures/pixel-utils.d.ts +112 -0
- package/dist/shadertypes/textures/pixel-utils.d.ts.map +1 -0
- package/dist/shadertypes/textures/pixel-utils.js +193 -0
- package/dist/shadertypes/textures/pixel-utils.js.map +1 -0
- package/dist/shadertypes/textures/texture-format-decoder.d.ts +18 -0
- package/dist/shadertypes/textures/texture-format-decoder.d.ts.map +1 -0
- package/dist/shadertypes/{utils/decode-texture-format.js → textures/texture-format-decoder.js} +48 -35
- package/dist/shadertypes/textures/texture-format-decoder.js.map +1 -0
- package/dist/shadertypes/textures/texture-format-generics.d.ts +33 -0
- package/dist/shadertypes/textures/texture-format-generics.d.ts.map +1 -0
- package/dist/shadertypes/{texture-formats.js → textures/texture-format-generics.js} +1 -1
- package/dist/shadertypes/textures/texture-format-generics.js.map +1 -0
- package/dist/shadertypes/{utils → textures}/texture-format-table.d.ts +2 -1
- package/dist/shadertypes/textures/texture-format-table.d.ts.map +1 -0
- package/dist/shadertypes/{utils → textures}/texture-format-table.js +41 -42
- package/dist/shadertypes/textures/texture-format-table.js.map +1 -0
- package/dist/shadertypes/textures/texture-formats.d.ts +90 -0
- package/dist/shadertypes/textures/texture-formats.d.ts.map +1 -0
- package/dist/shadertypes/textures/texture-formats.js +58 -0
- package/dist/shadertypes/textures/texture-formats.js.map +1 -0
- package/dist/shadertypes/{utils → vertex-arrays}/decode-vertex-format.d.ts +2 -2
- package/dist/shadertypes/vertex-arrays/decode-vertex-format.d.ts.map +1 -0
- package/dist/shadertypes/{utils → vertex-arrays}/decode-vertex-format.js +4 -4
- package/dist/shadertypes/vertex-arrays/decode-vertex-format.js.map +1 -0
- package/dist/shadertypes/{vertex-formats.d.ts → vertex-arrays/vertex-formats.d.ts} +5 -3
- package/dist/shadertypes/vertex-arrays/vertex-formats.d.ts.map +1 -0
- package/dist/shadertypes/vertex-arrays/vertex-formats.js.map +1 -0
- package/package.json +2 -2
- package/src/adapter/canvas-context.ts +60 -5
- package/src/adapter/device.ts +132 -47
- package/src/adapter/resources/buffer.ts +30 -14
- package/src/adapter/resources/command-encoder.ts +0 -2
- package/src/adapter/resources/framebuffer.ts +5 -5
- package/src/adapter/resources/pipeline-layout.ts +30 -0
- package/src/adapter/resources/render-pipeline.ts +6 -6
- package/src/adapter/resources/resource.ts +1 -0
- package/src/adapter/resources/texture-view.ts +1 -1
- package/src/adapter/resources/texture.ts +1 -1
- package/src/adapter/resources/vertex-array.ts +1 -1
- package/src/adapter/types/attachments.ts +5 -5
- package/src/adapter/types/buffer-layout.ts +1 -1
- package/src/adapter/types/parameters.ts +2 -2
- package/src/adapter/types/shader-layout.ts +8 -5
- package/src/adapter-utils/get-attribute-from-layouts.ts +5 -5
- package/src/index.ts +60 -36
- package/src/portable/uniform-block.ts +1 -1
- package/src/portable/uniform-buffer-layout.ts +4 -4
- package/src/portable/uniform-store.ts +1 -1
- package/src/shadertypes/data-types/data-types.ts +96 -0
- package/src/shadertypes/data-types/decode-data-types.ts +91 -0
- package/src/shadertypes/{utils → data-types}/decode-shader-types.ts +2 -2
- package/src/shadertypes/textures/pixel-utils.ts +239 -0
- package/src/shadertypes/{utils/decode-texture-format.ts → textures/texture-format-decoder.ts} +63 -47
- package/src/shadertypes/textures/texture-format-generics.ts +190 -0
- package/src/shadertypes/{utils → textures}/texture-format-table.ts +62 -52
- package/src/shadertypes/{texture-formats.ts → textures/texture-formats.ts} +229 -147
- package/src/shadertypes/{utils → vertex-arrays}/decode-vertex-format.ts +6 -10
- package/src/shadertypes/{vertex-formats.ts → vertex-arrays/vertex-formats.ts} +20 -6
- package/dist/adapter-utils/buffer-layout-helper.d.ts +0 -11
- package/dist/adapter-utils/buffer-layout-helper.d.ts.map +0 -1
- package/dist/adapter-utils/buffer-layout-helper.js +0 -33
- package/dist/adapter-utils/buffer-layout-helper.js.map +0 -1
- package/dist/shadertypes/data-types.d.ts.map +0 -1
- package/dist/shadertypes/data-types.js.map +0 -1
- package/dist/shadertypes/shader-types.d.ts.map +0 -1
- package/dist/shadertypes/shader-types.js.map +0 -1
- package/dist/shadertypes/texture-formats.d.ts +0 -74
- package/dist/shadertypes/texture-formats.d.ts.map +0 -1
- package/dist/shadertypes/texture-formats.js.map +0 -1
- package/dist/shadertypes/utils/decode-data-types.d.ts.map +0 -1
- package/dist/shadertypes/utils/decode-data-types.js +0 -114
- package/dist/shadertypes/utils/decode-data-types.js.map +0 -1
- package/dist/shadertypes/utils/decode-shader-types.d.ts.map +0 -1
- package/dist/shadertypes/utils/decode-shader-types.js.map +0 -1
- package/dist/shadertypes/utils/decode-texture-format.d.ts +0 -15
- package/dist/shadertypes/utils/decode-texture-format.d.ts.map +0 -1
- package/dist/shadertypes/utils/decode-texture-format.js.map +0 -1
- package/dist/shadertypes/utils/decode-vertex-format.d.ts.map +0 -1
- package/dist/shadertypes/utils/decode-vertex-format.js.map +0 -1
- package/dist/shadertypes/utils/texture-format-table.d.ts.map +0 -1
- package/dist/shadertypes/utils/texture-format-table.js.map +0 -1
- package/dist/shadertypes/vertex-formats.d.ts.map +0 -1
- package/dist/shadertypes/vertex-formats.js.map +0 -1
- package/src/adapter-utils/buffer-layout-helper.ts +0 -41
- package/src/shadertypes/data-types.ts +0 -42
- package/src/shadertypes/utils/decode-data-types.ts +0 -131
- /package/dist/shadertypes/{data-types.js → data-types/data-types.js} +0 -0
- /package/dist/shadertypes/{utils → data-types}/decode-shader-types.js +0 -0
- /package/dist/shadertypes/{shader-types.d.ts → data-types/shader-types.d.ts} +0 -0
- /package/dist/shadertypes/{shader-types.js → data-types/shader-types.js} +0 -0
- /package/dist/shadertypes/{vertex-formats.js → vertex-arrays/vertex-formats.js} +0 -0
- /package/src/shadertypes/{shader-types.ts → data-types/shader-types.ts} +0 -0
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
5
|
import {log} from '../utils/log';
|
|
6
|
-
import type {PrimitiveDataType, NormalizedDataType} from '../shadertypes/data-types';
|
|
7
|
-
import type {AttributeShaderType} from '../shadertypes/shader-types';
|
|
8
|
-
import type {VertexFormat} from '../shadertypes/vertex-formats';
|
|
9
|
-
import {getAttributeShaderTypeInfo} from '../shadertypes/
|
|
6
|
+
import type {PrimitiveDataType, NormalizedDataType} from '../shadertypes/data-types/data-types';
|
|
7
|
+
import type {AttributeShaderType} from '../shadertypes/data-types/shader-types';
|
|
8
|
+
import type {VertexFormat} from '../shadertypes/vertex-arrays/vertex-formats';
|
|
9
|
+
import {getAttributeShaderTypeInfo} from '../shadertypes/data-types/decode-shader-types';
|
|
10
10
|
import {
|
|
11
11
|
getVertexFormatInfo,
|
|
12
12
|
getCompatibleVertexFormat
|
|
13
|
-
} from '../shadertypes/
|
|
13
|
+
} from '../shadertypes/vertex-arrays/decode-vertex-format';
|
|
14
14
|
import type {ShaderLayout, AttributeDeclaration} from '../adapter/types/shader-layout';
|
|
15
15
|
import type {BufferLayout} from '../adapter/types/buffer-layout';
|
|
16
16
|
|
package/src/index.ts
CHANGED
|
@@ -21,17 +21,13 @@ export type {CanvasContextProps} from './adapter/canvas-context';
|
|
|
21
21
|
export {CanvasContext} from './adapter/canvas-context';
|
|
22
22
|
|
|
23
23
|
// GPU RESOURCES
|
|
24
|
-
export type
|
|
25
|
-
export {Resource} from './adapter/resources/resource';
|
|
24
|
+
export {Resource, type ResourceProps} from './adapter/resources/resource';
|
|
26
25
|
|
|
27
|
-
export type
|
|
28
|
-
export {Buffer} from './adapter/resources/buffer';
|
|
26
|
+
export {Buffer, type BufferProps, type BufferMapCallback} from './adapter/resources/buffer';
|
|
29
27
|
|
|
30
|
-
export type
|
|
31
|
-
export {Texture} from './adapter/resources/texture';
|
|
28
|
+
export {Texture, type TextureProps} from './adapter/resources/texture';
|
|
32
29
|
|
|
33
|
-
export type
|
|
34
|
-
export {TextureView} from './adapter/resources/texture-view';
|
|
30
|
+
export {TextureView, type TextureViewProps} from './adapter/resources/texture-view';
|
|
35
31
|
|
|
36
32
|
export type {ExternalTextureProps} from './adapter/resources/external-texture';
|
|
37
33
|
export {ExternalTexture} from './adapter/resources/external-texture';
|
|
@@ -72,6 +68,9 @@ export {TransformFeedback} from './adapter/resources/transform-feedback';
|
|
|
72
68
|
export type {QuerySetProps} from './adapter/resources/query-set';
|
|
73
69
|
export {QuerySet} from './adapter/resources/query-set';
|
|
74
70
|
|
|
71
|
+
export type {PipelineLayoutProps} from './adapter/resources/pipeline-layout';
|
|
72
|
+
export {PipelineLayout} from './adapter/resources/pipeline-layout';
|
|
73
|
+
|
|
75
74
|
// PORTABLE API - UNIFORM BUFFERS
|
|
76
75
|
export {UniformBufferLayout} from './portable/uniform-buffer-layout';
|
|
77
76
|
export {UniformBlock} from './portable/uniform-block';
|
|
@@ -82,7 +81,11 @@ export {UniformStore} from './portable/uniform-store';
|
|
|
82
81
|
export type {CompilerMessage} from './adapter/types/compiler-message';
|
|
83
82
|
|
|
84
83
|
export type {ExternalImage} from './image-utils/image-types';
|
|
85
|
-
|
|
84
|
+
|
|
85
|
+
export {
|
|
86
|
+
type CopyExternalImageOptions,
|
|
87
|
+
type CopyImageDataOptions
|
|
88
|
+
} from './adapter/resources/texture';
|
|
86
89
|
|
|
87
90
|
export type {Parameters, PrimitiveTopology, IndexFormat} from './adapter/types/parameters';
|
|
88
91
|
|
|
@@ -110,7 +113,12 @@ export type {
|
|
|
110
113
|
ComputeShaderLayout,
|
|
111
114
|
AttributeDeclaration,
|
|
112
115
|
BindingDeclaration,
|
|
113
|
-
Binding
|
|
116
|
+
Binding,
|
|
117
|
+
UniformBufferBindingLayout,
|
|
118
|
+
StorageBufferBindingLayout,
|
|
119
|
+
TextureBindingLayout,
|
|
120
|
+
SamplerBindingLayout,
|
|
121
|
+
StorageTextureBindingLayout
|
|
114
122
|
} from './adapter/types/shader-layout';
|
|
115
123
|
export type {BufferLayout, BufferAttributeLayout} from './adapter/types/buffer-layout';
|
|
116
124
|
export type {
|
|
@@ -133,41 +141,58 @@ export type {
|
|
|
133
141
|
BigTypedArrayConstructor
|
|
134
142
|
} from './types';
|
|
135
143
|
|
|
136
|
-
// GPU TYPE UTILS -
|
|
137
|
-
|
|
138
|
-
export type {PrimitiveDataType, SignedDataType, NormalizedDataType} from './shadertypes/data-types';
|
|
139
|
-
export type {AttributeShaderType, VariableShaderType} from './shadertypes/shader-types';
|
|
140
|
-
export type {VertexFormat} from './shadertypes/vertex-formats';
|
|
141
|
-
export type {
|
|
142
|
-
TextureFormat,
|
|
143
|
-
ColorTextureFormat,
|
|
144
|
-
DepthStencilTextureFormat,
|
|
145
|
-
TextureCompression,
|
|
146
|
-
TextureFormatInfo,
|
|
147
|
-
TextureFormatCapabilities
|
|
148
|
-
} from './shadertypes/texture-formats';
|
|
149
|
-
|
|
150
|
-
// GPU TYPE UTILS - GPU MEMORY LAYOUT HELPERS - CAN BE USED BY APPS BUT MOSTLY USED INTERNALLY
|
|
144
|
+
// GPU TYPE UTILS - BASIC DATA TYPES
|
|
151
145
|
|
|
146
|
+
export {
|
|
147
|
+
type PrimitiveDataType,
|
|
148
|
+
type SignedDataType,
|
|
149
|
+
type NormalizedDataType,
|
|
150
|
+
type DataTypeInfo,
|
|
151
|
+
type DataTypeArray,
|
|
152
|
+
type NormalizedDataTypeArray
|
|
153
|
+
} from './shadertypes/data-types/data-types';
|
|
154
|
+
export {
|
|
155
|
+
type AttributeShaderType,
|
|
156
|
+
type VariableShaderType
|
|
157
|
+
} from './shadertypes/data-types/shader-types';
|
|
152
158
|
export {
|
|
153
159
|
getDataTypeInfo,
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
} from './shadertypes/
|
|
160
|
+
getDataType,
|
|
161
|
+
getTypedArrayConstructor,
|
|
162
|
+
getNormalizedDataType
|
|
163
|
+
} from './shadertypes/data-types/decode-data-types';
|
|
158
164
|
export {
|
|
159
165
|
getVariableShaderTypeInfo,
|
|
160
166
|
getAttributeShaderTypeInfo
|
|
161
|
-
} from './shadertypes/
|
|
167
|
+
} from './shadertypes/data-types/decode-shader-types';
|
|
168
|
+
|
|
169
|
+
// GPU TYPE UTILS - VERTEX ARRAYs
|
|
170
|
+
|
|
171
|
+
export {type VertexFormat} from './shadertypes/vertex-arrays/vertex-formats';
|
|
172
|
+
|
|
162
173
|
export {
|
|
163
174
|
getVertexFormatInfo,
|
|
164
175
|
getVertexFormatFromAttribute,
|
|
165
176
|
makeVertexFormat
|
|
166
|
-
} from './shadertypes/
|
|
177
|
+
} from './shadertypes/vertex-arrays/decode-vertex-format';
|
|
178
|
+
|
|
179
|
+
// GPU TYPE UTILS - Texture Formats
|
|
180
|
+
|
|
181
|
+
export {
|
|
182
|
+
type TextureFormat,
|
|
183
|
+
type TextureFormatColor,
|
|
184
|
+
type TextureFormatDepthStencil,
|
|
185
|
+
type TextureCompression,
|
|
186
|
+
type TextureFormatInfo,
|
|
187
|
+
type TextureFormatCapabilities
|
|
188
|
+
} from './shadertypes/textures/texture-formats';
|
|
189
|
+
|
|
167
190
|
export {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
} from './shadertypes/
|
|
191
|
+
TextureFormatDecoder,
|
|
192
|
+
textureFormatDecoder
|
|
193
|
+
} from './shadertypes/textures/texture-format-decoder';
|
|
194
|
+
|
|
195
|
+
export {type PixelData, readPixel, writePixel} from './shadertypes/textures/pixel-utils';
|
|
171
196
|
|
|
172
197
|
// GENERAL EXPORTS - FOR APPLICATIONS
|
|
173
198
|
|
|
@@ -186,11 +211,10 @@ export type {
|
|
|
186
211
|
export {log} from './utils/log';
|
|
187
212
|
export {getScratchArray} from './utils/array-utils-flat';
|
|
188
213
|
export type {AttributeInfo} from './adapter-utils/get-attribute-from-layouts';
|
|
189
|
-
export {BufferLayoutHelper as _BufferLayoutHelper} from './adapter-utils/buffer-layout-helper';
|
|
190
214
|
export {getAttributeInfosFromLayouts} from './adapter-utils/get-attribute-from-layouts';
|
|
191
215
|
|
|
192
216
|
// TEST EXPORTS
|
|
193
217
|
export {
|
|
194
218
|
getTextureFormatDefinition as _getTextureFormatDefinition,
|
|
195
219
|
getTextureFormatTable as _getTextureFormatTable
|
|
196
|
-
} from './shadertypes/
|
|
220
|
+
} from './shadertypes/textures/texture-format-table';
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type {VariableShaderType} from '../shadertypes/shader-types';
|
|
5
|
+
import type {VariableShaderType} from '../shadertypes/data-types/shader-types';
|
|
6
6
|
import type {UniformValue} from '../adapter/types/uniforms';
|
|
7
7
|
import {
|
|
8
8
|
ShaderLayout,
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type {PrimitiveDataType} from '../shadertypes/data-types';
|
|
6
|
-
import type {VariableShaderType} from '../shadertypes/shader-types';
|
|
7
|
-
import {alignTo} from '../shadertypes/
|
|
8
|
-
import {getVariableShaderTypeInfo} from '../shadertypes/
|
|
5
|
+
import type {PrimitiveDataType} from '../shadertypes/data-types/data-types';
|
|
6
|
+
import type {VariableShaderType} from '../shadertypes/data-types/shader-types';
|
|
7
|
+
import {alignTo} from '../shadertypes/data-types/decode-data-types';
|
|
8
|
+
import {getVariableShaderTypeInfo} from '../shadertypes/data-types/decode-shader-types';
|
|
9
9
|
|
|
10
10
|
import type {UniformValue} from '../adapter/types/uniforms';
|
|
11
11
|
import {getScratchArrayBuffer} from '../utils/array-utils-flat';
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type {VariableShaderType} from '../shadertypes/shader-types';
|
|
5
|
+
import type {VariableShaderType} from '../shadertypes/data-types/shader-types';
|
|
6
6
|
import type {UniformValue} from '../adapter/types/uniforms';
|
|
7
7
|
import type {Device} from '../adapter/device';
|
|
8
8
|
import {Buffer} from '../adapter/resources/buffer';
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Primitive data types that shaders can perform calculations in and declare variables with.
|
|
7
|
+
* @note attribute inputs and texture samples can be populated from a different in-memory types, see below.
|
|
8
|
+
* @note `f16` requires the `f16` extension
|
|
9
|
+
*/
|
|
10
|
+
export type PrimitiveDataType = 'u32' | 'i32' | 'f32' | 'f16';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Signed data types describe signed and unsigned integers as well as floats of varying sizes
|
|
14
|
+
* @note These formats describe physical memory layouts in vertex and pixel formats, they are not used inside shaders
|
|
15
|
+
*/
|
|
16
|
+
export type SignedDataType =
|
|
17
|
+
| 'uint8'
|
|
18
|
+
| 'sint8'
|
|
19
|
+
| 'uint16'
|
|
20
|
+
| 'sint16'
|
|
21
|
+
| 'uint32'
|
|
22
|
+
| 'sint32'
|
|
23
|
+
| 'float16'
|
|
24
|
+
| 'float32';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Normalized data types describe signed and unsigned integers as well as floats of varying sizes together with normalization behavior
|
|
28
|
+
* @note These formats describe physical memory layouts in vertex and pixel formats, they are not used inside shaders
|
|
29
|
+
* @note Normalization means that these formats are converted into floats on read (shader must use f32 to process them)
|
|
30
|
+
* @note WebGPU does not support normalized 32 bit integer attributes: 'unorm32' | 'snorm32'
|
|
31
|
+
*/
|
|
32
|
+
export type NormalizedDataType = SignedDataType | 'unorm8' | 'snorm8' | 'unorm16' | 'snorm16';
|
|
33
|
+
|
|
34
|
+
/** Returns information about a signed or normalized DataType */
|
|
35
|
+
export type DataTypeInfo = {
|
|
36
|
+
signedType: SignedDataType;
|
|
37
|
+
primitiveType: PrimitiveDataType;
|
|
38
|
+
byteLength: 1 | 2 | 4;
|
|
39
|
+
normalized: boolean;
|
|
40
|
+
integer: boolean;
|
|
41
|
+
signed: boolean;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Returns a typed array appropriate to hold the actual values of a data type (integers for normalized integers) */
|
|
45
|
+
export type DataTypeArray<T extends NormalizedDataType> = T extends 'uint8'
|
|
46
|
+
? Uint8Array
|
|
47
|
+
: T extends 'sint8'
|
|
48
|
+
? Int8Array
|
|
49
|
+
: T extends 'unorm8'
|
|
50
|
+
? Uint8Array
|
|
51
|
+
: T extends 'snorm8'
|
|
52
|
+
? Int8Array
|
|
53
|
+
: T extends 'uint16'
|
|
54
|
+
? Uint16Array
|
|
55
|
+
: T extends 'sint16'
|
|
56
|
+
? Int16Array
|
|
57
|
+
: T extends 'unorm16'
|
|
58
|
+
? Uint16Array
|
|
59
|
+
: T extends 'snorm16'
|
|
60
|
+
? Int16Array
|
|
61
|
+
: T extends 'uint32'
|
|
62
|
+
? Uint32Array
|
|
63
|
+
: T extends 'sint32'
|
|
64
|
+
? Int32Array
|
|
65
|
+
: T extends 'float16'
|
|
66
|
+
? Uint16Array
|
|
67
|
+
: T extends 'float32'
|
|
68
|
+
? Float32Array
|
|
69
|
+
: never;
|
|
70
|
+
|
|
71
|
+
/** Returns a type array appropriate to hold the logical values of a data type (floats for normalized integers) */
|
|
72
|
+
export type NormalizedDataTypeArray<T extends NormalizedDataType> = T extends 'uint8'
|
|
73
|
+
? Uint8Array
|
|
74
|
+
: T extends 'sint8'
|
|
75
|
+
? Int8Array
|
|
76
|
+
: T extends 'unorm8'
|
|
77
|
+
? Float32Array
|
|
78
|
+
: T extends 'snorm8'
|
|
79
|
+
? Float32Array
|
|
80
|
+
: T extends 'uint16'
|
|
81
|
+
? Uint16Array
|
|
82
|
+
: T extends 'sint16'
|
|
83
|
+
? Int16Array
|
|
84
|
+
: T extends 'unorm16'
|
|
85
|
+
? Uint16Array
|
|
86
|
+
: T extends 'snorm16'
|
|
87
|
+
? Int16Array
|
|
88
|
+
: T extends 'uint32'
|
|
89
|
+
? Uint32Array
|
|
90
|
+
: T extends 'sint32'
|
|
91
|
+
? Int32Array
|
|
92
|
+
: T extends 'float16'
|
|
93
|
+
? Float32Array
|
|
94
|
+
: T extends 'float32'
|
|
95
|
+
? Float32Array
|
|
96
|
+
: never;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {TypedArray, TypedArrayConstructor} from '../../types';
|
|
6
|
+
import {PrimitiveDataType, SignedDataType, NormalizedDataType, DataTypeInfo} from './data-types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Gets info about a data type constant (signed or normalized)
|
|
10
|
+
* @returns underlying primitive / signed types, byte length, normalization, integer, signed flags
|
|
11
|
+
*/
|
|
12
|
+
export function getDataTypeInfo(type: NormalizedDataType): DataTypeInfo {
|
|
13
|
+
const [signedType, primitiveType, byteLength] = NORMALIZED_TYPE_MAP[type];
|
|
14
|
+
const normalized: boolean = type.includes('norm');
|
|
15
|
+
const integer: boolean = !normalized && !type.startsWith('float');
|
|
16
|
+
const signed: boolean = type.startsWith('s');
|
|
17
|
+
return {
|
|
18
|
+
signedType,
|
|
19
|
+
primitiveType,
|
|
20
|
+
byteLength,
|
|
21
|
+
normalized,
|
|
22
|
+
integer,
|
|
23
|
+
signed
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Build a vertex format from a signed data type and a component */
|
|
28
|
+
export function getNormalizedDataType(signedDataType: SignedDataType): NormalizedDataType {
|
|
29
|
+
const dataType: NormalizedDataType = signedDataType;
|
|
30
|
+
// prettier-ignore
|
|
31
|
+
switch (dataType) {
|
|
32
|
+
case 'uint8': return 'unorm8';
|
|
33
|
+
case 'sint8': return 'snorm8';
|
|
34
|
+
case 'uint16': return 'unorm16';
|
|
35
|
+
case 'sint16': return 'snorm16';
|
|
36
|
+
default: return dataType;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Align offset to 1, 2 or 4 elements (4, 8 or 16 bytes) */
|
|
41
|
+
export function alignTo(size: number, count: number): number {
|
|
42
|
+
// prettier-ignore
|
|
43
|
+
switch (count) {
|
|
44
|
+
case 1: return size; // Pad upwards to even multiple of 2
|
|
45
|
+
case 2: return size + (size % 2); // Pad upwards to even multiple of 2
|
|
46
|
+
default: return size + ((4 - (size % 4)) % 4); // Pad upwards to even multiple of 4
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Returns the VariableShaderType that corresponds to a typed array */
|
|
51
|
+
export function getDataType(arrayOrType: TypedArray | TypedArrayConstructor): SignedDataType {
|
|
52
|
+
const Constructor = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;
|
|
53
|
+
if (Constructor === Uint8ClampedArray) {
|
|
54
|
+
return 'uint8';
|
|
55
|
+
}
|
|
56
|
+
const info = Object.values(NORMALIZED_TYPE_MAP).find(entry => Constructor === entry[4]);
|
|
57
|
+
if (!info) {
|
|
58
|
+
throw new Error(Constructor.name);
|
|
59
|
+
}
|
|
60
|
+
return info[0];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Returns the TypedArray that corresponds to a shader data type */
|
|
64
|
+
export function getTypedArrayConstructor(type: NormalizedDataType): TypedArrayConstructor {
|
|
65
|
+
const [, , , , Constructor] = NORMALIZED_TYPE_MAP[type];
|
|
66
|
+
return Constructor;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const NORMALIZED_TYPE_MAP: Record<
|
|
70
|
+
NormalizedDataType,
|
|
71
|
+
[
|
|
72
|
+
SignedDataType,
|
|
73
|
+
PrimitiveDataType,
|
|
74
|
+
bytes: 1 | 2 | 4,
|
|
75
|
+
normalized: boolean,
|
|
76
|
+
arrayConstructor: TypedArrayConstructor
|
|
77
|
+
]
|
|
78
|
+
> = {
|
|
79
|
+
uint8: ['uint8', 'u32', 1, false, Uint8Array],
|
|
80
|
+
sint8: ['sint8', 'i32', 1, false, Int8Array],
|
|
81
|
+
unorm8: ['uint8', 'f32', 1, true, Uint8Array],
|
|
82
|
+
snorm8: ['sint8', 'f32', 1, true, Int8Array],
|
|
83
|
+
uint16: ['uint16', 'u32', 2, false, Uint16Array],
|
|
84
|
+
sint16: ['sint16', 'i32', 2, false, Int16Array],
|
|
85
|
+
unorm16: ['uint16', 'u32', 2, true, Uint16Array],
|
|
86
|
+
snorm16: ['sint16', 'i32', 2, true, Int16Array],
|
|
87
|
+
float16: ['float16', 'f16', 2, false, Uint16Array],
|
|
88
|
+
float32: ['float32', 'f32', 4, false, Float32Array],
|
|
89
|
+
uint32: ['uint32', 'u32', 4, false, Uint32Array],
|
|
90
|
+
sint32: ['sint32', 'i32', 4, false, Int32Array]
|
|
91
|
+
};
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import {PrimitiveDataType} from '
|
|
5
|
+
import {PrimitiveDataType} from './data-types';
|
|
6
6
|
import type {
|
|
7
7
|
VariableShaderType,
|
|
8
8
|
AttributeShaderType,
|
|
9
9
|
AttributeShaderTypeInfo,
|
|
10
10
|
VariableShaderTypeAlias,
|
|
11
11
|
AttributeShaderTypeAlias
|
|
12
|
-
} from '
|
|
12
|
+
} from './shader-types';
|
|
13
13
|
|
|
14
14
|
/** Split a uniform type string into type and components */
|
|
15
15
|
export function getVariableShaderTypeInfo(format: VariableShaderType): {
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export type ImageData = {
|
|
6
|
+
/** Offset into the data (in addition to any offset built-in to the ArrayBufferView) */
|
|
7
|
+
byteOffset?: number;
|
|
8
|
+
/** The stride, in bytes, between the beginning of each texel block row and the subsequent texel block row. Required if there are multiple texel block rows (i.e. the copy height or depth is more than one block). */
|
|
9
|
+
bytesPerRow?: number;
|
|
10
|
+
/** Number or rows per image (needed if multiple images are being set) */
|
|
11
|
+
rowsPerImage?: number;
|
|
12
|
+
/** Bits per channel */
|
|
13
|
+
bitsPerChannel: [number, number, number, number];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type PixelData = {
|
|
17
|
+
arrayBuffer: ArrayBuffer;
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
/** Bytes per pixel */
|
|
21
|
+
bytesPerPixel: number;
|
|
22
|
+
bytesPerRow: number;
|
|
23
|
+
bitsPerChannel: [number, number, number, number];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Extracts a single RGBA pixel value from PixelData at the given (x, y) coordinate.
|
|
28
|
+
*
|
|
29
|
+
* The pixel's data is assumed to be packed according to pixelData.bitsPerChannel.
|
|
30
|
+
* The pixel data for a given row is padded to pixelData.bytesPerRow.
|
|
31
|
+
*
|
|
32
|
+
* @param pixelData - The metadata and data for the pixel buffer.
|
|
33
|
+
* @param x - The x coordinate (0-based).
|
|
34
|
+
* @param y - The y coordinate (0-based).
|
|
35
|
+
* @returns A tuple [r, g, b, a] where each channel is the extracted numeric value.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
|
|
39
|
+
Assume you obtained an ArrayBuffer from copyTextureToBuffer and have the following metadata:
|
|
40
|
+
|
|
41
|
+
const pixelData: PixelData = {
|
|
42
|
+
bitsPerChannel: [5, 6, 5, 0], // For example, a 16-bit RGB565 format (no alpha)
|
|
43
|
+
width: 800,
|
|
44
|
+
height: 600,
|
|
45
|
+
bytesPerPixel: 2, // 16 bits per pixel
|
|
46
|
+
bytesPerRow: 1600, // Assuming no extra padding
|
|
47
|
+
arrayBuffer: myTextureBuffer, // Obtained from copyTextureToBuffer
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
You can then extract the pixel at (x, y) like so:
|
|
51
|
+
|
|
52
|
+
const rgba = extractPixel(pixelData, x, y);
|
|
53
|
+
console.log("Extracted RGBA:", rgba);
|
|
54
|
+
|
|
55
|
+
For RGBA formats where all channels are present (e.g. [8, 8, 8, 8]), the function will extract a 4-channel pixel value.
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
export function readPixel(
|
|
59
|
+
pixelData: PixelData,
|
|
60
|
+
x: number,
|
|
61
|
+
y: number,
|
|
62
|
+
bitsPerChannel: [number, number, number, number]
|
|
63
|
+
): [number, number, number, number] {
|
|
64
|
+
if (x < 0 || x >= pixelData.width || y < 0 || y >= pixelData.height) {
|
|
65
|
+
throw new Error('Coordinates out of bounds.');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Compute the byte offset of the pixel in the buffer.
|
|
69
|
+
const byteOffset = y * pixelData.bytesPerRow + x * pixelData.bytesPerPixel;
|
|
70
|
+
|
|
71
|
+
// Create a Uint8Array view for this pixel's bytes.
|
|
72
|
+
// We only need to view pixelData.bytesPerPixel bytes.
|
|
73
|
+
const pixelDataView = new DataView(pixelData.arrayBuffer, byteOffset, pixelData.bytesPerPixel);
|
|
74
|
+
|
|
75
|
+
let bitOffsetWithinPixel = 0;
|
|
76
|
+
const channels: number[] = [];
|
|
77
|
+
|
|
78
|
+
// Extract each of the four channels.
|
|
79
|
+
for (let i = 0; i < 4; i++) {
|
|
80
|
+
const bits = bitsPerChannel[i];
|
|
81
|
+
// If a channel's bit width is zero or negative, consider it not present.
|
|
82
|
+
if (bits <= 0) {
|
|
83
|
+
channels.push(0);
|
|
84
|
+
} else {
|
|
85
|
+
const channelValue = readBitsFromDataView(pixelDataView, bitOffsetWithinPixel, bits);
|
|
86
|
+
channels.push(channelValue);
|
|
87
|
+
bitOffsetWithinPixel += bits;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return [channels[0], channels[1], channels[2], channels[3]];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Encodes an RGBA pixel into a DataView at a given bit offset according to a specified bit layout.
|
|
96
|
+
*
|
|
97
|
+
* The channels are written sequentially in the order R, G, B, A. For each channel, the number
|
|
98
|
+
* of bits is taken from the bitsPerChannel array. Channel values are masked to fit within the specified width.
|
|
99
|
+
*
|
|
100
|
+
* @param dataView - The DataView into which the pixel will be encoded.
|
|
101
|
+
* @param bitOffset - The bit offset in the DataView where the pixel should be written.
|
|
102
|
+
* @param bitsPerChannel - A tuple specifying the number of bits for each channel: [R, G, B, A].
|
|
103
|
+
* @param pixel - A tuple [r, g, b, a] containing the channel values (as numbers).
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
|
|
107
|
+
Assume you want to encode a pixel into a packed format where:
|
|
108
|
+
- Red uses 5 bits
|
|
109
|
+
- Green uses 6 bits
|
|
110
|
+
- Blue uses 5 bits
|
|
111
|
+
- Alpha is not used (0 bits)
|
|
112
|
+
And the pixel format is packed into 16 bits total.
|
|
113
|
+
|
|
114
|
+
You might have:
|
|
115
|
+
const bitsPerChannel: [number, number, number, number] = [5, 6, 5, 0];
|
|
116
|
+
const pixel: [number, number, number, number] = [15, 31, 15, 0]; // Example values
|
|
117
|
+
const buffer = new ArrayBuffer(2); // 16 bits = 2 bytes
|
|
118
|
+
const dataView = new DataView(buffer);
|
|
119
|
+
|
|
120
|
+
Now encode the pixel at bit offset 0:
|
|
121
|
+
encodePixel(dataView, 0, bitsPerChannel, pixel);
|
|
122
|
+
|
|
123
|
+
The dataView now contains the 16-bit packed pixel value in big-endian order.
|
|
124
|
+
*/
|
|
125
|
+
export function writePixel(
|
|
126
|
+
dataView: DataView,
|
|
127
|
+
bitOffset: number,
|
|
128
|
+
bitsPerChannel: [number, number, number, number],
|
|
129
|
+
pixel: [number, number, number, number]
|
|
130
|
+
): void {
|
|
131
|
+
let currentBitOffset = bitOffset;
|
|
132
|
+
for (let channel = 0; channel < 4; channel++) {
|
|
133
|
+
const bits = bitsPerChannel[channel];
|
|
134
|
+
// Clamp the channel value to the maximum allowed by the bit width.
|
|
135
|
+
const maxValue = (1 << bits) - 1;
|
|
136
|
+
const channelValue = pixel[channel] & maxValue;
|
|
137
|
+
writeBitsToDataView(dataView, currentBitOffset, bits, channelValue);
|
|
138
|
+
currentBitOffset += bits;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Reads a specified number of bits from a DataView starting at a given bit offset.
|
|
144
|
+
*
|
|
145
|
+
* For channels with a bit width of 8, 16, or 32 bits and when the bitOffset is byte-aligned,
|
|
146
|
+
* this function uses DataView methods for fast extraction.
|
|
147
|
+
*
|
|
148
|
+
* Bits are assumed to be stored in big-endian order (i.e. the most-significant bit is at position 7 in each byte).
|
|
149
|
+
*
|
|
150
|
+
* @param dataView - The DataView containing the data.
|
|
151
|
+
* @param bitOffset - The offset (in bits) within the data from which to start reading.
|
|
152
|
+
* @param bitCount - The number of bits to read (supported range: 1 to 32).
|
|
153
|
+
* @returns The extracted value as a number.
|
|
154
|
+
*/
|
|
155
|
+
export function readBitsFromDataView(
|
|
156
|
+
dataView: DataView,
|
|
157
|
+
bitOffset: number,
|
|
158
|
+
bitCount: number
|
|
159
|
+
): number {
|
|
160
|
+
// Check if we can optimize when bitOffset is byte-aligned.
|
|
161
|
+
if (bitOffset % 8 === 0) {
|
|
162
|
+
const byteOffset = bitOffset / 8;
|
|
163
|
+
if (bitCount === 8 && byteOffset + 1 <= dataView.byteLength) {
|
|
164
|
+
return dataView.getUint8(byteOffset);
|
|
165
|
+
} else if (bitCount === 16 && byteOffset + 2 <= dataView.byteLength) {
|
|
166
|
+
// false for big-endian reading.
|
|
167
|
+
return dataView.getUint16(byteOffset, false);
|
|
168
|
+
} else if (bitCount === 32 && byteOffset + 4 <= dataView.byteLength) {
|
|
169
|
+
return dataView.getUint32(byteOffset, false);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Fallback: bit-level extraction for non-aligned or non-standard bit widths.
|
|
174
|
+
let value = 0;
|
|
175
|
+
for (let i = 0; i < bitCount; i++) {
|
|
176
|
+
const overallBitIndex = bitOffset + i;
|
|
177
|
+
const byteIndex = Math.floor(overallBitIndex / 8);
|
|
178
|
+
const bitIndex = overallBitIndex % 8;
|
|
179
|
+
// Read the byte and extract the bit at position (7 - bitIndex).
|
|
180
|
+
const byteValue = dataView.getUint8(byteIndex);
|
|
181
|
+
const bit = (byteValue >> (7 - bitIndex)) & 1;
|
|
182
|
+
value = (value << 1) | bit;
|
|
183
|
+
}
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Writes a specified number of bits from a value into a DataView at a given bit offset.
|
|
189
|
+
*
|
|
190
|
+
* For channels with a bit width of 8, 16, or 32 bits and when the bit offset is byte-aligned,
|
|
191
|
+
* this function uses DataView methods for fast writing.
|
|
192
|
+
*
|
|
193
|
+
* Bits are assumed to be stored in big-endian order (i.e. the most-significant bit is at position 7 in each byte).
|
|
194
|
+
*
|
|
195
|
+
* @param dataView - The DataView to write into.
|
|
196
|
+
* @param bitOffset - The bit offset at which to begin writing.
|
|
197
|
+
* @param bitCount - The number of bits to write (supported range: 1 to 32).
|
|
198
|
+
* @param value - The numeric value whose lower bitCount bits will be written.
|
|
199
|
+
*/
|
|
200
|
+
export function writeBitsToDataView(
|
|
201
|
+
dataView: DataView,
|
|
202
|
+
bitOffset: number,
|
|
203
|
+
bitCount: number,
|
|
204
|
+
value: number
|
|
205
|
+
): void {
|
|
206
|
+
// If the bitOffset is byte-aligned, we may optimize for common bit widths.
|
|
207
|
+
if (bitOffset % 8 === 0) {
|
|
208
|
+
const byteOffset = bitOffset / 8;
|
|
209
|
+
if (bitCount === 8 && byteOffset + 1 <= dataView.byteLength) {
|
|
210
|
+
dataView.setUint8(byteOffset, value & 0xff);
|
|
211
|
+
return;
|
|
212
|
+
} else if (bitCount === 16 && byteOffset + 2 <= dataView.byteLength) {
|
|
213
|
+
dataView.setUint16(byteOffset, value & 0xffff, false); // big-endian
|
|
214
|
+
return;
|
|
215
|
+
} else if (bitCount === 32 && byteOffset + 4 <= dataView.byteLength) {
|
|
216
|
+
dataView.setUint32(byteOffset, value, false); // big-endian
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Fallback: write bit-by-bit.
|
|
222
|
+
for (let i = 0; i < bitCount; i++) {
|
|
223
|
+
const overallBitIndex = bitOffset + i;
|
|
224
|
+
const byteIndex = Math.floor(overallBitIndex / 8);
|
|
225
|
+
const bitIndex = overallBitIndex % 8;
|
|
226
|
+
const mask = 1 << (7 - bitIndex);
|
|
227
|
+
// Extract the i-th bit from value (starting from the most-significant bit)
|
|
228
|
+
const bitValue = (value >> (bitCount - 1 - i)) & 1;
|
|
229
|
+
// Read the current byte.
|
|
230
|
+
let currentByte = dataView.getUint8(byteIndex);
|
|
231
|
+
// Clear the target bit.
|
|
232
|
+
currentByte &= ~mask;
|
|
233
|
+
// Set the target bit if bitValue is 1.
|
|
234
|
+
if (bitValue) {
|
|
235
|
+
currentByte |= mask;
|
|
236
|
+
}
|
|
237
|
+
dataView.setUint8(byteIndex, currentByte);
|
|
238
|
+
}
|
|
239
|
+
}
|