@zephyr3d/device 0.2.4 → 0.2.6

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 (44) hide show
  1. package/dist/base_types.js +19 -79
  2. package/dist/base_types.js.map +1 -1
  3. package/dist/builder/ast.js +142 -162
  4. package/dist/builder/ast.js.map +1 -1
  5. package/dist/builder/base.js +29 -27
  6. package/dist/builder/base.js.map +1 -1
  7. package/dist/builder/builtinfunc.js +61 -7
  8. package/dist/builder/builtinfunc.js.map +1 -1
  9. package/dist/builder/constructors.js +3 -1
  10. package/dist/builder/constructors.js.map +1 -1
  11. package/dist/builder/errors.js +11 -11
  12. package/dist/builder/errors.js.map +1 -1
  13. package/dist/builder/misc.js +10 -0
  14. package/dist/builder/misc.js.map +1 -0
  15. package/dist/builder/programbuilder.js +62 -58
  16. package/dist/builder/programbuilder.js.map +1 -1
  17. package/dist/builder/reflection.js +1 -1
  18. package/dist/builder/reflection.js.map +1 -1
  19. package/dist/builder/types.js +76 -75
  20. package/dist/builder/types.js.map +1 -1
  21. package/dist/device.js +220 -80
  22. package/dist/device.js.map +1 -1
  23. package/dist/gpuobject.js +84 -25
  24. package/dist/gpuobject.js.map +1 -1
  25. package/dist/helpers/drawtext.js +30 -25
  26. package/dist/helpers/drawtext.js.map +1 -1
  27. package/dist/helpers/font.js +1 -0
  28. package/dist/helpers/font.js.map +1 -1
  29. package/dist/helpers/glyphmanager.js +33 -15
  30. package/dist/helpers/glyphmanager.js.map +1 -1
  31. package/dist/helpers/textureatlas.js +10 -8
  32. package/dist/helpers/textureatlas.js.map +1 -1
  33. package/dist/index.d.ts +465 -394
  34. package/dist/index.js +6 -5
  35. package/dist/index.js.map +1 -1
  36. package/dist/pool.js +69 -31
  37. package/dist/pool.js.map +1 -1
  38. package/dist/timer.js +3 -2
  39. package/dist/timer.js.map +1 -1
  40. package/dist/uniformdata.js +3 -3
  41. package/dist/uniformdata.js.map +1 -1
  42. package/dist/vertexdata.js +29 -3
  43. package/dist/vertexdata.js.map +1 -1
  44. package/package.json +80 -65
@@ -1,7 +1,9 @@
1
1
  import { ShaderType } from '../base_types.js';
2
2
  import { semanticToAttrib } from '../gpuobject.js';
3
- import { PBPrimitiveTypeInfo, PBPrimitiveType, PBTextureType, PBAddressSpace, PBPointerTypeInfo, typeBool, typeF32, typeI32, typeU32, typeVoid } from './types.js';
3
+ import { PBTextureType, PBPrimitiveTypeInfo, PBPrimitiveType, PBAddressSpace, PBPointerTypeInfo, typeBool, typeF32, typeI32, typeU32, typeVoid } from './types.js';
4
4
  import { PBASTError, PBTypeCastError, PBInternalError, PBParamTypeError } from './errors.js';
5
+ import { getCurrentProgramBuilder } from './misc.js';
6
+ import { ASSERT } from '@zephyr3d/base';
5
7
 
6
8
  const BuiltinInputStructNameVS = 'zVSInput';
7
9
  const BuiltinOutputStructNameVS = 'zVSOutput';
@@ -18,22 +20,22 @@ const BuiltinInputStructInstanceNameFS = 'zFSInputCpy';
18
20
  const BuiltinOutputStructInstanceNameFS = 'zFSOutputCpy';
19
21
  const BuiltinInputStructInstanceNameCS = 'zCSInputCpy';
20
22
  const BuiltinOutputStructInstanceNameCS = 'zCSOutputCpy';
21
- var DeclareType;
22
- (function(DeclareType) {
23
+ /** @internal */ var DeclareType = /*#__PURE__*/ function(DeclareType) {
23
24
  DeclareType[DeclareType["DECLARE_TYPE_NONE"] = 0] = "DECLARE_TYPE_NONE";
24
25
  DeclareType[DeclareType["DECLARE_TYPE_IN"] = 1] = "DECLARE_TYPE_IN";
25
26
  DeclareType[DeclareType["DECLARE_TYPE_OUT"] = 2] = "DECLARE_TYPE_OUT";
26
27
  DeclareType[DeclareType["DECLARE_TYPE_WORKGROUP"] = 3] = "DECLARE_TYPE_WORKGROUP";
27
28
  DeclareType[DeclareType["DECLARE_TYPE_UNIFORM"] = 4] = "DECLARE_TYPE_UNIFORM";
28
29
  DeclareType[DeclareType["DECLARE_TYPE_STORAGE"] = 5] = "DECLARE_TYPE_STORAGE";
29
- })(DeclareType || (DeclareType = {}));
30
- var ShaderPrecisionType;
31
- (function(ShaderPrecisionType) {
30
+ return DeclareType;
31
+ }({});
32
+ /** @internal */ var ShaderPrecisionType = /*#__PURE__*/ function(ShaderPrecisionType) {
32
33
  ShaderPrecisionType[ShaderPrecisionType["NONE"] = 0] = "NONE";
33
34
  ShaderPrecisionType[ShaderPrecisionType["HIGH"] = 1] = "HIGH";
34
35
  ShaderPrecisionType[ShaderPrecisionType["MEDIUM"] = 2] = "MEDIUM";
35
36
  ShaderPrecisionType[ShaderPrecisionType["LOW"] = 3] = "LOW";
36
- })(ShaderPrecisionType || (ShaderPrecisionType = {}));
37
+ return ShaderPrecisionType;
38
+ }({});
37
39
  /** @internal */ function getBuiltinParamName(shaderType) {
38
40
  switch(shaderType){
39
41
  case ShaderType.Vertex:
@@ -43,7 +45,7 @@ var ShaderPrecisionType;
43
45
  case ShaderType.Compute:
44
46
  return BuiltinParamNameCS;
45
47
  default:
46
- return null;
48
+ throw new Error(`Invalid shader type: ${shaderType}`);
47
49
  }
48
50
  }
