runtime.js 53 KB

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