@luma.gl/core 9.3.0-alpha.4 → 9.3.0-alpha.8

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 (252) hide show
  1. package/dist/adapter/canvas-context.d.ts +6 -182
  2. package/dist/adapter/canvas-context.d.ts.map +1 -1
  3. package/dist/adapter/canvas-context.js +5 -481
  4. package/dist/adapter/canvas-context.js.map +1 -1
  5. package/dist/adapter/canvas-observer.d.ts +32 -0
  6. package/dist/adapter/canvas-observer.d.ts.map +1 -0
  7. package/dist/adapter/canvas-observer.js +90 -0
  8. package/dist/adapter/canvas-observer.js.map +1 -0
  9. package/dist/adapter/canvas-surface.d.ts +150 -0
  10. package/dist/adapter/canvas-surface.d.ts.map +1 -0
  11. package/dist/adapter/canvas-surface.js +392 -0
  12. package/dist/adapter/canvas-surface.js.map +1 -0
  13. package/dist/adapter/device.d.ts +76 -11
  14. package/dist/adapter/device.d.ts.map +1 -1
  15. package/dist/adapter/device.js +180 -9
  16. package/dist/adapter/device.js.map +1 -1
  17. package/dist/adapter/luma.js +1 -1
  18. package/dist/adapter/presentation-context.d.ts +11 -0
  19. package/dist/adapter/presentation-context.d.ts.map +1 -0
  20. package/dist/adapter/presentation-context.js +12 -0
  21. package/dist/adapter/presentation-context.js.map +1 -0
  22. package/dist/adapter/resources/buffer.d.ts +1 -1
  23. package/dist/adapter/resources/buffer.d.ts.map +1 -1
  24. package/dist/adapter/resources/buffer.js +14 -6
  25. package/dist/adapter/resources/buffer.js.map +1 -1
  26. package/dist/adapter/resources/command-buffer.d.ts +3 -1
  27. package/dist/adapter/resources/command-buffer.d.ts.map +1 -1
  28. package/dist/adapter/resources/command-buffer.js +3 -1
  29. package/dist/adapter/resources/command-buffer.js.map +1 -1
  30. package/dist/adapter/resources/command-encoder.d.ts +25 -2
  31. package/dist/adapter/resources/command-encoder.d.ts.map +1 -1
  32. package/dist/adapter/resources/command-encoder.js +68 -2
  33. package/dist/adapter/resources/command-encoder.js.map +1 -1
  34. package/dist/adapter/resources/compute-pipeline.d.ts +2 -2
  35. package/dist/adapter/resources/compute-pipeline.d.ts.map +1 -1
  36. package/dist/adapter/resources/fence.d.ts +1 -1
  37. package/dist/adapter/resources/fence.d.ts.map +1 -1
  38. package/dist/adapter/resources/fence.js +3 -1
  39. package/dist/adapter/resources/fence.js.map +1 -1
  40. package/dist/adapter/resources/framebuffer.d.ts +1 -1
  41. package/dist/adapter/resources/framebuffer.d.ts.map +1 -1
  42. package/dist/adapter/resources/query-set.d.ts +17 -1
  43. package/dist/adapter/resources/query-set.d.ts.map +1 -1
  44. package/dist/adapter/resources/query-set.js.map +1 -1
  45. package/dist/adapter/resources/render-pipeline.d.ts +28 -10
  46. package/dist/adapter/resources/render-pipeline.d.ts.map +1 -1
  47. package/dist/adapter/resources/render-pipeline.js +21 -2
  48. package/dist/adapter/resources/render-pipeline.js.map +1 -1
  49. package/dist/adapter/resources/resource.d.ts +8 -0
  50. package/dist/adapter/resources/resource.d.ts.map +1 -1
  51. package/dist/adapter/resources/resource.js +240 -14
  52. package/dist/adapter/resources/resource.js.map +1 -1
  53. package/dist/adapter/resources/shared-render-pipeline.d.ts +22 -0
  54. package/dist/adapter/resources/shared-render-pipeline.d.ts.map +1 -0
  55. package/dist/adapter/resources/shared-render-pipeline.js +25 -0
  56. package/dist/adapter/resources/shared-render-pipeline.js.map +1 -0
  57. package/dist/adapter/resources/texture-view.d.ts +1 -1
  58. package/dist/adapter/resources/texture-view.d.ts.map +1 -1
  59. package/dist/adapter/resources/texture.d.ts +82 -15
  60. package/dist/adapter/resources/texture.d.ts.map +1 -1
  61. package/dist/adapter/resources/texture.js +186 -33
  62. package/dist/adapter/resources/texture.js.map +1 -1
  63. package/dist/adapter/types/attachments.d.ts +1 -1
  64. package/dist/adapter/types/attachments.d.ts.map +1 -1
  65. package/dist/adapter/types/buffer-layout.d.ts +1 -1
  66. package/dist/adapter/types/buffer-layout.d.ts.map +1 -1
  67. package/dist/adapter/types/parameters.d.ts +3 -1
  68. package/dist/adapter/types/parameters.d.ts.map +1 -1
  69. package/dist/adapter/types/parameters.js +1 -0
  70. package/dist/adapter/types/parameters.js.map +1 -1
  71. package/dist/adapter/types/shader-layout.d.ts +10 -6
  72. package/dist/adapter/types/shader-layout.d.ts.map +1 -1
  73. package/dist/adapter/types/uniforms.d.ts +6 -0
  74. package/dist/adapter/types/uniforms.d.ts.map +1 -1
  75. package/dist/adapter-utils/bind-groups.d.ts +9 -0
  76. package/dist/adapter-utils/bind-groups.d.ts.map +1 -0
  77. package/dist/adapter-utils/bind-groups.js +41 -0
  78. package/dist/adapter-utils/bind-groups.js.map +1 -0
  79. package/dist/adapter-utils/get-attribute-from-layouts.d.ts +2 -2
  80. package/dist/adapter-utils/get-attribute-from-layouts.d.ts.map +1 -1
  81. package/dist/adapter-utils/get-attribute-from-layouts.js +6 -6
  82. package/dist/adapter-utils/get-attribute-from-layouts.js.map +1 -1
  83. package/dist/dist.dev.js +1934 -415
  84. package/dist/dist.min.js +6 -6
  85. package/dist/factories/bind-group-factory.d.ts +20 -0
  86. package/dist/factories/bind-group-factory.d.ts.map +1 -0
  87. package/dist/factories/bind-group-factory.js +79 -0
  88. package/dist/factories/bind-group-factory.js.map +1 -0
  89. package/dist/factories/core-module-state.d.ts +7 -0
  90. package/dist/factories/core-module-state.d.ts.map +1 -0
  91. package/dist/{shadertypes/data-types/shader-types.js → factories/core-module-state.js} +1 -1
  92. package/dist/factories/core-module-state.js.map +1 -0
  93. package/dist/factories/pipeline-factory.d.ts +54 -0
  94. package/dist/factories/pipeline-factory.d.ts.map +1 -0
  95. package/dist/factories/pipeline-factory.js +270 -0
  96. package/dist/factories/pipeline-factory.js.map +1 -0
  97. package/dist/factories/shader-factory.d.ts +20 -0
  98. package/dist/factories/shader-factory.d.ts.map +1 -0
  99. package/dist/factories/shader-factory.js +84 -0
  100. package/dist/factories/shader-factory.js.map +1 -0
  101. package/dist/index.cjs +1858 -409
  102. package/dist/index.cjs.map +4 -4
  103. package/dist/index.d.ts +25 -14
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +15 -9
  106. package/dist/index.js.map +1 -1
  107. package/dist/portable/uniform-block.d.ts +1 -1
  108. package/dist/portable/uniform-block.d.ts.map +1 -1
  109. package/dist/portable/uniform-buffer-layout.d.ts +20 -15
  110. package/dist/portable/uniform-buffer-layout.d.ts.map +1 -1
  111. package/dist/portable/uniform-buffer-layout.js +188 -43
  112. package/dist/portable/uniform-buffer-layout.js.map +1 -1
  113. package/dist/portable/uniform-store.d.ts +5 -6
  114. package/dist/portable/uniform-store.d.ts.map +1 -1
  115. package/dist/portable/uniform-store.js +6 -3
  116. package/dist/portable/uniform-store.js.map +1 -1
  117. package/dist/shadertypes/data-types/data-type-decoder.d.ts +20 -0
  118. package/dist/shadertypes/data-types/data-type-decoder.d.ts.map +1 -0
  119. package/dist/shadertypes/data-types/data-type-decoder.js +79 -0
  120. package/dist/shadertypes/data-types/data-type-decoder.js.map +1 -0
  121. package/dist/shadertypes/data-types/data-types.d.ts +31 -12
  122. package/dist/shadertypes/data-types/data-types.d.ts.map +1 -1
  123. package/dist/{image-utils → shadertypes/image-types}/image-types.d.ts +0 -6
  124. package/dist/shadertypes/image-types/image-types.d.ts.map +1 -0
  125. package/dist/shadertypes/image-types/image-types.js.map +1 -0
  126. package/dist/shadertypes/shader-types/shader-type-decoder.d.ts +41 -0
  127. package/dist/shadertypes/shader-types/shader-type-decoder.d.ts.map +1 -0
  128. package/dist/shadertypes/{data-types/decode-shader-types.js → shader-types/shader-type-decoder.js} +43 -4
  129. package/dist/shadertypes/shader-types/shader-type-decoder.js.map +1 -0
  130. package/dist/shadertypes/shader-types/shader-types.d.ts +101 -0
  131. package/dist/shadertypes/shader-types/shader-types.d.ts.map +1 -0
  132. package/dist/shadertypes/shader-types/shader-types.js +30 -0
  133. package/dist/shadertypes/shader-types/shader-types.js.map +1 -0
  134. package/dist/shadertypes/texture-types/pixel-utils.d.ts.map +1 -0
  135. package/dist/shadertypes/texture-types/pixel-utils.js.map +1 -0
  136. package/dist/shadertypes/{textures → texture-types}/texture-format-decoder.d.ts +1 -0
  137. package/dist/shadertypes/texture-types/texture-format-decoder.d.ts.map +1 -0
  138. package/dist/shadertypes/{textures → texture-types}/texture-format-decoder.js +55 -9
  139. package/dist/shadertypes/texture-types/texture-format-decoder.js.map +1 -0
  140. package/dist/shadertypes/texture-types/texture-format-generics.d.ts +34 -0
  141. package/dist/shadertypes/texture-types/texture-format-generics.d.ts.map +1 -0
  142. package/dist/shadertypes/texture-types/texture-format-generics.js.map +1 -0
  143. package/dist/shadertypes/texture-types/texture-format-table.d.ts.map +1 -0
  144. package/dist/shadertypes/{textures → texture-types}/texture-format-table.js +10 -9
  145. package/dist/shadertypes/texture-types/texture-format-table.js.map +1 -0
  146. package/dist/shadertypes/{textures → texture-types}/texture-formats.d.ts +10 -3
  147. package/dist/shadertypes/texture-types/texture-formats.d.ts.map +1 -0
  148. package/dist/shadertypes/{textures → texture-types}/texture-formats.js +1 -0
  149. package/dist/shadertypes/texture-types/texture-formats.js.map +1 -0
  150. package/dist/shadertypes/{textures → texture-types}/texture-layout.d.ts +1 -1
  151. package/dist/shadertypes/texture-types/texture-layout.d.ts.map +1 -0
  152. package/dist/shadertypes/texture-types/texture-layout.js.map +1 -0
  153. package/dist/shadertypes/vertex-types/vertex-format-decoder.d.ts +24 -0
  154. package/dist/shadertypes/vertex-types/vertex-format-decoder.d.ts.map +1 -0
  155. package/dist/shadertypes/vertex-types/vertex-format-decoder.js +106 -0
  156. package/dist/shadertypes/vertex-types/vertex-format-decoder.js.map +1 -0
  157. package/dist/shadertypes/vertex-types/vertex-formats.d.ts +50 -0
  158. package/dist/shadertypes/vertex-types/vertex-formats.d.ts.map +1 -0
  159. package/dist/shadertypes/vertex-types/vertex-formats.js.map +1 -0
  160. package/dist/utils/array-equal.d.ts +1 -1
  161. package/dist/utils/array-equal.d.ts.map +1 -1
  162. package/dist/utils/array-equal.js +15 -9
  163. package/dist/utils/array-equal.js.map +1 -1
  164. package/dist/utils/stats-manager.d.ts.map +1 -1
  165. package/dist/utils/stats-manager.js +61 -1
  166. package/dist/utils/stats-manager.js.map +1 -1
  167. package/package.json +3 -3
  168. package/src/adapter/canvas-context.ts +7 -623
  169. package/src/adapter/canvas-observer.ts +130 -0
  170. package/src/adapter/canvas-surface.ts +521 -0
  171. package/src/adapter/device.ts +287 -21
  172. package/src/adapter/presentation-context.ts +16 -0
  173. package/src/adapter/resources/buffer.ts +13 -5
  174. package/src/adapter/resources/command-buffer.ts +3 -1
  175. package/src/adapter/resources/command-encoder.ts +94 -3
  176. package/src/adapter/resources/compute-pipeline.ts +2 -2
  177. package/src/adapter/resources/fence.ts +3 -1
  178. package/src/adapter/resources/framebuffer.ts +1 -1
  179. package/src/adapter/resources/query-set.ts +17 -1
  180. package/src/adapter/resources/render-pipeline.ts +52 -16
  181. package/src/adapter/resources/resource.ts +284 -14
  182. package/src/adapter/resources/shared-render-pipeline.ts +40 -0
  183. package/src/adapter/resources/texture-view.ts +1 -1
  184. package/src/adapter/resources/texture.ts +273 -43
  185. package/src/adapter/types/attachments.ts +1 -1
  186. package/src/adapter/types/buffer-layout.ts +1 -1
  187. package/src/adapter/types/parameters.ts +4 -1
  188. package/src/adapter/types/shader-layout.ts +15 -9
  189. package/src/adapter/types/uniforms.ts +12 -0
  190. package/src/adapter-utils/bind-groups.ts +71 -0
  191. package/src/adapter-utils/get-attribute-from-layouts.ts +8 -11
  192. package/src/factories/bind-group-factory.ts +139 -0
  193. package/src/factories/core-module-state.ts +11 -0
  194. package/src/factories/pipeline-factory.ts +328 -0
  195. package/src/factories/shader-factory.ts +103 -0
  196. package/src/index.ts +54 -26
  197. package/src/portable/uniform-block.ts +1 -1
  198. package/src/portable/uniform-buffer-layout.ts +269 -62
  199. package/src/portable/uniform-store.ts +14 -11
  200. package/src/shadertypes/data-types/data-type-decoder.ts +105 -0
  201. package/src/shadertypes/data-types/data-types.ts +100 -48
  202. package/src/{image-utils → shadertypes/image-types}/image-types.ts +0 -7
  203. package/src/shadertypes/{data-types/decode-shader-types.ts → shader-types/shader-type-decoder.ts} +88 -14
  204. package/src/shadertypes/shader-types/shader-types.ts +207 -0
  205. package/src/shadertypes/{textures → texture-types}/texture-format-decoder.ts +75 -9
  206. package/src/shadertypes/{textures → texture-types}/texture-format-generics.ts +42 -48
  207. package/src/shadertypes/{textures → texture-types}/texture-format-table.ts +10 -9
  208. package/src/shadertypes/{textures → texture-types}/texture-formats.ts +20 -3
  209. package/src/shadertypes/vertex-types/vertex-format-decoder.ts +131 -0
  210. package/src/shadertypes/vertex-types/vertex-formats.ts +183 -0
  211. package/src/utils/array-equal.ts +21 -9
  212. package/src/utils/stats-manager.ts +76 -2
  213. package/dist/image-utils/image-types.d.ts.map +0 -1
  214. package/dist/image-utils/image-types.js.map +0 -1
  215. package/dist/shadertypes/data-types/decode-shader-types.d.ts +0 -17
  216. package/dist/shadertypes/data-types/decode-shader-types.d.ts.map +0 -1
  217. package/dist/shadertypes/data-types/decode-shader-types.js.map +0 -1
  218. package/dist/shadertypes/data-types/shader-types.d.ts +0 -43
  219. package/dist/shadertypes/data-types/shader-types.d.ts.map +0 -1
  220. package/dist/shadertypes/data-types/shader-types.js.map +0 -1
  221. package/dist/shadertypes/textures/pixel-utils.d.ts.map +0 -1
  222. package/dist/shadertypes/textures/pixel-utils.js.map +0 -1
  223. package/dist/shadertypes/textures/texture-format-decoder.d.ts.map +0 -1
  224. package/dist/shadertypes/textures/texture-format-decoder.js.map +0 -1
  225. package/dist/shadertypes/textures/texture-format-generics.d.ts +0 -33
  226. package/dist/shadertypes/textures/texture-format-generics.d.ts.map +0 -1
  227. package/dist/shadertypes/textures/texture-format-generics.js.map +0 -1
  228. package/dist/shadertypes/textures/texture-format-table.d.ts.map +0 -1
  229. package/dist/shadertypes/textures/texture-format-table.js.map +0 -1
  230. package/dist/shadertypes/textures/texture-formats.d.ts.map +0 -1
  231. package/dist/shadertypes/textures/texture-formats.js.map +0 -1
  232. package/dist/shadertypes/textures/texture-layout.d.ts.map +0 -1
  233. package/dist/shadertypes/textures/texture-layout.js.map +0 -1
  234. package/dist/shadertypes/vertex-arrays/decode-vertex-format.d.ts +0 -18
  235. package/dist/shadertypes/vertex-arrays/decode-vertex-format.d.ts.map +0 -1
  236. package/dist/shadertypes/vertex-arrays/decode-vertex-format.js +0 -100
  237. package/dist/shadertypes/vertex-arrays/decode-vertex-format.js.map +0 -1
  238. package/dist/shadertypes/vertex-arrays/vertex-formats.d.ts +0 -27
  239. package/dist/shadertypes/vertex-arrays/vertex-formats.d.ts.map +0 -1
  240. package/dist/shadertypes/vertex-arrays/vertex-formats.js.map +0 -1
  241. package/src/shadertypes/data-types/shader-types.ts +0 -94
  242. package/src/shadertypes/vertex-arrays/decode-vertex-format.ts +0 -124
  243. package/src/shadertypes/vertex-arrays/vertex-formats.ts +0 -91
  244. /package/dist/{image-utils → shadertypes/image-types}/image-types.js +0 -0
  245. /package/dist/shadertypes/{textures → texture-types}/pixel-utils.d.ts +0 -0
  246. /package/dist/shadertypes/{textures → texture-types}/pixel-utils.js +0 -0
  247. /package/dist/shadertypes/{textures → texture-types}/texture-format-generics.js +0 -0
  248. /package/dist/shadertypes/{textures → texture-types}/texture-format-table.d.ts +0 -0
  249. /package/dist/shadertypes/{textures → texture-types}/texture-layout.js +0 -0
  250. /package/dist/shadertypes/{vertex-arrays → vertex-types}/vertex-formats.js +0 -0
  251. /package/src/shadertypes/{textures → texture-types}/pixel-utils.ts +0 -0
  252. /package/src/shadertypes/{textures → texture-types}/texture-layout.ts +0 -0
