@luma.gl/core 9.0.0-alpha.9 → 9.0.0-beta.2

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.
Files changed (306) hide show
  1. package/LICENSE +3 -1
  2. package/README.md +4 -4
  3. package/dist/adapter/attribute-utils/get-attribute-from-layouts.d.ts +52 -0
  4. package/dist/adapter/attribute-utils/get-attribute-from-layouts.d.ts.map +1 -0
  5. package/dist/adapter/attribute-utils/get-attribute-from-layouts.js +130 -0
  6. package/dist/adapter/attribute-utils/get-attribute-from-layouts.js.map +1 -0
  7. package/dist/adapter/canvas-context.d.ts +109 -0
  8. package/dist/adapter/canvas-context.d.ts.map +1 -0
  9. package/dist/adapter/canvas-context.js +254 -0
  10. package/dist/adapter/canvas-context.js.map +1 -0
  11. package/dist/adapter/device.d.ts +219 -0
  12. package/dist/adapter/device.d.ts.map +1 -0
  13. package/dist/adapter/device.js +99 -0
  14. package/dist/adapter/device.js.map +1 -0
  15. package/dist/adapter/resources/buffer.d.ts +56 -0
  16. package/dist/adapter/resources/buffer.d.ts.map +1 -0
  17. package/dist/adapter/resources/buffer.js +62 -0
  18. package/dist/adapter/resources/buffer.js.map +1 -0
  19. package/dist/adapter/resources/command-buffer.d.ts +12 -0
  20. package/dist/adapter/resources/command-buffer.d.ts.map +1 -0
  21. package/dist/adapter/resources/command-buffer.js +15 -0
  22. package/dist/adapter/resources/command-buffer.js.map +1 -0
  23. package/dist/adapter/resources/command-encoder.d.ts +113 -0
  24. package/dist/adapter/resources/command-encoder.d.ts.map +1 -0
  25. package/dist/adapter/resources/command-encoder.js +19 -0
  26. package/dist/adapter/resources/command-encoder.js.map +1 -0
  27. package/dist/adapter/resources/compute-pass.d.ts +31 -0
  28. package/dist/adapter/resources/compute-pass.d.ts.map +1 -0
  29. package/dist/adapter/resources/compute-pass.js +15 -0
  30. package/dist/adapter/resources/compute-pass.js.map +1 -0
  31. package/dist/adapter/resources/compute-pipeline.d.ts +24 -0
  32. package/dist/adapter/resources/compute-pipeline.d.ts.map +1 -0
  33. package/dist/adapter/resources/compute-pipeline.js +20 -0
  34. package/dist/adapter/resources/compute-pipeline.js.map +1 -0
  35. package/dist/adapter/resources/external-texture.d.ts +12 -0
  36. package/dist/adapter/resources/external-texture.d.ts.map +1 -0
  37. package/dist/adapter/resources/external-texture.js +17 -0
  38. package/dist/adapter/resources/external-texture.js.map +1 -0
  39. package/dist/adapter/resources/framebuffer.d.ts +50 -0
  40. package/dist/adapter/resources/framebuffer.d.ts.map +1 -0
  41. package/dist/adapter/resources/framebuffer.js +102 -0
  42. package/dist/adapter/resources/framebuffer.js.map +1 -0
  43. package/dist/adapter/resources/render-pass.d.ts +51 -0
  44. package/dist/adapter/resources/render-pass.d.ts.map +1 -0
  45. package/dist/adapter/resources/render-pass.js +23 -0
  46. package/dist/adapter/resources/render-pass.js.map +1 -0
  47. package/dist/adapter/resources/render-pipeline.d.ts +89 -0
  48. package/dist/adapter/resources/render-pipeline.d.ts.map +1 -0
  49. package/dist/adapter/resources/render-pipeline.js +36 -0
  50. package/dist/adapter/resources/render-pipeline.js.map +1 -0
  51. package/dist/adapter/resources/resource.d.ts +73 -0
  52. package/dist/adapter/resources/resource.d.ts.map +1 -0
  53. package/dist/adapter/resources/resource.js +101 -0
  54. package/dist/adapter/resources/resource.js.map +1 -0
  55. package/dist/adapter/resources/sampler.d.ts +42 -0
  56. package/dist/adapter/resources/sampler.d.ts.map +1 -0
  57. package/dist/adapter/resources/sampler.js +26 -0
  58. package/dist/adapter/resources/sampler.js.map +1 -0
  59. package/dist/adapter/resources/shader.d.ts +45 -0
  60. package/dist/adapter/resources/shader.d.ts.map +1 -0
  61. package/dist/adapter/resources/shader.js +90 -0
  62. package/dist/adapter/resources/shader.js.map +1 -0
  63. package/dist/adapter/resources/texture.d.ts +90 -0
  64. package/dist/adapter/resources/texture.d.ts.map +1 -0
  65. package/dist/adapter/resources/texture.js +45 -0
  66. package/dist/adapter/resources/texture.js.map +1 -0
  67. package/dist/adapter/resources/transform-feedback.d.ts +30 -0
  68. package/dist/adapter/resources/transform-feedback.d.ts.map +1 -0
  69. package/dist/adapter/resources/transform-feedback.js +17 -0
  70. package/dist/adapter/resources/transform-feedback.js.map +1 -0
  71. package/dist/adapter/resources/vertex-array.d.ts +40 -0
  72. package/dist/adapter/resources/vertex-array.d.ts.map +1 -0
  73. package/dist/adapter/resources/vertex-array.js +24 -0
  74. package/dist/adapter/resources/vertex-array.js.map +1 -0
  75. package/dist/adapter/type-utils/decode-attribute-type.d.ts +20 -0
  76. package/dist/adapter/type-utils/decode-attribute-type.d.ts.map +1 -0
  77. package/dist/adapter/type-utils/decode-attribute-type.js +60 -0
  78. package/dist/adapter/type-utils/decode-attribute-type.js.map +1 -0
  79. package/dist/adapter/type-utils/decode-data-type.d.ts +16 -0
  80. package/dist/adapter/type-utils/decode-data-type.d.ts.map +1 -0
  81. package/dist/adapter/type-utils/decode-data-type.js +43 -0
  82. package/dist/adapter/type-utils/decode-data-type.js.map +1 -0
  83. package/dist/adapter/type-utils/decode-shader-types.d.ts +9 -0
  84. package/dist/adapter/type-utils/decode-shader-types.d.ts.map +1 -0
  85. package/dist/adapter/type-utils/decode-shader-types.js +103 -0
  86. package/dist/adapter/type-utils/decode-shader-types.js.map +1 -0
  87. package/dist/adapter/type-utils/decode-texture-format.d.ts +19 -0
  88. package/dist/adapter/type-utils/decode-texture-format.d.ts.map +1 -0
  89. package/dist/adapter/type-utils/decode-texture-format.js +101 -0
  90. package/dist/adapter/type-utils/decode-texture-format.js.map +1 -0
  91. package/dist/adapter/type-utils/decode-vertex-format.d.ts +22 -0
  92. package/dist/adapter/type-utils/decode-vertex-format.d.ts.map +1 -0
  93. package/dist/adapter/type-utils/decode-vertex-format.js +25 -0
  94. package/dist/adapter/type-utils/decode-vertex-format.js.map +1 -0
  95. package/dist/adapter/type-utils/vertex-format-from-attribute.d.ts +10 -0
  96. package/dist/adapter/type-utils/vertex-format-from-attribute.d.ts.map +1 -0
  97. package/dist/adapter/type-utils/vertex-format-from-attribute.js +76 -0
  98. package/dist/adapter/type-utils/vertex-format-from-attribute.js.map +1 -0
  99. package/dist/adapter/type-utils/wgsl-utils.d.ts +4 -0
  100. package/dist/adapter/type-utils/wgsl-utils.d.ts.map +1 -0
  101. package/dist/adapter/type-utils/wgsl-utils.js +15 -0
  102. package/dist/adapter/type-utils/wgsl-utils.js.map +1 -0
  103. package/dist/adapter/types/accessor.d.ts +23 -0
  104. package/dist/adapter/types/accessor.d.ts.map +1 -0
  105. package/dist/adapter/types/accessor.js +2 -0
  106. package/dist/adapter/types/accessor.js.map +1 -0
  107. package/dist/adapter/types/buffer-layout.d.ts +59 -0
  108. package/dist/adapter/types/buffer-layout.d.ts.map +1 -0
  109. package/dist/adapter/types/buffer-layout.js +2 -0
  110. package/dist/adapter/types/buffer-layout.js.map +1 -0
  111. package/dist/adapter/types/parameters.d.ts +97 -0
  112. package/dist/adapter/types/parameters.d.ts.map +1 -0
  113. package/dist/adapter/types/parameters.js +28 -0
  114. package/dist/adapter/types/parameters.js.map +1 -0
  115. package/dist/adapter/types/shader-layout.d.ts +145 -0
  116. package/dist/adapter/types/shader-layout.d.ts.map +1 -0
  117. package/dist/adapter/types/shader-layout.js +2 -0
  118. package/dist/adapter/types/shader-layout.js.map +1 -0
  119. package/dist/adapter/types/shader-types.d.ts +21 -0
  120. package/dist/adapter/types/shader-types.d.ts.map +1 -0
  121. package/dist/adapter/types/shader-types.js +2 -0
  122. package/dist/adapter/types/shader-types.js.map +1 -0
  123. package/dist/adapter/types/texture-formats.d.ts +12 -0
  124. package/dist/adapter/types/texture-formats.d.ts.map +1 -0
  125. package/dist/adapter/types/texture-formats.js +2 -0
  126. package/dist/adapter/types/texture-formats.js.map +1 -0
  127. package/dist/adapter/types/types.d.ts +90 -0
  128. package/dist/adapter/types/types.d.ts.map +1 -0
  129. package/dist/adapter/types/types.js +2 -0
  130. package/dist/adapter/types/types.js.map +1 -0
  131. package/dist/adapter/types/vertex-formats.d.ts +14 -0
  132. package/dist/adapter/types/vertex-formats.d.ts.map +1 -0
  133. package/dist/adapter/types/vertex-formats.js +2 -0
  134. package/dist/adapter/types/vertex-formats.js.map +1 -0
  135. package/dist/dist.dev.js +2976 -0
  136. package/dist/index.cjs +2445 -0
  137. package/dist/index.d.ts +81 -128
  138. package/dist/index.d.ts.map +1 -1
  139. package/dist/index.js +45 -18
  140. package/dist/index.js.map +1 -1
  141. package/dist/init.d.ts +5 -0
  142. package/dist/init.d.ts.map +1 -0
  143. package/dist/init.js +24 -0
  144. package/dist/init.js.map +1 -0
  145. package/dist/lib/compiler-log/compiler-message.d.ts +8 -0
  146. package/dist/lib/compiler-log/compiler-message.d.ts.map +1 -0
  147. package/dist/lib/compiler-log/compiler-message.js +2 -0
  148. package/dist/lib/compiler-log/compiler-message.js.map +1 -0
  149. package/dist/lib/compiler-log/format-compiler-log.d.ts +8 -0
  150. package/dist/lib/compiler-log/format-compiler-log.d.ts.map +1 -0
  151. package/dist/lib/compiler-log/format-compiler-log.js +64 -0
  152. package/dist/lib/compiler-log/format-compiler-log.js.map +1 -0
  153. package/dist/lib/compiler-log/get-shader-info.d.ts +9 -0
  154. package/dist/lib/compiler-log/get-shader-info.d.ts.map +1 -0
  155. package/dist/lib/compiler-log/get-shader-info.js +25 -0
  156. package/dist/lib/compiler-log/get-shader-info.js.map +1 -0
  157. package/dist/lib/luma.d.ts +22 -0
  158. package/dist/lib/luma.d.ts.map +1 -0
  159. package/dist/lib/luma.js +63 -0
  160. package/dist/lib/luma.js.map +1 -0
  161. package/dist/lib/uniforms/uniform-block.d.ts +29 -0
  162. package/dist/lib/uniforms/uniform-block.d.ts.map +1 -0
  163. package/dist/lib/uniforms/uniform-block.js +48 -0
  164. package/dist/lib/uniforms/uniform-block.js.map +1 -0
  165. package/dist/lib/uniforms/uniform-buffer-layout.d.ts +27 -0
  166. package/dist/lib/uniforms/uniform-buffer-layout.d.ts.map +1 -0
  167. package/dist/lib/uniforms/uniform-buffer-layout.js +76 -0
  168. package/dist/lib/uniforms/uniform-buffer-layout.js.map +1 -0
  169. package/dist/lib/uniforms/uniform-store.d.ts +62 -0
  170. package/dist/lib/uniforms/uniform-store.d.ts.map +1 -0
  171. package/dist/lib/uniforms/uniform-store.js +89 -0
  172. package/dist/lib/uniforms/uniform-store.js.map +1 -0
  173. package/dist/lib/uniforms/uniform.d.ts +10 -0
  174. package/dist/lib/uniforms/uniform.d.ts.map +1 -0
  175. package/dist/lib/uniforms/uniform.js +20 -0
  176. package/dist/lib/uniforms/uniform.js.map +1 -0
  177. package/dist/types.d.ts +19 -0
  178. package/dist/types.d.ts.map +1 -0
  179. package/dist/types.js +3 -0
  180. package/dist/types.js.map +1 -0
  181. package/dist/utils/array-equal.d.ts +5 -0
  182. package/dist/utils/array-equal.d.ts.map +1 -0
  183. package/dist/utils/array-equal.js +28 -0
  184. package/dist/utils/array-equal.js.map +1 -0
  185. package/dist/utils/array-utils-flat.d.ts +10 -0
  186. package/dist/utils/array-utils-flat.d.ts.map +1 -0
  187. package/dist/utils/array-utils-flat.js +36 -0
  188. package/dist/utils/array-utils-flat.js.map +1 -0
  189. package/dist/utils/assert.d.ts +2 -0
  190. package/dist/utils/assert.d.ts.map +1 -0
  191. package/dist/utils/assert.js +6 -0
  192. package/dist/utils/assert.js.map +1 -0
  193. package/dist/utils/cast.d.ts +3 -0
  194. package/dist/utils/cast.d.ts.map +1 -0
  195. package/dist/utils/cast.js +4 -0
  196. package/dist/utils/cast.js.map +1 -0
  197. package/dist/utils/check-props.d.ts +7 -0
  198. package/dist/utils/check-props.d.ts.map +1 -0
  199. package/dist/utils/check-props.js +32 -0
  200. package/dist/utils/check-props.js.map +1 -0
  201. package/dist/utils/deep-equal.d.ts +9 -0
  202. package/dist/utils/deep-equal.d.ts.map +1 -0
  203. package/dist/utils/deep-equal.js +40 -0
  204. package/dist/utils/deep-equal.js.map +1 -0
  205. package/dist/utils/format-value.d.ts +7 -0
  206. package/dist/utils/format-value.d.ts.map +1 -0
  207. package/dist/utils/format-value.js +42 -0
  208. package/dist/utils/format-value.js.map +1 -0
  209. package/dist/utils/is-array.d.ts +16 -0
  210. package/dist/utils/is-array.d.ts.map +1 -0
  211. package/dist/utils/is-array.js +10 -0
  212. package/dist/utils/is-array.js.map +1 -0
  213. package/dist/utils/load-file.d.ts +35 -0
  214. package/dist/utils/load-file.d.ts.map +1 -0
  215. package/dist/utils/load-file.js +48 -0
  216. package/dist/utils/load-file.js.map +1 -0
  217. package/dist/utils/log.d.ts +4 -0
  218. package/dist/utils/log.d.ts.map +1 -0
  219. package/dist/utils/log.js +5 -0
  220. package/dist/utils/log.js.map +1 -0
  221. package/dist/utils/random.d.ts +5 -0
  222. package/dist/utils/random.d.ts.map +1 -0
  223. package/dist/utils/random.js +14 -0
  224. package/dist/utils/random.js.map +1 -0
  225. package/dist/utils/request-animation-frame.d.ts +3 -0
  226. package/dist/utils/request-animation-frame.d.ts.map +1 -0
  227. package/dist/utils/request-animation-frame.js +7 -0
  228. package/dist/utils/request-animation-frame.js.map +1 -0
  229. package/dist/utils/stats-manager.d.ts +12 -0
  230. package/dist/utils/stats-manager.d.ts.map +1 -0
  231. package/dist/utils/stats-manager.js +19 -0
  232. package/dist/utils/stats-manager.js.map +1 -0
  233. package/dist/utils/stub-methods.d.ts +2 -0
  234. package/dist/utils/stub-methods.d.ts.map +1 -0
  235. package/dist/utils/stub-methods.js +16 -0
  236. package/dist/utils/stub-methods.js.map +1 -0
  237. package/dist/utils/utils.d.ts +15 -0
  238. package/dist/utils/utils.d.ts.map +1 -0
  239. package/dist/utils/utils.js +19 -0
  240. package/dist/utils/utils.js.map +1 -0
  241. package/dist.min.js +33 -0
  242. package/package.json +17 -12
  243. package/src/adapter/attribute-utils/get-attribute-from-layouts.ts +259 -0
  244. package/src/adapter/canvas-context.ts +433 -0
  245. package/src/adapter/device.ts +435 -0
  246. package/src/adapter/resources/buffer.ts +106 -0
  247. package/src/adapter/resources/command-buffer.ts +38 -0
  248. package/src/adapter/resources/command-encoder.ts +172 -0
  249. package/src/adapter/resources/compute-pass.ts +52 -0
  250. package/src/adapter/resources/compute-pipeline.ts +39 -0
  251. package/src/adapter/resources/external-texture.ts +20 -0
  252. package/src/adapter/resources/framebuffer.ts +230 -0
  253. package/src/adapter/resources/render-pass.ts +117 -0
  254. package/src/adapter/resources/render-pipeline.ts +133 -0
  255. package/src/adapter/resources/resource.ts +176 -0
  256. package/src/adapter/resources/sampler.ts +69 -0
  257. package/src/adapter/resources/shader.ts +143 -0
  258. package/src/adapter/resources/texture.ts +140 -0
  259. package/src/adapter/resources/transform-feedback.ts +45 -0
  260. package/src/adapter/resources/vertex-array.ts +65 -0
  261. package/src/adapter/type-utils/decode-attribute-type.ts +82 -0
  262. package/src/adapter/type-utils/decode-data-type.ts +62 -0
  263. package/src/adapter/type-utils/decode-shader-types.ts +48 -0
  264. package/src/adapter/type-utils/decode-texture-format.ts +190 -0
  265. package/src/adapter/type-utils/decode-vertex-format.ts +49 -0
  266. package/src/adapter/type-utils/vertex-format-from-attribute.ts +103 -0
  267. package/src/adapter/type-utils/wgsl-utils.ts +18 -0
  268. package/src/adapter/types/accessor.ts +34 -0
  269. package/src/adapter/types/buffer-layout.ts +62 -0
  270. package/src/adapter/types/parameters.ts +250 -0
  271. package/src/adapter/types/shader-layout.ts +193 -0
  272. package/src/adapter/types/shader-types.ts +77 -0
  273. package/src/adapter/types/texture-formats.ts +168 -0
  274. package/src/adapter/types/types.ts +109 -0
  275. package/src/adapter/types/vertex-formats.ts +91 -0
  276. package/src/index.ts +160 -92
  277. package/src/init.ts +47 -0
  278. package/src/lib/compiler-log/compiler-message.ts +10 -0
  279. package/src/lib/compiler-log/format-compiler-log.ts +113 -0
  280. package/src/lib/compiler-log/get-shader-info.ts +41 -0
  281. package/src/lib/luma.ts +84 -0
  282. package/src/lib/uniforms/uniform-block.ts +80 -0
  283. package/src/lib/uniforms/uniform-buffer-layout.ts +109 -0
  284. package/src/lib/uniforms/uniform-store.ts +173 -0
  285. package/src/lib/uniforms/uniform.ts +25 -0
  286. package/src/types.ts +33 -0
  287. package/src/utils/array-equal.ts +33 -0
  288. package/src/utils/array-utils-flat.ts +43 -0
  289. package/src/utils/assert.ts +7 -0
  290. package/src/utils/cast.ts +4 -0
  291. package/src/utils/check-props.ts +74 -0
  292. package/src/utils/deep-equal.ts +47 -0
  293. package/src/utils/format-value.ts +40 -0
  294. package/src/utils/is-array.ts +24 -0
  295. package/src/utils/load-file.ts +89 -0
  296. package/src/utils/log.ts +4 -0
  297. package/src/utils/random.ts +17 -0
  298. package/src/utils/request-animation-frame.ts +15 -0
  299. package/src/utils/stats-manager.ts +23 -0
  300. package/src/utils/stub-methods.ts +20 -0
  301. package/src/utils/utils.ts +36 -0
  302. package/dist/bundle.d.ts +0 -2
  303. package/dist/bundle.d.ts.map +0 -1
  304. package/dist/bundle.js +0 -5
  305. package/dist/bundle.js.map +0 -1
  306. package/src/bundle.ts +0 -4
