2
0

ShaderNode.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package hrt.shgraph;
  2. import hide.Element;
  3. using hxsl.Ast;
  4. @:autoBuild(hrt.shgraph.ParseFieldsMacro.build())
  5. @:keepSub
  6. class ShaderNode {
  7. static public var current_id : Int = 0; // TODO : check concurrency
  8. public var id : Int = current_id++;
  9. static var availableVariables = [{
  10. parent: null,
  11. id: 0,
  12. kind: Global,
  13. name: "pixelColor",
  14. type: TVec(4, VFloat)
  15. }];
  16. var inputs : Map<String, NodeVar> = [];
  17. var outputs : Map<String, TVar> = [];
  18. public var outputCompiled : Map<String, Bool> = []; // todo: put with outputs variable
  19. public function setId(id : Int) {
  20. this.id = id;
  21. ShaderNode.current_id = id+1;
  22. }
  23. public function setInput(key : String, s : NodeVar) {
  24. if (s == null)
  25. inputs.remove(key);
  26. else
  27. inputs.set(key, s);
  28. }
  29. public function getInput(key : String) : NodeVar {
  30. return inputs.get(key);
  31. }
  32. public function getInputsKey() {
  33. return [for (k in inputs.keys()) k ];
  34. }
  35. public function getInputs() {
  36. return [for (k in inputs.keys()) inputs.get(k) ];
  37. }
  38. function addOutput(key : String, t : Type) {
  39. outputs.set(key, { parent: null,
  40. id: 0,
  41. kind: Local,
  42. name: "output_" + id + "_" + key,
  43. type: t
  44. });
  45. }
  46. function removeOutput(key : String) {
  47. outputs.remove(key);
  48. }
  49. function addOutputTvar(tVar : TVar) {
  50. outputs.set(tVar.name, tVar);
  51. }
  52. public function createOutputs() : Void {}
  53. public function getOutput(key : String) : TVar {
  54. return outputs.get(key);
  55. }
  56. public function getOutputType(key : String) : Type {
  57. var output = getOutput(key);
  58. if (output == null)
  59. return null;
  60. return output.type;
  61. }
  62. public function getOutputTExpr(key : String) : TExpr {
  63. var o = getOutput(key);
  64. return {
  65. e: TVar(o),
  66. p: null,
  67. t: o.type
  68. };
  69. }
  70. public function build(key : String) : TExpr {
  71. throw "Not implemented";
  72. }
  73. public function checkTypeAndCompatibilyInput(key : String, type : ShaderType.SType) : Bool {
  74. var infoKey = getInputInfo(key);
  75. if (infoKey != null && !(ShaderType.checkConversion(type, infoKey))) {
  76. return false;
  77. }
  78. return checkValidityInput(key, type);
  79. }
  80. public function checkValidityInput(key : String, type : ShaderType.SType) : Bool {
  81. return true;
  82. }
  83. public function getInputInfo(key : String) : ShaderType.SType {
  84. return null;
  85. }
  86. public function getOutputInfo(key : String) : ShaderType.SType {
  87. return null;
  88. }
  89. public function loadParameters(params : Dynamic) {
  90. var fields = Reflect.fields(params);
  91. for (f in fields) {
  92. Reflect.setField(this, f, Reflect.field(params, f));
  93. }
  94. }
  95. public function saveParameters() : Dynamic {
  96. var parameters = {};
  97. var fields = std.Type.getInstanceFields(std.Type.getClass(this));
  98. var metas = haxe.rtti.Meta.getFields(std.Type.getClass(this));
  99. for (f in fields) {
  100. var m = Reflect.field(metas, f);
  101. if (m == null) {
  102. continue;
  103. }
  104. if (Reflect.hasField(m, "param")) {
  105. Reflect.setField(parameters, f, Reflect.getProperty(this, f));
  106. }
  107. }
  108. return parameters;
  109. }
  110. #if editor
  111. public function getParametersHTML(width : Float) : Array<Element> {
  112. return [];
  113. }
  114. static public var registeredNodes = new Map<String, Class<ShaderNode>>();
  115. static public function register(key : String, cl : Class<ShaderNode>) : Bool {
  116. registeredNodes.set(key, cl);
  117. return true;
  118. }
  119. #end
  120. }