49
51
  /** @internal */ function getBuiltinInputStructInstanceName(shaderType) {
@@ -55,7 +57,7 @@ var ShaderPrecisionType;
55
57
  case ShaderType.Compute:
56
58
  return BuiltinInputStructInstanceNameCS;
57
59
  default:
58
- return null;
60
+ throw new Error(`Invalid shader type: ${shaderType}`);
59
61
  }
60
62
  }
61
63
  /** @internal */ function getBuiltinOutputStructInstanceName(shaderType) {
@@ -67,7 +69,7 @@ var ShaderPrecisionType;
67
69
  case ShaderType.Compute:
68
70
  return BuiltinOutputStructInstanceNameCS;
69
71
  default:
70
- return null;
72
+ throw new Error(`Invalid shader type: ${shaderType}`);
71
73
  }
72
74
  }
73
75
  /** @internal */ function getBuiltinInputStructName(shaderType) {
@@ -79,7 +81,7 @@ var ShaderPrecisionType;
79
81
  case ShaderType.Compute:
80
82
  return BuiltinInputStructNameCS;
81
83
  default:
82
- return null;
84
+ throw new Error(`Invalid shader type: ${shaderType}`);
83
85
  }
84
86
  }
85
87
  /** @internal */ function getBuiltinOutputStructName(shaderType) {
@@ -91,7 +93,7 @@ var ShaderPrecisionType;
91
93
  case ShaderType.Compute:
92
94
  return BuiltinOutputStructNameCS;
93
95
  default:
94
- return null;
96
+ throw new Error(`Invalid shader type: ${shaderType}`);
95
97
  }
96
98
  }
97
99
  /** @internal */ function getTextureSampleType(type) {
@@ -124,7 +126,7 @@ var ShaderPrecisionType;
124
126
  case PBTextureType.UTEX_CUBE:
125
127
  return new PBPrimitiveTypeInfo(PBPrimitiveType.U32);
126
128
  default:
127
- return null;
129
+ throw new Error(`Invalid texture type: ${type}`);
128
130
  }
129
131
  }
