runtime.mjs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. class WasmMemoryInterface {
  2. constructor() {
  3. this.memory = null;
  4. this.exports = null;
  5. }
  6. setMemory(memory) {
  7. this.memory = memory;
  8. }
  9. setExports(exports) {
  10. this.exports = exports;
  11. this.listenerMap = {};
  12. }
  13. get mem() {
  14. return new DataView(this.memory.buffer);
  15. }
  16. loadF32Array(addr, len) {
  17. let array = new Float32Array(this.memory.buffer, addr, len);
  18. return array;
  19. }
  20. loadF64Array(addr, len) {
  21. let array = new Float64Array(this.memory.buffer, addr, len);
  22. return array;
  23. }
  24. loadU32Array(addr, len) {
  25. let array = new Uint32Array(this.memory.buffer, addr, len);
  26. return array;
  27. }
  28. loadI32Array(addr, len) {
  29. let array = new Int32Array(this.memory.buffer, addr, len);
  30. return array;
  31. }
  32. loadU8(addr) { return this.mem.getUint8 (addr, true); }
  33. loadI8(addr) { return this.mem.getInt8 (addr, true); }
  34. loadU16(addr) { return this.mem.getUint16 (addr, true); }
  35. loadI16(addr) { return this.mem.getInt16 (addr, true); }
  36. loadU32(addr) { return this.mem.getUint32 (addr, true); }
  37. loadI32(addr) { return this.mem.getInt32 (addr, true); }
  38. loadU64(addr) {
  39. const lo = this.mem.getUint32(addr + 0, true);
  40. const hi = this.mem.getUint32(addr + 4, true);
  41. return lo + hi*4294967296;
  42. };
  43. loadI64(addr) {
  44. // TODO(bill): loadI64 correctly
  45. const lo = this.mem.getUint32(addr + 0, true);
  46. const hi = this.mem.getUint32(addr + 4, true);
  47. return lo + hi*4294967296;
  48. };
  49. loadF32(addr) { return this.mem.getFloat32(addr, true); }
  50. loadF64(addr) { return this.mem.getFloat64(addr, true); }
  51. loadInt(addr) { return this.mem.getInt32 (addr, true); }
  52. loadUint(addr) { return this.mem.getUint32 (addr, true); }
  53. loadPtr(addr) { return this.loadUint(addr); }
  54. loadBytes(ptr, len) {
  55. return new Uint8Array(this.memory.buffer, ptr, len);
  56. }
  57. loadString(ptr, len) {
  58. const bytes = this.loadBytes(ptr, len);
  59. return new TextDecoder("utf-8").decode(bytes);
  60. }
  61. storeU8(addr, value) { this.mem.setUint8 (addr, value, true); }
  62. storeI8(addr, value) { this.mem.setInt8 (addr, value, true); }
  63. storeU16(addr, value) { this.mem.setUint16 (addr, value, true); }
  64. storeI16(addr, value) { this.mem.setInt16 (addr, value, true); }
  65. storeU32(addr, value) { this.mem.setUint32 (addr, value, true); }
  66. storeI32(addr, value) { this.mem.setInt32 (addr, value, true); }
  67. storeU64(addr, value) {
  68. this.mem.setUint32(addr + 0, value, true);
  69. this.mem.setUint32(addr + 4, Math.floor(value / 4294967296), true);
  70. }
  71. storeI64(addr, value) {
  72. // TODO(bill): storeI64 correctly
  73. this.mem.setUint32(addr + 0, value, true);
  74. this.mem.setUint32(addr + 4, Math.floor(value / 4294967296), true);
  75. }
  76. storeF32(addr, value) { this.mem.setFloat32(addr, value, true); }
  77. storeF64(addr, value) { this.mem.setFloat64(addr, value, true); }
  78. storeInt(addr, value) { this.mem.setInt32 (addr, value, true); }
  79. storeUint(addr, value) { this.mem.setUint32 (addr, value, true); }
  80. };
  81. function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
  82. const MAX_INFO_CONSOLE_LINES = 512;
  83. let infoConsoleLines = new Array();
  84. const addConsoleLine = (line) => {
  85. if (!line) {
  86. return;
  87. }
  88. if (line.endsWith("\n")) {
  89. line = line.substring(0, line.length-1);
  90. } else if (infoConsoleLines.length > 0) {
  91. let prev_line = infoConsoleLines.pop();
  92. line = prev_line.concat(line);
  93. }
  94. infoConsoleLines.push(line);
  95. if (infoConsoleLines.length > MAX_INFO_CONSOLE_LINES) {
  96. infoConsoleLines.shift();
  97. }
  98. let data = "";
  99. for (let i = 0; i < infoConsoleLines.length; i++) {
  100. if (i != 0) {
  101. data = data.concat("\n");
  102. }
  103. data = data.concat(infoConsoleLines[i]);
  104. }
  105. if (consoleElement) {
  106. let info = consoleElement;
  107. info.innerHTML = data;
  108. info.scrollTop = info.scrollHeight;
  109. }
  110. };
  111. let event_temp_data = {};
  112. return {
  113. "env": {},
  114. "odin_env": {
  115. write: (fd, ptr, len) => {
  116. const str = wasmMemoryInterface.loadString(ptr, len);
  117. if (fd == 1) {
  118. addConsoleLine(str);
  119. return;
  120. } else if (fd == 2) {
  121. addConsoleLine(str);
  122. return;
  123. } else {
  124. throw new Error("Invalid fd to 'write'" + stripNewline(str));
  125. }
  126. },
  127. trap: () => { throw new Error() },
  128. alert: (ptr, len) => { alert(wasmMemoryInterface.loadString(ptr, len)) },
  129. abort: () => { Module.abort() },
  130. evaluate: (str_ptr, str_len) => { eval.call(null, wasmMemoryInterface.loadString(str_ptr, str_len)); },
  131. time_now: () => {
  132. return performance.now() * 1e6;
  133. },
  134. sqrt: (x) => Math.sqrt(x),
  135. sin: (x) => Math.sin(x),
  136. cos: (x) => Math.cos(x),
  137. pow: (x) => Math.pow(x),
  138. fmuladd: (x, y, z) => x*y + z,
  139. ln: (x) => Math.log(x),
  140. exp: (x) => Math.exp(x),
  141. ldexp: (x) => Math.ldexp(x),
  142. },
  143. "odin_dom": {
  144. init_event_raw: (ep) => {
  145. const W = 4;
  146. let offset = ep;
  147. let off = (amount, alignment) => {
  148. if (alignment === undefined) {
  149. alignment = Math.min(amount, W);
  150. }
  151. if (offset % alignment != 0) {
  152. offset += alignment - (offset%alignment);
  153. }
  154. let x = offset;
  155. offset += amount;
  156. return x;
  157. };
  158. let wmi = wasmMemoryInterface;
  159. let e = event_temp_data.event;
  160. wmi.storeU32(off(4), event_temp_data.name_code);
  161. if (e.target == document) {
  162. wmi.storeU32(off(4), 1);
  163. } else if (e.target == window) {
  164. wmi.storeU32(off(4), 2);
  165. } else {
  166. wmi.storeU32(off(4), 0);
  167. }
  168. if (e.currentTarget == document) {
  169. wmi.storeU32(off(4), 1);
  170. } else if (e.currentTarget == window) {
  171. wmi.storeU32(off(4), 2);
  172. } else {
  173. wmi.storeU32(off(4), 0);
  174. }
  175. wmi.storeUint(off(W), event_temp_data.id_ptr);
  176. wmi.storeUint(off(W), event_temp_data.id_len);
  177. wmi.storeF64(off(8), e.timeStamp*1e-3);
  178. wmi.storeU8(off(1), e.eventPhase);
  179. wmi.storeU8(off(1), !!e.bubbles);
  180. wmi.storeU8(off(1), !!e.cancelable);
  181. wmi.storeU8(off(1), !!e.composed);
  182. wmi.storeU8(off(1), !!e.isComposing);
  183. wmi.storeU8(off(1), !!e.isTrusted);
  184. let base = off(0, 8);
  185. if (e instanceof MouseEvent) {
  186. wmi.storeI64(off(8), e.screenX);
  187. wmi.storeI64(off(8), e.screenY);
  188. wmi.storeI64(off(8), e.clientX);
  189. wmi.storeI64(off(8), e.clientY);
  190. wmi.storeI64(off(8), e.offsetX);
  191. wmi.storeI64(off(8), e.offsetY);
  192. wmi.storeI64(off(8), e.pageX);
  193. wmi.storeI64(off(8), e.pageY);
  194. wmi.storeI64(off(8), e.movementX);
  195. wmi.storeI64(off(8), e.movementY);
  196. wmi.storeU8(off(1), !!e.ctrlKey);
  197. wmi.storeU8(off(1), !!e.shiftKey);
  198. wmi.storeU8(off(1), !!e.altKey);
  199. wmi.storeU8(off(1), !!e.metaKey);
  200. wmi.storeI16(off(2), e.button);
  201. wmi.storeU16(off(2), e.buttons);
  202. } else if (e instanceof KeyboardEvent) {
  203. let keyOffset = off(W*2, W);
  204. let codeOffet = off(W*2, W);
  205. wmi.storeU8(off(1), e.location);
  206. wmi.storeU8(off(1), !!e.ctrlKey);
  207. wmi.storeU8(off(1), !!e.shiftKey);
  208. wmi.storeU8(off(1), !!e.altKey);
  209. wmi.storeU8(off(1), !!e.metaKey);
  210. wmi.storeU8(off(1), !!e.repeat);
  211. } else if (e instanceof WheelEvent) {
  212. wmi.storeF64(off(8), e.deltaX);
  213. wmi.storeF64(off(8), e.deltaY);
  214. wmi.storeF64(off(8), e.deltaZ);
  215. wmi.storeU32(off(4), e.deltaMode);
  216. } else if (e instanceof Event) {
  217. if ('scrollX' in e) {
  218. wmi.storeF64(off(8), e.scrollX);
  219. wmi.storeF64(off(8), e.scrollY);
  220. }
  221. }
  222. },
  223. add_event_listener: (id_ptr, id_len, name_ptr, name_len, name_code, data, callback, use_capture) => {
  224. let id = wasmMemoryInterface.loadString(id_ptr, id_len);
  225. let name = wasmMemoryInterface.loadString(name_ptr, name_len);
  226. let element = document.getElementById(id);
  227. if (element == undefined) {
  228. return false;
  229. }
  230. let listener = (e) => {
  231. const odin_ctx = wasmMemoryInterface.exports.default_context_ptr();
  232. event_temp_data.id_ptr = id_ptr;
  233. event_temp_data.id_len = id_len;
  234. event_temp_data.event = e;
  235. event_temp_data.name_code = name_code;
  236. // console.log(e);
  237. wasmMemoryInterface.exports.odin_dom_do_event_callback(data, callback, odin_ctx);
  238. };
  239. wasmMemoryInterface.listenerMap[{data: data, callback: callback}] = listener;
  240. element.addEventListener(name, listener, !!use_capture);
  241. return true;
  242. },
  243. remove_event_listener: (id_ptr, id_len, name_ptr, name_len, data, callback) => {
  244. let id = wasmMemoryInterface.loadString(id_ptr, id_len);
  245. let name = wasmMemoryInterface.loadString(name_ptr, name_len);
  246. let element = document.getElementById(id);
  247. if (element == undefined) {
  248. return false;
  249. }
  250. let listener = wasmMemoryInterface.listenerMap[{data: data, callback: callback}];
  251. if (listener == undefined) {
  252. return false;
  253. }
  254. element.removeEventListener(name, listener);
  255. return true;
  256. },
  257. add_window_event_listener: (name_ptr, name_len, name_code, data, callback, use_capture) => {
  258. let name = wasmMemoryInterface.loadString(name_ptr, name_len);
  259. let element = window;
  260. let listener = (e) => {
  261. const odin_ctx = wasmMemoryInterface.exports.default_context_ptr();
  262. event_temp_data.id_ptr = 0;
  263. event_temp_data.id_len = 0;
  264. event_temp_data.event = e;
  265. event_temp_data.name_code = name_code;
  266. // console.log(e);
  267. wasmMemoryInterface.exports.odin_dom_do_event_callback(data, callback, odin_ctx);
  268. };
  269. wasmMemoryInterface.listenerMap[{data: data, callback: callback}] = listener;
  270. element.addEventListener(name, listener, !!use_capture);
  271. return true;
  272. },
  273. remove_window_event_listener: (name_ptr, name_len, data, callback) => {
  274. let name = wasmMemoryInterface.loadString(name_ptr, name_len);
  275. let element = window;
  276. let listener = wasmMemoryInterface.listenerMap[{data: data, callback: callback}];
  277. if (listener == undefined) {
  278. return false;
  279. }
  280. element.removeEventListener(name, listener);
  281. return true;
  282. },
  283. event_stop_propagation: () => {
  284. if (event_temp_data && event_temp_data.event) {
  285. event_temp_data.event.eventStopPropagation();
  286. }
  287. },
  288. event_stop_immediate_propagation: () => {
  289. if (event_temp_data && event_temp_data.event) {
  290. event_temp_data.event.eventStopImmediatePropagation();
  291. }
  292. },
  293. event_prevent_default: () => {
  294. if (event_temp_data && event_temp_data.event) {
  295. event_temp_data.event.eventPreventDefault();
  296. }
  297. },
  298. get_element_value_f64: (id_ptr, id_len) => {
  299. let id = wasmMemoryInterface.loadString(id_ptr, id_len);
  300. let element = document.getElementById(id);
  301. return element ? element.value : 0;
  302. },
  303. get_element_value_string: (id_ptr, id_len, buf_ptr, buf_len) => {
  304. let id = wasmMemoryInterface.loadString(id_ptr, id_len);
  305. let element = document.getElementById(id);
  306. if (element) {
  307. let str = element.value;
  308. if (buf_len > 0 && buf_ptr) {
  309. let n = Math.min(buf_len, str.length);
  310. str = str.substring(0, n);
  311. this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(str))
  312. return n;
  313. }
  314. }
  315. return 0;
  316. },
  317. get_element_min_max: (ptr_array2_f64, id_ptr, id_len) => {
  318. let id = wasmMemoryInterface.loadString(id_ptr, id_len);
  319. let element = document.getElementById(id);
  320. if (element) {
  321. let values = wasmMemoryInterface.loadF64Array(ptr_array2_f64, 2);
  322. values[0] = element.min;
  323. values[1] = element.max;
  324. }
  325. },
  326. set_element_value: (id_ptr, id_len, value) => {
  327. let id = wasmMemoryInterface.loadString(id_ptr, id_len);
  328. let element = document.getElementById(id);
  329. if (element) {
  330. element.value = value;
  331. }
  332. },
  333. },
  334. };
  335. };
  336. class WebGLInterface {
  337. constructor(wasmMemoryInterface, canvasElement, contextSettings) {
  338. this.wasmMemoryInterface = wasmMemoryInterface;
  339. this.ctxElement = null;
  340. this.ctx = null;
  341. this.ctxVersion = 1.0;
  342. this.counter = 1;
  343. this.lastError = 0;
  344. this.buffers = [];
  345. this.mappedBuffers = {};
  346. this.programs = [];
  347. this.framebuffers = [];
  348. this.renderbuffers = [];
  349. this.textures = [];
  350. this.uniforms = [];
  351. this.shaders = [];
  352. this.vaos = [];
  353. this.contexts = [];
  354. this.currentContext = null;
  355. this.offscreenCanvases = {};
  356. this.timerQueriesEXT = [];
  357. this.queries = [];
  358. this.samplers = [];
  359. this.transformFeedbacks = [];
  360. this.syncs = [];
  361. this.programInfos = {};
  362. this.setCurrentContext(canvasElement, contextSettings);
  363. }
  364. get mem() {
  365. return this.wasmMemoryInterface
  366. }
  367. setCurrentContext(element, contextSettings) {
  368. if (!element) {
  369. return false;
  370. }
  371. if (this.ctxElement == element) {
  372. return true;
  373. }
  374. contextSettings = contextSettings ?? {};
  375. this.ctx = element.getContext("webgl2", contextSettings) || element.getContext("webgl", contextSettings);
  376. if (!this.ctx) {
  377. return false;
  378. }
  379. this.ctxElement = element;
  380. if (this.ctx.getParameter(0x1F02).indexOf("WebGL 2.0") !== -1) {
  381. this.ctxVersion = 2.0;
  382. } else {
  383. this.ctxVersion = 1.0;
  384. }
  385. return true;
  386. }
  387. assertWebGL2() {
  388. if (this.ctxVersion < 2) {
  389. throw new Error("WebGL2 procedure called in a canvas without a WebGL2 context");
  390. }
  391. }
  392. getNewId(table) {
  393. for (var ret = this.counter++, i = table.length; i < ret; i++) {
  394. table[i] = null;
  395. }
  396. return ret;
  397. }
  398. recordError(errorCode) {
  399. this.lastError || (this.lastError = errorCode);
  400. }
  401. populateUniformTable(program) {
  402. let p = this.programs[program];
  403. this.programInfos[program] = {
  404. uniforms: {},
  405. maxUniformLength: 0,
  406. maxAttributeLength: -1,
  407. maxUniformBlockNameLength: -1,
  408. };
  409. for (let ptable = this.programInfos[program], utable = ptable.uniforms, numUniforms = this.ctx.getProgramParameter(p, this.ctx.ACTIVE_UNIFORMS), i = 0; i < numUniforms; ++i) {
  410. let u = this.ctx.getActiveUniform(p, i);
  411. let name = u.name;
  412. if (ptable.maxUniformLength = Math.max(ptable.maxUniformLength, name.length + 1), name.indexOf("]", name.length - 1) !== -1) {
  413. name = name.slice(0, name.lastIndexOf("["));
  414. }
  415. let loc = this.ctx.getUniformLocation(p, name);
  416. if (loc !== null) {
  417. let id = this.getNewId(this.uniforms);
  418. utable[name] = [u.size, id], this.uniforms[id] = loc;
  419. for (let j = 1; j < u.size; ++j) {
  420. let n = name + "[" + j + "]";
  421. let loc = this.ctx.getUniformLocation(p, n);
  422. let id = this.getNewId(this.uniforms);
  423. this.uniforms[id] = loc;
  424. }
  425. }
  426. }
  427. }
  428. getSource(shader, strings_ptr, strings_length) {
  429. const STRING_SIZE = 2*4;
  430. let source = "";
  431. for (let i = 0; i < strings_length; i++) {
  432. let ptr = this.mem.loadPtr(strings_ptr + i*STRING_SIZE);
  433. let len = this.mem.loadPtr(strings_ptr + i*STRING_SIZE + 4);
  434. let str = this.mem.loadString(ptr, len);
  435. source += str;
  436. }
  437. return source;
  438. }
  439. getWebGL1Interface() {
  440. return {
  441. SetCurrentContextById: (name_ptr, name_len) => {
  442. let name = this.mem.loadString(name_ptr, name_len);
  443. let element = document.getElementById(name);
  444. return this.setCurrentContext(element, {alpha: true, antialias: true, depth: true, premultipliedAlpha: true});
  445. },
  446. CreateCurrentContextById: (name_ptr, name_len, attributes) => {
  447. let name = this.mem.loadString(name_ptr, name_len);
  448. let element = document.getElementById(name);
  449. let contextSettings = {
  450. alpha: !(attributes & (1<<0)),
  451. antialias: !(attributes & (1<<1)),
  452. depth: !(attributes & (1<<2)),
  453. failIfMajorPerformanceCaveat: !!(attributes & (1<<3)),
  454. premultipliedAlpha: !(attributes & (1<<4)),
  455. preserveDrawingBuffer: !!(attributes & (1<<5)),
  456. stencil: !!(attributes & (1<<6)),
  457. desynchronized: !!(attributes & (1<<7)),
  458. };
  459. return this.setCurrentContext(element, contextSettings);
  460. },
  461. GetCurrentContextAttributes: () => {
  462. if (!this.ctx) {
  463. return 0;
  464. }
  465. let attrs = this.ctx.getContextAttributes();
  466. let res = 0;
  467. if (!attrs.alpha) res |= 1<<0;
  468. if (!attrs.antialias) res |= 1<<1;
  469. if (!attrs.depth) res |= 1<<2;
  470. if (attrs.failIfMajorPerformanceCaveat) res |= 1<<3;
  471. if (!attrs.premultipliedAlpha) res |= 1<<4;
  472. if (attrs.preserveDrawingBuffer) res |= 1<<5;
  473. if (attrs.stencil) res |= 1<<6;
  474. if (attrs.desynchronized) res |= 1<<7;
  475. return res;
  476. },
  477. DrawingBufferWidth: () => this.ctx.drawingBufferWidth,
  478. DrawingBufferHeight: () => this.ctx.drawingBufferHeight,
  479. IsExtensionSupported: (name_ptr, name_len) => {
  480. let name = this.mem.loadString(name_ptr, name_len);
  481. let extensions = this.ctx.getSupportedExtensions();
  482. return extensions.indexOf(name) !== -1
  483. },
  484. GetError: () => {
  485. let err = this.lastError;
  486. this.recordError(0);
  487. if (err) {
  488. return err;
  489. }
  490. return this.ctx.getError();
  491. },
  492. GetWebGLVersion: (major_ptr, minor_ptr) => {
  493. let version = this.ctx.getParameter(0x1F02);
  494. if (version.indexOf("WebGL 2.0") !== -1) {
  495. this.mem.storeI32(major_ptr, 2);
  496. this.mem.storeI32(minor_ptr, 0);
  497. return;
  498. }
  499. this.mem.storeI32(major_ptr, 1);
  500. this.mem.storeI32(minor_ptr, 0);
  501. },
  502. GetESVersion: (major_ptr, minor_ptr) => {
  503. let version = this.ctx.getParameter(0x1F02);
  504. if (version.indexOf("OpenGL ES 3.0") !== -1) {
  505. this.mem.storeI32(major_ptr, 3);
  506. this.mem.storeI32(minor_ptr, 0);
  507. return;
  508. }
  509. this.mem.storeI32(major_ptr, 2);
  510. this.mem.storeI32(minor_ptr, 0);
  511. },
  512. ActiveTexture: (x) => {
  513. this.ctx.activeTexture(x);
  514. },
  515. AttachShader: (program, shader) => {
  516. this.ctx.attachShader(this.programs[program], this.shaders[shader]);
  517. },
  518. BindAttribLocation: (program, index, name_ptr, name_len) => {
  519. let name = this.mem.loadString(name_ptr, name_len);
  520. this.ctx.bindAttribLocation(this.programs[program], index, name)
  521. },
  522. BindBuffer: (target, buffer) => {
  523. let bufferObj = buffer ? this.buffers[buffer] : null;
  524. if (target == 35051) {
  525. this.ctx.currentPixelPackBufferBinding = buffer;
  526. } else {
  527. if (target == 35052) {
  528. this.ctx.currentPixelUnpackBufferBinding = buffer;
  529. }
  530. this.ctx.bindBuffer(target, bufferObj)
  531. }
  532. },
  533. BindFramebuffer: (target, buffer) => {
  534. // TODO: BindFramebuffer
  535. },
  536. BindTexture: (target, texture) => {
  537. this.ctx.bindTexture(target, texture ? this.textures[texture] : null)
  538. },
  539. BlendColor: (red, green, blue, alpha) => {
  540. this.ctx.blendColor(red, green, blue, alpha);
  541. },
  542. BlendEquation: (mode) => {
  543. this.ctx.blendEquation(mode);
  544. },
  545. BlendFunc: (sfactor, dfactor) => {
  546. this.ctx.blendFunc(sfactor, dfactor);
  547. },
  548. BlendFuncSeparate: (srcRGB, dstRGB, srcAlpha, dstAlpha) => {
  549. this.ctx.blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
  550. },
  551. BufferData: (target, size, data, usage) => {
  552. if (data) {
  553. this.ctx.bufferData(target, this.mem.loadBytes(data, size), usage);
  554. } else {
  555. this.ctx.bufferData(target, size, usage);
  556. }
  557. },
  558. BufferSubData: (target, offset, size, data) => {
  559. if (data) {
  560. this.ctx.bufferSubData(target, offset, this.mem.loadBytes(data, size));
  561. } else {
  562. this.ctx.bufferSubData(target, offset, null);
  563. }
  564. },
  565. Clear: (x) => {
  566. this.ctx.clear(x);
  567. },
  568. ClearColor: (r, g, b, a) => {
  569. this.ctx.clearColor(r, g, b, a);
  570. },
  571. ClearDepth: (x) => {
  572. this.ctx.clearDepth(x);
  573. },
  574. ClearStencil: (x) => {
  575. this.ctx.clearStencil(x);
  576. },
  577. ColorMask: (r, g, b, a) => {
  578. this.ctx.colorMask(!!r, !!g, !!b, !!a);
  579. },
  580. CompileShader: (shader) => {
  581. this.ctx.compileShader(this.shaders[shader]);
  582. },
  583. CompressedTexImage2D: (target, level, internalformat, width, height, border, imageSize, data) => {
  584. if (data) {
  585. this.ctx.compressedTexImage2D(target, level, internalformat, width, height, border, this.mem.loadBytes(data, imageSize));
  586. } else {
  587. this.ctx.compressedTexImage2D(target, level, internalformat, width, height, border, null);
  588. }
  589. },
  590. CompressedTexSubImage2D: (target, level, xoffset, yoffset, width, height, format, imageSize, data) => {
  591. if (data) {
  592. this.ctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, this.mem.loadBytes(data, imageSize));
  593. } else {
  594. this.ctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, null);
  595. }
  596. },
  597. CopyTexImage2D: (target, level, internalformat, x, y, width, height, border) => {
  598. this.ctx.copyTexImage2D(target, level, internalformat, x, y, width, height, border);
  599. },
  600. CopyTexSubImage2D: (target, level, xoffset, yoffset, x, y, width, height) => {
  601. this.ctx.copyTexImage2D(target, level, xoffset, yoffset, x, y, width, height);
  602. },
  603. CreateBuffer: () => {
  604. let buffer = this.ctx.createBuffer();
  605. if (!buffer) {
  606. this.recordError(1282);
  607. return 0;
  608. }
  609. let id = this.getNewId(this.buffers);
  610. buffer.name = id
  611. this.buffers[id] = buffer;
  612. return id;
  613. },
  614. CreateFramebuffer: () => {
  615. let buffer = this.ctx.createFramebuffer();
  616. let id = this.getNewId(this.framebuffers);
  617. buffer.name = id
  618. this.framebuffers[id] = buffer;
  619. return id;
  620. },
  621. CreateProgram: () => {
  622. let program = this.ctx.createProgram();
  623. let id = this.getNewId(this.programs);
  624. program.name = id;
  625. this.programs[id] = program;
  626. return id;
  627. },
  628. CreateRenderbuffer: () => {
  629. let buffer = this.ctx.createRenderbuffer();
  630. let id = this.getNewId(this.renderbuffers);
  631. buffer.name = id;
  632. this.renderbuffers[id] = buffer;
  633. return id;
  634. },
  635. CreateShader: (shaderType) => {
  636. let shader = this.ctx.createShader(shaderType);
  637. let id = this.getNewId(this.shaders);
  638. shader.name = id;
  639. this.shaders[id] = shader;
  640. return id;
  641. },
  642. CreateTexture: () => {
  643. let texture = this.ctx.createTexture();
  644. if (!texture) {
  645. this.recordError(1282)
  646. return 0;
  647. }
  648. let id = this.getNewId(this.textures);
  649. texture.name = id;
  650. this.textures[id] = texture;
  651. return id;
  652. },
  653. CullFace: (mode) => {
  654. this.ctx.cullFace(mode);
  655. },
  656. DeleteBuffer: (id) => {
  657. let obj = this.buffers[id];
  658. if (obj && id != 0) {
  659. this.ctx.deleteBuffer(obj);
  660. this.buffers[id] = null;
  661. }
  662. },
  663. DeleteFramebuffer: (id) => {
  664. let obj = this.framebuffers[id];
  665. if (obj && id != 0) {
  666. this.ctx.deleteFramebuffer(obj);
  667. this.framebuffers[id] = null;
  668. }
  669. },
  670. DeleteProgram: (id) => {
  671. let obj = this.programs[id];
  672. if (obj && id != 0) {
  673. this.ctx.deleteProgram(obj);
  674. this.programs[id] = null;
  675. }
  676. },
  677. DeleteRenderbuffer: (id) => {
  678. let obj = this.renderbuffers[id];
  679. if (obj && id != 0) {
  680. this.ctx.deleteRenderbuffer(obj);
  681. this.renderbuffers[id] = null;
  682. }
  683. },
  684. DeleteShader: (id) => {
  685. let obj = this.shaders[id];
  686. if (obj && id != 0) {
  687. this.ctx.deleteShader(obj);
  688. this.shaders[id] = null;
  689. }
  690. },
  691. DeleteTexture: (id) => {
  692. let obj = this.textures[id];
  693. if (obj && id != 0) {
  694. this.ctx.deleteTexture(obj);
  695. this.textures[id] = null;
  696. }
  697. },
  698. DepthFunc: (func) => {
  699. this.ctx.depthFunc(func);
  700. },
  701. DepthMask: (flag) => {
  702. this.ctx.depthMask(!!flag);
  703. },
  704. DepthRange: (zNear, zFar) => {
  705. this.ctx.depthRange(zNear, zFar);
  706. },
  707. DetachShader: (program, shader) => {
  708. this.ctx.detachShader(this.programs[program], this.shaders[shader]);
  709. },
  710. Disable: (cap) => {
  711. this.ctx.disable(cap);
  712. },
  713. DisableVertexAttribArray: (index) => {
  714. this.ctx.disableVertexAttribArray(index);
  715. },
  716. DrawArrays: (mode, first, count) => {
  717. this.ctx.drawArrays(mode, first, count);
  718. },
  719. DrawElements: (mode, count, type, indices) => {
  720. this.ctx.drawElements(mode, count, type, indices);
  721. },
  722. Enable: (cap) => {
  723. this.ctx.enable(cap);
  724. },
  725. EnableVertexAttribArray: (index) => {
  726. this.ctx.enableVertexAttribArray(index);
  727. },
  728. Finish: () => {
  729. this.ctx.finish();
  730. },
  731. Flush: () => {
  732. this.ctx.flush();
  733. },
  734. FramebufferRenderBuffer: (target, attachment, renderbuffertarget, renderbuffer) => {
  735. this.ctx.framebufferRenderBuffer(target, attachment, renderbuffertarget, this.renderbuffers[renderbuffer]);
  736. },
  737. FramebufferTexture2D: (target, attachment, textarget, texture, level) => {
  738. this.ctx.framebufferTexture2D(target, attachment, textarget, this.textures[texture], level);
  739. },
  740. FrontFace: (mode) => {
  741. this.ctx.frontFace(mode);
  742. },
  743. GenerateMipmap: (target) => {
  744. this.ctx.generateMipmap(target);
  745. },
  746. GetAttribLocation: (program, name_ptr, name_len) => {
  747. let name = this.mem.loadString(name_ptr, name_len);
  748. return this.ctx.getAttribLocation(this.programs[program], name);
  749. },
  750. GetProgramParameter: (program, pname) => {
  751. return this.ctx.getProgramParameter(this.programs[program], pname)
  752. },
  753. GetProgramInfoLog: (program, buf_ptr, buf_len, length_ptr) => {
  754. let log = this.ctx.getProgramInfoLog(this.programs[program]);
  755. if (log === null) {
  756. log = "(unknown error)";
  757. }
  758. if (buf_len > 0 && buf_ptr) {
  759. let n = Math.min(buf_len, log.length);
  760. log = log.substring(0, n);
  761. this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(log))
  762. this.mem.storeInt(length_ptr, n);
  763. }
  764. },
  765. GetShaderInfoLog: (shader, buf_ptr, buf_len, length_ptr) => {
  766. let log = this.ctx.getShaderInfoLog(this.shaders[shader]);
  767. if (log === null) {
  768. log = "(unknown error)";
  769. }
  770. if (buf_len > 0 && buf_ptr) {
  771. let n = Math.min(buf_len, log.length);
  772. log = log.substring(0, n);
  773. this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(log))
  774. this.mem.storeInt(length_ptr, n);
  775. }
  776. },
  777. GetShaderiv: (shader, pname, p) => {
  778. if (p) {
  779. if (pname == 35716) {
  780. let log = this.ctx.getShaderInfoLog(this.shaders[shader]);
  781. if (log === null) {
  782. log = "(unknown error)";
  783. }
  784. this.mem.storeInt(p, log.length+1);
  785. } else if (pname == 35720) {
  786. let source = this.ctx.getShaderSource(this.shaders[shader]);
  787. let sourceLength = (source === null || source.length == 0) ? 0 : source.length+1;
  788. this.mem.storeInt(p, sourceLength);
  789. } else {
  790. let param = this.ctx.getShaderParameter(this.shaders[shader], pname);
  791. this.mem.storeI32(p, param);
  792. }
  793. } else {
  794. this.recordError(1281);
  795. }
  796. },
  797. GetUniformLocation: (program, name_ptr, name_len) => {
  798. let name = this.mem.loadString(name_ptr, name_len);
  799. let arrayOffset = 0;
  800. if (name.indexOf("]", name.length - 1) !== -1) {
  801. let ls = name.lastIndexOf("["),
  802. arrayIndex = name.slice(ls + 1, -1);
  803. if (arrayIndex.length > 0 && (arrayOffset = parseInt(arrayIndex)) < 0) {
  804. return -1;
  805. }
  806. name = name.slice(0, ls)
  807. }
  808. var ptable = this.programInfos[program];
  809. if (!ptable) {
  810. return -1;
  811. }
  812. var uniformInfo = ptable.uniforms[name];
  813. return (uniformInfo && arrayOffset < uniformInfo[0]) ? uniformInfo[1] + arrayOffset : -1
  814. },
  815. GetVertexAttribOffset: (index, pname) => {
  816. return this.ctx.getVertexAttribOffset(index, pname);
  817. },
  818. Hint: (target, mode) => {
  819. this.ctx.hint(target, mode);
  820. },
  821. IsBuffer: (buffer) => this.ctx.isBuffer(this.buffers[buffer]),
  822. IsEnabled: (enabled) => this.ctx.isEnabled(this.enableds[enabled]),
  823. IsFramebuffer: (framebuffer) => this.ctx.isFramebuffer(this.framebuffers[framebuffer]),
  824. IsProgram: (program) => this.ctx.isProgram(this.programs[program]),
  825. IsRenderbuffer: (renderbuffer) => this.ctx.isRenderbuffer(this.renderbuffers[renderbuffer]),
  826. IsShader: (shader) => this.ctx.isShader(this.shaders[shader]),
  827. IsTexture: (texture) => this.ctx.isTexture(this.textures[texture]),
  828. LineWidth: (width) => {
  829. this.ctx.lineWidth(width);
  830. },
  831. LinkProgram: (program) => {
  832. this.ctx.linkProgram(this.programs[program]);
  833. this.programInfos[program] = null;
  834. this.populateUniformTable(program);
  835. },
  836. PixelStorei: (pname, param) => {
  837. this.ctx.pixelStorei(pname, param);
  838. },
  839. PolygonOffset: (factor, units) => {
  840. this.ctx.polygonOffset(factor, units);
  841. },
  842. ReadnPixels: (x, y, width, height, format, type, bufSize, data) => {
  843. this.ctx.readPixels(x, y, width, format, type, this.mem.loadBytes(data, bufSize));
  844. },
  845. RenderbufferStorage: (target, internalformat, width, height) => {
  846. this.ctx.renderbufferStorage(target, internalformat, width, height);
  847. },
  848. SampleCoverage: (value, invert) => {
  849. this.ctx.sampleCoverage(value, !!invert);
  850. },
  851. Scissor: (x, y, width, height) => {
  852. this.ctx.scissor(x, y, width, height);
  853. },
  854. ShaderSource: (shader, strings_ptr, strings_length) => {
  855. let source = this.getSource(shader, strings_ptr, strings_length);
  856. this.ctx.shaderSource(this.shaders[shader], source);
  857. },
  858. StencilFunc: (func, ref, mask) => {
  859. this.ctx.stencilFunc(func, ref, mask);
  860. },
  861. StencilFuncSeparate: (face, func, ref, mask) => {
  862. this.ctx.stencilFuncSeparate(face, func, ref, mask);
  863. },
  864. StencilMask: (mask) => {
  865. this.ctx.stencilMask(mask);
  866. },
  867. StencilMaskSeparate: (face, mask) => {
  868. this.ctx.stencilMaskSeparate(face, mask);
  869. },
  870. StencilOp: (fail, zfail, zpass) => {
  871. this.ctx.stencilOp(fail, zfail, zpass);
  872. },
  873. StencilOpSeparate: (face, fail, zfail, zpass) => {
  874. this.ctx.stencilOpSeparate(face, fail, zfail, zpass);
  875. },
  876. TexImage2D: (target, level, internalformat, width, height, border, format, type, size, data) => {
  877. if (data) {
  878. this.ctx.texImage2D(target, level, internalformat, width, height, border, format, type, this.mem.loadBytes(data, size));
  879. } else {
  880. this.ctx.texImage2D(target, level, internalformat, width, height, border, format, type, null);
  881. }
  882. },
  883. TexParameterf: (target, pname, param) => {
  884. this.ctx.texParameterf(target, pname, param);
  885. },
  886. TexParameteri: (target, pname, param) => {
  887. this.ctx.texParameteri(target, pname, param);
  888. },
  889. TexSubImage2D: (target, level, xoffset, yoffset, width, height, format, type, size, data) => {
  890. this.ctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, this.mem.loadBytes(data, size));
  891. },
  892. Uniform1f: (location, v0) => { this.ctx.uniform1f(this.uniforms[location], v0); },
  893. Uniform2f: (location, v0, v1) => { this.ctx.uniform2f(this.uniforms[location], v0, v1); },
  894. Uniform3f: (location, v0, v1, v2) => { this.ctx.uniform3f(this.uniforms[location], v0, v1, v2); },
  895. Uniform4f: (location, v0, v1, v2, v3) => { this.ctx.uniform4f(this.uniforms[location], v0, v1, v2, v3); },
  896. Uniform1i: (location, v0) => { this.ctx.uniform1i(this.uniforms[location], v0); },
  897. Uniform2i: (location, v0, v1) => { this.ctx.uniform2i(this.uniforms[location], v0, v1); },
  898. Uniform3i: (location, v0, v1, v2) => { this.ctx.uniform3i(this.uniforms[location], v0, v1, v2); },
  899. Uniform4i: (location, v0, v1, v2, v3) => { this.ctx.uniform4i(this.uniforms[location], v0, v1, v2, v3); },
  900. UniformMatrix2fv: (location, addr) => {
  901. let array = this.mem.loadF32Array(addr, 2*2);
  902. this.ctx.uniformMatrix4fv(this.uniforms[location], false, array);
  903. },
  904. UniformMatrix3fv: (location, addr) => {
  905. let array = this.mem.loadF32Array(addr, 3*3);
  906. this.ctx.uniformMatrix4fv(this.uniforms[location], false, array);
  907. },
  908. UniformMatrix4fv: (location, addr) => {
  909. let array = this.mem.loadF32Array(addr, 4*4);
  910. this.ctx.uniformMatrix4fv(this.uniforms[location], false, array);
  911. },
  912. UseProgram: (program) => {
  913. if (program) this.ctx.useProgram(this.programs[program]);
  914. },
  915. ValidateProgram: (program) => {
  916. if (program) this.ctx.validateProgram(this.programs[program]);
  917. },
  918. VertexAttrib1f: (index, x) => {
  919. this.ctx.vertexAttrib1f(index, x);
  920. },
  921. VertexAttrib2f: (index, x, y) => {
  922. this.ctx.vertexAttrib2f(index, x, y);
  923. },
  924. VertexAttrib3f: (index, x, y, z) => {
  925. this.ctx.vertexAttrib3f(index, x, y, z);
  926. },
  927. VertexAttrib4f: (index, x, y, z, w) => {
  928. this.ctx.vertexAttrib4f(index, x, y, z, w);
  929. },
  930. VertexAttribPointer: (index, size, type, normalized, stride, ptr) => {
  931. this.ctx.vertexAttribPointer(index, size, type, !!normalized, stride, ptr);
  932. },
  933. Viewport: (x, y, w, h) => {
  934. this.ctx.viewport(x, y, w, h);
  935. },
  936. };
  937. }
  938. getWebGL2Interface() {
  939. return {
  940. /* Buffer objects */
  941. CopyBufferSubData: (readTarget, writeTarget, readOffset, writeOffset, size) => {
  942. this.assertWebGL2();
  943. this.ctx.copyBufferSubData(readTarget, writeTarget, readOffset, writeOffset, size);
  944. },
  945. GetBufferSubData: (target, srcByteOffset, dst_buffer_ptr, dst_buffer_len, dstOffset, length) => {
  946. this.assertWebGL2();
  947. this.ctx.getBufferSubData(target, srcByteOffset, this.mem.loadBytes(dst_buffer_ptr, dst_buffer_len), dstOffset, length);
  948. },
  949. /* Framebuffer objects */
  950. BlitFramebuffer: (srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter) => {
  951. this.assertWebGL2();
  952. this.ctx.glitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
  953. },
  954. FramebufferTextureLayer: (target, attachment, texture, level, layer) => {
  955. this.assertWebGL2();
  956. this.ctx.framebufferTextureLayer(target, attachment, this.textures[texture], level, layer);
  957. },
  958. InvalidateFramebuffer: (target, attachments_ptr, attachments_len) => {
  959. this.assertWebGL2();
  960. let attachments = this.mem.loadU32Array(attachments_ptr, attachments_len);
  961. this.ctx.invalidateFramebuffer(target, attachments);
  962. },
  963. InvalidateSubFramebuffer: (target, attachments_ptr, attachments_len, x, y, width, height) => {
  964. this.assertWebGL2();
  965. let attachments = this.mem.loadU32Array(attachments_ptr, attachments_len);
  966. this.ctx.invalidateSubFramebuffer(target, attachments, x, y, width, height);
  967. },
  968. ReadBuffer: (src) => {
  969. this.assertWebGL2();
  970. this.ctx.readBuffer(src);
  971. },
  972. /* Renderbuffer objects */
  973. RenderbufferStorageMultisample: (target, samples, internalformat, width, height) => {
  974. this.assertWebGL2();
  975. this.ctx.renderbufferStorageMultisample(target, samples, internalformat, width, height);
  976. },
  977. /* Texture objects */
  978. TexStorage3D: (target, levels, internalformat, width, height, depth) => {
  979. this.assertWebGL2();
  980. this.ctx.texStorage3D(target, level, internalformat, width, heigh, depth);
  981. },
  982. TexImage3D: (target, level, internalformat, width, height, depth, border, format, type, size, data) => {
  983. this.assertWebGL2();
  984. if (data) {
  985. this.ctx.texImage3D(target, level, internalformat, width, height, depth, border, format, type, this.mem.loadBytes(data, size));
  986. } else {
  987. this.ctx.texImage3D(target, level, internalformat, width, height, depth, border, format, type, null);
  988. }
  989. },
  990. TexSubImage3D: (target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, size, data) => {
  991. this.assertWebGL2();
  992. this.ctx.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, this.mem.loadBytes(data, size));
  993. },
  994. CompressedTexImage3D: (target, level, internalformat, width, height, depth, border, imageSize, data) => {
  995. this.assertWebGL2();
  996. if (data) {
  997. this.ctx.compressedTexImage3D(target, level, internalformat, width, height, depth, border, this.mem.loadBytes(data, imageSize));
  998. } else {
  999. this.ctx.compressedTexImage3D(target, level, internalformat, width, height, depth, border, null);
  1000. }
  1001. },
  1002. CompressedTexSubImage3D: (target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data) => {
  1003. this.assertWebGL2();
  1004. if (data) {
  1005. this.ctx.compressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, this.mem.loadBytes(data, imageSize));
  1006. } else {
  1007. this.ctx.compressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, null);
  1008. }
  1009. },
  1010. CopyTexSubImage3D: (target, level, xoffset, yoffset, zoffset, x, y, width, height) => {
  1011. this.assertWebGL2();
  1012. this.ctx.copyTexImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height);
  1013. },
  1014. /* Programs and shaders */
  1015. GetFragDataLocation: (program, name_ptr, name_len) => {
  1016. this.assertWebGL2();
  1017. return this.ctx.getFragDataLocation(this.programs[program], this.mem.loadString(name_ptr, name_len));
  1018. },
  1019. /* Uniforms */
  1020. Uniform1ui: (location, v0) => {
  1021. this.assertWebGL2();
  1022. this.ctx.uniform1ui(this.uniforms[location], v0);
  1023. },
  1024. Uniform2ui: (location, v0, v1) => {
  1025. this.assertWebGL2();
  1026. this.ctx.uniform2ui(this.uniforms[location], v0, v1);
  1027. },
  1028. Uniform3ui: (location, v0, v1, v2) => {
  1029. this.assertWebGL2();
  1030. this.ctx.uniform3ui(this.uniforms[location], v0, v1, v2);
  1031. },
  1032. Uniform4ui: (location, v0, v1, v2, v3) => {
  1033. this.assertWebGL2();
  1034. this.ctx.uniform4ui(this.uniforms[location], v0, v1, v2, v3);
  1035. },
  1036. UniformMatrix3x2fv: (location, addr) => {
  1037. this.assertWebGL2();
  1038. let array = this.mem.loadF32Array(addr, 3*2);
  1039. this.ctx.uniformMatrix3x2fv(this.uniforms[location], false, array);
  1040. },
  1041. UniformMatrix4x2fv: (location, addr) => {
  1042. this.assertWebGL2();
  1043. let array = this.mem.loadF32Array(addr, 4*2);
  1044. this.ctx.uniformMatrix4x2fv(this.uniforms[location], false, array);
  1045. },
  1046. UniformMatrix2x3fv: (location, addr) => {
  1047. this.assertWebGL2();
  1048. let array = this.mem.loadF32Array(addr, 2*3);
  1049. this.ctx.uniformMatrix2x3fv(this.uniforms[location], false, array);
  1050. },
  1051. UniformMatrix4x3fv: (location, addr) => {
  1052. this.assertWebGL2();
  1053. let array = this.mem.loadF32Array(addr, 4*3);
  1054. this.ctx.uniformMatrix4x3fv(this.uniforms[location], false, array);
  1055. },
  1056. UniformMatrix2x4fv: (location, addr) => {
  1057. this.assertWebGL2();
  1058. let array = this.mem.loadF32Array(addr, 2*4);
  1059. this.ctx.uniformMatrix2x4fv(this.uniforms[location], false, array);
  1060. },
  1061. UniformMatrix3x4fv: (location, addr) => {
  1062. this.assertWebGL2();
  1063. let array = this.mem.loadF32Array(addr, 3*4);
  1064. this.ctx.uniformMatrix3x4fv(this.uniforms[location], false, array);
  1065. },
  1066. /* Vertex attribs */
  1067. VertexAttribI4i: (index, x, y, z, w) => {
  1068. this.assertWebGL2();
  1069. this.ctx.vertexAttribI4i(index, x, y, z, w);
  1070. },
  1071. VertexAttribI4ui: (index, x, y, z, w) => {
  1072. this.assertWebGL2();
  1073. this.ctx.vertexAttribI4ui(index, x, y, z, w);
  1074. },
  1075. VertexAttribIPointer: (index, size, type, stride, offset) => {
  1076. this.assertWebGL2();
  1077. this.ctx.vertexAttribIPointer(index, size, type, stride, offset);
  1078. },
  1079. /* Writing to the drawing buffer */
  1080. VertexAttribDivisor: (index, divisor) => {
  1081. this.assertWebGL2();
  1082. this.ctx.vertexAttribDivisor(index, divisor);
  1083. },
  1084. DrawArraysInstanced: (mode, first, count, instanceCount) => {
  1085. this.assertWebGL2();
  1086. this.ctx.drawArraysInstanced(mode, first, count, instanceCount);
  1087. },
  1088. DrawElementsInstanced: (mode, count, type, offset, instanceCount) => {
  1089. this.assertWebGL2();
  1090. this.ctx.drawElementsInstanced(mode, count, type, offset, instanceCount);
  1091. },
  1092. DrawRangeElements: (mode, start, end, count, type, offset) => {
  1093. this.assertWebGL2();
  1094. this.ctx.drawRangeElements(mode, start, end, count, type, offset);
  1095. },
  1096. /* Multiple Render Targets */
  1097. DrawBuffers: (buffers_ptr, buffers_len) => {
  1098. this.assertWebGL2();
  1099. let array = this.mem.loadU32Array(buffers_ptr, buffers_len);
  1100. this.ctx.drawBuffers(array);
  1101. },
  1102. ClearBufferfv: (buffer, drawbuffer, values_ptr, values_len) => {
  1103. this.assertWebGL2();
  1104. let array = this.mem.loadF32Array(values_ptr, values_len);
  1105. this.ctx.clearBufferfv(buffer, drawbuffer, array);
  1106. },
  1107. ClearBufferiv: (buffer, drawbuffer, values_ptr, values_len) => {
  1108. this.assertWebGL2();
  1109. let array = this.mem.loadI32Array(values_ptr, values_len);
  1110. this.ctx.clearBufferiv(buffer, drawbuffer, array);
  1111. },
  1112. ClearBufferuiv: (buffer, drawbuffer, values_ptr, values_len) => {
  1113. this.assertWebGL2();
  1114. let array = this.mem.loadU32Array(values_ptr, values_len);
  1115. this.ctx.clearBufferuiv(buffer, drawbuffer, array);
  1116. },
  1117. ClearBufferfi: (buffer, drawbuffer, depth, stencil) => {
  1118. this.assertWebGL2();
  1119. this.ctx.clearBufferfi(buffer, drawbuffer, depth, stencil);
  1120. },
  1121. /* Query Objects */
  1122. CreateQuery: () => {
  1123. this.assertWebGL2();
  1124. let query = this.ctx.createQuery();
  1125. let id = this.getNewId(this.queries);
  1126. query.name = id;
  1127. this.queries[id] = query;
  1128. return id;
  1129. },
  1130. DeleteQuery: (id) => {
  1131. this.assertWebGL2();
  1132. let obj = this.querys[id];
  1133. if (obj && id != 0) {
  1134. this.ctx.deleteQuery(obj);
  1135. this.querys[id] = null;
  1136. }
  1137. },
  1138. IsQuery: (query) => {
  1139. this.assertWebGL2();
  1140. return this.ctx.isQuery(this.queries[query]);
  1141. },
  1142. BeginQuery: (target, query) => {
  1143. this.assertWebGL2();
  1144. this.ctx.beginQuery(target, this.queries[query])
  1145. },
  1146. EndQuery: (target) => {
  1147. this.assertWebGL2();
  1148. this.ctx.endQuery(target);
  1149. },
  1150. GetQuery: (target, pname) => {
  1151. this.assertWebGL2();
  1152. let query = this.ctx.getQuery(target, pname);
  1153. if (!query) {
  1154. return 0;
  1155. }
  1156. if (this.queries.indexOf(query) !== -1) {
  1157. return query.name;
  1158. }
  1159. let id = this.getNewId(this.queries);
  1160. query.name = id;
  1161. this.queries[id] = query;
  1162. return id;
  1163. },
  1164. /* Sampler Objects */
  1165. CreateSampler: () => {
  1166. this.assertWebGL2();
  1167. let sampler = this.ctx.createSampler();
  1168. let id = this.getNewId(this.samplers);
  1169. sampler.name = id;
  1170. this.samplers[id] = sampler;
  1171. return id;
  1172. },
  1173. DeleteSampler: (id) => {
  1174. this.assertWebGL2();
  1175. let obj = this.samplers[id];
  1176. if (obj && id != 0) {
  1177. this.ctx.deleteSampler(obj);
  1178. this.samplers[id] = null;
  1179. }
  1180. },
  1181. IsSampler: (sampler) => {
  1182. this.assertWebGL2();
  1183. return this.ctx.isSampler(this.samplers[sampler]);
  1184. },
  1185. BindSampler: (unit, sampler) => {
  1186. this.assertWebGL2();
  1187. this.ctx.bindSampler(unit, this.samplers[Sampler]);
  1188. },
  1189. SamplerParameteri: (sampler, pname, param) => {
  1190. this.assertWebGL2();
  1191. this.ctx.samplerParameteri(this.samplers[sampler], pname, param);
  1192. },
  1193. SamplerParameterf: (sampler, pname, param) => {
  1194. this.assertWebGL2();
  1195. this.ctx.samplerParameterf(this.samplers[sampler], pname, param);
  1196. },
  1197. /* Sync objects */
  1198. FenceSync: (condition, flags) => {
  1199. this.assertWebGL2();
  1200. let sync = this.ctx.fenceSync(condition, flags);
  1201. let id = this.getNewId(this.syncs);
  1202. sync.name = id;
  1203. this.syncs[id] = sync;
  1204. return id;
  1205. },
  1206. IsSync: (sync) => {
  1207. this.assertWebGL2();
  1208. return this.ctx.isSync(this.syncs[sync]);
  1209. },
  1210. DeleteSync: (id) => {
  1211. this.assertWebGL2();
  1212. let obj = this.syncs[id];
  1213. if (obj && id != 0) {
  1214. this.ctx.deleteSampler(obj);
  1215. this.syncs[id] = null;
  1216. }
  1217. },
  1218. ClientWaitSync: (sync, flags, timeout) => {
  1219. this.assertWebGL2();
  1220. return this.ctx.clientWaitSync(this.syncs[sync], flags, timeout);
  1221. },
  1222. WaitSync: (sync, flags, timeout) => {
  1223. this.assertWebGL2();
  1224. this.ctx.waitSync(this.syncs[sync], flags, timeout) ;
  1225. },
  1226. /* Transform Feedback */
  1227. CreateTransformFeedback: () => {
  1228. this.assertWebGL2();
  1229. let transformFeedback = this.ctx.createtransformFeedback();
  1230. let id = this.getNewId(this.transformFeedbacks);
  1231. transformFeedback.name = id;
  1232. this.transformFeedbacks[id] = transformFeedback;
  1233. return id;
  1234. },
  1235. DeleteTransformFeedback: (id) => {
  1236. this.assertWebGL2();
  1237. let obj = this.transformFeedbacks[id];
  1238. if (obj && id != 0) {
  1239. this.ctx.deleteTransformFeedback(obj);
  1240. this.transformFeedbacks[id] = null;
  1241. }
  1242. },
  1243. IsTransformFeedback: (tf) => {
  1244. this.assertWebGL2();
  1245. return this.ctx.isTransformFeedback(this.transformFeedbacks[tf]);
  1246. },
  1247. BindTransformFeedback: (target, tf) => {
  1248. this.assertWebGL2();
  1249. this.ctx.bindTransformFeedback(target, this.transformFeedbacks[tf]);
  1250. },
  1251. BeginTransformFeedback: (primitiveMode) => {
  1252. this.assertWebGL2();
  1253. this.ctx.beginTransformFeedback(primitiveMode);
  1254. },
  1255. EndTransformFeedback: () => {
  1256. this.assertWebGL2();
  1257. this.ctx.endTransformFeedback();
  1258. },
  1259. TransformFeedbackVaryings: (program, varyings_ptr, varyings_len, bufferMode) => {
  1260. this.assertWebGL2();
  1261. let varyings = [];
  1262. for (let i = 0; i < varyings_len; i++) {
  1263. let ptr = this.mem.loadPtr(varyings_ptr + i*STRING_SIZE + 0*4);
  1264. let len = this.mem.loadPtr(varyings_ptr + i*STRING_SIZE + 1*4);
  1265. varyings.push(this.mem.loadString(ptr, len));
  1266. }
  1267. this.ctx.transformFeedbackVaryings(this.programs[program], varyings, bufferMode);
  1268. },
  1269. PauseTransformFeedback: () => {
  1270. this.assertWebGL2();
  1271. this.ctx.pauseTransformFeedback();
  1272. },
  1273. ResumeTransformFeedback: () => {
  1274. this.assertWebGL2();
  1275. this.ctx.resumeTransformFeedback();
  1276. },
  1277. /* Uniform Buffer Objects and Transform Feedback Buffers */
  1278. BindBufferBase: (target, index, buffer) => {
  1279. this.assertWebGL2();
  1280. this.ctx.bindBufferBase(target, index, this.buffers[buffer]);
  1281. },
  1282. BindBufferRange: (target, index, buffer, offset, size) => {
  1283. this.assertWebGL2();
  1284. this.ctx.bindBufferRange(target, index, this.buffers[buffer], offset, size);
  1285. },
  1286. GetUniformBlockIndex: (program, uniformBlockName_ptr, uniformBlockName_len) => {
  1287. this.assertWebGL2();
  1288. return this.ctx.getUniformBlockIndex(this.programs[program], this.mem.loadString(uniformBlockName_ptr, uniformBlockName_len));
  1289. },
  1290. // any getActiveUniformBlockParameter(WebGLProgram program, GLuint uniformBlockIndex, GLenum pname);
  1291. GetActiveUniformBlockName: (program, uniformBlockIndex, buf_ptr, buf_len, length_ptr) => {
  1292. this.assertWebGL2();
  1293. let name = this.ctx.getActiveUniformBlockName(this.programs[program], uniformBlockIndex);
  1294. let n = Math.min(buf_len, name.length);
  1295. name = name.substring(0, n);
  1296. this.mem.loadBytes(buf_ptr, buf_len).set(new TextEncoder("utf-8").encode(name))
  1297. this.mem.storeInt(length_ptr, n);
  1298. },
  1299. UniformBlockBinding: (program, uniformBlockIndex, uniformBlockBinding) => {
  1300. this.assertWebGL2();
  1301. this.ctx.uniformBlockBinding(this.programs[program], uniformBlockIndex, uniformBlockBinding);
  1302. },
  1303. /* Vertex Array Objects */
  1304. CreateVertexArray: () => {
  1305. this.assertWebGL2();
  1306. let vao = this.ctx.createVertexArray();
  1307. let id = this.getNewId(this.vaos);
  1308. vao.name = id;
  1309. this.vaos[id] = vao;
  1310. return id;
  1311. },
  1312. DeleteVertexArray: (id) => {
  1313. this.assertWebGL2();
  1314. let obj = this.vaos[id];
  1315. if (obj && id != 0) {
  1316. this.ctx.deleteVertexArray(obj);
  1317. this.vaos[id] = null;
  1318. }
  1319. },
  1320. IsVertexArray: (vertexArray) => {
  1321. this.assertWebGL2();
  1322. return this.ctx.isVertexArray(this.vaos[vertexArray]);
  1323. },
  1324. BindVertexArray: (vertexArray) => {
  1325. this.assertWebGL2();
  1326. this.ctx.bindVertexArray(this.vaos[vertexArray]);
  1327. },
  1328. };
  1329. }
  1330. };
  1331. export {WasmMemoryInterface, odinSetupDefaultImports, WebGLInterface};