@luma.gl/engine 9.0.0-alpha.9 → 9.0.0-beta.10

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 (187) hide show
  1. package/LICENSE +3 -1
  2. package/dist/animation/key-frames.d.ts +1 -1
  3. package/dist/animation/key-frames.d.ts.map +1 -1
  4. package/dist/animation/key-frames.js +51 -72
  5. package/dist/animation/timeline.d.ts +8 -8
  6. package/dist/animation/timeline.d.ts.map +1 -1
  7. package/dist/animation/timeline.js +95 -131
  8. package/dist/animation-loop/animation-loop-template.d.ts +23 -0
  9. package/dist/animation-loop/animation-loop-template.d.ts.map +1 -0
  10. package/dist/animation-loop/animation-loop-template.js +21 -0
  11. package/dist/{lib → animation-loop}/animation-loop.d.ts +31 -23
  12. package/dist/animation-loop/animation-loop.d.ts.map +1 -0
  13. package/dist/animation-loop/animation-loop.js +442 -0
  14. package/dist/{lib → animation-loop}/animation-props.d.ts +4 -5
  15. package/dist/animation-loop/animation-props.d.ts.map +1 -0
  16. package/dist/animation-loop/animation-props.js +1 -0
  17. package/dist/animation-loop/make-animation-loop.d.ts +6 -0
  18. package/dist/animation-loop/make-animation-loop.d.ts.map +1 -0
  19. package/dist/animation-loop/make-animation-loop.js +32 -0
  20. package/dist/computation.d.ts +95 -0
  21. package/dist/computation.d.ts.map +1 -0
  22. package/dist/computation.js +248 -0
  23. package/dist/debug/copy-texture-to-image.d.ts +26 -0
  24. package/dist/debug/copy-texture-to-image.d.ts.map +1 -0
  25. package/dist/debug/copy-texture-to-image.js +43 -0
  26. package/dist/debug/debug-framebuffer.d.ts +11 -0
  27. package/dist/debug/debug-framebuffer.d.ts.map +1 -0
  28. package/dist/debug/debug-framebuffer.js +46 -0
  29. package/dist/debug/debug-shader-layout.d.ts +9 -0
  30. package/dist/debug/debug-shader-layout.d.ts.map +1 -0
  31. package/dist/debug/debug-shader-layout.js +27 -0
  32. package/dist/debug/pixel-data-utils.d.ts +24 -0
  33. package/dist/debug/pixel-data-utils.d.ts.map +1 -0
  34. package/dist/debug/pixel-data-utils.js +39 -0
  35. package/dist/dist.dev.js +9592 -0
  36. package/dist/dist.min.js +102 -0
  37. package/dist/geometries/cone-geometry.d.ts +2 -2
  38. package/dist/geometries/cone-geometry.d.ts.map +1 -1
  39. package/dist/geometries/cone-geometry.js +13 -18
  40. package/dist/geometries/cube-geometry.d.ts +2 -2
  41. package/dist/geometries/cube-geometry.d.ts.map +1 -1
  42. package/dist/geometries/cube-geometry.js +192 -57
  43. package/dist/geometries/cylinder-geometry.d.ts +2 -2
  44. package/dist/geometries/cylinder-geometry.d.ts.map +1 -1
  45. package/dist/geometries/cylinder-geometry.js +11 -15
  46. package/dist/geometries/ico-sphere-geometry.d.ts +2 -2
  47. package/dist/geometries/ico-sphere-geometry.d.ts.map +1 -1
  48. package/dist/geometries/ico-sphere-geometry.js +143 -171
  49. package/dist/geometries/plane-geometry.d.ts +2 -2
  50. package/dist/geometries/plane-geometry.d.ts.map +1 -1
  51. package/dist/geometries/plane-geometry.js +95 -122
  52. package/dist/geometries/sphere-geometry.d.ts +2 -2
  53. package/dist/geometries/sphere-geometry.d.ts.map +1 -1
  54. package/dist/geometries/sphere-geometry.js +78 -101
  55. package/dist/geometries/truncated-cone-geometry.d.ts +2 -4
  56. package/dist/geometries/truncated-cone-geometry.d.ts.map +1 -1
  57. package/dist/geometries/truncated-cone-geometry.js +100 -134
  58. package/dist/geometry/geometry-table.d.ts +2 -2
  59. package/dist/geometry/geometry-table.d.ts.map +1 -1
  60. package/dist/geometry/geometry-table.js +3 -1
  61. package/dist/geometry/geometry-utils.d.ts.map +1 -1
  62. package/dist/geometry/geometry-utils.js +35 -41
  63. package/dist/geometry/geometry.d.ts +43 -43
  64. package/dist/geometry/geometry.d.ts.map +1 -1
  65. package/dist/geometry/geometry.js +82 -139
  66. package/dist/geometry/gpu-geometry.d.ts +37 -0
  67. package/dist/geometry/gpu-geometry.d.ts.map +1 -0
  68. package/dist/geometry/gpu-geometry.js +90 -0
  69. package/dist/geometry/gpu-table.d.ts +1 -0
  70. package/dist/geometry/gpu-table.d.ts.map +1 -0
  71. package/dist/geometry/gpu-table.js +42 -0
  72. package/dist/index.cjs +3444 -0
  73. package/dist/index.cjs.map +7 -0
  74. package/dist/index.d.ts +43 -24
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +29 -15
  77. package/dist/lib/clip-space.d.ts +8 -0
  78. package/dist/lib/clip-space.d.ts.map +1 -1
  79. package/dist/lib/clip-space.js +43 -2
  80. package/dist/lib/pipeline-factory.d.ts +17 -51
  81. package/dist/lib/pipeline-factory.d.ts.map +1 -1
  82. package/dist/lib/pipeline-factory.js +84 -209
  83. package/dist/lib/shader-factory.d.ts +17 -0
  84. package/dist/lib/shader-factory.d.ts.map +1 -0
  85. package/dist/lib/shader-factory.js +46 -0
  86. package/dist/model/model.d.ts +219 -0
  87. package/dist/model/model.d.ts.map +1 -0
  88. package/dist/model/model.js +659 -0
  89. package/dist/scenegraph/group-node.d.ts +21 -0
  90. package/dist/scenegraph/group-node.d.ts.map +1 -0
  91. package/dist/scenegraph/group-node.js +84 -0
  92. package/dist/scenegraph/model-node.d.ts +18 -0
  93. package/dist/scenegraph/model-node.d.ts.map +1 -0
  94. package/dist/scenegraph/model-node.js +35 -0
  95. package/dist/scenegraph/scenegraph-node.d.ts +56 -0
  96. package/dist/scenegraph/scenegraph-node.d.ts.map +1 -0
  97. package/dist/scenegraph/scenegraph-node.js +153 -0
  98. package/dist/shader-inputs.d.ts +63 -0
  99. package/dist/shader-inputs.d.ts.map +1 -0
  100. package/dist/shader-inputs.js +107 -0
  101. package/dist/transform/buffer-transform.d.ts +35 -0
  102. package/dist/transform/buffer-transform.d.ts.map +1 -0
  103. package/dist/transform/buffer-transform.js +70 -0
  104. package/dist/transform/texture-transform.d.ts +57 -0
  105. package/dist/transform/texture-transform.d.ts.map +1 -0
  106. package/dist/transform/texture-transform.js +117 -0
  107. package/dist.min.js +25 -0
  108. package/package.json +24 -14
  109. package/src/animation/timeline.ts +35 -34
  110. package/src/animation-loop/animation-loop-template.ts +25 -0
  111. package/src/{lib → animation-loop}/animation-loop.ts +114 -93
  112. package/src/{lib → animation-loop}/animation-props.ts +2 -2
  113. package/src/animation-loop/make-animation-loop.ts +53 -0
  114. package/src/computation.ts +346 -0
  115. package/src/debug/copy-texture-to-image.ts +70 -0
  116. package/src/debug/debug-framebuffer.ts +70 -0
  117. package/src/debug/debug-shader-layout.ts +38 -0
  118. package/src/debug/pixel-data-utils.ts +54 -0
  119. package/src/geometries/cone-geometry.ts +1 -1
  120. package/src/geometries/cube-geometry.ts +62 -56
  121. package/src/geometries/cylinder-geometry.ts +2 -2
  122. package/src/geometries/ico-sphere-geometry.ts +6 -5
  123. package/src/geometries/plane-geometry.ts +5 -4
  124. package/src/geometries/sphere-geometry.ts +4 -3
  125. package/src/geometries/truncated-cone-geometry.ts +6 -14
  126. package/src/geometry/geometry-table.ts +10 -7
  127. package/src/geometry/geometry-utils.ts +19 -3
  128. package/src/geometry/geometry.ts +68 -110
  129. package/src/geometry/gpu-geometry.ts +132 -0
  130. package/src/geometry/gpu-table.ts +41 -0
  131. package/src/index.ts +37 -10
  132. package/src/lib/clip-space.ts +32 -34
  133. package/src/lib/pipeline-factory.ts +83 -193
  134. package/src/lib/shader-factory.ts +57 -0
  135. package/src/model/model.ts +835 -0
  136. package/src/scenegraph/group-node.ts +107 -0
  137. package/src/scenegraph/model-node.ts +50 -0
  138. package/src/scenegraph/scenegraph-node.ts +204 -0
  139. package/src/shader-inputs.ts +157 -0
  140. package/src/transform/buffer-transform.ts +102 -0
  141. package/src/transform/texture-transform.ts +168 -0
  142. package/dist/animation/key-frames.js.map +0 -1
  143. package/dist/animation/timeline.js.map +0 -1
  144. package/dist/bundle.d.ts +0 -2
  145. package/dist/bundle.d.ts.map +0 -1
  146. package/dist/bundle.js +0 -5
  147. package/dist/bundle.js.map +0 -1
  148. package/dist/geometries/cone-geometry.js.map +0 -1
  149. package/dist/geometries/cube-geometry.js.map +0 -1
  150. package/dist/geometries/cylinder-geometry.js.map +0 -1
  151. package/dist/geometries/ico-sphere-geometry.js.map +0 -1
  152. package/dist/geometries/plane-geometry.js.map +0 -1
  153. package/dist/geometries/sphere-geometry.js.map +0 -1
  154. package/dist/geometries/truncated-cone-geometry.js.map +0 -1
  155. package/dist/geometry/geometry-table.js.map +0 -1
  156. package/dist/geometry/geometry-utils.js.map +0 -1
  157. package/dist/geometry/geometry.js.map +0 -1
  158. package/dist/geometry/primitive-utils.d.ts +0 -1
  159. package/dist/geometry/primitive-utils.d.ts.map +0 -1
  160. package/dist/geometry/primitive-utils.js +0 -2
  161. package/dist/geometry/primitive-utils.js.map +0 -1
  162. package/dist/index.js.map +0 -1
  163. package/dist/lib/animation-loop.d.ts.map +0 -1
  164. package/dist/lib/animation-loop.js +0 -480
  165. package/dist/lib/animation-loop.js.map +0 -1
  166. package/dist/lib/animation-props.d.ts.map +0 -1
  167. package/dist/lib/animation-props.js +0 -2
  168. package/dist/lib/animation-props.js.map +0 -1
  169. package/dist/lib/clip-space.js.map +0 -1
  170. package/dist/lib/model-utils.d.ts +0 -5
  171. package/dist/lib/model-utils.d.ts.map +0 -1
  172. package/dist/lib/model-utils.js +0 -45
  173. package/dist/lib/model-utils.js.map +0 -1
  174. package/dist/lib/model.d.ts +0 -41
  175. package/dist/lib/model.d.ts.map +0 -1
  176. package/dist/lib/model.js +0 -182
  177. package/dist/lib/model.js.map +0 -1
  178. package/dist/lib/pipeline-factory.js.map +0 -1
  179. package/dist/lib/render-loop.d.ts +0 -14
  180. package/dist/lib/render-loop.d.ts.map +0 -1
  181. package/dist/lib/render-loop.js +0 -49
  182. package/dist/lib/render-loop.js.map +0 -1
  183. package/src/bundle.ts +0 -4
  184. package/src/geometry/primitive-utils.ts +0 -30
  185. package/src/lib/model-utils.ts +0 -124
  186. package/src/lib/model.ts +0 -183
  187. package/src/lib/render-loop.ts +0 -58