@@ -0,0 +1,45 @@
1
+ // luma.gl, MIT license
2
+ import type {Device} from '../device';
3
+ import {PrimitiveTopology} from '../types/parameters';
4
+ import {ShaderLayout} from '../types/shader-layout';
5
+ import type {Buffer} from './buffer';
6
+ import {Resource, ResourceProps} from './resource';
7
+
8
+ /** For bindRange */
9
+ export type BufferRange = {
10
+ buffer: Buffer;
11
+ byteOffset?: number;
12
+ byteLength?: number;
13
+ };
14
+
15
+ /** Configures a set of output buffers for pipeline (WebGL 2 only) */
16
+ export type TransformFeedbackProps = ResourceProps & {
17
+ /** Layout of shader (for varyings) */
18
+ layout: ShaderLayout;
19
+ /** Buffer bindings (for varyings) */
20
+ buffers: Record<string, Buffer | BufferRange>;
21
+ };
22
+
23
+ /** Holds a set of output buffers for pipeline (WebGL 2 only) */
24
+ export abstract class TransformFeedback extends Resource<TransformFeedbackProps> {
25
+ static override defaultProps: Required<TransformFeedbackProps> = {
26
+ ...Resource.defaultProps,
27
+ layout: undefined!,
28
+ buffers: {}
29
+ };
30
+
31
+ get [Symbol.toStringTag](): string {
32
+ return 'TransformFeedback';
33
+ }
34
+
35
+ constructor(device: Device, props?: TransformFeedbackProps) {
36
+ super(device, props, TransformFeedback.defaultProps);
37
+ }
38
+
39
+ abstract begin(topology?: PrimitiveTopology): void;
40
+ abstract end(): void;
41
+
42
+ abstract setBuffers(buffers: Record<string, Buffer | BufferRange>): void;
43
+ abstract setBuffer(locationOrName: string | number, bufferOrRange: Buffer | BufferRange): void;
44
+ abstract getBuffer(locationOrName: string | number): Buffer | BufferRange | null;
45
+ }
@@ -0,0 +1,65 @@
1
+ // luma.gl, MIT license
2
+ import type {TypedArray} from '../../types';
3
+ import {
4
+ AttributeInfo,
5
+ getAttributeInfosByLocation
6
+ } from '../attribute-utils/get-attribute-from-layouts';
7
+ import type {Device} from '../device';
8
+ import type {Buffer} from './buffer';
9
+ import type {RenderPass} from './render-pass';
10
+ import type {RenderPipeline} from './render-pipeline';
11
+ import {Resource, ResourceProps} from './resource';
12
+
13
+ /** Properties for initializing a VertexArray */
14
+ export type VertexArrayProps = ResourceProps & {
15
+ renderPipeline: RenderPipeline | null;
16
+ };
17
+
18
+ /**
19
+ * Stores attribute bindings.
20
+ * Makes it easy to share a render pipeline and use separate vertex arrays.
21
+ * @note On WebGL, VertexArray allows non-constant bindings to be performed in advance
22
+ * reducing the number of WebGL calls per draw call.
23
+ * @note On WebGPU this is just a convenience class that collects the bindings.
24
+ */
25
+ export abstract class VertexArray extends Resource<VertexArrayProps> {
26
+ static override defaultProps: Required<VertexArrayProps> = {
27
+ ...Resource.defaultProps,
28
+ renderPipeline: null
29
+ };
30
+
31
+ override get [Symbol.toStringTag](): string {
32
+ return 'VertexArray';
33
+ }
34
+
35
+ /** Max number of vertex attributes */
36
+ readonly maxVertexAttributes: number;
37
+ /** Attribute infos indexed by location - TODO only needed by webgl module? */
38
+ protected readonly attributeInfos: AttributeInfo[];
39
+
40
+ /** Index buffer */
41
+ indexBuffer: Buffer | null = null;
42
+ /** Attributes indexed by buffer slot */
43
+ attributes: (Buffer | TypedArray | null)[];
44
+
45
+ constructor(device: Device, props: VertexArrayProps) {
46
+ super(device, props, VertexArray.defaultProps);
47
+ this.maxVertexAttributes = device.limits.maxVertexAttributes;
48
+ this.attributes = new Array(this.maxVertexAttributes).fill(null);
49
+ this.attributeInfos = getAttributeInfosByLocation(
50
+ props.renderPipeline.shaderLayout,
51
+ props.renderPipeline.bufferLayout,
52
+ this.maxVertexAttributes
53
+ );
54
+ }
55
+
56
+ /** Set attributes (stored on pipeline and set before each call) */
57
+ abstract setIndexBuffer(indices: Buffer | null): void;
58
+ /** Set attributes (stored on pipeline and set before each call) */
59
+ abstract setBuffer(bufferSlot: number, buffer: Buffer | null): void;
60
+ /** Set constant attributes (WebGL only) */
61
+ abstract setConstant(location: number, value: TypedArray | null): void;
62
+
63
+ abstract bindBeforeRender(renderPass: RenderPass): void;
64
+ abstract unbindAfterRender(renderPass: RenderPass): void;
65
+ }
@@ -0,0 +1,82 @@
1
+ // luma.gl, MIT licese
2
+ import {ShaderDataType, ShaderAttributeType} from '../types/shader-types';
3
+ import {VertexFormat, VertexType} from '../types/vertex-formats';
4
+
5
+ /** Information extracted from a ShaderAttributeInfo constant */
6
+ export type ShaderAttributeTypeInfo = {
7
+ /** WGSL-style primitive data type, f32, i32, u32 */
8
+ dataType: ShaderDataType,
9
+ /** Whether this is a normalized integer (that must be used as float) */
10
+ components: 1 | 2 | 3 | 4;
11
+ /** Length in bytes of the data for one vertex */
12
+ byteLength?: number;
13
+ /** Whether this is for integer or float vert */
14
+ integer: boolean;
15
+ /** Whether this data type is signed */
16
+ signed: boolean;
17
+ /** The simplest vertex format that matches the shader attribute's data type */
18
+ defaultVertexFormat?: VertexFormat;
19
+ };
20
+
21
+ /** Decodes a vertex type, returning byte length and flags (integer, signed, normalized) */
22
+ export function decodeShaderAttributeType(attributeType: ShaderAttributeType): ShaderAttributeTypeInfo {
23
+ const [dataType, components] = TYPE_INFO[attributeType];
24
+ const integer: boolean = dataType === 'i32' || dataType === 'u32';
25
+ const signed: boolean = dataType !== 'u32';
26
+
27
+ const byteLength = TYPE_SIZES[dataType] * components;
28
+ const defaultVertexFormat = getCompatibleVertexFormat(dataType, components);
29
+ return {
30
+ dataType,
31
+ components,
32
+ defaultVertexFormat,
33
+ byteLength,
34
+ integer,
35
+ signed
36
+ };
37
+ }
38
+
39
+ /** Get the "default" vertex format for a certain shader data type */
40
+ function getCompatibleVertexFormat(dataType: ShaderDataType, components: 1 | 2 | 3 | 4): VertexFormat {
41
+ let vertexType: VertexType;
42
+ switch (dataType) {
43
+ case 'f32': vertexType = 'float32'; break;
44
+ case 'i32': vertexType = 'sint32'; break;
45
+ case 'u32': vertexType = 'uint32'; break;
46
+ case 'f16':
47
+ return components <= 2 ? 'float16x2' : 'float16x4';
48
+ }
49
+ // TODO logic does not work for float16
50
+ if (components === 1) {
51
+ return vertexType;
52
+ }
53
+ return `${vertexType}x${components}`;
54
+ }
55
+
56
+ /** All valid shader attribute types. A table guarantees exhaustive list and fast execution */
57
+ const TYPE_INFO: Record<ShaderAttributeType, [ShaderDataType, 1 | 2 | 3 | 4]> = {
58
+ f32: ['f32', 1],
59
+ 'vec2<f32>': ['f32', 2],
60
+ 'vec3<f32>': ['f32', 3],
61
+ 'vec4<f32>': ['f32', 4],
62
+ f16: ['f16', 1],
63
+ 'vec2<f16>': ['f16', 2],
64
+ 'vec3<f16>': ['f16', 3],
65
+ 'vec4<f16>': ['f16', 4],
66
+ i32: ['i32', 1],
67
+ 'vec2<i32>': ['i32', 2],
68
+ 'vec3<i32>': ['i32', 3],
69
+ 'vec4<i32>': ['i32', 4],
70
+ u32: ['u32', 1],
71
+ 'vec2<u32>': ['u32', 2],
72
+ 'vec3<u32>': ['u32', 3],
73
+ 'vec4<u32>': ['u32', 4]
74
+ };
75
+
76
+ const TYPE_SIZES: Record<ShaderDataType, number> = {
77
+ f32: 4,
78
+ f16: 2,
79
+ i32: 4,
80
+ u32: 4,
81
+ // 'bool-webgl': 4,
82
+ };
@@ -0,0 +1,62 @@
1
+ import {DataType, NormalizedDataType} from '../types/vertex-formats';
2
+
3
+ export type DecodedVertexType = {
4
+ /** WebGPU data type */
5
+ dataType: DataType,
6
+ /** Length in bytes of the data for one vertex */
7
+ byteLength: number;
8
+ /** Whether this is for integer or float vert */
9
+ integer: boolean;
10
+ /** Whether this data type is signed */
11
+ signed: boolean;
12
+ /** Whether this is a normalized integer (that must be used as float) */
13
+ normalized: boolean;
14
+ };
15
+
16
+ /** Decodes a vertex type, returning byte length and flags (integer, signed, normalized) */
17
+ export function decodeVertexType(type: NormalizedDataType): DecodedVertexType {
18
+ const dataType = TYPE_MAP[type];
19
+ const bytes = getDataTypeBytes(dataType);
20
+ const normalized: boolean = type.includes('norm');
21
+ const integer: boolean = !normalized && !type.startsWith('float');
22
+ const signed: boolean = type.startsWith('s');
23
+ return {
24
+ dataType: TYPE_MAP[type],
25
+ byteLength: bytes,
26
+ integer,
27
+ signed,
28
+ normalized
29
+ };
30
+ }
31
+
32
+ function getDataTypeBytes(type: DataType): number {
33
+ const bytes = TYPE_SIZES[type];
34
+ // assert(bytes);
35
+ return bytes;
36
+ }
37
+
38
+ const TYPE_MAP: Record<NormalizedDataType, DataType> = {
39
+ uint8: 'uint8',
40
+ sint8: 'sint8',
41
+ unorm8: 'uint8',
42
+ snorm8: 'sint8',
43
+ uint16: 'uint16',
44
+ sint16: 'sint16',
45
+ unorm16: 'uint16',
46
+ snorm16: 'sint16',
47
+ float16: 'float16',
48
+ float32: 'float32',
49
+ uint32: 'uint32',
50
+ sint32: 'sint32'
51
+ };
52
+
53
+ const TYPE_SIZES: Record<DataType, number> = {
54
+ uint8: 1,
55
+ sint8: 1,
56
+ uint16: 2,
57
+ sint16: 2,
58
+ float16: 2,
59
+ float32: 4,
60
+ uint32: 4,
61
+ sint32: 4
62
+ };
@@ -0,0 +1,48 @@
1
+ // luma.gl, MIT license
2
+ // Copyright (c) vis.gl contributors
3
+
4
+ import {assert} from '../../utils/assert';
5
+ import {ShaderUniformType, ShaderDataType} from '../types/shader-types';
6
+
7
+ const UNIFORM_FORMATS: Record<ShaderUniformType, {type: ShaderDataType, components: number}> = {
8
+ 'f32': {type: 'f32', components: 1},
9
+ 'i32': {type: 'i32', components: 1},
10
+ 'u32': {type: 'u32', components: 1},
11
+ // 'bool-webgl': {type: 'bool-webgl', components: 1},
12
+ 'vec2<f32>': {type: 'f32', components: 2},
13
+ 'vec3<f32>': {type: 'f32', components: 3},
14
+ 'vec4<f32>': {type: 'f32', components: 4},
15
+ 'vec2<i32>': {type: 'i32', components: 2},
16
+ 'vec3<i32>': {type: 'i32', components: 3},
17
+ 'vec4<i32>': {type: 'i32', components: 4},
18
+ 'vec2<u32>': {type: 'u32', components: 2},
19
+ 'vec3<u32>': {type: 'u32', components: 3},
20
+ 'vec4<u32>': {type: 'u32', components: 4},
21
+ 'mat2x2<f32>': {type: 'f32', components: 4},
22
+ 'mat2x3<f32>': {type: 'f32', components: 6},
23
+ 'mat2x4<f32>': {type: 'f32', components: 8},
24
+ 'mat3x2<f32>': {type: 'f32', components: 6},
25
+ 'mat3x3<f32>': {type: 'f32', components: 9},
26
+ 'mat3x4<f32>': {type: 'f32', components: 12},
27
+ 'mat4x2<f32>': {type: 'f32', components: 8},
28
+ 'mat4x3<f32>': {type: 'f32', components: 12},
29
+ 'mat4x4<f32>': {type: 'f32', components: 16},
30
+ };
31
+
32
+ /** Split a uniform type string into type and components */
33
+ export function decodeShaderUniformType(format: ShaderUniformType): {type: ShaderDataType, components: number} {
34
+ const decoded = UNIFORM_FORMATS[format];
35
+ assert(format);
36
+ return decoded;
37
+ }
38
+
39
+ /** Align offset to 1, 2 or 4 elements (4, 8 or 16 bytes) */
40
+ export function alignTo(size: number, count: number): number {
41
+ // prettier-ignore
42
+ switch (count) {
43
+ case 1: return size; // Pad upwards to even multiple of 2
44
+ case 2: return size + (size % 2); // Pad upwards to even multiple of 2
45
+ default: return size + ((4 - (size % 4)) % 4); // Pad upwards to even multiple of 4
46
+ }
47
+ }
48
+
@@ -0,0 +1,190 @@
1
+ import {TextureFormat} from '../types/texture-formats';
2
+ import {VertexType} from '../types/vertex-formats';
3
+ import {decodeVertexType} from './decode-data-type';
4
+
5
+ const REGEX = /^(rg?b?a?)([0-9]*)([a-z]*)(-srgb)?(-webgl|-unsized)?$/;
6
+
7
+ export type DecodedTextureFormat = {
8
+ format: 'r' | 'rg' | 'rgb' | 'rgba';
9
+ components: 1 | 2 | 3 | 4;
10
+ dataType?: VertexType;
11
+ srgb: boolean;
12
+ webgl: boolean;
13
+ unsized: boolean;
14
+ byteLength: number;
15
+ integer: boolean;
16
+ signed: boolean;
17
+ normalized: boolean;
18
+ }
19
+
20
+ /**
21
+ * Decodes a vertex format, returning type, components, byte length and flags (integer, signed, normalized)
22
+ */
23
+ export function decodeTextureFormat(format: TextureFormat): DecodedTextureFormat {
24
+ const matches = REGEX.exec((format as string));
25
+ if (matches) {
26
+ const [, format, length, type, srgb, suffix] = matches;
27
+ if (format) {
28
+ const dataType = `${type}${length}` as VertexType;
29
+ const decodedType = decodeVertexType(dataType);
30
+ return {
31
+ format: format as 'r' | 'rg' | 'rgb' | 'rgba',
32
+ components: format.length as 1 | 2 | 3 | 4,
33
+ // dataType - overwritten by decodedType
34
+ srgb: srgb === '-srgb',
35
+ unsized: suffix === '-unsized',
36
+ webgl: suffix === '-webgl',
37
+ ...decodedType
38
+ };
39
+ }
40
+ }
41
+
42
+ return decodeNonStandardFormat(format);
43
+ }
44
+
45
+ // https://www.w3.org/TR/webgpu/#texture-format-caps
46
+
47
+ const EXCEPTIONS: Partial<Record<TextureFormat, any>> = {
48
+ // Packed 16 bit formats
49
+ 'rgba4unorm-webgl': {format: 'rgba', bpp: 2},
50
+ 'rgb565unorm-webgl': {format: 'rgb', bpp: 2},
51
+ 'rgb5a1unorm-webgl': {format: 'rgba', bbp: 2},
52
+ // Packed 32 bit formats
53
+ 'rgb9e5ufloat': {format: 'rgb', bbp: 4},
54
+ 'rg11b10ufloat': {format: 'rgb', bbp: 4},
55
+ 'rgb10a2unorm': {format: 'rgba', bbp: 4},
56
+ 'rgb10a2unorm-webgl': {format: 'rgba', bbp: 4},
57
+ // Depth/stencil
58
+ 'stencil8': {components: 1, bpp: 1, a: 'stencil'},
59
+ 'depth16unorm': {components: 1, bpp: 2, a: 'depth'},
60
+ 'depth24plus': {components: 1, bpp: 3, a: 'depth'},
61
+ 'depth32float': {components: 1, bpp: 4, a: 'depth'},
62
+ 'depth24plus-stencil8': {components: 2, bpp: 4, a: 'depth-stencil'},
63
+ // "depth24unorm-stencil8" feature
64
+ 'depth24unorm-stencil8': {components: 2, bpp: 4, a: 'depth-stencil'},
65
+ // "depth32float-stencil8" feature
66
+ 'depth32float-stencil8': {components: 2, bpp: 4, a: 'depth-stencil'}
67
+ };
68
+
69
+ function decodeNonStandardFormat(format: TextureFormat): DecodedTextureFormat {
70
+ const data = EXCEPTIONS[format];
71
+ if (!data) {
72
+ throw new Error(`Unknown format ${format}`);
73
+ }
74
+ return {
75
+ format: data.format || '',
76
+ components: data.components || data.format?.length || 1,
77
+ byteLength: data.bpp || 1,
78
+ srgb: false,
79
+ unsized: false
80
+ } as DecodedTextureFormat;
81
+ }
82
+
83
+ /*
84
+ 'r8unorm': {s: "float"}, // ✓ ✓ ✓ },
85
+ 'r8snorm': {s: "float"}, // ✓ },
86
+ 'r8uint': {s: "uint"}, // ✓ ✓ },
87
+ 'r8sint': {s: "sint"}, // ✓ ✓ },
88
+ 'rg8unorm': {s: "float"}, // ✓ ✓ ✓ },
89
+ 'rg8snorm': {s: "float"}, // ✓ },
90
+ 'rg8uint': {s: "uint"}, // ✓ ✓ },
91
+ 'rg8sint': {s: "sint"}, // ✓ ✓ },
92
+ 'rgba8unorm': {s: "float"}, // ✓ ✓ ✓ ✓},
93
+ 'rgba8unorm-srgb': {s: "float"}, // ✓ ✓ ✓ },
94
+ 'rgba8snorm': {s: "float"}, // ✓ ✓},
95
+ 'rgba8uint': {s: "uint"}, // ✓ ✓ ✓},
96
+ 'rgba8sint': {s: "sint"}, // ✓ ✓ ✓},
97
+ 'bgra8unorm': {s: "float"}, // ✓ ✓ ✓ },
98
+ 'bgra8unorm-srgb': {s: "float"}, // ✓ ✓ ✓ },
99
+ // 16-bit per component
100
+ 'r16uint': {s: "uint"}, // ✓ ✓ },
101
+ 'r16sint': {s: "sint"}, // ✓ ✓ },
102
+ 'r16float': {s: "float"}, // ✓ ✓ ✓ },
103
+ 'rg16uint': {s: "uint"}, // ✓ ✓ },
104
+ 'rg16sint': {s: "sint"}, // ✓ ✓ },
105
+ 'rg16float': {s: "float"}, // ✓ ✓ ✓ },
106
+ 'rgba16uint': {s: "uint"}, // ✓ ✓ ✓},
107
+ 'rgba16sint': {s: "sint"}, // ✓ ✓ ✓},
108
+ 'rgba16float': {s: "float"}, // ✓ ✓ ✓ ✓},
109
+ // 32-bit per component
110
+ 'r32uint': {s: "uint"}, // ✓ ✓},
111
+ 'r32sint': {s: "sint"}, // ✓ ✓},
112
+ 'r32float': {"unfilterable-float" ✓ ✓ ✓},
113
+ 'rg32uint': {s: "uint"}, // ✓ ✓},
114
+ 'rg32sint': {s: "sint"}, // ✓ ✓},
115
+ 'rg32float': {"unfilterable-float" ✓ ✓},
116
+ 'rgba32uint': {s: "uint"}, // ✓ ✓},
117
+ 'rgba32sint': {s: "sint"}, // ✓ ✓},
118
+ 'rgba32float': {"unfilterable-float" ✓ ✓},
119
+ // mixed component width
120
+ 'rgb10a2unorm': {s: "float"}, // ✓ ✓ ✓ }
121
+ 'rg11b10ufloat': {s: "float"}, // ✓ }
122
+ // Format Bytes per texel Aspect GPUTextureSampleType Valid image copy source Valid image copy destination
123
+ 'stencil8': {1 − 4 stencil "uint" ✓}
124
+ 'depth16unorm': {2 depth "depth" ✓}
125
+ 'depth24plus': {4 depth "depth" ✗}
126
+ 'depth24plus': {stencil8 4 − 8 depth "depth" ✗}
127
+ 'stencil': {s: "uint"}, // ✓}
128
+ 'depth32float': {4 depth "depth" ✓ ✗}
129
+ 'depth24unorm': {stencil8 4 depth "depth" ✗}
130
+ 'stencil': {s: "uint"}, // ✓}
131
+ 'depth32float': {stencil8}
132
+
133
+ // Format Bytes per block GPUTextureSampleType Block Size Feature
134
+ 'rgb9e5ufloat': {c: 4, s: "float", bpp: 4/(1*1)},
135
+
136
+ 'bc1-rgba-unorm': {c: 4. s: "float", bpp: 8/(4 * 4) f: 'texture-compression-bc'},
137
+ 'bc1-rgba-unorm-srgb': {c: 4. s: "float", bpp: 8/(4 * 4) f: 'texture-compression-bc'},
138
+ 'bc2-rgba-unorm': {c: 4. s: "float", bpp: 16/(4 * 4) f: 'texture-compression-bc'},
139
+ 'bc2-rgba-unorm-srgb': {c: 4. s: "float", bpp: 16/(4 * 4) f: 'texture-compression-bc'},
140
+ 'bc3-rgba-unorm': {c: 4. s: "float", bpp: 16/(4 * 4) f: 'texture-compression-bc'},
141
+ 'bc3-rgba-unorm-srgb': {c: 4. s: "float", bpp: 16/(4 * 4) f: 'texture-compression-bc'},
142
+ 'bc4-r-unorm': {c: 1. s: "float", bpp: 8/(4 * 4) f: 'texture-compression-bc'},
143
+ 'bc4-r-snorm': {c: 1. s: "float", bpp: 8/(4 * 4) f: 'texture-compression-bc'},
144
+ 'bc5-rg-unorm': {c: 2. s: "float", bpp: 16/(4 * 4) f: 'texture-compression-bc'},
145
+ 'bc5-rg-snorm': { },
146
+ 'bc6h-rgb-ufloat': { 16 },
147
+ 'bc6h-rgb-float': { },
148
+ 'bc7-rgba-unorm': { 16 },
149
+ 'bc7-rgba-unorm-srgb': { },
150
+
151
+ 'etc2-rgb8unorm': { 8 "float" 4 × 4 texture-compression-etc2 },
152
+ 'etc2-rgb8unorm-srgb': { },
153
+ 'etc2-rgb8a1unorm': { 8 },
154
+ 'etc2-rgb8a1unorm-srgb': { },
155
+ 'etc2-rgba8unorm': { 16 },
156
+ 'etc2-rgba8unorm-srgb': { },
157
+
158
+ 'eac-r11unorm': { 8 },
159
+ 'eac-r11snorm': { },
160
+ 'eac-rg11unorm': { 16 },
161
+ 'eac-rg11snorm': { },
162
+
163
+ 'astc-4x4-unorm': { 16 "float" 4 × 4 texture-compression-astc },
164
+ 'astc-4x4-unorm-srgb': { },
165
+ 'astc-5x4-unorm': { 16 5 × 4 },
166
+ 'astc-5x4-unorm-srgb': { },
167
+ 'astc-5x5-unorm': { 16 5 × 5 },
168
+ 'astc-5x5-unorm-srgb': { },
169
+ 'astc-6x5-unorm': { 16 6 × 5 },
170
+ 'astc-6x5-unorm-srgb': { },
171
+ 'astc-6x6-unorm': { 16 6 × 6 },
172
+ 'astc-6x6-unorm-srgb': { },
173
+ 'astc-8x5-unorm': { 16 8 × 5 },
174
+ 'astc-8x5-unorm-srgb': { },
175
+ 'astc-8x6-unorm': { 16 8 × 6 },
176
+ 'astc-8x6-unorm-srgb': { },
177
+ 'astc-8x8-unorm': { 16 8 × 8 },
178
+ 'astc-8x8-unorm-srgb': { },
179
+ 'astc-10x5-unorm': { 16 10 × 5 },
180
+ 'astc-10x5-unorm-srgb': { },
181
+ 'astc-10x6-unorm': { 16 10 × 6 },
182
+ 'astc-10x6-unorm-srgb': { },
183
+ 'astc-10x8-unorm': { 16 10 × 8 },
184
+ 'astc-10x8-unorm-srgb': { },
185
+ 'astc-10x10-unorm': { 16 10 × 10 },
186
+ 'astc-10x10-unorm-srgb': { },
187
+ 'astc-12x10-unorm': { 16 12 × 10 },
188
+ 'astc-12x10-unorm-srgb': { },
189
+ 'astc-12x12-unorm': { 16 },
190
+ */
@@ -0,0 +1,49 @@
1
+ import {VertexFormat, VertexType} from '../types/vertex-formats';
2
+ import {decodeVertexType} from './decode-data-type';
3
+
4
+ export type VertexFormatInfo = {
5
+ /** Length in bytes */
6
+ byteLength: number;
7
+ /** Type of each component */
8
+ type: VertexType;
9
+ /** Number of components per vertex / row */
10
+ components: 1 | 2 | 3 | 4;
11
+ /** Is this an integer format (normalized integer formats are not integer) */
12
+ integer: boolean;
13
+ /** Is this a signed format? */
14
+ signed: boolean;
15
+ /** Is this a normalized format? */
16
+ normalized: boolean;
17
+ /** Is this a webgl only format? */
18
+ webglOnly?: boolean;
19
+ };
20
+
21
+ /**
22
+ * Decodes a vertex format, returning type, components, byte length and flags (integer, signed, normalized)
23
+ */
24
+ export function decodeVertexFormat(format: VertexFormat): VertexFormatInfo {
25
+ // Strip the -webgl ending if present
26
+ let webglOnly: boolean | undefined;
27
+ if (format.endsWith('-webgl')) {
28
+ format.replace('-webgl', '');
29
+ webglOnly = true;
30
+ }
31
+ // split components from type
32
+ const [type_, count] = format.split('x');
33
+ const type = type_ as VertexType;
34
+ const components = (count ? parseInt(count) : 1) as 1 | 2 | 3 | 4;
35
+ // decode the type
36
+ const decodedType = decodeVertexType(type);
37
+ const result: VertexFormatInfo = {
38
+ type,
39
+ components,
40
+ byteLength: decodedType.byteLength * components,
41
+ integer: decodedType.integer,
42
+ signed: decodedType.signed,
43
+ normalized: decodedType.normalized
44
+ };
45
+ if (webglOnly) {
46
+ result.webglOnly = true;
47
+ }
48
+ return result;
49
+ }
@@ -0,0 +1,103 @@
1
+ // luma.gl, MIT licese
2
+ import {TypedArray, TypedArrayConstructor} from '../..';
3
+ import {VertexFormat} from '../types/vertex-formats';
4
+
5
+ // import {DataType} from '../types/vertex-formats';
6
+ // type Omit<DataType, 'float16'> unfortunately breaks Typescript inferance
7
+ type DataType = 'uint8' | 'sint8' | 'uint16' | 'sint16' | 'uint32' | 'sint32' | 'float32';
8
+ type DataTypeNorm = 'unorm8' | 'snorm8' | 'unorm16' | 'snorm16';
9
+
10
+ export function getDataTypeFromTypedArray(
11
+ arrayOrType: TypedArray | TypedArrayConstructor
12
+ ): DataType {
13
+ const type = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;
14
+ switch (type) {
15
+ case Float32Array:
16
+ return 'float32';
17
+ case Uint16Array:
18
+ return 'uint16';
19
+ case Uint32Array:
20
+ return 'uint32';
21
+ case Uint8Array:
22
+ case Uint8ClampedArray:
23
+ return 'uint8';
24
+ case Int8Array:
25
+ return 'sint8';
26
+ case Int16Array:
27
+ return 'sint16';
28
+ case Int32Array:
29
+ return 'sint32';
30
+ default:
31
+ // Failed to deduce data type from typed array
32
+ throw new Error(type.constructor.name);
33
+ }
34
+ }
35
+
36
+ export function getTypedArrayFromDataType(
37
+ dataType: DataType | DataTypeNorm
38
+ ): TypedArrayConstructor {
39
+ switch (dataType) {
40
+ case 'float32':
41
+ return Float32Array;
42
+ case 'uint32':
43
+ return Uint32Array;
44
+ case 'sint32':
45
+ return Int32Array;
46
+ case 'uint16':
47
+ case 'unorm16':
48
+ return Uint16Array;
49
+ case 'sint16':
50
+ case 'snorm16':
51
+ return Int16Array;
52
+ case 'uint8':
53
+ case 'unorm8':
54
+ return Uint8Array;
55
+ case 'sint8':
56
+ case 'snorm8':
57
+ return Int8Array;
58
+ default:
59
+ // Failed to deduce typed array from data type
60
+ throw new Error(dataType);
61
+ }
62
+ }
63
+
64
+ /** Get the vertex format for an attribute with TypedArray and size */
65
+ export function getVertexFormatFromAttribute(
66
+ typedArray: TypedArray,
67
+ size: number,
68
+ normalized?: boolean
69
+ ): VertexFormat {
70
+ if (!size || size > 4) {
71
+ throw new Error(`size ${size}`);
72
+ }
73
+
74
+ const components = size as 1 | 2 | 3 | 4;
75
+ let dataType: DataType | DataTypeNorm = getDataTypeFromTypedArray(typedArray);
76
+
77
+ if (dataType === 'uint8' || dataType === 'sint8') {
78
+ if (components === 1 || components === 3) {
79
+ // WebGPU 8 bit formats must be aligned to 16 bit boundaries');
80
+ throw new Error(`size: ${size}`);
81
+ }
82
+ if (normalized) {
83
+ dataType = dataType.replace('int', 'norm') as 'unorm8' | 'snorm8';
84
+ }
85
+ return `${dataType}x${components}`;
86
+ }
87
+ if (dataType === 'uint16' || dataType === 'sint16') {
88
+ if (components === 1 || components === 3) {
89
+ // WebGPU 16 bit formats must be aligned to 32 bit boundaries
90
+ throw new Error(`size: ${size}`);
91
+ }
92
+ if (normalized) {
93
+ dataType = dataType.replace('int', 'norm') as 'unorm16' | 'snorm16';
94
+ }
95
+ return `${dataType}x${components}`;
96
+ }
97
+
98
+ if (components === 1) {
99
+ return dataType;
100
+ }
101
+
102
+ return `${dataType}x${components}`;
103
+ }
@@ -0,0 +1,18 @@
1
+ import { ShaderAttributeType } from '../types/shader-types';
2
+
3
+ /** Predeclared aliases @see https://www.w3.org/TR/WGSL/#vector-types */
4
+ export const WGSL_TYPE_ALIAS_MAP: Record<string, ShaderAttributeType> = {
5
+ vec2i: 'vec2<i32>',
6
+ vec3i: 'vec3<i32>',
7
+ vec4i: 'vec4<i32>',
8
+ vec2u: 'vec2<u32>',
9
+ vec3u: 'vec3<u32>',
10
+ vec4u: 'vec4<u32>',
11
+ vec2f: 'vec2<f32>',
12
+ vec3f: 'vec3<f32>',
13
+ vec4f: 'vec4<f32>',
14
+ // Requires the f16 extension.
15
+ vec2h: 'vec2<f16>',
16
+ vec3h: 'vec3<f16>',
17
+ vec4h: 'vec4<f16>',
18
+ };