node_shader.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. type node_shader_t = {
  2. context?: node_shader_context_t;
  3. ins?: string[];
  4. outs?: string[];
  5. frag_out?: string;
  6. consts?: string[];
  7. textures?: string[];
  8. functions?: map_t<string, string>;
  9. vert?: string;
  10. vert_end?: string;
  11. vert_normal?: string;
  12. vert_attribs?: string;
  13. vert_write_normal?: i32;
  14. frag?: string;
  15. frag_end?: string;
  16. frag_normal?: string;
  17. frag_attribs?: string;
  18. frag_write_normal?: i32;
  19. // References
  20. vert_n?: bool;
  21. frag_bposition?: bool;
  22. frag_wposition?: bool;
  23. frag_mposition?: bool;
  24. frag_vposition?: bool;
  25. frag_wvpposition?: bool;
  26. frag_ndcpos?: bool;
  27. frag_wtangent?: bool;
  28. frag_vvec?: bool;
  29. frag_vvec_cam?: bool;
  30. frag_n?: bool;
  31. frag_nattr?: bool;
  32. frag_dotnv?: bool;
  33. };
  34. function node_shader_create(context: node_shader_context_t): node_shader_t {
  35. let raw: node_shader_t = {};
  36. raw.context = context;
  37. raw.ins = [];
  38. raw.outs = [];
  39. raw.frag_out = "float4";
  40. raw.consts = [];
  41. raw.textures = [];
  42. raw.functions = map_create();
  43. raw.vert = "";
  44. raw.vert_end = "";
  45. raw.vert_normal = "";
  46. raw.vert_attribs = "";
  47. raw.vert_write_normal = 0;
  48. raw.frag = "";
  49. raw.frag_end = "";
  50. raw.frag_normal = "";
  51. raw.frag_attribs = "";
  52. raw.frag_write_normal = 0;
  53. return raw;
  54. }
  55. function node_shader_add_in(raw: node_shader_t, s: string) {
  56. array_push(raw.ins, s);
  57. }
  58. function node_shader_add_out(raw: node_shader_t, s: string) {
  59. array_push(raw.outs, s);
  60. }
  61. function node_shader_add_constant(raw: node_shader_t, s: string, link: string = null) {
  62. // inp: float4
  63. if (array_index_of(raw.consts, s) == -1) {
  64. let ar: string[] = string_split(s, ": ");
  65. let uname: string = ar[0];
  66. let utype: string = ar[1];
  67. ////
  68. if (utype == "float2") utype = "vec2";
  69. if (utype == "float3") utype = "vec3";
  70. if (utype == "float4") utype = "vec4";
  71. if (utype == "float3x3") utype = "mat3";
  72. if (utype == "float4x4") utype = "mat4";
  73. ////
  74. array_push(raw.consts, s);
  75. node_shader_context_add_constant(raw.context, utype, uname, link);
  76. }
  77. }
  78. function node_shader_add_texture(raw: node_shader_t, s: string, link: string = null) {
  79. // mytex: tex2d
  80. if (array_index_of(raw.textures, s) == -1) {
  81. let ar: string[] = string_split(s, ": ");
  82. let uname: string = ar[0];
  83. let utype: string = ar[1];
  84. array_push(raw.textures, s);
  85. node_shader_context_add_texture_unit(raw.context, utype, uname, link);
  86. }
  87. }
  88. function node_shader_add_function(raw: node_shader_t, s: string) {
  89. let fname: string = string_split(s, "(")[0];
  90. if (map_get(raw.functions, fname) != null) {
  91. return;
  92. }
  93. map_set(raw.functions, fname, s);
  94. }
  95. function node_shader_write_vert(raw: node_shader_t, s: string) {
  96. if (raw.vert_write_normal > 0) {
  97. raw.vert_normal += s + "\n";
  98. }
  99. else {
  100. raw.vert += s + "\n";
  101. }
  102. }
  103. function node_shader_write_end_vert(raw: node_shader_t, s: string) {
  104. raw.vert_end += s + "\n";
  105. }
  106. function node_shader_write_attrib_vert(raw: node_shader_t, s: string) {
  107. raw.vert_attribs += s + "\n";
  108. }
  109. function node_shader_write_frag(raw: node_shader_t, s: string) {
  110. if (raw.frag_write_normal > 0) {
  111. raw.frag_normal += s + "\n";
  112. }
  113. else {
  114. raw.frag += s + "\n";
  115. }
  116. }
  117. function node_shader_write_attrib_frag(raw: node_shader_t, s: string) {
  118. raw.frag_attribs += s + "\n";
  119. }
  120. function node_shader_data_size(raw: node_shader_t, data: string): string {
  121. if (data == "float1") {
  122. return "1";
  123. }
  124. else if (data == "float2" || data == "short2norm") {
  125. return "2";
  126. }
  127. else if (data == "float3") {
  128. return "3";
  129. }
  130. else { // float4 || short4norm
  131. return "4";
  132. }
  133. }
  134. function node_shader_vstruct_to_vsin(raw: node_shader_t) {
  135. let vs: vertex_element_t[] = raw.context.data.vertex_elements;
  136. for (let i: i32 = 0; i < vs.length; ++i) {
  137. let e: vertex_element_t = vs[i];
  138. node_shader_add_in(raw, "" + e.name + ": " + "float" + node_shader_data_size(raw, e.data));
  139. }
  140. }
  141. function node_shader_get(raw: node_shader_t): string {
  142. node_shader_vstruct_to_vsin(raw);
  143. let s: string = "";
  144. s += "struct vert_in {\n";
  145. for (let i: i32 = 0; i < raw.ins.length; ++i) {
  146. let a: string = raw.ins[i];
  147. s += "\t" + a + ";\n";
  148. }
  149. s += "}\n\n";
  150. s += "struct vert_out {\n";
  151. s += "\tpos: float4;\n";
  152. for (let i: i32 = 0; i < raw.outs.length; ++i) {
  153. let a: string = raw.outs[i];
  154. s += "\t" + a + ";\n";
  155. }
  156. if (raw.consts.length == 0) {
  157. s += "\tempty: float4;\n";
  158. }
  159. s += "}\n\n";
  160. s += "#[set(everything)]\n";
  161. s += "const constants: {\n";
  162. for (let i: i32 = 0; i < raw.consts.length; ++i) {
  163. let a: string = raw.consts[i];
  164. s += "\t" + a + ";\n";
  165. }
  166. if (raw.consts.length == 0) {
  167. s += "\tempty: float4;\n";
  168. }
  169. s += "};\n\n";
  170. if (raw.textures.length > 0) {
  171. s += "#[set(everything)]\n";
  172. s += "const sampler_linear: sampler;\n\n";
  173. }
  174. for (let i: i32 = 0; i < raw.textures.length; ++i) {
  175. let a: string = raw.textures[i];
  176. s += "#[set(everything)]\n";
  177. s += "const " + a + ": tex2d;\n";
  178. }
  179. let keys: string[] = map_keys(raw.functions);
  180. for (let i: i32 = 0; i < keys.length; ++i) {
  181. let f: string = map_get(raw.functions, keys[i]);
  182. s += f + "\n";
  183. }
  184. s += "\n";
  185. s += "fun kong_vert(input: vert_in): vert_out {\n";
  186. s += "\tvar output: vert_out;\n\n";
  187. s += raw.vert_attribs;
  188. s += raw.vert_normal;
  189. s += raw.vert;
  190. s += raw.vert_end;
  191. s += "\toutput.pos.z = (output.pos.z + output.pos.w) * 0.5;\n"; ////
  192. if (raw.consts.length == 0) {
  193. s += "\toutput.empty = constants.empty;\n";
  194. }
  195. s += "\n\treturn output;\n";
  196. s += "}\n\n";
  197. s += "fun kong_frag(input: vert_out): " + raw.frag_out + " {\n";
  198. s += "\tvar output: " + raw.frag_out + ";\n\n";
  199. s += raw.frag_attribs;
  200. s += raw.frag_normal;
  201. s += raw.frag;
  202. s += raw.frag_end;
  203. s += "\n\treturn output;\n";
  204. s += "}\n\n";
  205. s += "#[pipe]\n";
  206. s += "struct pipe {\n";
  207. s += "\tvertex = kong_vert;\n";
  208. s += "\tfragment = kong_frag;\n";
  209. s += "}\n";
  210. return s;
  211. }