ashader.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // kong wrapper
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include "iron_string.h"
  7. #include "../../sources/libs/kong/analyzer.h"
  8. #include "../../sources/libs/kong/compiler.h"
  9. #include "../../sources/libs/kong/disasm.h"
  10. #include "../../sources/libs/kong/errors.h"
  11. #include "../../sources/libs/kong/functions.h"
  12. #include "../../sources/libs/kong/globals.h"
  13. #include "../../sources/libs/kong/log.h"
  14. #include "../../sources/libs/kong/names.h"
  15. #include "../../sources/libs/kong/parser.h"
  16. #include "../../sources/libs/kong/tokenizer.h"
  17. #include "../../sources/libs/kong/typer.h"
  18. #include "../../sources/libs/kong/types.h"
  19. #include "../../sources/libs/kong/transformer.h"
  20. #include "../../sources/libs/kong/backends/hlsl.h"
  21. #include "../../sources/libs/kong/backends/metal.h"
  22. #include "../../sources/libs/kong/backends/spirv.h"
  23. #include "../../sources/libs/kong/backends/wgsl.h"
  24. #ifdef _WIN32
  25. #include <d3d11.h>
  26. #include <D3Dcompiler.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. extern size_t vertex_inputs_size;
  61. extern size_t fragment_inputs_size;
  62. extern size_t vertex_functions_size;
  63. extern size_t fragment_functions_size;
  64. void kong_compile(const char *from, const char *to) {
  65. FILE *fp = fopen(from, "rb");
  66. fseek(fp , 0, SEEK_END);
  67. int size = ftell(fp);
  68. rewind(fp);
  69. char *data = malloc(size + 1);
  70. data[size] = 0;
  71. fread(data, size, 1, fp);
  72. fclose(fp);
  73. next_variable_id = 1;
  74. allocated_globals_size = 0;
  75. next_function_index = 0;
  76. globals_size = 0;
  77. names_index = 1;
  78. sets_count = 0;
  79. next_type_index = 0;
  80. vertex_inputs_size = 0;
  81. fragment_inputs_size = 0;
  82. vertex_functions_size = 0;
  83. fragment_functions_size = 0;
  84. names_init();
  85. types_init();
  86. functions_init();
  87. globals_init();
  88. tokens tokens = tokenize(from, data);
  89. parse(from, &tokens);
  90. resolve_types();
  91. allocate_globals();
  92. for (function_id i = 0; get_function(i) != NULL; ++i) {
  93. compile_function_block(&get_function(i)->code, get_function(i)->block);
  94. }
  95. analyze();
  96. #ifdef _WIN32
  97. char *vs;
  98. char *fs;
  99. hlsl_export2(&vs, &fs, API_DIRECT3D11, false);
  100. ////
  101. // int i = string_last_index_of(to, "\\");
  102. // char filename[512];
  103. // strcpy(filename, to);
  104. // char *filebase = &filename[i + 1];
  105. // int j = string_index_of(filebase, ".");
  106. // filebase[j] = '\0';
  107. // char tmp[512];
  108. // strcpy(tmp, to);
  109. // tmp[i] = '\0';
  110. // strcat(tmp, "\\..\\..\\temp\\");
  111. // strcat(tmp, filebase);
  112. // strcat(tmp, ".vert.hlsl");
  113. // fp = fopen(tmp, "wb");
  114. // fwrite(vs, 1, strlen(vs), fp);
  115. // fclose(fp);
  116. // tmp[i] = '\0';
  117. // strcat(tmp, "\\..\\..\\temp\\");
  118. // strcat(tmp, filebase);
  119. // strcat(tmp, ".frag.hlsl");
  120. // fp = fopen(tmp, "wb");
  121. // fwrite(fs, 1, strlen(fs), fp);
  122. // fclose(fp);
  123. ////
  124. char to_[512];
  125. strcpy(to_, to);
  126. to_[strlen(to_) - 4] = '\0';
  127. strcat(to_, "vert.d3d11");
  128. hlsl_to_bin(vs, "vert", to_);
  129. strcpy(to_, to);
  130. to_[strlen(to_) - 4] = '\0';
  131. strcat(to_, "frag.d3d11");
  132. hlsl_to_bin(fs, "frag", to_);
  133. #elif defined(__APPLE__)
  134. char *metal = metal_export("");
  135. int i = string_last_index_of(to, "/");
  136. char filename[512];
  137. strcpy(filename, to);
  138. char *filebase = &filename[i + 1];
  139. int j = string_index_of(filebase, ".");
  140. filebase[j] = '\0';
  141. char to_[512];
  142. strcpy(to_, to);
  143. to_[strlen(to_) - 5] = '\0';
  144. strcat(to_, "vert.metal");
  145. fp = fopen(to_, "wb");
  146. fwrite("//>", 1, 3, fp);
  147. fwrite(filebase, 1, strlen(filebase), fp);
  148. fwrite("_vert\n", 1, 6, fp);
  149. fwrite(metal, 1, strlen(metal), fp);
  150. fclose(fp);
  151. strcpy(to_, to);
  152. to_[strlen(to_) - 5] = '\0';
  153. strcat(to_, "frag.metal");
  154. fp = fopen(to_, "wb");
  155. fwrite("//>", 1, 3, fp);
  156. fwrite(filebase, 1, strlen(filebase), fp);
  157. fwrite("_frag\n", 1, 6, fp);
  158. fclose(fp);
  159. #else
  160. transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE | TRANSFORM_FLAG_BINARY_UNIFY_LENGTH);
  161. char *vs;
  162. char *fs;
  163. int vs_size;
  164. int fs_size;
  165. spirv_export2(&vs, &fs, &vs_size, &fs_size, false);
  166. char to_[512];
  167. strcpy(to_, to);
  168. to_[strlen(to_) - 5] = '\0';
  169. strcat(to_, "vert.spirv");
  170. fp = fopen(to_, "wb");
  171. fwrite(vs, 1, vs_size, fp);
  172. fclose(fp);
  173. strcpy(to_, to);
  174. to_[strlen(to_) - 5] = '\0';
  175. strcat(to_, "frag.spirv");
  176. fp = fopen(to_, "wb");
  177. fwrite(fs, 1, fs_size, fp);
  178. fclose(fp);
  179. #endif
  180. }
  181. int ashader(char *shader_lang, char *from, char *to) {
  182. // shader_lang == hlsl || metal || spirv
  183. kong_compile(from, to);
  184. return 0;
  185. }