plugins.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include <v8.h>
  2. #include <kinc/log.h>
  3. using namespace v8;
  4. static Isolate *isolate;
  5. extern "C" {
  6. void proc_xatlas_setVertexCount(int i);
  7. void proc_xatlas_setIndexCount(int i);
  8. int proc_xatlas_setPositions();
  9. int proc_xatlas_setNormals();
  10. int proc_xatlas_setIndices();
  11. uint8_t *proc_xatlas_getBuffer();
  12. uint32_t proc_xatlas_getBufferLength();
  13. int proc_xatlas_getVertexCount();
  14. int proc_xatlas_getIndexCount();
  15. int proc_xatlas_getPositions();
  16. int proc_xatlas_getNormals();
  17. int proc_xatlas_getUVs();
  18. int proc_xatlas_getIndices();
  19. void proc_xatlas_unwrap();
  20. void proc_xatlas_destroy();
  21. uint8_t *io_svg_getBuffer();
  22. uint32_t io_svg_getBufferLength();
  23. int io_svg_init(int i);
  24. void io_svg_parse();
  25. int io_svg_get_pixels_w();
  26. int io_svg_get_pixels_h();
  27. int io_svg_get_pixels();
  28. void io_svg_destroy();
  29. uint8_t *io_gltf_getBuffer();
  30. uint32_t io_gltf_getBufferLength();
  31. int io_gltf_init(int i);
  32. void io_gltf_parse();
  33. int io_gltf_get_vertex_count();
  34. int io_gltf_get_index_count();
  35. int io_gltf_get_indices();
  36. int io_gltf_get_positions();
  37. int io_gltf_get_normals();
  38. int io_gltf_get_uvs();
  39. float io_gltf_get_scale_pos();
  40. void io_gltf_destroy();
  41. uint8_t *io_usd_getBuffer();
  42. uint32_t io_usd_getBufferLength();
  43. int io_usd_init(int i);
  44. void io_usd_parse();
  45. int io_usd_get_vertex_count();
  46. int io_usd_get_index_count();
  47. int io_usd_get_indices();
  48. int io_usd_get_positions();
  49. int io_usd_get_normals();
  50. int io_usd_get_uvs();
  51. float io_usd_get_scale_pos();
  52. void io_usd_destroy();
  53. }
  54. namespace {
  55. void krom_uv_unwrap_buffer(const FunctionCallbackInfo<Value> &args) {
  56. HandleScope scope(args.GetIsolate());
  57. std::unique_ptr<v8::BackingStore> backing = v8::ArrayBuffer::NewBackingStore(proc_xatlas_getBuffer(), proc_xatlas_getBufferLength(), [](void*, size_t, void*) {}, nullptr);
  58. Local<ArrayBuffer> buffer = ArrayBuffer::New(isolate, std::move(backing));
  59. args.GetReturnValue().Set(buffer);
  60. }
  61. void krom_uv_unwrap_setVertexCount(const FunctionCallbackInfo<Value> &args) {
  62. HandleScope scope(args.GetIsolate());
  63. int i = args[0]->ToInt32(isolate->GetCurrentContext()).ToLocalChecked()->Value();
  64. proc_xatlas_setVertexCount(i);
  65. }
  66. void krom_uv_unwrap_setIndexCount(const FunctionCallbackInfo<Value> &args) {
  67. HandleScope scope(args.GetIsolate());
  68. int i = args[0]->ToInt32(isolate->GetCurrentContext()).ToLocalChecked()->Value();
  69. proc_xatlas_setIndexCount(i);
  70. }
  71. void krom_uv_unwrap_setPositions(const FunctionCallbackInfo<Value> &args) {
  72. HandleScope scope(args.GetIsolate());
  73. int i = proc_xatlas_setPositions();
  74. args.GetReturnValue().Set(Int32::New(isolate, i));
  75. }
  76. void krom_uv_unwrap_setNormals(const FunctionCallbackInfo<Value> &args) {
  77. HandleScope scope(args.GetIsolate());
  78. int i = proc_xatlas_setNormals();
  79. args.GetReturnValue().Set(Int32::New(isolate, i));
  80. }
  81. void krom_uv_unwrap_setIndices(const FunctionCallbackInfo<Value> &args) {
  82. HandleScope scope(args.GetIsolate());
  83. int i = proc_xatlas_setIndices();
  84. args.GetReturnValue().Set(Int32::New(isolate, i));
  85. }
  86. void krom_uv_unwrap_getVertexCount(const FunctionCallbackInfo<Value> &args) {
  87. HandleScope scope(args.GetIsolate());
  88. int i = proc_xatlas_getVertexCount();
  89. args.GetReturnValue().Set(Int32::New(isolate, i));
  90. }
  91. void krom_uv_unwrap_getIndexCount(const FunctionCallbackInfo<Value> &args) {
  92. HandleScope scope(args.GetIsolate());
  93. int i = proc_xatlas_getIndexCount();
  94. args.GetReturnValue().Set(Int32::New(isolate, i));
  95. }
  96. void krom_uv_unwrap_getPositions(const FunctionCallbackInfo<Value> &args) {
  97. HandleScope scope(args.GetIsolate());
  98. int i = proc_xatlas_getPositions();
  99. args.GetReturnValue().Set(Int32::New(isolate, i));
  100. }
  101. void krom_uv_unwrap_getNormals(const FunctionCallbackInfo<Value> &args) {
  102. HandleScope scope(args.GetIsolate());
  103. int i = proc_xatlas_getNormals();
  104. args.GetReturnValue().Set(Int32::New(isolate, i));
  105. }
  106. void krom_uv_unwrap_getUVs(const FunctionCallbackInfo<Value> &args) {
  107. HandleScope scope(args.GetIsolate());
  108. int i = proc_xatlas_getUVs();
  109. args.GetReturnValue().Set(Int32::New(isolate, i));
  110. }
  111. void krom_uv_unwrap_getIndices(const FunctionCallbackInfo<Value> &args) {
  112. HandleScope scope(args.GetIsolate());
  113. int i = proc_xatlas_getIndices();
  114. args.GetReturnValue().Set(Int32::New(isolate, i));
  115. }
  116. void krom_uv_unwrap_unwrap(const FunctionCallbackInfo<Value> &args) {
  117. HandleScope scope(args.GetIsolate());
  118. proc_xatlas_unwrap();
  119. }
  120. void krom_uv_unwrap_destroy(const FunctionCallbackInfo<Value> &args) {
  121. HandleScope scope(args.GetIsolate());
  122. proc_xatlas_destroy();
  123. }
  124. void krom_import_svg_buffer(const FunctionCallbackInfo<Value> &args) {
  125. HandleScope scope(args.GetIsolate());
  126. std::unique_ptr<v8::BackingStore> backing = v8::ArrayBuffer::NewBackingStore(io_svg_getBuffer(), io_svg_getBufferLength(), [](void*, size_t, void*) {}, nullptr);
  127. Local<ArrayBuffer> buffer = ArrayBuffer::New(isolate, std::move(backing));
  128. args.GetReturnValue().Set(buffer);
  129. }
  130. void krom_import_svg_init(const FunctionCallbackInfo<Value> &args) {
  131. HandleScope scope(args.GetIsolate());
  132. int i = args[0]->ToInt32(isolate->GetCurrentContext()).ToLocalChecked()->Value();
  133. int j = io_svg_init(i);
  134. args.GetReturnValue().Set(Int32::New(isolate, j));
  135. }
  136. void krom_import_svg_parse(const FunctionCallbackInfo<Value> &args) {
  137. HandleScope scope(args.GetIsolate());
  138. io_svg_parse();
  139. }
  140. void krom_import_svg_get_pixels_w(const FunctionCallbackInfo<Value> &args) {
  141. HandleScope scope(args.GetIsolate());
  142. int i = io_svg_get_pixels_w();
  143. args.GetReturnValue().Set(Int32::New(isolate, i));
  144. }
  145. void krom_import_svg_get_pixels_h(const FunctionCallbackInfo<Value> &args) {
  146. HandleScope scope(args.GetIsolate());
  147. int i = io_svg_get_pixels_h();
  148. args.GetReturnValue().Set(Int32::New(isolate, i));
  149. }
  150. void krom_import_svg_get_pixels(const FunctionCallbackInfo<Value> &args) {
  151. HandleScope scope(args.GetIsolate());
  152. int i = io_svg_get_pixels();
  153. args.GetReturnValue().Set(Int32::New(isolate, i));
  154. }
  155. void krom_import_svg_destroy(const FunctionCallbackInfo<Value> &args) {
  156. HandleScope scope(args.GetIsolate());
  157. io_svg_destroy();
  158. }
  159. void krom_import_gltf_buffer(const FunctionCallbackInfo<Value> &args) {
  160. HandleScope scope(args.GetIsolate());
  161. std::unique_ptr<v8::BackingStore> backing = v8::ArrayBuffer::NewBackingStore(io_gltf_getBuffer(), io_gltf_getBufferLength(), [](void*, size_t, void*) {}, nullptr);
  162. Local<ArrayBuffer> buffer = ArrayBuffer::New(isolate, std::move(backing));
  163. args.GetReturnValue().Set(buffer);
  164. }
  165. void krom_import_gltf_init(const FunctionCallbackInfo<Value> &args) {
  166. HandleScope scope(args.GetIsolate());
  167. int i = args[0]->ToInt32(isolate->GetCurrentContext()).ToLocalChecked()->Value();
  168. int j = io_gltf_init(i);
  169. args.GetReturnValue().Set(Int32::New(isolate, j));
  170. }
  171. void krom_import_gltf_parse(const FunctionCallbackInfo<Value> &args) {
  172. HandleScope scope(args.GetIsolate());
  173. io_gltf_parse();
  174. }
  175. void krom_import_gltf_get_vertex_count(const FunctionCallbackInfo<Value> &args) {
  176. HandleScope scope(args.GetIsolate());
  177. int i = io_gltf_get_vertex_count();
  178. args.GetReturnValue().Set(Int32::New(isolate, i));
  179. }
  180. void krom_import_gltf_get_index_count(const FunctionCallbackInfo<Value> &args) {
  181. HandleScope scope(args.GetIsolate());
  182. int i = io_gltf_get_index_count();
  183. args.GetReturnValue().Set(Int32::New(isolate, i));
  184. }
  185. void krom_import_gltf_get_indices(const FunctionCallbackInfo<Value> &args) {
  186. HandleScope scope(args.GetIsolate());
  187. int i = io_gltf_get_indices();
  188. args.GetReturnValue().Set(Int32::New(isolate, i));
  189. }
  190. void krom_import_gltf_get_positions(const FunctionCallbackInfo<Value> &args) {
  191. HandleScope scope(args.GetIsolate());
  192. int i = io_gltf_get_positions();
  193. args.GetReturnValue().Set(Int32::New(isolate, i));
  194. }
  195. void krom_import_gltf_get_normals(const FunctionCallbackInfo<Value> &args) {
  196. HandleScope scope(args.GetIsolate());
  197. int i = io_gltf_get_normals();
  198. args.GetReturnValue().Set(Int32::New(isolate, i));
  199. }
  200. void krom_import_gltf_get_uvs(const FunctionCallbackInfo<Value> &args) {
  201. HandleScope scope(args.GetIsolate());
  202. int i = io_gltf_get_uvs();
  203. args.GetReturnValue().Set(Int32::New(isolate, i));
  204. }
  205. void krom_import_gltf_get_scale_pos(const FunctionCallbackInfo<Value> &args) {
  206. HandleScope scope(args.GetIsolate());
  207. float f = io_gltf_get_scale_pos();
  208. args.GetReturnValue().Set(Number::New(isolate, f));
  209. }
  210. void krom_import_gltf_destroy(const FunctionCallbackInfo<Value> &args) {
  211. HandleScope scope(args.GetIsolate());
  212. io_gltf_destroy();
  213. }
  214. void krom_import_usdc_buffer(const FunctionCallbackInfo<Value> &args) {
  215. HandleScope scope(args.GetIsolate());
  216. std::unique_ptr<v8::BackingStore> backing = v8::ArrayBuffer::NewBackingStore(io_usd_getBuffer(), io_usd_getBufferLength(), [](void*, size_t, void*) {}, nullptr);
  217. Local<ArrayBuffer> buffer = ArrayBuffer::New(isolate, std::move(backing));
  218. args.GetReturnValue().Set(buffer);
  219. }
  220. void krom_import_usdc_init(const FunctionCallbackInfo<Value> &args) {
  221. HandleScope scope(args.GetIsolate());
  222. int i = args[0]->ToInt32(isolate->GetCurrentContext()).ToLocalChecked()->Value();
  223. int j = io_usd_init(i);
  224. args.GetReturnValue().Set(Int32::New(isolate, j));
  225. }
  226. void krom_import_usdc_parse(const FunctionCallbackInfo<Value> &args) {
  227. HandleScope scope(args.GetIsolate());
  228. io_usd_parse();
  229. }
  230. void krom_import_usdc_get_vertex_count(const FunctionCallbackInfo<Value> &args) {
  231. HandleScope scope(args.GetIsolate());
  232. int i = io_usd_get_vertex_count();
  233. args.GetReturnValue().Set(Int32::New(isolate, i));
  234. }
  235. void krom_import_usdc_get_index_count(const FunctionCallbackInfo<Value> &args) {
  236. HandleScope scope(args.GetIsolate());
  237. int i = io_usd_get_index_count();
  238. args.GetReturnValue().Set(Int32::New(isolate, i));
  239. }
  240. void krom_import_usdc_get_indices(const FunctionCallbackInfo<Value> &args) {
  241. HandleScope scope(args.GetIsolate());
  242. int i = io_usd_get_indices();
  243. args.GetReturnValue().Set(Int32::New(isolate, i));
  244. }
  245. void krom_import_usdc_get_positions(const FunctionCallbackInfo<Value> &args) {
  246. HandleScope scope(args.GetIsolate());
  247. int i = io_usd_get_positions();
  248. args.GetReturnValue().Set(Int32::New(isolate, i));
  249. }
  250. void krom_import_usdc_get_normals(const FunctionCallbackInfo<Value> &args) {
  251. HandleScope scope(args.GetIsolate());
  252. int i = io_usd_get_normals();
  253. args.GetReturnValue().Set(Int32::New(isolate, i));
  254. }
  255. void krom_import_usdc_get_uvs(const FunctionCallbackInfo<Value> &args) {
  256. HandleScope scope(args.GetIsolate());
  257. int i = io_usd_get_uvs();
  258. args.GetReturnValue().Set(Int32::New(isolate, i));
  259. }
  260. void krom_import_usdc_get_scale_pos(const FunctionCallbackInfo<Value> &args) {
  261. HandleScope scope(args.GetIsolate());
  262. float f = io_usd_get_scale_pos();
  263. args.GetReturnValue().Set(Number::New(isolate, f));
  264. }
  265. void krom_import_usdc_destroy(const FunctionCallbackInfo<Value> &args) {
  266. HandleScope scope(args.GetIsolate());
  267. io_usd_destroy();
  268. }
  269. }
  270. #define SET_FUNCTION(object, name, fn)\
  271. object->Set(String::NewFromUtf8(isolate, name).ToLocalChecked(),\
  272. FunctionTemplate::New(isolate, fn, Local<v8::Value>(), Local<v8::Signature>(), 0,\
  273. v8::ConstructorBehavior::kThrow, v8::SideEffectType::kHasNoSideEffect, nullptr))
  274. void plugin_embed(Isolate *_isolate, Local<ObjectTemplate> global) {
  275. isolate = _isolate;
  276. Isolate::Scope isolate_scope(isolate);
  277. HandleScope handle_scope(isolate);
  278. Local<ObjectTemplate> krom_uv_unwrap = ObjectTemplate::New(isolate);
  279. SET_FUNCTION(krom_uv_unwrap, "_buffer", krom_uv_unwrap_buffer);
  280. SET_FUNCTION(krom_uv_unwrap, "_setVertexCount", krom_uv_unwrap_setVertexCount);
  281. SET_FUNCTION(krom_uv_unwrap, "_setIndexCount", krom_uv_unwrap_setIndexCount);
  282. SET_FUNCTION(krom_uv_unwrap, "_setPositions", krom_uv_unwrap_setPositions);
  283. SET_FUNCTION(krom_uv_unwrap, "_setNormals", krom_uv_unwrap_setNormals);
  284. SET_FUNCTION(krom_uv_unwrap, "_setIndices", krom_uv_unwrap_setIndices);
  285. SET_FUNCTION(krom_uv_unwrap, "_getVertexCount", krom_uv_unwrap_getVertexCount);
  286. SET_FUNCTION(krom_uv_unwrap, "_getIndexCount", krom_uv_unwrap_getIndexCount);
  287. SET_FUNCTION(krom_uv_unwrap, "_getPositions", krom_uv_unwrap_getPositions);
  288. SET_FUNCTION(krom_uv_unwrap, "_getNormals", krom_uv_unwrap_getNormals);
  289. SET_FUNCTION(krom_uv_unwrap, "_getUVs", krom_uv_unwrap_getUVs);
  290. SET_FUNCTION(krom_uv_unwrap, "_getIndices", krom_uv_unwrap_getIndices);
  291. SET_FUNCTION(krom_uv_unwrap, "_unwrap", krom_uv_unwrap_unwrap);
  292. SET_FUNCTION(krom_uv_unwrap, "_destroy", krom_uv_unwrap_destroy);
  293. global->Set(String::NewFromUtf8(isolate, "Krom_uv_unwrap").ToLocalChecked(), krom_uv_unwrap);
  294. Local<ObjectTemplate> krom_import_svg = ObjectTemplate::New(isolate);
  295. SET_FUNCTION(krom_import_svg, "_buffer", krom_import_svg_buffer);
  296. SET_FUNCTION(krom_import_svg, "_init", krom_import_svg_init);
  297. SET_FUNCTION(krom_import_svg, "_parse", krom_import_svg_parse);
  298. SET_FUNCTION(krom_import_svg, "_get_pixels_w", krom_import_svg_get_pixels_w);
  299. SET_FUNCTION(krom_import_svg, "_get_pixels_h", krom_import_svg_get_pixels_h);
  300. SET_FUNCTION(krom_import_svg, "_get_pixels", krom_import_svg_get_pixels);
  301. SET_FUNCTION(krom_import_svg, "_destroy", krom_import_svg_destroy);
  302. global->Set(String::NewFromUtf8(isolate, "Krom_import_svg").ToLocalChecked(), krom_import_svg);
  303. Local<ObjectTemplate> krom_import_gltf = ObjectTemplate::New(isolate);
  304. SET_FUNCTION(krom_import_gltf, "_buffer", krom_import_gltf_buffer);
  305. SET_FUNCTION(krom_import_gltf, "_init", krom_import_gltf_init);
  306. SET_FUNCTION(krom_import_gltf, "_parse", krom_import_gltf_parse);
  307. SET_FUNCTION(krom_import_gltf, "_get_vertex_count", krom_import_gltf_get_vertex_count);
  308. SET_FUNCTION(krom_import_gltf, "_get_index_count", krom_import_gltf_get_index_count);
  309. SET_FUNCTION(krom_import_gltf, "_get_indices", krom_import_gltf_get_indices);
  310. SET_FUNCTION(krom_import_gltf, "_get_positions", krom_import_gltf_get_positions);
  311. SET_FUNCTION(krom_import_gltf, "_get_normals", krom_import_gltf_get_normals);
  312. SET_FUNCTION(krom_import_gltf, "_get_uvs", krom_import_gltf_get_uvs);
  313. SET_FUNCTION(krom_import_gltf, "_get_scale_pos", krom_import_gltf_get_scale_pos);
  314. SET_FUNCTION(krom_import_gltf, "_destroy", krom_import_gltf_destroy);
  315. global->Set(String::NewFromUtf8(isolate, "Krom_import_gltf").ToLocalChecked(), krom_import_gltf);
  316. Local<ObjectTemplate> krom_import_usdc = ObjectTemplate::New(isolate);
  317. SET_FUNCTION(krom_import_usdc, "_buffer", krom_import_usdc_buffer);
  318. SET_FUNCTION(krom_import_usdc, "_init", krom_import_usdc_init);
  319. SET_FUNCTION(krom_import_usdc, "_parse", krom_import_usdc_parse);
  320. SET_FUNCTION(krom_import_usdc, "_get_vertex_count", krom_import_usdc_get_vertex_count);
  321. SET_FUNCTION(krom_import_usdc, "_get_index_count", krom_import_usdc_get_index_count);
  322. SET_FUNCTION(krom_import_usdc, "_get_indices", krom_import_usdc_get_indices);
  323. SET_FUNCTION(krom_import_usdc, "_get_positions", krom_import_usdc_get_positions);
  324. SET_FUNCTION(krom_import_usdc, "_get_normals", krom_import_usdc_get_normals);
  325. SET_FUNCTION(krom_import_usdc, "_get_uvs", krom_import_usdc_get_uvs);
  326. SET_FUNCTION(krom_import_usdc, "_get_scale_pos", krom_import_usdc_get_scale_pos);
  327. SET_FUNCTION(krom_import_usdc, "_destroy", krom_import_usdc_destroy);
  328. global->Set(String::NewFromUtf8(isolate, "Krom_import_usdc").ToLocalChecked(), krom_import_usdc);
  329. }