@zephyr3d/device 0.1.0

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 (40) hide show
  1. package/dist/base_types.js +602 -0
  2. package/dist/base_types.js.map +1 -0
  3. package/dist/builder/ast.js +2135 -0
  4. package/dist/builder/ast.js.map +1 -0
  5. package/dist/builder/base.js +477 -0
  6. package/dist/builder/base.js.map +1 -0
  7. package/dist/builder/builtinfunc.js +4101 -0
  8. package/dist/builder/builtinfunc.js.map +1 -0
  9. package/dist/builder/constructors.js +173 -0
  10. package/dist/builder/constructors.js.map +1 -0
  11. package/dist/builder/errors.js +148 -0
  12. package/dist/builder/errors.js.map +1 -0
  13. package/dist/builder/programbuilder.js +2323 -0
  14. package/dist/builder/programbuilder.js.map +1 -0
  15. package/dist/builder/reflection.js +60 -0
  16. package/dist/builder/reflection.js.map +1 -0
  17. package/dist/builder/types.js +1563 -0
  18. package/dist/builder/types.js.map +1 -0
  19. package/dist/device.js +515 -0
  20. package/dist/device.js.map +1 -0
  21. package/dist/gpuobject.js +2047 -0
  22. package/dist/gpuobject.js.map +1 -0
  23. package/dist/helpers/drawtext.js +187 -0
  24. package/dist/helpers/drawtext.js.map +1 -0
  25. package/dist/helpers/font.js +189 -0
  26. package/dist/helpers/font.js.map +1 -0
  27. package/dist/helpers/glyphmanager.js +121 -0
  28. package/dist/helpers/glyphmanager.js.map +1 -0
  29. package/dist/helpers/textureatlas.js +170 -0
  30. package/dist/helpers/textureatlas.js.map +1 -0
  31. package/dist/index.d.ts +3873 -0
  32. package/dist/index.js +16 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/timer.js +38 -0
  35. package/dist/timer.js.map +1 -0
  36. package/dist/uniformdata.js +147 -0
  37. package/dist/uniformdata.js.map +1 -0
  38. package/dist/vertexdata.js +135 -0
  39. package/dist/vertexdata.js.map +1 -0
  40. package/package.json +69 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ast.js","sources":[],"s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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,477 @@