@@ -0,0 +1,107 @@
1
+ import {Matrix4, Vector3} from '@math.gl/core';
2
+ import {log} from '@luma.gl/core';
3
+ import {ScenegraphNode, ScenegraphNodeProps} from './scenegraph-node';
4
+
5
+ export type GroupNodeProps = ScenegraphNodeProps & {
6
+ children?: ScenegraphNode[];
7
+ };
8
+
9
+ export class GroupNode extends ScenegraphNode {
10
+ children: ScenegraphNode[];
11
+
12
+ constructor(children: ScenegraphNode[]);
13
+ constructor(props?: GroupNodeProps);
14
+
15
+ constructor(props: ScenegraphNode[] | GroupNodeProps = {}) {
16
+ props = Array.isArray(props) ? {children: props} : props;
17
+ const {children = []} = props;
18
+ log.assert(
19
+ children.every(child => child instanceof ScenegraphNode),
20
+ 'every child must an instance of ScenegraphNode'
21
+ );
22
+ super(props);
23
+ this.children = children;
24
+ }
25
+
26
+ override getBounds(): [number[], number[]] | null {
27
+ const result: [number[], number[]] = [
28
+ [Infinity, Infinity, Infinity],
29
+ [-Infinity, -Infinity, -Infinity]
30
+ ];
31
+
32
+ this.traverse((node, {worldMatrix}) => {
33
+ const bounds = node.getBounds();
34
+ if (!bounds) {
35
+ return;
36
+ }
37
+ const [min, max] = bounds;
38
+ const center = new Vector3(min).add(max).divide([2, 2, 2]);
39
+ worldMatrix.transformAsPoint(center, center);
40
+ const halfSize = new Vector3(max).subtract(min).divide([2, 2, 2]);
41
+ worldMatrix.transformAsVector(halfSize, halfSize);
42
+
43
+ for (let v = 0; v < 8; v++) {
44
+ // Test all 8 corners of the box
45
+ const position = new Vector3(v & 0b001 ? -1 : 1, v & 0b010 ? -1 : 1, v & 0b100 ? -1 : 1)
46
+ .multiply(halfSize)
47
+ .add(center);
48
+
49
+ for (let i = 0; i < 3; i++) {
50
+ result[0][i] = Math.min(result[0][i], position[i]);
51
+ result[1][i] = Math.max(result[1][i], position[i]);
52
+ }
53
+ }
54
+ });
55
+ if (!Number.isFinite(result[0][0])) {
56
+ return null;
57
+ }
58
+ return result;
59
+ }
60
+
61
+ override destroy(): void {
62
+ this.children.forEach(child => child.destroy());
63
+ this.removeAll();
64
+ super.destroy();
65
+ }
66
+
67
+ // Unpacks arrays and nested arrays of children
68
+ add(...children: (ScenegraphNode | ScenegraphNode[])[]): this {
69
+ for (const child of children) {
70
+ if (Array.isArray(child)) {
71
+ this.add(...child);
72
+ } else {
73
+ this.children.push(child);
74
+ }
75
+ }
76
+ return this;
77
+ }
78
+
79
+ remove(child: ScenegraphNode): this {
80
+ const children = this.children;
81
+ const indexOf = children.indexOf(child);
82
+ if (indexOf > -1) {
83
+ children.splice(indexOf, 1);
84
+ }
85
+ return this;
86
+ }
87
+
88
+ removeAll(): this {
89
+ this.children = [];
90
+ return this;
91
+ }
92
+
93
+ traverse(
94
+ visitor: (node: ScenegraphNode, context: {worldMatrix: Matrix4}) => void,
95
+ {worldMatrix = new Matrix4()} = {}
96
+ ) {
97
+ const modelMatrix = new Matrix4(worldMatrix).multiplyRight(this.matrix);
98
+
99
+ for (const child of this.children) {
100
+ if (child instanceof GroupNode) {
101
+ child.traverse(visitor, {worldMatrix: modelMatrix});
102
+ } else {
103
+ visitor(child, {worldMatrix: modelMatrix});
104
+ }
105
+ }
106
+ }
107
+ }
@@ -0,0 +1,50 @@
1
+ import {RenderPass} from '@luma.gl/core';
2
+ import {ScenegraphNode, ScenegraphNodeProps} from './scenegraph-node';
3
+ import {Model} from '../model/model';
4
+
5
+ export type ModelNodeProps = ScenegraphNodeProps & {
6
+ model: Model;
7
+ managedResources?: any[];
8
+ bounds?: [number[], number[]];
9
+ };
10
+
11
+ export class ModelNode extends ScenegraphNode {
12
+ readonly model: Model;
13
+ bounds: [number[], number[]] | null = null;
14
+ managedResources: any[];
15
+
16
+ // TODO - is this used? override callbacks to make sure we call them with this
17
+ // onBeforeRender = null;
18
+ // onAfterRender = null;
19
+ // AfterRender = null;
20
+
21
+ constructor(props: ModelNodeProps) {
22
+ super(props);
23
+
24
+ // Create new Model or used supplied Model
25
+ this.model = props.model;
26
+ this.managedResources = props.managedResources || [];
27
+ this.bounds = props.bounds || null;
28
+ this.setProps(props);
29
+ }
30
+
31
+ override getBounds(): [number[], number[]] | null {
32
+ return this.bounds;
33
+ }
34
+
35
+ override destroy(): void {
36
+ if (this.model) {
37
+ this.model.destroy();
38
+ // @ts-expect-error
39
+ this.model = null;
40
+ }
41
+ this.managedResources.forEach(resource => resource.destroy());
42
+ this.managedResources = [];
43
+ }
44
+
45
+ // Expose model methods
46
+ draw(renderPass?: RenderPass) {
47
+ // Return value indicates if something was actually drawn
48
+ return this.model.draw(renderPass);
49
+ }
50
+ }
@@ -0,0 +1,204 @@
1
+ import {assert, uid, NumberArray} from '@luma.gl/core';
2
+ import {Vector3, Matrix4} from '@math.gl/core';
3
+
4
+ /** Properties for creating a new Scenegraph */
5
+ export type ScenegraphNodeProps = {
6
+ id?: string;
7
+ /** whether to display the object at all */
8
+ display?: boolean;
9
+ matrix?: NumberArray;
10
+ position?: NumberArray;
11
+ rotation?: NumberArray;
12
+ scale?: NumberArray;
13
+ update?: boolean;
14
+ };
15
+
16
+ export class ScenegraphNode {
17
+ readonly id: string;
18
+ matrix: Matrix4 = new Matrix4();
19
+
20
+ display = true;
21
+ position = new Vector3();
22
+ rotation = new Vector3();
23
+ scale = new Vector3(1, 1, 1);
24
+ userData: Record<string, unknown> = {};
25
+
26
+ props: ScenegraphNodeProps = {};
27
+
28
+ constructor(props: ScenegraphNodeProps = {}) {
29
+ const {id} = props;
30
+
31
+ this.id = id || uid(this.constructor.name);
32
+
33
+ this._setScenegraphNodeProps(props);
34
+ }
35
+
36
+ getBounds(): [number[], number[]] | null {
37
+ return null;
38
+ }
39
+
40
+ destroy(): void {}
41
+
42
+ /** @deprecated use .destroy() */
43
+ delete(): void {
44
+ this.destroy();
45
+ }
46
+ setProps(props: ScenegraphNodeProps): this {
47
+ this._setScenegraphNodeProps(props);
48
+ return this;
49
+ }
50
+
51
+ toString(): string {
52
+ return `{type: ScenegraphNode, id: ${this.id})}`;
53
+ }
54
+
55
+ setPosition(position: any): this {
56
+ assert(position.length === 3, 'setPosition requires vector argument');
57
+ this.position = position;
58
+ return this;
59
+ }
60
+
61
+ setRotation(rotation: any): this {
62
+ assert(rotation.length === 3, 'setRotation requires vector argument');
63
+ this.rotation = rotation;
64
+ return this;
65
+ }
66
+
67
+ setScale(scale: any): this {
68
+ assert(scale.length === 3, 'setScale requires vector argument');
69
+ this.scale = scale;
70
+ return this;
71
+ }
72
+
73
+ setMatrix(matrix: any, copyMatrix: boolean = true): void {
74
+ if (copyMatrix) {
75
+ this.matrix.copy(matrix);
76
+ } else {
77
+ this.matrix = matrix;
78
+ }
79
+ }
80
+
81
+ setMatrixComponents(components: {
82
+ position?: any;
83
+ rotation?: any;
84
+ scale?: any;
85
+ update?: boolean;
86
+ }): this {
87
+ const {position, rotation, scale, update = true} = components;
88
+ if (position) {
89
+ this.setPosition(position);
90
+ }
91
+ if (rotation) {
92
+ this.setRotation(rotation);
93
+ }
94
+ if (scale) {
95
+ this.setScale(scale);
96
+ }
97
+ if (update) {
98
+ this.updateMatrix();
99
+ }
100
+ return this;
101
+ }
102
+
103
+ updateMatrix(): this {
104
+ const pos = this.position;
105
+ const rot = this.rotation;
106
+ const scale = this.scale;
107
+
108
+ this.matrix.identity();
109
+ this.matrix.translate(pos);
110
+ this.matrix.rotateXYZ(rot);
111
+ this.matrix.scale(scale);
112
+ return this;
113
+ }
114
+
115
+ update(options: {position?: any; rotation?: any; scale?: any} = {}): this {
116
+ const {position, rotation, scale} = options;
117
+ if (position) {
118
+ this.setPosition(position);
119
+ }
120
+ if (rotation) {
121
+ this.setRotation(rotation);
122
+ }
123
+ if (scale) {
124
+ this.setScale(scale);
125
+ }
126
+ this.updateMatrix();
127
+ return this;
128
+ }
129
+
130
+ getCoordinateUniforms(
131
+ viewMatrix: any,
132
+ modelMatrix?: any
133
+ ): {
134
+ viewMatrix: any;
135
+ modelMatrix: any;
136
+ objectMatrix: any;
137
+ worldMatrix: any;
138
+ worldInverseMatrix: any;
139
+ worldInverseTransposeMatrix: any;
140
+ } {
141
+ // TODO - solve multiple class problem
142
+ // assert(viewMatrix instanceof Matrix4);
143
+ assert(viewMatrix);
144
+ modelMatrix = modelMatrix || this.matrix;
145
+ const worldMatrix = new Matrix4(viewMatrix).multiplyRight(modelMatrix);
146
+ const worldInverse = worldMatrix.invert();
147
+ const worldInverseTranspose = worldInverse.transpose();
148
+
149
+ return {
150
+ viewMatrix,
151
+ modelMatrix,
152
+ objectMatrix: modelMatrix,
153
+ worldMatrix,
154
+ worldInverseMatrix: worldInverse,
155
+ worldInverseTransposeMatrix: worldInverseTranspose
156
+ };
157
+ }
158
+
159
+ // TODO - copied code, not yet vetted
160
+ /*
161
+ transform() {
162
+ if (!this.parent) {
163
+ this.endPosition.set(this.position);
164
+ this.endRotation.set(this.rotation);
165
+ this.endScale.set(this.scale);
166
+ } else {
167
+ const parent = this.parent;
168
+ this.endPosition.set(this.position.add(parent.endPosition));
169
+ this.endRotation.set(this.rotation.add(parent.endRotation));
170
+ this.endScale.set(this.scale.add(parent.endScale));
171
+ }
172
+
173
+ const ch = this.children;
174
+ for (let i = 0; i < ch.length; ++i) {
175
+ ch[i].transform();
176
+ }
177
+
178
+ return this;
179
+ }
180
+ */
181
+
182
+ _setScenegraphNodeProps(props: ScenegraphNodeProps): void {
183
+ if ('display' in props) {
184
+ this.display = props.display;
185
+ }
186
+
187
+ if ('position' in props) {
188
+ this.setPosition(props.position);
189
+ }
190
+ if ('rotation' in props) {
191
+ this.setRotation(props.rotation);
192
+ }
193
+ if ('scale' in props) {
194
+ this.setScale(props.scale);
195
+ }
196
+
197
+ // Matrix overwrites other props
198
+ if ('matrix' in props) {
199
+ this.setMatrix(props.matrix);
200
+ }
201
+
202
+ Object.assign(this.props, props);
203
+ }
204
+ }
@@ -0,0 +1,157 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {UniformValue, Texture, Sampler} from '@luma.gl/core';
6
+ import {log} from '@luma.gl/core';
7
+ // import type {ShaderUniformType, UniformValue, UniformFormat, UniformInfoDevice, Texture, Sampler} from '@luma.gl/core';
8
+ import {_resolveModules, ShaderModuleInstance} from '@luma.gl/shadertools';
9
+
10
+ /** Minimal ShaderModule subset, we don't need shader code etc */
11
+ export type ShaderModuleInputs<
12
+ PropsT extends Record<string, unknown> = Record<string, unknown>,
13
+ UniformsT extends Record<string, UniformValue> = Record<string, UniformValue>,
14
+ BindingsT extends Record<string, Texture | Sampler> = Record<string, Texture | Sampler>
15
+ > = {
16
+ defaultUniforms?: UniformsT;
17
+ getUniforms?: (settings: Partial<PropsT>, prevUniforms?: UniformsT) => UniformsT;
18
+
19
+ /** Not used. Used to access props type */
20
+ props?: PropsT;
21
+
22
+ bindings?: Record<
23
+ keyof BindingsT,
24
+ {
25
+ location: number;
26
+ type: 'texture' | 'sampler' | 'uniforms';
27
+ }
28
+ >;
29
+
30
+ uniformTypes?: any;
31
+ };
32
+
33
+ /**
34
+ * ShaderInputs holds uniform and binding values for one or more shader modules,
35
+ * - It can generate binary data for any uniform buffer
36
+ * - It can manage a uniform buffer for each block
37
+ * - It can update managed uniform buffers with a single call
38
+ * - It performs some book keeping on what has changed to minimize unnecessary writes to uniform buffers.
39
+ */
40
+ export class ShaderInputs<
41
+ ShaderPropsT extends Partial<Record<string, Record<string, unknown>>> = Partial<
42
+ Record<string, Record<string, unknown>>
43
+ >
44
+ > {
45
+ /**
46
+ * The map of modules
47
+ * @todo should should this include the resolved dependencies?
48
+ */
49
+ modules: Readonly<{[P in keyof ShaderPropsT]: ShaderModuleInputs<ShaderPropsT[P]>}>;
50
+
51
+ /** Stores the uniform values for each module */
52
+ moduleUniforms: Record<keyof ShaderPropsT, Record<string, UniformValue>>;
53
+ /** Stores the uniform bindings for each module */
54
+ moduleBindings: Record<keyof ShaderPropsT, Record<string, Texture | Sampler>>;
55
+ /** Tracks if uniforms have changed */
56
+ moduleUniformsChanged: Record<keyof ShaderPropsT, false | string>;
57
+
58
+ /**
59
+ * Create a new UniformStore instance
60
+ * @param modules
61
+ */
62
+ constructor(modules: {[P in keyof ShaderPropsT]: ShaderModuleInputs<ShaderPropsT[P]>}) {
63
+ // TODO - get all dependencies from modules
64
+ const allModules = _resolveModules(Object.values(modules));
65
+ log.log(
66
+ 1,
67
+ 'Creating ShaderInputs with modules',
68
+ allModules.map(m => m.name)
69
+ )();
70
+
71
+ // Store the module definitions and create storage for uniform values and binding values, per module
72
+ this.modules = modules;
73
+ this.moduleUniforms = {} as Record<keyof ShaderPropsT, Record<string, UniformValue>>;
74
+ this.moduleBindings = {} as Record<keyof ShaderPropsT, Record<string, Texture | Sampler>>;
75
+
76
+ // Initialize the modules
77
+ for (const [name, module] of Object.entries(modules)) {
78
+ const moduleName = name as keyof ShaderPropsT;
79
+
80
+ // Get default uniforms from module
81
+ this.moduleUniforms[moduleName] = module.defaultUniforms || {};
82
+ this.moduleBindings[moduleName] = {};
83
+ }
84
+ }
85
+
86
+ /** Destroy */
87
+ destroy(): void {}
88
+
89
+ /**
90
+ * Set module props
91
+ */
92
+ setProps(props: Partial<{[P in keyof ShaderPropsT]?: Partial<ShaderPropsT[P]>}>): void {
93
+ for (const name of Object.keys(props)) {
94
+ const moduleName = name as keyof ShaderPropsT;
95
+ const moduleProps = props[moduleName];
96
+ const module = this.modules[moduleName];
97
+ if (!module) {
98
+ // Ignore props for unregistered modules
99
+ log.warn(`Module ${name} not found`)();
100
+ continue; // eslint-disable-line no-continue
101
+ }
102
+
103
+ const oldUniforms = this.moduleUniforms[moduleName];
104
+ const uniforms =
105
+ module.getUniforms?.(moduleProps, this.moduleUniforms[moduleName]) || (moduleProps as any);
106
+ // console.error(uniforms)
107
+ this.moduleUniforms[moduleName] = {...oldUniforms, ...uniforms};
108
+ // this.moduleUniformsChanged ||= moduleName;
109
+
110
+ // console.log(`setProps(${String(moduleName)}`, moduleName, this.moduleUniforms[moduleName])
111
+
112
+ // TODO - Get Module bindings
113
+ // const bindings = module.getBindings?.(moduleProps);
114
+ // this.moduleUniforms[moduleName] = bindings;
115
+ }
116
+ }
117
+
118
+ /** Merges all bindings for the shader (from the various modules) */
119
+ // getUniformBlocks(): Record<string, Texture | Sampler> {
120
+ // return this.moduleUniforms;
121
+ // }
122
+
123
+ /**
124
+ * Return the map of modules
125
+ * @todo should should this include the resolved dependencies?
126
+ */
127
+ getModules(): ShaderModuleInstance[] {
128
+ return Object.values(this.modules);
129
+ }
130
+
131
+ /** Get all uniform values for all modules */
132
+ getUniformValues(): Record<keyof ShaderPropsT, Record<string, UniformValue>> {
133
+ return this.moduleUniforms;
134
+ }
135
+
136
+ /** Merges all bindings for the shader (from the various modules) */
137
+ getBindings(): Record<string, Texture | Sampler> {
138
+ const bindings = {} as Record<string, Texture | Sampler>;
139
+ for (const moduleBindings of Object.values(this.moduleBindings)) {
140
+ Object.assign(bindings, moduleBindings);
141
+ }
142
+ return bindings;
143
+ }
144
+
145
+ getDebugTable(): Record<string, Record<string, unknown>> {
146
+ const table: Record<string, Record<string, unknown>> = {};
147
+ for (const [moduleName, module] of Object.entries(this.moduleUniforms)) {
148
+ for (const [key, value] of Object.entries(module)) {
149
+ table[`${moduleName}.${key}`] = {
150
+ type: this.modules[moduleName].uniformTypes?.[key],
151
+ value: String(value)
152
+ };
153
+ }
154
+ }
155
+ return table;
156
+ }
157
+ }
@@ -0,0 +1,102 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ Device,
7
+ Buffer,
8
+ BufferRange,
9
+ TransformFeedback,
10
+ assert,
11
+ RenderPassProps
12
+ } from '@luma.gl/core';
13
+ import {getPassthroughFS} from '@luma.gl/shadertools';
14
+ import {Model} from '../model/model';
15
+ import type {ModelProps} from '..';
16
+
17
+ /**
18
+ * Properties for creating a {@link BufferTransform}
19
+ * @deprecated
20
+ */
21
+ export type BufferTransformProps = Omit<ModelProps, 'fs'> & {
22
+ fs?: ModelProps['fs']; // override as optional
23
+ feedbackBuffers?: Record<string, Buffer | BufferRange>;
24
+ };
25
+
26
+ /**
27
+ * Creates a pipeline for buffer→buffer transforms.
28
+ * @deprecated
29
+ */
30
+ export class BufferTransform {
31
+ readonly device: Device;
32
+ readonly model: Model;
33
+ readonly transformFeedback: TransformFeedback;
34
+
35
+ /** @deprecated Use device feature test. */
36
+ static isSupported(device: Device): boolean {
37
+ return device?.info?.type === 'webgl';
38
+ }
39
+
40
+ constructor(device: Device, props: BufferTransformProps = Model.defaultProps) {
41
+ assert(BufferTransform.isSupported(device), 'BufferTransform not yet implemented on WebGPU');
42
+
43
+ this.device = device;
44
+
45
+ this.model = new Model(this.device, {
46
+ id: props.id || 'buffer-transform-model',
47
+ fs: props.fs || getPassthroughFS(),
48
+ topology: props.topology || 'point-list',
49
+ ...props
50
+ });
51
+
52
+ this.transformFeedback = this.device.createTransformFeedback({
53
+ layout: this.model.pipeline.shaderLayout,
54
+ buffers: props.feedbackBuffers
55
+ });
56
+
57
+ this.model.setTransformFeedback(this.transformFeedback);
58
+
59
+ Object.seal(this);
60
+ }
61
+
62
+ /** Destroy owned resources. */
63
+ destroy(): void {
64
+ if (this.model) {
65
+ this.model.destroy();
66
+ }
67
+ }
68
+
69
+ /** @deprecated Use {@link destroy}. */
70
+ delete(): void {
71
+ this.destroy();
72
+ }
73
+
74
+ /** Run one transform loop. */
75
+ run(options?: RenderPassProps): void {
76
+ const renderPass = this.device.beginRenderPass(options);
77
+ this.model.draw(renderPass);
78
+ renderPass.end();
79
+ }
80
+
81
+ /** @deprecated */
82
+ update(...args: any[]): void {
83
+ // TODO(v9): Method should likely be removed for v9. Keeping a method stub
84
+ // to assist with migrating DeckGL usage.
85
+ // eslint-disable-next-line no-console
86
+ console.warn('TextureTransform#update() not implemented');
87
+ }
88
+
89
+ /** Returns the {@link Buffer} or {@link BufferRange} for given varying name. */
90
+ getBuffer(varyingName: string): Buffer | BufferRange | null {
91
+ return this.transformFeedback.getBuffer(varyingName);
92
+ }
93
+
94
+ readAsync(varyingName: string): Promise<Uint8Array> {
95
+ const result = this.getBuffer(varyingName);
96
+ if (result instanceof Buffer) {
97
+ return result.readAsync();
98
+ }
99
+ const {buffer, byteOffset = 0, byteLength = buffer.byteLength} = result;
100
+ return buffer.readAsync(byteOffset, byteLength);
101
+ }
102
+ }