@@ -0,0 +1,139 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {
6
+ Bindings,
7
+ BindingsByGroup,
8
+ ComputeShaderLayout,
9
+ ShaderLayout
10
+ } from '../adapter/types/shader-layout';
11
+ import type {Device} from '../adapter/device';
12
+ import type {ComputePipeline} from '../adapter/resources/compute-pipeline';
13
+ import type {RenderPipeline} from '../adapter/resources/render-pipeline';
14
+ import {normalizeBindingsByGroup} from '../adapter-utils/bind-groups';
15
+
16
+ type AnyPipeline = RenderPipeline | ComputePipeline;
17
+ type AnyShaderLayout = ShaderLayout | ComputeShaderLayout;
18
+ type BindGroupCacheKeys = Partial<Record<number, object>>;
19
+ type BindGroupMap = Partial<Record<number, unknown>>;
20
+ type LayoutCache = Partial<Record<number, object>>;
21
+ type LayoutBindGroupCache = {
22
+ bindGroupsBySource: WeakMap<object, unknown | null>;
23
+ emptyBindGroup?: unknown;
24
+ };
25
+
26
+ export class BindGroupFactory {
27
+ readonly device: Device;
28
+
29
+ private readonly _layoutCacheByPipeline: WeakMap<AnyPipeline, LayoutCache> = new WeakMap();
30
+ private readonly _bindGroupCacheByLayout: WeakMap<object, LayoutBindGroupCache> = new WeakMap();
31
+
32
+ constructor(device: Device) {
33
+ this.device = device;
34
+ }
35
+
36
+ getBindGroups(
37
+ pipeline: AnyPipeline,
38
+ bindings?: Bindings | BindingsByGroup,
39
+ bindGroupCacheKeys?: BindGroupCacheKeys
40
+ ): BindGroupMap {
41
+ if (this.device.type !== 'webgpu' || pipeline.shaderLayout.bindings.length === 0) {
42
+ return {};
43
+ }
44
+
45
+ const bindingsByGroup = normalizeBindingsByGroup(pipeline.shaderLayout, bindings);
46
+ const resolvedBindGroups: BindGroupMap = {};
47
+
48
+ for (const group of getBindGroupIndicesUpToMax(pipeline.shaderLayout.bindings)) {
49
+ const groupBindings = bindingsByGroup[group];
50
+ const bindGroupLayout = this._getBindGroupLayout(pipeline, group);
51
+
52
+ if (!groupBindings || Object.keys(groupBindings).length === 0) {
53
+ if (!hasBindingsInGroup(pipeline.shaderLayout.bindings, group)) {
54
+ resolvedBindGroups[group] = this._getEmptyBindGroup(
55
+ bindGroupLayout,
56
+ pipeline.shaderLayout,
57
+ group
58
+ );
59
+ }
60
+ continue;
61
+ }
62
+
63
+ const bindGroupCacheKey = bindGroupCacheKeys?.[group];
64
+ if (bindGroupCacheKey) {
65
+ const layoutCache = this._getLayoutBindGroupCache(bindGroupLayout);
66
+ if (layoutCache.bindGroupsBySource.has(bindGroupCacheKey)) {
67
+ resolvedBindGroups[group] = layoutCache.bindGroupsBySource.get(bindGroupCacheKey) || null;
68
+ continue;
69
+ }
70
+
71
+ const bindGroup = this.device._createBindGroupWebGPU(
72
+ bindGroupLayout,
73
+ pipeline.shaderLayout,
74
+ groupBindings,
75
+ group
76
+ );
77
+ layoutCache.bindGroupsBySource.set(bindGroupCacheKey, bindGroup);
78
+ resolvedBindGroups[group] = bindGroup;
79
+ } else {
80
+ resolvedBindGroups[group] = this.device._createBindGroupWebGPU(
81
+ bindGroupLayout,
82
+ pipeline.shaderLayout,
83
+ groupBindings,
84
+ group
85
+ );
86
+ }
87
+ }
88
+
89
+ return resolvedBindGroups;
90
+ }
91
+
92
+ private _getBindGroupLayout(pipeline: AnyPipeline, group: number): object {
93
+ let layoutCache = this._layoutCacheByPipeline.get(pipeline);
94
+ if (!layoutCache) {
95
+ layoutCache = {};
96
+ this._layoutCacheByPipeline.set(pipeline, layoutCache);
97
+ }
98
+
99
+ layoutCache[group] ||= this.device._createBindGroupLayoutWebGPU(pipeline, group) as object;
100
+ return layoutCache[group];
101
+ }
102
+
103
+ private _getEmptyBindGroup(
104
+ bindGroupLayout: object,
105
+ shaderLayout: AnyShaderLayout,
106
+ group: number
107
+ ): unknown {
108
+ const layoutCache = this._getLayoutBindGroupCache(bindGroupLayout);
109
+ layoutCache.emptyBindGroup ||=
110
+ this.device._createBindGroupWebGPU(bindGroupLayout, shaderLayout, {}, group) || null;
111
+ return layoutCache.emptyBindGroup;
112
+ }
113
+
114
+ private _getLayoutBindGroupCache(bindGroupLayout: object): LayoutBindGroupCache {
115
+ let layoutCache = this._bindGroupCacheByLayout.get(bindGroupLayout);
116
+ if (!layoutCache) {
117
+ layoutCache = {bindGroupsBySource: new WeakMap()};
118
+ this._bindGroupCacheByLayout.set(bindGroupLayout, layoutCache);
119
+ }
120
+ return layoutCache;
121
+ }
122
+ }
123
+
124
+ export function _getDefaultBindGroupFactory(device: Device): BindGroupFactory {
125
+ device._factories.bindGroupFactory ||= new BindGroupFactory(device);
126
+ return device._factories.bindGroupFactory as BindGroupFactory;
127
+ }
128
+
129
+ function getBindGroupIndicesUpToMax(bindings: AnyShaderLayout['bindings']): number[] {
130
+ const maxGroup = bindings.reduce(
131
+ (highestGroup, binding) => Math.max(highestGroup, binding.group),
132
+ -1
133
+ );
134
+ return Array.from({length: maxGroup + 1}, (_, group) => group);
135
+ }
136
+
137
+ function hasBindingsInGroup(bindings: AnyShaderLayout['bindings'], group: number): boolean {
138
+ return bindings.some(binding => binding.group === group);
139
+ }
@@ -0,0 +1,11 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {PipelineFactory} from './pipeline-factory';
6
+ import type {ShaderFactory} from './shader-factory';
7
+
8
+ export type CoreModuleState = {
9
+ defaultPipelineFactory?: PipelineFactory;
10
+ defaultShaderFactory?: ShaderFactory;
11
+ };
@@ -0,0 +1,328 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Device} from '../adapter/device';
6
+ import {ComputePipeline, type ComputePipelineProps} from '../adapter/resources/compute-pipeline';
7
+ import {RenderPipeline, type RenderPipelineProps} from '../adapter/resources/render-pipeline';
8
+ import {Resource} from '../adapter/resources/resource';
9
+ import type {SharedRenderPipeline} from '../adapter/resources/shared-render-pipeline';
10
+ import {log} from '../utils/log';
11
+ import {uid} from '../utils/uid';
12
+ import type {CoreModuleState} from './core-module-state';
13
+
14
+ export type PipelineFactoryProps = RenderPipelineProps;
15
+
16
+ type CacheItem<ResourceT extends Resource<any>> = {resource: ResourceT; useCount: number};
17
+
18
+ /**
19
+ * Efficiently creates / caches pipelines
20
+ */
21
+ export class PipelineFactory {
22
+ static defaultProps: Required<PipelineFactoryProps> = {...RenderPipeline.defaultProps};
23
+
24
+ /** Get the singleton default pipeline factory for the specified device */
25
+ static getDefaultPipelineFactory(device: Device): PipelineFactory {
26
+ const moduleData = device.getModuleData<CoreModuleState>('@luma.gl/core');
27
+ moduleData.defaultPipelineFactory ||= new PipelineFactory(device);
28
+ return moduleData.defaultPipelineFactory;
29
+ }
30
+
31
+ readonly device: Device;
32
+
33
+ private _hashCounter: number = 0;
34
+ private readonly _hashes: Record<string, number> = {};
35
+ private readonly _renderPipelineCache: Record<string, CacheItem<RenderPipeline>> = {};
36
+ private readonly _computePipelineCache: Record<string, CacheItem<ComputePipeline>> = {};
37
+ private readonly _sharedRenderPipelineCache: Record<string, CacheItem<SharedRenderPipeline>> = {};
38
+
39
+ get [Symbol.toStringTag](): string {
40
+ return 'PipelineFactory';
41
+ }
42
+
43
+ toString(): string {
44
+ return `PipelineFactory(${this.device.id})`;
45
+ }
46
+
47
+ constructor(device: Device) {
48
+ this.device = device;
49
+ }
50
+
51
+ /**
52
+ * WebGL has two cache layers with different priorities:
53
+ * - `_sharedRenderPipelineCache` owns `WEBGLSharedRenderPipeline` / `WebGLProgram` reuse.
54
+ * - `_renderPipelineCache` owns `RenderPipeline` wrapper reuse.
55
+ *
56
+ * Shared WebGL program reuse is the hard requirement. Wrapper reuse is beneficial,
57
+ * but wrapper cache misses are acceptable if that keeps the cache logic simple and
58
+ * prevents incorrect cache hits.
59
+ *
60
+ * In particular, wrapper hash logic must never force program creation or linked-program
61
+ * introspection just to decide whether a shared WebGL program can be reused.
62
+ */
63
+ /** Return a RenderPipeline matching supplied props. Reuses an equivalent pipeline if already created. */
64
+ createRenderPipeline(props: RenderPipelineProps): RenderPipeline {
65
+ if (!this.device.props._cachePipelines) {
66
+ return this.device.createRenderPipeline(props);
67
+ }
68
+
69
+ const allProps: Required<RenderPipelineProps> = {...RenderPipeline.defaultProps, ...props};
70
+
71
+ const cache = this._renderPipelineCache;
72
+ const hash = this._hashRenderPipeline(allProps);
73
+
74
+ let pipeline: RenderPipeline = cache[hash]?.resource;
75
+ if (!pipeline) {
76
+ const sharedRenderPipeline =
77
+ this.device.type === 'webgl' && this.device.props._sharePipelines
78
+ ? this.createSharedRenderPipeline(allProps)
79
+ : undefined;
80
+ pipeline = this.device.createRenderPipeline({
81
+ ...allProps,
82
+ id: allProps.id ? `${allProps.id}-cached` : uid('unnamed-cached'),
83
+ _sharedRenderPipeline: sharedRenderPipeline
84
+ });
85
+ pipeline.hash = hash;
86
+ cache[hash] = {resource: pipeline, useCount: 1};
87
+ if (this.device.props.debugFactories) {
88
+ log.log(3, `${this}: ${pipeline} created, count=${cache[hash].useCount}`)();
89
+ }
90
+ } else {
91
+ cache[hash].useCount++;
92
+ if (this.device.props.debugFactories) {
93
+ log.log(
94
+ 3,
95
+ `${this}: ${cache[hash].resource} reused, count=${cache[hash].useCount}, (id=${props.id})`
96
+ )();
97
+ }
98
+ }
99
+
100
+ return pipeline;
101
+ }
102
+
103
+ /** Return a ComputePipeline matching supplied props. Reuses an equivalent pipeline if already created. */
104
+ createComputePipeline(props: ComputePipelineProps): ComputePipeline {
105
+ if (!this.device.props._cachePipelines) {
106
+ return this.device.createComputePipeline(props);
107
+ }
108
+
109
+ const allProps: Required<ComputePipelineProps> = {...ComputePipeline.defaultProps, ...props};
110
+
111
+ const cache = this._computePipelineCache;
112
+ const hash = this._hashComputePipeline(allProps);
113
+
114
+ let pipeline: ComputePipeline = cache[hash]?.resource;
115
+ if (!pipeline) {
116
+ pipeline = this.device.createComputePipeline({
117
+ ...allProps,
118
+ id: allProps.id ? `${allProps.id}-cached` : undefined
119
+ });
120
+ pipeline.hash = hash;
121
+ cache[hash] = {resource: pipeline, useCount: 1};
122
+ if (this.device.props.debugFactories) {
123
+ log.log(3, `${this}: ${pipeline} created, count=${cache[hash].useCount}`)();
124
+ }
125
+ } else {
126
+ cache[hash].useCount++;
127
+ if (this.device.props.debugFactories) {
128
+ log.log(
129
+ 3,
130
+ `${this}: ${cache[hash].resource} reused, count=${cache[hash].useCount}, (id=${props.id})`
131
+ )();
132
+ }
133
+ }
134
+
135
+ return pipeline;
136
+ }
137
+
138
+ release(pipeline: RenderPipeline | ComputePipeline): void {
139
+ if (!this.device.props._cachePipelines) {
140
+ pipeline.destroy();
141
+ return;
142
+ }
143
+
144
+ const cache = this._getCache(pipeline);
145
+ const hash = pipeline.hash;
146
+
147
+ cache[hash].useCount--;
148
+ if (cache[hash].useCount === 0) {
149
+ this._destroyPipeline(pipeline);
150
+ if (this.device.props.debugFactories) {
151
+ log.log(3, `${this}: ${pipeline} released and destroyed`)();
152
+ }
153
+ } else if (cache[hash].useCount < 0) {
154
+ log.error(`${this}: ${pipeline} released, useCount < 0, resetting`)();
155
+ cache[hash].useCount = 0;
156
+ } else if (this.device.props.debugFactories) {
157
+ log.log(3, `${this}: ${pipeline} released, count=${cache[hash].useCount}`)();
158
+ }
159
+ }
160
+
161
+ createSharedRenderPipeline(props: RenderPipelineProps): SharedRenderPipeline {
162
+ const sharedPipelineHash = this._hashSharedRenderPipeline(props);
163
+ let sharedCacheItem = this._sharedRenderPipelineCache[sharedPipelineHash];
164
+ if (!sharedCacheItem) {
165
+ const sharedRenderPipeline = this.device._createSharedRenderPipelineWebGL(props);
166
+ sharedCacheItem = {resource: sharedRenderPipeline, useCount: 0};
167
+ this._sharedRenderPipelineCache[sharedPipelineHash] = sharedCacheItem;
168
+ }
169
+ sharedCacheItem.useCount++;
170
+ return sharedCacheItem.resource;
171
+ }
172
+
173
+ releaseSharedRenderPipeline(pipeline: RenderPipeline): void {
174
+ if (!pipeline.sharedRenderPipeline) {
175
+ return;
176
+ }
177
+
178
+ const sharedPipelineHash = this._hashSharedRenderPipeline(pipeline.sharedRenderPipeline.props);
179
+ const sharedCacheItem = this._sharedRenderPipelineCache[sharedPipelineHash];
180
+ if (!sharedCacheItem) {
181
+ return;
182
+ }
183
+
184
+ sharedCacheItem.useCount--;
185
+ if (sharedCacheItem.useCount === 0) {
186
+ sharedCacheItem.resource.destroy();
187
+ delete this._sharedRenderPipelineCache[sharedPipelineHash];
188
+ }
189
+ }
190
+
191
+ // PRIVATE
192
+
193
+ /** Destroy a cached pipeline, removing it from the cache if configured to do so. */
194
+ private _destroyPipeline(pipeline: RenderPipeline | ComputePipeline): boolean {
195
+ const cache = this._getCache(pipeline);
196
+
197
+ if (!this.device.props._destroyPipelines) {
198
+ return false;
199
+ }
200
+
201
+ delete cache[pipeline.hash];
202
+ pipeline.destroy();
203
+ if (pipeline instanceof RenderPipeline) {
204
+ this.releaseSharedRenderPipeline(pipeline);
205
+ }
206
+ return true;
207
+ }
208
+
209
+ /** Get the appropriate cache for the type of pipeline */
210
+ private _getCache(
211
+ pipeline: RenderPipeline | ComputePipeline
212
+ ): Record<string, CacheItem<RenderPipeline>> | Record<string, CacheItem<ComputePipeline>> {
213
+ let cache:
214
+ | Record<string, CacheItem<RenderPipeline>>
215
+ | Record<string, CacheItem<ComputePipeline>>
216
+ | undefined;
217
+ if (pipeline instanceof ComputePipeline) {
218
+ cache = this._computePipelineCache;
219
+ }
220
+ if (pipeline instanceof RenderPipeline) {
221
+ cache = this._renderPipelineCache;
222
+ }
223
+ if (!cache) {
224
+ throw new Error(`${this}`);
225
+ }
226
+ if (!cache[pipeline.hash]) {
227
+ throw new Error(`${this}: ${pipeline} matched incorrect entry`);
228
+ }
229
+ return cache;
230
+ }
231
+
232
+ /** Calculate a hash based on all the inputs for a compute pipeline */
233
+ private _hashComputePipeline(props: ComputePipelineProps): string {
234
+ const {type} = this.device;
235
+ const shaderHash = this._getHash(props.shader.source);
236
+ const shaderLayoutHash = this._getHash(JSON.stringify(props.shaderLayout));
237
+ return `${type}/C/${shaderHash}SL${shaderLayoutHash}`;
238
+ }
239
+
240
+ /** Calculate a hash based on all the inputs for a render pipeline */
241
+ private _hashRenderPipeline(props: RenderPipelineProps): string {
242
+ // Backend-specific hashing requirements:
243
+ // - WebGPU hash keys must include every immutable descriptor-shaping input that can
244
+ // change the created `GPURenderPipeline`, including attachment formats.
245
+ // - WebGL hash keys only govern wrapper reuse. They must remain compatible with
246
+ // shared-program reuse and must not depend on linked-program introspection just
247
+ // to decide whether a shared `WebGLProgram` can be reused.
248
+ //
249
+ // General exclusions:
250
+ // - `id`, `handle`: resource identity / caller-supplied handles, not cache shape
251
+ // - `bindings`: mutable per-pipeline compatibility state
252
+ // - `disableWarnings`: logging only, no rendering impact
253
+ // - `vsConstants`, `fsConstants`: currently unused by pipeline creation
254
+ const vsHash = props.vs ? this._getHash(props.vs.source) : 0;
255
+ const fsHash = props.fs ? this._getHash(props.fs.source) : 0;
256
+ const varyingHash = this._getWebGLVaryingHash(props);
257
+ const shaderLayoutHash = this._getHash(JSON.stringify(props.shaderLayout));
258
+ const bufferLayoutHash = this._getHash(JSON.stringify(props.bufferLayout));
259
+
260
+ const {type} = this.device;
261
+ switch (type) {
262
+ case 'webgl':
263
+ // WebGL wrappers preserve default topology and parameter semantics for direct
264
+ // callers, even though the underlying linked program may be shared separately.
265
+ // Future WebGL-only additions here must not turn wrapper reuse into a prerequisite
266
+ // for shared `WebGLProgram` reuse.
267
+ const webglParameterHash = this._getHash(JSON.stringify(props.parameters));
268
+ return `${type}/R/${vsHash}/${fsHash}V${varyingHash}T${props.topology}P${webglParameterHash}SL${shaderLayoutHash}BL${bufferLayoutHash}`;
269
+
270
+ case 'webgpu':
271
+ default:
272
+ // On WebGPU we need to rebuild the pipeline if topology, entry points,
273
+ // shader/layout data, parameters, bufferLayout or attachment formats change.
274
+ // Attachment formats must stay in the key so screen and offscreen passes do not
275
+ // accidentally alias the same cached `GPURenderPipeline`.
276
+ const entryPointHash = this._getHash(
277
+ JSON.stringify({
278
+ vertexEntryPoint: props.vertexEntryPoint,
279
+ fragmentEntryPoint: props.fragmentEntryPoint
280
+ })
281
+ );
282
+ const parameterHash = this._getHash(JSON.stringify(props.parameters));
283
+ const attachmentHash = this._getWebGPUAttachmentHash(props);
284
+ // TODO - Can json.stringify() generate different strings for equivalent objects if order of params is different?
285
+ // create a deepHash() to deduplicate?
286
+ return `${type}/R/${vsHash}/${fsHash}V${varyingHash}T${props.topology}EP${entryPointHash}P${parameterHash}SL${shaderLayoutHash}BL${bufferLayoutHash}A${attachmentHash}`;
287
+ }
288
+ }
289
+
290
+ // This is the only gate for shared `WebGLProgram` reuse.
291
+ // Only include inputs that affect program linking or transform-feedback linkage.
292
+ // Wrapper-only concerns such as topology, parameters, attachment formats and layout
293
+ // overrides must not be added here.
294
+ private _hashSharedRenderPipeline(props: RenderPipelineProps): string {
295
+ const vsHash = props.vs ? this._getHash(props.vs.source) : 0;
296
+ const fsHash = props.fs ? this._getHash(props.fs.source) : 0;
297
+ const varyingHash = this._getWebGLVaryingHash(props);
298
+ return `webgl/S/${vsHash}/${fsHash}V${varyingHash}`;
299
+ }
300
+
301
+ private _getHash(key: string): number {
302
+ if (this._hashes[key] === undefined) {
303
+ this._hashes[key] = this._hashCounter++;
304
+ }
305
+ return this._hashes[key];
306
+ }
307
+
308
+ private _getWebGLVaryingHash(props: RenderPipelineProps): number {
309
+ const {varyings = [], bufferMode = null} = props;
310
+ return this._getHash(JSON.stringify({varyings, bufferMode}));
311
+ }
312
+
313
+ private _getWebGPUAttachmentHash(props: RenderPipelineProps): number {
314
+ const colorAttachmentFormats = props.colorAttachmentFormats ?? [
315
+ this.device.preferredColorFormat
316
+ ];
317
+ const depthStencilAttachmentFormat = props.parameters?.depthWriteEnabled
318
+ ? props.depthStencilAttachmentFormat || this.device.preferredDepthFormat
319
+ : null;
320
+
321
+ return this._getHash(
322
+ JSON.stringify({
323
+ colorAttachmentFormats,
324
+ depthStencilAttachmentFormat
325
+ })
326
+ );
327
+ }
328
+ }
@@ -0,0 +1,103 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Device} from '../adapter/device';
6
+ import {Shader, type ShaderProps} from '../adapter/resources/shader';
7
+ import {log} from '../utils/log';
8
+ import type {CoreModuleState} from './core-module-state';
9
+
10
+ type CacheItem = {resource: Shader; useCount: number};
11
+
12
+ /** Manages a cached pool of Shaders for reuse. */
13
+ export class ShaderFactory {
14
+ static readonly defaultProps: Required<ShaderProps> = {...Shader.defaultProps};
15
+
16
+ /** Returns the default ShaderFactory for the given {@link Device}, creating one if necessary. */
17
+ static getDefaultShaderFactory(device: Device): ShaderFactory {
18
+ const moduleData = device.getModuleData<CoreModuleState>('@luma.gl/core');
19
+ moduleData.defaultShaderFactory ||= new ShaderFactory(device);
20
+ return moduleData.defaultShaderFactory;
21
+ }
22
+
23
+ public readonly device: Device;
24
+
25
+ private readonly _cache: Record<string, CacheItem> = {};
26
+
27
+ get [Symbol.toStringTag](): string {
28
+ return 'ShaderFactory';
29
+ }
30
+
31
+ toString(): string {
32
+ return `${this[Symbol.toStringTag]}(${this.device.id})`;
33
+ }
34
+
35
+ /** @internal */
36
+ constructor(device: Device) {
37
+ this.device = device;
38
+ }
39
+
40
+ /** Requests a {@link Shader} from the cache, creating a new Shader only if necessary. */
41
+ createShader(props: ShaderProps): Shader {
42
+ if (!this.device.props._cacheShaders) {
43
+ return this.device.createShader(props);
44
+ }
45
+
46
+ const key = this._hashShader(props);
47
+
48
+ let cacheEntry = this._cache[key];
49
+ if (!cacheEntry) {
50
+ const resource = this.device.createShader({
51
+ ...props,
52
+ id: props.id ? `${props.id}-cached` : undefined
53
+ });
54
+ this._cache[key] = cacheEntry = {resource, useCount: 1};
55
+ if (this.device.props.debugFactories) {
56
+ log.log(3, `${this}: Created new shader ${resource.id}`)();
57
+ }
58
+ } else {
59
+ cacheEntry.useCount++;
60
+ if (this.device.props.debugFactories) {
61
+ log.log(
62
+ 3,
63
+ `${this}: Reusing shader ${cacheEntry.resource.id} count=${cacheEntry.useCount}`
64
+ )();
65
+ }
66
+ }
67
+
68
+ return cacheEntry.resource;
69
+ }
70
+
71
+ /** Releases a previously-requested {@link Shader}, destroying it if no users remain. */
72
+ release(shader: Shader): void {
73
+ if (!this.device.props._cacheShaders) {
74
+ shader.destroy();
75
+ return;
76
+ }
77
+
78
+ const key = this._hashShader(shader);
79
+ const cacheEntry = this._cache[key];
80
+ if (cacheEntry) {
81
+ cacheEntry.useCount--;
82
+ if (cacheEntry.useCount === 0) {
83
+ if (this.device.props._destroyShaders) {
84
+ delete this._cache[key];
85
+ cacheEntry.resource.destroy();
86
+ if (this.device.props.debugFactories) {
87
+ log.log(3, `${this}: Releasing shader ${shader.id}, destroyed`)();
88
+ }
89
+ }
90
+ } else if (cacheEntry.useCount < 0) {
91
+ throw new Error(`ShaderFactory: Shader ${shader.id} released too many times`);
92
+ } else if (this.device.props.debugFactories) {
93
+ log.log(3, `${this}: Releasing shader ${shader.id} count=${cacheEntry.useCount}`)();
94
+ }
95
+ }
96
+ }
97
+
98
+ // PRIVATE
99
+
100
+ protected _hashShader(value: Shader | ShaderProps): string {
101
+ return `${value.stage}:${value.source}`;
102
+ }
103
+ }