1
+ import { ShaderPrecisionType, ASTPrimitive, DeclareType, getTextureSampleType, ASTArrayIndex, ASTScalar, ASTCast, ASTAssignment, ASTLValueArray, ASTLValueScalar, ASTHash, ASTLValueHash, ASTShaderExpConstructor } from './ast.js';
2
+ import { PBPointerTypeInfo, PBAddressSpace, PBArrayTypeInfo, PBPrimitiveType, PBPrimitiveTypeInfo, typeI32 } from './types.js';
3
+ import { PBASTError } from './errors.js';
4
+
5
+ let currentProgramBuilder = null;
6
+ const constructorCache = new Map();
7
+ /** @internal */ function setCurrentProgramBuilder(pb) {
8
+ currentProgramBuilder = pb;
9
+ }
10
+ /** @internal */ function getCurrentProgramBuilder() {
11
+ return currentProgramBuilder;
12
+ }
13
+ /** @internal */ function makeConstructor(typeFunc, elementType) {
14
+ const wrappedTypeFunc = new Proxy(typeFunc, {
15
+ get: function(target, prop) {
16
+ if (typeof prop === 'symbol' || prop in target) {
17
+ return target[prop];
18
+ }
19
+ let entries = constructorCache.get(typeFunc);
20
+ if (!entries) {
21
+ entries = {};
22
+ constructorCache.set(typeFunc, entries);
23
+ }
24
+ let ctor = entries[prop];
25
+ if (!ctor) {
26
+ if (elementType.isPrimitiveType() || elementType.isStructType() || elementType.isArrayType()) {
27
+ if (prop === 'ptr') {
28
+ const pointerType = new PBPointerTypeInfo(elementType, PBAddressSpace.FUNCTION);
29
+ ctor = function pointerCtor(...args) {
30
+ if (args.length === 1 && typeof args[0] === 'string') {
31
+ return new PBShaderExp(args[0], pointerType);
32
+ } else {
33
+ throw new Error(`Invalid pointer type constructor`);
34
+ }
35
+ };
36
+ } else {
37
+ const dim = Number(prop);
38
+ if (Number.isInteger(dim) && dim >= 0) {
39
+ const arrayType = new PBArrayTypeInfo(elementType, dim);
40
+ const arrayTypeFunc = function arrayCtor(...args) {
41
+ if (args.length === 1 && typeof args[0] === 'string') {
42
+ return new PBShaderExp(args[0], arrayType);
43
+ } else {
44
+ const exp = new PBShaderExp('', arrayType);
45
+ exp.$ast = new ASTShaderExpConstructor(exp.$typeinfo, args.map((arg)=>arg instanceof PBShaderExp ? arg.$ast : arg));
46
+ return exp;
47
+ }
48
+ };
49
+ ctor = makeConstructor(arrayTypeFunc, arrayType);
50
+ }
51
+ }
52
+ }
53
+ }
54
+ if (ctor) {
55
+ entries[prop] = ctor;
56
+ }
57
+ return ctor;
58
+ }
59
+ });
60
+ return wrappedTypeFunc;
61
+ }
62
+ /**
63
+ * Base class for proxiable object
64
+ * @public
65
+ */ class Proxiable {
66
+ /** @internal */ proxy;
67
+ constructor(){
68
+ this.proxy = new Proxy(this, {
69
+ get: function(target, prop) {
70
+ return typeof prop === 'string' ? target.$get(prop) : undefined;
71
+ },
72
+ set: function(target, prop, value) {
73
+ return typeof prop === 'string' ? target.$set(prop, value) : false;
74
+ }
75
+ });
76
+ return this.proxy;
77
+ }
78
+ get $thisProxy() {
79
+ return this.proxy;
80
+ }
81
+ }
82
+ let uidExp = 0;
83
+ /**
84
+ * Base class for a expression in the shader
85
+ * @public
86
+ */ class PBShaderExp extends Proxiable {
87
+ /** @internal */ $uid;
88
+ /** @internal */ $str;
89
+ /** @internal */ $location;
90
+ /** @internal */ $typeinfo;
91
+ /** @internal */ $global;
92
+ /** @internal */ $sampleType;
93
+ /** @internal */ $precision;
94
+ /** @internal */ $ast;
95
+ /** @internal */ $inout;
96
+ /** @internal */ $memberCache;
97
+ /** @internal */ $attrib;
98
+ /** @internal */ $tags;
99
+ /** @internal */ $_group;
100
+ /** @internal */ $declareType;
101
+ /** @internal */ $isBuffer;
102
+ /** @internal */ constructor(str, typeInfo){
103
+ super();
104
+ if (!str && typeInfo.isPointerType()) {
105
+ throw new Error('no default constructor for pointer type');
106
+ }
107
+ this.$uid = uidExp++;
108
+ this.$str = str || '';
109
+ this.$location = 0;
110
+ this.$global = false;
111
+ this.$typeinfo = typeInfo;
112
+ this.$qualifier = null;
113
+ this.$precision = ShaderPrecisionType.NONE;
114
+ this.$ast = new ASTPrimitive(this);
115
+ this.$inout = null;
116
+ this.$memberCache = {};
117
+ this.$attrib = null;
118
+ this.$tags = [];
119
+ this.$_group = null;
120
+ this.$declareType = DeclareType.DECLARE_TYPE_NONE;
121
+ this.$isBuffer = false;
122
+ if (typeInfo.isTextureType()) {
123
+ if (typeInfo.isDepthTexture()) {
124
+ this.$sampleType = 'depth';
125
+ } else {
126
+ const t = getTextureSampleType(typeInfo);
127
+ if (t.primitiveType === PBPrimitiveType.I32) {
128
+ this.$sampleType = 'sint';
129
+ } else if (t.primitiveType === PBPrimitiveType.U32) {
130
+ this.$sampleType = 'uint';
131
+ } else {
132
+ this.$sampleType = 'float';
133
+ }
134
+ }
135
+ }
136
+ }
137
+ get $group() {
138
+ return this.$_group;
139
+ }
140
+ set $group(val) {
141
+ this.$_group = val;
142
+ if (this.$_group === undefined) {
143
+ debugger;
144
+ }
145
+ }
146
+ /**
147
+ * Point out that the variable should be in uniform address space
148
+ * @param group - The bind group index
149
+ * @returns self
150
+ */ uniform(group) {
151
+ this.$declareType = DeclareType.DECLARE_TYPE_UNIFORM;
152
+ this.$group = group;
153
+ this.$isBuffer = false;
154
+ return this;
155
+ }
156
+ /**
157
+ * Point out that the variable should be an uniform buffer
158
+ * @param group - The bind group index
159
+ * @returns self
160
+ */ uniformBuffer(group) {
161
+ if (!this.$typeinfo.isPrimitiveType() && !this.$typeinfo.isArrayType() && !this.$typeinfo.isStructType()) {
162
+ throw new PBASTError(this.$ast, 'only primitive type, array type or structure type can be set as uniform buffer');
163
+ }
164
+ this.$declareType = DeclareType.DECLARE_TYPE_UNIFORM;
165
+ this.$group = group;
166
+ this.$isBuffer = true;
167
+ return this;
168
+ }
169
+ /**
170
+ * Point out that the variable should be in workgroup address space
171
+ *
172
+ * @remarks
173
+ * WebGPU device only
174
+ *
175
+ * @returns self
176
+ */ workgroup() {
177
+ this.$declareType = DeclareType.DECLARE_TYPE_WORKGROUP;
178
+ return this;
179
+ }
180
+ /**
181
+ * Point out that the variable should be in storage address space
182
+ * @param group - The bind group index
183
+ * @returns self
184
+ */ storage(group) {
185
+ if (!this.$typeinfo.isHostSharable()) {
186
+ throw new PBASTError(this.$ast, 'type cannot be declared in storage address space');
187
+ }
188
+ this.$declareType = DeclareType.DECLARE_TYPE_STORAGE;
189
+ this.$group = group;
190
+ this.$isBuffer = false;
191
+ return this;
192
+ }
193
+ /**
194
+ * Point out that the variable should be a storage buffer
195
+ * @param group - The bind group index
196
+ * @returns self
197
+ */ storageBuffer(group) {
198
+ if (!this.$typeinfo.isPrimitiveType() && !this.$typeinfo.isArrayType() && !this.$typeinfo.isStructType()) {
199
+ throw new PBASTError(this.$ast, 'only primitive type, array type or structure type can be set as storage buffer');
200
+ }
201
+ this.$declareType = DeclareType.DECLARE_TYPE_STORAGE;
202
+ this.$group = group;
203
+ this.$isBuffer = true;
204
+ return this;
205
+ }
206
+ inout() {
207
+ this.$inout = 'inout';
208
+ return this;
209
+ }
210
+ out() {
211
+ this.$inout = 'out';
212
+ return this;
213
+ }
214
+ /**
215
+ * Point out that the variable is a input vertex attribute
216
+ * @param attr - The vertex semantic
217
+ * @returns self
218
+ */ attrib(attr) {
219
+ this.$declareType = DeclareType.DECLARE_TYPE_IN;
220
+ this.$attrib = attr;
221
+ return this;
222
+ }
223
+ /**
224
+ * Create tags for the variable
225
+ * @param args - tags
226
+ * @returns self
227
+ */ tag(...args) {
228
+ args.forEach((val)=>{
229
+ if (this.$tags.indexOf(val) < 0) {
230
+ this.$tags.push(val);
231
+ }
232
+ });
233
+ return this;
234
+ }
235
+ /**
236
+ * Set sample type for the variable if the variable is of type texture
237
+ * @param type - sample type
238
+ * @returns self
239
+ */ sampleType(type) {
240
+ if (type) {
241
+ this.$sampleType = type;
242
+ }
243
+ return this;
244
+ }
245
+ /**
246
+ * Get element in the array by index
247
+ * @param index - index of the element
248
+ * @returns the element variable
249
+ */ at(index) {
250
+ const varType = this.$ast.getType();
251
+ if (!varType.isArrayType() && (!varType.isPrimitiveType() || !varType.isVectorType() && !varType.isMatrixType())) {
252
+ throw new Error('at() function must be used with array types');
253
+ }
254
+ let elementType = null;
255
+ let dimension;
256
+ if (varType.isArrayType()) {
257
+ elementType = varType.elementType;
258
+ dimension = varType.dimension;
259
+ } else if (varType.isVectorType()) {
260
+ elementType = PBPrimitiveTypeInfo.getCachedTypeInfo(varType.resizeType(1, 1));
261
+ dimension = varType.cols;
262
+ } else if (varType.isMatrixType()) {
263
+ elementType = PBPrimitiveTypeInfo.getCachedTypeInfo(varType.resizeType(1, varType.cols));
264
+ dimension = varType.rows;
265
+ }
266
+ const result = new PBShaderExp('', elementType);
267
+ if (typeof index === 'number') {
268
+ if (!Number.isInteger(index)) {
269
+ throw new Error('at() array index must be integer type');
270
+ }
271
+ if (index < 0 || dimension > 0 && index >= dimension) {
272
+ throw new Error('at() array index out of bounds');
273
+ }
274
+ result.$ast = new ASTArrayIndex(this.$ast, new ASTScalar(index, typeI32), elementType);
275
+ } else {
276
+ const type = index.$ast.getType();
277
+ if (!type.isPrimitiveType() || !type.isScalarType()) {
278
+ throw new Error('at() array index must be scalar type');
279
+ }
280
+ let ast = index.$ast;
281
+ if (type.scalarType !== PBPrimitiveType.I32 && type.scalarType !== PBPrimitiveType.U32) {
282
+ ast = new ASTCast(ast, typeI32);
283
+ }
284
+ result.$ast = new ASTArrayIndex(this.$ast, ast, elementType);
285
+ }
286
+ return result;
287
+ }
288
+ /**
289
+ * Set element in the array by index
290
+ * @param index - index of the element
291
+ * @param val - value to set
292
+ */ setAt(index, val) {
293
+ const varType = this.$ast.getType();
294
+ if (!varType.isArrayType()) {
295
+ throw new Error('setAt() function must be used with array types');
296
+ }
297
+ if (typeof index === 'number') {
298
+ if (!Number.isInteger(index)) {
299
+ throw new Error('setAt() array index must be integer type');
300
+ }
301
+ if (index < 0 || varType.dimension > 0 && index >= varType.dimension) {
302
+ throw new Error('setAt() array index out of bounds');
303
+ }
304
+ }
305
+ currentProgramBuilder.getCurrentScope().$ast.statements.push(new ASTAssignment(new ASTLValueArray(new ASTLValueScalar(this.$ast), typeof index === 'number' ? new ASTScalar(index, typeI32) : index.$ast, varType.elementType), val instanceof PBShaderExp ? val.$ast : val));
306
+ }
307
+ /**
308
+ * Point out that the variable should be in high precision
309
+ * @returns self
310
+ */ highp() {
311
+ this.$precision = ShaderPrecisionType.HIGH;
312
+ return this;
313
+ }
314
+ /**
315
+ * Points out that the variable should be in medium precision
316
+ * @returns self
317
+ */ mediump() {
318
+ this.$precision = ShaderPrecisionType.MEDIUM;
319
+ return this;
320
+ }
321
+ /**
322
+ * Points out that the variable should be in low precision
323
+ * @returns self
324
+ */ lowp() {
325
+ this.$precision = ShaderPrecisionType.LOW;
326
+ return this;
327
+ }
328
+ /**
329
+ * Determine if this variable is of vector type
330
+ * @returns true if the variable is of vector type, otherwise false
331
+ */ isVector() {
332
+ const varType = this.$ast.getType();
333
+ return varType.isPrimitiveType() && varType.isVectorType();
334
+ }
335
+ /**
336
+ * Get vector component count of the variable if this variable is of vector type
337
+ * @returns the vector component count
338
+ */ numComponents() {
339
+ const varType = this.$ast.getType();
340
+ return varType.isPrimitiveType() ? varType.cols : 0;
341
+ }
342
+ /**
343
+ * Get type name of this variable
344
+ * @returns The type name of this variable
345
+ */ getTypeName() {
346
+ return this.$ast.getType().toTypeName(currentProgramBuilder.getDevice().type);
347
+ }
348
+ /** @internal */ $get(prop) {
349
+ if (typeof prop === 'string') {
350
+ if (prop[0] === '$' || prop in this) {
351
+ return this[prop];
352
+ } else {
353
+ let exp = this.$memberCache[prop];
354
+ if (!exp) {
355
+ const varType = this.$ast?.getType() || this.$typeinfo;
356
+ const num = Number(prop);
357
+ if (Number.isNaN(num)) {
358
+ if (varType.isStructType()) {
359
+ const elementIndex = varType.structMembers.findIndex((val)=>val.name === prop);
360
+ if (elementIndex < 0) {
361
+ throw new Error(`unknown struct member '${prop}'`);
362
+ }
363
+ const element = varType.structMembers[elementIndex];
364
+ if (element.type.isStructType()) {
365
+ const ctor = currentProgramBuilder.structInfo.structs[element.type.structName];
366
+ exp = ctor.call(currentProgramBuilder, `${this.$str}.${prop}`);
367
+ } else {
368
+ exp = new PBShaderExp(`${this.$str}.${prop}`, element.type);
369
+ }
370
+ exp.$ast = new ASTHash(this.$ast, prop, element.type);
371
+ } else {
372
+ if (!varType.isPrimitiveType() || !varType.isVectorType()) {
373
+ throw new Error(`invalid index operation: ${this.$ast.toString(currentProgramBuilder.getDevice().type)}[${prop}]`);
374
+ }
375
+ if (prop.length === 0 || prop.length > 4 || [
376
+ ...prop
377
+ ].some((val)=>'xyzw'.slice(0, varType.cols).indexOf(val) < 0) && [
378
+ ...prop
379
+ ].some((val)=>'rgba'.slice(0, varType.cols).indexOf(val) < 0)) {
380
+ throw new Error(`unknown swizzle target: ${this.$ast.toString(currentProgramBuilder.getDevice().type)}[${prop}]`);
381
+ }
382
+ const type = PBPrimitiveTypeInfo.getCachedTypeInfo(varType.resizeType(1, prop.length));
383
+ exp = new PBShaderExp('', type);
384
+ exp.$ast = new ASTHash(this.$ast, prop, type);
385
+ }
386
+ } else {
387
+ if (varType.isArrayType()) {
388
+ exp = this.at(num);
389
+ } else if (varType.isPrimitiveType() && varType.isVectorType()) {
390
+ if (num >= varType.cols) {
391
+ throw new Error(`component index out of bounds: ${this.$str}[${num}]`);
392
+ }
393
+ exp = this.$get('xyzw'[num]);
394
+ } else if (varType.isPrimitiveType() && varType.isMatrixType()) {
395
+ const type = PBPrimitiveTypeInfo.getCachedTypeInfo(varType.resizeType(1, varType.cols));
396
+ exp = new PBShaderExp('', type);
397
+ exp.$ast = new ASTArrayIndex(this.$ast, new ASTScalar(num, typeI32), type);
398
+ } else {
399
+ throw new Error(`invalid index operation: ${this.$str}[${num}]`);
400
+ }
401
+ }
402
+ this.$memberCache[prop] = exp;
403
+ }
404
+ return exp;
405
+ }
406
+ } else {
407
+ return undefined;
408
+ }
409
+ }
410
+ /** @internal */ $set(prop, value) {
411
+ if (typeof prop === 'string') {
412
+ if (prop[0] === '$' || prop in this) {
413
+ this[prop] = value;
414
+ } else {
415
+ if (typeof value !== 'number' && typeof value !== 'boolean' && !(value instanceof PBShaderExp)) {
416
+ throw new Error(`Invalid output value assignment`);
417
+ }
418
+ const varType = this.$ast?.getType() || this.$typeinfo;
419
+ const num = Number(prop);
420
+ if (Number.isNaN(num)) {
421
+ if (varType.isStructType()) {
422
+ const elementIndex = varType.structMembers.findIndex((val)=>val.name === prop);
423
+ if (elementIndex < 0) {
424
+ throw new Error(`unknown struct member '${prop}`);
425
+ }
426
+ const element = varType.structMembers[elementIndex];
427
+ let dstAST;
428
+ if (typeof value === 'number' || typeof value === 'boolean') {
429
+ if (!element.type.isPrimitiveType() || !element.type.isScalarType()) {
430
+ throw new Error(`can not set struct member '${prop}: invalid value type`);
431
+ }
432
+ dstAST = new ASTScalar(value, element.type);
433
+ } else if (value instanceof PBShaderExp) {
434
+ dstAST = value.$ast;
435
+ }
436
+ if (!dstAST) {
437
+ throw new Error(`can not set struct member '${prop}: invalid value type`);
438
+ }
439
+ currentProgramBuilder.getCurrentScope().$ast.statements.push(new ASTAssignment(new ASTLValueHash(new ASTLValueScalar(this.$ast), prop, element.type), dstAST));
440
+ } else {
441
+ // FIXME: WGSL does not support l-value swizzling
442
+ if (prop.length > 1 || 'xyzw'.indexOf(prop) < 0 && 'rgba'.indexOf(prop) < 0) {
443
+ throw new Error(`invalid index operation: ${this.$str}[${num}]`);
444
+ }
445
+ if (!varType.isPrimitiveType() || !varType.isVectorType()) {
446
+ throw new Error(`invalid index operation: ${this.$str}[${num}]`);
447
+ }
448
+ const type = PBPrimitiveTypeInfo.getCachedTypeInfo(varType.scalarType);
449
+ currentProgramBuilder.getCurrentScope().$ast.statements.push(new ASTAssignment(new ASTLValueHash(new ASTLValueScalar(this.$ast), prop, type), value instanceof PBShaderExp ? value.$ast : value));
450
+ }
451
+ } else {
452
+ if (varType.isArrayType()) {
453
+ this.setAt(num, value);
454
+ } else if (varType.isPrimitiveType() && varType.isVectorType()) {
455
+ if (num >= varType.cols) {
456
+ throw new Error(`component index out of bounds: ${this.$str}[${num}]`);
457
+ }
458
+ this.$set('xyzw'[num], value);
459
+ } else if (varType.isPrimitiveType() && varType.isMatrixType()) {
460
+ if (!(value instanceof PBShaderExp)) {
461
+ throw new Error(`invalid matrix column vector assignment: ${this.$str}[${num}]`);
462
+ }
463
+ const type = PBPrimitiveTypeInfo.getCachedTypeInfo(varType.resizeType(1, varType.cols));
464
+ currentProgramBuilder.getCurrentScope().$ast.statements.push(new ASTAssignment(new ASTLValueArray(new ASTLValueScalar(this.$ast), new ASTScalar(num, typeI32), type), value.$ast));
465
+ } else {
466
+ throw new Error(`invalid index operation: ${this.$str}[${num}]`);
467
+ }
468
+ }
469
+ }
470
+ return true;
471
+ }
472
+ return false;
473
+ }
474
+ }
475
+
476
+ export { PBShaderExp, Proxiable, getCurrentProgramBuilder, makeConstructor, setCurrentProgramBuilder };
477
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}