NodeVar.hx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package hrt.shgraph;
  2. using hxsl.Ast;
  3. class NodeVar {
  4. public var node : ShaderNode;
  5. public var keyOutput : String;
  6. public function new ( n : ShaderNode, key : String ) {
  7. node = n;
  8. keyOutput = key;
  9. }
  10. public function getKey() : String {
  11. return keyOutput;
  12. }
  13. public function getTVar() {
  14. return node.getOutput(keyOutput);
  15. }
  16. public function getType() : Type {
  17. return node.getOutputType(keyOutput);
  18. }
  19. public function isEmpty() {
  20. return node.getOutputTExpr(keyOutput) == null;
  21. }
  22. public function getVar(?type: Type) : TExpr {
  23. var currentType = getType();
  24. if (type == null || currentType == type) {
  25. return node.getOutputTExpr(keyOutput);
  26. }
  27. switch(currentType) {
  28. case TBool:
  29. var tExprBool = node.getOutputTExpr(keyOutput);
  30. switch(type) {
  31. case TVec(size, VBool):
  32. if (size == 2) {
  33. return {
  34. e: TCall({
  35. e: TGlobal(Vec2),
  36. p: null,
  37. t: TFun([
  38. {
  39. ret: type,
  40. args: [
  41. { name: "u", type : TBool },
  42. { name: "v", type : TBool }]
  43. }
  44. ])
  45. }, [tExprBool,
  46. tExprBool]),
  47. p: null,
  48. t: type
  49. };
  50. } else if (size == 3) {
  51. return {
  52. e: TCall({
  53. e: TGlobal(Vec3),
  54. p: null,
  55. t: TFun([
  56. {
  57. ret: type,
  58. args: [
  59. { name: "x", type : TBool },
  60. { name: "y", type : TBool },
  61. { name: "z", type : TBool }]
  62. }
  63. ])
  64. }, [tExprBool,
  65. tExprBool,
  66. tExprBool]),
  67. p: null,
  68. t: type
  69. };
  70. } else {
  71. return {
  72. e: TCall({
  73. e: TGlobal(Vec4),
  74. p: null,
  75. t: TFun([
  76. {
  77. ret: type,
  78. args: [
  79. { name: "r", type : TBool },
  80. { name: "g", type : TBool },
  81. { name: "b", type : TBool },
  82. { name: "a", type : TBool }]
  83. }
  84. ])
  85. }, [tExprBool,
  86. tExprBool,
  87. tExprBool,
  88. tExprBool]),
  89. p: null,
  90. t: type
  91. };
  92. }
  93. default:
  94. };
  95. case TFloat:
  96. var tExprFloat = node.getOutputTExpr(keyOutput);
  97. switch(type) {
  98. case TVec(size, VFloat):
  99. if (size == 2) {
  100. return {
  101. e: TCall({
  102. e: TGlobal(Vec2),
  103. p: null,
  104. t: TFun([
  105. {
  106. ret: type,
  107. args: [
  108. { name: "u", type : TFloat },
  109. { name: "v", type : TFloat }]
  110. }
  111. ])
  112. }, [tExprFloat,
  113. tExprFloat]),
  114. p: null,
  115. t: type
  116. };
  117. } else if (size == 3) {
  118. return {
  119. e: TCall({
  120. e: TGlobal(Vec3),
  121. p: null,
  122. t: TFun([
  123. {
  124. ret: type,
  125. args: [
  126. { name: "x", type : TFloat },
  127. { name: "y", type : TFloat },
  128. { name: "z", type : TFloat }]
  129. }
  130. ])
  131. }, [tExprFloat,
  132. tExprFloat,
  133. tExprFloat]),
  134. p: null,
  135. t: type
  136. };
  137. } else {
  138. return {
  139. e: TCall({
  140. e: TGlobal(Vec4),
  141. p: null,
  142. t: TFun([
  143. {
  144. ret: type,
  145. args: [
  146. { name: "r", type : TFloat },
  147. { name: "g", type : TFloat },
  148. { name: "b", type : TFloat },
  149. { name: "a", type : TFloat }]
  150. }
  151. ])
  152. }, [tExprFloat,
  153. tExprFloat,
  154. tExprFloat,
  155. {
  156. e: TConst(CFloat(1.0)),
  157. p: null,
  158. t: TFloat
  159. }]),
  160. p: null,
  161. t: type
  162. };
  163. }
  164. default:
  165. };
  166. case TVec(sizeCurrentType, VFloat):
  167. var tExprFloat = node.getOutputTExpr(keyOutput);
  168. if (sizeCurrentType == 2) {
  169. switch(type) {
  170. case TVec(size, VFloat):
  171. if (size == 3) {
  172. return {
  173. e: TCall({
  174. e: TGlobal(Vec3),
  175. p: null,
  176. t: TFun([
  177. {
  178. ret: type,
  179. args: [
  180. { name: "x", type : TFloat },
  181. { name: "y", type : TFloat },
  182. { name: "z", type : TFloat }]
  183. }
  184. ])
  185. }, [{
  186. e: TSwiz(tExprFloat, [X]),
  187. p: null,
  188. t: TVec(1, VFloat)
  189. },
  190. {
  191. e: TSwiz(tExprFloat, [Y]),
  192. p: null,
  193. t: TVec(1, VFloat)
  194. },
  195. {
  196. e: TConst(CFloat(0.0)),
  197. p: null,
  198. t: TFloat
  199. }]),
  200. p: null,
  201. t: type
  202. };
  203. } else if (size == 4) {
  204. return {
  205. e: TCall({
  206. e: TGlobal(Vec4),
  207. p: null,
  208. t: TFun([
  209. {
  210. ret: type,
  211. args: [
  212. { name: "r", type : TFloat },
  213. { name: "g", type : TFloat },
  214. { name: "b", type : TFloat },
  215. { name: "a", type : TFloat }]
  216. }
  217. ])
  218. }, [{
  219. e: TSwiz(tExprFloat, [X]),
  220. p: null,
  221. t: TVec(1, VFloat)
  222. },
  223. {
  224. e: TSwiz(tExprFloat, [Y]),
  225. p: null,
  226. t: TVec(1, VFloat)
  227. },
  228. {
  229. e: TConst(CFloat(0.0)),
  230. p: null,
  231. t: TFloat
  232. },
  233. {
  234. e: TConst(CFloat(0.0)),
  235. p: null,
  236. t: TFloat
  237. }]),
  238. p: null,
  239. t: type
  240. };
  241. }
  242. default:
  243. };
  244. } else if (sizeCurrentType == 3) {
  245. switch(type) {
  246. case TVec(size, VFloat):
  247. if (size == 4) {
  248. return {
  249. e: TCall({
  250. e: TGlobal(Vec4),
  251. p: null,
  252. t: TFun([
  253. {
  254. ret: type,
  255. args: [
  256. { name: "r", type : TFloat },
  257. { name: "g", type : TFloat },
  258. { name: "b", type : TFloat },
  259. { name: "a", type : TFloat }]
  260. }
  261. ])
  262. }, [{
  263. e: TSwiz(tExprFloat, [X]),
  264. p: null,
  265. t: TVec(1, VFloat)
  266. },
  267. {
  268. e: TSwiz(tExprFloat, [Y]),
  269. p: null,
  270. t: TVec(1, VFloat)
  271. },
  272. {
  273. e: TSwiz(tExprFloat, [Z]),
  274. p: null,
  275. t: TVec(1, VFloat)
  276. },
  277. {
  278. e: TConst(CFloat(0.0)),
  279. p: null,
  280. t: TFloat
  281. }]),
  282. p: null,
  283. t: type
  284. };
  285. }
  286. default:
  287. };
  288. }
  289. default:
  290. }
  291. return node.getOutputTExpr(keyOutput);
  292. }
  293. public function getExpr() : Array<TExpr> {
  294. if (node.outputCompiled.get(keyOutput) != null)
  295. return [];
  296. node.outputCompiled.set(keyOutput, true);
  297. var res = [];
  298. var nodeBuild = node.build(keyOutput);
  299. var tvar = getTVar();
  300. if (tvar != null && tvar.kind == Local && ShaderInput.availableInputs.indexOf(tvar) < 0)
  301. res.push({ e : TVarDecl(getTVar()), t : getType(), p : null });
  302. if (nodeBuild != null)
  303. res.push(nodeBuild);
  304. return res;
  305. }
  306. }