ParseFieldsMacro.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package hrt.shgraph;
  2. import hrt.shgraph.ShaderType;
  3. import haxe.macro.Context;
  4. import haxe.macro.Expr;
  5. using haxe.macro.Tools;
  6. typedef SField = { name : String, value : ShaderType.SType };
  7. class ParseFieldsMacro {
  8. #if macro
  9. public static function build() : Array<Field> {
  10. var fields = Context.getBuildFields();
  11. var mapInputs = new Array<Expr>();
  12. var hasInputs = false;
  13. var mapOutputs = new Array<Expr>();
  14. var hasOutputs = false;
  15. for ( f in fields ) {
  16. if( f.meta == null ) continue;
  17. switch (f.kind) {
  18. case FVar(t, e):
  19. var saveMeta = f.meta;
  20. for (m in saveMeta) {
  21. if (m.name == "input") {
  22. hasInputs = true;
  23. var sel = f.name;
  24. var get_sel = "get_" + sel;
  25. var sfields = macro class {
  26. inline function $get_sel() : NodeVar return getInput($v{sel});
  27. };
  28. for( field in sfields.fields )
  29. fields.push(field);
  30. if (e == null)
  31. Context.error('Input ${sel} has not affectation', f.pos);
  32. var enumValue = ["ShaderType", "SType", e.toString().split(".").pop()];
  33. mapInputs.push(macro $v{sel} => ${enumValue.toFieldExpr()});
  34. f.kind = FProp("get", "null", TPath({ pack: ["hrt", "shgraph"], name: "NodeVar" }));
  35. f.meta = saveMeta;
  36. break;
  37. }
  38. if (m.name == "output") {
  39. hasOutputs = true;
  40. var sel = f.name;
  41. var get_sel = "get_" + sel;
  42. var sfields = macro class {
  43. inline function $get_sel() : TVar return getOutput($v{sel});
  44. };
  45. for( field in sfields.fields )
  46. fields.push(field);
  47. if (e == null)
  48. Context.error('Output ${sel} has not affectation', f.pos);
  49. var enumValue = ["ShaderType", "SType", e.toString().split(".").pop()];
  50. mapOutputs.push(macro $v{sel} => ${enumValue.toFieldExpr()});
  51. f.kind = FProp("get", "null", TPath({ pack: [], name: "TVar" }));
  52. f.meta = saveMeta;
  53. break;
  54. }
  55. }
  56. default:
  57. }
  58. }
  59. if (hasInputs) {
  60. fields.push({
  61. name: "inputsInfo",
  62. access: [Access.APrivate],
  63. kind: FieldType.FVar(macro:Map<String, ShaderType.SType>, macro $a{mapInputs}),
  64. pos: Context.currentPos(),
  65. });
  66. var sfields = macro class {
  67. override public function getInputInfo(key : String) : ShaderType.SType return inputsInfo.get(key);
  68. };
  69. for( field in sfields.fields )
  70. fields.push(field);
  71. }
  72. if (hasOutputs) {
  73. fields.push({
  74. name: "outputsInfo",
  75. access: [Access.APrivate],
  76. kind: FieldType.FVar(macro:Map<String, ShaderType.SType>, macro $a{mapOutputs}),
  77. pos: Context.currentPos(),
  78. });
  79. var sfields = macro class {
  80. override public function getOutputInfo(key : String) : ShaderType.SType return outputsInfo.get(key);
  81. };
  82. for( field in sfields.fields )
  83. fields.push(field);
  84. }
  85. var thisClass = Context.getLocalClass();
  86. var cl = thisClass.get();
  87. var clPath = cl.pack.copy();
  88. clPath.push(cl.name);
  89. fields.push({
  90. name: "_",
  91. access: [Access.AStatic],
  92. kind: FieldType.FVar(macro:Bool, macro ShaderNode.register($v{cl.name}, ${clPath.toFieldExpr()})),
  93. pos: Context.currentPos(),
  94. });
  95. return fields;
  96. }
  97. #end
  98. }