ashader.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // kong wrapper
  2. #include "iron_string.h"
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "../../sources/libs/kong/analyzer.h"
  8. #include "../../sources/libs/kong/backends/hlsl.h"
  9. #include "../../sources/libs/kong/backends/metal.h"
  10. #include "../../sources/libs/kong/backends/spirv.h"
  11. #include "../../sources/libs/kong/backends/wgsl.h"
  12. #include "../../sources/libs/kong/compiler.h"
  13. #include "../../sources/libs/kong/disasm.h"
  14. #include "../../sources/libs/kong/errors.h"
  15. #include "../../sources/libs/kong/functions.h"
  16. #include "../../sources/libs/kong/globals.h"
  17. #include "../../sources/libs/kong/log.h"
  18. #include "../../sources/libs/kong/names.h"
  19. #include "../../sources/libs/kong/parser.h"
  20. #include "../../sources/libs/kong/tokenizer.h"
  21. #include "../../sources/libs/kong/transformer.h"
  22. #include "../../sources/libs/kong/typer.h"
  23. #include "../../sources/libs/kong/types.h"
  24. #ifdef _WIN32
  25. #include <D3Dcompiler.h>
  26. #include <d3d11.h>
  27. char *hlsl_to_bin(char *source, char *shader_type, char *to) {
  28. char *type;
  29. if (string_equals(shader_type, "vert")) {
  30. type = "vs_5_0";
  31. }
  32. else {
  33. type = "ps_5_0";
  34. }
  35. ID3DBlob *error_message;
  36. ID3DBlob *shader_buffer;
  37. // UINT flags = D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_SKIP_VALIDATION;
  38. UINT flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
  39. HRESULT hr = D3DCompile(source, strlen(source) + 1, NULL, NULL, NULL, "main", type, flags, 0, &shader_buffer, &error_message);
  40. if (hr != S_OK) {
  41. printf("%s\n", (char *)error_message->lpVtbl->GetBufferPointer(error_message));
  42. return NULL;
  43. }
  44. FILE *fp = fopen(to, "wb");
  45. int len = shader_buffer->lpVtbl->GetBufferSize(shader_buffer);
  46. fwrite((char *)shader_buffer->lpVtbl->GetBufferPointer(shader_buffer), 1, len, fp);
  47. fclose(fp);
  48. shader_buffer->lpVtbl->Release(shader_buffer);
  49. }
  50. #endif
  51. extern uint64_t next_variable_id;
  52. extern size_t allocated_globals_size;
  53. extern function_id next_function_index;
  54. extern global_id globals_size;
  55. extern name_id names_index;
  56. extern size_t sets_count;
  57. extern type_id next_type_index;
  58. void hlsl_export2(char **vs, char **fs, api_kind d3d, bool debug);
  59. void spirv_export2(char **vs, char **fs, int *vs_size, int *fs_size, bool debug);
  60. void wgsl_export2(char **vs, char **fs);
  61. extern size_t vertex_inputs_size;
  62. extern size_t fragment_inputs_size;
  63. extern size_t vertex_functions_size;
  64. extern size_t fragment_functions_size;
  65. void kong_compile(char *shader_lang, const char *from, const char *to) {
  66. FILE *fp = fopen(from, "rb");
  67. fseek(fp, 0, SEEK_END);
  68. int size = ftell(fp);
  69. rewind(fp);
  70. char *data = malloc(size + 1);
  71. data[size] = 0;
  72. fread(data, size, 1, fp);
  73. fclose(fp);
  74. next_variable_id = 1;
  75. allocated_globals_size = 0;
  76. next_function_index = 0;
  77. globals_size = 0;
  78. names_index = 1;
  79. sets_count = 0;
  80. next_type_index = 0;
  81. vertex_inputs_size = 0;
  82. fragment_inputs_size = 0;
  83. vertex_functions_size = 0;
  84. fragment_functions_size = 0;
  85. names_init();
  86. types_init();
  87. functions_init();
  88. globals_init();
  89. tokens tokens = tokenize(from, data);
  90. parse(from, &tokens);
  91. resolve_types();
  92. allocate_globals();
  93. for (function_id i = 0; get_function(i) != NULL; ++i) {
  94. compile_function_block(&get_function(i)->code, get_function(i)->block);
  95. }
  96. analyze();
  97. if (strcmp(shader_lang, "wgsl") == 0) {
  98. char *vs;
  99. char *fs;
  100. wgsl_export2(&vs, &fs);
  101. char to_[512];
  102. strcpy(to_, to);
  103. to_[strlen(to_) - 4] = '\0';
  104. strcat(to_, "vert.wgsl");
  105. FILE *fp = fopen(to_, "wb");
  106. fwrite(vs, 1, strlen(vs), fp);
  107. fclose(fp);
  108. strcpy(to_, to);
  109. to_[strlen(to_) - 4] = '\0';
  110. strcat(to_, "frag.wgsl");
  111. fp = fopen(to_, "wb");
  112. fwrite(fs, 1, strlen(fs), fp);
  113. fclose(fp);
  114. return;
  115. }
  116. #ifdef _WIN32
  117. char *vs;
  118. char *fs;
  119. hlsl_export2(&vs, &fs, API_DIRECT3D11, false);
  120. ////
  121. // int i = string_last_index_of(to, "\\");
  122. // char filename[512];
  123. // strcpy(filename, to);
  124. // char *filebase = &filename[i + 1];
  125. // int j = string_index_of(filebase, ".");
  126. // filebase[j] = '\0';
  127. // char tmp[512];
  128. // strcpy(tmp, to);
  129. // tmp[i] = '\0';
  130. // strcat(tmp, "\\..\\..\\temp\\");
  131. // strcat(tmp, filebase);
  132. // strcat(tmp, ".vert.hlsl");
  133. // fp = fopen(tmp, "wb");
  134. // fwrite(vs, 1, strlen(vs), fp);
  135. // fclose(fp);
  136. // tmp[i] = '\0';
  137. // strcat(tmp, "\\..\\..\\temp\\");
  138. // strcat(tmp, filebase);
  139. // strcat(tmp, ".frag.hlsl");
  140. // fp = fopen(tmp, "wb");
  141. // fwrite(fs, 1, strlen(fs), fp);
  142. // fclose(fp);
  143. ////
  144. char to_[512];
  145. strcpy(to_, to);
  146. to_[strlen(to_) - 4] = '\0';
  147. strcat(to_, "vert.d3d11");
  148. hlsl_to_bin(vs, "vert", to_);
  149. strcpy(to_, to);
  150. to_[strlen(to_) - 4] = '\0';
  151. strcat(to_, "frag.d3d11");
  152. hlsl_to_bin(fs, "frag", to_);
  153. #elif defined(__APPLE__)
  154. char *metal = metal_export("");
  155. int i = string_last_index_of(to, "/");
  156. char filename[512];
  157. strcpy(filename, to);
  158. char *filebase = &filename[i + 1];
  159. int j = string_index_of(filebase, ".");
  160. filebase[j] = '\0';
  161. char to_[512];
  162. strcpy(to_, to);
  163. to_[strlen(to_) - 5] = '\0';
  164. strcat(to_, "vert.metal");
  165. fp = fopen(to_, "wb");
  166. fwrite("//>", 1, 3, fp);
  167. fwrite(filebase, 1, strlen(filebase), fp);
  168. fwrite("_vert\n", 1, 6, fp);
  169. fwrite(metal, 1, strlen(metal), fp);
  170. fclose(fp);
  171. strcpy(to_, to);
  172. to_[strlen(to_) - 5] = '\0';
  173. strcat(to_, "frag.metal");
  174. fp = fopen(to_, "wb");
  175. fwrite("//>", 1, 3, fp);
  176. fwrite(filebase, 1, strlen(filebase), fp);
  177. fwrite("_frag\n", 1, 6, fp);
  178. fclose(fp);
  179. #else
  180. transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE | TRANSFORM_FLAG_BINARY_UNIFY_LENGTH);
  181. char *vs;
  182. char *fs;
  183. int vs_size;
  184. int fs_size;
  185. spirv_export2(&vs, &fs, &vs_size, &fs_size, false);
  186. char to_[512];
  187. strcpy(to_, to);
  188. to_[strlen(to_) - 5] = '\0';
  189. strcat(to_, "vert.spirv");
  190. fp = fopen(to_, "wb");
  191. fwrite(vs, 1, vs_size, fp);
  192. fclose(fp);
  193. strcpy(to_, to);
  194. to_[strlen(to_) - 5] = '\0';
  195. strcat(to_, "frag.spirv");
  196. fp = fopen(to_, "wb");
  197. fwrite(fs, 1, fs_size, fp);
  198. fclose(fp);
  199. #endif
  200. }
  201. int ashader(char *shader_lang, char *from, char *to) {
  202. // shader_lang == hlsl || metal || spirv || wgsl
  203. kong_compile(shader_lang, from, to);
  204. return 0;
  205. }