130
132
  /** @internal */ function genSamplerName(textureName, comparison) {
@@ -336,16 +338,16 @@ function unbracket(e) {
336
338
  getType() {
337
339
  return null;
338
340
  }
339
- toWebGL(indent, ctx) {
341
+ toWebGL(_indent, _ctx) {
340
342
  return '';
341
343
  }
342
- toWebGL2(indent, ctx) {
344
+ toWebGL2(_indent, _ctx) {
343
345
  return '';
344
346
  }
345
- toWGSL(indent, ctx) {
347
+ toWGSL(_indent, _ctx) {
346
348
  return '';
347
349
  }
348
- toString(deviceType) {
350
+ toString(_deviceType) {
349
351
  return this.constructor.name;
350
352
  }
351
353
  }
@@ -446,7 +448,7 @@ function unbracket(e) {
446
448
  const body = ctx.types.map((val)=>val.toWebGL(indent, ctx)).join('') + this.uniforms.map((uniform)=>uniform.toWebGL(indent, ctx)).join('') + ctx.inputs.map((input)=>input.toWebGL(indent, ctx)).join('') + ctx.outputs.map((output)=>output.toWebGL(indent, ctx)).join('') + super.toWebGL(indent, ctx);
447
449
  for (const k of ctx.builtins){
448
450
  const info = builtinVariables.webgl[k];
449
- if (info.extension) {
451
+ if ('extension' in info) {
450
452
  ctx.extensions.add(info.extension);
451
453
  }
452
454
  }
@@ -462,7 +464,7 @@ function unbracket(e) {
462
464
  const body = ctx.types.map((val)=>val.toWebGL2(indent, ctx)).join('') + this.uniforms.map((uniform)=>uniform.toWebGL2(indent, ctx)).join('') + ctx.inputs.map((input)=>input.toWebGL2(indent, ctx)).join('') + ctx.outputs.map((output)=>output.toWebGL2(indent, ctx)).join('') + super.toWebGL2(indent, ctx);
463
465
  for (const k of ctx.builtins){
464
466
  const info = builtinVariables.webgl2[k];
465
- if (info.extension) {
467
+ if ('extension' in info) {
466
468
  ctx.extensions.add(info.extension);
467
469
  }
468
470
  }
@@ -491,7 +493,7 @@ function unbracket(e) {
491
493
  if (type instanceof ASTStructDefine && structNames.indexOf(type.type.structName) >= 0) {
492
494
  for(let i = type.type.structMembers.length - 1; i >= 0; i--){
493
495
  const member = type.type.structMembers[i];
494
- if (allBuiltins.indexOf(member.name) >= 0 && usedBuiltins.indexOf(member.name) < 0) {
496
+ if ('name' in member && allBuiltins.indexOf(member.name) >= 0 && usedBuiltins.indexOf(member.name) < 0) {
495
497
  type.type.structMembers.splice(i, 1);
496
498
  type.prefix.splice(i, 1);
497
499
  }
@@ -536,12 +538,12 @@ function unbracket(e) {
536
538
  }
537
539
  getAddressSpace() {
538
540
  switch(this.value.$declareType){
539
- case DeclareType.DECLARE_TYPE_UNIFORM:
541
+ case 4:
540
542
  return PBAddressSpace.UNIFORM;
541
- case DeclareType.DECLARE_TYPE_STORAGE:
543
+ case 5:
542
544
  return PBAddressSpace.STORAGE;
543
- case DeclareType.DECLARE_TYPE_IN:
544
- case DeclareType.DECLARE_TYPE_OUT:
545
+ case 1:
546
+ case 2:
545
547
  return null;
546
548
  default:
547
549
  return this.value.$global ? PBAddressSpace.PRIVATE : PBAddressSpace.FUNCTION;
@@ -550,24 +552,24 @@ function unbracket(e) {
550
552
  getType() {
551
553
  return this.value.$typeinfo;
552
554
  }
553
- toWebGL(indent, ctx) {
555
+ toWebGL(_indent, _ctx) {
554
556
  return this.name;
555
557
  }
556
- toWebGL2(indent, ctx) {
558
+ toWebGL2(_indent, _ctx) {
557
559
  return this.name;
558
560
  }
559
561
  toWGSL(indent, ctx) {
560
- if (this.value.$declareType === DeclareType.DECLARE_TYPE_IN) {
562
+ if (this.value.$declareType === 1) {
561
563
  const structName = getBuiltinInputStructInstanceName(ctx.type);
562
564
  return ctx.global[structName][this.name].$ast.toWGSL(indent, ctx);
563
- } else if (this.value.$declareType === DeclareType.DECLARE_TYPE_OUT) {
565
+ } else if (this.value.$declareType === 2) {
564
566
  const structName = getBuiltinOutputStructInstanceName(ctx.type);
565
567
  return ctx.global[structName][this.name].$ast.toWGSL(indent, ctx);
566
568
  } else {
567
569
  return this.name;
568
570
  }
569
571
  }
570
- toString(deviceType) {
572
+ toString(_deviceType) {
571
573
  return this.name;
572
574
  }
573
575
  }
@@ -704,45 +706,41 @@ function unbracket(e) {
704
706
  isReference() {
705
707
  return true;
706
708
  }
707
- toWebGL(indent, ctx) {
709
+ toWebGL(_indent, _ctx) {
708
710
  let prefix = '';
709
711
  switch(this.value.value.$declareType){
710
- case DeclareType.DECLARE_TYPE_IN:
711
- case DeclareType.DECLARE_TYPE_OUT:
712
- case DeclareType.DECLARE_TYPE_UNIFORM:
713
- case DeclareType.DECLARE_TYPE_STORAGE:
712
+ case 1:
713
+ case 2:
714
+ case 4:
715
+ case 5:
714
716
  throw new Error('invalid declare type');
715
717
  default:
716
718
  prefix = this.value.constExp && !this.value.isWritable() && !this.getType().isStructType() ? 'const ' : '';
717
719
  break;
718
720
  }
719
- {
720
- return `${prefix}${this.getType().toTypeName('webgl', this.value.name)}`;
721
- }
721
+ return `${prefix}${this.getType().toTypeName('webgl', this.value.name)}`;
722
722
  }
723
- toWebGL2(indent, ctx) {
723
+ toWebGL2(_indent, _ctx) {
724
724
  let prefix = '';
725
725
  switch(this.value.value.$declareType){
726
- case DeclareType.DECLARE_TYPE_IN:
727
- case DeclareType.DECLARE_TYPE_OUT:
728
- case DeclareType.DECLARE_TYPE_UNIFORM:
729
- case DeclareType.DECLARE_TYPE_STORAGE:
726
+ case 1:
727
+ case 2:
728
+ case 4:
729
+ case 5:
730
730
  throw new Error('invalid declare type');
731
731
  default:
732
732
  prefix = this.value.constExp && !this.value.isWritable() && !this.getType().isStructType() ? 'const ' : '';
733
733
  break;
734
734
  }
735
- {
736
- return `${prefix}${this.getType().toTypeName('webgl2', this.value.name)}`;
737
- }
735
+ return `${prefix}${this.getType().toTypeName('webgl2', this.value.name)}`;
738
736
  }
739
- toWGSL(indent, ctx) {
737
+ toWGSL(_indent, _ctx) {
740
738
  let prefix;
741
739
  switch(this.value.value.$declareType){
742
- case DeclareType.DECLARE_TYPE_IN:
743
- case DeclareType.DECLARE_TYPE_OUT:
744
- case DeclareType.DECLARE_TYPE_UNIFORM:
745
- case DeclareType.DECLARE_TYPE_STORAGE:
740
+ case 1:
741
+ case 2:
742
+ case 4:
743
+ case 5:
746
744
  throw new Error('invalid declare type');
747
745
  default:
748
746
  {
@@ -755,15 +753,13 @@ function unbracket(e) {
755
753
  break;
756
754
  }
757
755
  }
758
- {
759
- // const decl = this.value.value.$global ? this.getType().toTypeName('webgpu', this.value.name) : this.value.name;
760
- const type = this.getType();
761
- if (type.isPointerType() && (this.value.isWritable() || this.value.ref.isWritable())) {
762
- type.writable = true;
763
- }
764
- const decl = type.toTypeName('webgpu', this.value.name);
765
- return `${prefix}${decl}`;
756
+ // const decl = this.value.value.$global ? this.getType().toTypeName('webgpu', this.value.name) : this.value.name;
757
+ const type = this.getType();
758
+ if (type.isPointerType() && (this.value.isWritable() || this.value.ref.isWritable())) {
759
+ type.writable = true;
766
760
  }
761
+ const decl = type.toTypeName('webgpu', this.value.name);
762
+ return `${prefix}${decl}`;
767
763
  }
768
764
  toString(deviceType) {
769
765
  return this.value.toString(deviceType);
@@ -773,6 +769,7 @@ function unbracket(e) {
773
769
  /** @internal */ type;
774
770
  /** @internal */ args;
775
771
  /** @internal */ constExp;
772
+ /** @internal */ convertedArgs;
776
773
  constructor(type, args){
777
774
  super();
778
775
  this.type = type;
@@ -787,6 +784,18 @@ function unbracket(e) {
787
784
  }
788
785
  this.constExp &&= !(arg instanceof ASTExpression) || arg.isConstExp();
789
786
  }
787
+ const deviceType = getCurrentProgramBuilder().getDevice().type;
788
+ const overloads = this.type.getConstructorOverloads(deviceType);
789
+ for (const overload of overloads){
790
+ const convertedArgs = convertArgs(this.args, overload);
791
+ if (convertedArgs) {
792
+ this.convertedArgs = convertedArgs;
793
+ break;
794
+ }
795
+ }
796
+ if (!this.convertedArgs) {
797
+ throw new Error(`no matching overload function found for type ${this.type.toTypeName(deviceType)}`);
798
+ }
790
799
  }
791
800
  getType() {
792
801
  return this.type;
@@ -802,48 +811,18 @@ function unbracket(e) {
802
811
  return null;
803
812
  }
804
813
  toWebGL(indent, ctx) {
805
- console.assert(!this.type.isArrayType(), 'array constructor not supported in webgl1 device');
806
- console.assert(this.type.isConstructible(), `type '${this.type.toTypeName('webgl')}' is not constructible`);
807
- const overloads = this.type.getConstructorOverloads('webgl');
808
- for (const overload of overloads){
809
- const convertedArgs = convertArgs(this.args, overload);
810
- if (convertedArgs) {
811
- const c = convertedArgs.args.map((arg)=>unbracket(arg.toWebGL(indent, ctx))).join(',');
812
- return `${convertedArgs.name}(${c})`;
813
- }
814
- }
815
- throw new Error(`no matching overload function found for type ${this.type.toTypeName('webgl')}`);
814
+ const c = this.convertedArgs.args.map((arg)=>unbracket(arg.toWebGL(indent, ctx))).join(',');
815
+ return `${this.convertedArgs.name}(${c})`;
816
816
  }
817
817
  toWebGL2(indent, ctx) {
818
- console.assert(this.type.isConstructible(), `type '${this.type.toTypeName('webgl2')}' is not constructible`, true);
819
- const overloads = this.type.getConstructorOverloads('webgl2');
820
- for (const overload of overloads){
821
- const convertedArgs = convertArgs(this.args, overload);
822
- if (convertedArgs) {
823
- const c = convertedArgs.args.map((arg)=>unbracket(arg.toWebGL2(indent, ctx))).join(',');
824
- return `${convertedArgs.name}(${c})`;
825
- }
826
- }
827
- throw new Error(`no matching overload function found for type ${this.type.toTypeName('webgl2')}`);
818
+ const c = this.convertedArgs.args.map((arg)=>unbracket(arg.toWebGL2(indent, ctx))).join(',');
819
+ return `${this.convertedArgs.name}(${c})`;
828
820
  }
829
821
  toWGSL(indent, ctx) {
830
- /*
831
- console.assert(
832
- this.type.isConstructible(),
833
- `type '${this.type.toTypeName('webgpu')}' is not constructible`,
834
- true
835
- );
836
- */ const overloads = this.type.getConstructorOverloads('webgpu');
837
- for (const overload of overloads){
838
- const convertedArgs = convertArgs(this.args, overload);
839
- if (convertedArgs) {
840
- const c = convertedArgs.args.map((arg)=>unbracket(arg.toWGSL(indent, ctx))).join(',');
841
- return `${convertedArgs.name}(${c})`;
842
- }
843
- }
844
- throw new Error(`no matching overload function found for type ${this.type.toTypeName('webgpu')}`);
822
+ const c = this.convertedArgs.args.map((arg)=>unbracket(arg.toWGSL(indent, ctx))).join(',');
823
+ return `${this.convertedArgs.name}(${c})`;
845
824
  }
846
- toString(deviceType) {
825
+ toString(_deviceType) {
847
826
  return 'constructor';
848
827
  }
849
828
  }
@@ -881,21 +860,21 @@ function unbracket(e) {
881
860
  getAddressSpace() {
882
861
  return null;
883
862
  }
884
- toWebGL(indent, ctx) {
863
+ toWebGL(_indent, _ctx) {
885
864
  switch(this.type.primitiveType){
886
865
  case PBPrimitiveType.F32:
887
866
  return toFixed(this.value);
888
867
  case PBPrimitiveType.I32:
889
868
  return toInt(this.value);
890
869
  case PBPrimitiveType.U32:
891
- return toUint(this.value);
870
+ return `${toUint(this.value)}u`;
892
871
  case PBPrimitiveType.BOOL:
893
872
  return String(!!this.value);
894
873
  default:
895
874
  throw new Error('Invalid scalar type');
896
875
  }
897
876
  }
898
- toWebGL2(indent, ctx) {
877
+ toWebGL2(_indent, _ctx) {
899
878
  switch(this.type.primitiveType){
900
879
  case PBPrimitiveType.F32:
901
880
  return toFixed(this.value);
@@ -909,7 +888,7 @@ function unbracket(e) {
909
888
  throw new Error('Invalid scalar type');
910
889
  }
911
890
  }
912
- toWGSL(indent, ctx) {
891
+ toWGSL(_indent, _ctx) {
913
892
  switch(this.type.primitiveType){
914
893
  case PBPrimitiveType.F32:
915
894
  return toFixed(this.value);
@@ -923,7 +902,7 @@ function unbracket(e) {
923
902
  throw new Error('Invalid scalar type');
924
903
  }
925
904
  }
926
- toString(deviceType) {
905
+ toString(_deviceType) {
927
906
  return `${this.value}`;
928
907
  }
929
908
  }
@@ -959,14 +938,14 @@ function unbracket(e) {
959
938
  return this.source.getAddressSpace();
960
939
  }
961
940
  toWebGL(indent, ctx) {
962
- return `${this.source.toWebGL(indent, ctx)}.${this.field}`;
941
+ return this.source instanceof ASTScalar ? `(${this.source.toWebGL(indent, ctx)}).${this.field}` : `${this.source.toWebGL(indent, ctx)}.${this.field}`;
963
942
  }
964
943
  toWebGL2(indent, ctx) {
965
- return `${this.source.toWebGL2(indent, ctx)}.${this.field}`;
944
+ return this.source instanceof ASTScalar ? `(${this.source.toWebGL(indent, ctx)}).${this.field}` : `${this.source.toWebGL(indent, ctx)}.${this.field}`;
966
945
  }
967
946
  toWGSL(indent, ctx) {
968
947
  const source = this.source.isPointer() ? new ASTReferenceOf(this.source) : this.source;
969
- return `${source.toWGSL(indent, ctx)}.${this.field}`;
948
+ return source instanceof ASTScalar ? `(${source.toWGSL(indent, ctx)}).${this.field}` : `${source.toWGSL(indent, ctx)}.${this.field}`;
970
949
  }
971
950
  toString(deviceType) {
972
951
  const source = this.source.isPointer() ? new ASTReferenceOf(this.source) : this.source;
@@ -1027,7 +1006,7 @@ function unbracket(e) {
1027
1006
  /** @internal */ type;
1028
1007
  constructor(value){
1029
1008
  super();
1030
- console.assert(value.isReference(), 'no pointer type for non-reference values', true);
1009
+ ASSERT(value.isReference(), 'no pointer type for non-reference values');
1031
1010
  this.value = value;
1032
1011
  this.type = new PBPointerTypeInfo(value.getType(), value.getAddressSpace());
1033
1012
  }
@@ -1050,10 +1029,10 @@ function unbracket(e) {
1050
1029
  getAddressSpace() {
1051
1030
  return this.value.getAddressSpace();
1052
1031
  }
1053
- toWebGL(indent, ctx) {
1032
+ toWebGL(_indent, _ctx) {
1054
1033
  throw new Error('GLSL does not support pointer type');
1055
1034
  }
1056
- toWebGL2(indent, ctx) {
1035
+ toWebGL2(_indent, _ctx) {
1057
1036
  throw new Error('GLSL does not support pointer type');
1058
1037
  }
1059
1038
  toWGSL(indent, ctx) {
@@ -1407,12 +1386,20 @@ function unbracket(e) {
1407
1386
  if (this.rvalue instanceof ASTCallFunction) {
1408
1387
  this.rvalue.isStatement = false;
1409
1388
  }
1389
+ const ltype = this.lvalue.getType();
1390
+ const valueTypeLeft = ltype.isPointerType() ? ltype.pointerType : ltype;
1391
+ const rtype = this.checkScalarType(this.rvalue, valueTypeLeft);
1392
+ const rvalueIsPtr = rtype && rtype.isPointerType();
1393
+ const valueTypeRight = rvalueIsPtr ? rtype.pointerType : rtype;
1394
+ if (!valueTypeLeft.isCompatibleType(valueTypeRight)) {
1395
+ throw new PBTypeCastError(this.rvalue instanceof ASTExpression ? this.rvalue.toString(rtype.isPointerType() ? 'webgpu' : 'webgl') : `${this.rvalue}`, rtype, ltype);
1396
+ }
1410
1397
  }
1411
1398
  getType() {
1412
1399
  return null;
1413
1400
  }
1414
1401
  toWebGL(indent, ctx) {
1415
- let rhs = null;
1402
+ let rhs;
1416
1403
  const ltype = this.lvalue.getType();
1417
1404
  const rtype = this.checkScalarType(this.rvalue, ltype);
1418
1405
  if (!ltype.isCompatibleType(rtype)) {
@@ -1429,7 +1416,7 @@ function unbracket(e) {
1429
1416
  return `${indent}${this.lvalue.toWebGL(indent, ctx)} = ${rhs};\n`;
1430
1417
  }
1431
1418
  toWebGL2(indent, ctx) {
1432
- let rhs = null;
1419
+ let rhs;
1433
1420
  const ltype = this.lvalue.getType();
1434
1421
  const rtype = this.checkScalarType(this.rvalue, ltype);
1435
1422
  if (!ltype.isCompatibleType(rtype)) {
@@ -1512,35 +1499,35 @@ function unbracket(e) {
1512
1499
  }
1513
1500
  }
1514
1501
  /** @internal */ class ASTDiscard extends ShaderAST {
1515
- toWebGL(indent, ctx) {
1502
+ toWebGL(indent, _ctx) {
1516
1503
  return `${indent}discard;\n`;
1517
1504
  }
1518
- toWebGL2(indent, ctx) {
1505
+ toWebGL2(indent, _ctx) {
1519
1506
  return `${indent}discard;\n`;
1520
1507
  }
1521
- toWGSL(indent, ctx) {
1508
+ toWGSL(indent, _ctx) {
1522
1509
  return `${indent}discard;\n`;
1523
1510
  }
1524
1511
  }
1525
1512
  /** @internal */ class ASTBreak extends ShaderAST {
1526
- toWebGL(indent, ctx) {
1513
+ toWebGL(indent, _ctx) {
1527
1514
  return `${indent}break;\n`;
1528
1515
  }
1529
- toWebGL2(indent, ctx) {
1516
+ toWebGL2(indent, _ctx) {
1530
1517
  return `${indent}break;\n`;
1531
1518
  }
1532
- toWGSL(indent, ctx) {
1519
+ toWGSL(indent, _ctx) {
1533
1520
  return `${indent}break;\n`;
1534
1521
  }
1535
1522
  }
1536
1523
  /** @internal */ class ASTContinue extends ShaderAST {
1537
- toWebGL(indent, ctx) {
1524
+ toWebGL(indent, _ctx) {
1538
1525
  return `${indent}continue;\n`;
1539
1526
  }
1540
- toWebGL2(indent, ctx) {
1527
+ toWebGL2(indent, _ctx) {
1541
1528
  return `${indent}continue;\n`;
1542
1529
  }
1543
- toWGSL(indent, ctx) {
1530
+ toWGSL(indent, _ctx) {
1544
1531
  return `${indent}continue;\n`;
1545
1532
  }
1546
1533
  }
@@ -1637,7 +1624,7 @@ function unbracket(e) {
1637
1624
  toWGSL(indent, ctx) {
1638
1625
  let thisArgs = this.args;
1639
1626
  if (this.func) {
1640
- let argsNew;
1627
+ let argsNew = null;
1641
1628
  const convertedArgs = convertArgs(thisArgs, this.func.funcType);
1642
1629
  if (convertedArgs) {
1643
1630
  argsNew = convertedArgs.args;
@@ -1656,7 +1643,7 @@ function unbracket(e) {
1656
1643
  const args = thisArgs.map((arg)=>unbracket(arg.toWGSL(indent, ctx)));
1657
1644
  return `${this.isStatement ? indent : ''}${this.name}(${args.join(',')})${this.isStatement ? ';\n' : ''}`;
1658
1645
  }
1659
- toString(deviceType) {
1646
+ toString(_deviceType) {
1660
1647
  return `${this.name}(...)`;
1661
1648
  }
1662
1649
  }
@@ -1670,6 +1657,7 @@ function unbracket(e) {
1670
1657
  this.value = exp;
1671
1658
  this.group = 0;
1672
1659
  this.binding = 0;
1660
+ this.blockName = '';
1673
1661
  }
1674
1662
  isReference() {
1675
1663
  return true;
@@ -1682,7 +1670,7 @@ function unbracket(e) {
1682
1670
  let builtin = false;
1683
1671
  let valueType = this.value.getType();
1684
1672
  switch(this.value.value.$declareType){
1685
- case DeclareType.DECLARE_TYPE_IN:
1673
+ case 1:
1686
1674
  if (ctx.type === ShaderType.Vertex) {
1687
1675
  prefix = 'attribute ';
1688
1676
  ctx.defines.push(`#define ${this.value.name} ${semanticToAttrib(ctx.vertexAttributes[this.value.value.$location])}\n`);
@@ -1691,7 +1679,7 @@ function unbracket(e) {
1691
1679
  // ctx.defines.push(`#define ${this.value.$str} ch_varying_${this.value.$location}\n`);
1692
1680
  }
1693
1681
  break;
1694
- case DeclareType.DECLARE_TYPE_OUT:
1682
+ case 2:
1695
1683
  if (ctx.type === ShaderType.Vertex) {
1696
1684
  prefix = 'varying ';
1697
1685
  // ctx.defines.push(`#define ${this.value.$str} ch_varying_${this.value.$location}\n`);
@@ -1705,22 +1693,23 @@ function unbracket(e) {
1705
1693
  }
1706
1694
  }
1707
1695
  break;
1708
- case DeclareType.DECLARE_TYPE_UNIFORM:
1696
+ case 4:
1709
1697
  prefix = 'uniform ';
1710
1698
  valueType = ctx.typeReplacement?.get(this.value.value) || valueType;
1711
1699
  break;
1712
- case DeclareType.DECLARE_TYPE_STORAGE:
1700
+ case 5:
1713
1701
  throw new Error(`invalid variable declare type: ${this.value.name}`);
1714
1702
  }
1715
1703
  if (!builtin) {
1716
1704
  return `${indent}${prefix}${valueType.toTypeName('webgl', this.value.name)};\n`;
1717
1705
  }
1706
+ return '';
1718
1707
  }
1719
1708
  toWebGL2(indent, ctx) {
1720
1709
  let prefix = '';
1721
1710
  let valueType = this.value.getType();
1722
1711
  switch(this.value.value.$declareType){
1723
- case DeclareType.DECLARE_TYPE_IN:
1712
+ case 1:
1724
1713
  if (ctx.type === ShaderType.Fragment && valueType.isPrimitiveType() && valueType.isInteger()) {
1725
1714
  prefix = 'flat in ';
1726
1715
  } else {
@@ -1730,7 +1719,7 @@ function unbracket(e) {
1730
1719
  ctx.defines.push(`#define ${this.value.name} ${semanticToAttrib(ctx.vertexAttributes[this.value.value.$location])}\n`);
1731
1720
  }
1732
1721
  break;
1733
- case DeclareType.DECLARE_TYPE_OUT:
1722
+ case 2:
1734
1723
  if (ctx.type === ShaderType.Vertex) {
1735
1724
  if (valueType.isPrimitiveType() && valueType.isInteger()) {
1736
1725
  prefix = 'flat out ';
@@ -1741,7 +1730,7 @@ function unbracket(e) {
1741
1730
  prefix = `layout(location = ${this.value.value.$location}) out `;
1742
1731
  }
1743
1732
  break;
1744
- case DeclareType.DECLARE_TYPE_UNIFORM:
1733
+ case 4:
1745
1734
  if (valueType.isStructType()) {
1746
1735
  /*
1747
1736
  if (valueType.layout !== 'std140') {
@@ -1752,42 +1741,38 @@ function unbracket(e) {
1752
1741
  valueType = ctx.typeReplacement?.get(this.value.value) || valueType;
1753
1742
  return `${indent}uniform ${valueType.toTypeName('webgl2', this.value.name)};\n`;
1754
1743
  }
1755
- case DeclareType.DECLARE_TYPE_STORAGE:
1744
+ case 5:
1756
1745
  throw new Error(`invalid variable declare type: ${this.value.name}`);
1757
1746
  }
1758
- {
1759
- return `${indent}${prefix}${this.value.getType().toTypeName('webgl2', this.value.name)};\n`;
1760
- }
1747
+ return `${indent}${prefix}${this.value.getType().toTypeName('webgl2', this.value.name)};\n`;
1761
1748
  }
1762
1749
  toWGSL(indent, ctx) {
1763
1750
  let prefix;
1764
1751
  const isBlock = this.value.getType().isPrimitiveType() || this.value.getType().isStructType() || this.value.getType().isArrayType();
1765
1752
  switch(this.value.value.$declareType){
1766
- case DeclareType.DECLARE_TYPE_IN:
1767
- case DeclareType.DECLARE_TYPE_OUT:
1753
+ case 1:
1754
+ case 2:
1768
1755
  // prefix = `@location(${this.value.value.$location}) var<out> `;
1769
1756
  throw new Error(`Internal error`);
1770
- case DeclareType.DECLARE_TYPE_UNIFORM:
1757
+ case 4:
1771
1758
  prefix = `@group(${this.group}) @binding(${this.binding}) var${isBlock ? '<uniform>' : ''} `;
1772
1759
  break;
1773
- case DeclareType.DECLARE_TYPE_STORAGE:
1760
+ case 5:
1774
1761
  prefix = `@group(${this.group}) @binding(${this.binding}) var<storage, ${this.value.value.$readonly ? 'read' : 'read_write' //this.value.isWritable() || this.value.getType().haveAtomicMembers() ? 'read_write' : 'read'
1775
1762
  }> `;
1776
1763
  break;
1777
- case DeclareType.DECLARE_TYPE_WORKGROUP:
1764
+ case 3:
1778
1765
  prefix = `var<workgroup> `;
1779
1766
  break;
1780
1767
  default:
1781
1768
  prefix = `${this.value.getType().isPointerType() ? 'let' : 'var'}${this.value.value.$global && !this.value.getType().isPointerType() ? '<private>' : ''} `;
1782
1769
  }
1783
- {
1784
- const type = this.value.getType();
1785
- const structName = type.isStructType() ? type.structName : null;
1786
- if (structName && ctx.types.findIndex((val)=>val instanceof ASTStructDefine && val.type.structName === structName) < 0) {
1787
- return '';
1788
- } else {
1789
- return `${indent}${prefix}${type.toTypeName('webgpu', this.value.name)};\n`;
1790
- }
1770
+ const type = this.value.getType();
1771
+ const structName = type.isStructType() ? type.structName : null;
1772
+ if (structName && ctx.types.findIndex((val)=>val instanceof ASTStructDefine && val.type.structName === structName) < 0) {
1773
+ return '';
1774
+ } else {
1775
+ return `${indent}${prefix}${type.toTypeName('webgpu', this.value.name)};\n`;
1791
1776
  }
1792
1777
  }
1793
1778
  toString(deviceType) {
@@ -1952,12 +1937,14 @@ function unbracket(e) {
1952
1937
  /** @internal */ start;
1953
1938
  /** @internal */ end;
1954
1939
  /** @internal */ open;
1955
- constructor(init, start, end, open){
1940
+ /** @internal */ reverse;
1941
+ constructor(init, start, end, open, reverse){
1956
1942
  super();
1957
1943
  this.init = init;
1958
1944
  this.start = start;
1959
1945
  this.end = end;
1960
1946
  this.open = open;
1947
+ this.reverse = reverse;
1961
1948
  this.statements = [];
1962
1949
  if (this.start instanceof ASTCallFunction) {
1963
1950
  this.start.isStatement = false;
@@ -1970,8 +1957,8 @@ function unbracket(e) {
1970
1957
  const init = this.init.getType().toTypeName('webgl', this.init.name);
1971
1958
  const start = unbracket(this.start.toWebGL(indent, ctx));
1972
1959
  const end = unbracket(this.end.toWebGL(indent, ctx));
1973
- const comp = this.open ? '<' : '<=';
1974
- let str = `${indent}for (${init} = ${start}; ${this.init.name} ${comp} ${end}; ${this.init.name}++) {\n`;
1960
+ const comp = this.open ? this.reverse ? '>' : '<' : this.reverse ? '>=' : '<=';
1961
+ let str = `${indent}for (${init} = ${start}; ${this.init.name} ${comp} ${end}; ${this.init.name}${this.reverse ? '--' : '++'}) {\n`;
1975
1962
  str += super.toWebGL(indent + ' ', ctx);
1976
1963
  str += `${indent}}\n`;
1977
1964
  return str;
@@ -1980,8 +1967,8 @@ function unbracket(e) {
1980
1967
  const init = this.init.getType().toTypeName('webgl2', this.init.name);
1981
1968
  const start = unbracket(this.start.toWebGL2(indent, ctx));
1982
1969
  const end = unbracket(this.end.toWebGL2(indent, ctx));
1983
- const comp = this.open ? '<' : '<=';
1984
- let str = `${indent}for (${init} = ${start}; ${this.init.name} ${comp} ${end}; ${this.init.name}++) {\n`;
1970
+ const comp = this.open ? this.reverse ? '>' : '<' : this.reverse ? '>=' : '<=';
1971
+ let str = `${indent}for (${init} = ${start}; ${this.init.name} ${comp} ${end}; ${this.init.name}${this.reverse ? '--' : '++'}) {\n`;
1985
1972
  str += super.toWebGL2(indent + ' ', ctx);
1986
1973
  str += `${indent}}\n`;
1987
1974
  return str;
@@ -1991,8 +1978,8 @@ function unbracket(e) {
1991
1978
  const start = unbracket(this.start.toWGSL(indent, ctx));
1992
1979
  const end = unbracket(this.end.toWGSL(indent, ctx));
1993
1980
  const incr = new ASTScalar(1, this.init.getType()).toWGSL(indent, ctx);
1994
- const comp = this.open ? '<' : '<=';
1995
- let str = `${indent}for (${init} = ${start}; ${this.init.name} ${comp} ${end}; ${this.init.name} = ${this.init.name} + ${incr}) {\n`;
1981
+ const comp = this.open ? this.reverse ? '> ' : '<' : this.reverse ? '>=' : '<=';
1982
+ let str = `${indent}for (${init} = ${start}; ${this.init.name} ${comp} ${end}; ${this.init.name} = ${this.init.name} ${this.reverse ? '-' : '+'} ${incr}) {\n`;
1996
1983
  str += super.toWGSL(indent + ' ', ctx);
1997
1984
  str += `${indent}}\n`;
1998
1985
  return str;
@@ -2007,7 +1994,7 @@ function unbracket(e) {
2007
1994
  this.condition.isStatement = false;
2008
1995
  }
2009
1996
  }
2010
- toWebGL(indent, ctx) {
1997
+ toWebGL(_indent, _ctx) {
2011
1998
  throw new Error(`No do-while() loop support for WebGL1.0 device`);
2012
1999
  }
2013
2000
  toWebGL2(indent, ctx) {
@@ -2052,14 +2039,7 @@ function unbracket(e) {
2052
2039
  str += super.toWGSL(indent + ' ', ctx);
2053
2040
  str += `${indent}}\n`;
2054
2041
  return str;
2055
- /*
2056
- let str = `${indent}loop {\n`;
2057
- const newIndent = indent + ' ';
2058
- str += `${newIndent}if (!(${unbracket(this.condition.toWGSL(indent, ctx))})) { break; }\n`;
2059
- str += super.toWGSL(newIndent, ctx);
2060
- str += `${indent}}\n`;
2061
- return str;
2062
- */ }
2042
+ }
2063
2043
  }
2064
2044
  /** @internal */ class ASTStructDefine extends ShaderAST {
2065
2045
  /** @internal */ type;
@@ -2074,7 +2054,7 @@ function unbracket(e) {
2074
2054
  getType() {
2075
2055
  return this.type;
2076
2056
  }
2077
- toWebGL(indent, ctx) {
2057
+ toWebGL(indent, _ctx) {
2078
2058
  if (!this.builtin) {
2079
2059
  let str = `${indent}struct ${this.type.structName} {\n`;
2080
2060
  for (const arg of this.type.structMembers){
@@ -2086,7 +2066,7 @@ function unbracket(e) {
2086
2066
  return '';
2087
2067
  }
2088
2068
  }
2089
- toWebGL2(indent, ctx) {
2069
+ toWebGL2(indent, _ctx) {
2090
2070
  if (!this.builtin) {
2091
2071
  let str = `${indent}struct ${this.type.structName} {\n`;
2092
2072
  for (const arg of this.type.structMembers){
@@ -2098,7 +2078,7 @@ function unbracket(e) {
2098
2078
  return '';
2099
2079
  }
2100
2080
  }
2101
- toWGSL(indent, ctx) {
2081
+ toWGSL(indent, _ctx) {
2102
2082
  if (!this.builtin) {
2103
2083
  let str = `${indent}struct ${this.type.structName} {\n`;
2104
2084
  str += this.type.structMembers.map((arg, i)=>{