@luma.gl/engine 9.0.0-alpha.47 → 9.0.0-alpha.48

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.
@@ -1,178 +0,0 @@
1
- // luma.gl, MIT license
2
- import {NumberArray, isNumberArray} from '@luma.gl/core';
3
- import {Device, Buffer, UniformBufferLayout, ShaderUniformType} from '@luma.gl/core';
4
- import {ShaderModule} from '@luma.gl/shadertools';
5
-
6
- /** Duplicates definition in core module to avoid cross dependencies */
7
- export type UniformValue = number | boolean | Readonly<NumberArray>; // Float32Array> | Readonly<Int32Array> | Readonly<Uint32Array> | Readonly<number[]>;
8
-
9
- /** A uniform store holds a number of uniform values and does some book keeping on what has changes */
10
- export class ShaderModuleUniforms<TUniforms extends Record<string, UniformValue>> {
11
- device: Device;
12
- shaderModule: ShaderModule<TUniforms>;
13
-
14
- useUniformBuffers: boolean;
15
- uniformBufferLayout: UniformBufferLayout;
16
- uniformBuffer: Buffer | null = null;
17
-
18
- uniforms: Record<string, UniformValue> = {};
19
- modifiedUniforms: Record<string, boolean> = {};
20
- modified: boolean = true;
21
-
22
- needsRedraw: string | false = 'initialized';
23
-
24
- constructor(props?: {
25
- device: Device;
26
- shaderModule: ShaderModule<TUniforms>
27
- }) {
28
- this.device = props.device;
29
- this.shaderModule = props.shaderModule;
30
- this.useUniformBuffers = this.device.info.type !== 'webgl';
31
-
32
- // extract uniform formats and create a uniform buffer layout
33
- const uniformTypes: Record<string, ShaderUniformType> = {};
34
- for (const [name, {format}] of Object.entries(props.shaderModule.uniformPropTypes || {})) {
35
- uniformTypes[name] = format;
36
- }
37
- this.uniformBufferLayout = new UniformBufferLayout(uniformTypes);
38
-
39
- // create a uniform buffer
40
- if (this.useUniformBuffers) {
41
- this.uniformBuffer = this.device.createBuffer({
42
- usage: Buffer.UNIFORM,
43
- byteLength: this.uniformBufferLayout.byteLength
44
- });
45
- }
46
- }
47
-
48
- /** Set a map of uniforms */
49
- setUniforms(uniforms: TUniforms): void {
50
- for (const [key, value] of Object.entries(uniforms)) {
51
- this._setUniform(key, value);
52
- this.setNeedsRedraw(key);
53
- }
54
- }
55
-
56
- setNeedsRedraw(reason: string): void {
57
- this.needsRedraw = this.needsRedraw || reason;
58
- }
59
-
60
- /**
61
- * Updates the uniform buffer if needed.
62
- * Clears the dirty flag.
63
- */
64
- getUniformBuffer(): Buffer {
65
- if (this.needsRedraw) {
66
- const modifiedUniforms = this.getModifiedUniforms();
67
- const data = this.uniformBufferLayout.getData(modifiedUniforms);
68
- this.uniformBuffer.write(data);
69
- }
70
- return this.uniformBuffer;
71
- }
72
-
73
- /** Returns all uniforms */
74
- getUniforms(groupName: string): Record<string, UniformValue> {
75
- this.modifiedUniforms = {};
76
- this.needsRedraw = false;
77
- return (this.uniforms[groupName] || {}) as Record<string, UniformValue>;
78
- }
79
-
80
- /** Returns modified uniforms */
81
- getModifiedUniforms() {
82
- const modifiedUniforms = this.modifiedUniforms;
83
- this.modifiedUniforms = {};
84
- this.needsRedraw = false;
85
- return modifiedUniforms;
86
- }
87
-
88
- /** Set a single uniform */
89
- private _setUniform(key: string, value: UniformValue) {
90
- // if (this.layout[key] !== undefined) {
91
- // this.uniforms[key] = value;
92
- // this.modifiedUniforms[key] = true;
93
- // this.modified = true;
94
- // } else {
95
- // log.warn(`Unknown uniform ${key}`)
96
- // }
97
- if (arrayEqual(this.uniforms[key], value)) {
98
- return;
99
- }
100
- this.uniforms[key] = value;
101
- this.modifiedUniforms[key] = true;
102
- this.modified = true;
103
- }
104
- }
105
-
106
- function arrayEqual(a: unknown, b: unknown, limit: number = 16) {
107
- if (a !== b) {
108
- return false;
109
- }
110
- const arrayA = isNumberArray(a);
111
- if (!arrayA) {
112
- return false;
113
- }
114
- const arrayB = isNumberArray(b);
115
- if (arrayB && arrayA.length === arrayB.length) {
116
- for (let i = 0; i < arrayA.length; ++i) {
117
- if (arrayB[i] !== arrayA[i]) {
118
- return false;
119
- }
120
- }
121
- }
122
- return true;
123
- }
124
-
125
-
126
- // export function makeUniformStore<I extends Record<string, Record<string, unknown>>>(name: string, uniforms: I): UniformStore<{ [P in keyof I]: I[P] }> {
127
- // return new UniformStore<{ [P in keyof I]: I[P] }>({name, uniforms});
128
- // }
129
-
130
- // type ShaderModule<Uniforms extends Record<string, unknown> = {}> = {
131
- // uniformTypes: Record<keyof Uniforms, string>;
132
- // };
133
-
134
- // type Module1Uniforms = {
135
- // uniform1?: number;
136
- // uniform2?: [number, number];
137
- // }
138
-
139
- // type Module2Uniforms = {
140
- // uniform3: number;
141
- // uniform4: [number, number];
142
- // }
143
-
144
- // const shaderModule: ShaderModule<Module1Uniforms> = {
145
- // uniformTypes: {
146
- // uniform1: 'aaa',
147
- // uniform2: 'aaa'
148
- // }
149
- // }
150
-
151
-
152
- // const module1Uniforms = new ShaderModuleUniforms<Module1Uniforms>();
153
-
154
- // module1Uniforms.setUniforms({
155
- // uniform1: 1,
156
- // uniform2: [1, 1]
157
- // });
158
-
159
-
160
-
161
- // setUniformStore(pipeline, blockIndex, block);
162
-
163
- // GLSL utilities
164
-
165
-
166
- // TYPE TESTS
167
-
168
- /*
169
- new UniformStore<ModuleUniforms>().setUniforms({
170
- uniform1: 1,
171
- uniform2: 1,
172
- });
173
-
174
- new UniformStore<ModuleUniforms>().setUniforms({
175
- three: 1,
176
- uniform1: 1,
177
- });
178
- */