node-sdl
node-sdl copied to clipboard
Segmentation fault in @kmamal/gl
(Opening this issue here because I'm using it with @kmamal/sdl
, and with the gl
repo being a fork, it has issues disabled, probably unintentionally?)
My process just segfaulted after a GLSL compilation error; this may very possibly be caused by my somewhat janky hot-reloading setup, but I was unable to determine this from the stacktrace, so I'm posting the stacktrace here in case it's a gl
bug.
The error output in terminal:
[nix-shell:~/projects/breakout]$ node bootstrap.js
file number 0: /home/sven/projects/breakout/game.js:27:24
1|
2| attribute vec2 position;
3| varying vec3 color;
4| uniform float u_step;
5|
6| void main() {
7| gl_Position = vec4(position, 0, 1);
8| float red = mix(0, 1, abs(sin(u_step + x)));
9| float blue = mix(1.0, 0.0, abs(sin(u_step + y)));
| ^^^
| undeclared identifier
| ^^^
| no matching overloaded function found
|
10| color = vec3(position, 0);
| ^^^
| undeclared identifier
|
11| }
Error: (regl) Error compiling vertex shader, /home/sven/projects/breakout/game.js:27:24
at Function.raise (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:47:15)
at Function.checkShaderError [as shaderError] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:339:11)
at Object.getShader [as shader] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:5499:15)
at /home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:6882:23
at Function.checkOptional [as optional] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:432:3)
at parseShader (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:6881:17)
at parseProgram (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:6908:16)
at parseArguments (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:8115:18)
at Object.compileCommand [as compile] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:9628:16)
at compileProcedure (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:10253:25)
ERROR: Error: (regl) Error compiling vertex shader, /home/sven/projects/breakout/game.js:27:24
at Function.raise (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:47:15)
at Function.checkShaderError [as shaderError] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:339:11)
at Object.getShader [as shader] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:5499:15)
at /home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:6882:23
at Function.checkOptional [as optional] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:432:3)
at parseShader (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:6881:17)
at parseProgram (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:6908:16)
at parseArguments (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:8115:18)
at Object.compileCommand [as compile] (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:9628:16)
at compileProcedure (/home/sven/projects/breakout/node_modules/.pnpm/[email protected]/node_modules/regl/dist/regl.js:10253:25)
Segmentation fault (core dumped)
Coredump/stacktrace:
PID: 1470991 (node)
UID: 1000 (sven)
GID: 100 (users)
Signal: 11 (SEGV)
Timestamp: Thu 2024-07-25 18:30:38 CEST (27s ago)
Command Line: node bootstrap.js
Executable: /nix/store/mhbh2l1rsxpvq1czhjiqcpnmahbgvql9-nodejs-18.20.3/bin/node
Control Group: /user.slice/user-1000.slice/[email protected]/app.slice/app-code-d4a27b81b39d4b9fae15ea3e6cb66fb4.scope
Unit: [email protected]
User Unit: app-code-d4a27b81b39d4b9fae15ea3e6cb66fb4.scope
Slice: user-1000.slice
Owner UID: 1000 (sven)
Boot ID: 29c15c031a8f4798a5ce62ea3a095191
Machine ID: 971ecd159ab34baf85eea76d9bda063f
Hostname: desktop-home
Storage: /var/lib/systemd/coredump/core.node.1000.29c15c031a8f4798a5ce62ea3a095191.1470991.1721925038000000.zst (present)
Size on Disk: 7.4M
Message: Process 1470991 (node) of user 1000 dumped core.
Module libpciaccess.so.0 without build-id.
Module libbz2.so.1 without build-id.
Module liblzma.so.5 without build-id.
Module libxml2.so.2 without build-id.
Module libncursesw.so.6 without build-id.
Module libffi.so.8 without build-id.
Module libdrm_intel.so.1 without build-id.
Module libdrm_nouveau.so.2 without build-id.
Module libdrm_amdgpu.so.1 without build-id.
Module libdrm_radeon.so.1 without build-id.
Module libsensors.so.5 without build-id.
Module libzstd.so.1 without build-id.
Module libxcb-xfixes.so.0 without build-id.
Module libxcb-sync.so.1 without build-id.
Module libxcb-present.so.0 without build-id.
Module libxcb-dri3.so.0 without build-id.
Module libxcb-randr.so.0 without build-id.
Module libxshmfence.so.1 without build-id.
Module libexpat.so.1 without build-id.
Module libxcb-shm.so.0 without build-id.
Module libXxf86vm.so.1 without build-id.
Module libXfixes.so.3 without build-id.
Module libxcb-dri2.so.0 without build-id.
Module libX11-xcb.so.1 without build-id.
Module libxcb-glx.so.0 without build-id.
Module libdrm.so.2 without build-id.
Module libGLdispatch.so.0 without build-id.
Module libGLX.so.0 without build-id.
Module libGL.so.1 without build-id.
Module libXext.so.6 without build-id.
Module libXdmcp.so.6 without build-id.
Module libXau.so.6 without build-id.
Module libxcb.so.1 without build-id.
Module libX11.so.6 without build-id.
Module libcap.so.2 without build-id.
Module libudev.so.1 without build-id.
Module libudev.so.0 without build-id.
Module libgcc_s.so.1 without build-id.
Module libstdc++.so.6 without build-id.
Module libicudata.so.73 without build-id.
Module libicuuc.so.73 without build-id.
Module libicui18n.so.73 without build-id.
Module libz.so.1 without build-id.
Module node without build-id.
Stack trace of thread 1470991:
#0 0x00007f7a45eb98f9 dri2_destroy_image (radeonsi_dri.so + 0xb98f9)
#1 0x00007f7a6011b99c dri3_free_render_buffer (libGLX_mesa.so.0 + 0x5199c)
#2 0x00007f7a6011ceed loader_dri3_drawable_fini (libGLX_mesa.so.0 + 0x52eed)
#3 0x00007f7a6010ef6d dri3_destroy_drawable (libGLX_mesa.so.0 + 0x44f6d)
#4 0x00007f7a6010282c DestroyDRIDrawable (libGLX_mesa.so.0 + 0x3882c)
#5 0x00007f7a4d936631 n/a (/home/sven/projects/breakout/node_modules/.pnpm/@[email protected]/node_modules/@kmamal/gl/build/Release/webgl.node + 0x136631)
#6 0x00007f7a4d936686 n/a (/home/sven/projects/breakout/node_modules/.pnpm/@[email protected]/node_modules/@kmamal/gl/build/Release/webgl.node + 0x136686)
#7 0x00007f7a4d90ef16 n/a (/home/sven/projects/breakout/node_modules/.pnpm/@[email protected]/node_modules/@kmamal/gl/build/Release/webgl.node + 0x10ef16)
#8 0x00007f7a4d8e71b0 n/a (/home/sven/projects/breakout/node_modules/.pnpm/@[email protected]/node_modules/@kmamal/gl/build/Release/webgl.node + 0xe71b0)
#9 0x00007f7a4d8abfdc n/a (/home/sven/projects/breakout/node_modules/.pnpm/@[email protected]/node_modules/@kmamal/gl/build/Release/webgl.node + 0xabfdc)
#10 0x00007f7a4d895557 n/a (/home/sven/projects/breakout/node_modules/.pnpm/@[email protected]/node_modules/@kmamal/gl/build/Release/webgl.node + 0x95557)
#11 0x00007f7a4d88bf8f n/a (/home/sven/projects/breakout/node_modules/.pnpm/@[email protected]/node_modules/@kmamal/gl/build/Release/webgl.node + 0x8bf8f)
#12 0x0000000000d281ce _ZN2v88internal25FunctionCallbackArguments4CallENS0_15CallHandlerInfoE (node + 0x9281ce)
#13 0x0000000000d287f1 _ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb0EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEESA_NS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EENS0_16BuiltinArgumentsE (node + 0x9287f1)
#14 0x0000000000d290e7 _ZN2v88internal21Builtin_HandleApiCallEiPmPNS0_7IsolateE (node + 0x9290e7)
#15 0x00000000016c4479 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit (node + 0x12c4479)
#16 0x0000000001648390 Builtins_InterpreterEntryTrampoline (node + 0x1248390)
#17 0x0000000001648390 Builtins_InterpreterEntryTrampoline (node + 0x1248390)
#18 0x000000000164699c Builtins_JSEntryTrampoline (node + 0x124699c)
#19 0x00000000016466c3 Builtins_JSEntry (node + 0x12466c3)
#20 0x0000000000e110a1 _ZN2v88internal12_GLOBAL__N_16InvokeEPNS0_7IsolateERKNS1_12InvokeParamsE (node + 0xa110a1)
#21 0x0000000000e12194 _ZN2v88internal9Execution4CallEPNS0_7IsolateENS0_6HandleINS0_6ObjectEEES6_iPS6_ (node + 0xa12194)
#22 0x0000000000ce699b _ZN2v88Function4CallENS_5LocalINS_7ContextEEENS1_INS_5ValueEEEiPS5_ (node + 0x8e699b)
#23 0x0000000000ac45a9 _ZN4node6errors24TriggerUncaughtExceptionEPN2v87IsolateENS1_5LocalINS1_5ValueEEENS4_INS1_7MessageEEEb (node + 0x6c45a9)
#24 0x0000000000e3dd94 _ZN2v88internal14MessageHandler25ReportMessageNoExceptionsEPNS0_7IsolateEPKNS0_15MessageLocationENS0_6HandleINS0_6ObjectEEENS_5LocalINS_5ValueEEE (node + 0xa3dd94)
#25 0x0000000000e3e010 _ZN2v88internal14MessageHandler13ReportMessageEPNS0_7IsolateEPKNS0_15MessageLocationENS0_6HandleINS0_15JSMessageObjectEEE (node + 0xa3e010)
#26 0x0000000000e2af2e _ZN2v88internal7Isolate21ReportPendingMessagesEv (node + 0xa2af2e)
#27 0x0000000000e114e8 _ZN2v88internal12_GLOBAL__N_16InvokeEPNS0_7IsolateERKNS1_12InvokeParamsE (node + 0xa114e8)
#28 0x0000000000e12194 _ZN2v88internal9Execution4CallEPNS0_7IsolateENS0_6HandleINS0_6ObjectEEES6_iPS6_ (node + 0xa12194)
#29 0x0000000000ce699b _ZN2v88Function4CallENS_5LocalINS_7ContextEEENS1_INS_5ValueEEEiPS5_ (node + 0x8e699b)
#30 0x00000000009e4a07 _ZN4node21InternalCallbackScope5CloseEv (node + 0x5e4a07)
#31 0x00000000009e4aa1 _ZN4node21InternalCallbackScopeD1Ev (node + 0x5e4aa1)
#32 0x0000000000a7607c _ZN4node14StartExecutionEPNS_11EnvironmentESt8functionIFN2v810MaybeLocalINS3_5ValueEEERKNS_26StartExecutionCallbackInfoEEE (node + 0x67607c)
#33 0x00000000009e9a88 _ZN4node15LoadEnvironmentEPNS_11EnvironmentESt8functionIFN2v810MaybeLocalINS3_5ValueEEERKNS_26StartExecutionCallbackInfoEEE (node + 0x5e9a88)
#34 0x0000000000b0d25d _ZN4node16NodeMainInstance3RunEPiPNS_11EnvironmentE (node + 0x70d25d)
#35 0x0000000000b0d6cb _ZN4node16NodeMainInstance3RunEv (node + 0x70d6cb)
#36 0x0000000000a759c6 _ZN4node22LoadSnapshotDataAndRunEPPKNS_12SnapshotDataEPKNS_20InitializationResultE (node + 0x6759c6)
#37 0x0000000000a79c55 _ZN4node5StartEiPPc (node + 0x679c55)
#38 0x00007f7a6263d10e __libc_start_call_main (libc.so.6 + 0x2a10e)
#39 0x00007f7a6263d1c9 __libc_start_main@@GLIBC_2.34 (libc.so.6 + 0x2a1c9)
#40 0x00000000009e22b5 _start (node + 0x5e22b5)
Stack trace of thread 1470996:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a65a26569 uv_cond_wait (libuv.so.1 + 0x1f569)
#3 0x0000000000b38283 _ZN4node12_GLOBAL__N_1L20PlatformWorkerThreadEPv (node + 0x738283)
#4 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#5 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1470995:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a65a26569 uv_cond_wait (libuv.so.1 + 0x1f569)
#3 0x0000000000b38283 _ZN4node12_GLOBAL__N_1L20PlatformWorkerThreadEPv (node + 0x738283)
#4 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#5 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471002:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1470993:
#0 0x00007f7a6271d086 epoll_pwait (libc.so.6 + 0x10a086)
#1 0x00007f7a65a2b140 uv__io_poll (libuv.so.1 + 0x24140)
#2 0x00007f7a65a18910 uv_run (libuv.so.1 + 0x11910)
#3 0x0000000000b3d2b6 _ZZN4node23WorkerThreadsTaskRunner20DelayedTaskScheduler5StartEvENUlPvE_4_FUNES2_ (node + 0x73d2b6)
#4 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#5 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471003:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471000:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a8e80 __new_sem_wait_slow64.constprop.0 (libc.so.6 + 0x95e80)
#2 0x00007f7a65a265f2 uv_sem_wait (libuv.so.1 + 0x1f5f2)
#3 0x0000000000be4981 _ZN4node9inspector12_GLOBAL__N_117StartIoThreadMainEPv (node + 0x7e4981)
#4 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#5 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471004:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471005:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471020:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471023:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471026:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1471006:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a45f175e9 cnd_wait (radeonsi_dri.so + 0x1175e9)
#3 0x00007f7a45ef79db util_queue_thread_func (radeonsi_dri.so + 0xf79db)
#4 0x00007f7a45f17527 impl_thrd_routine (radeonsi_dri.so + 0x117527)
#5 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#6 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1470997:
#0 0x00007f7a6269dc5e __futex_abstimed_wait_common (libc.so.6 + 0x8ac5e)
#1 0x00007f7a626a04c0 pthread_cond_wait@@GLIBC_2.3.2 (libc.so.6 + 0x8d4c0)
#2 0x00007f7a65a26569 uv_cond_wait (libuv.so.1 + 0x1f569)
#3 0x0000000000b38283 _ZN4node12_GLOBAL__N_1L20PlatformWorkerThreadEPv (node + 0x738283)
#4 0x00007f7a626a1272 start_thread (libc.so.6 + 0x8e272)
#5 0x00007f7a6271cdec __clone3 (libc.so.6 + 0x109dec)
Stack trace of thread 1470992:
#0 0x0000000000000000 n/a (n/a + 0x0)
ELF object binary architecture: AMD x86-64
bootstrap.js
, which is the wrapper that handles hot-reloading:
"use strict";
const sdl = require("@kmamal/sdl");
const createContext = require("@kmamal/gl");
const chokidar = require("chokidar");
const clearModule = require("clear-module");
const debounce = require("debounce");
const path = require("path");
const window = sdl.video.createWindow({ title: "WebGL", opengl: true, resizable: true });
const { pixelWidth: width, pixelHeight: height, native } = window;
const gl = createContext(width, height, { window: native });
let game;
let triggerReload = debounce(() => {
console.log("File(s) changed, triggering reload...");
loadGame();
}, 100);
function initialize(gameModule) {
clearModule.all();
game = require(gameModule);
game.load({ window, gl });
}
function loadGame() {
let startTime = Date.now();
if (game != null) {
game.unload();
}
try {
initialize("./game");
} catch (error) {
let timePassed = Date.now() - startTime;
console.error("ERROR:", error);
// At least 5 seconds must have passed since start, otherwise we fail to an error screen
if (timePassed > 5000) {
console.error("Restarting...");
loadGame();
} else {
initialize("./fallback-error");
}
}
}
loadGame();
let watcher = chokidar.watch(path.join(__dirname, `**/*.{js,glsl}`), {
ignoreInitial: true,
awaitWriteFinish: { stabilityThreshold: 500 }
});
watcher.on("change", (path) => {
console.log(`file changed: ${path}`);
triggerReload();
});
watcher.on("add", () => {
console.log(`file added: ${path}`);
triggerReload();
});
game.js
, which is the actual file I'm working in:
"use strict";
const reglModule = require("regl");
const startTimedLoop = require("./loops/timed");
const startRenderLoop = require("./loops/render");
let renderLoop, gameLoop, windowRef;
module.exports = {
load: function ({ gl, window }) {
windowRef = window;
const glResize = gl.getExtension("STACKGL_resize_drawingbuffer");
window.on("resize", ({ pixelWidth, pixelHeight, width, height }) => {
glResize.resize(pixelWidth, pixelHeight);
gl.viewport(0, 0, width, height);
gl.swap();
render();
});
let regl = reglModule(gl);
let step = 0;
const drawTriangle = regl({
vert: `
attribute vec2 position;
varying vec3 color;
uniform float u_step;
void main() {
gl_Position = vec4(position, 0, 1);
float red = mix(0, 1, abs(sin(u_step + x)));
float blue = mix(1.0, 0.0, abs(sin(u_step + y)));
color = vec3(position, 0);
}`,
frag: `
precision mediump float;
varying vec3 color;
void main() {
gl_FragColor = vec4(color, 1);
}`,
attributes: {
position: [[0, -1], [-1, 0], [1, 1]]
},
uniforms: {
u_step: () => step++
},
count: 3
});
gameLoop = startTimedLoop(30, () => {
// Nothing yet
});
// Defined separately because it can also be invoked by eg. resizes
function render() {
regl.clear({
color: [ 0, 0, 0, 1 ],
depth: 1,
stencil: 0
});
drawTriangle();
gl.swap();
}
renderLoop = startRenderLoop(render);
},
unload: function () {
if (gameLoop != null) {
gameLoop.stop();
}
if (renderLoop != null) {
renderLoop.stop();
}
if (windowRef != null) {
windowRef.removeEventListener("resize");
}
}
};
For the sake of completeness, the fallback-error.js
for error-after-start conditions:
"use strict";
const reglModule = require("regl");
module.exports = {
load: function ({ gl, window }) {
let regl = reglModule(gl);
const drawError = regl({
vert: `
attribute vec2 position;
varying vec3 color;
void main() {
gl_Position = vec4(position, 0, 1);
}`,
frag: `
precision mediump float;
void main() {
gl_FragColor = vec4(1, 0, 0, 1);
}`,
attributes: {
position: [[-1, -1], [-1, 1], [1, -1]]
},
count: 3
});
regl.clear({
color: [ 0, 0, 0, 1 ],
depth: 1,
stencil: 0
});
drawError();
gl.swap();
},
unload: function () {}
};